]> code.delx.au - gnu-emacs/blob - lisp/add-log.el
(org-bbdb-export): Remove leftover docstring.
[gnu-emacs] / 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 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This facility is documented in the Emacs Manual.
27
28 ;; Todo:
29
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.
36
37 ;;; Code:
38
39 (eval-when-compile
40 (require 'cl) ; ignore-errors
41 (require 'timezone))
42
43 (defgroup change-log nil
44 "Change log maintenance."
45 :group 'tools
46 :link '(custom-manual "(emacs)Change Log")
47 :prefix "change-log-"
48 :prefix "add-log-")
49
50
51 (defcustom change-log-default-name nil
52 "Name of a change log file for \\[add-change-log-entry]."
53 :type '(choice (const :tag "default" nil)
54 string)
55 :group 'change-log)
56 (put 'change-log-default-name 'safe-local-variable 'string-or-null-p)
57
58 (defcustom change-log-mode-hook nil
59 "Normal hook run by `change-log-mode'."
60 :type 'hook
61 :group 'change-log)
62
63 ;; Many modes set this variable, so avoid warnings.
64 ;;;###autoload
65 (defcustom add-log-current-defun-function nil
66 "If non-nil, function to guess name of surrounding function.
67 It is used by `add-log-current-defun' in preference to built-in rules.
68 Returns function's name as a string, or nil if outside a function."
69 :type '(choice (const nil) function)
70 :group 'change-log)
71
72 ;;;###autoload
73 (defcustom add-log-full-name nil
74 "Full name of user, for inclusion in ChangeLog daily headers.
75 This defaults to the value returned by the function `user-full-name'."
76 :type '(choice (const :tag "Default" nil)
77 string)
78 :group 'change-log)
79
80 ;;;###autoload
81 (defcustom add-log-mailing-address nil
82 "Email addresses of user, for inclusion in ChangeLog headers.
83 This defaults to the value of `user-mail-address'. In addition to
84 being a simple string, this value can also be a list. All elements
85 will be recognized as referring to the same user; when creating a new
86 ChangeLog entry, one element will be chosen at random."
87 :type '(choice (const :tag "Default" nil)
88 (string :tag "String")
89 (repeat :tag "List of Strings" string))
90 :group 'change-log)
91
92 (defcustom add-log-time-format 'add-log-iso8601-time-string
93 "Function that defines the time format.
94 For example, `add-log-iso8601-time-string', which gives the
95 date in international ISO 8601 format,
96 and `current-time-string' are two valid values."
97 :type '(radio (const :tag "International ISO 8601 format"
98 add-log-iso8601-time-string)
99 (const :tag "Old format, as returned by `current-time-string'"
100 current-time-string)
101 (function :tag "Other"))
102 :group 'change-log)
103
104 (defcustom add-log-keep-changes-together nil
105 "If non-nil, normally keep day's log entries for one file together.
106
107 Log entries for a given file made with \\[add-change-log-entry] or
108 \\[add-change-log-entry-other-window] will only be added to others \
109 for that file made
110 today if this variable is non-nil or that file comes first in today's
111 entries. Otherwise another entry for that file will be started. An
112 original log:
113
114 * foo (...): ...
115 * bar (...): change 1
116
117 in the latter case, \\[add-change-log-entry-other-window] in a \
118 buffer visiting `bar', yields:
119
120 * bar (...): -!-
121 * foo (...): ...
122 * bar (...): change 1
123
124 and in the former:
125
126 * foo (...): ...
127 * bar (...): change 1
128 (...): -!-
129
130 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
131 this variable."
132 :version "20.3"
133 :type 'boolean
134 :group 'change-log)
135
136 (defcustom add-log-always-start-new-record nil
137 "If non-nil, `add-change-log-entry' will always start a new record."
138 :version "22.1"
139 :type 'boolean
140 :group 'change-log)
141
142 (defcustom add-log-buffer-file-name-function nil
143 "If non-nil, function to call to identify the full filename of a buffer.
144 This function is called with no argument. If this is nil, the default is to
145 use `buffer-file-name'."
146 :type '(choice (const nil) function)
147 :group 'change-log)
148
149 (defcustom add-log-file-name-function nil
150 "If non-nil, function to call to identify the filename for a ChangeLog entry.
151 This function is called with one argument, the value of variable
152 `buffer-file-name' in that buffer. If this is nil, the default is to
153 use the file's name relative to the directory of the change log file."
154 :type '(choice (const nil) function)
155 :group 'change-log)
156
157
158 (defcustom change-log-version-info-enabled nil
159 "If non-nil, enable recording version numbers with the changes."
160 :version "21.1"
161 :type 'boolean
162 :group 'change-log)
163
164 (defcustom change-log-version-number-regexp-list
165 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
166 (list
167 ;; (defconst ad-version "2.15"
168 (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re)
169 ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
170 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re)))
171 "List of regexps to search for version number.
172 The version number must be in group 1.
173 Note: The search is conducted only within 10%, at the beginning of the file."
174 :version "21.1"
175 :type '(repeat regexp)
176 :group 'change-log)
177
178 (defface change-log-date
179 '((t (:inherit font-lock-string-face)))
180 "Face used to highlight dates in date lines."
181 :version "21.1"
182 :group 'change-log)
183 ;; backward-compatibility alias
184 (put 'change-log-date-face 'face-alias 'change-log-date)
185
186 (defface change-log-name
187 '((t (:inherit font-lock-constant-face)))
188 "Face for highlighting author names."
189 :version "21.1"
190 :group 'change-log)
191 ;; backward-compatibility alias
192 (put 'change-log-name-face 'face-alias 'change-log-name)
193
194 (defface change-log-email
195 '((t (:inherit font-lock-variable-name-face)))
196 "Face for highlighting author email addresses."
197 :version "21.1"
198 :group 'change-log)
199 ;; backward-compatibility alias
200 (put 'change-log-email-face 'face-alias 'change-log-email)
201
202 (defface change-log-file
203 '((t (:inherit font-lock-function-name-face)))
204 "Face for highlighting file names."
205 :version "21.1"
206 :group 'change-log)
207 ;; backward-compatibility alias
208 (put 'change-log-file-face 'face-alias 'change-log-file)
209
210 (defface change-log-list
211 '((t (:inherit font-lock-keyword-face)))
212 "Face for highlighting parenthesized lists of functions or variables."
213 :version "21.1"
214 :group 'change-log)
215 ;; backward-compatibility alias
216 (put 'change-log-list-face 'face-alias 'change-log-list)
217
218 (defface change-log-conditionals
219 '((t (:inherit font-lock-variable-name-face)))
220 "Face for highlighting conditionals of the form `[...]'."
221 :version "21.1"
222 :group 'change-log)
223 ;; backward-compatibility alias
224 (put 'change-log-conditionals-face 'face-alias 'change-log-conditionals)
225
226 (defface change-log-function
227 '((t (:inherit font-lock-variable-name-face)))
228 "Face for highlighting items of the form `<....>'."
229 :version "21.1"
230 :group 'change-log)
231 ;; backward-compatibility alias
232 (put 'change-log-function-face 'face-alias 'change-log-function)
233
234 (defface change-log-acknowledgement
235 '((t (:inherit font-lock-comment-face)))
236 "Face for highlighting acknowledgments."
237 :version "21.1"
238 :group 'change-log)
239 ;; backward-compatibility alias
240 (put 'change-log-acknowledgement-face 'face-alias 'change-log-acknowledgement)
241
242 (defconst change-log-file-names-re "^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)")
243 (defconst change-log-start-entry-re "^\\sw.........[0-9:+ ]*")
244
245 (defvar change-log-font-lock-keywords
246 `(;;
247 ;; Date lines, new (2000-01-01) and old (Sat Jan 1 00:00:00 2000) styles.
248 ;; Fixme: this regepx is just an approximate one and may match
249 ;; wrongly with a non-date line existing as a random note. In
250 ;; addition, using any kind of fixed setting like this doesn't
251 ;; work if a user customizes add-log-time-format.
252 ("^[0-9-]+ +\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+"
253 (0 'change-log-date-face)
254 ;; Name and e-mail; some people put e-mail in parens, not angles.
255 ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
256 (1 'change-log-name)
257 (2 'change-log-email)))
258 ;;
259 ;; File names.
260 (,change-log-file-names-re
261 (2 'change-log-file)
262 ;; Possibly further names in a list:
263 ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 'change-log-file))
264 ;; Possibly a parenthesized list of names:
265 ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
266 nil nil (1 'change-log-list))
267 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
268 nil nil (1 'change-log-list)))
269 ;;
270 ;; Function or variable names.
271 ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
272 (2 'change-log-list)
273 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
274 (1 'change-log-list)))
275 ;;
276 ;; Conditionals.
277 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals))
278 ;;
279 ;; Function of change.
280 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function))
281 ;;
282 ;; Acknowledgements.
283 ;; Don't include plain "From" because that is vague;
284 ;; we want to encourage people to say something more specific.
285 ;; Note that the FSF does not use "Patches by"; our convention
286 ;; is to put the name of the author of the changes at the top
287 ;; of the change log entry.
288 ("\\(^\\( +\\|\t\\)\\| \\)\\(Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
289 3 'change-log-acknowledgement))
290 "Additional expressions to highlight in Change Log mode.")
291
292 (defun change-log-search-file-name (where)
293 "Return the file-name for the change under point."
294 (save-excursion
295 (goto-char where)
296 (beginning-of-line 1)
297 (if (looking-at change-log-start-entry-re)
298 ;; We are at the start of an entry, search forward for a file
299 ;; name.
300 (progn
301 (re-search-forward change-log-file-names-re nil t)
302 (match-string 2))
303 (if (looking-at change-log-file-names-re)
304 ;; We found a file name.
305 (match-string 2)
306 ;; Look backwards for either a file name or the log entry start.
307 (if (re-search-backward
308 (concat "\\(" change-log-start-entry-re
309 "\\)\\|\\("
310 change-log-file-names-re "\\)") nil t)
311 (if (match-beginning 1)
312 ;; We got the start of the entry, look forward for a
313 ;; file name.
314 (progn
315 (re-search-forward change-log-file-names-re nil t)
316 (match-string 2))
317 (match-string 4))
318 ;; We must be before any file name, look forward.
319 (re-search-forward change-log-file-names-re nil t)
320 (match-string 2))))))
321
322 (defun change-log-find-file ()
323 "Visit the file for the change under point."
324 (interactive)
325 (let ((file (change-log-search-file-name (point))))
326 (if (and file (file-exists-p file))
327 (find-file file)
328 (message "No such file or directory: %s" file))))
329
330 (defvar change-log-mode-map
331 (let ((map (make-sparse-keymap)))
332 (define-key map [?\C-c ?\C-p] 'add-log-edit-prev-comment)
333 (define-key map [?\C-c ?\C-n] 'add-log-edit-next-comment)
334 (define-key map [?\C-c ?\C-f] 'change-log-find-file)
335 map)
336 "Keymap for Change Log major mode.")
337
338 ;; It used to be called change-log-time-zone-rule but really should be
339 ;; called add-log-time-zone-rule since it's only used from add-log-* code.
340 (defvaralias 'change-log-time-zone-rule 'add-log-time-zone-rule)
341 (defvar add-log-time-zone-rule nil
342 "Time zone used for calculating change log time stamps.
343 It takes the same format as the TZ argument of `set-time-zone-rule'.
344 If nil, use local time.
345 If t, use universal time.")
346 (put 'add-log-time-zone-rule 'safe-local-variable
347 '(lambda (x) (or (booleanp x) (stringp x))))
348
349 (defun add-log-iso8601-time-zone (&optional time)
350 (let* ((utc-offset (or (car (current-time-zone time)) 0))
351 (sign (if (< utc-offset 0) ?- ?+))
352 (sec (abs utc-offset))
353 (ss (% sec 60))
354 (min (/ sec 60))
355 (mm (% min 60))
356 (hh (/ min 60)))
357 (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
358 ((not (zerop mm)) "%c%02d:%02d")
359 (t "%c%02d"))
360 sign hh mm ss)))
361
362 (defvar add-log-iso8601-with-time-zone nil)
363
364 (defun add-log-iso8601-time-string ()
365 (let ((time (format-time-string "%Y-%m-%d"
366 nil (eq t add-log-time-zone-rule))))
367 (if add-log-iso8601-with-time-zone
368 (concat time " " (add-log-iso8601-time-zone))
369 time)))
370
371 (defun change-log-name ()
372 "Return (system-dependent) default name for a change log file."
373 (or change-log-default-name
374 (if (eq system-type 'vax-vms)
375 "$CHANGE_LOG$.TXT"
376 "ChangeLog")))
377
378 (defun add-log-edit-prev-comment (arg)
379 "Cycle backward through Log-Edit mode comment history.
380 With a numeric prefix ARG, go back ARG comments."
381 (interactive "*p")
382 (save-restriction
383 (narrow-to-region (point)
384 (if (memq last-command '(add-log-edit-prev-comment
385 add-log-edit-next-comment))
386 (mark) (point)))
387 (when (fboundp 'log-edit-previous-comment)
388 (log-edit-previous-comment arg)
389 (indent-region (point-min) (point-max))
390 (goto-char (point-min))
391 (unless (save-restriction (widen) (bolp))
392 (delete-region (point) (progn (skip-chars-forward " \t\n") (point))))
393 (set-mark (point-min))
394 (goto-char (point-max))
395 (delete-region (point) (progn (skip-chars-backward " \t\n") (point))))))
396
397 (defun add-log-edit-next-comment (arg)
398 "Cycle forward through Log-Edit mode comment history.
399 With a numeric prefix ARG, go back ARG comments."
400 (interactive "*p")
401 (add-log-edit-prev-comment (- arg)))
402
403 ;;;###autoload
404 (defun prompt-for-change-log-name ()
405 "Prompt for a change log name."
406 (let* ((default (change-log-name))
407 (name (expand-file-name
408 (read-file-name (format "Log file (default %s): " default)
409 nil default))))
410 ;; Handle something that is syntactically a directory name.
411 ;; Look for ChangeLog or whatever in that directory.
412 (if (string= (file-name-nondirectory name) "")
413 (expand-file-name (file-name-nondirectory default)
414 name)
415 ;; Handle specifying a file that is a directory.
416 (if (file-directory-p name)
417 (expand-file-name (file-name-nondirectory default)
418 (file-name-as-directory name))
419 name))))
420
421 (defun change-log-version-number-search ()
422 "Return version number of current buffer's file.
423 This is the value returned by `vc-working-revision' or, if that is
424 nil, by matching `change-log-version-number-regexp-list'."
425 (let* ((size (buffer-size))
426 (limit
427 ;; The version number can be anywhere in the file, but
428 ;; restrict search to the file beginning: 10% should be
429 ;; enough to prevent some mishits.
430 ;;
431 ;; Apply percentage only if buffer size is bigger than
432 ;; approx 100 lines.
433 (if (> size (* 100 80)) (+ (point) (/ size 10)))))
434 (or (and buffer-file-name (vc-working-revision buffer-file-name))
435 (save-restriction
436 (widen)
437 (let ((regexps change-log-version-number-regexp-list)
438 version)
439 (while regexps
440 (save-excursion
441 (goto-char (point-min))
442 (when (re-search-forward (pop regexps) limit t)
443 (setq version (match-string 1)
444 regexps nil))))
445 version)))))
446
447
448 ;;;###autoload
449 (defun find-change-log (&optional file-name buffer-file)
450 "Find a change log file for \\[add-change-log-entry] and return the name.
451
452 Optional arg FILE-NAME specifies the file to use.
453 If FILE-NAME is nil, use the value of `change-log-default-name'.
454 If `change-log-default-name' is nil, behave as though it were 'ChangeLog'
455 \(or whatever we use on this operating system).
456
457 If `change-log-default-name' contains a leading directory component, then
458 simply find it in the current directory. Otherwise, search in the current
459 directory and its successive parents for a file so named.
460
461 Once a file is found, `change-log-default-name' is set locally in the
462 current buffer to the complete file name.
463 Optional arg BUFFER-FILE overrides `buffer-file-name'."
464 ;; If user specified a file name or if this buffer knows which one to use,
465 ;; just use that.
466 (or file-name
467 (setq file-name (and change-log-default-name
468 (file-name-directory change-log-default-name)
469 change-log-default-name))
470 (progn
471 ;; Chase links in the source file
472 ;; and use the change log in the dir where it points.
473 (setq file-name (or (and (or buffer-file buffer-file-name)
474 (file-name-directory
475 (file-chase-links
476 (or buffer-file buffer-file-name))))
477 default-directory))
478 (if (file-directory-p file-name)
479 (setq file-name (expand-file-name (change-log-name) file-name)))
480 ;; Chase links before visiting the file.
481 ;; This makes it easier to use a single change log file
482 ;; for several related directories.
483 (setq file-name (file-chase-links file-name))
484 (setq file-name (expand-file-name file-name))
485 ;; Move up in the dir hierarchy till we find a change log file.
486 (let ((file1 file-name)
487 parent-dir)
488 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
489 (progn (setq parent-dir
490 (file-name-directory
491 (directory-file-name
492 (file-name-directory file1))))
493 ;; Give up if we are already at the root dir.
494 (not (string= (file-name-directory file1)
495 parent-dir))))
496 ;; Move up to the parent dir and try again.
497 (setq file1 (expand-file-name
498 (file-name-nondirectory (change-log-name))
499 parent-dir)))
500 ;; If we found a change log in a parent, use that.
501 (if (or (get-file-buffer file1) (file-exists-p file1))
502 (setq file-name file1)))))
503 ;; Make a local variable in this buffer so we needn't search again.
504 (set (make-local-variable 'change-log-default-name) file-name)
505 file-name)
506
507 (defun add-log-file-name (buffer-file log-file)
508 ;; Never want to add a change log entry for the ChangeLog file itself.
509 (unless (or (null buffer-file) (string= buffer-file log-file))
510 (if add-log-file-name-function
511 (funcall add-log-file-name-function buffer-file)
512 (setq buffer-file
513 (file-relative-name buffer-file (file-name-directory log-file)))
514 ;; If we have a backup file, it's presumably because we're
515 ;; comparing old and new versions (e.g. for deleted
516 ;; functions) and we'll want to use the original name.
517 (if (backup-file-name-p buffer-file)
518 (file-name-sans-versions buffer-file)
519 buffer-file))))
520
521 ;;;###autoload
522 (defun add-change-log-entry (&optional whoami file-name other-window new-entry)
523 "Find change log file, and add an entry for today and an item for this file.
524 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
525 name and email (stored in `add-log-full-name' and `add-log-mailing-address').
526
527 Second arg FILE-NAME is file name of the change log.
528 If nil, use the value of `change-log-default-name'.
529
530 Third arg OTHER-WINDOW non-nil means visit in other window.
531
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.
535
536 Option `add-log-always-start-new-record' non-nil means always create a
537 new record, even when the last record was made on the same date and by
538 the same person.
539
540 The change log file can start with a copyright notice and a copying
541 permission notice. The first blank line indicates the end of these
542 notices.
543
544 Today's date is calculated according to `add-log-time-zone-rule' if
545 non-nil, otherwise in local time."
546 (interactive (list current-prefix-arg
547 (prompt-for-change-log-name)))
548 (let* ((defun (add-log-current-defun))
549 (version (and change-log-version-info-enabled
550 (change-log-version-number-search)))
551 (buf-file-name (if add-log-buffer-file-name-function
552 (funcall add-log-buffer-file-name-function)
553 buffer-file-name))
554 (buffer-file (if buf-file-name (expand-file-name buf-file-name)))
555 (file-name (expand-file-name (find-change-log file-name buffer-file)))
556 ;; Set ITEM to the file name to use in the new item.
557 (item (add-log-file-name buffer-file file-name)))
558
559 (unless (equal file-name buffer-file-name)
560 (if (or other-window (window-dedicated-p (selected-window)))
561 (find-file-other-window file-name)
562 (find-file file-name)))
563 (or (derived-mode-p 'change-log-mode)
564 (change-log-mode))
565 (undo-boundary)
566 (goto-char (point-min))
567
568 (let ((full-name (or add-log-full-name (user-full-name)))
569 (mailing-address (or add-log-mailing-address user-mail-address)))
570
571 (when whoami
572 (setq full-name (read-string "Full name: " full-name))
573 ;; Note that some sites have room and phone number fields in
574 ;; full name which look silly when inserted. Rather than do
575 ;; anything about that here, let user give prefix argument so that
576 ;; s/he can edit the full name field in prompter if s/he wants.
577 (setq mailing-address
578 (read-string "Mailing address: " mailing-address)))
579
580 ;; If file starts with a copyright and permission notice, skip them.
581 ;; Assume they end at first blank line.
582 (when (looking-at "Copyright")
583 (search-forward "\n\n")
584 (skip-chars-forward "\n"))
585
586 ;; Advance into first entry if it is usable; else make new one.
587 (let ((new-entries
588 (mapcar (lambda (addr)
589 (concat
590 (if (stringp add-log-time-zone-rule)
591 (let ((tz (getenv "TZ")))
592 (unwind-protect
593 (progn
594 (set-time-zone-rule add-log-time-zone-rule)
595 (funcall add-log-time-format))
596 (set-time-zone-rule tz)))
597 (funcall add-log-time-format))
598 " " full-name
599 " <" addr ">"))
600 (if (consp mailing-address)
601 mailing-address
602 (list mailing-address)))))
603 (if (and (not add-log-always-start-new-record)
604 (let ((hit nil))
605 (dolist (entry new-entries hit)
606 (when (looking-at (regexp-quote entry))
607 (setq hit t)))))
608 (forward-line 1)
609 (insert (nth (random (length new-entries))
610 new-entries)
611 (if use-hard-newlines hard-newline "\n")
612 (if use-hard-newlines hard-newline "\n"))
613 (forward-line -1))))
614
615 ;; Determine where we should stop searching for a usable
616 ;; item to add to, within this entry.
617 (let ((bound
618 (save-excursion
619 (if (looking-at "\n*[^\n* \t]")
620 (skip-chars-forward "\n")
621 (if add-log-keep-changes-together
622 (forward-page) ; page delimits entries for date
623 (forward-paragraph))) ; paragraph delimits entries for file
624 (point))))
625
626 ;; Now insert the new line for this item.
627 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t)
628 ;; Put this file name into the existing empty item.
629 (if item
630 (insert item)))
631 ((and (not new-entry)
632 (let (case-fold-search)
633 (re-search-forward
634 (concat (regexp-quote (concat "* " item))
635 ;; Don't accept `foo.bar' when
636 ;; looking for `foo':
637 "\\(\\s \\|[(),:]\\)")
638 bound t)))
639 ;; Add to the existing item for the same file.
640 (re-search-forward "^\\s *$\\|^\\s \\*")
641 (goto-char (match-beginning 0))
642 ;; Delete excess empty lines; make just 2.
643 (while (and (not (eobp)) (looking-at "^\\s *$"))
644 (delete-region (point) (line-beginning-position 2)))
645 (insert (if use-hard-newlines hard-newline "\n")
646 (if use-hard-newlines hard-newline "\n"))
647 (forward-line -2)
648 (indent-relative-maybe))
649 (t
650 ;; Make a new item.
651 (while (looking-at "\\sW")
652 (forward-line 1))
653 (while (and (not (eobp)) (looking-at "^\\s *$"))
654 (delete-region (point) (line-beginning-position 2)))
655 (insert (if use-hard-newlines hard-newline "\n")
656 (if use-hard-newlines hard-newline "\n")
657 (if use-hard-newlines hard-newline "\n"))
658 (forward-line -2)
659 (indent-to left-margin)
660 (insert "* ")
661 (if item (insert item)))))
662 ;; Now insert the function name, if we have one.
663 ;; Point is at the item for this file,
664 ;; either at the end of the line or at the first blank line.
665 (if (not defun)
666 ;; No function name, so put in a colon unless we have just a star.
667 (unless (save-excursion
668 (beginning-of-line 1)
669 (looking-at "\\s *\\(\\*\\s *\\)?$"))
670 (insert ": ")
671 (if version (insert version ?\s)))
672 ;; Make it easy to get rid of the function name.
673 (undo-boundary)
674 (unless (save-excursion
675 (beginning-of-line 1)
676 (looking-at "\\s *$"))
677 (insert ?\s))
678 ;; See if the prev function name has a message yet or not.
679 ;; If not, merge the two items.
680 (let ((pos (point-marker)))
681 (skip-syntax-backward " ")
682 (skip-chars-backward "):")
683 (if (and (looking-at "):")
684 (let ((pos (save-excursion (backward-sexp 1) (point))))
685 (when (equal (buffer-substring pos (point)) defun)
686 (delete-region pos (point)))
687 (> fill-column (+ (current-column) (length defun) 4))))
688 (progn (skip-chars-backward ", ")
689 (delete-region (point) pos)
690 (unless (memq (char-before) '(?\()) (insert ", ")))
691 (if (looking-at "):")
692 (delete-region (+ 1 (point)) (line-end-position)))
693 (goto-char pos)
694 (insert "("))
695 (set-marker pos nil))
696 (insert defun "): ")
697 (if version (insert version ?\s)))))
698
699 ;;;###autoload
700 (defun add-change-log-entry-other-window (&optional whoami file-name)
701 "Find change log file in other window and add entry and item.
702 This is just like `add-change-log-entry' except that it displays
703 the change log file in another window."
704 (interactive (if current-prefix-arg
705 (list current-prefix-arg
706 (prompt-for-change-log-name))))
707 (add-change-log-entry whoami file-name t))
708
709
710 (defvar change-log-indent-text 0)
711
712 (defun change-log-fill-parenthesized-list ()
713 ;; Fill parenthesized lists of names according to GNU standards.
714 ;; * file-name.ext (very-long-foo, very-long-bar, very-long-foobar):
715 ;; should be filled as
716 ;; * file-name.ext (very-long-foo, very-long-bar)
717 ;; (very-long-foobar):
718 (save-excursion
719 (end-of-line 0)
720 (skip-chars-backward " \t")
721 (when (and (equal (char-before) ?\,)
722 (> (point) (1+ (point-min))))
723 (condition-case nil
724 (when (save-excursion
725 (and (prog2
726 (up-list -1)
727 (equal (char-after) ?\()
728 (skip-chars-backward " \t"))
729 (or (bolp)
730 ;; Skip everything but a whitespace or asterisk.
731 (and (not (zerop (skip-chars-backward "^ \t\n*")))
732 (skip-chars-backward " \t")
733 ;; We want one asterisk here.
734 (= (skip-chars-backward "*") -1)
735 (skip-chars-backward " \t")
736 (bolp)))))
737 ;; Delete the comma.
738 (delete-char -1)
739 ;; Close list on previous line.
740 (insert ")")
741 (skip-chars-forward " \t\n")
742 ;; Start list on new line.
743 (insert-before-markers "("))
744 (error nil)))))
745
746 (defun change-log-indent ()
747 (change-log-fill-parenthesized-list)
748 (let* ((indent
749 (save-excursion
750 (beginning-of-line)
751 (skip-chars-forward " \t")
752 (cond
753 ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
754 ;; Matching the output of add-log-time-format is difficult,
755 ;; but I'll get it has at least two adjacent digits.
756 (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
757 0)
758 ((looking-at "[^*(]")
759 (+ (current-left-margin) change-log-indent-text))
760 (t (current-left-margin)))))
761 (pos (save-excursion (indent-line-to indent) (point))))
762 (if (> pos (point)) (goto-char pos))))
763
764
765 (defvar smerge-resolve-function)
766 (defvar copyright-at-end-flag)
767
768 ;;;###autoload
769 (define-derived-mode change-log-mode text-mode "Change Log"
770 "Major mode for editing change logs; like Indented Text Mode.
771 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
772 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
773 Each entry behaves as a paragraph, and the entries for one day as a page.
774 Runs `change-log-mode-hook'.
775 \\{change-log-mode-map}"
776 (setq left-margin 8
777 fill-column 74
778 indent-tabs-mode t
779 tab-width 8
780 show-trailing-whitespace t)
781 (set (make-local-variable 'fill-paragraph-function)
782 'change-log-fill-paragraph)
783 ;; Avoid that filling leaves behind a single "*" on a line.
784 (add-hook 'fill-nobreak-predicate
785 '(lambda ()
786 (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
787 nil t)
788 (set (make-local-variable 'indent-line-function) 'change-log-indent)
789 (set (make-local-variable 'tab-always-indent) nil)
790 (set (make-local-variable 'copyright-at-end-flag) t)
791 ;; We really do want "^" in paragraph-start below: it is only the
792 ;; lines that begin at column 0 (despite the left-margin of 8) that
793 ;; we are looking for. Adding `* ' allows eliding the blank line
794 ;; between entries for different files.
795 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
796 (set (make-local-variable 'paragraph-separate) paragraph-start)
797 ;; Match null string on the date-line so that the date-line
798 ;; is grouped with what follows.
799 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
800 (set (make-local-variable 'version-control) 'never)
801 (set (make-local-variable 'smerge-resolve-function)
802 'change-log-resolve-conflict)
803 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
804 (set (make-local-variable 'font-lock-defaults)
805 '(change-log-font-lock-keywords t nil nil backward-paragraph))
806 (set (make-local-variable 'isearch-buffers-next-buffer-function)
807 'change-log-next-buffer)
808 (set (make-local-variable 'beginning-of-defun-function)
809 'change-log-beginning-of-defun)
810 (set (make-local-variable 'end-of-defun-function)
811 'change-log-end-of-defun)
812 (isearch-buffers-minor-mode))
813
814 (defun change-log-next-buffer (&optional buffer wrap)
815 "Return the next buffer in the series of ChangeLog file buffers.
816 This function is used for multiple buffers isearch.
817 A sequence of buffers is formed by ChangeLog files with decreasing
818 numeric file name suffixes in the directory of the initial ChangeLog
819 file were isearch was started."
820 (let* ((name (change-log-name))
821 (files (cons name (sort (file-expand-wildcards
822 (concat name "[-.][0-9]*"))
823 (lambda (a b)
824 ;; The file's extension may not have a valid
825 ;; version form (e.g. VC backup revisions).
826 (ignore-errors
827 (version< (substring b (length name))
828 (substring a (length name))))))))
829 (files (if isearch-forward files (reverse files))))
830 (find-file-noselect
831 (if wrap
832 (car files)
833 (cadr (member (file-name-nondirectory (buffer-file-name buffer))
834 files))))))
835
836 ;; It might be nice to have a general feature to replace this. The idea I
837 ;; have is a variable giving a regexp matching text which should not be
838 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
839 ;; But I don't feel up to implementing that today.
840 (defun change-log-fill-paragraph (&optional justify)
841 "Fill the paragraph, but preserve open parentheses at beginning of lines.
842 Prefix arg means justify as well."
843 (interactive "P")
844 (let ((end (progn (forward-paragraph) (point)))
845 (beg (progn (backward-paragraph) (point)))
846 ;; Add lines starting with whitespace followed by a left paren or an
847 ;; asterisk.
848 (paragraph-start (concat paragraph-start "\\|\\s *\\(?:\\s(\\|\\*\\)"))
849 ;; Make sure we call `change-log-indent'.
850 (fill-indent-according-to-mode t))
851 (fill-region beg end justify)
852 t))
853 \f
854 (defcustom add-log-current-defun-header-regexp
855 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
856 "Heuristic regexp used by `add-log-current-defun' for unknown major modes."
857 :type 'regexp
858 :group 'change-log)
859
860 ;;;###autoload
861 (defvar add-log-lisp-like-modes
862 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode)
863 "*Modes that look like Lisp to `add-log-current-defun'.")
864
865 ;;;###autoload
866 (defvar add-log-c-like-modes
867 '(c-mode c++-mode c++-c-mode objc-mode)
868 "*Modes that look like C to `add-log-current-defun'.")
869
870 ;;;###autoload
871 (defvar add-log-tex-like-modes
872 '(TeX-mode plain-TeX-mode LaTeX-mode tex-mode)
873 "*Modes that look like TeX to `add-log-current-defun'.")
874
875 (declare-function c-cpp-define-name "cc-cmds" ())
876 (declare-function c-defun-name "cc-cmds" ())
877
878 ;;;###autoload
879 (defun add-log-current-defun ()
880 "Return name of function definition point is in, or nil.
881
882 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
883 Texinfo (@node titles) and Perl.
884
885 Other modes are handled by a heuristic that looks in the 10K before
886 point for uppercase headings starting in the first column or
887 identifiers followed by `:' or `='. See variables
888 `add-log-current-defun-header-regexp' and
889 `add-log-current-defun-function'.
890
891 Has a preference of looking backwards."
892 (condition-case nil
893 (save-excursion
894 (let ((location (point)))
895 (cond (add-log-current-defun-function
896 (funcall add-log-current-defun-function))
897 ((apply 'derived-mode-p add-log-lisp-like-modes)
898 ;; If we are now precisely at the beginning of a defun,
899 ;; make sure beginning-of-defun finds that one
900 ;; rather than the previous one.
901 (or (eobp) (forward-char 1))
902 (beginning-of-defun)
903 ;; Make sure we are really inside the defun found,
904 ;; not after it.
905 (when (and (looking-at "\\s(")
906 (progn (end-of-defun)
907 (< location (point)))
908 (progn (forward-sexp -1)
909 (>= location (point))))
910 (if (looking-at "\\s(")
911 (forward-char 1))
912 ;; Skip the defining construct name, typically "defun"
913 ;; or "defvar".
914 (forward-sexp 1)
915 ;; The second element is usually a symbol being defined.
916 ;; If it is not, use the first symbol in it.
917 (skip-chars-forward " \t\n'(")
918 (buffer-substring-no-properties (point)
919 (progn (forward-sexp 1)
920 (point)))))
921 ((apply 'derived-mode-p add-log-c-like-modes)
922 (or (c-cpp-define-name)
923 (c-defun-name)))
924 ((memq major-mode add-log-tex-like-modes)
925 (if (re-search-backward
926 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
927 nil t)
928 (progn
929 (goto-char (match-beginning 0))
930 (buffer-substring-no-properties
931 (1+ (point)) ; without initial backslash
932 (line-end-position)))))
933 ((derived-mode-p 'texinfo-mode)
934 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
935 (match-string-no-properties 1)))
936 ((derived-mode-p 'perl-mode 'cperl-mode)
937 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t)
938 (match-string-no-properties 1)))
939 ;; Emacs's autoconf-mode installs its own
940 ;; `add-log-current-defun-function'. This applies to
941 ;; a different mode apparently for editing .m4
942 ;; autoconf source.
943 ((derived-mode-p 'autoconf-mode)
944 (if (re-search-backward
945 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t)
946 (match-string-no-properties 3)))
947 (t
948 ;; If all else fails, try heuristics
949 (let (case-fold-search
950 result)
951 (end-of-line)
952 (when (re-search-backward
953 add-log-current-defun-header-regexp
954 (- (point) 10000)
955 t)
956 (setq result (or (match-string-no-properties 1)
957 (match-string-no-properties 0)))
958 ;; Strip whitespace away
959 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
960 result)
961 (setq result (match-string-no-properties 1 result)))
962 result))))))
963 (error nil)))
964
965 (defvar change-log-get-method-definition-md)
966
967 ;; Subroutine used within change-log-get-method-definition.
968 ;; Add the last match in the buffer to the end of `md',
969 ;; followed by the string END; move to the end of that match.
970 (defun change-log-get-method-definition-1 (end)
971 (setq change-log-get-method-definition-md
972 (concat change-log-get-method-definition-md
973 (match-string 1)
974 end))
975 (goto-char (match-end 0)))
976
977 (defun change-log-get-method-definition ()
978 "For Objective C, return the method name if we are in a method."
979 (let ((change-log-get-method-definition-md "["))
980 (save-excursion
981 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
982 (change-log-get-method-definition-1 " ")))
983 (save-excursion
984 (cond
985 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
986 (change-log-get-method-definition-1 "")
987 (while (not (looking-at "[{;]"))
988 (looking-at
989 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
990 (change-log-get-method-definition-1 ""))
991 (concat change-log-get-method-definition-md "]"))))))
992 \f
993 (defun change-log-sortable-date-at ()
994 "Return date of log entry in a consistent form for sorting.
995 Point is assumed to be at the start of the entry."
996 (require 'timezone)
997 (if (looking-at change-log-start-entry-re)
998 (let ((date (match-string-no-properties 0)))
999 (if date
1000 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
1001 (concat (match-string 1 date) (match-string 2 date)
1002 (match-string 3 date))
1003 (condition-case nil
1004 (timezone-make-date-sortable date)
1005 (error nil)))))
1006 (error "Bad date")))
1007
1008 (defun change-log-resolve-conflict ()
1009 "Function to be used in `smerge-resolve-function'."
1010 (save-excursion
1011 (save-restriction
1012 (narrow-to-region (match-beginning 0) (match-end 0))
1013 (let ((mb1 (match-beginning 1))
1014 (me1 (match-end 1))
1015 (mb3 (match-beginning 3))
1016 (me3 (match-end 3))
1017 (tmp1 (generate-new-buffer " *changelog-resolve-1*"))
1018 (tmp2 (generate-new-buffer " *changelog-resolve-2*")))
1019 (unwind-protect
1020 (let ((buf (current-buffer)))
1021 (with-current-buffer tmp1
1022 (change-log-mode)
1023 (insert-buffer-substring buf mb1 me1))
1024 (with-current-buffer tmp2
1025 (change-log-mode)
1026 (insert-buffer-substring buf mb3 me3)
1027 ;; Do the merge here instead of inside `buf' so as to be
1028 ;; more robust in case change-log-merge fails.
1029 (change-log-merge tmp1))
1030 (goto-char (point-max))
1031 (delete-region (point-min)
1032 (prog1 (point)
1033 (insert-buffer-substring tmp2))))
1034 (kill-buffer tmp1)
1035 (kill-buffer tmp2))))))
1036
1037 ;;;###autoload
1038 (defun change-log-merge (other-log)
1039 "Merge the contents of change log file OTHER-LOG with this buffer.
1040 Both must be found in Change Log mode (since the merging depends on
1041 the appropriate motion commands). OTHER-LOG can be either a file name
1042 or a buffer.
1043
1044 Entries are inserted in chronological order. Both the current and
1045 old-style time formats for entries are supported."
1046 (interactive "*fLog file name to merge: ")
1047 (if (not (derived-mode-p 'change-log-mode))
1048 (error "Not in Change Log mode"))
1049 (let ((other-buf (if (bufferp other-log) other-log
1050 (find-file-noselect other-log)))
1051 (buf (current-buffer))
1052 date1 start end)
1053 (save-excursion
1054 (goto-char (point-min))
1055 (set-buffer other-buf)
1056 (goto-char (point-min))
1057 (if (not (derived-mode-p 'change-log-mode))
1058 (error "%s not found in Change Log mode" other-log))
1059 ;; Loop through all the entries in OTHER-LOG.
1060 (while (not (eobp))
1061 (setq date1 (change-log-sortable-date-at))
1062 (setq start (point)
1063 end (progn (forward-page) (point)))
1064 ;; Look for an entry in original buffer that isn't later.
1065 (with-current-buffer buf
1066 (while (and (not (eobp))
1067 (string< date1 (change-log-sortable-date-at)))
1068 (forward-page))
1069 (if (not (eobp))
1070 (insert-buffer-substring other-buf start end)
1071 ;; At the end of the original buffer, insert a newline to
1072 ;; separate entries and then the rest of the file being
1073 ;; merged.
1074 (unless (or (bobp)
1075 (and (= ?\n (char-before))
1076 (or (<= (1- (point)) (point-min))
1077 (= ?\n (char-before (1- (point)))))))
1078 (insert (if use-hard-newlines hard-newline "\n")))
1079 ;; Move to the end of it to terminate outer loop.
1080 (with-current-buffer other-buf
1081 (goto-char (point-max)))
1082 (insert-buffer-substring other-buf start)))))))
1083
1084 (defun change-log-beginning-of-defun ()
1085 (re-search-backward change-log-start-entry-re nil 'move))
1086
1087 (defun change-log-end-of-defun ()
1088 ;; Look back and if there is no entry there it means we are before
1089 ;; the first ChangeLog entry, so go forward until finding one.
1090 (unless (save-excursion (re-search-backward change-log-start-entry-re nil t))
1091 (re-search-forward change-log-start-entry-re nil t))
1092
1093 ;; In case we are at the end of log entry going forward a line will
1094 ;; make us find the next entry when searching. If we are inside of
1095 ;; an entry going forward a line will still keep the point inside
1096 ;; the same entry.
1097 (forward-line 1)
1098
1099 ;; In case we are at the beginning of an entry, move past it.
1100 (when (looking-at change-log-start-entry-re)
1101 (goto-char (match-end 0))
1102 (forward-line 1))
1103
1104 ;; Search for the start of the next log entry. Go to the end of the
1105 ;; buffer if we could not find a next entry.
1106 (when (re-search-forward change-log-start-entry-re nil 'move)
1107 (goto-char (match-beginning 0))
1108 (forward-line -1)))
1109
1110 (provide 'add-log)
1111
1112 ;; arch-tag: 81eee6fc-088f-4372-a37f-80ad9620e762
1113 ;;; add-log.el ends here