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 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
42 (defgroup change-log nil
43 "Change log maintenance."
45 :link
'(custom-manual "(emacs)Change Log")
50 (defcustom change-log-default-name nil
51 "Name of a change log file for \\[add-change-log-entry]."
52 :type
'(choice (const :tag
"default" nil
)
55 (put 'change-log-default-name
'safe-local-variable
'string-or-null-p
)
57 (defcustom change-log-mode-hook nil
58 "Normal hook run by `change-log-mode'."
62 ;; Many modes set this variable, so avoid warnings.
64 (defcustom add-log-current-defun-function nil
65 "If non-nil, function to guess name of surrounding function.
66 It is used by `add-log-current-defun' in preference to built-in rules.
67 Returns function's name as a string, or nil if outside a function."
68 :type
'(choice (const nil
) function
)
72 (defcustom add-log-full-name nil
73 "Full name of user, for inclusion in ChangeLog daily headers.
74 This defaults to the value returned by the function `user-full-name'."
75 :type
'(choice (const :tag
"Default" nil
)
80 (defcustom add-log-mailing-address nil
81 "Email addresses of user, for inclusion in ChangeLog headers.
82 This defaults to the value of `user-mail-address'. In addition to
83 being a simple string, this value can also be a list. All elements
84 will be recognized as referring to the same user; when creating a new
85 ChangeLog entry, one element will be chosen at random."
86 :type
'(choice (const :tag
"Default" nil
)
87 (string :tag
"String")
88 (repeat :tag
"List of Strings" string
))
91 (defcustom add-log-time-format
'add-log-iso8601-time-string
92 "Function that defines the time format.
93 For example, `add-log-iso8601-time-string', which gives the
94 date in international ISO 8601 format,
95 and `current-time-string' are two valid values."
96 :type
'(radio (const :tag
"International ISO 8601 format"
97 add-log-iso8601-time-string
)
98 (const :tag
"Old format, as returned by `current-time-string'"
100 (function :tag
"Other"))
103 (defcustom add-log-keep-changes-together nil
104 "If non-nil, normally keep day's log entries for one file together.
106 Log entries for a given file made with \\[add-change-log-entry] or
107 \\[add-change-log-entry-other-window] will only be added to others \
109 today if this variable is non-nil or that file comes first in today's
110 entries. Otherwise another entry for that file will be started. An
114 * bar (...): change 1
116 in the latter case, \\[add-change-log-entry-other-window] in a \
117 buffer visiting `bar', yields:
121 * bar (...): change 1
126 * bar (...): change 1
129 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
135 (defcustom add-log-always-start-new-record nil
136 "If non-nil, `add-change-log-entry' will always start a new record."
141 (defcustom add-log-buffer-file-name-function nil
142 "If non-nil, function to call to identify the full filename of a buffer.
143 This function is called with no argument. If this is nil, the default is to
144 use `buffer-file-name'."
145 :type
'(choice (const nil
) function
)
148 (defcustom add-log-file-name-function nil
149 "If non-nil, function to call to identify the filename for a ChangeLog entry.
150 This function is called with one argument, the value of variable
151 `buffer-file-name' in that buffer. If this is nil, the default is to
152 use the file's name relative to the directory of the change log file."
153 :type
'(choice (const nil
) function
)
157 (defcustom change-log-version-info-enabled nil
158 "If non-nil, enable recording version numbers with the changes."
163 (defcustom change-log-version-number-regexp-list
164 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
166 ;; (defconst ad-version "2.15"
167 (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re
)
168 ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
169 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re
)))
170 "List of regexps to search for version number.
171 The version number must be in group 1.
172 Note: The search is conducted only within 10%, at the beginning of the file."
174 :type
'(repeat regexp
)
177 (defface change-log-date
178 '((t (:inherit font-lock-string-face
)))
179 "Face used to highlight dates in date lines."
182 ;; backward-compatibility alias
183 (put 'change-log-date-face
'face-alias
'change-log-date
)
185 (defface change-log-name
186 '((t (:inherit font-lock-constant-face
)))
187 "Face for highlighting author names."
190 ;; backward-compatibility alias
191 (put 'change-log-name-face
'face-alias
'change-log-name
)
193 (defface change-log-email
194 '((t (:inherit font-lock-variable-name-face
)))
195 "Face for highlighting author email addresses."
198 ;; backward-compatibility alias
199 (put 'change-log-email-face
'face-alias
'change-log-email
)
201 (defface change-log-file
202 '((t (:inherit font-lock-function-name-face
)))
203 "Face for highlighting file names."
206 ;; backward-compatibility alias
207 (put 'change-log-file-face
'face-alias
'change-log-file
)
209 (defface change-log-list
210 '((t (:inherit font-lock-keyword-face
)))
211 "Face for highlighting parenthesized lists of functions or variables."
214 ;; backward-compatibility alias
215 (put 'change-log-list-face
'face-alias
'change-log-list
)
217 (defface change-log-conditionals
218 '((t (:inherit font-lock-variable-name-face
)))
219 "Face for highlighting conditionals of the form `[...]'."
222 ;; backward-compatibility alias
223 (put 'change-log-conditionals-face
'face-alias
'change-log-conditionals
)
225 (defface change-log-function
226 '((t (:inherit font-lock-variable-name-face
)))
227 "Face for highlighting items of the form `<....>'."
230 ;; backward-compatibility alias
231 (put 'change-log-function-face
'face-alias
'change-log-function
)
233 (defface change-log-acknowledgement
234 '((t (:inherit font-lock-comment-face
)))
235 "Face for highlighting acknowledgments."
238 ;; backward-compatibility alias
239 (put 'change-log-acknowledgement-face
'face-alias
'change-log-acknowledgement
)
241 (defconst change-log-file-names-re
"^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)")
242 (defconst change-log-start-entry-re
"^\\sw.........[0-9:+ ]*")
244 (defvar change-log-font-lock-keywords
246 ;; Date lines, new (2000-01-01) and old (Sat Jan 1 00:00:00 2000) styles.
247 ;; Fixme: this regepx is just an approximate one and may match
248 ;; wrongly with a non-date line existing as a random note. In
249 ;; addition, using any kind of fixed setting like this doesn't
250 ;; work if a user customizes add-log-time-format.
251 ("^[0-9-]+ +\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+"
252 (0 'change-log-date-face
)
253 ;; Name and e-mail; some people put e-mail in parens, not angles.
254 ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
256 (2 'change-log-email
)))
259 (,change-log-file-names-re
261 ;; Possibly further names in a list:
262 ("\\=, \\([^ ,:([\n]+\\)" nil nil
(1 'change-log-file
))
263 ;; Possibly a parenthesized list of names:
264 ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
265 nil nil
(1 'change-log-list
))
266 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
267 nil nil
(1 'change-log-list
)))
269 ;; Function or variable names.
270 ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
272 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
273 (1 'change-log-list
)))
276 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals
))
278 ;; Function of change.
279 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function
))
282 ;; Don't include plain "From" because that is vague;
283 ;; we want to encourage people to say something more specific.
284 ;; Note that the FSF does not use "Patches by"; our convention
285 ;; is to put the name of the author of the changes at the top
286 ;; of the change log entry.
287 ("\\(^\\( +\\|\t\\)\\| \\)\\(Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
288 3 'change-log-acknowledgement
))
289 "Additional expressions to highlight in Change Log mode.")
291 (defun change-log-search-file-name (where)
292 "Return the file-name for the change under point."
295 (beginning-of-line 1)
296 (if (looking-at change-log-start-entry-re
)
297 ;; We are at the start of an entry, search forward for a file
300 (re-search-forward change-log-file-names-re nil t
)
301 (match-string-no-properties 2))
302 (if (looking-at change-log-file-names-re
)
303 ;; We found a file name.
304 (match-string-no-properties 2)
305 ;; Look backwards for either a file name or the log entry start.
306 (if (re-search-backward
307 (concat "\\(" change-log-start-entry-re
309 change-log-file-names-re
"\\)") nil t
)
310 (if (match-beginning 1)
311 ;; We got the start of the entry, look forward for a
314 (re-search-forward change-log-file-names-re nil t
)
315 (match-string-no-properties 2))
316 (match-string-no-properties 4))
317 ;; We must be before any file name, look forward.
318 (re-search-forward change-log-file-names-re nil t
)
319 (match-string-no-properties 2))))))
321 (defun change-log-find-file ()
322 "Visit the file for the change under point."
324 (let ((file (change-log-search-file-name (point))))
325 (if (and file
(file-exists-p file
))
327 (message "No such file or directory: %s" file
))))
329 (defun change-log-search-tag-name-1 (&optional from
)
330 "Search for a tag name within subexpression 1 of last match.
331 Optional argument FROM specifies a buffer position where the tag
332 name should be located. Return value is a cons whose car is the
333 string representing the tag and whose cdr is the position where
336 (narrow-to-region (match-beginning 1) (match-end 1))
337 (when from
(goto-char from
))
338 ;; The regexp below skips any symbol near `point' (FROM) followed by
339 ;; whitespace and another symbol. This should skip, for example,
340 ;; "struct" in a specification like "(struct buffer)" and move to
341 ;; "buffer". A leading paren is ignored.
343 "[(]?\\(?:\\(?:\\sw\\|\\s_\\)+\\(?:[ \t]+\\(\\sw\\|\\s_\\)+\\)\\)")
344 (goto-char (match-beginning 1)))
345 (cons (find-tag-default) (point))))
347 (defconst change-log-tag-re
348 "(\\(\\(?:\\sw\\|\\s_\\)+\\(?:[, \t]+\\(?:\\sw\\|\\s_\\)+\\)*\\))"
349 "Regexp matching a tag name in change log entries.")
351 (defun change-log-search-tag-name (&optional at
)
352 "Search for a tag name near `point'.
353 Optional argument AT non-nil means search near buffer position
354 AT. Return value is a cons whose car is the string representing
355 the tag and whose cdr is the position where the tag was found."
357 (goto-char (setq at
(or at
(point))))
360 (or (condition-case nil
361 ;; Within parenthesized list?
364 (when (looking-at change-log-tag-re
)
365 (change-log-search-tag-name-1 at
)))
368 ;; Before parenthesized list on same line?
370 (when (and (skip-chars-forward " \t")
371 (looking-at change-log-tag-re
))
372 (change-log-search-tag-name-1)))
379 (looking-at change-log-file-names-re
))
380 (goto-char (match-end 0))
381 (skip-syntax-forward " ")
382 (looking-at change-log-tag-re
))
383 (change-log-search-tag-name-1)))
386 ;; Anywhere else within current entry?
390 (if (re-search-backward change-log-start-entry-re nil t
)
396 (if (re-search-forward change-log-start-entry-re nil t
)
399 (when (and (< from to
) (<= from at
) (<= at to
))
401 ;; Narrow to current change log entry.
402 (narrow-to-region from to
)
404 ((re-search-backward change-log-tag-re nil t
)
405 (narrow-to-region (match-beginning 1) (match-end 1))
406 (goto-char (point-max))
407 (cons (find-tag-default) (point-max)))
408 ((re-search-forward change-log-tag-re nil t
)
409 (narrow-to-region (match-beginning 1) (match-end 1))
410 (goto-char (point-min))
411 (cons (find-tag-default) (point-min)))))))
414 (defvar change-log-find-head nil
)
415 (defvar change-log-find-tail nil
)
417 (defun change-log-goto-source-1 (tag regexp file buffer
418 &optional window first last
)
419 "Search for tag TAG in buffer BUFFER visiting file FILE.
420 REGEXP is a regular expression for TAG. The remaining arguments
421 are optional: WINDOW denotes the window to display the results of
422 the search. FIRST is a position in BUFFER denoting the first
423 match from previous searches for TAG. LAST is the position in
424 BUFFER denoting the last match for TAG in the last search."
425 (with-current-buffer buffer
431 ;; When LAST is set make sure we continue from the next
432 ;; line end to not find the same tag again.
436 ;; Try to go to the end of the current defun to avoid
437 ;; false positives within the current defun's body
438 ;; since these would match `add-log-current-defun'.
440 ;; Don't fall behind when `end-of-defun' fails.
441 (error (progn (goto-char last
) (end-of-line))))
443 ;; When LAST was not set start at beginning of BUFFER.
444 (goto-char (point-min)))
446 (while (and (not last
) (re-search-forward regexp nil t
))
447 ;; Verify that `add-log-current-defun' invoked at the end
448 ;; of the match returns TAG. This heuristic works well
449 ;; whenever the name of the defun occurs within the first
450 ;; line of the defun.
451 (setq current-defun
(add-log-current-defun))
452 (when (and current-defun
(string-equal current-defun
tag))
453 ;; Record this as last match.
454 (setq last
(line-beginning-position))
455 ;; Record this as first match when there's none.
456 (unless first
(setq first last
)))))))
458 (with-selected-window (or window
(display-buffer buffer
))
461 (when (or (< last
(point-min)) (> last
(point-max)))
462 ;; Widen to show TAG.
466 ;; When there are no more matches go (back) to FIRST.
467 (message "No more matches for tag `%s' in file `%s'" tag file
)
470 ;; Return new "tail".
471 (list (selected-window) first last
))
472 (message "Source location of tag `%s' not found in file `%s'" tag file
)
475 (defun change-log-goto-source ()
476 "Go to source location of \"change log tag\" near `point'.
477 A change log tag is a symbol within a parenthesized,
478 comma-separated list. If no suitable tag can be found nearby,
479 try to visit the file for the change under `point' instead."
481 (if (and (eq last-command
'change-log-goto-source
)
482 change-log-find-tail
)
483 (setq change-log-find-tail
485 (apply 'change-log-goto-source-1
486 (append change-log-find-head change-log-find-tail
))
488 (format "Cannot find more matches for tag `%s' in file `%s'"
489 (car change-log-find-head
)
490 (nth 2 change-log-find-head
)))))
493 (tag-at (change-log-search-tag-name))
495 (file (when tag-at
(change-log-search-file-name (cdr tag-at
))))
496 (file-at (when file
(match-beginning 2)))
497 ;; `file-2' is the file `change-log-search-file-name' finds
498 ;; at `point'. We use `file-2' as a fallback when `tag' or
499 ;; `file' are not suitable for some reason.
500 (file-2 (change-log-search-file-name at
))
501 (file-2-at (when file-2
(match-beginning 2))))
503 ((and (or (not tag
) (not file
) (not (file-exists-p file
)))
504 (or (not file-2
) (not (file-exists-p file-2
))))
505 (error "Cannot find tag or file near `point'"))
506 ((and file-2
(file-exists-p file-2
)
507 (or (not tag
) (not file
) (not (file-exists-p file
))
508 (and (or (and (< file-at file-2-at
) (<= file-2-at at
))
509 (and (<= at file-2-at
) (< file-2-at file-at
))))))
510 ;; We either have not found a suitable file name or `file-2'
511 ;; provides a "better" file name wrt `point'. Go to the
512 ;; buffer of `file-2' instead.
513 (display-buffer (find-file-noselect file-2
)))
515 (setq change-log-find-head
516 (list tag
(concat "\\_<" (regexp-quote tag
) "\\_>")
517 file
(find-file-noselect file
)))
519 (setq change-log-find-tail
520 (apply 'change-log-goto-source-1 change-log-find-head
))
522 (format "Cannot find matches for tag `%s' in file `%s'"
525 (defun change-log-next-error (&optional argp reset
)
526 "Move to the Nth (default 1) next match in an Occur mode buffer.
527 Compatibility function for \\[next-error] invocations."
529 (let* ((argp (or argp
0))
530 (count (abs argp
)) ; how many cycles
531 (down (< argp
0)) ; are we going down? (is argp negative?)
533 (search-function (if up
're-search-forward
're-search-backward
)))
535 ;; set the starting position
536 (goto-char (cond (reset (point-min))
537 (down (line-beginning-position))
538 (up (line-end-position))
541 (funcall search-function change-log-file-names-re nil t count
))
544 ;; if we found a place to visit...
545 (when (looking-at change-log-file-names-re
)
546 (change-log-goto-source)
547 ;; go to the file itself
548 (let ((file (nth 2 change-log-find-head
)))
549 (when file
(pop-to-buffer (find-file-noselect file
))))))
551 (defvar change-log-mode-map
552 (let ((map (make-sparse-keymap)))
553 (define-key map
[?\C-c ?\C-p
] 'add-log-edit-prev-comment
)
554 (define-key map
[?\C-c ?\C-n
] 'add-log-edit-next-comment
)
555 (define-key map
[?\C-c ?\C-f
] 'change-log-find-file
)
556 (define-key map
[?\C-c ?\C-c
] 'change-log-goto-source
)
558 "Keymap for Change Log major mode.")
560 ;; It used to be called change-log-time-zone-rule but really should be
561 ;; called add-log-time-zone-rule since it's only used from add-log-* code.
562 (defvaralias 'change-log-time-zone-rule
'add-log-time-zone-rule
)
563 (defvar add-log-time-zone-rule nil
564 "Time zone used for calculating change log time stamps.
565 It takes the same format as the TZ argument of `set-time-zone-rule'.
566 If nil, use local time.
567 If t, use universal time.")
568 (put 'add-log-time-zone-rule
'safe-local-variable
569 '(lambda (x) (or (booleanp x
) (stringp x
))))
571 (defun add-log-iso8601-time-zone (&optional time
)
572 (let* ((utc-offset (or (car (current-time-zone time
)) 0))
573 (sign (if (< utc-offset
0) ?- ?
+))
574 (sec (abs utc-offset
))
579 (format (cond ((not (zerop ss
)) "%c%02d:%02d:%02d")
580 ((not (zerop mm
)) "%c%02d:%02d")
584 (defvar add-log-iso8601-with-time-zone nil
)
586 (defun add-log-iso8601-time-string ()
587 (let ((time (format-time-string "%Y-%m-%d"
588 nil
(eq t add-log-time-zone-rule
))))
589 (if add-log-iso8601-with-time-zone
590 (concat time
" " (add-log-iso8601-time-zone))
593 (defun change-log-name ()
594 "Return (system-dependent) default name for a change log file."
595 (or change-log-default-name
598 (defun add-log-edit-prev-comment (arg)
599 "Cycle backward through Log-Edit mode comment history.
600 With a numeric prefix ARG, go back ARG comments."
603 (narrow-to-region (point)
604 (if (memq last-command
'(add-log-edit-prev-comment
605 add-log-edit-next-comment
))
607 (when (fboundp 'log-edit-previous-comment
)
608 (log-edit-previous-comment arg
)
609 (indent-region (point-min) (point-max))
610 (goto-char (point-min))
611 (unless (save-restriction (widen) (bolp))
612 (delete-region (point) (progn (skip-chars-forward " \t\n") (point))))
613 (set-mark (point-min))
614 (goto-char (point-max))
615 (delete-region (point) (progn (skip-chars-backward " \t\n") (point))))))
617 (defun add-log-edit-next-comment (arg)
618 "Cycle forward through Log-Edit mode comment history.
619 With a numeric prefix ARG, go back ARG comments."
621 (add-log-edit-prev-comment (- arg
)))
624 (defun prompt-for-change-log-name ()
625 "Prompt for a change log name."
626 (let* ((default (change-log-name))
627 (name (expand-file-name
628 (read-file-name (format "Log file (default %s): " default
)
630 ;; Handle something that is syntactically a directory name.
631 ;; Look for ChangeLog or whatever in that directory.
632 (if (string= (file-name-nondirectory name
) "")
633 (expand-file-name (file-name-nondirectory default
)
635 ;; Handle specifying a file that is a directory.
636 (if (file-directory-p name
)
637 (expand-file-name (file-name-nondirectory default
)
638 (file-name-as-directory name
))
641 (defun change-log-version-number-search ()
642 "Return version number of current buffer's file.
643 This is the value returned by `vc-working-revision' or, if that is
644 nil, by matching `change-log-version-number-regexp-list'."
645 (let* ((size (buffer-size))
647 ;; The version number can be anywhere in the file, but
648 ;; restrict search to the file beginning: 10% should be
649 ;; enough to prevent some mishits.
651 ;; Apply percentage only if buffer size is bigger than
653 (if (> size
(* 100 80)) (+ (point) (/ size
10)))))
654 (or (and buffer-file-name
(vc-working-revision buffer-file-name
))
657 (let ((regexps change-log-version-number-regexp-list
)
661 (goto-char (point-min))
662 (when (re-search-forward (pop regexps
) limit t
)
663 (setq version
(match-string 1)
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.
681 Once a file is found, `change-log-default-name' is set locally in the
682 current buffer to the complete file name.
683 Optional arg BUFFER-FILE overrides `buffer-file-name'."
684 ;; If user specified a file name or if this buffer knows which one to use,
687 (setq file-name
(and change-log-default-name
688 (file-name-directory change-log-default-name
)
689 change-log-default-name
))
691 ;; Chase links in the source file
692 ;; and use the change log in the dir where it points.
693 (setq file-name
(or (and (or buffer-file buffer-file-name
)
696 (or buffer-file buffer-file-name
))))
698 (if (file-directory-p file-name
)
699 (setq file-name
(expand-file-name (change-log-name) file-name
)))
700 ;; Chase links before visiting the file.
701 ;; This makes it easier to use a single change log file
702 ;; for several related directories.
703 (setq file-name
(file-chase-links file-name
))
704 (setq file-name
(expand-file-name file-name
))
705 ;; Move up in the dir hierarchy till we find a change log file.
706 (let ((file1 file-name
)
708 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
709 (progn (setq parent-dir
712 (file-name-directory file1
))))
713 ;; Give up if we are already at the root dir.
714 (not (string= (file-name-directory file1
)
716 ;; Move up to the parent dir and try again.
717 (setq file1
(expand-file-name
718 (file-name-nondirectory (change-log-name))
720 ;; If we found a change log in a parent, use that.
721 (if (or (get-file-buffer file1
) (file-exists-p file1
))
722 (setq file-name file1
)))))
723 ;; Make a local variable in this buffer so we needn't search again.
724 (set (make-local-variable 'change-log-default-name
) file-name
)
727 (defun add-log-file-name (buffer-file log-file
)
728 ;; Never want to add a change log entry for the ChangeLog file itself.
729 (unless (or (null buffer-file
) (string= buffer-file log-file
))
730 (if add-log-file-name-function
731 (funcall add-log-file-name-function buffer-file
)
733 (file-relative-name buffer-file
(file-name-directory log-file
)))
734 ;; If we have a backup file, it's presumably because we're
735 ;; comparing old and new versions (e.g. for deleted
736 ;; functions) and we'll want to use the original name.
737 (if (backup-file-name-p buffer-file
)
738 (file-name-sans-versions buffer-file
)
742 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
743 put-new-entry-on-new-line
)
744 "Find change log file, and add an entry for today and an item for this file.
745 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
746 name and email (stored in `add-log-full-name' and `add-log-mailing-address').
748 Second arg FILE-NAME is file name of the change log.
749 If nil, use the value of `change-log-default-name'.
751 Third arg OTHER-WINDOW non-nil means visit in other window.
753 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
754 never append to an existing entry. Option `add-log-keep-changes-together'
755 otherwise affects whether a new entry is created.
757 Fifth arg PUT-NEW-ENTRY-ON-NEW-LINE non-nil means that if a new
758 entry is created, put it on a new line by itself, do not put it
759 after a comma on an existing line.
761 Option `add-log-always-start-new-record' non-nil means always create a
762 new record, even when the last record was made on the same date and by
765 The change log file can start with a copyright notice and a copying
766 permission notice. The first blank line indicates the end of these
769 Today's date is calculated according to `add-log-time-zone-rule' if
770 non-nil, otherwise in local time."
771 (interactive (list current-prefix-arg
772 (prompt-for-change-log-name)))
773 (let* ((defun (add-log-current-defun))
774 (version (and change-log-version-info-enabled
775 (change-log-version-number-search)))
776 (buf-file-name (if add-log-buffer-file-name-function
777 (funcall add-log-buffer-file-name-function
)
779 (buffer-file (if buf-file-name
(expand-file-name buf-file-name
)))
780 (file-name (expand-file-name (find-change-log file-name buffer-file
)))
781 ;; Set ITEM to the file name to use in the new item.
782 (item (add-log-file-name buffer-file file-name
)))
784 (unless (equal file-name buffer-file-name
)
785 (if (or other-window
(window-dedicated-p (selected-window)))
786 (find-file-other-window file-name
)
787 (find-file file-name
)))
788 (or (derived-mode-p 'change-log-mode
)
791 (goto-char (point-min))
793 (let ((full-name (or add-log-full-name
(user-full-name)))
794 (mailing-address (or add-log-mailing-address user-mail-address
)))
797 (setq full-name
(read-string "Full name: " full-name
))
798 ;; Note that some sites have room and phone number fields in
799 ;; full name which look silly when inserted. Rather than do
800 ;; anything about that here, let user give prefix argument so that
801 ;; s/he can edit the full name field in prompter if s/he wants.
802 (setq mailing-address
803 (read-string "Mailing address: " mailing-address
)))
805 ;; If file starts with a copyright and permission notice, skip them.
806 ;; Assume they end at first blank line.
807 (when (looking-at "Copyright")
808 (search-forward "\n\n")
809 (skip-chars-forward "\n"))
811 ;; Advance into first entry if it is usable; else make new one.
813 (mapcar (lambda (addr)
815 (if (stringp add-log-time-zone-rule
)
816 (let ((tz (getenv "TZ")))
819 (set-time-zone-rule add-log-time-zone-rule
)
820 (funcall add-log-time-format
))
821 (set-time-zone-rule tz
)))
822 (funcall add-log-time-format
))
825 (if (consp mailing-address
)
827 (list mailing-address
)))))
828 (if (and (not add-log-always-start-new-record
)
830 (dolist (entry new-entries hit
)
831 (when (looking-at (regexp-quote entry
))
834 (insert (nth (random (length new-entries
))
836 (if use-hard-newlines hard-newline
"\n")
837 (if use-hard-newlines hard-newline
"\n"))
840 ;; Determine where we should stop searching for a usable
841 ;; item to add to, within this entry.
844 (if (looking-at "\n*[^\n* \t]")
845 (skip-chars-forward "\n")
846 (if add-log-keep-changes-together
847 (forward-page) ; page delimits entries for date
848 (forward-paragraph))) ; paragraph delimits entries for file
851 ;; Now insert the new line for this item.
852 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t
)
853 ;; Put this file name into the existing empty item.
856 ((and (not new-entry
)
857 (let (case-fold-search)
859 (concat (regexp-quote (concat "* " item
))
860 ;; Don't accept `foo.bar' when
861 ;; looking for `foo':
862 "\\(\\s \\|[(),:]\\)")
864 ;; Add to the existing item for the same file.
865 (re-search-forward "^\\s *$\\|^\\s \\*")
866 (goto-char (match-beginning 0))
867 ;; Delete excess empty lines; make just 2.
868 (while (and (not (eobp)) (looking-at "^\\s *$"))
869 (delete-region (point) (line-beginning-position 2)))
870 (insert (if use-hard-newlines hard-newline
"\n")
871 (if use-hard-newlines hard-newline
"\n"))
873 (indent-relative-maybe))
876 (while (looking-at "\\sW")
878 (while (and (not (eobp)) (looking-at "^\\s *$"))
879 (delete-region (point) (line-beginning-position 2)))
880 (insert (if use-hard-newlines hard-newline
"\n")
881 (if use-hard-newlines hard-newline
"\n")
882 (if use-hard-newlines hard-newline
"\n"))
884 (indent-to left-margin
)
886 (if item
(insert item
)))))
887 ;; Now insert the function name, if we have one.
888 ;; Point is at the item for this file,
889 ;; either at the end of the line or at the first blank line.
891 ;; No function name, so put in a colon unless we have just a star.
892 (unless (save-excursion
893 (beginning-of-line 1)
894 (looking-at "\\s *\\(\\*\\s *\\)?$"))
896 (if version
(insert version ?\s
)))
897 ;; Make it easy to get rid of the function name.
899 (unless (save-excursion
900 (beginning-of-line 1)
901 (looking-at "\\s *$"))
903 ;; See if the prev function name has a message yet or not.
904 ;; If not, merge the two items.
905 (let ((pos (point-marker)))
906 (skip-syntax-backward " ")
907 (skip-chars-backward "):")
908 (if (and (not put-new-entry-on-new-line
)
910 (let ((pos (save-excursion (backward-sexp 1) (point))))
911 (when (equal (buffer-substring pos
(point)) defun
)
912 (delete-region pos
(point)))
913 (> fill-column
(+ (current-column) (length defun
) 4))))
914 (progn (skip-chars-backward ", ")
915 (delete-region (point) pos
)
916 (unless (memq (char-before) '(?\
()) (insert ", ")))
917 (when (and (not put-new-entry-on-new-line
) (looking-at "):"))
918 (delete-region (+ 1 (point)) (line-end-position)))
921 (set-marker pos nil
))
923 (if version
(insert version ?\s
)))))
926 (defun add-change-log-entry-other-window (&optional whoami file-name
)
927 "Find change log file in other window and add entry and item.
928 This is just like `add-change-log-entry' except that it displays
929 the change log file in another window."
930 (interactive (if current-prefix-arg
931 (list current-prefix-arg
932 (prompt-for-change-log-name))))
933 (add-change-log-entry whoami file-name t
))
936 (defvar change-log-indent-text
0)
938 (defun change-log-fill-parenthesized-list ()
939 ;; Fill parenthesized lists of names according to GNU standards.
940 ;; * file-name.ext (very-long-foo, very-long-bar, very-long-foobar):
941 ;; should be filled as
942 ;; * file-name.ext (very-long-foo, very-long-bar)
943 ;; (very-long-foobar):
946 (skip-chars-backward " \t")
947 (when (and (equal (char-before) ?\
,)
948 (> (point) (1+ (point-min))))
950 (when (save-excursion
953 (equal (char-after) ?\
()
954 (skip-chars-backward " \t"))
956 ;; Skip everything but a whitespace or asterisk.
957 (and (not (zerop (skip-chars-backward "^ \t\n*")))
958 (skip-chars-backward " \t")
959 ;; We want one asterisk here.
960 (= (skip-chars-backward "*") -
1)
961 (skip-chars-backward " \t")
965 ;; Close list on previous line.
967 (skip-chars-forward " \t\n")
968 ;; Start list on new line.
969 (insert-before-markers "("))
972 (defun change-log-indent ()
973 (change-log-fill-parenthesized-list)
977 (skip-chars-forward " \t")
979 ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
980 ;; Matching the output of add-log-time-format is difficult,
981 ;; but I'll get it has at least two adjacent digits.
982 (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
984 ((looking-at "[^*(]")
985 (+ (current-left-margin) change-log-indent-text
))
986 (t (current-left-margin)))))
987 (pos (save-excursion (indent-line-to indent
) (point))))
988 (if (> pos
(point)) (goto-char pos
))))
991 (defvar smerge-resolve-function
)
992 (defvar copyright-at-end-flag
)
995 (define-derived-mode change-log-mode text-mode
"Change Log"
996 "Major mode for editing change logs; like Indented Text Mode.
997 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
998 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
999 Each entry behaves as a paragraph, and the entries for one day as a page.
1000 Runs `change-log-mode-hook'.
1001 \\{change-log-mode-map}"
1006 show-trailing-whitespace t
)
1007 (set (make-local-variable 'fill-paragraph-function
)
1008 'change-log-fill-paragraph
)
1009 ;; Avoid that filling leaves behind a single "*" on a line.
1010 (add-hook 'fill-nobreak-predicate
1012 (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
1014 (set (make-local-variable 'indent-line-function
) 'change-log-indent
)
1015 (set (make-local-variable 'tab-always-indent
) nil
)
1016 (set (make-local-variable 'copyright-at-end-flag
) t
)
1017 ;; We really do want "^" in paragraph-start below: it is only the
1018 ;; lines that begin at column 0 (despite the left-margin of 8) that
1019 ;; we are looking for. Adding `* ' allows eliding the blank line
1020 ;; between entries for different files.
1021 (set (make-local-variable 'paragraph-start
) "\\s *$\\|\f\\|^\\<")
1022 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
1023 ;; Match null string on the date-line so that the date-line
1024 ;; is grouped with what follows.
1025 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
1026 (set (make-local-variable 'version-control
) 'never
)
1027 (set (make-local-variable 'smerge-resolve-function
)
1028 'change-log-resolve-conflict
)
1029 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
1030 (set (make-local-variable 'font-lock-defaults
)
1031 '(change-log-font-lock-keywords t nil nil backward-paragraph
))
1032 (set (make-local-variable 'multi-isearch-next-buffer-function
)
1033 'change-log-next-buffer
)
1034 (set (make-local-variable 'beginning-of-defun-function
)
1035 'change-log-beginning-of-defun
)
1036 (set (make-local-variable 'end-of-defun-function
)
1037 'change-log-end-of-defun
)
1038 ;; next-error function glue
1039 (setq next-error-function
'change-log-next-error
)
1040 (setq next-error-last-buffer
(current-buffer)))
1042 (defun change-log-next-buffer (&optional buffer wrap
)
1043 "Return the next buffer in the series of ChangeLog file buffers.
1044 This function is used for multiple buffers isearch.
1045 A sequence of buffers is formed by ChangeLog files with decreasing
1046 numeric file name suffixes in the directory of the initial ChangeLog
1047 file were isearch was started."
1048 (let* ((name (change-log-name))
1049 (files (cons name
(sort (file-expand-wildcards
1050 (concat name
"[-.][0-9]*"))
1052 ;; The file's extension may not have a valid
1053 ;; version form (e.g. VC backup revisions).
1055 (version< (substring b
(length name
))
1056 (substring a
(length name
))))))))
1057 (files (if isearch-forward files
(reverse files
))))
1061 (cadr (member (file-name-nondirectory (buffer-file-name buffer
))
1064 ;; It might be nice to have a general feature to replace this. The idea I
1065 ;; have is a variable giving a regexp matching text which should not be
1066 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
1067 ;; But I don't feel up to implementing that today.
1068 (defun change-log-fill-paragraph (&optional justify
)
1069 "Fill the paragraph, but preserve open parentheses at beginning of lines.
1070 Prefix arg means justify as well."
1072 (let ((end (progn (forward-paragraph) (point)))
1073 (beg (progn (backward-paragraph) (point)))
1074 ;; Add lines starting with whitespace followed by a left paren or an
1076 (paragraph-start (concat paragraph-start
"\\|\\s *\\(?:\\s(\\|\\*\\)"))
1077 ;; Make sure we call `change-log-indent'.
1078 (fill-indent-according-to-mode t
))
1079 (fill-region beg end justify
)
1082 (defcustom add-log-current-defun-header-regexp
1083 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
1084 "Heuristic regexp used by `add-log-current-defun' for unknown major modes."
1089 (defvar add-log-lisp-like-modes
1090 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode
)
1091 "*Modes that look like Lisp to `add-log-current-defun'.")
1094 (defvar add-log-c-like-modes
1095 '(c-mode c
++-mode c
++-c-mode objc-mode
)
1096 "*Modes that look like C to `add-log-current-defun'.")
1099 (defvar add-log-tex-like-modes
1100 '(TeX-mode plain-TeX-mode LaTeX-mode tex-mode
)
1101 "*Modes that look like TeX to `add-log-current-defun'.")
1103 (declare-function c-cpp-define-name
"cc-cmds" ())
1104 (declare-function c-defun-name
"cc-cmds" ())
1107 (defun add-log-current-defun ()
1108 "Return name of function definition point is in, or nil.
1110 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
1111 Texinfo (@node titles) and Perl.
1113 Other modes are handled by a heuristic that looks in the 10K before
1114 point for uppercase headings starting in the first column or
1115 identifiers followed by `:' or `='. See variables
1116 `add-log-current-defun-header-regexp' and
1117 `add-log-current-defun-function'.
1119 Has a preference of looking backwards."
1122 (let ((location (point)))
1123 (cond (add-log-current-defun-function
1124 (funcall add-log-current-defun-function
))
1125 ((apply 'derived-mode-p add-log-lisp-like-modes
)
1126 ;; If we are now precisely at the beginning of a defun,
1127 ;; make sure beginning-of-defun finds that one
1128 ;; rather than the previous one.
1129 (or (eobp) (forward-char 1))
1130 (beginning-of-defun)
1131 ;; Make sure we are really inside the defun found,
1133 (when (and (looking-at "\\s(")
1134 (progn (end-of-defun)
1135 (< location
(point)))
1136 (progn (forward-sexp -
1)
1137 (>= location
(point))))
1138 (if (looking-at "\\s(")
1140 ;; Skip the defining construct name, typically "defun"
1143 ;; The second element is usually a symbol being defined.
1144 ;; If it is not, use the first symbol in it.
1145 (skip-chars-forward " \t\n'(")
1146 (buffer-substring-no-properties (point)
1147 (progn (forward-sexp 1)
1149 ((apply 'derived-mode-p add-log-c-like-modes
)
1150 (or (c-cpp-define-name)
1152 ((memq major-mode add-log-tex-like-modes
)
1153 (if (re-search-backward
1154 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
1157 (goto-char (match-beginning 0))
1158 (buffer-substring-no-properties
1159 (1+ (point)) ; without initial backslash
1160 (line-end-position)))))
1161 ((derived-mode-p 'texinfo-mode
)
1162 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
1163 (match-string-no-properties 1)))
1164 ((derived-mode-p 'perl-mode
'cperl-mode
)
1165 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t
)
1166 (match-string-no-properties 1)))
1167 ;; Emacs's autoconf-mode installs its own
1168 ;; `add-log-current-defun-function'. This applies to
1169 ;; a different mode apparently for editing .m4
1171 ((derived-mode-p 'autoconf-mode
)
1172 (if (re-search-backward
1173 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t
)
1174 (match-string-no-properties 3)))
1176 ;; If all else fails, try heuristics
1177 (let (case-fold-search
1180 (when (re-search-backward
1181 add-log-current-defun-header-regexp
1184 (setq result
(or (match-string-no-properties 1)
1185 (match-string-no-properties 0)))
1186 ;; Strip whitespace away
1187 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
1189 (setq result
(match-string-no-properties 1 result
)))
1193 (defvar change-log-get-method-definition-md
)
1195 ;; Subroutine used within change-log-get-method-definition.
1196 ;; Add the last match in the buffer to the end of `md',
1197 ;; followed by the string END; move to the end of that match.
1198 (defun change-log-get-method-definition-1 (end)
1199 (setq change-log-get-method-definition-md
1200 (concat change-log-get-method-definition-md
1203 (goto-char (match-end 0)))
1205 (defun change-log-get-method-definition ()
1206 "For Objective C, return the method name if we are in a method."
1207 (let ((change-log-get-method-definition-md "["))
1209 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
1210 (change-log-get-method-definition-1 " ")))
1213 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
1214 (change-log-get-method-definition-1 "")
1215 (while (not (looking-at "[{;]"))
1217 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
1218 (change-log-get-method-definition-1 ""))
1219 (concat change-log-get-method-definition-md
"]"))))))
1221 (defun change-log-sortable-date-at ()
1222 "Return date of log entry in a consistent form for sorting.
1223 Point is assumed to be at the start of the entry."
1225 (if (looking-at change-log-start-entry-re
)
1226 (let ((date (match-string-no-properties 0)))
1228 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date
)
1229 (concat (match-string 1 date
) (match-string 2 date
)
1230 (match-string 3 date
))
1232 (timezone-make-date-sortable date
)
1234 (error "Bad date")))
1236 (defun change-log-resolve-conflict ()
1237 "Function to be used in `smerge-resolve-function'."
1240 (narrow-to-region (match-beginning 0) (match-end 0))
1241 (let ((mb1 (match-beginning 1))
1243 (mb3 (match-beginning 3))
1245 (tmp1 (generate-new-buffer " *changelog-resolve-1*"))
1246 (tmp2 (generate-new-buffer " *changelog-resolve-2*")))
1248 (let ((buf (current-buffer)))
1249 (with-current-buffer tmp1
1251 (insert-buffer-substring buf mb1 me1
))
1252 (with-current-buffer tmp2
1254 (insert-buffer-substring buf mb3 me3
)
1255 ;; Do the merge here instead of inside `buf' so as to be
1256 ;; more robust in case change-log-merge fails.
1257 (change-log-merge tmp1
))
1258 (goto-char (point-max))
1259 (delete-region (point-min)
1261 (insert-buffer-substring tmp2
))))
1263 (kill-buffer tmp2
))))))
1266 (defun change-log-merge (other-log)
1267 "Merge the contents of change log file OTHER-LOG with this buffer.
1268 Both must be found in Change Log mode (since the merging depends on
1269 the appropriate motion commands). OTHER-LOG can be either a file name
1272 Entries are inserted in chronological order. Both the current and
1273 old-style time formats for entries are supported."
1274 (interactive "*fLog file name to merge: ")
1275 (if (not (derived-mode-p 'change-log-mode
))
1276 (error "Not in Change Log mode"))
1277 (let ((other-buf (if (bufferp other-log
) other-log
1278 (find-file-noselect other-log
)))
1279 (buf (current-buffer))
1282 (goto-char (point-min))
1283 (set-buffer other-buf
)
1284 (goto-char (point-min))
1285 (if (not (derived-mode-p 'change-log-mode
))
1286 (error "%s not found in Change Log mode" other-log
))
1287 ;; Loop through all the entries in OTHER-LOG.
1289 (setq date1
(change-log-sortable-date-at))
1291 end
(progn (forward-page) (point)))
1292 ;; Look for an entry in original buffer that isn't later.
1293 (with-current-buffer buf
1294 (while (and (not (eobp))
1295 (string< date1
(change-log-sortable-date-at)))
1298 (insert-buffer-substring other-buf start end
)
1299 ;; At the end of the original buffer, insert a newline to
1300 ;; separate entries and then the rest of the file being
1303 (and (= ?
\n (char-before))
1304 (or (<= (1- (point)) (point-min))
1305 (= ?
\n (char-before (1- (point)))))))
1306 (insert (if use-hard-newlines hard-newline
"\n")))
1307 ;; Move to the end of it to terminate outer loop.
1308 (with-current-buffer other-buf
1309 (goto-char (point-max)))
1310 (insert-buffer-substring other-buf start
)))))))
1312 (defun change-log-beginning-of-defun ()
1313 (re-search-backward change-log-start-entry-re nil
'move
))
1315 (defun change-log-end-of-defun ()
1316 ;; Look back and if there is no entry there it means we are before
1317 ;; the first ChangeLog entry, so go forward until finding one.
1318 (unless (save-excursion (re-search-backward change-log-start-entry-re nil t
))
1319 (re-search-forward change-log-start-entry-re nil t
))
1321 ;; In case we are at the end of log entry going forward a line will
1322 ;; make us find the next entry when searching. If we are inside of
1323 ;; an entry going forward a line will still keep the point inside
1327 ;; In case we are at the beginning of an entry, move past it.
1328 (when (looking-at change-log-start-entry-re
)
1329 (goto-char (match-end 0))
1332 ;; Search for the start of the next log entry. Go to the end of the
1333 ;; buffer if we could not find a next entry.
1334 (when (re-search-forward change-log-start-entry-re nil
'move
)
1335 (goto-char (match-beginning 0))
1340 ;; arch-tag: 81eee6fc-088f-4372-a37f-80ad9620e762
1341 ;;; add-log.el ends here