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
40 (require 'cl
) ; ignore-errors
43 (defgroup change-log nil
44 "Change log maintenance."
46 :link
'(custom-manual "(emacs)Change Log")
51 (defcustom change-log-default-name nil
52 "Name of a change log file for \\[add-change-log-entry]."
53 :type
'(choice (const :tag
"default" nil
)
56 (put 'change-log-default-name
'safe-local-variable
'string-or-null-p
)
58 (defcustom change-log-mode-hook nil
59 "Normal hook run by `change-log-mode'."
63 ;; Many modes set this variable, so avoid warnings.
65 (defcustom add-log-current-defun-function nil
66 "If non-nil, function to guess name of surrounding function.
67 It is used by `add-log-current-defun' in preference to built-in rules.
68 Returns function's name as a string, or nil if outside a function."
69 :type
'(choice (const nil
) function
)
73 (defcustom add-log-full-name nil
74 "Full name of user, for inclusion in ChangeLog daily headers.
75 This defaults to the value returned by the function `user-full-name'."
76 :type
'(choice (const :tag
"Default" nil
)
81 (defcustom add-log-mailing-address nil
82 "Email addresses of user, for inclusion in ChangeLog headers.
83 This defaults to the value of `user-mail-address'. In addition to
84 being a simple string, this value can also be a list. All elements
85 will be recognized as referring to the same user; when creating a new
86 ChangeLog entry, one element will be chosen at random."
87 :type
'(choice (const :tag
"Default" nil
)
88 (string :tag
"String")
89 (repeat :tag
"List of Strings" string
))
92 (defcustom add-log-time-format
'add-log-iso8601-time-string
93 "Function that defines the time format.
94 For example, `add-log-iso8601-time-string', which gives the
95 date in international ISO 8601 format,
96 and `current-time-string' are two valid values."
97 :type
'(radio (const :tag
"International ISO 8601 format"
98 add-log-iso8601-time-string
)
99 (const :tag
"Old format, as returned by `current-time-string'"
101 (function :tag
"Other"))
104 (defcustom add-log-keep-changes-together nil
105 "If non-nil, normally keep day's log entries for one file together.
107 Log entries for a given file made with \\[add-change-log-entry] or
108 \\[add-change-log-entry-other-window] will only be added to others \
110 today if this variable is non-nil or that file comes first in today's
111 entries. Otherwise another entry for that file will be started. An
115 * bar (...): change 1
117 in the latter case, \\[add-change-log-entry-other-window] in a \
118 buffer visiting `bar', yields:
122 * bar (...): change 1
127 * bar (...): change 1
130 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
136 (defcustom add-log-always-start-new-record nil
137 "If non-nil, `add-change-log-entry' will always start a new record."
142 (defcustom add-log-buffer-file-name-function nil
143 "If non-nil, function to call to identify the full filename of a buffer.
144 This function is called with no argument. If this is nil, the default is to
145 use `buffer-file-name'."
146 :type
'(choice (const nil
) function
)
149 (defcustom add-log-file-name-function nil
150 "If non-nil, function to call to identify the filename for a ChangeLog entry.
151 This function is called with one argument, the value of variable
152 `buffer-file-name' in that buffer. If this is nil, the default is to
153 use the file's name relative to the directory of the change log file."
154 :type
'(choice (const nil
) function
)
158 (defcustom change-log-version-info-enabled nil
159 "If non-nil, enable recording version numbers with the changes."
164 (defcustom change-log-version-number-regexp-list
165 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
167 ;; (defconst ad-version "2.15"
168 (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re
)
169 ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
170 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re
)))
171 "List of regexps to search for version number.
172 The version number must be in group 1.
173 Note: The search is conducted only within 10%, at the beginning of the file."
175 :type
'(repeat regexp
)
178 (defface change-log-date
179 '((t (:inherit font-lock-string-face
)))
180 "Face used to highlight dates in date lines."
183 ;; backward-compatibility alias
184 (put 'change-log-date-face
'face-alias
'change-log-date
)
186 (defface change-log-name
187 '((t (:inherit font-lock-constant-face
)))
188 "Face for highlighting author names."
191 ;; backward-compatibility alias
192 (put 'change-log-name-face
'face-alias
'change-log-name
)
194 (defface change-log-email
195 '((t (:inherit font-lock-variable-name-face
)))
196 "Face for highlighting author email addresses."
199 ;; backward-compatibility alias
200 (put 'change-log-email-face
'face-alias
'change-log-email
)
202 (defface change-log-file
203 '((t (:inherit font-lock-function-name-face
)))
204 "Face for highlighting file names."
207 ;; backward-compatibility alias
208 (put 'change-log-file-face
'face-alias
'change-log-file
)
210 (defface change-log-list
211 '((t (:inherit font-lock-keyword-face
)))
212 "Face for highlighting parenthesized lists of functions or variables."
215 ;; backward-compatibility alias
216 (put 'change-log-list-face
'face-alias
'change-log-list
)
218 (defface change-log-conditionals
219 '((t (:inherit font-lock-variable-name-face
)))
220 "Face for highlighting conditionals of the form `[...]'."
223 ;; backward-compatibility alias
224 (put 'change-log-conditionals-face
'face-alias
'change-log-conditionals
)
226 (defface change-log-function
227 '((t (:inherit font-lock-variable-name-face
)))
228 "Face for highlighting items of the form `<....>'."
231 ;; backward-compatibility alias
232 (put 'change-log-function-face
'face-alias
'change-log-function
)
234 (defface change-log-acknowledgement
235 '((t (:inherit font-lock-comment-face
)))
236 "Face for highlighting acknowledgments."
239 ;; backward-compatibility alias
240 (put 'change-log-acknowledgement-face
'face-alias
'change-log-acknowledgement
)
242 (defconst change-log-file-names-re
"^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)")
243 (defconst change-log-start-entry-re
"^\\sw.........[0-9:+ ]*")
245 (defvar change-log-font-lock-keywords
247 ;; Date lines, new (2000-01-01) and old (Sat Jan 1 00:00:00 2000) styles.
248 ;; Fixme: this regepx is just an approximate one and may match
249 ;; wrongly with a non-date line existing as a random note. In
250 ;; addition, using any kind of fixed setting like this doesn't
251 ;; work if a user customizes add-log-time-format.
252 ("^[0-9-]+ +\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+"
253 (0 'change-log-date-face
)
254 ;; Name and e-mail; some people put e-mail in parens, not angles.
255 ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
257 (2 'change-log-email
)))
260 (,change-log-file-names-re
262 ;; Possibly further names in a list:
263 ("\\=, \\([^ ,:([\n]+\\)" nil nil
(1 'change-log-file
))
264 ;; Possibly a parenthesized list of names:
265 ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
266 nil nil
(1 'change-log-list
))
267 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
268 nil nil
(1 'change-log-list
)))
270 ;; Function or variable names.
271 ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
273 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
274 (1 'change-log-list
)))
277 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals
))
279 ;; Function of change.
280 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function
))
283 ;; Don't include plain "From" because that is vague;
284 ;; we want to encourage people to say something more specific.
285 ;; Note that the FSF does not use "Patches by"; our convention
286 ;; is to put the name of the author of the changes at the top
287 ;; of the change log entry.
288 ("\\(^\\( +\\|\t\\)\\| \\)\\(Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
289 3 'change-log-acknowledgement
))
290 "Additional expressions to highlight in Change Log mode.")
292 (defun change-log-search-file-name (where)
293 "Return the file-name for the change under point."
296 (beginning-of-line 1)
297 (if (looking-at change-log-start-entry-re
)
298 ;; We are at the start of an entry, search forward for a file
301 (re-search-forward change-log-file-names-re nil t
)
303 (if (looking-at change-log-file-names-re
)
304 ;; We found a file name.
306 ;; Look backwards for either a file name or the log entry start.
307 (if (re-search-backward
308 (concat "\\(" change-log-start-entry-re
310 change-log-file-names-re
"\\)") nil t
)
311 (if (match-beginning 1)
312 ;; We got the start of the entry, look forward for a
315 (re-search-forward change-log-file-names-re nil t
)
318 ;; We must be before any file name, look forward.
319 (re-search-forward change-log-file-names-re nil t
)
320 (match-string 2))))))
322 (defun change-log-find-file ()
323 "Visit the file for the change under point."
325 (let ((file (change-log-search-file-name (point))))
326 (if (and file
(file-exists-p file
))
328 (message "No such file or directory: %s" file
))))
330 (defvar change-log-mode-map
331 (let ((map (make-sparse-keymap)))
332 (define-key map
[?\C-c ?\C-p
] 'add-log-edit-prev-comment
)
333 (define-key map
[?\C-c ?\C-n
] 'add-log-edit-next-comment
)
334 (define-key map
[?\C-c ?\C-f
] 'change-log-find-file
)
336 "Keymap for Change Log major mode.")
338 ;; It used to be called change-log-time-zone-rule but really should be
339 ;; called add-log-time-zone-rule since it's only used from add-log-* code.
340 (defvaralias 'change-log-time-zone-rule
'add-log-time-zone-rule
)
341 (defvar add-log-time-zone-rule nil
342 "Time zone used for calculating change log time stamps.
343 It takes the same format as the TZ argument of `set-time-zone-rule'.
344 If nil, use local time.
345 If t, use universal time.")
346 (put 'add-log-time-zone-rule
'safe-local-variable
347 '(lambda (x) (or (booleanp x
) (stringp x
))))
349 (defun add-log-iso8601-time-zone (&optional time
)
350 (let* ((utc-offset (or (car (current-time-zone time
)) 0))
351 (sign (if (< utc-offset
0) ?- ?
+))
352 (sec (abs utc-offset
))
357 (format (cond ((not (zerop ss
)) "%c%02d:%02d:%02d")
358 ((not (zerop mm
)) "%c%02d:%02d")
362 (defvar add-log-iso8601-with-time-zone nil
)
364 (defun add-log-iso8601-time-string ()
365 (let ((time (format-time-string "%Y-%m-%d"
366 nil
(eq t add-log-time-zone-rule
))))
367 (if add-log-iso8601-with-time-zone
368 (concat time
" " (add-log-iso8601-time-zone))
371 (defun change-log-name ()
372 "Return (system-dependent) default name for a change log file."
373 (or change-log-default-name
374 (if (eq system-type
'vax-vms
)
378 (defun add-log-edit-prev-comment (arg)
379 "Cycle backward through Log-Edit mode comment history.
380 With a numeric prefix ARG, go back ARG comments."
383 (narrow-to-region (point)
384 (if (memq last-command
'(add-log-edit-prev-comment
385 add-log-edit-next-comment
))
387 (when (fboundp 'log-edit-previous-comment
)
388 (log-edit-previous-comment arg
)
389 (indent-region (point-min) (point-max))
390 (goto-char (point-min))
391 (unless (save-restriction (widen) (bolp))
392 (delete-region (point) (progn (skip-chars-forward " \t\n") (point))))
393 (set-mark (point-min))
394 (goto-char (point-max))
395 (delete-region (point) (progn (skip-chars-backward " \t\n") (point))))))
397 (defun add-log-edit-next-comment (arg)
398 "Cycle forward through Log-Edit mode comment history.
399 With a numeric prefix ARG, go back ARG comments."
401 (add-log-edit-prev-comment (- arg
)))
404 (defun prompt-for-change-log-name ()
405 "Prompt for a change log name."
406 (let* ((default (change-log-name))
407 (name (expand-file-name
408 (read-file-name (format "Log file (default %s): " default
)
410 ;; Handle something that is syntactically a directory name.
411 ;; Look for ChangeLog or whatever in that directory.
412 (if (string= (file-name-nondirectory name
) "")
413 (expand-file-name (file-name-nondirectory default
)
415 ;; Handle specifying a file that is a directory.
416 (if (file-directory-p name
)
417 (expand-file-name (file-name-nondirectory default
)
418 (file-name-as-directory name
))
421 (defun change-log-version-number-search ()
422 "Return version number of current buffer's file.
423 This is the value returned by `vc-working-revision' or, if that is
424 nil, by matching `change-log-version-number-regexp-list'."
425 (let* ((size (buffer-size))
427 ;; The version number can be anywhere in the file, but
428 ;; restrict search to the file beginning: 10% should be
429 ;; enough to prevent some mishits.
431 ;; Apply percentage only if buffer size is bigger than
433 (if (> size
(* 100 80)) (+ (point) (/ size
10)))))
434 (or (and buffer-file-name
(vc-working-revision buffer-file-name
))
437 (let ((regexps change-log-version-number-regexp-list
)
441 (goto-char (point-min))
442 (when (re-search-forward (pop regexps
) limit t
)
443 (setq version
(match-string 1)
449 (defun find-change-log (&optional file-name buffer-file
)
450 "Find a change log file for \\[add-change-log-entry] and return the name.
452 Optional arg FILE-NAME specifies the file to use.
453 If FILE-NAME is nil, use the value of `change-log-default-name'.
454 If `change-log-default-name' is nil, behave as though it were 'ChangeLog'
455 \(or whatever we use on this operating system).
457 If `change-log-default-name' contains a leading directory component, then
458 simply find it in the current directory. Otherwise, search in the current
459 directory and its successive parents for a file so named.
461 Once a file is found, `change-log-default-name' is set locally in the
462 current buffer to the complete file name.
463 Optional arg BUFFER-FILE overrides `buffer-file-name'."
464 ;; If user specified a file name or if this buffer knows which one to use,
467 (setq file-name
(and change-log-default-name
468 (file-name-directory change-log-default-name
)
469 change-log-default-name
))
471 ;; Chase links in the source file
472 ;; and use the change log in the dir where it points.
473 (setq file-name
(or (and (or buffer-file buffer-file-name
)
476 (or buffer-file buffer-file-name
))))
478 (if (file-directory-p file-name
)
479 (setq file-name
(expand-file-name (change-log-name) file-name
)))
480 ;; Chase links before visiting the file.
481 ;; This makes it easier to use a single change log file
482 ;; for several related directories.
483 (setq file-name
(file-chase-links file-name
))
484 (setq file-name
(expand-file-name file-name
))
485 ;; Move up in the dir hierarchy till we find a change log file.
486 (let ((file1 file-name
)
488 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
489 (progn (setq parent-dir
492 (file-name-directory file1
))))
493 ;; Give up if we are already at the root dir.
494 (not (string= (file-name-directory file1
)
496 ;; Move up to the parent dir and try again.
497 (setq file1
(expand-file-name
498 (file-name-nondirectory (change-log-name))
500 ;; If we found a change log in a parent, use that.
501 (if (or (get-file-buffer file1
) (file-exists-p file1
))
502 (setq file-name file1
)))))
503 ;; Make a local variable in this buffer so we needn't search again.
504 (set (make-local-variable 'change-log-default-name
) file-name
)
507 (defun add-log-file-name (buffer-file log-file
)
508 ;; Never want to add a change log entry for the ChangeLog file itself.
509 (unless (or (null buffer-file
) (string= buffer-file log-file
))
510 (if add-log-file-name-function
511 (funcall add-log-file-name-function buffer-file
)
513 (file-relative-name buffer-file
(file-name-directory log-file
)))
514 ;; If we have a backup file, it's presumably because we're
515 ;; comparing old and new versions (e.g. for deleted
516 ;; functions) and we'll want to use the original name.
517 (if (backup-file-name-p buffer-file
)
518 (file-name-sans-versions buffer-file
)
522 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
523 "Find change log file, and add an entry for today and an item for this file.
524 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
525 name and email (stored in `add-log-full-name' and `add-log-mailing-address').
527 Second arg FILE-NAME is file name of the change log.
528 If nil, use the value of `change-log-default-name'.
530 Third arg OTHER-WINDOW non-nil means visit in other window.
532 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
533 never append to an existing entry. Option `add-log-keep-changes-together'
534 otherwise affects whether a new entry is created.
536 Option `add-log-always-start-new-record' non-nil means always create a
537 new record, even when the last record was made on the same date and by
540 The change log file can start with a copyright notice and a copying
541 permission notice. The first blank line indicates the end of these
544 Today's date is calculated according to `add-log-time-zone-rule' if
545 non-nil, otherwise in local time."
546 (interactive (list current-prefix-arg
547 (prompt-for-change-log-name)))
548 (let* ((defun (add-log-current-defun))
549 (version (and change-log-version-info-enabled
550 (change-log-version-number-search)))
551 (buf-file-name (if add-log-buffer-file-name-function
552 (funcall add-log-buffer-file-name-function
)
554 (buffer-file (if buf-file-name
(expand-file-name buf-file-name
)))
555 (file-name (expand-file-name (find-change-log file-name buffer-file
)))
556 ;; Set ITEM to the file name to use in the new item.
557 (item (add-log-file-name buffer-file file-name
)))
559 (unless (equal file-name buffer-file-name
)
560 (if (or other-window
(window-dedicated-p (selected-window)))
561 (find-file-other-window file-name
)
562 (find-file file-name
)))
563 (or (derived-mode-p 'change-log-mode
)
566 (goto-char (point-min))
568 (let ((full-name (or add-log-full-name
(user-full-name)))
569 (mailing-address (or add-log-mailing-address user-mail-address
)))
572 (setq full-name
(read-string "Full name: " full-name
))
573 ;; Note that some sites have room and phone number fields in
574 ;; full name which look silly when inserted. Rather than do
575 ;; anything about that here, let user give prefix argument so that
576 ;; s/he can edit the full name field in prompter if s/he wants.
577 (setq mailing-address
578 (read-string "Mailing address: " mailing-address
)))
580 ;; If file starts with a copyright and permission notice, skip them.
581 ;; Assume they end at first blank line.
582 (when (looking-at "Copyright")
583 (search-forward "\n\n")
584 (skip-chars-forward "\n"))
586 ;; Advance into first entry if it is usable; else make new one.
588 (mapcar (lambda (addr)
590 (if (stringp add-log-time-zone-rule
)
591 (let ((tz (getenv "TZ")))
594 (set-time-zone-rule add-log-time-zone-rule
)
595 (funcall add-log-time-format
))
596 (set-time-zone-rule tz
)))
597 (funcall add-log-time-format
))
600 (if (consp mailing-address
)
602 (list mailing-address
)))))
603 (if (and (not add-log-always-start-new-record
)
605 (dolist (entry new-entries hit
)
606 (when (looking-at (regexp-quote entry
))
609 (insert (nth (random (length new-entries
))
611 (if use-hard-newlines hard-newline
"\n")
612 (if use-hard-newlines hard-newline
"\n"))
615 ;; Determine where we should stop searching for a usable
616 ;; item to add to, within this entry.
619 (if (looking-at "\n*[^\n* \t]")
620 (skip-chars-forward "\n")
621 (if add-log-keep-changes-together
622 (forward-page) ; page delimits entries for date
623 (forward-paragraph))) ; paragraph delimits entries for file
626 ;; Now insert the new line for this item.
627 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t
)
628 ;; Put this file name into the existing empty item.
631 ((and (not new-entry
)
632 (let (case-fold-search)
634 (concat (regexp-quote (concat "* " item
))
635 ;; Don't accept `foo.bar' when
636 ;; looking for `foo':
637 "\\(\\s \\|[(),:]\\)")
639 ;; Add to the existing item for the same file.
640 (re-search-forward "^\\s *$\\|^\\s \\*")
641 (goto-char (match-beginning 0))
642 ;; Delete excess empty lines; make just 2.
643 (while (and (not (eobp)) (looking-at "^\\s *$"))
644 (delete-region (point) (line-beginning-position 2)))
645 (insert (if use-hard-newlines hard-newline
"\n")
646 (if use-hard-newlines hard-newline
"\n"))
648 (indent-relative-maybe))
651 (while (looking-at "\\sW")
653 (while (and (not (eobp)) (looking-at "^\\s *$"))
654 (delete-region (point) (line-beginning-position 2)))
655 (insert (if use-hard-newlines hard-newline
"\n")
656 (if use-hard-newlines hard-newline
"\n")
657 (if use-hard-newlines hard-newline
"\n"))
659 (indent-to left-margin
)
661 (if item
(insert item
)))))
662 ;; Now insert the function name, if we have one.
663 ;; Point is at the item for this file,
664 ;; either at the end of the line or at the first blank line.
666 ;; No function name, so put in a colon unless we have just a star.
667 (unless (save-excursion
668 (beginning-of-line 1)
669 (looking-at "\\s *\\(\\*\\s *\\)?$"))
671 (if version
(insert version ?\s
)))
672 ;; Make it easy to get rid of the function name.
674 (unless (save-excursion
675 (beginning-of-line 1)
676 (looking-at "\\s *$"))
678 ;; See if the prev function name has a message yet or not.
679 ;; If not, merge the two items.
680 (let ((pos (point-marker)))
681 (skip-syntax-backward " ")
682 (skip-chars-backward "):")
683 (if (and (looking-at "):")
684 (let ((pos (save-excursion (backward-sexp 1) (point))))
685 (when (equal (buffer-substring pos
(point)) defun
)
686 (delete-region pos
(point)))
687 (> fill-column
(+ (current-column) (length defun
) 4))))
688 (progn (skip-chars-backward ", ")
689 (delete-region (point) pos
)
690 (unless (memq (char-before) '(?\
()) (insert ", ")))
691 (if (looking-at "):")
692 (delete-region (+ 1 (point)) (line-end-position)))
695 (set-marker pos nil
))
697 (if version
(insert version ?\s
)))))
700 (defun add-change-log-entry-other-window (&optional whoami file-name
)
701 "Find change log file in other window and add entry and item.
702 This is just like `add-change-log-entry' except that it displays
703 the change log file in another window."
704 (interactive (if current-prefix-arg
705 (list current-prefix-arg
706 (prompt-for-change-log-name))))
707 (add-change-log-entry whoami file-name t
))
710 (defvar change-log-indent-text
0)
712 (defun change-log-fill-parenthesized-list ()
713 ;; Fill parenthesized lists of names according to GNU standards.
714 ;; * file-name.ext (very-long-foo, very-long-bar, very-long-foobar):
715 ;; should be filled as
716 ;; * file-name.ext (very-long-foo, very-long-bar)
717 ;; (very-long-foobar):
720 (skip-chars-backward " \t")
721 (when (and (equal (char-before) ?\
,)
722 (> (point) (1+ (point-min))))
724 (when (save-excursion
727 (equal (char-after) ?\
()
728 (skip-chars-backward " \t"))
730 ;; Skip everything but a whitespace or asterisk.
731 (and (not (zerop (skip-chars-backward "^ \t\n*")))
732 (skip-chars-backward " \t")
733 ;; We want one asterisk here.
734 (= (skip-chars-backward "*") -
1)
735 (skip-chars-backward " \t")
739 ;; Close list on previous line.
741 (skip-chars-forward " \t\n")
742 ;; Start list on new line.
743 (insert-before-markers "("))
746 (defun change-log-indent ()
747 (change-log-fill-parenthesized-list)
751 (skip-chars-forward " \t")
753 ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
754 ;; Matching the output of add-log-time-format is difficult,
755 ;; but I'll get it has at least two adjacent digits.
756 (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
758 ((looking-at "[^*(]")
759 (+ (current-left-margin) change-log-indent-text
))
760 (t (current-left-margin)))))
761 (pos (save-excursion (indent-line-to indent
) (point))))
762 (if (> pos
(point)) (goto-char pos
))))
765 (defvar smerge-resolve-function
)
766 (defvar copyright-at-end-flag
)
769 (define-derived-mode change-log-mode text-mode
"Change Log"
770 "Major mode for editing change logs; like Indented Text Mode.
771 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
772 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
773 Each entry behaves as a paragraph, and the entries for one day as a page.
774 Runs `change-log-mode-hook'.
775 \\{change-log-mode-map}"
780 show-trailing-whitespace t
)
781 (set (make-local-variable 'fill-paragraph-function
)
782 'change-log-fill-paragraph
)
783 ;; Avoid that filling leaves behind a single "*" on a line.
784 (add-hook 'fill-nobreak-predicate
786 (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
788 (set (make-local-variable 'indent-line-function
) 'change-log-indent
)
789 (set (make-local-variable 'tab-always-indent
) nil
)
790 (set (make-local-variable 'copyright-at-end-flag
) t
)
791 ;; We really do want "^" in paragraph-start below: it is only the
792 ;; lines that begin at column 0 (despite the left-margin of 8) that
793 ;; we are looking for. Adding `* ' allows eliding the blank line
794 ;; between entries for different files.
795 (set (make-local-variable 'paragraph-start
) "\\s *$\\|\f\\|^\\<")
796 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
797 ;; Match null string on the date-line so that the date-line
798 ;; is grouped with what follows.
799 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
800 (set (make-local-variable 'version-control
) 'never
)
801 (set (make-local-variable 'smerge-resolve-function
)
802 'change-log-resolve-conflict
)
803 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
804 (set (make-local-variable 'font-lock-defaults
)
805 '(change-log-font-lock-keywords t nil nil backward-paragraph
))
806 (set (make-local-variable 'isearch-buffers-next-buffer-function
)
807 'change-log-next-buffer
)
808 (set (make-local-variable 'beginning-of-defun-function
)
809 'change-log-beginning-of-defun
)
810 (set (make-local-variable 'end-of-defun-function
)
811 'change-log-end-of-defun
)
812 (isearch-buffers-minor-mode))
814 (defun change-log-next-buffer (&optional buffer wrap
)
815 "Return the next buffer in the series of ChangeLog file buffers.
816 This function is used for multiple buffers isearch.
817 A sequence of buffers is formed by ChangeLog files with decreasing
818 numeric file name suffixes in the directory of the initial ChangeLog
819 file were isearch was started."
820 (let* ((name (change-log-name))
821 (files (cons name
(sort (file-expand-wildcards
822 (concat name
"[-.][0-9]*"))
824 ;; The file's extension may not have a valid
825 ;; version form (e.g. VC backup revisions).
827 (version< (substring b
(length name
))
828 (substring a
(length name
))))))))
829 (files (if isearch-forward files
(reverse files
))))
833 (cadr (member (file-name-nondirectory (buffer-file-name buffer
))
836 ;; It might be nice to have a general feature to replace this. The idea I
837 ;; have is a variable giving a regexp matching text which should not be
838 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
839 ;; But I don't feel up to implementing that today.
840 (defun change-log-fill-paragraph (&optional justify
)
841 "Fill the paragraph, but preserve open parentheses at beginning of lines.
842 Prefix arg means justify as well."
844 (let ((end (progn (forward-paragraph) (point)))
845 (beg (progn (backward-paragraph) (point)))
846 ;; Add lines starting with whitespace followed by a left paren or an
848 (paragraph-start (concat paragraph-start
"\\|\\s *\\(?:\\s(\\|\\*\\)"))
849 ;; Make sure we call `change-log-indent'.
850 (fill-indent-according-to-mode t
))
851 (fill-region beg end justify
)
854 (defcustom add-log-current-defun-header-regexp
855 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
856 "Heuristic regexp used by `add-log-current-defun' for unknown major modes."
861 (defvar add-log-lisp-like-modes
862 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode
)
863 "*Modes that look like Lisp to `add-log-current-defun'.")
866 (defvar add-log-c-like-modes
867 '(c-mode c
++-mode c
++-c-mode objc-mode
)
868 "*Modes that look like C to `add-log-current-defun'.")
871 (defvar add-log-tex-like-modes
872 '(TeX-mode plain-TeX-mode LaTeX-mode tex-mode
)
873 "*Modes that look like TeX to `add-log-current-defun'.")
875 (declare-function c-cpp-define-name
"cc-cmds" ())
876 (declare-function c-defun-name
"cc-cmds" ())
879 (defun add-log-current-defun ()
880 "Return name of function definition point is in, or nil.
882 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
883 Texinfo (@node titles) and Perl.
885 Other modes are handled by a heuristic that looks in the 10K before
886 point for uppercase headings starting in the first column or
887 identifiers followed by `:' or `='. See variables
888 `add-log-current-defun-header-regexp' and
889 `add-log-current-defun-function'.
891 Has a preference of looking backwards."
894 (let ((location (point)))
895 (cond (add-log-current-defun-function
896 (funcall add-log-current-defun-function
))
897 ((apply 'derived-mode-p add-log-lisp-like-modes
)
898 ;; If we are now precisely at the beginning of a defun,
899 ;; make sure beginning-of-defun finds that one
900 ;; rather than the previous one.
901 (or (eobp) (forward-char 1))
903 ;; Make sure we are really inside the defun found,
905 (when (and (looking-at "\\s(")
906 (progn (end-of-defun)
907 (< location
(point)))
908 (progn (forward-sexp -
1)
909 (>= location
(point))))
910 (if (looking-at "\\s(")
912 ;; Skip the defining construct name, typically "defun"
915 ;; The second element is usually a symbol being defined.
916 ;; If it is not, use the first symbol in it.
917 (skip-chars-forward " \t\n'(")
918 (buffer-substring-no-properties (point)
919 (progn (forward-sexp 1)
921 ((apply 'derived-mode-p add-log-c-like-modes
)
922 (or (c-cpp-define-name)
924 ((memq major-mode add-log-tex-like-modes
)
925 (if (re-search-backward
926 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
929 (goto-char (match-beginning 0))
930 (buffer-substring-no-properties
931 (1+ (point)) ; without initial backslash
932 (line-end-position)))))
933 ((derived-mode-p 'texinfo-mode
)
934 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
935 (match-string-no-properties 1)))
936 ((derived-mode-p 'perl-mode
'cperl-mode
)
937 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t
)
938 (match-string-no-properties 1)))
939 ;; Emacs's autoconf-mode installs its own
940 ;; `add-log-current-defun-function'. This applies to
941 ;; a different mode apparently for editing .m4
943 ((derived-mode-p 'autoconf-mode
)
944 (if (re-search-backward
945 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t
)
946 (match-string-no-properties 3)))
948 ;; If all else fails, try heuristics
949 (let (case-fold-search
952 (when (re-search-backward
953 add-log-current-defun-header-regexp
956 (setq result
(or (match-string-no-properties 1)
957 (match-string-no-properties 0)))
958 ;; Strip whitespace away
959 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
961 (setq result
(match-string-no-properties 1 result
)))
965 (defvar change-log-get-method-definition-md
)
967 ;; Subroutine used within change-log-get-method-definition.
968 ;; Add the last match in the buffer to the end of `md',
969 ;; followed by the string END; move to the end of that match.
970 (defun change-log-get-method-definition-1 (end)
971 (setq change-log-get-method-definition-md
972 (concat change-log-get-method-definition-md
975 (goto-char (match-end 0)))
977 (defun change-log-get-method-definition ()
978 "For Objective C, return the method name if we are in a method."
979 (let ((change-log-get-method-definition-md "["))
981 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
982 (change-log-get-method-definition-1 " ")))
985 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
986 (change-log-get-method-definition-1 "")
987 (while (not (looking-at "[{;]"))
989 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
990 (change-log-get-method-definition-1 ""))
991 (concat change-log-get-method-definition-md
"]"))))))
993 (defun change-log-sortable-date-at ()
994 "Return date of log entry in a consistent form for sorting.
995 Point is assumed to be at the start of the entry."
997 (if (looking-at change-log-start-entry-re
)
998 (let ((date (match-string-no-properties 0)))
1000 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date
)
1001 (concat (match-string 1 date
) (match-string 2 date
)
1002 (match-string 3 date
))
1004 (timezone-make-date-sortable date
)
1006 (error "Bad date")))
1008 (defun change-log-resolve-conflict ()
1009 "Function to be used in `smerge-resolve-function'."
1012 (narrow-to-region (match-beginning 0) (match-end 0))
1013 (let ((mb1 (match-beginning 1))
1015 (mb3 (match-beginning 3))
1017 (tmp1 (generate-new-buffer " *changelog-resolve-1*"))
1018 (tmp2 (generate-new-buffer " *changelog-resolve-2*")))
1020 (let ((buf (current-buffer)))
1021 (with-current-buffer tmp1
1023 (insert-buffer-substring buf mb1 me1
))
1024 (with-current-buffer tmp2
1026 (insert-buffer-substring buf mb3 me3
)
1027 ;; Do the merge here instead of inside `buf' so as to be
1028 ;; more robust in case change-log-merge fails.
1029 (change-log-merge tmp1
))
1030 (goto-char (point-max))
1031 (delete-region (point-min)
1033 (insert-buffer-substring tmp2
))))
1035 (kill-buffer tmp2
))))))
1038 (defun change-log-merge (other-log)
1039 "Merge the contents of change log file OTHER-LOG with this buffer.
1040 Both must be found in Change Log mode (since the merging depends on
1041 the appropriate motion commands). OTHER-LOG can be either a file name
1044 Entries are inserted in chronological order. Both the current and
1045 old-style time formats for entries are supported."
1046 (interactive "*fLog file name to merge: ")
1047 (if (not (derived-mode-p 'change-log-mode
))
1048 (error "Not in Change Log mode"))
1049 (let ((other-buf (if (bufferp other-log
) other-log
1050 (find-file-noselect other-log
)))
1051 (buf (current-buffer))
1054 (goto-char (point-min))
1055 (set-buffer other-buf
)
1056 (goto-char (point-min))
1057 (if (not (derived-mode-p 'change-log-mode
))
1058 (error "%s not found in Change Log mode" other-log
))
1059 ;; Loop through all the entries in OTHER-LOG.
1061 (setq date1
(change-log-sortable-date-at))
1063 end
(progn (forward-page) (point)))
1064 ;; Look for an entry in original buffer that isn't later.
1065 (with-current-buffer buf
1066 (while (and (not (eobp))
1067 (string< date1
(change-log-sortable-date-at)))
1070 (insert-buffer-substring other-buf start end
)
1071 ;; At the end of the original buffer, insert a newline to
1072 ;; separate entries and then the rest of the file being
1075 (and (= ?
\n (char-before))
1076 (or (<= (1- (point)) (point-min))
1077 (= ?
\n (char-before (1- (point)))))))
1078 (insert (if use-hard-newlines hard-newline
"\n")))
1079 ;; Move to the end of it to terminate outer loop.
1080 (with-current-buffer other-buf
1081 (goto-char (point-max)))
1082 (insert-buffer-substring other-buf start
)))))))
1084 (defun change-log-beginning-of-defun ()
1085 (re-search-backward change-log-start-entry-re nil
'move
))
1087 (defun change-log-end-of-defun ()
1088 ;; Look back and if there is no entry there it means we are before
1089 ;; the first ChangeLog entry, so go forward until finding one.
1090 (unless (save-excursion (re-search-backward change-log-start-entry-re nil t
))
1091 (re-search-forward change-log-start-entry-re nil t
))
1093 ;; In case we are at the end of log entry going forward a line will
1094 ;; make us find the next entry when searching. If we are inside of
1095 ;; an entry going forward a line will still keep the point inside
1099 ;; In case we are at the beginning of an entry, move past it.
1100 (when (looking-at change-log-start-entry-re
)
1101 (goto-char (match-end 0))
1104 ;; Search for the start of the next log entry. Go to the end of the
1105 ;; buffer if we could not find a next entry.
1106 (when (re-search-forward change-log-start-entry-re nil
'move
)
1107 (goto-char (match-beginning 0))
1112 ;; arch-tag: 81eee6fc-088f-4372-a37f-80ad9620e762
1113 ;;; add-log.el ends here