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