1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1988, 1993, 1994, 1997, 1998, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008 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 3, 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.
32 ;; - Find/use/create _MTN/log if there's a _MTN directory.
33 ;; - Find/use/create ++log.* if there's an {arch} directory.
34 ;; - Use an open *VC-Log* or *cvs-commit* buffer if it's related to the
36 ;; - Don't add TAB indents (and username?) if inserting entries in those
44 (defgroup change-log nil
45 "Change log maintenance."
47 :link
'(custom-manual "(emacs)Change Log")
52 (defcustom change-log-default-name nil
53 "Name of a change log file for \\[add-change-log-entry]."
54 :type
'(choice (const :tag
"default" nil
)
57 (put 'change-log-default-name
'safe-local-variable
'string-or-null-p
)
59 (defcustom change-log-mode-hook nil
60 "Normal hook run by `change-log-mode'."
64 ;; Many modes set this variable, so avoid warnings.
66 (defcustom add-log-current-defun-function nil
67 "If non-nil, function to guess name of surrounding function.
68 It is used by `add-log-current-defun' in preference to built-in rules.
69 Returns function's name as a string, or nil if outside a function."
70 :type
'(choice (const nil
) function
)
74 (defcustom add-log-full-name nil
75 "Full name of user, for inclusion in ChangeLog daily headers.
76 This defaults to the value returned by the function `user-full-name'."
77 :type
'(choice (const :tag
"Default" nil
)
82 (defcustom add-log-mailing-address nil
83 "Email addresses of user, for inclusion in ChangeLog headers.
84 This defaults to the value of `user-mail-address'. In addition to
85 being a simple string, this value can also be a list. All elements
86 will be recognized as referring to the same user; when creating a new
87 ChangeLog entry, one element will be chosen at random."
88 :type
'(choice (const :tag
"Default" nil
)
89 (string :tag
"String")
90 (repeat :tag
"List of Strings" string
))
93 (defcustom add-log-time-format
'add-log-iso8601-time-string
94 "Function that defines the time format.
95 For example, `add-log-iso8601-time-string', which gives the
96 date in international ISO 8601 format,
97 and `current-time-string' are two valid values."
98 :type
'(radio (const :tag
"International ISO 8601 format"
99 add-log-iso8601-time-string
)
100 (const :tag
"Old format, as returned by `current-time-string'"
102 (function :tag
"Other"))
105 (defcustom add-log-keep-changes-together nil
106 "If non-nil, normally keep day's log entries for one file together.
108 Log entries for a given file made with \\[add-change-log-entry] or
109 \\[add-change-log-entry-other-window] will only be added to others \
111 today if this variable is non-nil or that file comes first in today's
112 entries. Otherwise another entry for that file will be started. An
116 * bar (...): change 1
118 in the latter case, \\[add-change-log-entry-other-window] in a \
119 buffer visiting `bar', yields:
123 * bar (...): change 1
128 * bar (...): change 1
131 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
137 (defcustom add-log-always-start-new-record nil
138 "If non-nil, `add-change-log-entry' will always start a new record."
143 (defcustom add-log-buffer-file-name-function nil
144 "If non-nil, function to call to identify the full filename of a buffer.
145 This function is called with no argument. If this is nil, the default is to
146 use `buffer-file-name'."
147 :type
'(choice (const nil
) function
)
150 (defcustom add-log-file-name-function nil
151 "If non-nil, function to call to identify the filename for a ChangeLog entry.
152 This function is called with one argument, the value of variable
153 `buffer-file-name' in that buffer. If this is nil, the default is to
154 use the file's name relative to the directory of the change log file."
155 :type
'(choice (const nil
) function
)
159 (defcustom change-log-version-info-enabled nil
160 "If non-nil, enable recording version numbers with the changes."
165 (defcustom change-log-version-number-regexp-list
166 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
168 ;; (defconst ad-version "2.15"
169 (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re
)
170 ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
171 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re
)))
172 "List of regexps to search for version number.
173 The version number must be in group 1.
174 Note: The search is conducted only within 10%, at the beginning of the file."
176 :type
'(repeat regexp
)
179 (defface change-log-date
180 '((t (:inherit font-lock-string-face
)))
181 "Face used to highlight dates in date lines."
184 ;; backward-compatibility alias
185 (put 'change-log-date-face
'face-alias
'change-log-date
)
187 (defface change-log-name
188 '((t (:inherit font-lock-constant-face
)))
189 "Face for highlighting author names."
192 ;; backward-compatibility alias
193 (put 'change-log-name-face
'face-alias
'change-log-name
)
195 (defface change-log-email
196 '((t (:inherit font-lock-variable-name-face
)))
197 "Face for highlighting author email addresses."
200 ;; backward-compatibility alias
201 (put 'change-log-email-face
'face-alias
'change-log-email
)
203 (defface change-log-file
204 '((t (:inherit font-lock-function-name-face
)))
205 "Face for highlighting file names."
208 ;; backward-compatibility alias
209 (put 'change-log-file-face
'face-alias
'change-log-file
)
211 (defface change-log-list
212 '((t (:inherit font-lock-keyword-face
)))
213 "Face for highlighting parenthesized lists of functions or variables."
216 ;; backward-compatibility alias
217 (put 'change-log-list-face
'face-alias
'change-log-list
)
219 (defface change-log-conditionals
220 '((t (:inherit font-lock-variable-name-face
)))
221 "Face for highlighting conditionals of the form `[...]'."
224 ;; backward-compatibility alias
225 (put 'change-log-conditionals-face
'face-alias
'change-log-conditionals
)
227 (defface change-log-function
228 '((t (:inherit font-lock-variable-name-face
)))
229 "Face for highlighting items of the form `<....>'."
232 ;; backward-compatibility alias
233 (put 'change-log-function-face
'face-alias
'change-log-function
)
235 (defface change-log-acknowledgement
236 '((t (:inherit font-lock-comment-face
)))
237 "Face for highlighting acknowledgments."
240 ;; backward-compatibility alias
241 (put 'change-log-acknowledgement-face
'face-alias
'change-log-acknowledgement
)
243 (defconst change-log-file-names-re
"^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)")
244 (defconst change-log-start-entry-re
"^\\sw.........[0-9:+ ]*")
246 (defvar change-log-font-lock-keywords
248 ;; Date lines, new (2000-01-01) and old (Sat Jan 1 00:00:00 2000) styles.
249 ;; Fixme: this regepx is just an approximate one and may match
250 ;; wrongly with a non-date line existing as a random note. In
251 ;; addition, using any kind of fixed setting like this doesn't
252 ;; work if a user customizes add-log-time-format.
253 ("^[0-9-]+ +\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+"
254 (0 'change-log-date-face
)
255 ;; Name and e-mail; some people put e-mail in parens, not angles.
256 ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
258 (2 'change-log-email
)))
261 (,change-log-file-names-re
263 ;; Possibly further names in a list:
264 ("\\=, \\([^ ,:([\n]+\\)" nil nil
(1 'change-log-file
))
265 ;; Possibly a parenthesized list of names:
266 ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
267 nil nil
(1 'change-log-list
))
268 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
269 nil nil
(1 'change-log-list
)))
271 ;; Function or variable names.
272 ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
274 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
275 (1 'change-log-list
)))
278 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals
))
280 ;; Function of change.
281 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function
))
284 ;; Don't include plain "From" because that is vague;
285 ;; we want to encourage people to say something more specific.
286 ;; Note that the FSF does not use "Patches by"; our convention
287 ;; is to put the name of the author of the changes at the top
288 ;; of the change log entry.
289 ("\\(^\\( +\\|\t\\)\\| \\)\\(Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
290 3 'change-log-acknowledgement
))
291 "Additional expressions to highlight in Change Log mode.")
293 (defun change-log-search-file-name (where)
294 "Return the file-name for the change under point."
297 (beginning-of-line 1)
298 (if (looking-at change-log-start-entry-re
)
299 ;; We are at the start of an entry, search forward for a file
302 (re-search-forward change-log-file-names-re nil t
)
304 (if (looking-at change-log-file-names-re
)
305 ;; We found a file name.
307 ;; Look backwards for either a file name or the log entry start.
308 (if (re-search-backward
309 (concat "\\(" change-log-start-entry-re
311 change-log-file-names-re
"\\)") nil t
)
312 (if (match-beginning 1)
313 ;; We got the start of the entry, look forward for a
316 (re-search-forward change-log-file-names-re nil t
)
319 ;; We must be before any file name, look forward.
320 (re-search-forward change-log-file-names-re nil t
)
321 (match-string 2))))))
323 (defun change-log-find-file ()
324 "Visit the file for the change under point."
326 (let ((file (change-log-search-file-name (point))))
327 (if (and file
(file-exists-p file
))
329 (message "No such file or directory: %s" file
))))
331 (defvar change-log-mode-map
332 (let ((map (make-sparse-keymap)))
333 (define-key map
[?\C-c ?\C-p
] 'add-log-edit-prev-comment
)
334 (define-key map
[?\C-c ?\C-n
] 'add-log-edit-next-comment
)
335 (define-key map
[?\C-c ?\C-f
] 'change-log-find-file
)
337 "Keymap for Change Log major mode.")
339 ;; It used to be called change-log-time-zone-rule but really should be
340 ;; called add-log-time-zone-rule since it's only used from add-log-* code.
341 (defvaralias 'change-log-time-zone-rule
'add-log-time-zone-rule
)
342 (defvar add-log-time-zone-rule nil
343 "Time zone used for calculating change log time stamps.
344 It takes the same format as the TZ argument of `set-time-zone-rule'.
345 If nil, use local time.
346 If t, use universal time.")
347 (put 'add-log-time-zone-rule
'safe-local-variable
348 '(lambda (x) (or (booleanp x
) (stringp x
))))
350 (defun add-log-iso8601-time-zone (&optional time
)
351 (let* ((utc-offset (or (car (current-time-zone time
)) 0))
352 (sign (if (< utc-offset
0) ?- ?
+))
353 (sec (abs utc-offset
))
358 (format (cond ((not (zerop ss
)) "%c%02d:%02d:%02d")
359 ((not (zerop mm
)) "%c%02d:%02d")
363 (defvar add-log-iso8601-with-time-zone nil
)
365 (defun add-log-iso8601-time-string ()
366 (let ((time (format-time-string "%Y-%m-%d"
367 nil
(eq t add-log-time-zone-rule
))))
368 (if add-log-iso8601-with-time-zone
369 (concat time
" " (add-log-iso8601-time-zone))
372 (defun change-log-name ()
373 "Return (system-dependent) default name for a change log file."
374 (or change-log-default-name
375 (if (eq system-type
'vax-vms
)
379 (defun add-log-edit-prev-comment (arg)
380 "Cycle backward through Log-Edit mode comment history.
381 With a numeric prefix ARG, go back ARG comments."
384 (narrow-to-region (point)
385 (if (memq last-command
'(add-log-edit-prev-comment
386 add-log-edit-next-comment
))
388 (when (fboundp 'log-edit-previous-comment
)
389 (log-edit-previous-comment arg
)
390 (indent-region (point-min) (point-max))
391 (goto-char (point-min))
392 (unless (save-restriction (widen) (bolp))
393 (delete-region (point) (progn (skip-chars-forward " \t\n") (point))))
394 (set-mark (point-min))
395 (goto-char (point-max))
396 (delete-region (point) (progn (skip-chars-backward " \t\n") (point))))))
398 (defun add-log-edit-next-comment (arg)
399 "Cycle forward through Log-Edit mode comment history.
400 With a numeric prefix ARG, go back ARG comments."
402 (add-log-edit-prev-comment (- arg
)))
405 (defun prompt-for-change-log-name ()
406 "Prompt for a change log name."
407 (let* ((default (change-log-name))
408 (name (expand-file-name
409 (read-file-name (format "Log file (default %s): " default
)
411 ;; Handle something that is syntactically a directory name.
412 ;; Look for ChangeLog or whatever in that directory.
413 (if (string= (file-name-nondirectory name
) "")
414 (expand-file-name (file-name-nondirectory default
)
416 ;; Handle specifying a file that is a directory.
417 (if (file-directory-p name
)
418 (expand-file-name (file-name-nondirectory default
)
419 (file-name-as-directory name
))
422 (defun change-log-version-number-search ()
423 "Return version number of current buffer's file.
424 This is the value returned by `vc-working-revision' or, if that is
425 nil, by matching `change-log-version-number-regexp-list'."
426 (let* ((size (buffer-size))
428 ;; The version number can be anywhere in the file, but
429 ;; restrict search to the file beginning: 10% should be
430 ;; enough to prevent some mishits.
432 ;; Apply percentage only if buffer size is bigger than
434 (if (> size
(* 100 80)) (+ (point) (/ size
10)))))
435 (or (and buffer-file-name
(vc-working-revision buffer-file-name
))
438 (let ((regexps change-log-version-number-regexp-list
)
442 (goto-char (point-min))
443 (when (re-search-forward (pop regexps
) limit t
)
444 (setq version
(match-string 1)
450 (defun find-change-log (&optional file-name buffer-file
)
451 "Find a change log file for \\[add-change-log-entry] and return the name.
453 Optional arg FILE-NAME specifies the file to use.
454 If FILE-NAME is nil, use the value of `change-log-default-name'.
455 If `change-log-default-name' is nil, behave as though it were 'ChangeLog'
456 \(or whatever we use on this operating system).
458 If `change-log-default-name' contains a leading directory component, then
459 simply find it in the current directory. Otherwise, search in the current
460 directory and its successive parents for a file so named.
462 Once a file is found, `change-log-default-name' is set locally in the
463 current buffer to the complete file name.
464 Optional arg BUFFER-FILE overrides `buffer-file-name'."
465 ;; If user specified a file name or if this buffer knows which one to use,
468 (setq file-name
(and change-log-default-name
469 (file-name-directory change-log-default-name
)
470 change-log-default-name
))
472 ;; Chase links in the source file
473 ;; and use the change log in the dir where it points.
474 (setq file-name
(or (and (or buffer-file buffer-file-name
)
477 (or buffer-file buffer-file-name
))))
479 (if (file-directory-p file-name
)
480 (setq file-name
(expand-file-name (change-log-name) file-name
)))
481 ;; Chase links before visiting the file.
482 ;; This makes it easier to use a single change log file
483 ;; for several related directories.
484 (setq file-name
(file-chase-links file-name
))
485 (setq file-name
(expand-file-name file-name
))
486 ;; Move up in the dir hierarchy till we find a change log file.
487 (let ((file1 file-name
)
489 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
490 (progn (setq parent-dir
493 (file-name-directory file1
))))
494 ;; Give up if we are already at the root dir.
495 (not (string= (file-name-directory file1
)
497 ;; Move up to the parent dir and try again.
498 (setq file1
(expand-file-name
499 (file-name-nondirectory (change-log-name))
501 ;; If we found a change log in a parent, use that.
502 (if (or (get-file-buffer file1
) (file-exists-p file1
))
503 (setq file-name file1
)))))
504 ;; Make a local variable in this buffer so we needn't search again.
505 (set (make-local-variable 'change-log-default-name
) file-name
)
508 (defun add-log-file-name (buffer-file log-file
)
509 ;; Never want to add a change log entry for the ChangeLog file itself.
510 (unless (or (null buffer-file
) (string= buffer-file log-file
))
511 (if add-log-file-name-function
512 (funcall add-log-file-name-function buffer-file
)
514 (file-relative-name buffer-file
(file-name-directory log-file
)))
515 ;; If we have a backup file, it's presumably because we're
516 ;; comparing old and new versions (e.g. for deleted
517 ;; functions) and we'll want to use the original name.
518 (if (backup-file-name-p buffer-file
)
519 (file-name-sans-versions buffer-file
)
523 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
524 "Find change log file, and add an entry for today and an item for this file.
525 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
526 name and email (stored in `add-log-full-name' and `add-log-mailing-address').
528 Second arg FILE-NAME is file name of the change log.
529 If nil, use the value of `change-log-default-name'.
531 Third arg OTHER-WINDOW non-nil means visit in other window.
533 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
534 never append to an existing entry. Option `add-log-keep-changes-together'
535 otherwise affects whether a new entry is created.
537 Option `add-log-always-start-new-record' non-nil means always create a
538 new record, even when the last record was made on the same date and by
541 The change log file can start with a copyright notice and a copying
542 permission notice. The first blank line indicates the end of these
545 Today's date is calculated according to `add-log-time-zone-rule' if
546 non-nil, otherwise in local time."
547 (interactive (list current-prefix-arg
548 (prompt-for-change-log-name)))
549 (let* ((defun (add-log-current-defun))
550 (version (and change-log-version-info-enabled
551 (change-log-version-number-search)))
552 (buf-file-name (if add-log-buffer-file-name-function
553 (funcall add-log-buffer-file-name-function
)
555 (buffer-file (if buf-file-name
(expand-file-name buf-file-name
)))
556 (file-name (expand-file-name (find-change-log file-name buffer-file
)))
557 ;; Set ITEM to the file name to use in the new item.
558 (item (add-log-file-name buffer-file file-name
)))
560 (unless (equal file-name buffer-file-name
)
561 (if (or other-window
(window-dedicated-p (selected-window)))
562 (find-file-other-window file-name
)
563 (find-file file-name
)))
564 (or (derived-mode-p 'change-log-mode
)
567 (goto-char (point-min))
569 (let ((full-name (or add-log-full-name
(user-full-name)))
570 (mailing-address (or add-log-mailing-address user-mail-address
)))
573 (setq full-name
(read-string "Full name: " full-name
))
574 ;; Note that some sites have room and phone number fields in
575 ;; full name which look silly when inserted. Rather than do
576 ;; anything about that here, let user give prefix argument so that
577 ;; s/he can edit the full name field in prompter if s/he wants.
578 (setq mailing-address
579 (read-string "Mailing address: " mailing-address
)))
581 ;; If file starts with a copyright and permission notice, skip them.
582 ;; Assume they end at first blank line.
583 (when (looking-at "Copyright")
584 (search-forward "\n\n")
585 (skip-chars-forward "\n"))
587 ;; Advance into first entry if it is usable; else make new one.
589 (mapcar (lambda (addr)
591 (if (stringp add-log-time-zone-rule
)
592 (let ((tz (getenv "TZ")))
595 (set-time-zone-rule add-log-time-zone-rule
)
596 (funcall add-log-time-format
))
597 (set-time-zone-rule tz
)))
598 (funcall add-log-time-format
))
601 (if (consp mailing-address
)
603 (list mailing-address
)))))
604 (if (and (not add-log-always-start-new-record
)
606 (dolist (entry new-entries hit
)
607 (when (looking-at (regexp-quote entry
))
610 (insert (nth (random (length new-entries
))
612 (if use-hard-newlines hard-newline
"\n")
613 (if use-hard-newlines hard-newline
"\n"))
616 ;; Determine where we should stop searching for a usable
617 ;; item to add to, within this entry.
620 (if (looking-at "\n*[^\n* \t]")
621 (skip-chars-forward "\n")
622 (if add-log-keep-changes-together
623 (forward-page) ; page delimits entries for date
624 (forward-paragraph))) ; paragraph delimits entries for file
627 ;; Now insert the new line for this item.
628 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t
)
629 ;; Put this file name into the existing empty item.
632 ((and (not new-entry
)
633 (let (case-fold-search)
635 (concat (regexp-quote (concat "* " item
))
636 ;; Don't accept `foo.bar' when
637 ;; looking for `foo':
638 "\\(\\s \\|[(),:]\\)")
640 ;; Add to the existing item for the same file.
641 (re-search-forward "^\\s *$\\|^\\s \\*")
642 (goto-char (match-beginning 0))
643 ;; Delete excess empty lines; make just 2.
644 (while (and (not (eobp)) (looking-at "^\\s *$"))
645 (delete-region (point) (line-beginning-position 2)))
646 (insert (if use-hard-newlines hard-newline
"\n")
647 (if use-hard-newlines hard-newline
"\n"))
649 (indent-relative-maybe))
652 (while (looking-at "\\sW")
654 (while (and (not (eobp)) (looking-at "^\\s *$"))
655 (delete-region (point) (line-beginning-position 2)))
656 (insert (if use-hard-newlines hard-newline
"\n")
657 (if use-hard-newlines hard-newline
"\n")
658 (if use-hard-newlines hard-newline
"\n"))
660 (indent-to left-margin
)
662 (if item
(insert item
)))))
663 ;; Now insert the function name, if we have one.
664 ;; Point is at the item for this file,
665 ;; either at the end of the line or at the first blank line.
667 ;; No function name, so put in a colon unless we have just a star.
668 (unless (save-excursion
669 (beginning-of-line 1)
670 (looking-at "\\s *\\(\\*\\s *\\)?$"))
672 (if version
(insert version ?\s
)))
673 ;; Make it easy to get rid of the function name.
675 (unless (save-excursion
676 (beginning-of-line 1)
677 (looking-at "\\s *$"))
679 ;; See if the prev function name has a message yet or not.
680 ;; If not, merge the two items.
681 (let ((pos (point-marker)))
682 (skip-syntax-backward " ")
683 (skip-chars-backward "):")
684 (if (and (looking-at "):")
685 (let ((pos (save-excursion (backward-sexp 1) (point))))
686 (when (equal (buffer-substring pos
(point)) defun
)
687 (delete-region pos
(point)))
688 (> fill-column
(+ (current-column) (length defun
) 4))))
689 (progn (skip-chars-backward ", ")
690 (delete-region (point) pos
)
691 (unless (memq (char-before) '(?\
()) (insert ", ")))
692 (if (looking-at "):")
693 (delete-region (+ 1 (point)) (line-end-position)))
696 (set-marker pos nil
))
698 (if version
(insert version ?\s
)))))
701 (defun add-change-log-entry-other-window (&optional whoami file-name
)
702 "Find change log file in other window and add entry and item.
703 This is just like `add-change-log-entry' except that it displays
704 the change log file in another window."
705 (interactive (if current-prefix-arg
706 (list current-prefix-arg
707 (prompt-for-change-log-name))))
708 (add-change-log-entry whoami file-name t
))
711 (defvar change-log-indent-text
0)
713 (defun change-log-fill-parenthesized-list ()
714 ;; Fill parenthesized lists of names according to GNU standards.
715 ;; * file-name.ext (very-long-foo, very-long-bar, very-long-foobar):
716 ;; should be filled as
717 ;; * file-name.ext (very-long-foo, very-long-bar)
718 ;; (very-long-foobar):
721 (skip-chars-backward " \t")
722 (when (and (equal (char-before) ?\
,)
723 (> (point) (1+ (point-min))))
725 (when (save-excursion
728 (equal (char-after) ?\
()
729 (skip-chars-backward " \t"))
731 ;; Skip everything but a whitespace or asterisk.
732 (and (not (zerop (skip-chars-backward "^ \t\n*")))
733 (skip-chars-backward " \t")
734 ;; We want one asterisk here.
735 (= (skip-chars-backward "*") -
1)
736 (skip-chars-backward " \t")
740 ;; Close list on previous line.
742 (skip-chars-forward " \t\n")
743 ;; Start list on new line.
744 (insert-before-markers "("))
747 (defun change-log-indent ()
748 (change-log-fill-parenthesized-list)
752 (skip-chars-forward " \t")
754 ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
755 ;; Matching the output of add-log-time-format is difficult,
756 ;; but I'll get it has at least two adjacent digits.
757 (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
759 ((looking-at "[^*(]")
760 (+ (current-left-margin) change-log-indent-text
))
761 (t (current-left-margin)))))
762 (pos (save-excursion (indent-line-to indent
) (point))))
763 (if (> pos
(point)) (goto-char pos
))))
766 (defvar smerge-resolve-function
)
769 (define-derived-mode change-log-mode text-mode
"Change Log"
770 "Major mode for editing change logs; like Indented Text Mode.
771 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
772 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
773 Each entry behaves as a paragraph, and the entries for one day as a page.
774 Runs `change-log-mode-hook'.
775 \\{change-log-mode-map}"
780 show-trailing-whitespace t
)
781 (set (make-local-variable 'fill-paragraph-function
)
782 'change-log-fill-paragraph
)
783 ;; Avoid that filling leaves behind a single "*" on a line.
784 (add-hook 'fill-nobreak-predicate
786 (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
788 (set (make-local-variable 'indent-line-function
) 'change-log-indent
)
789 (set (make-local-variable 'tab-always-indent
) nil
)
790 ;; We really do want "^" in paragraph-start below: it is only the
791 ;; lines that begin at column 0 (despite the left-margin of 8) that
792 ;; we are looking for. Adding `* ' allows eliding the blank line
793 ;; between entries for different files.
794 (set (make-local-variable 'paragraph-start
) "\\s *$\\|\f\\|^\\<")
795 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
796 ;; Match null string on the date-line so that the date-line
797 ;; is grouped with what follows.
798 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
799 (set (make-local-variable 'version-control
) 'never
)
800 (set (make-local-variable 'smerge-resolve-function
)
801 'change-log-resolve-conflict
)
802 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
803 (set (make-local-variable 'font-lock-defaults
)
804 '(change-log-font-lock-keywords t nil nil backward-paragraph
))
805 (set (make-local-variable 'isearch-buffers-next-buffer-function
)
806 'change-log-next-buffer
)
807 (set (make-local-variable 'beginning-of-defun-function
)
808 'change-log-beginning-of-defun
)
809 (set (make-local-variable 'end-of-defun-function
)
810 'change-log-end-of-defun
)
811 (isearch-buffers-minor-mode))
813 (defun change-log-next-buffer (&optional buffer wrap
)
814 "Return the next buffer in the series of ChangeLog file buffers.
815 This function is used for multiple buffers isearch.
816 A sequence of buffers is formed by ChangeLog files with decreasing
817 numeric file name suffixes in the directory of the initial ChangeLog
818 file were isearch was started."
819 (let* ((name (change-log-name))
820 (files (cons name
(sort (file-expand-wildcards
821 (concat name
"[-.][0-9]*"))
823 ;; The file's extension may not have a valid
824 ;; version form (e.g. VC backup revisions).
826 (version< (substring b
(length name
))
827 (substring a
(length name
))))))))
828 (files (if isearch-forward files
(reverse files
))))
832 (cadr (member (file-name-nondirectory (buffer-file-name buffer
))
835 ;; It might be nice to have a general feature to replace this. The idea I
836 ;; have is a variable giving a regexp matching text which should not be
837 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
838 ;; But I don't feel up to implementing that today.
839 (defun change-log-fill-paragraph (&optional justify
)
840 "Fill the paragraph, but preserve open parentheses at beginning of lines.
841 Prefix arg means justify as well."
843 (let ((end (progn (forward-paragraph) (point)))
844 (beg (progn (backward-paragraph) (point)))
845 ;; Add lines starting with whitespace followed by a left paren or an
847 (paragraph-start (concat paragraph-start
"\\|\\s *\\(?:\\s(\\|\\*\\)"))
848 ;; Make sure we call `change-log-indent'.
849 (fill-indent-according-to-mode t
))
850 (fill-region beg end justify
)
853 (defcustom add-log-current-defun-header-regexp
854 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
855 "Heuristic regexp used by `add-log-current-defun' for unknown major modes."
860 (defvar add-log-lisp-like-modes
861 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode
)
862 "*Modes that look like Lisp to `add-log-current-defun'.")
865 (defvar add-log-c-like-modes
866 '(c-mode c
++-mode c
++-c-mode objc-mode
)
867 "*Modes that look like C to `add-log-current-defun'.")
870 (defvar add-log-tex-like-modes
871 '(TeX-mode plain-TeX-mode LaTeX-mode tex-mode
)
872 "*Modes that look like TeX to `add-log-current-defun'.")
874 (declare-function c-cpp-define-name
"cc-cmds" ())
875 (declare-function c-defun-name
"cc-cmds" ())
878 (defun add-log-current-defun ()
879 "Return name of function definition point is in, or nil.
881 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
882 Texinfo (@node titles) and Perl.
884 Other modes are handled by a heuristic that looks in the 10K before
885 point for uppercase headings starting in the first column or
886 identifiers followed by `:' or `='. See variables
887 `add-log-current-defun-header-regexp' and
888 `add-log-current-defun-function'.
890 Has a preference of looking backwards."
893 (let ((location (point)))
894 (cond (add-log-current-defun-function
895 (funcall add-log-current-defun-function
))
896 ((apply 'derived-mode-p add-log-lisp-like-modes
)
897 ;; If we are now precisely at the beginning of a defun,
898 ;; make sure beginning-of-defun finds that one
899 ;; rather than the previous one.
900 (or (eobp) (forward-char 1))
902 ;; Make sure we are really inside the defun found,
904 (when (and (looking-at "\\s(")
905 (progn (end-of-defun)
906 (< location
(point)))
907 (progn (forward-sexp -
1)
908 (>= location
(point))))
909 (if (looking-at "\\s(")
911 ;; Skip the defining construct name, typically "defun"
914 ;; The second element is usually a symbol being defined.
915 ;; If it is not, use the first symbol in it.
916 (skip-chars-forward " \t\n'(")
917 (buffer-substring-no-properties (point)
918 (progn (forward-sexp 1)
920 ((apply 'derived-mode-p add-log-c-like-modes
)
921 (or (c-cpp-define-name)
923 ((memq major-mode add-log-tex-like-modes
)
924 (if (re-search-backward
925 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
928 (goto-char (match-beginning 0))
929 (buffer-substring-no-properties
930 (1+ (point)) ; without initial backslash
931 (line-end-position)))))
932 ((derived-mode-p 'texinfo-mode
)
933 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
934 (match-string-no-properties 1)))
935 ((derived-mode-p 'perl-mode
'cperl-mode
)
936 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t
)
937 (match-string-no-properties 1)))
938 ;; Emacs's autoconf-mode installs its own
939 ;; `add-log-current-defun-function'. This applies to
940 ;; a different mode apparently for editing .m4
942 ((derived-mode-p 'autoconf-mode
)
943 (if (re-search-backward
944 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t
)
945 (match-string-no-properties 3)))
947 ;; If all else fails, try heuristics
948 (let (case-fold-search
951 (when (re-search-backward
952 add-log-current-defun-header-regexp
955 (setq result
(or (match-string-no-properties 1)
956 (match-string-no-properties 0)))
957 ;; Strip whitespace away
958 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
960 (setq result
(match-string-no-properties 1 result
)))
964 (defvar change-log-get-method-definition-md
)
966 ;; Subroutine used within change-log-get-method-definition.
967 ;; Add the last match in the buffer to the end of `md',
968 ;; followed by the string END; move to the end of that match.
969 (defun change-log-get-method-definition-1 (end)
970 (setq change-log-get-method-definition-md
971 (concat change-log-get-method-definition-md
974 (goto-char (match-end 0)))
976 (defun change-log-get-method-definition ()
977 "For Objective C, return the method name if we are in a method."
978 (let ((change-log-get-method-definition-md "["))
980 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
981 (change-log-get-method-definition-1 " ")))
984 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
985 (change-log-get-method-definition-1 "")
986 (while (not (looking-at "[{;]"))
988 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
989 (change-log-get-method-definition-1 ""))
990 (concat change-log-get-method-definition-md
"]"))))))
992 (defun change-log-sortable-date-at ()
993 "Return date of log entry in a consistent form for sorting.
994 Point is assumed to be at the start of the entry."
996 (if (looking-at change-log-start-entry-re
)
997 (let ((date (match-string-no-properties 0)))
999 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date
)
1000 (concat (match-string 1 date
) (match-string 2 date
)
1001 (match-string 3 date
))
1003 (timezone-make-date-sortable date
)
1005 (error "Bad date")))
1007 (defun change-log-resolve-conflict ()
1008 "Function to be used in `smerge-resolve-function'."
1011 (narrow-to-region (match-beginning 0) (match-end 0))
1012 (let ((mb1 (match-beginning 1))
1014 (mb3 (match-beginning 3))
1016 (tmp1 (generate-new-buffer " *changelog-resolve-1*"))
1017 (tmp2 (generate-new-buffer " *changelog-resolve-2*")))
1019 (let ((buf (current-buffer)))
1020 (with-current-buffer tmp1
1022 (insert-buffer-substring buf mb1 me1
))
1023 (with-current-buffer tmp2
1025 (insert-buffer-substring buf mb3 me3
)
1026 ;; Do the merge here instead of inside `buf' so as to be
1027 ;; more robust in case change-log-merge fails.
1028 (change-log-merge tmp1
))
1029 (goto-char (point-max))
1030 (delete-region (point-min)
1032 (insert-buffer-substring tmp2
))))
1034 (kill-buffer tmp2
))))))
1037 (defun change-log-merge (other-log)
1038 "Merge the contents of change log file OTHER-LOG with this buffer.
1039 Both must be found in Change Log mode (since the merging depends on
1040 the appropriate motion commands). OTHER-LOG can be either a file name
1043 Entries are inserted in chronological order. Both the current and
1044 old-style time formats for entries are supported."
1045 (interactive "*fLog file name to merge: ")
1046 (if (not (derived-mode-p 'change-log-mode
))
1047 (error "Not in Change Log mode"))
1048 (let ((other-buf (if (bufferp other-log
) other-log
1049 (find-file-noselect other-log
)))
1050 (buf (current-buffer))
1053 (goto-char (point-min))
1054 (set-buffer other-buf
)
1055 (goto-char (point-min))
1056 (if (not (derived-mode-p 'change-log-mode
))
1057 (error "%s not found in Change Log mode" other-log
))
1058 ;; Loop through all the entries in OTHER-LOG.
1060 (setq date1
(change-log-sortable-date-at))
1062 end
(progn (forward-page) (point)))
1063 ;; Look for an entry in original buffer that isn't later.
1064 (with-current-buffer buf
1065 (while (and (not (eobp))
1066 (string< date1
(change-log-sortable-date-at)))
1069 (insert-buffer-substring other-buf start end
)
1070 ;; At the end of the original buffer, insert a newline to
1071 ;; separate entries and then the rest of the file being
1074 (and (= ?
\n (char-before))
1075 (or (<= (1- (point)) (point-min))
1076 (= ?
\n (char-before (1- (point)))))))
1077 (insert (if use-hard-newlines hard-newline
"\n")))
1078 ;; Move to the end of it to terminate outer loop.
1079 (with-current-buffer other-buf
1080 (goto-char (point-max)))
1081 (insert-buffer-substring other-buf start
)))))))
1083 (defun change-log-beginning-of-defun ()
1084 (re-search-backward change-log-start-entry-re nil
'move
))
1086 (defun change-log-end-of-defun ()
1087 ;; Look back and if there is no entry there it means we are before
1088 ;; the first ChangeLog entry, so go forward until finding one.
1089 (unless (save-excursion (re-search-backward change-log-start-entry-re nil t
))
1090 (re-search-forward change-log-start-entry-re nil t
))
1092 ;; In case we are at the end of log entry going forward a line will
1093 ;; make us find the next entry when searching. If we are inside of
1094 ;; an entry going forward a line will still keep the point inside
1098 ;; In case we are at the beginning of an entry, move past it.
1099 (when (looking-at change-log-start-entry-re
)
1100 (goto-char (match-end 0))
1103 ;; Search for the start of the next log entry. Go to the end of the
1104 ;; buffer if we could not find a next entry.
1105 (when (re-search-forward change-log-start-entry-re nil
'move
)
1106 (goto-char (match-beginning 0))
1111 ;; arch-tag: 81eee6fc-088f-4372-a37f-80ad9620e762
1112 ;;; add-log.el ends here