(top-level): Require 'cl when compiling.
[bpt/emacs.git] / lisp / add-log.el
1 ;;; add-log.el --- change log maintenance commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1988, 1993, 1994, 1997, 1998, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: tools
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; This facility is documented in the Emacs Manual.
29
30 ;; Todo:
31
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
35 ;; source file.
36 ;; - Don't add TAB indents (and username?) if inserting entries in those
37 ;; special places.
38
39 ;;; Code:
40
41 (eval-when-compile
42 (require 'cl) ; ignore-errors
43 (require 'timezone))
44
45 (defgroup change-log nil
46 "Change log maintenance."
47 :group 'tools
48 :link '(custom-manual "(emacs)Change Log")
49 :prefix "change-log-"
50 :prefix "add-log-")
51
52
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)
56 string)
57 :group 'change-log)
58 (put 'change-log-default-name 'safe-local-variable 'string-or-null-p)
59
60 (defcustom change-log-mode-hook nil
61 "Normal hook run by `change-log-mode'."
62 :type 'hook
63 :group 'change-log)
64
65 ;; Many modes set this variable, so avoid warnings.
66 ;;;###autoload
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)
72 :group 'change-log)
73
74 ;;;###autoload
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)
79 string)
80 :group 'change-log)
81
82 ;;;###autoload
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))
92 :group 'change-log)
93
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'"
102 current-time-string)
103 (function :tag "Other"))
104 :group 'change-log)
105
106 (defcustom add-log-keep-changes-together nil
107 "If non-nil, normally keep day's log entries for one file together.
108
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 \
111 for that file made
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
114 original log:
115
116 * foo (...): ...
117 * bar (...): change 1
118
119 in the latter case, \\[add-change-log-entry-other-window] in a \
120 buffer visiting `bar', yields:
121
122 * bar (...): -!-
123 * foo (...): ...
124 * bar (...): change 1
125
126 and in the former:
127
128 * foo (...): ...
129 * bar (...): change 1
130 (...): -!-
131
132 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
133 this variable."
134 :version "20.3"
135 :type 'boolean
136 :group 'change-log)
137
138 (defcustom add-log-always-start-new-record nil
139 "If non-nil, `add-change-log-entry' will always start a new record."
140 :version "22.1"
141 :type 'boolean
142 :group 'change-log)
143
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)
149 :group 'change-log)
150
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)
157 :group 'change-log)
158
159
160 (defcustom change-log-version-info-enabled nil
161 "If non-nil, enable recording version numbers with the changes."
162 :version "21.1"
163 :type 'boolean
164 :group 'change-log)
165
166 (defcustom change-log-version-number-regexp-list
167 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
168 (list
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."
176 :version "21.1"
177 :type '(repeat regexp)
178 :group 'change-log)
179
180 (defface change-log-date
181 '((t (:inherit font-lock-string-face)))
182 "Face used to highlight dates in date lines."
183 :version "21.1"
184 :group 'change-log)
185 ;; backward-compatibility alias
186 (put 'change-log-date-face 'face-alias 'change-log-date)
187
188 (defface change-log-name
189 '((t (:inherit font-lock-constant-face)))
190 "Face for highlighting author names."
191 :version "21.1"
192 :group 'change-log)
193 ;; backward-compatibility alias
194 (put 'change-log-name-face 'face-alias 'change-log-name)
195
196 (defface change-log-email
197 '((t (:inherit font-lock-variable-name-face)))
198 "Face for highlighting author email addresses."
199 :version "21.1"
200 :group 'change-log)
201 ;; backward-compatibility alias
202 (put 'change-log-email-face 'face-alias 'change-log-email)
203
204 (defface change-log-file
205 '((t (:inherit font-lock-function-name-face)))
206 "Face for highlighting file names."
207 :version "21.1"
208 :group 'change-log)
209 ;; backward-compatibility alias
210 (put 'change-log-file-face 'face-alias 'change-log-file)
211
212 (defface change-log-list
213 '((t (:inherit font-lock-keyword-face)))
214 "Face for highlighting parenthesized lists of functions or variables."
215 :version "21.1"
216 :group 'change-log)
217 ;; backward-compatibility alias
218 (put 'change-log-list-face 'face-alias 'change-log-list)
219
220 (defface change-log-conditionals
221 '((t (:inherit font-lock-variable-name-face)))
222 "Face for highlighting conditionals of the form `[...]'."
223 :version "21.1"
224 :group 'change-log)
225 ;; backward-compatibility alias
226 (put 'change-log-conditionals-face 'face-alias 'change-log-conditionals)
227
228 (defface change-log-function
229 '((t (:inherit font-lock-variable-name-face)))
230 "Face for highlighting items of the form `<....>'."
231 :version "21.1"
232 :group 'change-log)
233 ;; backward-compatibility alias
234 (put 'change-log-function-face 'face-alias 'change-log-function)
235
236 (defface change-log-acknowledgement
237 '((t (:inherit font-lock-comment-face)))
238 "Face for highlighting acknowledgments."
239 :version "21.1"
240 :group 'change-log)
241 ;; backward-compatibility alias
242 (put 'change-log-acknowledgement-face 'face-alias 'change-log-acknowledgement)
243
244 (defconst change-log-file-names-re "^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)")
245 (defconst change-log-start-entry-re "^\\sw.........[0-9:+ ]*")
246
247 (defvar change-log-font-lock-keywords
248 `(;;
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
258 (1 'change-log-name)
259 (2 'change-log-email)))
260 ;;
261 ;; File names.
262 (,change-log-file-names-re
263 (2 'change-log-file)
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)))
271 ;;
272 ;; Function or variable names.
273 ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
274 (2 'change-log-list)
275 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
276 (1 'change-log-list)))
277 ;;
278 ;; Conditionals.
279 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals))
280 ;;
281 ;; Function of change.
282 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function))
283 ;;
284 ;; Acknowledgements.
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.")
293
294 (defun change-log-search-file-name (where)
295 "Return the file-name for the change under point."
296 (save-excursion
297 (goto-char where)
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
301 ;; name.
302 (progn
303 (re-search-forward change-log-file-names-re nil t)
304 (match-string 2))
305 (if (looking-at change-log-file-names-re)
306 ;; We found a file name.
307 (match-string 2)
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
311 "\\)\\|\\("
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
315 ;; file name.
316 (progn
317 (re-search-forward change-log-file-names-re nil t)
318 (match-string 2))
319 (match-string 4))
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))))))
323
324 (defun change-log-find-file ()
325 "Visit the file for the change under point."
326 (interactive)
327 (let ((file (change-log-search-file-name (point))))
328 (if (and file (file-exists-p file))
329 (find-file file)
330 (message "No such file or directory: %s" file))))
331
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)
337 map)
338 "Keymap for Change Log major mode.")
339
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))))
350
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))
355 (ss (% sec 60))
356 (min (/ sec 60))
357 (mm (% min 60))
358 (hh (/ min 60)))
359 (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
360 ((not (zerop mm)) "%c%02d:%02d")
361 (t "%c%02d"))
362 sign hh mm ss)))
363
364 (defvar add-log-iso8601-with-time-zone nil)
365
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))
371 time)))
372
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)
377 "$CHANGE_LOG$.TXT"
378 "ChangeLog")))
379
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."
383 (interactive "*p")
384 (save-restriction
385 (narrow-to-region (point)
386 (if (memq last-command '(add-log-edit-prev-comment
387 add-log-edit-next-comment))
388 (mark) (point)))
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))))))
398
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."
402 (interactive "*p")
403 (add-log-edit-prev-comment (- arg)))
404
405 ;;;###autoload
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)
411 nil 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)
416 name)
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))
421 name))))
422
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))
428 (limit
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.
432 ;;
433 ;; Apply percentage only if buffer size is bigger than
434 ;; approx 100 lines.
435 (if (> size (* 100 80)) (+ (point) (/ size 10)))))
436 (or (and buffer-file-name (vc-working-revision buffer-file-name))
437 (save-restriction
438 (widen)
439 (let ((regexps change-log-version-number-regexp-list)
440 version)
441 (while regexps
442 (save-excursion
443 (goto-char (point-min))
444 (when (re-search-forward (pop regexps) limit t)
445 (setq version (match-string 1)
446 regexps nil))))
447 version)))))
448
449
450 ;;;###autoload
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.
453
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).
458
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.
462
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,
467 ;; just use that.
468 (or file-name
469 (setq file-name (and change-log-default-name
470 (file-name-directory change-log-default-name)
471 change-log-default-name))
472 (progn
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)
476 (file-name-directory
477 (file-chase-links
478 (or buffer-file buffer-file-name))))
479 default-directory))
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)
489 parent-dir)
490 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
491 (progn (setq parent-dir
492 (file-name-directory
493 (directory-file-name
494 (file-name-directory file1))))
495 ;; Give up if we are already at the root dir.
496 (not (string= (file-name-directory file1)
497 parent-dir))))
498 ;; Move up to the parent dir and try again.
499 (setq file1 (expand-file-name
500 (file-name-nondirectory (change-log-name))
501 parent-dir)))
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)
507 file-name)
508
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)
514 (setq 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)
521 buffer-file))))
522
523 ;;;###autoload
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').
528
529 Second arg FILE-NAME is file name of the change log.
530 If nil, use the value of `change-log-default-name'.
531
532 Third arg OTHER-WINDOW non-nil means visit in other window.
533
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.
537
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
540 the same person.
541
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
544 notices.
545
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)
555 buffer-file-name))
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)))
560
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)
566 (change-log-mode))
567 (undo-boundary)
568 (goto-char (point-min))
569
570 (let ((full-name (or add-log-full-name (user-full-name)))
571 (mailing-address (or add-log-mailing-address user-mail-address)))
572
573 (when whoami
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)))
581
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"))
587
588 ;; Advance into first entry if it is usable; else make new one.
589 (let ((new-entries
590 (mapcar (lambda (addr)
591 (concat
592 (if (stringp add-log-time-zone-rule)
593 (let ((tz (getenv "TZ")))
594 (unwind-protect
595 (progn
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))
600 " " full-name
601 " <" addr ">"))
602 (if (consp mailing-address)
603 mailing-address
604 (list mailing-address)))))
605 (if (and (not add-log-always-start-new-record)
606 (let ((hit nil))
607 (dolist (entry new-entries hit)
608 (when (looking-at (regexp-quote entry))
609 (setq hit t)))))
610 (forward-line 1)
611 (insert (nth (random (length new-entries))
612 new-entries)
613 (if use-hard-newlines hard-newline "\n")
614 (if use-hard-newlines hard-newline "\n"))
615 (forward-line -1))))
616
617 ;; Determine where we should stop searching for a usable
618 ;; item to add to, within this entry.
619 (let ((bound
620 (save-excursion
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
626 (point))))
627
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.
631 (if item
632 (insert item)))
633 ((and (not new-entry)
634 (let (case-fold-search)
635 (re-search-forward
636 (concat (regexp-quote (concat "* " item))
637 ;; Don't accept `foo.bar' when
638 ;; looking for `foo':
639 "\\(\\s \\|[(),:]\\)")
640 bound t)))
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"))
649 (forward-line -2)
650 (indent-relative-maybe))
651 (t
652 ;; Make a new item.
653 (while (looking-at "\\sW")
654 (forward-line 1))
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"))
660 (forward-line -2)
661 (indent-to left-margin)
662 (insert "* ")
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.
667 (if (not defun)
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 *\\)?$"))
672 (insert ": ")
673 (if version (insert version ?\s)))
674 ;; Make it easy to get rid of the function name.
675 (undo-boundary)
676 (unless (save-excursion
677 (beginning-of-line 1)
678 (looking-at "\\s *$"))
679 (insert ?\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)))
695 (goto-char pos)
696 (insert "("))
697 (set-marker pos nil))
698 (insert defun "): ")
699 (if version (insert version ?\s)))))
700
701 ;;;###autoload
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))
710
711
712 (defvar change-log-indent-text 0)
713
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):
720 (save-excursion
721 (end-of-line 0)
722 (skip-chars-backward " \t")
723 (when (and (equal (char-before) ?\,)
724 (> (point) (1+ (point-min))))
725 (condition-case nil
726 (when (save-excursion
727 (and (prog2
728 (up-list -1)
729 (equal (char-after) ?\()
730 (skip-chars-backward " \t"))
731 (or (bolp)
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")
738 (bolp)))))
739 ;; Delete the comma.
740 (delete-char -1)
741 ;; Close list on previous line.
742 (insert ")")
743 (skip-chars-forward " \t\n")
744 ;; Start list on new line.
745 (insert-before-markers "("))
746 (error nil)))))
747
748 (defun change-log-indent ()
749 (change-log-fill-parenthesized-list)
750 (let* ((indent
751 (save-excursion
752 (beginning-of-line)
753 (skip-chars-forward " \t")
754 (cond
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)))
759 0)
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))))
765
766
767 (defvar smerge-resolve-function)
768 (defvar copyright-at-end-flag)
769
770 ;;;###autoload
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}"
778 (setq left-margin 8
779 fill-column 74
780 indent-tabs-mode t
781 tab-width 8
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
787 '(lambda ()
788 (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
789 nil t)
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))
815
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]*"))
825 (lambda (a b)
826 ;; The file's extension may not have a valid
827 ;; version form (e.g. VC backup revisions).
828 (ignore-errors
829 (version< (substring b (length name))
830 (substring a (length name))))))))
831 (files (if isearch-forward files (reverse files))))
832 (find-file-noselect
833 (if wrap
834 (car files)
835 (cadr (member (file-name-nondirectory (buffer-file-name buffer))
836 files))))))
837
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."
845 (interactive "P")
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
849 ;; asterisk.
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)
854 t))
855 \f
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."
859 :type 'regexp
860 :group 'change-log)
861
862 ;;;###autoload
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'.")
866
867 ;;;###autoload
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'.")
871
872 ;;;###autoload
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'.")
876
877 (declare-function c-cpp-define-name "cc-cmds" ())
878 (declare-function c-defun-name "cc-cmds" ())
879
880 ;;;###autoload
881 (defun add-log-current-defun ()
882 "Return name of function definition point is in, or nil.
883
884 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
885 Texinfo (@node titles) and Perl.
886
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'.
892
893 Has a preference of looking backwards."
894 (condition-case nil
895 (save-excursion
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))
904 (beginning-of-defun)
905 ;; Make sure we are really inside the defun found,
906 ;; not after it.
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(")
913 (forward-char 1))
914 ;; Skip the defining construct name, typically "defun"
915 ;; or "defvar".
916 (forward-sexp 1)
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)
922 (point)))))
923 ((apply 'derived-mode-p add-log-c-like-modes)
924 (or (c-cpp-define-name)
925 (c-defun-name)))
926 ((memq major-mode add-log-tex-like-modes)
927 (if (re-search-backward
928 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
929 nil t)
930 (progn
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
944 ;; autoconf source.
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)))
949 (t
950 ;; If all else fails, try heuristics
951 (let (case-fold-search
952 result)
953 (end-of-line)
954 (when (re-search-backward
955 add-log-current-defun-header-regexp
956 (- (point) 10000)
957 t)
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]\\)"
962 result)
963 (setq result (match-string-no-properties 1 result)))
964 result))))))
965 (error nil)))
966
967 (defvar change-log-get-method-definition-md)
968
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
975 (match-string 1)
976 end))
977 (goto-char (match-end 0)))
978
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 "["))
982 (save-excursion
983 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
984 (change-log-get-method-definition-1 " ")))
985 (save-excursion
986 (cond
987 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
988 (change-log-get-method-definition-1 "")
989 (while (not (looking-at "[{;]"))
990 (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 "]"))))))
994 \f
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."
998 (require 'timezone)
999 (if (looking-at change-log-start-entry-re)
1000 (let ((date (match-string-no-properties 0)))
1001 (if date
1002 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
1003 (concat (match-string 1 date) (match-string 2 date)
1004 (match-string 3 date))
1005 (condition-case nil
1006 (timezone-make-date-sortable date)
1007 (error nil)))))
1008 (error "Bad date")))
1009
1010 (defun change-log-resolve-conflict ()
1011 "Function to be used in `smerge-resolve-function'."
1012 (save-excursion
1013 (save-restriction
1014 (narrow-to-region (match-beginning 0) (match-end 0))
1015 (let ((mb1 (match-beginning 1))
1016 (me1 (match-end 1))
1017 (mb3 (match-beginning 3))
1018 (me3 (match-end 3))
1019 (tmp1 (generate-new-buffer " *changelog-resolve-1*"))
1020 (tmp2 (generate-new-buffer " *changelog-resolve-2*")))
1021 (unwind-protect
1022 (let ((buf (current-buffer)))
1023 (with-current-buffer tmp1
1024 (change-log-mode)
1025 (insert-buffer-substring buf mb1 me1))
1026 (with-current-buffer tmp2
1027 (change-log-mode)
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)
1034 (prog1 (point)
1035 (insert-buffer-substring tmp2))))
1036 (kill-buffer tmp1)
1037 (kill-buffer tmp2))))))
1038
1039 ;;;###autoload
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
1044 or a buffer.
1045
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))
1054 date1 start end)
1055 (save-excursion
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.
1062 (while (not (eobp))
1063 (setq date1 (change-log-sortable-date-at))
1064 (setq start (point)
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)))
1070 (forward-page))
1071 (if (not (eobp))
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
1075 ;; merged.
1076 (unless (or (bobp)
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)))))))
1085
1086 (defun change-log-beginning-of-defun ()
1087 (re-search-backward change-log-start-entry-re nil 'move))
1088
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))
1094
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
1098 ;; the same entry.
1099 (forward-line 1)
1100
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))
1104 (forward-line 1))
1105
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))
1110 (forward-line -1)))
1111
1112 (provide 'add-log)
1113
1114 ;; arch-tag: 81eee6fc-088f-4372-a37f-80ad9620e762
1115 ;;; add-log.el ends here