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