1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1988, 1993, 1994, 1997, 1998, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This facility is documented in the Emacs Manual.
32 ;; - Find/use/create _MTN/log if there's a _MTN directory.
33 ;; - Find/use/create ++log.* if there's an {arch} directory.
34 ;; - Use an open *VC-Log* or *cvs-commit* buffer if it's related to the
36 ;; - Don't add TAB indents (and username?) if inserting entries in those
42 (require 'cl
) ; ignore-errors
45 (defgroup change-log nil
46 "Change log maintenance."
48 :link
'(custom-manual "(emacs)Change Log")
53 (defcustom change-log-default-name nil
54 "Name of a change log file for \\[add-change-log-entry]."
55 :type
'(choice (const :tag
"default" nil
)
58 (put 'change-log-default-name
'safe-local-variable
'string-or-null-p
)
60 (defcustom change-log-mode-hook nil
61 "Normal hook run by `change-log-mode'."
65 ;; Many modes set this variable, so avoid warnings.
67 (defcustom add-log-current-defun-function nil
68 "If non-nil, function to guess name of surrounding function.
69 It is used by `add-log-current-defun' in preference to built-in rules.
70 Returns function's name as a string, or nil if outside a function."
71 :type
'(choice (const nil
) function
)
75 (defcustom add-log-full-name nil
76 "Full name of user, for inclusion in ChangeLog daily headers.
77 This defaults to the value returned by the function `user-full-name'."
78 :type
'(choice (const :tag
"Default" nil
)
83 (defcustom add-log-mailing-address nil
84 "Email addresses of user, for inclusion in ChangeLog headers.
85 This defaults to the value of `user-mail-address'. In addition to
86 being a simple string, this value can also be a list. All elements
87 will be recognized as referring to the same user; when creating a new
88 ChangeLog entry, one element will be chosen at random."
89 :type
'(choice (const :tag
"Default" nil
)
90 (string :tag
"String")
91 (repeat :tag
"List of Strings" string
))
94 (defcustom add-log-time-format
'add-log-iso8601-time-string
95 "Function that defines the time format.
96 For example, `add-log-iso8601-time-string', which gives the
97 date in international ISO 8601 format,
98 and `current-time-string' are two valid values."
99 :type
'(radio (const :tag
"International ISO 8601 format"
100 add-log-iso8601-time-string
)
101 (const :tag
"Old format, as returned by `current-time-string'"
103 (function :tag
"Other"))
106 (defcustom add-log-keep-changes-together nil
107 "If non-nil, normally keep day's log entries for one file together.
109 Log entries for a given file made with \\[add-change-log-entry] or
110 \\[add-change-log-entry-other-window] will only be added to others \
112 today if this variable is non-nil or that file comes first in today's
113 entries. Otherwise another entry for that file will be started. An
117 * bar (...): change 1
119 in the latter case, \\[add-change-log-entry-other-window] in a \
120 buffer visiting `bar', yields:
124 * bar (...): change 1
129 * bar (...): change 1
132 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
138 (defcustom add-log-always-start-new-record nil
139 "If non-nil, `add-change-log-entry' will always start a new record."
144 (defcustom add-log-buffer-file-name-function nil
145 "If non-nil, function to call to identify the full filename of a buffer.
146 This function is called with no argument. If this is nil, the default is to
147 use `buffer-file-name'."
148 :type
'(choice (const nil
) function
)
151 (defcustom add-log-file-name-function nil
152 "If non-nil, function to call to identify the filename for a ChangeLog entry.
153 This function is called with one argument, the value of variable
154 `buffer-file-name' in that buffer. If this is nil, the default is to
155 use the file's name relative to the directory of the change log file."
156 :type
'(choice (const nil
) function
)
160 (defcustom change-log-version-info-enabled nil
161 "If non-nil, enable recording version numbers with the changes."
166 (defcustom change-log-version-number-regexp-list
167 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
169 ;; (defconst ad-version "2.15"
170 (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re
)
171 ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
172 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re
)))
173 "List of regexps to search for version number.
174 The version number must be in group 1.
175 Note: The search is conducted only within 10%, at the beginning of the file."
177 :type
'(repeat regexp
)
180 (defface change-log-date
181 '((t (:inherit font-lock-string-face
)))
182 "Face used to highlight dates in date lines."
185 ;; backward-compatibility alias
186 (put 'change-log-date-face
'face-alias
'change-log-date
)
188 (defface change-log-name
189 '((t (:inherit font-lock-constant-face
)))
190 "Face for highlighting author names."
193 ;; backward-compatibility alias
194 (put 'change-log-name-face
'face-alias
'change-log-name
)
196 (defface change-log-email
197 '((t (:inherit font-lock-variable-name-face
)))
198 "Face for highlighting author email addresses."
201 ;; backward-compatibility alias
202 (put 'change-log-email-face
'face-alias
'change-log-email
)
204 (defface change-log-file
205 '((t (:inherit font-lock-function-name-face
)))
206 "Face for highlighting file names."
209 ;; backward-compatibility alias
210 (put 'change-log-file-face
'face-alias
'change-log-file
)
212 (defface change-log-list
213 '((t (:inherit font-lock-keyword-face
)))
214 "Face for highlighting parenthesized lists of functions or variables."
217 ;; backward-compatibility alias
218 (put 'change-log-list-face
'face-alias
'change-log-list
)
220 (defface change-log-conditionals
221 '((t (:inherit font-lock-variable-name-face
)))
222 "Face for highlighting conditionals of the form `[...]'."
225 ;; backward-compatibility alias
226 (put 'change-log-conditionals-face
'face-alias
'change-log-conditionals
)
228 (defface change-log-function
229 '((t (:inherit font-lock-variable-name-face
)))
230 "Face for highlighting items of the form `<....>'."
233 ;; backward-compatibility alias
234 (put 'change-log-function-face
'face-alias
'change-log-function
)
236 (defface change-log-acknowledgement
237 '((t (:inherit font-lock-comment-face
)))
238 "Face for highlighting acknowledgments."
241 ;; backward-compatibility alias
242 (put 'change-log-acknowledgement-face
'face-alias
'change-log-acknowledgement
)
244 (defconst change-log-file-names-re
"^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)")
245 (defconst change-log-start-entry-re
"^\\sw.........[0-9:+ ]*")
247 (defvar change-log-font-lock-keywords
249 ;; Date lines, new (2000-01-01) and old (Sat Jan 1 00:00:00 2000) styles.
250 ;; Fixme: this regepx is just an approximate one and may match
251 ;; wrongly with a non-date line existing as a random note. In
252 ;; addition, using any kind of fixed setting like this doesn't
253 ;; work if a user customizes add-log-time-format.
254 ("^[0-9-]+ +\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+"
255 (0 'change-log-date-face
)
256 ;; Name and e-mail; some people put e-mail in parens, not angles.
257 ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
259 (2 'change-log-email
)))
262 (,change-log-file-names-re
264 ;; Possibly further names in a list:
265 ("\\=, \\([^ ,:([\n]+\\)" nil nil
(1 'change-log-file
))
266 ;; Possibly a parenthesized list of names:
267 ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
268 nil nil
(1 'change-log-list
))
269 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
270 nil nil
(1 'change-log-list
)))
272 ;; Function or variable names.
273 ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
275 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
276 (1 'change-log-list
)))
279 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals
))
281 ;; Function of change.
282 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function
))
285 ;; Don't include plain "From" because that is vague;
286 ;; we want to encourage people to say something more specific.
287 ;; Note that the FSF does not use "Patches by"; our convention
288 ;; is to put the name of the author of the changes at the top
289 ;; of the change log entry.
290 ("\\(^\\( +\\|\t\\)\\| \\)\\(Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
291 3 'change-log-acknowledgement
))
292 "Additional expressions to highlight in Change Log mode.")
294 (defun change-log-search-file-name (where)
295 "Return the file-name for the change under point."
298 (beginning-of-line 1)
299 (if (looking-at change-log-start-entry-re
)
300 ;; We are at the start of an entry, search forward for a file
303 (re-search-forward change-log-file-names-re nil t
)
305 (if (looking-at change-log-file-names-re
)
306 ;; We found a file name.
308 ;; Look backwards for either a file name or the log entry start.
309 (if (re-search-backward
310 (concat "\\(" change-log-start-entry-re
312 change-log-file-names-re
"\\)") nil t
)
313 (if (match-beginning 1)
314 ;; We got the start of the entry, look forward for a
317 (re-search-forward change-log-file-names-re nil t
)
320 ;; We must be before any file name, look forward.
321 (re-search-forward change-log-file-names-re nil t
)
322 (match-string 2))))))
324 (defun change-log-find-file ()
325 "Visit the file for the change under point."
327 (let ((file (change-log-search-file-name (point))))
328 (if (and file
(file-exists-p file
))
330 (message "No such file or directory: %s" file
))))
332 (defvar change-log-mode-map
333 (let ((map (make-sparse-keymap)))
334 (define-key map
[?\C-c ?\C-p
] 'add-log-edit-prev-comment
)
335 (define-key map
[?\C-c ?\C-n
] 'add-log-edit-next-comment
)
336 (define-key map
[?\C-c ?\C-f
] 'change-log-find-file
)
338 "Keymap for Change Log major mode.")
340 ;; It used to be called change-log-time-zone-rule but really should be
341 ;; called add-log-time-zone-rule since it's only used from add-log-* code.
342 (defvaralias 'change-log-time-zone-rule
'add-log-time-zone-rule
)
343 (defvar add-log-time-zone-rule nil
344 "Time zone used for calculating change log time stamps.
345 It takes the same format as the TZ argument of `set-time-zone-rule'.
346 If nil, use local time.
347 If t, use universal time.")
348 (put 'add-log-time-zone-rule
'safe-local-variable
349 '(lambda (x) (or (booleanp x
) (stringp x
))))
351 (defun add-log-iso8601-time-zone (&optional time
)
352 (let* ((utc-offset (or (car (current-time-zone time
)) 0))
353 (sign (if (< utc-offset
0) ?- ?
+))
354 (sec (abs utc-offset
))
359 (format (cond ((not (zerop ss
)) "%c%02d:%02d:%02d")
360 ((not (zerop mm
)) "%c%02d:%02d")
364 (defvar add-log-iso8601-with-time-zone nil
)
366 (defun add-log-iso8601-time-string ()
367 (let ((time (format-time-string "%Y-%m-%d"
368 nil
(eq t add-log-time-zone-rule
))))
369 (if add-log-iso8601-with-time-zone
370 (concat time
" " (add-log-iso8601-time-zone))
373 (defun change-log-name ()
374 "Return (system-dependent) default name for a change log file."
375 (or change-log-default-name
376 (if (eq system-type
'vax-vms
)
380 (defun add-log-edit-prev-comment (arg)
381 "Cycle backward through Log-Edit mode comment history.
382 With a numeric prefix ARG, go back ARG comments."
385 (narrow-to-region (point)
386 (if (memq last-command
'(add-log-edit-prev-comment
387 add-log-edit-next-comment
))
389 (when (fboundp 'log-edit-previous-comment
)
390 (log-edit-previous-comment arg
)
391 (indent-region (point-min) (point-max))
392 (goto-char (point-min))
393 (unless (save-restriction (widen) (bolp))
394 (delete-region (point) (progn (skip-chars-forward " \t\n") (point))))
395 (set-mark (point-min))
396 (goto-char (point-max))
397 (delete-region (point) (progn (skip-chars-backward " \t\n") (point))))))
399 (defun add-log-edit-next-comment (arg)
400 "Cycle forward through Log-Edit mode comment history.
401 With a numeric prefix ARG, go back ARG comments."
403 (add-log-edit-prev-comment (- arg
)))
406 (defun prompt-for-change-log-name ()
407 "Prompt for a change log name."
408 (let* ((default (change-log-name))
409 (name (expand-file-name
410 (read-file-name (format "Log file (default %s): " default
)
412 ;; Handle something that is syntactically a directory name.
413 ;; Look for ChangeLog or whatever in that directory.
414 (if (string= (file-name-nondirectory name
) "")
415 (expand-file-name (file-name-nondirectory default
)
417 ;; Handle specifying a file that is a directory.
418 (if (file-directory-p name
)
419 (expand-file-name (file-name-nondirectory default
)
420 (file-name-as-directory name
))
423 (defun change-log-version-number-search ()
424 "Return version number of current buffer's file.
425 This is the value returned by `vc-working-revision' or, if that is
426 nil, by matching `change-log-version-number-regexp-list'."
427 (let* ((size (buffer-size))
429 ;; The version number can be anywhere in the file, but
430 ;; restrict search to the file beginning: 10% should be
431 ;; enough to prevent some mishits.
433 ;; Apply percentage only if buffer size is bigger than
435 (if (> size
(* 100 80)) (+ (point) (/ size
10)))))
436 (or (and buffer-file-name
(vc-working-revision buffer-file-name
))
439 (let ((regexps change-log-version-number-regexp-list
)
443 (goto-char (point-min))
444 (when (re-search-forward (pop regexps
) limit t
)
445 (setq version
(match-string 1)
451 (defun find-change-log (&optional file-name buffer-file
)
452 "Find a change log file for \\[add-change-log-entry] and return the name.
454 Optional arg FILE-NAME specifies the file to use.
455 If FILE-NAME is nil, use the value of `change-log-default-name'.
456 If `change-log-default-name' is nil, behave as though it were 'ChangeLog'
457 \(or whatever we use on this operating system).
459 If `change-log-default-name' contains a leading directory component, then
460 simply find it in the current directory. Otherwise, search in the current
461 directory and its successive parents for a file so named.
463 Once a file is found, `change-log-default-name' is set locally in the
464 current buffer to the complete file name.
465 Optional arg BUFFER-FILE overrides `buffer-file-name'."
466 ;; If user specified a file name or if this buffer knows which one to use,
469 (setq file-name
(and change-log-default-name
470 (file-name-directory change-log-default-name
)
471 change-log-default-name
))
473 ;; Chase links in the source file
474 ;; and use the change log in the dir where it points.
475 (setq file-name
(or (and (or buffer-file buffer-file-name
)
478 (or buffer-file buffer-file-name
))))
480 (if (file-directory-p file-name
)
481 (setq file-name
(expand-file-name (change-log-name) file-name
)))
482 ;; Chase links before visiting the file.
483 ;; This makes it easier to use a single change log file
484 ;; for several related directories.
485 (setq file-name
(file-chase-links file-name
))
486 (setq file-name
(expand-file-name file-name
))
487 ;; Move up in the dir hierarchy till we find a change log file.
488 (let ((file1 file-name
)
490 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
491 (progn (setq parent-dir
494 (file-name-directory file1
))))
495 ;; Give up if we are already at the root dir.
496 (not (string= (file-name-directory file1
)
498 ;; Move up to the parent dir and try again.
499 (setq file1
(expand-file-name
500 (file-name-nondirectory (change-log-name))
502 ;; If we found a change log in a parent, use that.
503 (if (or (get-file-buffer file1
) (file-exists-p file1
))
504 (setq file-name file1
)))))
505 ;; Make a local variable in this buffer so we needn't search again.
506 (set (make-local-variable 'change-log-default-name
) file-name
)
509 (defun add-log-file-name (buffer-file log-file
)
510 ;; Never want to add a change log entry for the ChangeLog file itself.
511 (unless (or (null buffer-file
) (string= buffer-file log-file
))
512 (if add-log-file-name-function
513 (funcall add-log-file-name-function buffer-file
)
515 (file-relative-name buffer-file
(file-name-directory log-file
)))
516 ;; If we have a backup file, it's presumably because we're
517 ;; comparing old and new versions (e.g. for deleted
518 ;; functions) and we'll want to use the original name.
519 (if (backup-file-name-p buffer-file
)
520 (file-name-sans-versions buffer-file
)
524 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
525 "Find change log file, and add an entry for today and an item for this file.
526 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
527 name and email (stored in `add-log-full-name' and `add-log-mailing-address').
529 Second arg FILE-NAME is file name of the change log.
530 If nil, use the value of `change-log-default-name'.
532 Third arg OTHER-WINDOW non-nil means visit in other window.
534 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
535 never append to an existing entry. Option `add-log-keep-changes-together'
536 otherwise affects whether a new entry is created.
538 Option `add-log-always-start-new-record' non-nil means always create a
539 new record, even when the last record was made on the same date and by
542 The change log file can start with a copyright notice and a copying
543 permission notice. The first blank line indicates the end of these
546 Today's date is calculated according to `add-log-time-zone-rule' if
547 non-nil, otherwise in local time."
548 (interactive (list current-prefix-arg
549 (prompt-for-change-log-name)))
550 (let* ((defun (add-log-current-defun))
551 (version (and change-log-version-info-enabled
552 (change-log-version-number-search)))
553 (buf-file-name (if add-log-buffer-file-name-function
554 (funcall add-log-buffer-file-name-function
)
556 (buffer-file (if buf-file-name
(expand-file-name buf-file-name
)))
557 (file-name (expand-file-name (find-change-log file-name buffer-file
)))
558 ;; Set ITEM to the file name to use in the new item.
559 (item (add-log-file-name buffer-file file-name
)))
561 (unless (equal file-name buffer-file-name
)
562 (if (or other-window
(window-dedicated-p (selected-window)))
563 (find-file-other-window file-name
)
564 (find-file file-name
)))
565 (or (derived-mode-p 'change-log-mode
)
568 (goto-char (point-min))
570 (let ((full-name (or add-log-full-name
(user-full-name)))
571 (mailing-address (or add-log-mailing-address user-mail-address
)))
574 (setq full-name
(read-string "Full name: " full-name
))
575 ;; Note that some sites have room and phone number fields in
576 ;; full name which look silly when inserted. Rather than do
577 ;; anything about that here, let user give prefix argument so that
578 ;; s/he can edit the full name field in prompter if s/he wants.
579 (setq mailing-address
580 (read-string "Mailing address: " mailing-address
)))
582 ;; If file starts with a copyright and permission notice, skip them.
583 ;; Assume they end at first blank line.
584 (when (looking-at "Copyright")
585 (search-forward "\n\n")
586 (skip-chars-forward "\n"))
588 ;; Advance into first entry if it is usable; else make new one.
590 (mapcar (lambda (addr)
592 (if (stringp add-log-time-zone-rule
)
593 (let ((tz (getenv "TZ")))
596 (set-time-zone-rule add-log-time-zone-rule
)
597 (funcall add-log-time-format
))
598 (set-time-zone-rule tz
)))
599 (funcall add-log-time-format
))
602 (if (consp mailing-address
)
604 (list mailing-address
)))))
605 (if (and (not add-log-always-start-new-record
)
607 (dolist (entry new-entries hit
)
608 (when (looking-at (regexp-quote entry
))
611 (insert (nth (random (length new-entries
))
613 (if use-hard-newlines hard-newline
"\n")
614 (if use-hard-newlines hard-newline
"\n"))
617 ;; Determine where we should stop searching for a usable
618 ;; item to add to, within this entry.
621 (if (looking-at "\n*[^\n* \t]")
622 (skip-chars-forward "\n")
623 (if add-log-keep-changes-together
624 (forward-page) ; page delimits entries for date
625 (forward-paragraph))) ; paragraph delimits entries for file
628 ;; Now insert the new line for this item.
629 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t
)
630 ;; Put this file name into the existing empty item.
633 ((and (not new-entry
)
634 (let (case-fold-search)
636 (concat (regexp-quote (concat "* " item
))
637 ;; Don't accept `foo.bar' when
638 ;; looking for `foo':
639 "\\(\\s \\|[(),:]\\)")
641 ;; Add to the existing item for the same file.
642 (re-search-forward "^\\s *$\\|^\\s \\*")
643 (goto-char (match-beginning 0))
644 ;; Delete excess empty lines; make just 2.
645 (while (and (not (eobp)) (looking-at "^\\s *$"))
646 (delete-region (point) (line-beginning-position 2)))
647 (insert (if use-hard-newlines hard-newline
"\n")
648 (if use-hard-newlines hard-newline
"\n"))
650 (indent-relative-maybe))
653 (while (looking-at "\\sW")
655 (while (and (not (eobp)) (looking-at "^\\s *$"))
656 (delete-region (point) (line-beginning-position 2)))
657 (insert (if use-hard-newlines hard-newline
"\n")
658 (if use-hard-newlines hard-newline
"\n")
659 (if use-hard-newlines hard-newline
"\n"))
661 (indent-to left-margin
)
663 (if item
(insert item
)))))
664 ;; Now insert the function name, if we have one.
665 ;; Point is at the item for this file,
666 ;; either at the end of the line or at the first blank line.
668 ;; No function name, so put in a colon unless we have just a star.
669 (unless (save-excursion
670 (beginning-of-line 1)
671 (looking-at "\\s *\\(\\*\\s *\\)?$"))
673 (if version
(insert version ?\s
)))
674 ;; Make it easy to get rid of the function name.
676 (unless (save-excursion
677 (beginning-of-line 1)
678 (looking-at "\\s *$"))
680 ;; See if the prev function name has a message yet or not.
681 ;; If not, merge the two items.
682 (let ((pos (point-marker)))
683 (skip-syntax-backward " ")
684 (skip-chars-backward "):")
685 (if (and (looking-at "):")
686 (let ((pos (save-excursion (backward-sexp 1) (point))))
687 (when (equal (buffer-substring pos
(point)) defun
)
688 (delete-region pos
(point)))
689 (> fill-column
(+ (current-column) (length defun
) 4))))
690 (progn (skip-chars-backward ", ")
691 (delete-region (point) pos
)
692 (unless (memq (char-before) '(?\
()) (insert ", ")))
693 (if (looking-at "):")
694 (delete-region (+ 1 (point)) (line-end-position)))
697 (set-marker pos nil
))
699 (if version
(insert version ?\s
)))))
702 (defun add-change-log-entry-other-window (&optional whoami file-name
)
703 "Find change log file in other window and add entry and item.
704 This is just like `add-change-log-entry' except that it displays
705 the change log file in another window."
706 (interactive (if current-prefix-arg
707 (list current-prefix-arg
708 (prompt-for-change-log-name))))
709 (add-change-log-entry whoami file-name t
))
712 (defvar change-log-indent-text
0)
714 (defun change-log-fill-parenthesized-list ()
715 ;; Fill parenthesized lists of names according to GNU standards.
716 ;; * file-name.ext (very-long-foo, very-long-bar, very-long-foobar):
717 ;; should be filled as
718 ;; * file-name.ext (very-long-foo, very-long-bar)
719 ;; (very-long-foobar):
722 (skip-chars-backward " \t")
723 (when (and (equal (char-before) ?\
,)
724 (> (point) (1+ (point-min))))
726 (when (save-excursion
729 (equal (char-after) ?\
()
730 (skip-chars-backward " \t"))
732 ;; Skip everything but a whitespace or asterisk.
733 (and (not (zerop (skip-chars-backward "^ \t\n*")))
734 (skip-chars-backward " \t")
735 ;; We want one asterisk here.
736 (= (skip-chars-backward "*") -
1)
737 (skip-chars-backward " \t")
741 ;; Close list on previous line.
743 (skip-chars-forward " \t\n")
744 ;; Start list on new line.
745 (insert-before-markers "("))
748 (defun change-log-indent ()
749 (change-log-fill-parenthesized-list)
753 (skip-chars-forward " \t")
755 ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
756 ;; Matching the output of add-log-time-format is difficult,
757 ;; but I'll get it has at least two adjacent digits.
758 (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
760 ((looking-at "[^*(]")
761 (+ (current-left-margin) change-log-indent-text
))
762 (t (current-left-margin)))))
763 (pos (save-excursion (indent-line-to indent
) (point))))
764 (if (> pos
(point)) (goto-char pos
))))
767 (defvar smerge-resolve-function
)
768 (defvar copyright-at-end-flag
)
771 (define-derived-mode change-log-mode text-mode
"Change Log"
772 "Major mode for editing change logs; like Indented Text Mode.
773 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
774 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
775 Each entry behaves as a paragraph, and the entries for one day as a page.
776 Runs `change-log-mode-hook'.
777 \\{change-log-mode-map}"
782 show-trailing-whitespace t
)
783 (set (make-local-variable 'fill-paragraph-function
)
784 'change-log-fill-paragraph
)
785 ;; Avoid that filling leaves behind a single "*" on a line.
786 (add-hook 'fill-nobreak-predicate
788 (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
790 (set (make-local-variable 'indent-line-function
) 'change-log-indent
)
791 (set (make-local-variable 'tab-always-indent
) nil
)
792 (set (make-local-variable 'copyright-at-end-flag
) t
)
793 ;; We really do want "^" in paragraph-start below: it is only the
794 ;; lines that begin at column 0 (despite the left-margin of 8) that
795 ;; we are looking for. Adding `* ' allows eliding the blank line
796 ;; between entries for different files.
797 (set (make-local-variable 'paragraph-start
) "\\s *$\\|\f\\|^\\<")
798 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
799 ;; Match null string on the date-line so that the date-line
800 ;; is grouped with what follows.
801 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
802 (set (make-local-variable 'version-control
) 'never
)
803 (set (make-local-variable 'smerge-resolve-function
)
804 'change-log-resolve-conflict
)
805 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
806 (set (make-local-variable 'font-lock-defaults
)
807 '(change-log-font-lock-keywords t nil nil backward-paragraph
))
808 (set (make-local-variable 'isearch-buffers-next-buffer-function
)
809 'change-log-next-buffer
)
810 (set (make-local-variable 'beginning-of-defun-function
)
811 'change-log-beginning-of-defun
)
812 (set (make-local-variable 'end-of-defun-function
)
813 'change-log-end-of-defun
)
814 (isearch-buffers-minor-mode))
816 (defun change-log-next-buffer (&optional buffer wrap
)
817 "Return the next buffer in the series of ChangeLog file buffers.
818 This function is used for multiple buffers isearch.
819 A sequence of buffers is formed by ChangeLog files with decreasing
820 numeric file name suffixes in the directory of the initial ChangeLog
821 file were isearch was started."
822 (let* ((name (change-log-name))
823 (files (cons name
(sort (file-expand-wildcards
824 (concat name
"[-.][0-9]*"))
826 ;; The file's extension may not have a valid
827 ;; version form (e.g. VC backup revisions).
829 (version< (substring b
(length name
))
830 (substring a
(length name
))))))))
831 (files (if isearch-forward files
(reverse files
))))
835 (cadr (member (file-name-nondirectory (buffer-file-name buffer
))
838 ;; It might be nice to have a general feature to replace this. The idea I
839 ;; have is a variable giving a regexp matching text which should not be
840 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
841 ;; But I don't feel up to implementing that today.
842 (defun change-log-fill-paragraph (&optional justify
)
843 "Fill the paragraph, but preserve open parentheses at beginning of lines.
844 Prefix arg means justify as well."
846 (let ((end (progn (forward-paragraph) (point)))
847 (beg (progn (backward-paragraph) (point)))
848 ;; Add lines starting with whitespace followed by a left paren or an
850 (paragraph-start (concat paragraph-start
"\\|\\s *\\(?:\\s(\\|\\*\\)"))
851 ;; Make sure we call `change-log-indent'.
852 (fill-indent-according-to-mode t
))
853 (fill-region beg end justify
)
856 (defcustom add-log-current-defun-header-regexp
857 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
858 "Heuristic regexp used by `add-log-current-defun' for unknown major modes."
863 (defvar add-log-lisp-like-modes
864 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode
)
865 "*Modes that look like Lisp to `add-log-current-defun'.")
868 (defvar add-log-c-like-modes
869 '(c-mode c
++-mode c
++-c-mode objc-mode
)
870 "*Modes that look like C to `add-log-current-defun'.")
873 (defvar add-log-tex-like-modes
874 '(TeX-mode plain-TeX-mode LaTeX-mode tex-mode
)
875 "*Modes that look like TeX to `add-log-current-defun'.")
877 (declare-function c-cpp-define-name
"cc-cmds" ())
878 (declare-function c-defun-name
"cc-cmds" ())
881 (defun add-log-current-defun ()
882 "Return name of function definition point is in, or nil.
884 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
885 Texinfo (@node titles) and Perl.
887 Other modes are handled by a heuristic that looks in the 10K before
888 point for uppercase headings starting in the first column or
889 identifiers followed by `:' or `='. See variables
890 `add-log-current-defun-header-regexp' and
891 `add-log-current-defun-function'.
893 Has a preference of looking backwards."
896 (let ((location (point)))
897 (cond (add-log-current-defun-function
898 (funcall add-log-current-defun-function
))
899 ((apply 'derived-mode-p add-log-lisp-like-modes
)
900 ;; If we are now precisely at the beginning of a defun,
901 ;; make sure beginning-of-defun finds that one
902 ;; rather than the previous one.
903 (or (eobp) (forward-char 1))
905 ;; Make sure we are really inside the defun found,
907 (when (and (looking-at "\\s(")
908 (progn (end-of-defun)
909 (< location
(point)))
910 (progn (forward-sexp -
1)
911 (>= location
(point))))
912 (if (looking-at "\\s(")
914 ;; Skip the defining construct name, typically "defun"
917 ;; The second element is usually a symbol being defined.
918 ;; If it is not, use the first symbol in it.
919 (skip-chars-forward " \t\n'(")
920 (buffer-substring-no-properties (point)
921 (progn (forward-sexp 1)
923 ((apply 'derived-mode-p add-log-c-like-modes
)
924 (or (c-cpp-define-name)
926 ((memq major-mode add-log-tex-like-modes
)
927 (if (re-search-backward
928 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
931 (goto-char (match-beginning 0))
932 (buffer-substring-no-properties
933 (1+ (point)) ; without initial backslash
934 (line-end-position)))))
935 ((derived-mode-p 'texinfo-mode
)
936 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
937 (match-string-no-properties 1)))
938 ((derived-mode-p 'perl-mode
'cperl-mode
)
939 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t
)
940 (match-string-no-properties 1)))
941 ;; Emacs's autoconf-mode installs its own
942 ;; `add-log-current-defun-function'. This applies to
943 ;; a different mode apparently for editing .m4
945 ((derived-mode-p 'autoconf-mode
)
946 (if (re-search-backward
947 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t
)
948 (match-string-no-properties 3)))
950 ;; If all else fails, try heuristics
951 (let (case-fold-search
954 (when (re-search-backward
955 add-log-current-defun-header-regexp
958 (setq result
(or (match-string-no-properties 1)
959 (match-string-no-properties 0)))
960 ;; Strip whitespace away
961 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
963 (setq result
(match-string-no-properties 1 result
)))
967 (defvar change-log-get-method-definition-md
)
969 ;; Subroutine used within change-log-get-method-definition.
970 ;; Add the last match in the buffer to the end of `md',
971 ;; followed by the string END; move to the end of that match.
972 (defun change-log-get-method-definition-1 (end)
973 (setq change-log-get-method-definition-md
974 (concat change-log-get-method-definition-md
977 (goto-char (match-end 0)))
979 (defun change-log-get-method-definition ()
980 "For Objective C, return the method name if we are in a method."
981 (let ((change-log-get-method-definition-md "["))
983 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
984 (change-log-get-method-definition-1 " ")))
987 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
988 (change-log-get-method-definition-1 "")
989 (while (not (looking-at "[{;]"))
991 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
992 (change-log-get-method-definition-1 ""))
993 (concat change-log-get-method-definition-md
"]"))))))
995 (defun change-log-sortable-date-at ()
996 "Return date of log entry in a consistent form for sorting.
997 Point is assumed to be at the start of the entry."
999 (if (looking-at change-log-start-entry-re
)
1000 (let ((date (match-string-no-properties 0)))
1002 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date
)
1003 (concat (match-string 1 date
) (match-string 2 date
)
1004 (match-string 3 date
))
1006 (timezone-make-date-sortable date
)
1008 (error "Bad date")))
1010 (defun change-log-resolve-conflict ()
1011 "Function to be used in `smerge-resolve-function'."
1014 (narrow-to-region (match-beginning 0) (match-end 0))
1015 (let ((mb1 (match-beginning 1))
1017 (mb3 (match-beginning 3))
1019 (tmp1 (generate-new-buffer " *changelog-resolve-1*"))
1020 (tmp2 (generate-new-buffer " *changelog-resolve-2*")))
1022 (let ((buf (current-buffer)))
1023 (with-current-buffer tmp1
1025 (insert-buffer-substring buf mb1 me1
))
1026 (with-current-buffer tmp2
1028 (insert-buffer-substring buf mb3 me3
)
1029 ;; Do the merge here instead of inside `buf' so as to be
1030 ;; more robust in case change-log-merge fails.
1031 (change-log-merge tmp1
))
1032 (goto-char (point-max))
1033 (delete-region (point-min)
1035 (insert-buffer-substring tmp2
))))
1037 (kill-buffer tmp2
))))))
1040 (defun change-log-merge (other-log)
1041 "Merge the contents of change log file OTHER-LOG with this buffer.
1042 Both must be found in Change Log mode (since the merging depends on
1043 the appropriate motion commands). OTHER-LOG can be either a file name
1046 Entries are inserted in chronological order. Both the current and
1047 old-style time formats for entries are supported."
1048 (interactive "*fLog file name to merge: ")
1049 (if (not (derived-mode-p 'change-log-mode
))
1050 (error "Not in Change Log mode"))
1051 (let ((other-buf (if (bufferp other-log
) other-log
1052 (find-file-noselect other-log
)))
1053 (buf (current-buffer))
1056 (goto-char (point-min))
1057 (set-buffer other-buf
)
1058 (goto-char (point-min))
1059 (if (not (derived-mode-p 'change-log-mode
))
1060 (error "%s not found in Change Log mode" other-log
))
1061 ;; Loop through all the entries in OTHER-LOG.
1063 (setq date1
(change-log-sortable-date-at))
1065 end
(progn (forward-page) (point)))
1066 ;; Look for an entry in original buffer that isn't later.
1067 (with-current-buffer buf
1068 (while (and (not (eobp))
1069 (string< date1
(change-log-sortable-date-at)))
1072 (insert-buffer-substring other-buf start end
)
1073 ;; At the end of the original buffer, insert a newline to
1074 ;; separate entries and then the rest of the file being
1077 (and (= ?
\n (char-before))
1078 (or (<= (1- (point)) (point-min))
1079 (= ?
\n (char-before (1- (point)))))))
1080 (insert (if use-hard-newlines hard-newline
"\n")))
1081 ;; Move to the end of it to terminate outer loop.
1082 (with-current-buffer other-buf
1083 (goto-char (point-max)))
1084 (insert-buffer-substring other-buf start
)))))))
1086 (defun change-log-beginning-of-defun ()
1087 (re-search-backward change-log-start-entry-re nil
'move
))
1089 (defun change-log-end-of-defun ()
1090 ;; Look back and if there is no entry there it means we are before
1091 ;; the first ChangeLog entry, so go forward until finding one.
1092 (unless (save-excursion (re-search-backward change-log-start-entry-re nil t
))
1093 (re-search-forward change-log-start-entry-re nil t
))
1095 ;; In case we are at the end of log entry going forward a line will
1096 ;; make us find the next entry when searching. If we are inside of
1097 ;; an entry going forward a line will still keep the point inside
1101 ;; In case we are at the beginning of an entry, move past it.
1102 (when (looking-at change-log-start-entry-re
)
1103 (goto-char (match-end 0))
1106 ;; Search for the start of the next log entry. Go to the end of the
1107 ;; buffer if we could not find a next entry.
1108 (when (re-search-forward change-log-start-entry-re nil
'move
)
1109 (goto-char (match-beginning 0))
1114 ;; arch-tag: 81eee6fc-088f-4372-a37f-80ad9620e762
1115 ;;; add-log.el ends here