]> code.delx.au - gnu-emacs/blob - lisp/add-log.el
Rename `MS-DOG' into `MS-DOS'.
[gnu-emacs] / lisp / add-log.el
1 ;;; add-log.el --- change log maintenance commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1988, 1993, 1994, 1997, 1998, 2000, 2002,
4 ;; 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: tools
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; This facility is documented in the Emacs Manual.
29
30 ;;; Code:
31
32 (eval-when-compile
33 (require 'timezone))
34
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-")
41
42
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))))
50
51 (defcustom change-log-mode-hook nil
52 "Normal hook run by `change-log-mode'."
53 :type 'hook
54 :group 'change-log)
55
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)
64
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)
72
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)
84
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)
96
97 (defcustom add-log-keep-changes-together nil
98 "If non-nil, normally keep day's log entries for one file together.
99
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:
106
107 * foo (...): ...
108 * bar (...): change 1
109
110 in the latter case, \\[add-change-log-entry-other-window] in a \
111 buffer visiting `bar', yields:
112
113 * bar (...): -!-
114 * foo (...): ...
115 * bar (...): change 1
116
117 and in the former:
118
119 * foo (...): ...
120 * bar (...): change 1
121 (...): -!-
122
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)
128
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)
134
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)
141
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)
149
150
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)
156
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)
170
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)
178
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)
186
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)
194
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)
202
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)
210
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)
218
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)
226
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)
234
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)))
248 ;;
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)))
259 ;;
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)))
265 ;;
266 ;; Conditionals.
267 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals))
268 ;;
269 ;; Function of change.
270 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function))
271 ;;
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.")
281
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.")
288
289 ;; It used to be called change-log-time-zone-rule but really should be
290 ;; called add-log-time-zone-rule since it's only used from add-log-* code.
291 (defvaralias 'change-log-time-zone-rule 'add-log-time-zone-rule)
292 (defvar add-log-time-zone-rule nil
293 "Time zone used for calculating change log time stamps.
294 It takes the same format as the TZ argument of `set-time-zone-rule'.
295 If nil, use local time.
296 If t, use universal time.")
297
298 (defun add-log-iso8601-time-zone (&optional time)
299 (let* ((utc-offset (or (car (current-time-zone time)) 0))
300 (sign (if (< utc-offset 0) ?- ?+))
301 (sec (abs utc-offset))
302 (ss (% sec 60))
303 (min (/ sec 60))
304 (mm (% min 60))
305 (hh (/ min 60)))
306 (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
307 ((not (zerop mm)) "%c%02d:%02d")
308 (t "%c%02d"))
309 sign hh mm ss)))
310
311 (defvar add-log-iso8601-with-time-zone nil)
312
313 (defun add-log-iso8601-time-string ()
314 (let ((time (format-time-string "%Y-%m-%d"
315 nil (eq t add-log-time-zone-rule))))
316 (if add-log-iso8601-with-time-zone
317 (concat time " " (add-log-iso8601-time-zone))
318 time)))
319
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")))
326
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))))))
345
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)))
351
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))))
369
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.
379 ;;
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)))))
395
396
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.
400
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).
405
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.
409
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)
455
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))))
473
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').
479
480 Second arg FILE-NAME is file name of the change log.
481 If nil, use the value of `change-log-default-name'.
482
483 Third arg OTHER-WINDOW non-nil means visit in other window.
484
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.
488
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.
492
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.
496
497 Today's date is calculated according to `add-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)))
514
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))))
524
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))
533
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"))
539
540 ;; Advance into first entry if it is usable; else make new one.
541 (let ((new-entries
542 (mapcar (lambda (addr)
543 (concat
544 (if (stringp add-log-time-zone-rule)
545 (let ((tz (getenv "TZ")))
546 (unwind-protect
547 (progn
548 (set-time-zone-rule add-log-time-zone-rule)
549 (funcall add-log-time-format))
550 (set-time-zone-rule tz)))
551 (funcall add-log-time-format))
552 " " full-name
553 " <" addr ">"))
554 (if (consp mailing-address)
555 mailing-address
556 (list mailing-address)))))
557 (if (and (not add-log-always-start-new-record)
558 (let ((hit nil))
559 (dolist (entry new-entries hit)
560 (when (looking-at (regexp-quote entry))
561 (setq hit t)))))
562 (forward-line 1)
563 (insert (nth (random (length new-entries))
564 new-entries)
565 (if use-hard-newlines hard-newline "\n")
566 (if use-hard-newlines hard-newline "\n"))
567 (forward-line -1)))
568
569 ;; Determine where we should stop searching for a usable
570 ;; item to add to, within this entry.
571 (setq bound
572 (save-excursion
573 (if (looking-at "\n*[^\n* \t]")
574 (skip-chars-forward "\n")
575 (if add-log-keep-changes-together
576 (forward-page) ; page delimits entries for date
577 (forward-paragraph))) ; paragraph delimits entries for file
578 (point)))
579
580 ;; Now insert the new line for this item.
581 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t)
582 ;; Put this file name into the existing empty item.
583 (if item
584 (insert item)))
585 ((and (not new-entry)
586 (let (case-fold-search)
587 (re-search-forward
588 (concat (regexp-quote (concat "* " item))
589 ;; Don't accept `foo.bar' when
590 ;; looking for `foo':
591 "\\(\\s \\|[(),:]\\)")
592 bound t)))
593 ;; Add to the existing item for the same file.
594 (re-search-forward "^\\s *$\\|^\\s \\*")
595 (goto-char (match-beginning 0))
596 ;; Delete excess empty lines; make just 2.
597 (while (and (not (eobp)) (looking-at "^\\s *$"))
598 (delete-region (point) (line-beginning-position 2)))
599 (insert (if use-hard-newlines hard-newline "\n")
600 (if use-hard-newlines hard-newline "\n"))
601 (forward-line -2)
602 (indent-relative-maybe))
603 (t
604 ;; Make a new item.
605 (while (looking-at "\\sW")
606 (forward-line 1))
607 (while (and (not (eobp)) (looking-at "^\\s *$"))
608 (delete-region (point) (line-beginning-position 2)))
609 (insert (if use-hard-newlines hard-newline "\n")
610 (if use-hard-newlines hard-newline "\n")
611 (if use-hard-newlines hard-newline "\n"))
612 (forward-line -2)
613 (indent-to left-margin)
614 (insert "* ")
615 (if item (insert item))))
616 ;; Now insert the function name, if we have one.
617 ;; Point is at the item for this file,
618 ;; either at the end of the line or at the first blank line.
619 (if (not defun)
620 ;; No function name, so put in a colon unless we have just a star.
621 (unless (save-excursion
622 (beginning-of-line 1)
623 (looking-at "\\s *\\(\\*\\s *\\)?$"))
624 (insert ": ")
625 (if version (insert version ?\s)))
626 ;; Make it easy to get rid of the function name.
627 (undo-boundary)
628 (unless (save-excursion
629 (beginning-of-line 1)
630 (looking-at "\\s *$"))
631 (insert ?\s))
632 ;; See if the prev function name has a message yet or not.
633 ;; If not, merge the two items.
634 (let ((pos (point-marker)))
635 (skip-syntax-backward " ")
636 (skip-chars-backward "):")
637 (if (and (looking-at "):")
638 (let ((pos (save-excursion (backward-sexp 1) (point))))
639 (when (equal (buffer-substring pos (point)) defun)
640 (delete-region pos (point)))
641 (> fill-column (+ (current-column) (length defun) 4))))
642 (progn (skip-chars-backward ", ")
643 (delete-region (point) pos)
644 (unless (memq (char-before) '(?\()) (insert ", ")))
645 (if (looking-at "):")
646 (delete-region (+ 1 (point)) (line-end-position)))
647 (goto-char pos)
648 (insert "("))
649 (set-marker pos nil))
650 (insert defun "): ")
651 (if version (insert version ?\s)))))
652
653 ;;;###autoload
654 (defun add-change-log-entry-other-window (&optional whoami file-name)
655 "Find change log file in other window and add entry and item.
656 This is just like `add-change-log-entry' except that it displays
657 the change log file in another window."
658 (interactive (if current-prefix-arg
659 (list current-prefix-arg
660 (prompt-for-change-log-name))))
661 (add-change-log-entry whoami file-name t))
662 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
663
664 (defvar change-log-indent-text 0)
665
666 (defun change-log-indent ()
667 (let* ((indent
668 (save-excursion
669 (beginning-of-line)
670 (skip-chars-forward " \t")
671 (cond
672 ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
673 ;; Matching the output of add-log-time-format is difficult,
674 ;; but I'll get it has at least two adjacent digits.
675 (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
676 0)
677 ((looking-at "[^*(]")
678 (+ (current-left-margin) change-log-indent-text))
679 (t (current-left-margin)))))
680 (pos (save-excursion (indent-line-to indent) (point))))
681 (if (> pos (point)) (goto-char pos))))
682
683
684 (defvar smerge-resolve-function)
685
686 ;;;###autoload
687 (define-derived-mode change-log-mode text-mode "Change Log"
688 "Major mode for editing change logs; like Indented Text Mode.
689 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
690 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
691 Each entry behaves as a paragraph, and the entries for one day as a page.
692 Runs `change-log-mode-hook'.
693 \\{change-log-mode-map}"
694 (setq left-margin 8
695 fill-column 74
696 indent-tabs-mode t
697 tab-width 8)
698 (set (make-local-variable 'fill-paragraph-function)
699 'change-log-fill-paragraph)
700 (set (make-local-variable 'indent-line-function) 'change-log-indent)
701 (set (make-local-variable 'tab-always-indent) nil)
702 ;; We really do want "^" in paragraph-start below: it is only the
703 ;; lines that begin at column 0 (despite the left-margin of 8) that
704 ;; we are looking for. Adding `* ' allows eliding the blank line
705 ;; between entries for different files.
706 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
707 (set (make-local-variable 'paragraph-separate) paragraph-start)
708 ;; Match null string on the date-line so that the date-line
709 ;; is grouped with what follows.
710 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
711 (set (make-local-variable 'version-control) 'never)
712 (set (make-local-variable 'smerge-resolve-function)
713 'change-log-resolve-conflict)
714 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
715 (set (make-local-variable 'font-lock-defaults)
716 '(change-log-font-lock-keywords t nil nil backward-paragraph)))
717
718 ;; It might be nice to have a general feature to replace this. The idea I
719 ;; have is a variable giving a regexp matching text which should not be
720 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
721 ;; But I don't feel up to implementing that today.
722 (defun change-log-fill-paragraph (&optional justify)
723 "Fill the paragraph, but preserve open parentheses at beginning of lines.
724 Prefix arg means justify as well."
725 (interactive "P")
726 (let ((end (progn (forward-paragraph) (point)))
727 (beg (progn (backward-paragraph) (point)))
728 (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
729 (fill-region beg end justify)
730 t))
731 \f
732 (defcustom add-log-current-defun-header-regexp
733 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
734 "Heuristic regexp used by `add-log-current-defun' for unknown major modes."
735 :type 'regexp
736 :group 'change-log)
737
738 ;;;###autoload
739 (defvar add-log-lisp-like-modes
740 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode)
741 "*Modes that look like Lisp to `add-log-current-defun'.")
742
743 ;;;###autoload
744 (defvar add-log-c-like-modes
745 '(c-mode c++-mode c++-c-mode objc-mode)
746 "*Modes that look like C to `add-log-current-defun'.")
747
748 ;;;###autoload
749 (defvar add-log-tex-like-modes
750 '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode)
751 "*Modes that look like TeX to `add-log-current-defun'.")
752
753 ;;;###autoload
754 (defun add-log-current-defun ()
755 "Return name of function definition point is in, or nil.
756
757 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
758 Texinfo (@node titles) and Perl.
759
760 Other modes are handled by a heuristic that looks in the 10K before
761 point for uppercase headings starting in the first column or
762 identifiers followed by `:' or `='. See variables
763 `add-log-current-defun-header-regexp' and
764 `add-log-current-defun-function'.
765
766 Has a preference of looking backwards."
767 (condition-case nil
768 (save-excursion
769 (let ((location (point)))
770 (cond (add-log-current-defun-function
771 (funcall add-log-current-defun-function))
772 ((memq major-mode add-log-lisp-like-modes)
773 ;; If we are now precisely at the beginning of a defun,
774 ;; make sure beginning-of-defun finds that one
775 ;; rather than the previous one.
776 (or (eobp) (forward-char 1))
777 (beginning-of-defun)
778 ;; Make sure we are really inside the defun found,
779 ;; not after it.
780 (when (and (looking-at "\\s(")
781 (progn (end-of-defun)
782 (< location (point)))
783 (progn (forward-sexp -1)
784 (>= location (point))))
785 (if (looking-at "\\s(")
786 (forward-char 1))
787 ;; Skip the defining construct name, typically "defun"
788 ;; or "defvar".
789 (forward-sexp 1)
790 ;; The second element is usually a symbol being defined.
791 ;; If it is not, use the first symbol in it.
792 (skip-chars-forward " \t\n'(")
793 (buffer-substring-no-properties (point)
794 (progn (forward-sexp 1)
795 (point)))))
796 ((and (memq major-mode add-log-c-like-modes)
797 (save-excursion
798 (beginning-of-line)
799 ;; Use eq instead of = here to avoid
800 ;; error when at bob and char-after
801 ;; returns nil.
802 (while (eq (char-after (- (point) 2)) ?\\)
803 (forward-line -1))
804 (looking-at "[ \t]*#[ \t]*define[ \t]")))
805 ;; Handle a C macro definition.
806 (beginning-of-line)
807 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
808 (forward-line -1))
809 (search-forward "define")
810 (skip-chars-forward " \t")
811 (buffer-substring-no-properties (point)
812 (progn (forward-sexp 1)
813 (point))))
814 ((memq major-mode add-log-c-like-modes)
815 (beginning-of-line)
816 ;; See if we are in the beginning part of a function,
817 ;; before the open brace. If so, advance forward.
818 (while (not (looking-at "{\\|\\(\\s *$\\)"))
819 (forward-line 1))
820 (or (eobp)
821 (forward-char 1))
822 (let (maybe-beg)
823 ;; Try to find the containing defun.
824 (beginning-of-defun)
825 (end-of-defun)
826 ;; If the defun we found ends before the desired position,
827 ;; see if there's a DEFUN construct
828 ;; between that end and the desired position.
829 (when (save-excursion
830 (and (> location (point))
831 (re-search-forward "^DEFUN"
832 (save-excursion
833 (goto-char location)
834 (line-end-position))
835 t)
836 (re-search-forward "^{" nil t)
837 (setq maybe-beg (point))))
838 ;; If so, go to the end of that instead.
839 (goto-char maybe-beg)
840 (end-of-defun)))
841 ;; If the desired position is within the defun we found,
842 ;; find the function name.
843 (when (< location (point))
844 ;; Move back over function body.
845 (backward-sexp 1)
846 (let (beg)
847 ;; Skip back over typedefs and arglist.
848 ;; Stop at the function definition itself
849 ;; or at the line that follows end of function doc string.
850 (forward-line -1)
851 (while (and (not (bobp))
852 (looking-at "[ \t\n]")
853 (not (looking-back "[*]/)\n" (- (point) 4))))
854 (forward-line -1))
855 ;; If we found a doc string, this must be the DEFUN macro
856 ;; used in Emacs. Move back to the DEFUN line.
857 (when (looking-back "[*]/)\n" (- (point) 4))
858 (backward-sexp 1)
859 (beginning-of-line))
860 ;; Is this a DEFUN construct? And is LOCATION in it?
861 (if (and (looking-at "DEFUN\\b")
862 (>= location (point)))
863 ;; DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, ...) ==> Ffile_name_directory
864 ;; DEFUN(POSIX::STREAM-LOCK, stream lockp &key BLOCK SHARED START LENGTH) ==> POSIX::STREAM-LOCK
865 (progn
866 (down-list 1)
867 (when (= (char-after (point)) ?\")
868 (forward-sexp 1)
869 (search-forward ","))
870 (skip-syntax-forward " ")
871 (buffer-substring-no-properties
872 (point)
873 (progn (search-forward ",")
874 (forward-char -1)
875 (skip-syntax-backward " ")
876 (point))))
877 (if (looking-at "^[+-]")
878 ;; Objective-C
879 (change-log-get-method-definition)
880 ;; Ordinary C function syntax.
881 (setq beg (point))
882 (if (and
883 ;; Protect against "Unbalanced parens" error.
884 (condition-case nil
885 (progn
886 (down-list 1) ; into arglist
887 (backward-up-list 1)
888 (skip-chars-backward " \t")
889 t)
890 (error nil))
891 ;; Verify initial pos was after
892 ;; real start of function.
893 (save-excursion
894 (goto-char beg)
895 ;; For this purpose, include the line
896 ;; that has the decl keywords. This
897 ;; may also include some of the
898 ;; comments before the function.
899 (while (and (not (bobp))
900 (save-excursion
901 (forward-line -1)
902 (looking-at "[^\n\f]")))
903 (forward-line -1))
904 (>= location (point)))
905 ;; Consistency check: going down and up
906 ;; shouldn't take us back before BEG.
907 (> (point) beg))
908 (let (end middle)
909 ;; Don't include any final whitespace
910 ;; in the name we use.
911 (skip-chars-backward " \t\n")
912 (setq end (point))
913 (backward-sexp 1)
914 ;; Now find the right beginning of the name.
915 ;; Include certain keywords if they
916 ;; precede the name.
917 (setq middle (point))
918 (forward-word -1)
919 ;; Is this C++ method?
920 (when (and (< 2 middle)
921 (string= (buffer-substring (- middle 2)
922 middle)
923 "::"))
924 ;; Include "classname::".
925 (setq middle (point)))
926 ;; Ignore these subparts of a class decl
927 ;; and move back to the class name itself.
928 (while (looking-at "public \\|private ")
929 (skip-chars-backward " \t:")
930 (setq end (point))
931 (backward-sexp 1)
932 (setq middle (point))
933 (forward-word -1))
934 (and (bolp)
935 (looking-at
936 "enum \\|struct \\|union \\|class ")
937 (setq middle (point)))
938 (goto-char end)
939 (when (eq (preceding-char) ?=)
940 (forward-char -1)
941 (skip-chars-backward " \t")
942 (setq end (point)))
943 (buffer-substring-no-properties
944 middle end))))))))
945 ((memq major-mode add-log-tex-like-modes)
946 (if (re-search-backward
947 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
948 nil t)
949 (progn
950 (goto-char (match-beginning 0))
951 (buffer-substring-no-properties
952 (1+ (point)) ; without initial backslash
953 (line-end-position)))))
954 ((eq major-mode 'texinfo-mode)
955 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
956 (match-string-no-properties 1)))
957 ((memq major-mode '(perl-mode cperl-mode))
958 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t)
959 (match-string-no-properties 1)))
960 ;; Emacs's autoconf-mode installs its own
961 ;; `add-log-current-defun-function'. This applies to
962 ;; a different mode apparently for editing .m4
963 ;; autoconf source.
964 ((eq major-mode 'autoconf-mode)
965 (if (re-search-backward
966 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t)
967 (match-string-no-properties 3)))
968 (t
969 ;; If all else fails, try heuristics
970 (let (case-fold-search
971 result)
972 (end-of-line)
973 (when (re-search-backward
974 add-log-current-defun-header-regexp
975 (- (point) 10000)
976 t)
977 (setq result (or (match-string-no-properties 1)
978 (match-string-no-properties 0)))
979 ;; Strip whitespace away
980 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
981 result)
982 (setq result (match-string-no-properties 1 result)))
983 result))))))
984 (error nil)))
985
986 (defvar change-log-get-method-definition-md)
987
988 ;; Subroutine used within change-log-get-method-definition.
989 ;; Add the last match in the buffer to the end of `md',
990 ;; followed by the string END; move to the end of that match.
991 (defun change-log-get-method-definition-1 (end)
992 (setq change-log-get-method-definition-md
993 (concat change-log-get-method-definition-md
994 (match-string 1)
995 end))
996 (goto-char (match-end 0)))
997
998 (defun change-log-get-method-definition ()
999 "For Objective C, return the method name if we are in a method."
1000 (let ((change-log-get-method-definition-md "["))
1001 (save-excursion
1002 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
1003 (change-log-get-method-definition-1 " ")))
1004 (save-excursion
1005 (cond
1006 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
1007 (change-log-get-method-definition-1 "")
1008 (while (not (looking-at "[{;]"))
1009 (looking-at
1010 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
1011 (change-log-get-method-definition-1 ""))
1012 (concat change-log-get-method-definition-md "]"))))))
1013 \f
1014 (defun change-log-sortable-date-at ()
1015 "Return date of log entry in a consistent form for sorting.
1016 Point is assumed to be at the start of the entry."
1017 (require 'timezone)
1018 (if (looking-at "^\\sw.........[0-9:+ ]*")
1019 (let ((date (match-string-no-properties 0)))
1020 (if date
1021 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
1022 (concat (match-string 1 date) (match-string 2 date)
1023 (match-string 3 date))
1024 (condition-case nil
1025 (timezone-make-date-sortable date)
1026 (error nil)))))
1027 (error "Bad date")))
1028
1029 (defun change-log-resolve-conflict ()
1030 "Function to be used in `smerge-resolve-function'."
1031 (let ((buf (current-buffer)))
1032 (with-temp-buffer
1033 (insert-buffer-substring buf (match-beginning 1) (match-end 1))
1034 (save-match-data (change-log-mode))
1035 (let ((other-buf (current-buffer)))
1036 (with-current-buffer buf
1037 (save-excursion
1038 (save-restriction
1039 (narrow-to-region (match-beginning 0) (match-end 0))
1040 (replace-match (match-string 3) t t)
1041 (change-log-merge other-buf))))))))
1042
1043 ;;;###autoload
1044 (defun change-log-merge (other-log)
1045 "Merge the contents of change log file OTHER-LOG with this buffer.
1046 Both must be found in Change Log mode (since the merging depends on
1047 the appropriate motion commands). OTHER-LOG can be either a file name
1048 or a buffer.
1049
1050 Entries are inserted in chronological order. Both the current and
1051 old-style time formats for entries are supported."
1052 (interactive "*fLog file name to merge: ")
1053 (if (not (eq major-mode 'change-log-mode))
1054 (error "Not in Change Log mode"))
1055 (let ((other-buf (if (bufferp other-log) other-log
1056 (find-file-noselect other-log)))
1057 (buf (current-buffer))
1058 date1 start end)
1059 (save-excursion
1060 (goto-char (point-min))
1061 (set-buffer other-buf)
1062 (goto-char (point-min))
1063 (if (not (eq major-mode 'change-log-mode))
1064 (error "%s not found in Change Log mode" other-log))
1065 ;; Loop through all the entries in OTHER-LOG.
1066 (while (not (eobp))
1067 (setq date1 (change-log-sortable-date-at))
1068 (setq start (point)
1069 end (progn (forward-page) (point)))
1070 ;; Look for an entry in original buffer that isn't later.
1071 (with-current-buffer buf
1072 (while (and (not (eobp))
1073 (string< date1 (change-log-sortable-date-at)))
1074 (forward-page))
1075 (if (not (eobp))
1076 (insert-buffer-substring other-buf start end)
1077 ;; At the end of the original buffer, insert a newline to
1078 ;; separate entries and then the rest of the file being
1079 ;; merged.
1080 (unless (or (bobp)
1081 (and (= ?\n (char-before))
1082 (or (<= (1- (point)) (point-min))
1083 (= ?\n (char-before (1- (point)))))))
1084 (insert (if use-hard-newlines hard-newline "\n")))
1085 ;; Move to the end of it to terminate outer loop.
1086 (with-current-buffer other-buf
1087 (goto-char (point-max)))
1088 (insert-buffer-substring other-buf start)))))))
1089
1090 ;;;###autoload
1091 (defun change-log-redate ()
1092 "Fix any old-style date entries in the current log file to default format."
1093 (interactive)
1094 (require 'timezone)
1095 (save-excursion
1096 (goto-char (point-min))
1097 (while (re-search-forward "^\\sw.........[0-9:+ ]*" nil t)
1098 (unless (= 12 (- (match-end 0) (match-beginning 0)))
1099 (let* ((date (save-match-data
1100 (timezone-fix-time (match-string 0) nil nil)))
1101 (zone (if (consp (aref date 6))
1102 (nth 1 (aref date 6)))))
1103 (replace-match (format-time-string
1104 "%Y-%m-%d "
1105 (encode-time (aref date 5)
1106 (aref date 4)
1107 (aref date 3)
1108 (aref date 2)
1109 (aref date 1)
1110 (aref date 0)
1111 zone))))))))
1112
1113 (provide 'add-log)
1114
1115 ;; arch-tag: 81eee6fc-088f-4372-a37f-80ad9620e762
1116 ;;; add-log.el ends here