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