1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1988, 1993, 1994, 1997, 1998, 2000, 2002,
4 ;; 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This facility is documented in the Emacs Manual.
35 (defgroup change-log nil
36 "Change log maintenance."
38 :link '(custom-manual "(emacs)Change Log")
43 (defcustom change-log-default-name nil
44 "*Name of a change log file for \\[add-change-log-entry]."
45 :type '(choice (const :tag "default" nil)
48 (put 'change-log-default-name 'safe-local-variable
49 (lambda (a) (or (stringp a) (null a))))
51 (defcustom change-log-mode-hook nil
52 "Normal hook run by `change-log-mode'."
56 ;; Many modes set this variable, so avoid warnings.
58 (defcustom add-log-current-defun-function nil
59 "*If non-nil, function to guess name of surrounding function.
60 It is used by `add-log-current-defun' in preference to built-in rules.
61 Returns function's name as a string, or nil if outside a function."
62 :type '(choice (const nil) function)
66 (defcustom add-log-full-name nil
67 "*Full name of user, for inclusion in ChangeLog daily headers.
68 This defaults to the value returned by the function `user-full-name'."
69 :type '(choice (const :tag "Default" nil)
74 (defcustom add-log-mailing-address nil
75 "Email addresses of user, for inclusion in ChangeLog headers.
76 This defaults to the value of `user-mail-address'. In addition to
77 being a simple string, this value can also be a list. All elements
78 will be recognized as referring to the same user; when creating a new
79 ChangeLog entry, one element will be chosen at random."
80 :type '(choice (const :tag "Default" nil)
81 (string :tag "String")
82 (repeat :tag "List of Strings" string))
85 (defcustom add-log-time-format 'add-log-iso8601-time-string
86 "Function that defines the time format.
87 For example, `add-log-iso8601-time-string', which gives the
88 date in international ISO 8601 format,
89 and `current-time-string' are two valid values."
90 :type '(radio (const :tag "International ISO 8601 format"
91 add-log-iso8601-time-string)
92 (const :tag "Old format, as returned by `current-time-string'"
94 (function :tag "Other"))
97 (defcustom add-log-keep-changes-together nil
98 "If non-nil, normally keep day's log entries for one file together.
100 Log entries for a given file made with \\[add-change-log-entry] or
101 \\[add-change-log-entry-other-window] will only be added to others \
103 today if this variable is non-nil or that file comes first in today's
104 entries. Otherwise another entry for that file will be started. An
108 * bar (...): change 1
110 in the latter case, \\[add-change-log-entry-other-window] in a \
111 buffer visiting `bar', yields:
115 * bar (...): change 1
120 * bar (...): change 1
123 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
129 (defcustom add-log-always-start-new-record nil
130 "If non-nil, `add-change-log-entry' will always start a new record."
135 (defcustom add-log-buffer-file-name-function nil
136 "If non-nil, function to call to identify the full filename of a buffer.
137 This function is called with no argument. If this is nil, the default is to
138 use `buffer-file-name'."
139 :type '(choice (const nil) function)
142 (defcustom add-log-file-name-function nil
143 "If non-nil, function to call to identify the filename for a ChangeLog entry.
144 This function is called with one argument, the value of variable
145 `buffer-file-name' in that buffer. If this is nil, the default is to
146 use the file's name relative to the directory of the change log file."
147 :type '(choice (const nil) function)
151 (defcustom change-log-version-info-enabled nil
152 "*If non-nil, enable recording version numbers with the changes."
157 (defcustom change-log-version-number-regexp-list
158 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
160 ;; (defconst ad-version "2.15"
161 (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re)
162 ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
163 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re)))
164 "*List of regexps to search for version number.
165 The version number must be in group 1.
166 Note: The search is conducted only within 10%, at the beginning of the file."
168 :type '(repeat regexp)
171 (defface change-log-date
172 '((t (:inherit font-lock-string-face)))
173 "Face used to highlight dates in date lines."
176 ;; backward-compatibility alias
177 (put 'change-log-date-face 'face-alias 'change-log-date)
179 (defface change-log-name
180 '((t (:inherit font-lock-constant-face)))
181 "Face for highlighting author names."
184 ;; backward-compatibility alias
185 (put 'change-log-name-face 'face-alias 'change-log-name)
187 (defface change-log-email
188 '((t (:inherit font-lock-variable-name-face)))
189 "Face for highlighting author email addresses."
192 ;; backward-compatibility alias
193 (put 'change-log-email-face 'face-alias 'change-log-email)
195 (defface change-log-file
196 '((t (:inherit font-lock-function-name-face)))
197 "Face for highlighting file names."
200 ;; backward-compatibility alias
201 (put 'change-log-file-face 'face-alias 'change-log-file)
203 (defface change-log-list
204 '((t (:inherit font-lock-keyword-face)))
205 "Face for highlighting parenthesized lists of functions or variables."
208 ;; backward-compatibility alias
209 (put 'change-log-list-face 'face-alias 'change-log-list)
211 (defface change-log-conditionals
212 '((t (:inherit font-lock-variable-name-face)))
213 "Face for highlighting conditionals of the form `[...]'."
216 ;; backward-compatibility alias
217 (put 'change-log-conditionals-face 'face-alias 'change-log-conditionals)
219 (defface change-log-function
220 '((t (:inherit font-lock-variable-name-face)))
221 "Face for highlighting items of the form `<....>'."
224 ;; backward-compatibility alias
225 (put 'change-log-function-face 'face-alias 'change-log-function)
227 (defface change-log-acknowledgement
228 '((t (:inherit font-lock-comment-face)))
229 "Face for highlighting acknowledgments."
232 ;; backward-compatibility alias
233 (put 'change-log-acknowledgement-face 'face-alias 'change-log-acknowledgement)
235 (defvar change-log-font-lock-keywords
237 ;; Date lines, new (2000-01-01) and old (Sat Jan 1 00:00:00 2000) styles.
238 ;; Fixme: this regepx is just an approximate one and may match
239 ;; wrongly with a non-date line existing as a random note. In
240 ;; addition, using any kind of fixed setting like this doesn't
241 ;; work if a user customizes add-log-time-format.
242 ("^[0-9-]+ +\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+"
243 (0 'change-log-date-face)
244 ;; Name and e-mail; some people put e-mail in parens, not angles.
245 ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
247 (2 'change-log-email)))
250 ("^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)"
252 ;; Possibly further names in a list:
253 ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 'change-log-file))
254 ;; Possibly a parenthesized list of names:
255 ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
256 nil nil (1 'change-log-list))
257 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
258 nil nil (1 'change-log-list)))
260 ;; Function or variable names.
261 ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
263 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
264 (1 'change-log-list)))
267 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals))
269 ;; Function of change.
270 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function))
273 ;; Don't include plain "From" because that is vague;
274 ;; we want to encourage people to say something more specific.
275 ;; Note that the FSF does not use "Patches by"; our convention
276 ;; is to put the name of the author of the changes at the top
277 ;; of the change log entry.
278 ("\\(^\\( +\\|\t\\)\\| \\)\\(Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
279 3 'change-log-acknowledgement))
280 "Additional expressions to highlight in Change Log mode.")
282 (defvar change-log-mode-map
283 (let ((map (make-sparse-keymap)))
284 (define-key map [?\C-c ?\C-p] 'add-log-edit-prev-comment)
285 (define-key map [?\C-c ?\C-n] 'add-log-edit-next-comment)
287 "Keymap for Change Log major mode.")
289 ;; It used to be called change-log-time-zone-rule but really should be
290 ;; called add-log-time-zone-rule since it's only used from add-log-* code.
291 (defvaralias 'change-log-time-zone-rule 'add-log-time-zone-rule)
292 (defvar add-log-time-zone-rule nil
293 "Time zone used for calculating change log time stamps.
294 It takes the same format as the TZ argument of `set-time-zone-rule'.
295 If nil, use local time.
296 If t, use universal time.")
298 (defun add-log-iso8601-time-zone (&optional time)
299 (let* ((utc-offset (or (car (current-time-zone time)) 0))
300 (sign (if (< utc-offset 0) ?- ?+))
301 (sec (abs utc-offset))
306 (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
307 ((not (zerop mm)) "%c%02d:%02d")
311 (defvar add-log-iso8601-with-time-zone nil)
313 (defun add-log-iso8601-time-string ()
314 (let ((time (format-time-string "%Y-%m-%d"
315 nil (eq t add-log-time-zone-rule))))
316 (if add-log-iso8601-with-time-zone
317 (concat time " " (add-log-iso8601-time-zone))
320 (defun change-log-name ()
321 "Return (system-dependent) default name for a change log file."
322 (or change-log-default-name
323 (if (eq system-type 'vax-vms)
327 (defun add-log-edit-prev-comment (arg)
328 "Cycle backward through Log-Edit mode comment history.
329 With a numeric prefix ARG, go back ARG comments."
332 (narrow-to-region (point)
333 (if (memq last-command '(add-log-edit-prev-comment
334 add-log-edit-next-comment))
336 (when (fboundp 'log-edit-previous-comment)
337 (log-edit-previous-comment arg)
338 (indent-region (point-min) (point-max))
339 (goto-char (point-min))
340 (unless (save-restriction (widen) (bolp))
341 (delete-region (point) (progn (skip-chars-forward " \t\n") (point))))
342 (set-mark (point-min))
343 (goto-char (point-max))
344 (delete-region (point) (progn (skip-chars-backward " \t\n") (point))))))
346 (defun add-log-edit-next-comment (arg)
347 "Cycle forward through Log-Edit mode comment history.
348 With a numeric prefix ARG, go back ARG comments."
350 (add-log-edit-prev-comment (- arg)))
353 (defun prompt-for-change-log-name ()
354 "Prompt for a change log name."
355 (let* ((default (change-log-name))
356 (name (expand-file-name
357 (read-file-name (format "Log file (default %s): " default)
359 ;; Handle something that is syntactically a directory name.
360 ;; Look for ChangeLog or whatever in that directory.
361 (if (string= (file-name-nondirectory name) "")
362 (expand-file-name (file-name-nondirectory default)
364 ;; Handle specifying a file that is a directory.
365 (if (file-directory-p name)
366 (expand-file-name (file-name-nondirectory default)
367 (file-name-as-directory name))
370 (defun change-log-version-number-search ()
371 "Return version number of current buffer's file.
372 This is the value returned by `vc-workfile-version' or, if that is
373 nil, by matching `change-log-version-number-regexp-list'."
374 (let* ((size (buffer-size))
376 ;; The version number can be anywhere in the file, but
377 ;; restrict search to the file beginning: 10% should be
378 ;; enough to prevent some mishits.
380 ;; Apply percentage only if buffer size is bigger than
382 (if (> size (* 100 80)) (+ (point) (/ size 10)))))
383 (or (and buffer-file-name (vc-workfile-version buffer-file-name))
386 (let ((regexps change-log-version-number-regexp-list)
390 (goto-char (point-min))
391 (when (re-search-forward (pop regexps) limit t)
392 (setq version (match-string 1)
398 (defun find-change-log (&optional file-name buffer-file)
399 "Find a change log file for \\[add-change-log-entry] and return the name.
401 Optional arg FILE-NAME specifies the file to use.
402 If FILE-NAME is nil, use the value of `change-log-default-name'.
403 If `change-log-default-name' is nil, behave as though it were 'ChangeLog'
404 \(or whatever we use on this operating system).
406 If `change-log-default-name' contains a leading directory component, then
407 simply find it in the current directory. Otherwise, search in the current
408 directory and its successive parents for a file so named.
410 Once a file is found, `change-log-default-name' is set locally in the
411 current buffer to the complete file name.
412 Optional arg BUFFER-FILE overrides `buffer-file-name'."
413 ;; If user specified a file name or if this buffer knows which one to use,
416 (setq file-name (and change-log-default-name
417 (file-name-directory change-log-default-name)
418 change-log-default-name))
420 ;; Chase links in the source file
421 ;; and use the change log in the dir where it points.
422 (setq file-name (or (and (or buffer-file buffer-file-name)
425 (or buffer-file buffer-file-name))))
427 (if (file-directory-p file-name)
428 (setq file-name (expand-file-name (change-log-name) file-name)))
429 ;; Chase links before visiting the file.
430 ;; This makes it easier to use a single change log file
431 ;; for several related directories.
432 (setq file-name (file-chase-links file-name))
433 (setq file-name (expand-file-name file-name))
434 ;; Move up in the dir hierarchy till we find a change log file.
435 (let ((file1 file-name)
437 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
438 (progn (setq parent-dir
441 (file-name-directory file1))))
442 ;; Give up if we are already at the root dir.
443 (not (string= (file-name-directory file1)
445 ;; Move up to the parent dir and try again.
446 (setq file1 (expand-file-name
447 (file-name-nondirectory (change-log-name))
449 ;; If we found a change log in a parent, use that.
450 (if (or (get-file-buffer file1) (file-exists-p file1))
451 (setq file-name file1)))))
452 ;; Make a local variable in this buffer so we needn't search again.
453 (set (make-local-variable 'change-log-default-name) file-name)
456 (defun add-log-file-name (buffer-file log-file)
457 ;; Never want to add a change log entry for the ChangeLog file itself.
458 (unless (or (null buffer-file) (string= buffer-file log-file))
459 (if add-log-file-name-function
460 (funcall add-log-file-name-function buffer-file)
463 (concat "^" (regexp-quote (file-name-directory log-file)))
465 (substring buffer-file (match-end 0))
466 (file-name-nondirectory buffer-file)))
467 ;; If we have a backup file, it's presumably because we're
468 ;; comparing old and new versions (e.g. for deleted
469 ;; functions) and we'll want to use the original name.
470 (if (backup-file-name-p buffer-file)
471 (file-name-sans-versions buffer-file)
475 (defun add-change-log-entry (&optional whoami file-name other-window new-entry)
476 "Find change log file, and add an entry for today and an item for this file.
477 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
478 name and email (stored in `add-log-full-name' and `add-log-mailing-address').
480 Second arg FILE-NAME is file name of the change log.
481 If nil, use the value of `change-log-default-name'.
483 Third arg OTHER-WINDOW non-nil means visit in other window.
485 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
486 never append to an existing entry. Option `add-log-keep-changes-together'
487 otherwise affects whether a new entry is created.
489 Option `add-log-always-start-new-record' non-nil means always create a
490 new record, even when the last record was made on the same date and by
493 The change log file can start with a copyright notice and a copying
494 permission notice. The first blank line indicates the end of these
497 Today's date is calculated according to `add-log-time-zone-rule' if
498 non-nil, otherwise in local time."
499 (interactive (list current-prefix-arg
500 (prompt-for-change-log-name)))
501 (let* ((defun (add-log-current-defun))
502 (version (and change-log-version-info-enabled
503 (change-log-version-number-search)))
504 (buf-file-name (if add-log-buffer-file-name-function
505 (funcall add-log-buffer-file-name-function)
507 (buffer-file (if buf-file-name (expand-file-name buf-file-name)))
508 (file-name (expand-file-name (find-change-log file-name buffer-file)))
509 ;; Set ITEM to the file name to use in the new item.
510 (item (add-log-file-name buffer-file file-name))
512 (full-name (or add-log-full-name (user-full-name)))
513 (mailing-address (or add-log-mailing-address user-mail-address)))
517 (setq full-name (read-string "Full name: " full-name))
518 ;; Note that some sites have room and phone number fields in
519 ;; full name which look silly when inserted. Rather than do
520 ;; anything about that here, let user give prefix argument so that
521 ;; s/he can edit the full name field in prompter if s/he wants.
522 (setq mailing-address
523 (read-string "Mailing address: " mailing-address))))
525 (unless (equal file-name buffer-file-name)
526 (if (or other-window (window-dedicated-p (selected-window)))
527 (find-file-other-window file-name)
528 (find-file file-name)))
529 (or (eq major-mode 'change-log-mode)
532 (goto-char (point-min))
534 ;; If file starts with a copyright and permission notice, skip them.
535 ;; Assume they end at first blank line.
536 (when (looking-at "Copyright")
537 (search-forward "\n\n")
538 (skip-chars-forward "\n"))
540 ;; Advance into first entry if it is usable; else make new one.
542 (mapcar (lambda (addr)
544 (if (stringp add-log-time-zone-rule)
545 (let ((tz (getenv "TZ")))
548 (set-time-zone-rule add-log-time-zone-rule)
549 (funcall add-log-time-format))
550 (set-time-zone-rule tz)))
551 (funcall add-log-time-format))
554 (if (consp mailing-address)
556 (list mailing-address)))))
557 (if (and (not add-log-always-start-new-record)
559 (dolist (entry new-entries hit)
560 (when (looking-at (regexp-quote entry))
563 (insert (nth (random (length new-entries))
565 (if use-hard-newlines hard-newline "\n")
566 (if use-hard-newlines hard-newline "\n"))
569 ;; Determine where we should stop searching for a usable
570 ;; item to add to, within this entry.
573 (if (looking-at "\n*[^\n* \t]")
574 (skip-chars-forward "\n")
575 (if add-log-keep-changes-together
576 (forward-page) ; page delimits entries for date
577 (forward-paragraph))) ; paragraph delimits entries for file
580 ;; Now insert the new line for this item.
581 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t)
582 ;; Put this file name into the existing empty item.
585 ((and (not new-entry)
586 (let (case-fold-search)
588 (concat (regexp-quote (concat "* " item))
589 ;; Don't accept `foo.bar' when
590 ;; looking for `foo':
591 "\\(\\s \\|[(),:]\\)")
593 ;; Add to the existing item for the same file.
594 (re-search-forward "^\\s *$\\|^\\s \\*")
595 (goto-char (match-beginning 0))
596 ;; Delete excess empty lines; make just 2.
597 (while (and (not (eobp)) (looking-at "^\\s *$"))
598 (delete-region (point) (line-beginning-position 2)))
599 (insert (if use-hard-newlines hard-newline "\n")
600 (if use-hard-newlines hard-newline "\n"))
602 (indent-relative-maybe))
605 (while (looking-at "\\sW")
607 (while (and (not (eobp)) (looking-at "^\\s *$"))
608 (delete-region (point) (line-beginning-position 2)))
609 (insert (if use-hard-newlines hard-newline "\n")
610 (if use-hard-newlines hard-newline "\n")
611 (if use-hard-newlines hard-newline "\n"))
613 (indent-to left-margin)
615 (if item (insert item))))
616 ;; Now insert the function name, if we have one.
617 ;; Point is at the item for this file,
618 ;; either at the end of the line or at the first blank line.
620 ;; No function name, so put in a colon unless we have just a star.
621 (unless (save-excursion
622 (beginning-of-line 1)
623 (looking-at "\\s *\\(\\*\\s *\\)?$"))
625 (if version (insert version ?\s)))
626 ;; Make it easy to get rid of the function name.
628 (unless (save-excursion
629 (beginning-of-line 1)
630 (looking-at "\\s *$"))
632 ;; See if the prev function name has a message yet or not.
633 ;; If not, merge the two items.
634 (let ((pos (point-marker)))
635 (skip-syntax-backward " ")
636 (skip-chars-backward "):")
637 (if (and (looking-at "):")
638 (let ((pos (save-excursion (backward-sexp 1) (point))))
639 (when (equal (buffer-substring pos (point)) defun)
640 (delete-region pos (point)))
641 (> fill-column (+ (current-column) (length defun) 4))))
642 (progn (skip-chars-backward ", ")
643 (delete-region (point) pos)
644 (unless (memq (char-before) '(?\()) (insert ", ")))
645 (if (looking-at "):")
646 (delete-region (+ 1 (point)) (line-end-position)))
649 (set-marker pos nil))
651 (if version (insert version ?\s)))))
654 (defun add-change-log-entry-other-window (&optional whoami file-name)
655 "Find change log file in other window and add entry and item.
656 This is just like `add-change-log-entry' except that it displays
657 the change log file in another window."
658 (interactive (if current-prefix-arg
659 (list current-prefix-arg
660 (prompt-for-change-log-name))))
661 (add-change-log-entry whoami file-name t))
662 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
664 (defvar change-log-indent-text 0)
666 (defun change-log-indent ()
670 (skip-chars-forward " \t")
672 ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
673 ;; Matching the output of add-log-time-format is difficult,
674 ;; but I'll get it has at least two adjacent digits.
675 (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
677 ((looking-at "[^*(]")
678 (+ (current-left-margin) change-log-indent-text))
679 (t (current-left-margin)))))
680 (pos (save-excursion (indent-line-to indent) (point))))
681 (if (> pos (point)) (goto-char pos))))
684 (defvar smerge-resolve-function)
687 (define-derived-mode change-log-mode text-mode "Change Log"
688 "Major mode for editing change logs; like Indented Text Mode.
689 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
690 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
691 Each entry behaves as a paragraph, and the entries for one day as a page.
692 Runs `change-log-mode-hook'.
693 \\{change-log-mode-map}"
698 (set (make-local-variable 'fill-paragraph-function)
699 'change-log-fill-paragraph)
700 (set (make-local-variable 'indent-line-function) 'change-log-indent)
701 (set (make-local-variable 'tab-always-indent) nil)
702 ;; We really do want "^" in paragraph-start below: it is only the
703 ;; lines that begin at column 0 (despite the left-margin of 8) that
704 ;; we are looking for. Adding `* ' allows eliding the blank line
705 ;; between entries for different files.
706 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
707 (set (make-local-variable 'paragraph-separate) paragraph-start)
708 ;; Match null string on the date-line so that the date-line
709 ;; is grouped with what follows.
710 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
711 (set (make-local-variable 'version-control) 'never)
712 (set (make-local-variable 'smerge-resolve-function)
713 'change-log-resolve-conflict)
714 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
715 (set (make-local-variable 'font-lock-defaults)
716 '(change-log-font-lock-keywords t nil nil backward-paragraph)))
718 ;; It might be nice to have a general feature to replace this. The idea I
719 ;; have is a variable giving a regexp matching text which should not be
720 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
721 ;; But I don't feel up to implementing that today.
722 (defun change-log-fill-paragraph (&optional justify)
723 "Fill the paragraph, but preserve open parentheses at beginning of lines.
724 Prefix arg means justify as well."
726 (let ((end (progn (forward-paragraph) (point)))
727 (beg (progn (backward-paragraph) (point)))
728 (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
729 (fill-region beg end justify)
732 (defcustom add-log-current-defun-header-regexp
733 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
734 "Heuristic regexp used by `add-log-current-defun' for unknown major modes."
739 (defvar add-log-lisp-like-modes
740 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode)
741 "*Modes that look like Lisp to `add-log-current-defun'.")
744 (defvar add-log-c-like-modes
745 '(c-mode c++-mode c++-c-mode objc-mode)
746 "*Modes that look like C to `add-log-current-defun'.")
749 (defvar add-log-tex-like-modes
750 '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode)
751 "*Modes that look like TeX to `add-log-current-defun'.")
754 (defun add-log-current-defun ()
755 "Return name of function definition point is in, or nil.
757 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
758 Texinfo (@node titles) and Perl.
760 Other modes are handled by a heuristic that looks in the 10K before
761 point for uppercase headings starting in the first column or
762 identifiers followed by `:' or `='. See variables
763 `add-log-current-defun-header-regexp' and
764 `add-log-current-defun-function'.
766 Has a preference of looking backwards."
769 (let ((location (point)))
770 (cond (add-log-current-defun-function
771 (funcall add-log-current-defun-function))
772 ((memq major-mode add-log-lisp-like-modes)
773 ;; If we are now precisely at the beginning of a defun,
774 ;; make sure beginning-of-defun finds that one
775 ;; rather than the previous one.
776 (or (eobp) (forward-char 1))
778 ;; Make sure we are really inside the defun found,
780 (when (and (looking-at "\\s(")
781 (progn (end-of-defun)
782 (< location (point)))
783 (progn (forward-sexp -1)
784 (>= location (point))))
785 (if (looking-at "\\s(")
787 ;; Skip the defining construct name, typically "defun"
790 ;; The second element is usually a symbol being defined.
791 ;; If it is not, use the first symbol in it.
792 (skip-chars-forward " \t\n'(")
793 (buffer-substring-no-properties (point)
794 (progn (forward-sexp 1)
796 ((and (memq major-mode add-log-c-like-modes)
799 ;; Use eq instead of = here to avoid
800 ;; error when at bob and char-after
802 (while (eq (char-after (- (point) 2)) ?\\)
804 (looking-at "[ \t]*#[ \t]*define[ \t]")))
805 ;; Handle a C macro definition.
807 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
809 (search-forward "define")
810 (skip-chars-forward " \t")
811 (buffer-substring-no-properties (point)
812 (progn (forward-sexp 1)
814 ((memq major-mode add-log-c-like-modes)
816 ;; See if we are in the beginning part of a function,
817 ;; before the open brace. If so, advance forward.
818 (while (not (looking-at "{\\|\\(\\s *$\\)"))
823 ;; Try to find the containing defun.
826 ;; If the defun we found ends before the desired position,
827 ;; see if there's a DEFUN construct
828 ;; between that end and the desired position.
829 (when (save-excursion
830 (and (> location (point))
831 (re-search-forward "^DEFUN"
836 (re-search-forward "^{" nil t)
837 (setq maybe-beg (point))))
838 ;; If so, go to the end of that instead.
839 (goto-char maybe-beg)
841 ;; If the desired position is within the defun we found,
842 ;; find the function name.
843 (when (< location (point))
844 ;; Move back over function body.
847 ;; Skip back over typedefs and arglist.
848 ;; Stop at the function definition itself
849 ;; or at the line that follows end of function doc string.
851 (while (and (not (bobp))
852 (looking-at "[ \t\n]")
853 (not (looking-back "[*]/)\n" (- (point) 4))))
855 ;; If we found a doc string, this must be the DEFUN macro
856 ;; used in Emacs. Move back to the DEFUN line.
857 (when (looking-back "[*]/)\n" (- (point) 4))
860 ;; Is this a DEFUN construct? And is LOCATION in it?
861 (if (and (looking-at "DEFUN\\b")
862 (>= location (point)))
863 ;; DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, ...) ==> Ffile_name_directory
864 ;; DEFUN(POSIX::STREAM-LOCK, stream lockp &key BLOCK SHARED START LENGTH) ==> POSIX::STREAM-LOCK
867 (when (= (char-after (point)) ?\")
869 (search-forward ","))
870 (skip-syntax-forward " ")
871 (buffer-substring-no-properties
873 (progn (search-forward ",")
875 (skip-syntax-backward " ")
877 (if (looking-at "^[+-]")
879 (change-log-get-method-definition)
880 ;; Ordinary C function syntax.
883 ;; Protect against "Unbalanced parens" error.
886 (down-list 1) ; into arglist
888 (skip-chars-backward " \t")
891 ;; Verify initial pos was after
892 ;; real start of function.
895 ;; For this purpose, include the line
896 ;; that has the decl keywords. This
897 ;; may also include some of the
898 ;; comments before the function.
899 (while (and (not (bobp))
902 (looking-at "[^\n\f]")))
904 (>= location (point)))
905 ;; Consistency check: going down and up
906 ;; shouldn't take us back before BEG.
909 ;; Don't include any final whitespace
910 ;; in the name we use.
911 (skip-chars-backward " \t\n")
914 ;; Now find the right beginning of the name.
915 ;; Include certain keywords if they
917 (setq middle (point))
919 ;; Is this C++ method?
920 (when (and (< 2 middle)
921 (string= (buffer-substring (- middle 2)
924 ;; Include "classname::".
925 (setq middle (point)))
926 ;; Ignore these subparts of a class decl
927 ;; and move back to the class name itself.
928 (while (looking-at "public \\|private ")
929 (skip-chars-backward " \t:")
932 (setq middle (point))
936 "enum \\|struct \\|union \\|class ")
937 (setq middle (point)))
939 (when (eq (preceding-char) ?=)
941 (skip-chars-backward " \t")
943 (buffer-substring-no-properties
945 ((memq major-mode add-log-tex-like-modes)
946 (if (re-search-backward
947 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
950 (goto-char (match-beginning 0))
951 (buffer-substring-no-properties
952 (1+ (point)) ; without initial backslash
953 (line-end-position)))))
954 ((eq major-mode 'texinfo-mode)
955 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
956 (match-string-no-properties 1)))
957 ((memq major-mode '(perl-mode cperl-mode))
958 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t)
959 (match-string-no-properties 1)))
960 ;; Emacs's autoconf-mode installs its own
961 ;; `add-log-current-defun-function'. This applies to
962 ;; a different mode apparently for editing .m4
964 ((eq major-mode 'autoconf-mode)
965 (if (re-search-backward
966 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t)
967 (match-string-no-properties 3)))
969 ;; If all else fails, try heuristics
970 (let (case-fold-search
973 (when (re-search-backward
974 add-log-current-defun-header-regexp
977 (setq result (or (match-string-no-properties 1)
978 (match-string-no-properties 0)))
979 ;; Strip whitespace away
980 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
982 (setq result (match-string-no-properties 1 result)))
986 (defvar change-log-get-method-definition-md)
988 ;; Subroutine used within change-log-get-method-definition.
989 ;; Add the last match in the buffer to the end of `md',
990 ;; followed by the string END; move to the end of that match.
991 (defun change-log-get-method-definition-1 (end)
992 (setq change-log-get-method-definition-md
993 (concat change-log-get-method-definition-md
996 (goto-char (match-end 0)))
998 (defun change-log-get-method-definition ()
999 "For Objective C, return the method name if we are in a method."
1000 (let ((change-log-get-method-definition-md "["))
1002 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
1003 (change-log-get-method-definition-1 " ")))
1006 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
1007 (change-log-get-method-definition-1 "")
1008 (while (not (looking-at "[{;]"))
1010 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
1011 (change-log-get-method-definition-1 ""))
1012 (concat change-log-get-method-definition-md "]"))))))
1014 (defun change-log-sortable-date-at ()
1015 "Return date of log entry in a consistent form for sorting.
1016 Point is assumed to be at the start of the entry."
1018 (if (looking-at "^\\sw.........[0-9:+ ]*")
1019 (let ((date (match-string-no-properties 0)))
1021 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
1022 (concat (match-string 1 date) (match-string 2 date)
1023 (match-string 3 date))
1025 (timezone-make-date-sortable date)
1027 (error "Bad date")))
1029 (defun change-log-resolve-conflict ()
1030 "Function to be used in `smerge-resolve-function'."
1031 (let ((buf (current-buffer)))
1033 (insert-buffer-substring buf (match-beginning 1) (match-end 1))
1034 (save-match-data (change-log-mode))
1035 (let ((other-buf (current-buffer)))
1036 (with-current-buffer buf
1039 (narrow-to-region (match-beginning 0) (match-end 0))
1040 (replace-match (match-string 3) t t)
1041 (change-log-merge other-buf))))))))
1044 (defun change-log-merge (other-log)
1045 "Merge the contents of change log file OTHER-LOG with this buffer.
1046 Both must be found in Change Log mode (since the merging depends on
1047 the appropriate motion commands). OTHER-LOG can be either a file name
1050 Entries are inserted in chronological order. Both the current and
1051 old-style time formats for entries are supported."
1052 (interactive "*fLog file name to merge: ")
1053 (if (not (eq major-mode 'change-log-mode))
1054 (error "Not in Change Log mode"))
1055 (let ((other-buf (if (bufferp other-log) other-log
1056 (find-file-noselect other-log)))
1057 (buf (current-buffer))
1060 (goto-char (point-min))
1061 (set-buffer other-buf)
1062 (goto-char (point-min))
1063 (if (not (eq major-mode 'change-log-mode))
1064 (error "%s not found in Change Log mode" other-log))
1065 ;; Loop through all the entries in OTHER-LOG.
1067 (setq date1 (change-log-sortable-date-at))
1069 end (progn (forward-page) (point)))
1070 ;; Look for an entry in original buffer that isn't later.
1071 (with-current-buffer buf
1072 (while (and (not (eobp))
1073 (string< date1 (change-log-sortable-date-at)))
1076 (insert-buffer-substring other-buf start end)
1077 ;; At the end of the original buffer, insert a newline to
1078 ;; separate entries and then the rest of the file being
1081 (and (= ?\n (char-before))
1082 (or (<= (1- (point)) (point-min))
1083 (= ?\n (char-before (1- (point)))))))
1084 (insert (if use-hard-newlines hard-newline "\n")))
1085 ;; Move to the end of it to terminate outer loop.
1086 (with-current-buffer other-buf
1087 (goto-char (point-max)))
1088 (insert-buffer-substring other-buf start)))))))
1091 (defun change-log-redate ()
1092 "Fix any old-style date entries in the current log file to default format."
1096 (goto-char (point-min))
1097 (while (re-search-forward "^\\sw.........[0-9:+ ]*" nil t)
1098 (unless (= 12 (- (match-end 0) (match-beginning 0)))
1099 (let* ((date (save-match-data
1100 (timezone-fix-time (match-string 0) nil nil)))
1101 (zone (if (consp (aref date 6))
1102 (nth 1 (aref date 6)))))
1103 (replace-match (format-time-string
1105 (encode-time (aref date 5)
1115 ;; arch-tag: 81eee6fc-088f-4372-a37f-80ad9620e762
1116 ;;; add-log.el ends here