1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985-1986, 1988, 1993-1994, 1997-1998, 2000-2017 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 <https://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 (defcustom change-log-directory-files
'(".bzr" ".git" ".hg" ".svn")
175 "List of files that cause `find-change-log' to stop in containing directory.
176 This applies if no pre-existing ChangeLog is found. If nil, then in such
177 a case simply use the directory containing the changed file."
182 (defface change-log-date
183 '((t (:inherit font-lock-string-face
)))
184 "Face used to highlight dates in date lines."
188 (defface change-log-name
189 '((t (:inherit font-lock-constant-face
)))
190 "Face for highlighting author names."
194 (defface change-log-email
195 '((t (:inherit font-lock-variable-name-face
)))
196 "Face for highlighting author email addresses."
200 (defface change-log-file
201 '((t (:inherit font-lock-function-name-face
)))
202 "Face for highlighting file names."
206 (defface change-log-list
207 '((t (:inherit font-lock-keyword-face
)))
208 "Face for highlighting parenthesized lists of functions or variables."
212 (defface change-log-conditionals
213 '((t (:inherit font-lock-variable-name-face
)))
214 "Face for highlighting conditionals of the form `[...]'."
218 (defface change-log-function
219 '((t (:inherit font-lock-variable-name-face
)))
220 "Face for highlighting items of the form `<....>'."
224 (defface change-log-acknowledgment
225 '((t (:inherit font-lock-comment-face
)))
226 "Face for highlighting acknowledgments."
229 (define-obsolete-face-alias 'change-log-acknowledgement
230 'change-log-acknowledgment
"24.3")
232 (defconst change-log-file-names-re
"^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)")
233 (defconst change-log-start-entry-re
"^\\sw.........[0-9:+ ]*")
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 regexp 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-]+ +\\|^ \\{11,\\}\\|^\t \\{3,\\}\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+"
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 (,change-log-file-names-re
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\\)\\| \\)\\(Thanks to\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
279 3 'change-log-acknowledgment
))
280 "Additional expressions to highlight in Change Log mode.")
282 (defun change-log-search-file-name (where)
283 "Return the file-name for the change under point."
286 (beginning-of-line 1)
287 (if (looking-at change-log-start-entry-re
)
288 ;; We are at the start of an entry, search forward for a file
291 (re-search-forward change-log-file-names-re nil t
)
292 (match-string-no-properties 2))
293 (if (looking-at change-log-file-names-re
)
294 ;; We found a file name.
295 (match-string-no-properties 2)
296 ;; Look backwards for either a file name or the log entry start.
297 (if (re-search-backward
298 (concat "\\(" change-log-start-entry-re
300 change-log-file-names-re
"\\)") nil t
)
301 (if (match-beginning 1)
302 ;; We got the start of the entry, look forward for a
305 (re-search-forward change-log-file-names-re nil t
)
306 (match-string-no-properties 2))
307 (match-string-no-properties 4))
308 ;; We must be before any file name, look forward.
309 (re-search-forward change-log-file-names-re nil t
)
310 (match-string-no-properties 2))))))
312 (defun change-log-find-file ()
313 "Visit the file for the change under point."
315 (let ((file (change-log-search-file-name (point))))
316 (if (and file
(file-exists-p file
))
318 (message "No such file or directory: %s" file
))))
320 (defun change-log-search-tag-name-1 (&optional from
)
321 "Search for a tag name within subexpression 1 of last match.
322 Optional argument FROM specifies a buffer position where the tag
323 name should be located. Return value is a cons whose car is the
324 string representing the tag and whose cdr is the position where
327 (narrow-to-region (match-beginning 1) (match-end 1))
328 (when from
(goto-char from
))
329 ;; The regexp below skips any symbol near `point' (FROM) followed by
330 ;; whitespace and another symbol. This should skip, for example,
331 ;; "struct" in a specification like "(struct buffer)" and move to
332 ;; "buffer". A leading paren is ignored.
334 "[(]?\\(?:\\(?:\\sw\\|\\s_\\)+\\(?:[ \t]+\\(\\sw\\|\\s_\\)+\\)\\)")
335 (goto-char (match-beginning 1)))
336 (cons (find-tag-default) (point))))
338 (defconst change-log-tag-re
339 "(\\(\\(?:\\sw\\|\\s_\\)+\\(?:[, \t]+\\(?:\\sw\\|\\s_\\)+\\)*\\))"
340 "Regexp matching a tag name in change log entries.")
342 (defun change-log-search-tag-name (&optional at
)
343 "Search for a tag name near `point'.
344 Optional argument AT non-nil means search near buffer position AT.
345 Return value is a cons whose car is the string representing
346 the tag and whose cdr is the position where the tag was found."
348 (goto-char (setq at
(or at
(point))))
351 (or (condition-case nil
352 ;; Within parenthesized list?
355 (when (looking-at change-log-tag-re
)
356 (change-log-search-tag-name-1 at
)))
359 ;; Before parenthesized list on same line?
361 (when (and (skip-chars-forward " \t")
362 (looking-at change-log-tag-re
))
363 (change-log-search-tag-name-1)))
370 (looking-at change-log-file-names-re
))
371 (goto-char (match-end 0))
372 (skip-syntax-forward " ")
373 (looking-at change-log-tag-re
))
374 (change-log-search-tag-name-1)))
377 ;; Anywhere else within current entry?
381 (if (re-search-backward change-log-start-entry-re nil t
)
387 (if (re-search-forward change-log-start-entry-re nil t
)
390 (when (and (< from to
) (<= from at
) (<= at to
))
392 ;; Narrow to current change log entry.
393 (narrow-to-region from to
)
395 ((re-search-backward change-log-tag-re nil t
)
396 (narrow-to-region (match-beginning 1) (match-end 1))
397 (goto-char (point-max))
398 (cons (find-tag-default) (point-max)))
399 ((re-search-forward change-log-tag-re nil t
)
400 (narrow-to-region (match-beginning 1) (match-end 1))
401 (goto-char (point-min))
402 (cons (find-tag-default) (point-min)))))))
405 (defvar change-log-find-head nil
)
406 (defvar change-log-find-tail nil
)
407 (defvar change-log-find-window nil
)
409 (defun change-log-goto-source-1 (tag regexp file buffer
410 &optional window first last
)
411 "Search for tag TAG in buffer BUFFER visiting file FILE.
412 REGEXP is a regular expression for TAG. The remaining arguments
413 are optional: WINDOW denotes the window to display the results of
414 the search. FIRST is a position in BUFFER denoting the first
415 match from previous searches for TAG. LAST is the position in
416 BUFFER denoting the last match for TAG in the last search."
417 (with-current-buffer buffer
423 ;; When LAST is set make sure we continue from the next
424 ;; line end to not find the same tag again.
428 ;; Try to go to the end of the current defun to avoid
429 ;; false positives within the current defun's body
430 ;; since these would match `add-log-current-defun'.
432 ;; Don't fall behind when `end-of-defun' fails.
433 (error (progn (goto-char last
) (end-of-line))))
435 ;; When LAST was not set start at beginning of BUFFER.
436 (goto-char (point-min)))
438 (while (and (not last
) (re-search-forward regexp nil t
))
439 ;; Verify that `add-log-current-defun' invoked at the end
440 ;; of the match returns TAG. This heuristic works well
441 ;; whenever the name of the defun occurs within the first
442 ;; line of the defun.
443 (setq current-defun
(add-log-current-defun))
444 (when (and current-defun
(string-equal current-defun
tag))
445 ;; Record this as last match.
446 (setq last
(line-beginning-position))
447 ;; Record this as first match when there's none.
448 (unless first
(setq first last
)))))))
450 (with-selected-window
451 (setq change-log-find-window
(or window
(display-buffer buffer
)))
454 (when (or (< last
(point-min)) (> last
(point-max)))
455 ;; Widen to show TAG.
459 ;; When there are no more matches go (back) to FIRST.
460 (message "No more matches for tag `%s' in file `%s'" tag file
)
463 ;; Return new "tail".
464 (list (selected-window) first last
))
465 (message "Source location of tag `%s' not found in file `%s'" tag file
)
468 (defun change-log-goto-source ()
469 "Go to source location of \"change log tag\" near `point'.
470 A change log tag is a symbol within a parenthesized,
471 comma-separated list. If no suitable tag can be found nearby,
472 try to visit the file for the change under `point' instead."
474 (if (and (eq last-command
'change-log-goto-source
)
475 change-log-find-tail
)
476 (setq change-log-find-tail
478 (apply 'change-log-goto-source-1
479 (append change-log-find-head change-log-find-tail
))
482 "Cannot find more matches for tag `%s' in file `%s'"
483 (car change-log-find-head
)
484 (nth 2 change-log-find-head
)))))
487 (tag-at (change-log-search-tag-name))
489 (file (when tag-at
(change-log-search-file-name (cdr tag-at
))))
490 (file-at (when file
(match-beginning 2)))
491 ;; `file-2' is the file `change-log-search-file-name' finds
492 ;; at `point'. We use `file-2' as a fallback when `tag' or
493 ;; `file' are not suitable for some reason.
494 (file-2 (change-log-search-file-name at
))
495 (file-2-at (when file-2
(match-beginning 2))))
497 ((and (or (not tag
) (not file
) (not (file-exists-p file
)))
498 (or (not file-2
) (not (file-exists-p file-2
))))
499 (error "Cannot find tag or file near `point'"))
500 ((and file-2
(file-exists-p file-2
)
501 (or (not tag
) (not file
) (not (file-exists-p file
))
502 (and (or (and (< file-at file-2-at
) (<= file-2-at at
))
503 (and (<= at file-2-at
) (< file-2-at file-at
))))))
504 ;; We either have not found a suitable file name or `file-2'
505 ;; provides a "better" file name wrt `point'. Go to the
506 ;; buffer of `file-2' instead.
507 (setq change-log-find-window
508 (display-buffer (find-file-noselect file-2
))))
510 (setq change-log-find-head
511 (list tag
(concat "\\_<" (regexp-quote tag
) "\\_>")
512 file
(find-file-noselect file
)))
514 (setq change-log-find-tail
515 (apply 'change-log-goto-source-1 change-log-find-head
))
517 (format-message "Cannot find matches for tag `%s' in file `%s'"
520 (defun change-log-next-error (&optional argp reset
)
521 "Move to the Nth (default 1) next match in a ChangeLog buffer.
522 Compatibility function for \\[next-error] invocations."
524 (let* ((argp (or argp
0))
525 (count (abs argp
)) ; how many cycles
526 (down (< argp
0)) ; are we going down? (is argp negative?)
528 (search-function (if up
're-search-forward
're-search-backward
)))
530 ;; set the starting position
531 (goto-char (cond (reset (point-min))
532 (down (line-beginning-position))
533 (up (line-end-position))
536 (funcall search-function change-log-file-names-re nil t count
))
539 ;; if we found a place to visit...
540 (when (looking-at change-log-file-names-re
)
541 (let (change-log-find-window)
542 (change-log-goto-source)
543 (when change-log-find-window
544 ;; Select window displaying source file.
545 (select-window change-log-find-window
)))))
547 (defvar change-log-mode-map
548 (let ((map (make-sparse-keymap))
549 (menu-map (make-sparse-keymap)))
550 (define-key map
[?\C-c ?\C-p
] 'add-log-edit-prev-comment
)
551 (define-key map
[?\C-c ?\C-n
] 'add-log-edit-next-comment
)
552 (define-key map
[?\C-c ?\C-f
] 'change-log-find-file
)
553 (define-key map
[?\C-c ?\C-c
] 'change-log-goto-source
)
554 (define-key map
[menu-bar changelog
] (cons "ChangeLog" menu-map
))
555 (define-key menu-map
[gs]
556 '(menu-item "Go To Source" change-log-goto-source
557 :help "Go to source location of ChangeLog tag near point"))
558 (define-key menu-map [ff]
559 '(menu-item "Find File" change-log-find-file
560 :help "Visit the file for the change under point"))
561 (define-key menu-map [sep] '("--"))
562 (define-key menu-map [nx]
563 '(menu-item "Next Log-Edit Comment" add-log-edit-next-comment
564 :help "Cycle forward through Log-Edit mode comment history"))
565 (define-key menu-map [pr]
566 '(menu-item "Previous Log-Edit Comment" add-log-edit-prev-comment
567 :help "Cycle backward through Log-Edit mode comment history"))
569 "Keymap for Change Log major mode.")
571 ;; It used to be called change-log-time-zone-rule but really should be
572 ;; called add-log-time-zone-rule since it's only used from add-log-* code.
573 (defvaralias 'change-log-time-zone-rule 'add-log-time-zone-rule)
574 (defvar add-log-time-zone-rule nil
575 "Time zone rule used for calculating change log time stamps.
576 If nil, use local time. If t, use Universal Time.
577 If a string, interpret as the ZONE argument of `format-time-string'.")
578 (put 'add-log-time-zone-rule 'safe-local-variable
579 (lambda (x) (or (booleanp x) (stringp x))))
581 (defun add-log-iso8601-time-zone (&optional time zone)
582 (declare (obsolete nil "26.1"))
583 (format-time-string "%:::z" time zone))
585 (defvar add-log-iso8601-with-time-zone nil)
587 (defun add-log-iso8601-time-string (&optional time zone)
589 (if add-log-iso8601-with-time-zone "%Y-%m-%d %:::z" "%Y-%m-%d") time zone))
591 (defun change-log-name ()
592 "Return (system-dependent) default name for a change log file."
593 (or change-log-default-name
596 (defun add-log-edit-prev-comment (arg)
597 "Cycle backward through Log-Edit mode comment history.
598 With a numeric prefix ARG, go back ARG comments."
601 (narrow-to-region (point)
602 (if (memq last-command '(add-log-edit-prev-comment
603 add-log-edit-next-comment))
605 (when (fboundp 'log-edit-previous-comment)
606 (log-edit-previous-comment arg)
607 (indent-region (point-min) (point-max))
608 (goto-char (point-min))
609 (unless (save-restriction (widen) (bolp))
610 (delete-region (point) (progn (skip-chars-forward " \t\n") (point))))
611 (set-mark (point-min))
612 (goto-char (point-max))
613 (delete-region (point) (progn (skip-chars-backward " \t\n") (point))))))
615 (defun add-log-edit-next-comment (arg)
616 "Cycle forward through Log-Edit mode comment history.
617 With a numeric prefix ARG, go back ARG comments."
619 (add-log-edit-prev-comment (- arg)))
622 (defun prompt-for-change-log-name ()
623 "Prompt for a change log name."
624 (let* ((default (change-log-name))
625 (name (expand-file-name
626 (read-file-name (format "Log file (default %s): " default)
628 ;; Handle something that is syntactically a directory name.
629 ;; Look for ChangeLog or whatever in that directory.
630 (if (string= (file-name-nondirectory name) "")
631 (expand-file-name (file-name-nondirectory default)
633 ;; Handle specifying a file that is a directory.
634 (if (file-directory-p name)
635 (expand-file-name (file-name-nondirectory default)
636 (file-name-as-directory name))
639 (defun change-log-version-number-search ()
640 "Return version number of current buffer's file.
641 This is the value returned by `vc-working-revision' or, if that is
642 nil, by matching `change-log-version-number-regexp-list'."
643 (let* ((size (buffer-size))
645 ;; The version number can be anywhere in the file, but
646 ;; restrict search to the file beginning: 10% should be
647 ;; enough to prevent some mishits.
649 ;; Apply percentage only if buffer size is bigger than
651 (if (> size (* 100 80)) (+ (point) (/ size 10)))))
652 (or (and buffer-file-name (vc-working-revision buffer-file-name))
655 (let ((regexps change-log-version-number-regexp-list)
659 (goto-char (point-min))
660 (when (re-search-forward (pop regexps) limit t)
661 (setq version (match-string 1)
665 (declare-function diff-find-source-location "diff-mode"
666 (&optional other-file reverse noprompt))
669 (defun find-change-log (&optional file-name buffer-file)
670 "Find a change log file for \\[add-change-log-entry] and return the name.
672 Optional arg FILE-NAME specifies the file to use.
673 If FILE-NAME is nil, use the value of `change-log-default-name'.
674 If `change-log-default-name' is nil, behave as though it were \"ChangeLog\"
675 \(or whatever we use on this operating system).
677 If `change-log-default-name' contains a leading directory component, then
678 simply find it in the current directory. Otherwise, search in the current
679 directory and its successive parents for a file so named. Stop at the first
680 such file that exists (or has a buffer visiting it), or the first directory
681 that contains any of `change-log-directory-files'. If no match is found,
682 use the current directory. To override the choice of this function,
683 simply create an empty ChangeLog file first by hand in the desired place.
685 Once a file is found, `change-log-default-name' is set locally in the
686 current buffer to the complete file name.
687 Optional arg BUFFER-FILE overrides `buffer-file-name'."
688 ;; If we are called from a diff, first switch to the source buffer;
689 ;; in order to respect buffer-local settings of change-log-default-name, etc.
690 (with-current-buffer (let ((buff (if (derived-mode-p 'diff-mode)
692 (diff-find-source-location))))))
693 (if (buffer-live-p buff) buff
695 ;; If user specified a file name or if this buffer knows which one to use,
698 (setq file-name (and change-log-default-name
699 (file-name-directory change-log-default-name)
700 change-log-default-name))
702 ;; Chase links in the source file
703 ;; and use the change log in the dir where it points.
704 (setq file-name (or (and (or buffer-file buffer-file-name)
707 (or buffer-file buffer-file-name))))
709 (if (file-directory-p file-name)
710 (setq file-name (expand-file-name (change-log-name) file-name)))
711 ;; Chase links before visiting the file.
712 ;; This makes it easier to use a single change log file
713 ;; for several related directories.
714 (setq file-name (file-chase-links file-name))
715 (setq file-name (expand-file-name file-name))
716 (let* ((cbase (file-name-nondirectory (change-log-name)))
718 (locate-dominating-file
722 (let ((clog (expand-file-name cbase dir)))
723 (or (get-file-buffer clog) (file-exists-p clog)))
725 (and change-log-directory-files
726 (let ((files change-log-directory-files)
733 (expand-file-name (car files) dir))))
734 (setq files (cdr files))))
736 (if root (setq file-name (expand-file-name cbase root))))))
737 ;; Make a local variable in this buffer so we needn't search again.
738 (set (make-local-variable 'change-log-default-name) file-name))
741 (defun add-log-file-name (buffer-file log-file)
742 ;; Never want to add a change log entry for the ChangeLog file itself.
743 (unless (or (null buffer-file) (string= buffer-file log-file))
744 (if add-log-file-name-function
745 (funcall add-log-file-name-function buffer-file)
747 (let* ((dir (file-name-directory log-file))
748 (rel (file-relative-name buffer-file dir)))
749 ;; Sometimes with symlinks, the two buffers may have names that
750 ;; appear to belong to different directory trees. So check the
751 ;; file-truenames, to see if we get a better result.
752 (if (not (string-match "\\`\\.\\./" rel))
754 (let ((new (file-relative-name (file-truename buffer-file)
755 (file-truename dir))))
756 (if (< (length new) (length rel))
758 ;; If we have a backup file, it's presumably because we're
759 ;; comparing old and new versions (e.g. for deleted
760 ;; functions) and we'll want to use the original name.
761 (if (backup-file-name-p buffer-file)
762 (file-name-sans-versions buffer-file)
766 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
767 put-new-entry-on-new-line)
768 "Find change log file, and add an entry for today and an item for this file.
769 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
770 name and email (stored in `add-log-full-name' and `add-log-mailing-address').
772 Second arg FILE-NAME is file name of the change log.
773 If nil, use the value of `change-log-default-name'.
775 Third arg OTHER-WINDOW non-nil means visit in other window.
777 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
778 never append to an existing entry. Option `add-log-keep-changes-together'
779 otherwise affects whether a new entry is created.
781 Fifth arg PUT-NEW-ENTRY-ON-NEW-LINE non-nil means that if a new
782 entry is created, put it on a new line by itself, do not put it
783 after a comma on an existing line.
785 Option `add-log-always-start-new-record' non-nil means always create a
786 new record, even when the last record was made on the same date and by
789 The change log file can start with a copyright notice and a copying
790 permission notice. The first blank line indicates the end of these
793 Today's date is calculated according to `add-log-time-zone-rule' if
794 non-nil, otherwise in local time."
795 (interactive (list current-prefix-arg
796 (prompt-for-change-log-name)))
797 (let* ((defun (add-log-current-defun))
798 (version (and change-log-version-info-enabled
799 (change-log-version-number-search)))
800 (buf-file-name (funcall add-log-buffer-file-name-function))
801 (buffer-file (if buf-file-name (expand-file-name buf-file-name)))
802 (file-name (expand-file-name (find-change-log file-name buffer-file)))
803 ;; Set ITEM to the file name to use in the new item.
804 (item (add-log-file-name buffer-file file-name)))
806 (unless (equal file-name buffer-file-name)
808 ((equal file-name (buffer-file-name (window-buffer)))
809 ;; If the selected window already shows the desired buffer don't show
810 ;; it again (particularly important if other-window is true).
811 ;; This is important for diff-add-change-log-entries-other-window.
812 (set-buffer (window-buffer)))
813 ((or other-window (window-dedicated-p))
814 (find-file-other-window file-name))
815 (t (find-file file-name))))
816 (or (derived-mode-p 'change-log-mode)
819 (goto-char (point-min))
821 (let ((full-name (or add-log-full-name (user-full-name)))
822 (mailing-address (or add-log-mailing-address user-mail-address)))
825 (setq full-name (read-string "Full name: " full-name))
826 ;; Note that some sites have room and phone number fields in
827 ;; full name which look silly when inserted. Rather than do
828 ;; anything about that here, let user give prefix argument so that
829 ;; s/he can edit the full name field in prompter if s/he wants.
830 (setq mailing-address
831 (read-string "Mailing address: " mailing-address)))
833 ;; If file starts with a copyright and permission notice, skip them.
834 ;; Assume they end at first blank line.
835 (when (looking-at "Copyright")
836 (search-forward "\n\n")
837 (skip-chars-forward "\n"))
839 ;; Advance into first entry if it is usable; else make new one.
841 (mapcar (lambda (addr)
843 (funcall add-log-time-format
844 nil add-log-time-zone-rule)
847 (if (consp mailing-address)
849 (list mailing-address)))))
850 (if (and (not add-log-always-start-new-record)
852 (dolist (entry new-entries hit)
853 (and (looking-at (regexp-quote entry))
854 ;; Reject multiple author entries. (Bug#8645)
857 (not (looking-at "[ \t]+.*<.*>$")))
860 (insert (nth (random (length new-entries))
862 (if use-hard-newlines hard-newline "\n")
863 (if use-hard-newlines hard-newline "\n"))
866 ;; Determine where we should stop searching for a usable
867 ;; item to add to, within this entry.
870 (if (looking-at "\n*[^\n* \t]")
871 (skip-chars-forward "\n")
872 (if add-log-keep-changes-together
873 (forward-page) ; page delimits entries for date
874 (forward-paragraph))) ; paragraph delimits entries for file
877 ;; Now insert the new line for this item.
878 (cond ((re-search-forward "^\\s *\\* *$" bound t)
879 ;; Put this file name into the existing empty item.
882 ((and (not new-entry)
883 (let (case-fold-search)
885 (concat (regexp-quote (concat "* " item))
886 ;; Don't accept `foo.bar' when
887 ;; looking for `foo':
888 "\\(\\s \\|[(),:]\\)")
890 ;; Add to the existing item for the same file.
891 (if (re-search-forward "^\\s *$\\|^\\s \\*" nil t)
892 (goto-char (match-beginning 0))
893 (goto-char (point-max))
895 ;; Delete excess empty lines; make just 2.
896 (while (and (not (eobp)) (looking-at "^\\s *$"))
897 (delete-region (point) (line-beginning-position 2)))
898 (insert (if use-hard-newlines hard-newline "\n")
899 (if use-hard-newlines hard-newline "\n"))
901 (indent-relative-maybe))
904 (while (looking-at "\\sW")
906 (while (and (not (eobp)) (looking-at "^\\s *$"))
907 (delete-region (point) (line-beginning-position 2)))
908 (insert (if use-hard-newlines hard-newline "\n")
909 (if use-hard-newlines hard-newline "\n")
910 (if use-hard-newlines hard-newline "\n"))
912 (indent-to left-margin)
914 (if item (insert item)))))
915 ;; Now insert the function name, if we have one.
916 ;; Point is at the item for this file,
917 ;; either at the end of the line or at the first blank line.
919 ;; No function name, so put in a colon unless we have just a star.
920 (unless (save-excursion
921 (beginning-of-line 1)
922 (looking-at "\\s *\\(\\* *\\)?$"))
924 (if version (insert version ?\s)))
925 ;; Make it easy to get rid of the function name.
927 (unless (save-excursion
928 (beginning-of-line 1)
929 (looking-at "\\s *$"))
931 ;; See if the prev function name has a message yet or not.
932 ;; If not, merge the two items.
933 (let ((pos (point-marker)))
934 (skip-syntax-backward " ")
935 (skip-chars-backward "):")
936 (if (and (not put-new-entry-on-new-line)
938 (let ((pos (save-excursion (backward-sexp 1) (point))))
939 (when (equal (buffer-substring pos (point)) defun)
940 (delete-region pos (point)))
941 (> fill-column (+ (current-column) (length defun) 4))))
942 (progn (skip-chars-backward ", ")
943 (delete-region (point) pos)
944 (unless (memq (char-before) '(?\()) (insert ", ")))
945 (when (and (not put-new-entry-on-new-line) (looking-at "):"))
946 (delete-region (+ 1 (point)) (line-end-position)))
949 (set-marker pos nil))
951 (if version (insert version ?\s)))))
954 (defun add-change-log-entry-other-window (&optional whoami file-name)
955 "Find change log file in other window and add entry and item.
956 This is just like `add-change-log-entry' except that it displays
957 the change log file in another window."
958 (interactive (if current-prefix-arg
959 (list current-prefix-arg
960 (prompt-for-change-log-name))))
961 (add-change-log-entry whoami file-name t))
964 (defvar change-log-indent-text 0)
966 (defun change-log-fill-parenthesized-list ()
967 ;; Fill parenthesized lists of names according to GNU standards.
968 ;; * file-name.ext (very-long-foo, very-long-bar, very-long-foobar):
969 ;; should be filled as
970 ;; * file-name.ext (very-long-foo, very-long-bar)
971 ;; (very-long-foobar):
974 (skip-chars-backward " \t")
975 (when (and (equal (char-before) ?\,)
976 (> (point) (1+ (point-min))))
978 (when (save-excursion
981 (equal (char-after) ?\()
982 (skip-chars-backward " \t"))
984 ;; Skip everything but a whitespace or asterisk.
985 (and (not (zerop (skip-chars-backward "^ \t\n*")))
986 (skip-chars-backward " \t")
987 ;; We want one asterisk here.
988 (= (skip-chars-backward "*") -1)
989 (skip-chars-backward " \t")
993 ;; Close list on previous line.
995 (skip-chars-forward " \t\n")
996 ;; Start list on new line.
997 (insert-before-markers "("))
1000 (defun change-log-indent ()
1001 (change-log-fill-parenthesized-list)
1005 (skip-chars-forward " \t")
1007 ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
1008 ;; Matching the output of add-log-time-format is difficult,
1009 ;; but I'll get it has at least two adjacent digits.
1010 (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
1012 ((looking-at "[^*(]")
1013 (+ (current-left-margin) change-log-indent-text))
1014 (t (current-left-margin)))))
1015 (pos (save-excursion (indent-line-to indent) (point))))
1016 (if (> pos (point)) (goto-char pos))))
1019 (defvar smerge-resolve-function)
1020 (defvar copyright-at-end-flag)
1023 (define-derived-mode change-log-mode text-mode "Change Log"
1024 "Major mode for editing change logs; like Indented Text mode.
1025 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
1026 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
1027 Each entry behaves as a paragraph, and the entries for one day as a page.
1028 Runs `change-log-mode-hook'.
1029 \n\\{change-log-mode-map}"
1034 show-trailing-whitespace t)
1035 (set (make-local-variable 'fill-forward-paragraph-function)
1036 'change-log-fill-forward-paragraph)
1037 (set (make-local-variable 'comment-start) nil)
1038 ;; Make sure we call `change-log-indent' when filling.
1039 (set (make-local-variable 'fill-indent-according-to-mode) t)
1040 ;; Avoid that filling leaves behind a single "*" on a line.
1041 (add-hook 'fill-nobreak-predicate
1043 (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
1045 (set (make-local-variable 'indent-line-function) 'change-log-indent)
1046 (set (make-local-variable 'tab-always-indent) nil)
1047 (set (make-local-variable 'copyright-at-end-flag) t)
1048 ;; We really do want "^" in paragraph-start below: it is only the
1049 ;; lines that begin at column 0 (despite the left-margin of 8) that
1050 ;; we are looking for. Adding `* ' allows eliding the blank line
1051 ;; between entries for different files.
1052 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
1053 (set (make-local-variable 'paragraph-separate) paragraph-start)
1054 ;; Match null string on the date-line so that the date-line
1055 ;; is grouped with what follows.
1056 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
1057 (set (make-local-variable 'version-control) 'never)
1058 (set (make-local-variable 'smerge-resolve-function)
1059 'change-log-resolve-conflict)
1060 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
1061 (set (make-local-variable 'font-lock-defaults)
1062 '(change-log-font-lock-keywords t nil nil backward-paragraph))
1063 (set (make-local-variable 'multi-isearch-next-buffer-function)
1064 'change-log-next-buffer)
1065 (set (make-local-variable 'beginning-of-defun-function)
1066 'change-log-beginning-of-defun)
1067 (set (make-local-variable 'end-of-defun-function)
1068 'change-log-end-of-defun)
1069 ;; next-error function glue
1070 (setq next-error-function 'change-log-next-error)
1071 (setq next-error-last-buffer (current-buffer)))
1073 (defun change-log-next-buffer (&optional buffer wrap)
1074 "Return the next buffer in the series of ChangeLog file buffers.
1075 This function is used for multiple buffers isearch.
1076 A sequence of buffers is formed by ChangeLog files with decreasing
1077 numeric file name suffixes in the directory of the initial ChangeLog
1078 file were isearch was started."
1079 (let* ((name (change-log-name))
1081 (and (file-exists-p name) (list name))
1082 (sort (file-expand-wildcards
1083 (concat name "[-.][0-9]*"))
1085 ;; The file's extension may not have a valid
1086 ;; version form (e.g. VC backup revisions).
1088 (version< (substring b (length name))
1089 (substring a (length name))))))))
1090 (files (if isearch-forward files (reverse files)))
1093 (cadr (member (file-name-nondirectory (buffer-file-name buffer))
1095 ;; If there are no files that match the default pattern ChangeLog.[0-9],
1096 ;; return the current buffer to force isearch wrapping to its beginning.
1097 ;; If file is nil, multi-isearch-search-fun will signal "end of multi".
1098 (if (file-exists-p file)
1099 (find-file-noselect file)
1102 (defun change-log-fill-forward-paragraph (n)
1103 "Cut paragraphs so filling preserves open parentheses at beginning of lines."
1104 (let (;; Add lines starting with whitespace followed by a left paren or an
1106 (paragraph-start (concat paragraph-start "\\|\\s *\\(?:\\s(\\|\\*\\)")))
1107 (forward-paragraph n)))
1109 (defcustom add-log-current-defun-header-regexp
1110 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
1111 "Heuristic regexp used by `add-log-current-defun' for unknown major modes.
1112 The regexp's first submatch is placed in the ChangeLog entry, in
1117 (declare-function c-cpp-define-name "cc-cmds" ())
1118 (declare-function c-defun-name "cc-cmds" ())
1121 (defun add-log-current-defun ()
1122 "Return name of function definition point is in, or nil.
1124 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
1125 Texinfo (@node titles) and Perl.
1127 Other modes are handled by a heuristic that looks in the 10K before
1128 point for uppercase headings starting in the first column or
1129 identifiers followed by `:' or `='. See variables
1130 `add-log-current-defun-header-regexp' and
1131 `add-log-current-defun-function'.
1133 Has a preference of looking backwards."
1136 (if add-log-current-defun-function
1137 (funcall add-log-current-defun-function)
1138 ;; If all else fails, try heuristics
1139 (let (case-fold-search
1142 (when (re-search-backward add-log-current-defun-header-regexp
1143 (- (point) 10000) t)
1144 (setq result (or (match-string-no-properties 1)
1145 (match-string-no-properties 0)))
1146 ;; Strip whitespace away
1147 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
1149 (setq result (match-string-no-properties 1 result)))
1153 (defvar change-log-get-method-definition-md)
1155 ;; Subroutine used within change-log-get-method-definition.
1156 ;; Add the last match in the buffer to the end of `md',
1157 ;; followed by the string END; move to the end of that match.
1158 (defun change-log-get-method-definition-1 (end)
1159 (setq change-log-get-method-definition-md
1160 (concat change-log-get-method-definition-md
1163 (goto-char (match-end 0)))
1165 (defun change-log-get-method-definition ()
1166 "For Objective C, return the method name if we are in a method."
1167 (let ((change-log-get-method-definition-md "["))
1169 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
1170 (change-log-get-method-definition-1 " ")))
1173 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
1174 (change-log-get-method-definition-1 "")
1175 (while (not (looking-at "[{;]"))
1177 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
1178 (change-log-get-method-definition-1 ""))
1179 (concat change-log-get-method-definition-md "]"))))))
1181 (autoload 'timezone-make-date-sortable "timezone")
1183 (defun change-log-sortable-date-at ()
1184 "Return date of log entry in a consistent form for sorting.
1185 Point is assumed to be at the start of the entry."
1186 (if (looking-at change-log-start-entry-re)
1187 (let ((date (match-string-no-properties 0)))
1189 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
1190 (concat (match-string 1 date) (match-string 2 date)
1191 (match-string 3 date))
1192 (ignore-errors (timezone-make-date-sortable date)))))
1193 (error "Bad date")))
1195 (defun change-log-resolve-conflict ()
1196 "Function to be used in `smerge-resolve-function'."
1199 (narrow-to-region (match-beginning 0) (match-end 0))
1200 (let ((mb1 (match-beginning 1))
1202 (mb3 (match-beginning 3))
1204 (tmp1 (generate-new-buffer " *changelog-resolve-1*"))
1205 (tmp2 (generate-new-buffer " *changelog-resolve-2*")))
1207 (let ((buf (current-buffer)))
1208 (with-current-buffer tmp1
1210 (insert-buffer-substring buf mb1 me1))
1211 (with-current-buffer tmp2
1213 (insert-buffer-substring buf mb3 me3)
1214 ;; Do the merge here instead of inside `buf' so as to be
1215 ;; more robust in case change-log-merge fails.
1216 (change-log-merge tmp1))
1217 (goto-char (point-max))
1218 (delete-region (point-min)
1220 (insert-buffer-substring tmp2))))
1222 (kill-buffer tmp2))))))
1225 (defun change-log-merge (other-log)
1226 "Merge the contents of change log file OTHER-LOG with this buffer.
1227 Both must be found in Change Log mode (since the merging depends on
1228 the appropriate motion commands). OTHER-LOG can be either a file name
1231 Entries are inserted in chronological order. Both the current and
1232 old-style time formats for entries are supported."
1233 (interactive "*fLog file name to merge: ")
1234 (if (not (derived-mode-p 'change-log-mode))
1235 (error "Not in Change Log mode"))
1236 (let ((other-buf (if (bufferp other-log) other-log
1237 (find-file-noselect other-log)))
1238 (buf (current-buffer))
1241 (goto-char (point-min))
1242 (set-buffer other-buf)
1243 (goto-char (point-min))
1244 (if (not (derived-mode-p 'change-log-mode))
1245 (error "%s not found in Change Log mode" other-log))
1246 ;; Loop through all the entries in OTHER-LOG.
1248 (setq date1 (change-log-sortable-date-at))
1250 end (progn (forward-page) (point)))
1251 ;; Look for an entry in original buffer that isn't later.
1252 (with-current-buffer buf
1253 (while (and (not (eobp))
1254 (string< date1 (change-log-sortable-date-at)))
1257 (insert-buffer-substring other-buf start end)
1258 ;; At the end of the original buffer, insert a newline to
1259 ;; separate entries and then the rest of the file being
1262 (and (= ?\n (char-before))
1263 (or (<= (1- (point)) (point-min))
1264 (= ?\n (char-before (1- (point)))))))
1265 (insert (if use-hard-newlines hard-newline "\n")))
1266 ;; Move to the end of it to terminate outer loop.
1267 (with-current-buffer other-buf
1268 (goto-char (point-max)))
1269 (insert-buffer-substring other-buf start)))))))
1271 (defun change-log-beginning-of-defun ()
1272 (re-search-backward change-log-start-entry-re nil 'move))
1274 (defun change-log-end-of-defun ()
1275 ;; Look back and if there is no entry there it means we are before
1276 ;; the first ChangeLog entry, so go forward until finding one.
1277 (unless (save-excursion (re-search-backward change-log-start-entry-re nil t))
1278 (re-search-forward change-log-start-entry-re nil t))
1280 ;; In case we are at the end of log entry going forward a line will
1281 ;; make us find the next entry when searching. If we are inside of
1282 ;; an entry going forward a line will still keep the point inside
1286 ;; In case we are at the beginning of an entry, move past it.
1287 (when (looking-at change-log-start-entry-re)
1288 (goto-char (match-end 0))
1291 ;; Search for the start of the next log entry. Go to the end of the
1292 ;; buffer if we could not find a next entry.
1293 (when (re-search-forward change-log-start-entry-re nil 'move)
1294 (goto-char (match-beginning 0))
1299 ;;; add-log.el ends here