Add arch tagline
[emacs.git] / lisp / add-log.el
blobfc8224293ca9139347c5964faba7237e259ae164
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.
6 ;; Maintainer: FSF
7 ;; Keywords: tools
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/>.
24 ;;; Commentary:
26 ;; This facility is documented in the Emacs Manual.
28 ;; Todo:
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
33 ;; source file.
34 ;; - Don't add TAB indents (and username?) if inserting entries in those
35 ;; special places.
37 ;;; Code:
39 (eval-when-compile
40 (require 'timezone))
42 (defgroup change-log nil
43 "Change log maintenance."
44 :group 'tools
45 :link '(custom-manual "(emacs)Change Log")
46 :prefix "change-log-"
47 :prefix "add-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)
53 string)
54 :group 'change-log)
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'."
59 :type 'hook
60 :group 'change-log)
62 ;; Many modes set this variable, so avoid warnings.
63 ;;;###autoload
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)
69 :group 'change-log)
71 ;;;###autoload
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)
76 string)
77 :group 'change-log)
79 ;;;###autoload
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))
89 :group 'change-log)
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'"
99 current-time-string)
100 (function :tag "Other"))
101 :group 'change-log)
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 \
108 for that file made
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
111 original log:
113 * foo (...): ...
114 * bar (...): change 1
116 in the latter case, \\[add-change-log-entry-other-window] in a \
117 buffer visiting `bar', yields:
119 * bar (...): -!-
120 * foo (...): ...
121 * bar (...): change 1
123 and in the former:
125 * foo (...): ...
126 * bar (...): change 1
127 (...): -!-
129 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
130 this variable."
131 :version "20.3"
132 :type 'boolean
133 :group 'change-log)
135 (defcustom add-log-always-start-new-record nil
136 "If non-nil, `add-change-log-entry' will always start a new record."
137 :version "22.1"
138 :type 'boolean
139 :group 'change-log)
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)
146 :group 'change-log)
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)
154 :group 'change-log)
157 (defcustom change-log-version-info-enabled nil
158 "If non-nil, enable recording version numbers with the changes."
159 :version "21.1"
160 :type 'boolean
161 :group 'change-log)
163 (defcustom change-log-version-number-regexp-list
164 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
165 (list
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."
173 :version "21.1"
174 :type '(repeat regexp)
175 :group 'change-log)
177 (defface change-log-date
178 '((t (:inherit font-lock-string-face)))
179 "Face used to highlight dates in date lines."
180 :version "21.1"
181 :group 'change-log)
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."
188 :version "21.1"
189 :group 'change-log)
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."
196 :version "21.1"
197 :group 'change-log)
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."
204 :version "21.1"
205 :group 'change-log)
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."
212 :version "21.1"
213 :group 'change-log)
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 `[...]'."
220 :version "21.1"
221 :group 'change-log)
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 `<....>'."
228 :version "21.1"
229 :group 'change-log)
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."
236 :version "21.1"
237 :group 'change-log)
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
245 `(;;
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
255 (1 'change-log-name)
256 (2 'change-log-email)))
258 ;; File names.
259 (,change-log-file-names-re
260 (2 'change-log-file)
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]+)\\)"
271 (2 'change-log-list)
272 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
273 (1 'change-log-list)))
275 ;; Conditionals.
276 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals))
278 ;; Function of change.
279 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function))
281 ;; Acknowledgements.
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."
293 (save-excursion
294 (goto-char where)
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
298 ;; name.
299 (progn
300 (re-search-forward change-log-file-names-re nil t)
301 (match-string 2))
302 (if (looking-at change-log-file-names-re)
303 ;; We found a file name.
304 (match-string 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
308 "\\)\\|\\("
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
312 ;; file name.
313 (progn
314 (re-search-forward change-log-file-names-re nil t)
315 (match-string 2))
316 (match-string 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 2))))))
321 (defun change-log-find-file ()
322 "Visit the file for the change under point."
323 (interactive)
324 (let ((file (change-log-search-file-name (point))))
325 (if (and file (file-exists-p file))
326 (find-file file)
327 (message "No such file or directory: %s" file))))
329 (defvar change-log-mode-map
330 (let ((map (make-sparse-keymap)))
331 (define-key map [?\C-c ?\C-p] 'add-log-edit-prev-comment)
332 (define-key map [?\C-c ?\C-n] 'add-log-edit-next-comment)
333 (define-key map [?\C-c ?\C-f] 'change-log-find-file)
334 map)
335 "Keymap for Change Log major mode.")
337 ;; It used to be called change-log-time-zone-rule but really should be
338 ;; called add-log-time-zone-rule since it's only used from add-log-* code.
339 (defvaralias 'change-log-time-zone-rule 'add-log-time-zone-rule)
340 (defvar add-log-time-zone-rule nil
341 "Time zone used for calculating change log time stamps.
342 It takes the same format as the TZ argument of `set-time-zone-rule'.
343 If nil, use local time.
344 If t, use universal time.")
345 (put 'add-log-time-zone-rule 'safe-local-variable
346 '(lambda (x) (or (booleanp x) (stringp x))))
348 (defun add-log-iso8601-time-zone (&optional time)
349 (let* ((utc-offset (or (car (current-time-zone time)) 0))
350 (sign (if (< utc-offset 0) ?- ?+))
351 (sec (abs utc-offset))
352 (ss (% sec 60))
353 (min (/ sec 60))
354 (mm (% min 60))
355 (hh (/ min 60)))
356 (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
357 ((not (zerop mm)) "%c%02d:%02d")
358 (t "%c%02d"))
359 sign hh mm ss)))
361 (defvar add-log-iso8601-with-time-zone nil)
363 (defun add-log-iso8601-time-string ()
364 (let ((time (format-time-string "%Y-%m-%d"
365 nil (eq t add-log-time-zone-rule))))
366 (if add-log-iso8601-with-time-zone
367 (concat time " " (add-log-iso8601-time-zone))
368 time)))
370 (defun change-log-name ()
371 "Return (system-dependent) default name for a change log file."
372 (or change-log-default-name
373 (if (eq system-type 'vax-vms)
374 "$CHANGE_LOG$.TXT"
375 "ChangeLog")))
377 (defun add-log-edit-prev-comment (arg)
378 "Cycle backward through Log-Edit mode comment history.
379 With a numeric prefix ARG, go back ARG comments."
380 (interactive "*p")
381 (save-restriction
382 (narrow-to-region (point)
383 (if (memq last-command '(add-log-edit-prev-comment
384 add-log-edit-next-comment))
385 (mark) (point)))
386 (when (fboundp 'log-edit-previous-comment)
387 (log-edit-previous-comment arg)
388 (indent-region (point-min) (point-max))
389 (goto-char (point-min))
390 (unless (save-restriction (widen) (bolp))
391 (delete-region (point) (progn (skip-chars-forward " \t\n") (point))))
392 (set-mark (point-min))
393 (goto-char (point-max))
394 (delete-region (point) (progn (skip-chars-backward " \t\n") (point))))))
396 (defun add-log-edit-next-comment (arg)
397 "Cycle forward through Log-Edit mode comment history.
398 With a numeric prefix ARG, go back ARG comments."
399 (interactive "*p")
400 (add-log-edit-prev-comment (- arg)))
402 ;;;###autoload
403 (defun prompt-for-change-log-name ()
404 "Prompt for a change log name."
405 (let* ((default (change-log-name))
406 (name (expand-file-name
407 (read-file-name (format "Log file (default %s): " default)
408 nil default))))
409 ;; Handle something that is syntactically a directory name.
410 ;; Look for ChangeLog or whatever in that directory.
411 (if (string= (file-name-nondirectory name) "")
412 (expand-file-name (file-name-nondirectory default)
413 name)
414 ;; Handle specifying a file that is a directory.
415 (if (file-directory-p name)
416 (expand-file-name (file-name-nondirectory default)
417 (file-name-as-directory name))
418 name))))
420 (defun change-log-version-number-search ()
421 "Return version number of current buffer's file.
422 This is the value returned by `vc-working-revision' or, if that is
423 nil, by matching `change-log-version-number-regexp-list'."
424 (let* ((size (buffer-size))
425 (limit
426 ;; The version number can be anywhere in the file, but
427 ;; restrict search to the file beginning: 10% should be
428 ;; enough to prevent some mishits.
430 ;; Apply percentage only if buffer size is bigger than
431 ;; approx 100 lines.
432 (if (> size (* 100 80)) (+ (point) (/ size 10)))))
433 (or (and buffer-file-name (vc-working-revision buffer-file-name))
434 (save-restriction
435 (widen)
436 (let ((regexps change-log-version-number-regexp-list)
437 version)
438 (while regexps
439 (save-excursion
440 (goto-char (point-min))
441 (when (re-search-forward (pop regexps) limit t)
442 (setq version (match-string 1)
443 regexps nil))))
444 version)))))
447 ;;;###autoload
448 (defun find-change-log (&optional file-name buffer-file)
449 "Find a change log file for \\[add-change-log-entry] and return the name.
451 Optional arg FILE-NAME specifies the file to use.
452 If FILE-NAME is nil, use the value of `change-log-default-name'.
453 If `change-log-default-name' is nil, behave as though it were 'ChangeLog'
454 \(or whatever we use on this operating system).
456 If `change-log-default-name' contains a leading directory component, then
457 simply find it in the current directory. Otherwise, search in the current
458 directory and its successive parents for a file so named.
460 Once a file is found, `change-log-default-name' is set locally in the
461 current buffer to the complete file name.
462 Optional arg BUFFER-FILE overrides `buffer-file-name'."
463 ;; If user specified a file name or if this buffer knows which one to use,
464 ;; just use that.
465 (or file-name
466 (setq file-name (and change-log-default-name
467 (file-name-directory change-log-default-name)
468 change-log-default-name))
469 (progn
470 ;; Chase links in the source file
471 ;; and use the change log in the dir where it points.
472 (setq file-name (or (and (or buffer-file buffer-file-name)
473 (file-name-directory
474 (file-chase-links
475 (or buffer-file buffer-file-name))))
476 default-directory))
477 (if (file-directory-p file-name)
478 (setq file-name (expand-file-name (change-log-name) file-name)))
479 ;; Chase links before visiting the file.
480 ;; This makes it easier to use a single change log file
481 ;; for several related directories.
482 (setq file-name (file-chase-links file-name))
483 (setq file-name (expand-file-name file-name))
484 ;; Move up in the dir hierarchy till we find a change log file.
485 (let ((file1 file-name)
486 parent-dir)
487 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
488 (progn (setq parent-dir
489 (file-name-directory
490 (directory-file-name
491 (file-name-directory file1))))
492 ;; Give up if we are already at the root dir.
493 (not (string= (file-name-directory file1)
494 parent-dir))))
495 ;; Move up to the parent dir and try again.
496 (setq file1 (expand-file-name
497 (file-name-nondirectory (change-log-name))
498 parent-dir)))
499 ;; If we found a change log in a parent, use that.
500 (if (or (get-file-buffer file1) (file-exists-p file1))
501 (setq file-name file1)))))
502 ;; Make a local variable in this buffer so we needn't search again.
503 (set (make-local-variable 'change-log-default-name) file-name)
504 file-name)
506 (defun add-log-file-name (buffer-file log-file)
507 ;; Never want to add a change log entry for the ChangeLog file itself.
508 (unless (or (null buffer-file) (string= buffer-file log-file))
509 (if add-log-file-name-function
510 (funcall add-log-file-name-function buffer-file)
511 (setq buffer-file
512 (file-relative-name buffer-file (file-name-directory log-file)))
513 ;; If we have a backup file, it's presumably because we're
514 ;; comparing old and new versions (e.g. for deleted
515 ;; functions) and we'll want to use the original name.
516 (if (backup-file-name-p buffer-file)
517 (file-name-sans-versions buffer-file)
518 buffer-file))))
520 ;;;###autoload
521 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
522 put-new-entry-on-new-line)
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 Fifth arg PUT-NEW-ENTRY-ON-NEW-LINE non-nil means that if a new
537 entry is created, put it on a new line by itself, do not put it
538 after a comma on an existing line.
540 Option `add-log-always-start-new-record' non-nil means always create a
541 new record, even when the last record was made on the same date and by
542 the same person.
544 The change log file can start with a copyright notice and a copying
545 permission notice. The first blank line indicates the end of these
546 notices.
548 Today's date is calculated according to `add-log-time-zone-rule' if
549 non-nil, otherwise in local time."
550 (interactive (list current-prefix-arg
551 (prompt-for-change-log-name)))
552 (let* ((defun (add-log-current-defun))
553 (version (and change-log-version-info-enabled
554 (change-log-version-number-search)))
555 (buf-file-name (if add-log-buffer-file-name-function
556 (funcall add-log-buffer-file-name-function)
557 buffer-file-name))
558 (buffer-file (if buf-file-name (expand-file-name buf-file-name)))
559 (file-name (expand-file-name (find-change-log file-name buffer-file)))
560 ;; Set ITEM to the file name to use in the new item.
561 (item (add-log-file-name buffer-file file-name)))
563 (unless (equal file-name buffer-file-name)
564 (if (or other-window (window-dedicated-p (selected-window)))
565 (find-file-other-window file-name)
566 (find-file file-name)))
567 (or (derived-mode-p 'change-log-mode)
568 (change-log-mode))
569 (undo-boundary)
570 (goto-char (point-min))
572 (let ((full-name (or add-log-full-name (user-full-name)))
573 (mailing-address (or add-log-mailing-address user-mail-address)))
575 (when whoami
576 (setq full-name (read-string "Full name: " full-name))
577 ;; Note that some sites have room and phone number fields in
578 ;; full name which look silly when inserted. Rather than do
579 ;; anything about that here, let user give prefix argument so that
580 ;; s/he can edit the full name field in prompter if s/he wants.
581 (setq mailing-address
582 (read-string "Mailing address: " mailing-address)))
584 ;; If file starts with a copyright and permission notice, skip them.
585 ;; Assume they end at first blank line.
586 (when (looking-at "Copyright")
587 (search-forward "\n\n")
588 (skip-chars-forward "\n"))
590 ;; Advance into first entry if it is usable; else make new one.
591 (let ((new-entries
592 (mapcar (lambda (addr)
593 (concat
594 (if (stringp add-log-time-zone-rule)
595 (let ((tz (getenv "TZ")))
596 (unwind-protect
597 (progn
598 (set-time-zone-rule add-log-time-zone-rule)
599 (funcall add-log-time-format))
600 (set-time-zone-rule tz)))
601 (funcall add-log-time-format))
602 " " full-name
603 " <" addr ">"))
604 (if (consp mailing-address)
605 mailing-address
606 (list mailing-address)))))
607 (if (and (not add-log-always-start-new-record)
608 (let ((hit nil))
609 (dolist (entry new-entries hit)
610 (when (looking-at (regexp-quote entry))
611 (setq hit t)))))
612 (forward-line 1)
613 (insert (nth (random (length new-entries))
614 new-entries)
615 (if use-hard-newlines hard-newline "\n")
616 (if use-hard-newlines hard-newline "\n"))
617 (forward-line -1))))
619 ;; Determine where we should stop searching for a usable
620 ;; item to add to, within this entry.
621 (let ((bound
622 (save-excursion
623 (if (looking-at "\n*[^\n* \t]")
624 (skip-chars-forward "\n")
625 (if add-log-keep-changes-together
626 (forward-page) ; page delimits entries for date
627 (forward-paragraph))) ; paragraph delimits entries for file
628 (point))))
630 ;; Now insert the new line for this item.
631 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t)
632 ;; Put this file name into the existing empty item.
633 (if item
634 (insert item)))
635 ((and (not new-entry)
636 (let (case-fold-search)
637 (re-search-forward
638 (concat (regexp-quote (concat "* " item))
639 ;; Don't accept `foo.bar' when
640 ;; looking for `foo':
641 "\\(\\s \\|[(),:]\\)")
642 bound t)))
643 ;; Add to the existing item for the same file.
644 (re-search-forward "^\\s *$\\|^\\s \\*")
645 (goto-char (match-beginning 0))
646 ;; Delete excess empty lines; make just 2.
647 (while (and (not (eobp)) (looking-at "^\\s *$"))
648 (delete-region (point) (line-beginning-position 2)))
649 (insert (if use-hard-newlines hard-newline "\n")
650 (if use-hard-newlines hard-newline "\n"))
651 (forward-line -2)
652 (indent-relative-maybe))
654 ;; Make a new item.
655 (while (looking-at "\\sW")
656 (forward-line 1))
657 (while (and (not (eobp)) (looking-at "^\\s *$"))
658 (delete-region (point) (line-beginning-position 2)))
659 (insert (if use-hard-newlines hard-newline "\n")
660 (if use-hard-newlines hard-newline "\n")
661 (if use-hard-newlines hard-newline "\n"))
662 (forward-line -2)
663 (indent-to left-margin)
664 (insert "* ")
665 (if item (insert item)))))
666 ;; Now insert the function name, if we have one.
667 ;; Point is at the item for this file,
668 ;; either at the end of the line or at the first blank line.
669 (if (not defun)
670 ;; No function name, so put in a colon unless we have just a star.
671 (unless (save-excursion
672 (beginning-of-line 1)
673 (looking-at "\\s *\\(\\*\\s *\\)?$"))
674 (insert ": ")
675 (if version (insert version ?\s)))
676 ;; Make it easy to get rid of the function name.
677 (undo-boundary)
678 (unless (save-excursion
679 (beginning-of-line 1)
680 (looking-at "\\s *$"))
681 (insert ?\s))
682 ;; See if the prev function name has a message yet or not.
683 ;; If not, merge the two items.
684 (let ((pos (point-marker)))
685 (skip-syntax-backward " ")
686 (skip-chars-backward "):")
687 (if (and (not put-new-entry-on-new-line)
688 (looking-at "):")
689 (let ((pos (save-excursion (backward-sexp 1) (point))))
690 (when (equal (buffer-substring pos (point)) defun)
691 (delete-region pos (point)))
692 (> fill-column (+ (current-column) (length defun) 4))))
693 (progn (skip-chars-backward ", ")
694 (delete-region (point) pos)
695 (unless (memq (char-before) '(?\()) (insert ", ")))
696 (when (and (not put-new-entry-on-new-line) (looking-at "):"))
697 (delete-region (+ 1 (point)) (line-end-position)))
698 (goto-char pos)
699 (insert "("))
700 (set-marker pos nil))
701 (insert defun "): ")
702 (if version (insert version ?\s)))))
704 ;;;###autoload
705 (defun add-change-log-entry-other-window (&optional whoami file-name)
706 "Find change log file in other window and add entry and item.
707 This is just like `add-change-log-entry' except that it displays
708 the change log file in another window."
709 (interactive (if current-prefix-arg
710 (list current-prefix-arg
711 (prompt-for-change-log-name))))
712 (add-change-log-entry whoami file-name t))
715 (defvar change-log-indent-text 0)
717 (defun change-log-fill-parenthesized-list ()
718 ;; Fill parenthesized lists of names according to GNU standards.
719 ;; * file-name.ext (very-long-foo, very-long-bar, very-long-foobar):
720 ;; should be filled as
721 ;; * file-name.ext (very-long-foo, very-long-bar)
722 ;; (very-long-foobar):
723 (save-excursion
724 (end-of-line 0)
725 (skip-chars-backward " \t")
726 (when (and (equal (char-before) ?\,)
727 (> (point) (1+ (point-min))))
728 (condition-case nil
729 (when (save-excursion
730 (and (prog2
731 (up-list -1)
732 (equal (char-after) ?\()
733 (skip-chars-backward " \t"))
734 (or (bolp)
735 ;; Skip everything but a whitespace or asterisk.
736 (and (not (zerop (skip-chars-backward "^ \t\n*")))
737 (skip-chars-backward " \t")
738 ;; We want one asterisk here.
739 (= (skip-chars-backward "*") -1)
740 (skip-chars-backward " \t")
741 (bolp)))))
742 ;; Delete the comma.
743 (delete-char -1)
744 ;; Close list on previous line.
745 (insert ")")
746 (skip-chars-forward " \t\n")
747 ;; Start list on new line.
748 (insert-before-markers "("))
749 (error nil)))))
751 (defun change-log-indent ()
752 (change-log-fill-parenthesized-list)
753 (let* ((indent
754 (save-excursion
755 (beginning-of-line)
756 (skip-chars-forward " \t")
757 (cond
758 ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
759 ;; Matching the output of add-log-time-format is difficult,
760 ;; but I'll get it has at least two adjacent digits.
761 (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
763 ((looking-at "[^*(]")
764 (+ (current-left-margin) change-log-indent-text))
765 (t (current-left-margin)))))
766 (pos (save-excursion (indent-line-to indent) (point))))
767 (if (> pos (point)) (goto-char pos))))
770 (defvar smerge-resolve-function)
771 (defvar copyright-at-end-flag)
773 ;;;###autoload
774 (define-derived-mode change-log-mode text-mode "Change Log"
775 "Major mode for editing change logs; like Indented Text Mode.
776 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
777 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
778 Each entry behaves as a paragraph, and the entries for one day as a page.
779 Runs `change-log-mode-hook'.
780 \\{change-log-mode-map}"
781 (setq left-margin 8
782 fill-column 74
783 indent-tabs-mode t
784 tab-width 8
785 show-trailing-whitespace t)
786 (set (make-local-variable 'fill-paragraph-function)
787 'change-log-fill-paragraph)
788 ;; Avoid that filling leaves behind a single "*" on a line.
789 (add-hook 'fill-nobreak-predicate
790 '(lambda ()
791 (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
792 nil t)
793 (set (make-local-variable 'indent-line-function) 'change-log-indent)
794 (set (make-local-variable 'tab-always-indent) nil)
795 (set (make-local-variable 'copyright-at-end-flag) t)
796 ;; We really do want "^" in paragraph-start below: it is only the
797 ;; lines that begin at column 0 (despite the left-margin of 8) that
798 ;; we are looking for. Adding `* ' allows eliding the blank line
799 ;; between entries for different files.
800 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
801 (set (make-local-variable 'paragraph-separate) paragraph-start)
802 ;; Match null string on the date-line so that the date-line
803 ;; is grouped with what follows.
804 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
805 (set (make-local-variable 'version-control) 'never)
806 (set (make-local-variable 'smerge-resolve-function)
807 'change-log-resolve-conflict)
808 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
809 (set (make-local-variable 'font-lock-defaults)
810 '(change-log-font-lock-keywords t nil nil backward-paragraph))
811 (set (make-local-variable 'isearch-buffers-next-buffer-function)
812 'change-log-next-buffer)
813 (set (make-local-variable 'beginning-of-defun-function)
814 'change-log-beginning-of-defun)
815 (set (make-local-variable 'end-of-defun-function)
816 'change-log-end-of-defun)
817 (isearch-buffers-minor-mode))
819 (defun change-log-next-buffer (&optional buffer wrap)
820 "Return the next buffer in the series of ChangeLog file buffers.
821 This function is used for multiple buffers isearch.
822 A sequence of buffers is formed by ChangeLog files with decreasing
823 numeric file name suffixes in the directory of the initial ChangeLog
824 file were isearch was started."
825 (let* ((name (change-log-name))
826 (files (cons name (sort (file-expand-wildcards
827 (concat name "[-.][0-9]*"))
828 (lambda (a b)
829 ;; The file's extension may not have a valid
830 ;; version form (e.g. VC backup revisions).
831 (ignore-errors
832 (version< (substring b (length name))
833 (substring a (length name))))))))
834 (files (if isearch-forward files (reverse files))))
835 (find-file-noselect
836 (if wrap
837 (car files)
838 (cadr (member (file-name-nondirectory (buffer-file-name buffer))
839 files))))))
841 ;; It might be nice to have a general feature to replace this. The idea I
842 ;; have is a variable giving a regexp matching text which should not be
843 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
844 ;; But I don't feel up to implementing that today.
845 (defun change-log-fill-paragraph (&optional justify)
846 "Fill the paragraph, but preserve open parentheses at beginning of lines.
847 Prefix arg means justify as well."
848 (interactive "P")
849 (let ((end (progn (forward-paragraph) (point)))
850 (beg (progn (backward-paragraph) (point)))
851 ;; Add lines starting with whitespace followed by a left paren or an
852 ;; asterisk.
853 (paragraph-start (concat paragraph-start "\\|\\s *\\(?:\\s(\\|\\*\\)"))
854 ;; Make sure we call `change-log-indent'.
855 (fill-indent-according-to-mode t))
856 (fill-region beg end justify)
859 (defcustom add-log-current-defun-header-regexp
860 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
861 "Heuristic regexp used by `add-log-current-defun' for unknown major modes."
862 :type 'regexp
863 :group 'change-log)
865 ;;;###autoload
866 (defvar add-log-lisp-like-modes
867 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode)
868 "*Modes that look like Lisp to `add-log-current-defun'.")
870 ;;;###autoload
871 (defvar add-log-c-like-modes
872 '(c-mode c++-mode c++-c-mode objc-mode)
873 "*Modes that look like C to `add-log-current-defun'.")
875 ;;;###autoload
876 (defvar add-log-tex-like-modes
877 '(TeX-mode plain-TeX-mode LaTeX-mode tex-mode)
878 "*Modes that look like TeX to `add-log-current-defun'.")
880 (declare-function c-cpp-define-name "cc-cmds" ())
881 (declare-function c-defun-name "cc-cmds" ())
883 ;;;###autoload
884 (defun add-log-current-defun ()
885 "Return name of function definition point is in, or nil.
887 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
888 Texinfo (@node titles) and Perl.
890 Other modes are handled by a heuristic that looks in the 10K before
891 point for uppercase headings starting in the first column or
892 identifiers followed by `:' or `='. See variables
893 `add-log-current-defun-header-regexp' and
894 `add-log-current-defun-function'.
896 Has a preference of looking backwards."
897 (condition-case nil
898 (save-excursion
899 (let ((location (point)))
900 (cond (add-log-current-defun-function
901 (funcall add-log-current-defun-function))
902 ((apply 'derived-mode-p add-log-lisp-like-modes)
903 ;; If we are now precisely at the beginning of a defun,
904 ;; make sure beginning-of-defun finds that one
905 ;; rather than the previous one.
906 (or (eobp) (forward-char 1))
907 (beginning-of-defun)
908 ;; Make sure we are really inside the defun found,
909 ;; not after it.
910 (when (and (looking-at "\\s(")
911 (progn (end-of-defun)
912 (< location (point)))
913 (progn (forward-sexp -1)
914 (>= location (point))))
915 (if (looking-at "\\s(")
916 (forward-char 1))
917 ;; Skip the defining construct name, typically "defun"
918 ;; or "defvar".
919 (forward-sexp 1)
920 ;; The second element is usually a symbol being defined.
921 ;; If it is not, use the first symbol in it.
922 (skip-chars-forward " \t\n'(")
923 (buffer-substring-no-properties (point)
924 (progn (forward-sexp 1)
925 (point)))))
926 ((apply 'derived-mode-p add-log-c-like-modes)
927 (or (c-cpp-define-name)
928 (c-defun-name)))
929 ((memq major-mode add-log-tex-like-modes)
930 (if (re-search-backward
931 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
932 nil t)
933 (progn
934 (goto-char (match-beginning 0))
935 (buffer-substring-no-properties
936 (1+ (point)) ; without initial backslash
937 (line-end-position)))))
938 ((derived-mode-p 'texinfo-mode)
939 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
940 (match-string-no-properties 1)))
941 ((derived-mode-p 'perl-mode 'cperl-mode)
942 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t)
943 (match-string-no-properties 1)))
944 ;; Emacs's autoconf-mode installs its own
945 ;; `add-log-current-defun-function'. This applies to
946 ;; a different mode apparently for editing .m4
947 ;; autoconf source.
948 ((derived-mode-p 'autoconf-mode)
949 (if (re-search-backward
950 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t)
951 (match-string-no-properties 3)))
953 ;; If all else fails, try heuristics
954 (let (case-fold-search
955 result)
956 (end-of-line)
957 (when (re-search-backward
958 add-log-current-defun-header-regexp
959 (- (point) 10000)
961 (setq result (or (match-string-no-properties 1)
962 (match-string-no-properties 0)))
963 ;; Strip whitespace away
964 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
965 result)
966 (setq result (match-string-no-properties 1 result)))
967 result))))))
968 (error nil)))
970 (defvar change-log-get-method-definition-md)
972 ;; Subroutine used within change-log-get-method-definition.
973 ;; Add the last match in the buffer to the end of `md',
974 ;; followed by the string END; move to the end of that match.
975 (defun change-log-get-method-definition-1 (end)
976 (setq change-log-get-method-definition-md
977 (concat change-log-get-method-definition-md
978 (match-string 1)
979 end))
980 (goto-char (match-end 0)))
982 (defun change-log-get-method-definition ()
983 "For Objective C, return the method name if we are in a method."
984 (let ((change-log-get-method-definition-md "["))
985 (save-excursion
986 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
987 (change-log-get-method-definition-1 " ")))
988 (save-excursion
989 (cond
990 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
991 (change-log-get-method-definition-1 "")
992 (while (not (looking-at "[{;]"))
993 (looking-at
994 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
995 (change-log-get-method-definition-1 ""))
996 (concat change-log-get-method-definition-md "]"))))))
998 (defun change-log-sortable-date-at ()
999 "Return date of log entry in a consistent form for sorting.
1000 Point is assumed to be at the start of the entry."
1001 (require 'timezone)
1002 (if (looking-at change-log-start-entry-re)
1003 (let ((date (match-string-no-properties 0)))
1004 (if date
1005 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
1006 (concat (match-string 1 date) (match-string 2 date)
1007 (match-string 3 date))
1008 (condition-case nil
1009 (timezone-make-date-sortable date)
1010 (error nil)))))
1011 (error "Bad date")))
1013 (defun change-log-resolve-conflict ()
1014 "Function to be used in `smerge-resolve-function'."
1015 (save-excursion
1016 (save-restriction
1017 (narrow-to-region (match-beginning 0) (match-end 0))
1018 (let ((mb1 (match-beginning 1))
1019 (me1 (match-end 1))
1020 (mb3 (match-beginning 3))
1021 (me3 (match-end 3))
1022 (tmp1 (generate-new-buffer " *changelog-resolve-1*"))
1023 (tmp2 (generate-new-buffer " *changelog-resolve-2*")))
1024 (unwind-protect
1025 (let ((buf (current-buffer)))
1026 (with-current-buffer tmp1
1027 (change-log-mode)
1028 (insert-buffer-substring buf mb1 me1))
1029 (with-current-buffer tmp2
1030 (change-log-mode)
1031 (insert-buffer-substring buf mb3 me3)
1032 ;; Do the merge here instead of inside `buf' so as to be
1033 ;; more robust in case change-log-merge fails.
1034 (change-log-merge tmp1))
1035 (goto-char (point-max))
1036 (delete-region (point-min)
1037 (prog1 (point)
1038 (insert-buffer-substring tmp2))))
1039 (kill-buffer tmp1)
1040 (kill-buffer tmp2))))))
1042 ;;;###autoload
1043 (defun change-log-merge (other-log)
1044 "Merge the contents of change log file OTHER-LOG with this buffer.
1045 Both must be found in Change Log mode (since the merging depends on
1046 the appropriate motion commands). OTHER-LOG can be either a file name
1047 or a buffer.
1049 Entries are inserted in chronological order. Both the current and
1050 old-style time formats for entries are supported."
1051 (interactive "*fLog file name to merge: ")
1052 (if (not (derived-mode-p 'change-log-mode))
1053 (error "Not in Change Log mode"))
1054 (let ((other-buf (if (bufferp other-log) other-log
1055 (find-file-noselect other-log)))
1056 (buf (current-buffer))
1057 date1 start end)
1058 (save-excursion
1059 (goto-char (point-min))
1060 (set-buffer other-buf)
1061 (goto-char (point-min))
1062 (if (not (derived-mode-p 'change-log-mode))
1063 (error "%s not found in Change Log mode" other-log))
1064 ;; Loop through all the entries in OTHER-LOG.
1065 (while (not (eobp))
1066 (setq date1 (change-log-sortable-date-at))
1067 (setq start (point)
1068 end (progn (forward-page) (point)))
1069 ;; Look for an entry in original buffer that isn't later.
1070 (with-current-buffer buf
1071 (while (and (not (eobp))
1072 (string< date1 (change-log-sortable-date-at)))
1073 (forward-page))
1074 (if (not (eobp))
1075 (insert-buffer-substring other-buf start end)
1076 ;; At the end of the original buffer, insert a newline to
1077 ;; separate entries and then the rest of the file being
1078 ;; merged.
1079 (unless (or (bobp)
1080 (and (= ?\n (char-before))
1081 (or (<= (1- (point)) (point-min))
1082 (= ?\n (char-before (1- (point)))))))
1083 (insert (if use-hard-newlines hard-newline "\n")))
1084 ;; Move to the end of it to terminate outer loop.
1085 (with-current-buffer other-buf
1086 (goto-char (point-max)))
1087 (insert-buffer-substring other-buf start)))))))
1089 (defun change-log-beginning-of-defun ()
1090 (re-search-backward change-log-start-entry-re nil 'move))
1092 (defun change-log-end-of-defun ()
1093 ;; Look back and if there is no entry there it means we are before
1094 ;; the first ChangeLog entry, so go forward until finding one.
1095 (unless (save-excursion (re-search-backward change-log-start-entry-re nil t))
1096 (re-search-forward change-log-start-entry-re nil t))
1098 ;; In case we are at the end of log entry going forward a line will
1099 ;; make us find the next entry when searching. If we are inside of
1100 ;; an entry going forward a line will still keep the point inside
1101 ;; the same entry.
1102 (forward-line 1)
1104 ;; In case we are at the beginning of an entry, move past it.
1105 (when (looking-at change-log-start-entry-re)
1106 (goto-char (match-end 0))
1107 (forward-line 1))
1109 ;; Search for the start of the next log entry. Go to the end of the
1110 ;; buffer if we could not find a next entry.
1111 (when (re-search-forward change-log-start-entry-re nil 'move)
1112 (goto-char (match-beginning 0))
1113 (forward-line -1)))
1115 (provide 'add-log)
1117 ;; arch-tag: 81eee6fc-088f-4372-a37f-80ad9620e762
1118 ;;; add-log.el ends here