1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985-1986, 1988, 1993-1994, 1997-1998, 2000-2015 Free
4 ;; Software Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
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 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
26 ;; This facility is documented in the Emacs Manual.
30 ;; - Find/use/create _MTN/log if there's a _MTN directory.
31 ;; - Find/use/create ++log.* if there's an {arch} directory.
32 ;; - Use an open *VC-Log* or *cvs-commit* buffer if it's related to the
34 ;; - Don't add TAB indents (and username?) if inserting entries in those
39 (defgroup change-log nil
40 "Change log maintenance."
42 :link
'(custom-manual "(emacs)Change Log")
47 (defcustom change-log-default-name nil
48 "Name of a change log file for \\[add-change-log-entry]."
49 :type
'(choice (const :tag
"default" nil
)
53 (put 'change-log-default-name
'safe-local-variable
'string-or-null-p
)
55 (defcustom change-log-mode-hook nil
56 "Normal hook run by `change-log-mode'."
60 ;; Many modes set this variable, so avoid warnings.
62 (defcustom add-log-current-defun-function nil
63 "If non-nil, function to guess name of surrounding function.
64 It is called by `add-log-current-defun' with no argument, and
65 should return the function's name as a string, or nil if point is
67 :type
'(choice (const nil
) function
)
71 (defcustom add-log-full-name nil
72 "Full name of user, for inclusion in ChangeLog daily headers.
73 This defaults to the value returned by the function `user-full-name'."
74 :type
'(choice (const :tag
"Default" nil
)
79 (defcustom add-log-mailing-address nil
80 "Email addresses of user, for inclusion in ChangeLog headers.
81 This defaults to the value of `user-mail-address'. In addition to
82 being a simple string, this value can also be a list. All elements
83 will be recognized as referring to the same user; when creating a new
84 ChangeLog entry, one element will be chosen at random."
85 :type
'(choice (const :tag
"Default" nil
)
86 (string :tag
"String")
87 (repeat :tag
"List of Strings" string
))
90 (defcustom add-log-time-format
'add-log-iso8601-time-string
91 "Function that defines the time format.
92 For example, `add-log-iso8601-time-string', which gives the
93 date in international ISO 8601 format,
94 and `current-time-string' are two valid values."
95 :type
'(radio (const :tag
"International ISO 8601 format"
96 add-log-iso8601-time-string
)
97 (const :tag
"Old format, as returned by `current-time-string'"
99 (function :tag
"Other"))
102 (defcustom add-log-keep-changes-together nil
103 "If non-nil, normally keep day's log entries for one file together.
105 Log entries for a given file made with \\[add-change-log-entry] or
106 \\[add-change-log-entry-other-window] will only be added to others \
108 today if this variable is non-nil or that file comes first in today's
109 entries. Otherwise another entry for that file will be started. An
113 * bar (...): change 1
115 in the latter case, \\[add-change-log-entry-other-window] in a \
116 buffer visiting `bar', yields:
120 * bar (...): change 1
125 * bar (...): change 1
128 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
134 (defcustom add-log-always-start-new-record nil
135 "If non-nil, `add-change-log-entry' will always start a new record."
140 (defvar add-log-buffer-file-name-function
'buffer-file-name
141 "If non-nil, function to call to identify the full filename of a buffer.
142 This function is called with no argument. The default is to
143 use `buffer-file-name'.")
145 (defcustom add-log-file-name-function nil
146 "If non-nil, function to call to identify the filename for a ChangeLog entry.
147 This function is called with one argument, the value of variable
148 `buffer-file-name' in that buffer. If this is nil, the default is to
149 use the file's name relative to the directory of the change log file."
150 :type
'(choice (const nil
) function
)
154 (defcustom change-log-version-info-enabled nil
155 "If non-nil, enable recording version numbers with the changes."
160 (defcustom change-log-version-number-regexp-list
161 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
163 ;; (defconst ad-version "2.15"
164 (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re
)
165 ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
166 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re
)))
167 "List of regexps to search for version number.
168 The version number must be in group 1.
169 Note: The search is conducted only within 10%, at the beginning of the file."
171 :type
'(repeat regexp
)
174 (defface change-log-date
175 '((t (:inherit font-lock-string-face
)))
176 "Face used to highlight dates in date lines."
179 (define-obsolete-face-alias 'change-log-date-face
'change-log-date
"22.1")
181 (defface change-log-name
182 '((t (:inherit font-lock-constant-face
)))
183 "Face for highlighting author names."
186 (define-obsolete-face-alias 'change-log-name-face
'change-log-name
"22.1")
188 (defface change-log-email
189 '((t (:inherit font-lock-variable-name-face
)))
190 "Face for highlighting author email addresses."
193 (define-obsolete-face-alias 'change-log-email-face
'change-log-email
"22.1")
195 (defface change-log-file
196 '((t (:inherit font-lock-function-name-face
)))
197 "Face for highlighting file names."
200 (define-obsolete-face-alias 'change-log-file-face
'change-log-file
"22.1")
202 (defface change-log-list
203 '((t (:inherit font-lock-keyword-face
)))
204 "Face for highlighting parenthesized lists of functions or variables."
207 (define-obsolete-face-alias 'change-log-list-face
'change-log-list
"22.1")
209 (defface change-log-conditionals
210 '((t (:inherit font-lock-variable-name-face
)))
211 "Face for highlighting conditionals of the form `[...]'."
214 (define-obsolete-face-alias 'change-log-conditionals-face
215 'change-log-conditionals
"22.1")
217 (defface change-log-function
218 '((t (:inherit font-lock-variable-name-face
)))
219 "Face for highlighting items of the form `<....>'."
222 (define-obsolete-face-alias 'change-log-function-face
223 'change-log-function
"22.1")
225 (defface change-log-acknowledgment
226 '((t (:inherit font-lock-comment-face
)))
227 "Face for highlighting acknowledgments."
230 (define-obsolete-face-alias 'change-log-acknowledgement
231 'change-log-acknowledgment
"24.3")
232 (define-obsolete-face-alias 'change-log-acknowledgement-face
233 'change-log-acknowledgment
"22.1")
235 (defconst change-log-file-names-re
"^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)")
236 (defconst change-log-start-entry-re
"^\\sw.........[0-9:+ ]*")
238 (defvar change-log-font-lock-keywords
240 ;; Date lines, new (2000-01-01) and old (Sat Jan 1 00:00:00 2000) styles.
241 ;; Fixme: this regexp is just an approximate one and may match
242 ;; wrongly with a non-date line existing as a random note. In
243 ;; addition, using any kind of fixed setting like this doesn't
244 ;; work if a user customizes add-log-time-format.
245 ("^[0-9-]+ +\\|^ \\{11,\\}\\|^\t \\{3,\\}\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+"
246 (0 'change-log-date-face
)
247 ;; Name and e-mail; some people put e-mail in parens, not angles.
248 ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
250 (2 'change-log-email
)))
253 (,change-log-file-names-re
255 ;; Possibly further names in a list:
256 ("\\=, \\([^ ,:([\n]+\\)" nil nil
(1 'change-log-file
))
257 ;; Possibly a parenthesized list of names:
258 ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
259 nil nil
(1 'change-log-list
))
260 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
261 nil nil
(1 'change-log-list
)))
263 ;; Function or variable names.
264 ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
266 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
267 (1 'change-log-list
)))
270 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals
))
272 ;; Function of change.
273 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function
))
276 ;; Don't include plain "From" because that is vague;
277 ;; we want to encourage people to say something more specific.
278 ;; Note that the FSF does not use "Patches by"; our convention
279 ;; is to put the name of the author of the changes at the top
280 ;; of the change log entry.
281 ("\\(^\\( +\\|\t\\)\\| \\)\\(Thanks to\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
282 3 'change-log-acknowledgment
))
283 "Additional expressions to highlight in Change Log mode.")
285 (defun change-log-search-file-name (where)
286 "Return the file-name for the change under point."
289 (beginning-of-line 1)
290 (if (looking-at change-log-start-entry-re
)
291 ;; We are at the start of an entry, search forward for a file
294 (re-search-forward change-log-file-names-re nil t
)
295 (match-string-no-properties 2))
296 (if (looking-at change-log-file-names-re
)
297 ;; We found a file name.
298 (match-string-no-properties 2)
299 ;; Look backwards for either a file name or the log entry start.
300 (if (re-search-backward
301 (concat "\\(" change-log-start-entry-re
303 change-log-file-names-re
"\\)") nil t
)
304 (if (match-beginning 1)
305 ;; We got the start of the entry, look forward for a
308 (re-search-forward change-log-file-names-re nil t
)
309 (match-string-no-properties 2))
310 (match-string-no-properties 4))
311 ;; We must be before any file name, look forward.
312 (re-search-forward change-log-file-names-re nil t
)
313 (match-string-no-properties 2))))))
315 (defun change-log-find-file ()
316 "Visit the file for the change under point."
318 (let ((file (change-log-search-file-name (point))))
319 (if (and file
(file-exists-p file
))
321 (message "No such file or directory: %s" file
))))
323 (defun change-log-search-tag-name-1 (&optional from
)
324 "Search for a tag name within subexpression 1 of last match.
325 Optional argument FROM specifies a buffer position where the tag
326 name should be located. Return value is a cons whose car is the
327 string representing the tag and whose cdr is the position where
330 (narrow-to-region (match-beginning 1) (match-end 1))
331 (when from
(goto-char from
))
332 ;; The regexp below skips any symbol near `point' (FROM) followed by
333 ;; whitespace and another symbol. This should skip, for example,
334 ;; "struct" in a specification like "(struct buffer)" and move to
335 ;; "buffer". A leading paren is ignored.
337 "[(]?\\(?:\\(?:\\sw\\|\\s_\\)+\\(?:[ \t]+\\(\\sw\\|\\s_\\)+\\)\\)")
338 (goto-char (match-beginning 1)))
339 (cons (find-tag-default) (point))))
341 (defconst change-log-tag-re
342 "(\\(\\(?:\\sw\\|\\s_\\)+\\(?:[, \t]+\\(?:\\sw\\|\\s_\\)+\\)*\\))"
343 "Regexp matching a tag name in change log entries.")
345 (defun change-log-search-tag-name (&optional at
)
346 "Search for a tag name near `point'.
347 Optional argument AT non-nil means search near buffer position AT.
348 Return value is a cons whose car is the string representing
349 the tag and whose cdr is the position where the tag was found."
351 (goto-char (setq at
(or at
(point))))
354 (or (condition-case nil
355 ;; Within parenthesized list?
358 (when (looking-at change-log-tag-re
)
359 (change-log-search-tag-name-1 at
)))
362 ;; Before parenthesized list on same line?
364 (when (and (skip-chars-forward " \t")
365 (looking-at change-log-tag-re
))
366 (change-log-search-tag-name-1)))
373 (looking-at change-log-file-names-re
))
374 (goto-char (match-end 0))
375 (skip-syntax-forward " ")
376 (looking-at change-log-tag-re
))
377 (change-log-search-tag-name-1)))
380 ;; Anywhere else within current entry?
384 (if (re-search-backward change-log-start-entry-re nil t
)
390 (if (re-search-forward change-log-start-entry-re nil t
)
393 (when (and (< from to
) (<= from at
) (<= at to
))
395 ;; Narrow to current change log entry.
396 (narrow-to-region from to
)
398 ((re-search-backward change-log-tag-re nil t
)
399 (narrow-to-region (match-beginning 1) (match-end 1))
400 (goto-char (point-max))
401 (cons (find-tag-default) (point-max)))
402 ((re-search-forward change-log-tag-re nil t
)
403 (narrow-to-region (match-beginning 1) (match-end 1))
404 (goto-char (point-min))
405 (cons (find-tag-default) (point-min)))))))
408 (defvar change-log-find-head nil
)
409 (defvar change-log-find-tail nil
)
410 (defvar change-log-find-window nil
)
412 (defun change-log-goto-source-1 (tag regexp file buffer
413 &optional window first last
)
414 "Search for tag TAG in buffer BUFFER visiting file FILE.
415 REGEXP is a regular expression for TAG. The remaining arguments
416 are optional: WINDOW denotes the window to display the results of
417 the search. FIRST is a position in BUFFER denoting the first
418 match from previous searches for TAG. LAST is the position in
419 BUFFER denoting the last match for TAG in the last search."
420 (with-current-buffer buffer
426 ;; When LAST is set make sure we continue from the next
427 ;; line end to not find the same tag again.
431 ;; Try to go to the end of the current defun to avoid
432 ;; false positives within the current defun's body
433 ;; since these would match `add-log-current-defun'.
435 ;; Don't fall behind when `end-of-defun' fails.
436 (error (progn (goto-char last
) (end-of-line))))
438 ;; When LAST was not set start at beginning of BUFFER.
439 (goto-char (point-min)))
441 (while (and (not last
) (re-search-forward regexp nil t
))
442 ;; Verify that `add-log-current-defun' invoked at the end
443 ;; of the match returns TAG. This heuristic works well
444 ;; whenever the name of the defun occurs within the first
445 ;; line of the defun.
446 (setq current-defun
(add-log-current-defun))
447 (when (and current-defun
(string-equal current-defun
tag))
448 ;; Record this as last match.
449 (setq last
(line-beginning-position))
450 ;; Record this as first match when there's none.
451 (unless first
(setq first last
)))))))
453 (with-selected-window
454 (setq change-log-find-window
(or window
(display-buffer buffer
)))
457 (when (or (< last
(point-min)) (> last
(point-max)))
458 ;; Widen to show TAG.
462 ;; When there are no more matches go (back) to FIRST.
463 (message "No more matches for tag `%s' in file `%s'" tag file
)
466 ;; Return new "tail".
467 (list (selected-window) first last
))
468 (message "Source location of tag `%s' not found in file `%s'" tag file
)
471 (defun change-log-goto-source ()
472 "Go to source location of \"change log tag\" near `point'.
473 A change log tag is a symbol within a parenthesized,
474 comma-separated list. If no suitable tag can be found nearby,
475 try to visit the file for the change under `point' instead."
477 (if (and (eq last-command
'change-log-goto-source
)
478 change-log-find-tail
)
479 (setq change-log-find-tail
481 (apply 'change-log-goto-source-1
482 (append change-log-find-head change-log-find-tail
))
484 (format "Cannot find more matches for tag `%s' in file `%s'"
485 (car change-log-find-head
)
486 (nth 2 change-log-find-head
)))))
489 (tag-at (change-log-search-tag-name))
491 (file (when tag-at
(change-log-search-file-name (cdr tag-at
))))
492 (file-at (when file
(match-beginning 2)))
493 ;; `file-2' is the file `change-log-search-file-name' finds
494 ;; at `point'. We use `file-2' as a fallback when `tag' or
495 ;; `file' are not suitable for some reason.
496 (file-2 (change-log-search-file-name at
))
497 (file-2-at (when file-2
(match-beginning 2))))
499 ((and (or (not tag
) (not file
) (not (file-exists-p file
)))
500 (or (not file-2
) (not (file-exists-p file-2
))))
501 (error "Cannot find tag or file near `point'"))
502 ((and file-2
(file-exists-p file-2
)
503 (or (not tag
) (not file
) (not (file-exists-p file
))
504 (and (or (and (< file-at file-2-at
) (<= file-2-at at
))
505 (and (<= at file-2-at
) (< file-2-at file-at
))))))
506 ;; We either have not found a suitable file name or `file-2'
507 ;; provides a "better" file name wrt `point'. Go to the
508 ;; buffer of `file-2' instead.
509 (setq change-log-find-window
510 (display-buffer (find-file-noselect file-2
))))
512 (setq change-log-find-head
513 (list tag
(concat "\\_<" (regexp-quote tag
) "\\_>")
514 file
(find-file-noselect file
)))
516 (setq change-log-find-tail
517 (apply 'change-log-goto-source-1 change-log-find-head
))
519 (format "Cannot find matches for tag `%s' in file `%s'"
522 (defun change-log-next-error (&optional argp reset
)
523 "Move to the Nth (default 1) next match in a ChangeLog buffer.
524 Compatibility function for \\[next-error] invocations."
526 (let* ((argp (or argp
0))
527 (count (abs argp
)) ; how many cycles
528 (down (< argp
0)) ; are we going down? (is argp negative?)
530 (search-function (if up
're-search-forward
're-search-backward
)))
532 ;; set the starting position
533 (goto-char (cond (reset (point-min))
534 (down (line-beginning-position))
535 (up (line-end-position))
538 (funcall search-function change-log-file-names-re nil t count
))
541 ;; if we found a place to visit...
542 (when (looking-at change-log-file-names-re
)
543 (let (change-log-find-window)
544 (change-log-goto-source)
545 (when change-log-find-window
546 ;; Select window displaying source file.
547 (select-window change-log-find-window
)))))
549 (defvar change-log-mode-map
550 (let ((map (make-sparse-keymap))
551 (menu-map (make-sparse-keymap)))
552 (define-key map
[?\C-c ?\C-p
] 'add-log-edit-prev-comment
)
553 (define-key map
[?\C-c ?\C-n
] 'add-log-edit-next-comment
)
554 (define-key map
[?\C-c ?\C-f
] 'change-log-find-file
)
555 (define-key map
[?\C-c ?\C-c
] 'change-log-goto-source
)
556 (define-key map
[menu-bar changelog
] (cons "ChangeLog" menu-map
))
557 (define-key menu-map
[gs]
558 '(menu-item "Go To Source" change-log-goto-source
559 :help "Go to source location of ChangeLog tag near point"))
560 (define-key menu-map [ff]
561 '(menu-item "Find File" change-log-find-file
562 :help "Visit the file for the change under point"))
563 (define-key menu-map [sep] '("--"))
564 (define-key menu-map [nx]
565 '(menu-item "Next Log-Edit Comment" add-log-edit-next-comment
566 :help "Cycle forward through Log-Edit mode comment history"))
567 (define-key menu-map [pr]
568 '(menu-item "Previous Log-Edit Comment" add-log-edit-prev-comment
569 :help "Cycle backward through Log-Edit mode comment history"))
571 "Keymap for Change Log major mode.")
573 ;; It used to be called change-log-time-zone-rule but really should be
574 ;; called add-log-time-zone-rule since it's only used from add-log-* code.
575 (defvaralias 'change-log-time-zone-rule 'add-log-time-zone-rule)
576 (defvar add-log-time-zone-rule nil
577 "Time zone used for calculating change log time stamps.
578 It takes the same format as the TZ argument of `set-time-zone-rule'.
579 If nil, use local time.
580 If t, use universal time.")
581 (put 'add-log-time-zone-rule 'safe-local-variable
582 (lambda (x) (or (booleanp x) (stringp x))))
584 (defun add-log-iso8601-time-zone (&optional time)
585 (let* ((utc-offset (or (car (current-time-zone time)) 0))
586 (sign (if (< utc-offset 0) ?- ?+))
587 (sec (abs utc-offset))
592 (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
593 ((not (zerop mm)) "%c%02d:%02d")
597 (defvar add-log-iso8601-with-time-zone nil)
599 (defun add-log-iso8601-time-string ()
600 (let ((time (format-time-string "%Y-%m-%d"
601 nil (eq t add-log-time-zone-rule))))
602 (if add-log-iso8601-with-time-zone
603 (concat time " " (add-log-iso8601-time-zone))
606 (defun change-log-name ()
607 "Return (system-dependent) default name for a change log file."
608 (or change-log-default-name
611 (defun add-log-edit-prev-comment (arg)
612 "Cycle backward through Log-Edit mode comment history.
613 With a numeric prefix ARG, go back ARG comments."
616 (narrow-to-region (point)
617 (if (memq last-command '(add-log-edit-prev-comment
618 add-log-edit-next-comment))
620 (when (fboundp 'log-edit-previous-comment)
621 (log-edit-previous-comment arg)
622 (indent-region (point-min) (point-max))
623 (goto-char (point-min))
624 (unless (save-restriction (widen) (bolp))
625 (delete-region (point) (progn (skip-chars-forward " \t\n") (point))))
626 (set-mark (point-min))
627 (goto-char (point-max))
628 (delete-region (point) (progn (skip-chars-backward " \t\n") (point))))))
630 (defun add-log-edit-next-comment (arg)
631 "Cycle forward through Log-Edit mode comment history.
632 With a numeric prefix ARG, go back ARG comments."
634 (add-log-edit-prev-comment (- arg)))
637 (defun prompt-for-change-log-name ()
638 "Prompt for a change log name."
639 (let* ((default (change-log-name))
640 (name (expand-file-name
641 (read-file-name (format "Log file (default %s): " default)
643 ;; Handle something that is syntactically a directory name.
644 ;; Look for ChangeLog or whatever in that directory.
645 (if (string= (file-name-nondirectory name) "")
646 (expand-file-name (file-name-nondirectory default)
648 ;; Handle specifying a file that is a directory.
649 (if (file-directory-p name)
650 (expand-file-name (file-name-nondirectory default)
651 (file-name-as-directory name))
654 (defun change-log-version-number-search ()
655 "Return version number of current buffer's file.
656 This is the value returned by `vc-working-revision' or, if that is
657 nil, by matching `change-log-version-number-regexp-list'."
658 (let* ((size (buffer-size))
660 ;; The version number can be anywhere in the file, but
661 ;; restrict search to the file beginning: 10% should be
662 ;; enough to prevent some mishits.
664 ;; Apply percentage only if buffer size is bigger than
666 (if (> size (* 100 80)) (+ (point) (/ size 10)))))
667 (or (and buffer-file-name (vc-working-revision buffer-file-name))
670 (let ((regexps change-log-version-number-regexp-list)
674 (goto-char (point-min))
675 (when (re-search-forward (pop regexps) limit t)
676 (setq version (match-string 1)
680 (declare-function diff-find-source-location "diff-mode"
681 (&optional other-file reverse noprompt))
684 (defun find-change-log (&optional file-name buffer-file)
685 "Find a change log file for \\[add-change-log-entry] and return the name.
687 Optional arg FILE-NAME specifies the file to use.
688 If FILE-NAME is nil, use the value of `change-log-default-name'.
689 If `change-log-default-name' is nil, behave as though it were 'ChangeLog'
690 \(or whatever we use on this operating system).
692 If `change-log-default-name' contains a leading directory component, then
693 simply find it in the current directory. Otherwise, search in the current
694 directory and its successive parents for a file so named.
696 Once a file is found, `change-log-default-name' is set locally in the
697 current buffer to the complete file name.
698 Optional arg BUFFER-FILE overrides `buffer-file-name'."
699 ;; If we are called from a diff, first switch to the source buffer;
700 ;; in order to respect buffer-local settings of change-log-default-name, etc.
701 (with-current-buffer (let ((buff (if (derived-mode-p 'diff-mode)
703 (diff-find-source-location))))))
704 (if (buffer-live-p buff) buff
706 ;; If user specified a file name or if this buffer knows which one to use,
709 (setq file-name (and change-log-default-name
710 (file-name-directory change-log-default-name)
711 change-log-default-name))
713 ;; Chase links in the source file
714 ;; and use the change log in the dir where it points.
715 (setq file-name (or (and (or buffer-file buffer-file-name)
718 (or buffer-file buffer-file-name))))
720 (if (file-directory-p file-name)
721 (setq file-name (expand-file-name (change-log-name) file-name)))
722 ;; Chase links before visiting the file.
723 ;; This makes it easier to use a single change log file
724 ;; for several related directories.
725 (setq file-name (file-chase-links file-name))
726 (setq file-name (expand-file-name file-name))
727 ;; Move up in the dir hierarchy till we find a change log file.
728 (let ((file1 file-name)
730 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
731 (progn (setq parent-dir
734 (file-name-directory file1))))
735 ;; Give up if we are already at the root dir.
736 (not (string= (file-name-directory file1)
738 ;; Move up to the parent dir and try again.
739 (setq file1 (expand-file-name
740 (file-name-nondirectory (change-log-name))
742 ;; If we found a change log in a parent, use that.
743 (if (or (get-file-buffer file1) (file-exists-p file1))
744 (setq file-name file1)))))
745 ;; Make a local variable in this buffer so we needn't search again.
746 (set (make-local-variable 'change-log-default-name) file-name))
749 (defun add-log-file-name (buffer-file log-file)
750 ;; Never want to add a change log entry for the ChangeLog file itself.
751 (unless (or (null buffer-file) (string= buffer-file log-file))
752 (if add-log-file-name-function
753 (funcall add-log-file-name-function buffer-file)
755 (let* ((dir (file-name-directory log-file))
756 (rel (file-relative-name buffer-file dir)))
757 ;; Sometimes with symlinks, the two buffers may have names that
758 ;; appear to belong to different directory trees. So check the
759 ;; file-truenames, to see if we get a better result.
760 (if (not (string-match "\\`\\.\\./" rel))
762 (let ((new (file-relative-name (file-truename buffer-file)
763 (file-truename dir))))
764 (if (< (length new) (length rel))
766 ;; If we have a backup file, it's presumably because we're
767 ;; comparing old and new versions (e.g. for deleted
768 ;; functions) and we'll want to use the original name.
769 (if (backup-file-name-p buffer-file)
770 (file-name-sans-versions buffer-file)
774 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
775 put-new-entry-on-new-line)
776 "Find change log file, and add an entry for today and an item for this file.
777 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
778 name and email (stored in `add-log-full-name' and `add-log-mailing-address').
780 Second arg FILE-NAME is file name of the change log.
781 If nil, use the value of `change-log-default-name'.
783 Third arg OTHER-WINDOW non-nil means visit in other window.
785 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
786 never append to an existing entry. Option `add-log-keep-changes-together'
787 otherwise affects whether a new entry is created.
789 Fifth arg PUT-NEW-ENTRY-ON-NEW-LINE non-nil means that if a new
790 entry is created, put it on a new line by itself, do not put it
791 after a comma on an existing line.
793 Option `add-log-always-start-new-record' non-nil means always create a
794 new record, even when the last record was made on the same date and by
797 The change log file can start with a copyright notice and a copying
798 permission notice. The first blank line indicates the end of these
801 Today's date is calculated according to `add-log-time-zone-rule' if
802 non-nil, otherwise in local time."
803 (interactive (list current-prefix-arg
804 (prompt-for-change-log-name)))
805 (let* ((defun (add-log-current-defun))
806 (version (and change-log-version-info-enabled
807 (change-log-version-number-search)))
808 (buf-file-name (funcall add-log-buffer-file-name-function))
809 (buffer-file (if buf-file-name (expand-file-name buf-file-name)))
810 (file-name (expand-file-name (find-change-log file-name buffer-file)))
811 ;; Set ITEM to the file name to use in the new item.
812 (item (add-log-file-name buffer-file file-name)))
814 (unless (equal file-name buffer-file-name)
816 ((equal file-name (buffer-file-name (window-buffer)))
817 ;; If the selected window already shows the desired buffer don't show
818 ;; it again (particularly important if other-window is true).
819 ;; This is important for diff-add-change-log-entries-other-window.
820 (set-buffer (window-buffer)))
821 ((or other-window (window-dedicated-p))
822 (find-file-other-window file-name))
823 (t (find-file file-name))))
824 (or (derived-mode-p 'change-log-mode)
827 (goto-char (point-min))
829 (let ((full-name (or add-log-full-name (user-full-name)))
830 (mailing-address (or add-log-mailing-address user-mail-address)))
833 (setq full-name (read-string "Full name: " full-name))
834 ;; Note that some sites have room and phone number fields in
835 ;; full name which look silly when inserted. Rather than do
836 ;; anything about that here, let user give prefix argument so that
837 ;; s/he can edit the full name field in prompter if s/he wants.
838 (setq mailing-address
839 (read-string "Mailing address: " mailing-address)))
841 ;; If file starts with a copyright and permission notice, skip them.
842 ;; Assume they end at first blank line.
843 (when (looking-at "Copyright")
844 (search-forward "\n\n")
845 (skip-chars-forward "\n"))
847 ;; Advance into first entry if it is usable; else make new one.
849 (mapcar (lambda (addr)
851 (if (stringp add-log-time-zone-rule)
852 (let ((tz (getenv "TZ")))
855 (setenv "TZ" add-log-time-zone-rule)
856 (funcall add-log-time-format))
858 (funcall add-log-time-format))
861 (if (consp mailing-address)
863 (list mailing-address)))))
864 (if (and (not add-log-always-start-new-record)
866 (dolist (entry new-entries hit)
867 (and (looking-at (regexp-quote entry))
868 ;; Reject multiple author entries. (Bug#8645)
871 (not (looking-at "[ \t]+.*<.*>$")))
874 (insert (nth (random (length new-entries))
876 (if use-hard-newlines hard-newline "\n")
877 (if use-hard-newlines hard-newline "\n"))
880 ;; Determine where we should stop searching for a usable
881 ;; item to add to, within this entry.
884 (if (looking-at "\n*[^\n* \t]")
885 (skip-chars-forward "\n")
886 (if add-log-keep-changes-together
887 (forward-page) ; page delimits entries for date
888 (forward-paragraph))) ; paragraph delimits entries for file
891 ;; Now insert the new line for this item.
892 (cond ((re-search-forward "^\\s *\\* *$" bound t)
893 ;; Put this file name into the existing empty item.
896 ((and (not new-entry)
897 (let (case-fold-search)
899 (concat (regexp-quote (concat "* " item))
900 ;; Don't accept `foo.bar' when
901 ;; looking for `foo':
902 "\\(\\s \\|[(),:]\\)")
904 ;; Add to the existing item for the same file.
905 (re-search-forward "^\\s *$\\|^\\s \\*")
906 (goto-char (match-beginning 0))
907 ;; Delete excess empty lines; make just 2.
908 (while (and (not (eobp)) (looking-at "^\\s *$"))
909 (delete-region (point) (line-beginning-position 2)))
910 (insert (if use-hard-newlines hard-newline "\n")
911 (if use-hard-newlines hard-newline "\n"))
913 (indent-relative-maybe))
916 (while (looking-at "\\sW")
918 (while (and (not (eobp)) (looking-at "^\\s *$"))
919 (delete-region (point) (line-beginning-position 2)))
920 (insert (if use-hard-newlines hard-newline "\n")
921 (if use-hard-newlines hard-newline "\n")
922 (if use-hard-newlines hard-newline "\n"))
924 (indent-to left-margin)
926 (if item (insert item)))))
927 ;; Now insert the function name, if we have one.
928 ;; Point is at the item for this file,
929 ;; either at the end of the line or at the first blank line.
931 ;; No function name, so put in a colon unless we have just a star.
932 (unless (save-excursion
933 (beginning-of-line 1)
934 (looking-at "\\s *\\(\\* *\\)?$"))
936 (if version (insert version ?\s)))
937 ;; Make it easy to get rid of the function name.
939 (unless (save-excursion
940 (beginning-of-line 1)
941 (looking-at "\\s *$"))
943 ;; See if the prev function name has a message yet or not.
944 ;; If not, merge the two items.
945 (let ((pos (point-marker)))
946 (skip-syntax-backward " ")
947 (skip-chars-backward "):")
948 (if (and (not put-new-entry-on-new-line)
950 (let ((pos (save-excursion (backward-sexp 1) (point))))
951 (when (equal (buffer-substring pos (point)) defun)
952 (delete-region pos (point)))
953 (> fill-column (+ (current-column) (length defun) 4))))
954 (progn (skip-chars-backward ", ")
955 (delete-region (point) pos)
956 (unless (memq (char-before) '(?\()) (insert ", ")))
957 (when (and (not put-new-entry-on-new-line) (looking-at "):"))
958 (delete-region (+ 1 (point)) (line-end-position)))
961 (set-marker pos nil))
963 (if version (insert version ?\s)))))
966 (defun add-change-log-entry-other-window (&optional whoami file-name)
967 "Find change log file in other window and add entry and item.
968 This is just like `add-change-log-entry' except that it displays
969 the change log file in another window."
970 (interactive (if current-prefix-arg
971 (list current-prefix-arg
972 (prompt-for-change-log-name))))
973 (add-change-log-entry whoami file-name t))
976 (defvar change-log-indent-text 0)
978 (defun change-log-fill-parenthesized-list ()
979 ;; Fill parenthesized lists of names according to GNU standards.
980 ;; * file-name.ext (very-long-foo, very-long-bar, very-long-foobar):
981 ;; should be filled as
982 ;; * file-name.ext (very-long-foo, very-long-bar)
983 ;; (very-long-foobar):
986 (skip-chars-backward " \t")
987 (when (and (equal (char-before) ?\,)
988 (> (point) (1+ (point-min))))
990 (when (save-excursion
993 (equal (char-after) ?\()
994 (skip-chars-backward " \t"))
996 ;; Skip everything but a whitespace or asterisk.
997 (and (not (zerop (skip-chars-backward "^ \t\n*")))
998 (skip-chars-backward " \t")
999 ;; We want one asterisk here.
1000 (= (skip-chars-backward "*") -1)
1001 (skip-chars-backward " \t")
1003 ;; Delete the comma.
1005 ;; Close list on previous line.
1007 (skip-chars-forward " \t\n")
1008 ;; Start list on new line.
1009 (insert-before-markers "("))
1012 (defun change-log-indent ()
1013 (change-log-fill-parenthesized-list)
1017 (skip-chars-forward " \t")
1019 ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
1020 ;; Matching the output of add-log-time-format is difficult,
1021 ;; but I'll get it has at least two adjacent digits.
1022 (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
1024 ((looking-at "[^*(]")
1025 (+ (current-left-margin) change-log-indent-text))
1026 (t (current-left-margin)))))
1027 (pos (save-excursion (indent-line-to indent) (point))))
1028 (if (> pos (point)) (goto-char pos))))
1031 (defvar smerge-resolve-function)
1032 (defvar copyright-at-end-flag)
1035 (define-derived-mode change-log-mode text-mode "Change Log"
1036 "Major mode for editing change logs; like Indented Text mode.
1037 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
1038 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
1039 Each entry behaves as a paragraph, and the entries for one day as a page.
1040 Runs `change-log-mode-hook'.
1041 \n\\{change-log-mode-map}"
1046 show-trailing-whitespace t)
1047 (set (make-local-variable 'fill-forward-paragraph-function)
1048 'change-log-fill-forward-paragraph)
1049 (set (make-local-variable 'comment-start) nil)
1050 ;; Make sure we call `change-log-indent' when filling.
1051 (set (make-local-variable 'fill-indent-according-to-mode) t)
1052 ;; Avoid that filling leaves behind a single "*" on a line.
1053 (add-hook 'fill-nobreak-predicate
1055 (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
1057 (set (make-local-variable 'indent-line-function) 'change-log-indent)
1058 (set (make-local-variable 'tab-always-indent) nil)
1059 (set (make-local-variable 'copyright-at-end-flag) t)
1060 ;; We really do want "^" in paragraph-start below: it is only the
1061 ;; lines that begin at column 0 (despite the left-margin of 8) that
1062 ;; we are looking for. Adding `* ' allows eliding the blank line
1063 ;; between entries for different files.
1064 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
1065 (set (make-local-variable 'paragraph-separate) paragraph-start)
1066 ;; Match null string on the date-line so that the date-line
1067 ;; is grouped with what follows.
1068 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
1069 (set (make-local-variable 'version-control) 'never)
1070 (set (make-local-variable 'smerge-resolve-function)
1071 'change-log-resolve-conflict)
1072 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
1073 (set (make-local-variable 'font-lock-defaults)
1074 '(change-log-font-lock-keywords t nil nil backward-paragraph))
1075 (set (make-local-variable 'multi-isearch-next-buffer-function)
1076 'change-log-next-buffer)
1077 (set (make-local-variable 'beginning-of-defun-function)
1078 'change-log-beginning-of-defun)
1079 (set (make-local-variable 'end-of-defun-function)
1080 'change-log-end-of-defun)
1081 ;; next-error function glue
1082 (setq next-error-function 'change-log-next-error)
1083 (setq next-error-last-buffer (current-buffer)))
1085 (defun change-log-next-buffer (&optional buffer wrap)
1086 "Return the next buffer in the series of ChangeLog file buffers.
1087 This function is used for multiple buffers isearch.
1088 A sequence of buffers is formed by ChangeLog files with decreasing
1089 numeric file name suffixes in the directory of the initial ChangeLog
1090 file were isearch was started."
1091 (let* ((name (change-log-name))
1092 (files (cons name (sort (file-expand-wildcards
1093 (concat name "[-.][0-9]*"))
1095 ;; The file's extension may not have a valid
1096 ;; version form (e.g. VC backup revisions).
1098 (version< (substring b (length name))
1099 (substring a (length name))))))))
1100 (files (if isearch-forward files (reverse files))))
1104 (cadr (member (file-name-nondirectory (buffer-file-name buffer))
1107 (defun change-log-fill-forward-paragraph (n)
1108 "Cut paragraphs so filling preserves open parentheses at beginning of lines."
1109 (let (;; Add lines starting with whitespace followed by a left paren or an
1111 (paragraph-start (concat paragraph-start "\\|\\s *\\(?:\\s(\\|\\*\\)")))
1112 (forward-paragraph n)))
1114 (defcustom add-log-current-defun-header-regexp
1115 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
1116 "Heuristic regexp used by `add-log-current-defun' for unknown major modes.
1117 The regexp's first submatch is placed in the ChangeLog entry, in
1122 (declare-function c-cpp-define-name "cc-cmds" ())
1123 (declare-function c-defun-name "cc-cmds" ())
1126 (defun add-log-current-defun ()
1127 "Return name of function definition point is in, or nil.
1129 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
1130 Texinfo (@node titles) and Perl.
1132 Other modes are handled by a heuristic that looks in the 10K before
1133 point for uppercase headings starting in the first column or
1134 identifiers followed by `:' or `='. See variables
1135 `add-log-current-defun-header-regexp' and
1136 `add-log-current-defun-function'.
1138 Has a preference of looking backwards."
1141 (if add-log-current-defun-function
1142 (funcall add-log-current-defun-function)
1143 ;; If all else fails, try heuristics
1144 (let (case-fold-search
1147 (when (re-search-backward add-log-current-defun-header-regexp
1148 (- (point) 10000) t)
1149 (setq result (or (match-string-no-properties 1)
1150 (match-string-no-properties 0)))
1151 ;; Strip whitespace away
1152 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
1154 (setq result (match-string-no-properties 1 result)))
1158 (defvar change-log-get-method-definition-md)
1160 ;; Subroutine used within change-log-get-method-definition.
1161 ;; Add the last match in the buffer to the end of `md',
1162 ;; followed by the string END; move to the end of that match.
1163 (defun change-log-get-method-definition-1 (end)
1164 (setq change-log-get-method-definition-md
1165 (concat change-log-get-method-definition-md
1168 (goto-char (match-end 0)))
1170 (defun change-log-get-method-definition ()
1171 "For Objective C, return the method name if we are in a method."
1172 (let ((change-log-get-method-definition-md "["))
1174 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
1175 (change-log-get-method-definition-1 " ")))
1178 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
1179 (change-log-get-method-definition-1 "")
1180 (while (not (looking-at "[{;]"))
1182 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
1183 (change-log-get-method-definition-1 ""))
1184 (concat change-log-get-method-definition-md "]"))))))
1186 (autoload 'timezone-make-date-sortable "timezone")
1188 (defun change-log-sortable-date-at ()
1189 "Return date of log entry in a consistent form for sorting.
1190 Point is assumed to be at the start of the entry."
1191 (if (looking-at change-log-start-entry-re)
1192 (let ((date (match-string-no-properties 0)))
1194 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
1195 (concat (match-string 1 date) (match-string 2 date)
1196 (match-string 3 date))
1197 (ignore-errors (timezone-make-date-sortable date)))))
1198 (error "Bad date")))
1200 (defun change-log-resolve-conflict ()
1201 "Function to be used in `smerge-resolve-function'."
1204 (narrow-to-region (match-beginning 0) (match-end 0))
1205 (let ((mb1 (match-beginning 1))
1207 (mb3 (match-beginning 3))
1209 (tmp1 (generate-new-buffer " *changelog-resolve-1*"))
1210 (tmp2 (generate-new-buffer " *changelog-resolve-2*")))
1212 (let ((buf (current-buffer)))
1213 (with-current-buffer tmp1
1215 (insert-buffer-substring buf mb1 me1))
1216 (with-current-buffer tmp2
1218 (insert-buffer-substring buf mb3 me3)
1219 ;; Do the merge here instead of inside `buf' so as to be
1220 ;; more robust in case change-log-merge fails.
1221 (change-log-merge tmp1))
1222 (goto-char (point-max))
1223 (delete-region (point-min)
1225 (insert-buffer-substring tmp2))))
1227 (kill-buffer tmp2))))))
1230 (defun change-log-merge (other-log)
1231 "Merge the contents of change log file OTHER-LOG with this buffer.
1232 Both must be found in Change Log mode (since the merging depends on
1233 the appropriate motion commands). OTHER-LOG can be either a file name
1236 Entries are inserted in chronological order. Both the current and
1237 old-style time formats for entries are supported."
1238 (interactive "*fLog file name to merge: ")
1239 (if (not (derived-mode-p 'change-log-mode))
1240 (error "Not in Change Log mode"))
1241 (let ((other-buf (if (bufferp other-log) other-log
1242 (find-file-noselect other-log)))
1243 (buf (current-buffer))
1246 (goto-char (point-min))
1247 (set-buffer other-buf)
1248 (goto-char (point-min))
1249 (if (not (derived-mode-p 'change-log-mode))
1250 (error "%s not found in Change Log mode" other-log))
1251 ;; Loop through all the entries in OTHER-LOG.
1253 (setq date1 (change-log-sortable-date-at))
1255 end (progn (forward-page) (point)))
1256 ;; Look for an entry in original buffer that isn't later.
1257 (with-current-buffer buf
1258 (while (and (not (eobp))
1259 (string< date1 (change-log-sortable-date-at)))
1262 (insert-buffer-substring other-buf start end)
1263 ;; At the end of the original buffer, insert a newline to
1264 ;; separate entries and then the rest of the file being
1267 (and (= ?\n (char-before))
1268 (or (<= (1- (point)) (point-min))
1269 (= ?\n (char-before (1- (point)))))))
1270 (insert (if use-hard-newlines hard-newline "\n")))
1271 ;; Move to the end of it to terminate outer loop.
1272 (with-current-buffer other-buf
1273 (goto-char (point-max)))
1274 (insert-buffer-substring other-buf start)))))))
1276 (defun change-log-beginning-of-defun ()
1277 (re-search-backward change-log-start-entry-re nil 'move))
1279 (defun change-log-end-of-defun ()
1280 ;; Look back and if there is no entry there it means we are before
1281 ;; the first ChangeLog entry, so go forward until finding one.
1282 (unless (save-excursion (re-search-backward change-log-start-entry-re nil t))
1283 (re-search-forward change-log-start-entry-re nil t))
1285 ;; In case we are at the end of log entry going forward a line will
1286 ;; make us find the next entry when searching. If we are inside of
1287 ;; an entry going forward a line will still keep the point inside
1291 ;; In case we are at the beginning of an entry, move past it.
1292 (when (looking-at change-log-start-entry-re)
1293 (goto-char (match-end 0))
1296 ;; Search for the start of the next log entry. Go to the end of the
1297 ;; buffer if we could not find a next entry.
1298 (when (re-search-forward change-log-start-entry-re nil 'move)
1299 (goto-char (match-beginning 0))
1304 ;;; add-log.el ends here