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