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