*** empty log message ***
[bpt/emacs.git] / lisp / add-log.el
CommitLineData
5abdc915 1;;; add-log.el --- change log maintenance commands for Emacs
84fc2cfa 2
5960adc7 3;; Copyright (C) 1985, 86, 88, 93, 94, 97, 98, 2000 Free Software Foundation, Inc.
84fc2cfa 4
6228c05b 5;; Maintainer: FSF
df63ae66 6;; Keywords: tools
e9571d2a 7
84fc2cfa
ER
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
fd7fa35a 12;; the Free Software Foundation; either version 2, or (at your option)
84fc2cfa
ER
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
84fc2cfa 24
e41b2db1
ER
25;;; Commentary:
26
27;; This facility is documented in the Emacs Manual.
28
fd7fa35a 29;;; Code:
84fc2cfa 30
776d8e16 31(eval-when-compile
5960adc7 32 (require 'timezone))
3697b807 33
fcad5199
RS
34(defgroup change-log nil
35 "Change log maintenance"
36 :group 'tools
3697b807 37 :link '(custom-manual "(emacs)Change Log")
fcad5199
RS
38 :prefix "change-log-"
39 :prefix "add-log-")
84fc2cfa 40
fcad5199
RS
41
42(defcustom change-log-default-name nil
43 "*Name of a change log file for \\[add-change-log-entry]."
44 :type '(choice (const :tag "default" nil)
45 string)
46 :group 'change-log)
47
c3979f12
DL
48(defcustom change-log-mode-hook nil
49 "Normal hook run by `change-log-mode'."
50 :type 'hook
51 :group 'change-log)
52
fcad5199 53(defcustom add-log-current-defun-function nil
5960adc7
DL
54 "*If non-nil, function to guess name of surrounding function.
55It is used by `add-log-current-defun' in preference to built-in rules.
56Returns function's name as a string, or nil if outside a function."
cea3855a 57 :type '(choice (const nil) function)
fcad5199 58 :group 'change-log)
6258d3af 59
29db528b 60;;;###autoload
fcad5199 61(defcustom add-log-full-name nil
02ec1592 62 "*Full name of user, for inclusion in ChangeLog daily headers.
075a6629 63This defaults to the value returned by the function `user-full-name'."
fcad5199
RS
64 :type '(choice (const :tag "Default" nil)
65 string)
66 :group 'change-log)
02ec1592 67
29db528b 68;;;###autoload
fcad5199 69(defcustom add-log-mailing-address nil
02ec1592 70 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
fcad5199
RS
71This defaults to the value of `user-mail-address'."
72 :type '(choice (const :tag "Default" nil)
73 string)
74 :group 'change-log)
75
df63ae66
RS
76(defcustom add-log-time-format 'add-log-iso8601-time-string
77 "*Function that defines the time format.
78For example, `add-log-iso8601-time-string', which gives the
79date in international ISO 8601 format,
80and `current-time-string' are two valid values."
81 :type '(radio (const :tag "International ISO 8601 format"
82 add-log-iso8601-time-string)
83 (const :tag "Old format, as returned by `current-time-string'"
84 current-time-string)
85 (function :tag "Other"))
86 :group 'change-log)
02ec1592 87
83afd62c 88(defcustom add-log-keep-changes-together nil
3697b807
DL
89 "*If non-nil, normally keep day's log entries for one file together.
90
91Log entries for a given file made with \\[add-change-log-entry] or
92\\[add-change-log-entry-other-window] will only be added to others \
93for that file made
94today if this variable is non-nil or that file comes first in today's
95entries. Otherwise another entry for that file will be started. An
96original log:
97
98 * foo (...): ...
99 * bar (...): change 1
83afd62c 100
3697b807
DL
101in the latter case, \\[add-change-log-entry-other-window] in a \
102buffer visiting `bar', yields:
83afd62c 103
3697b807
DL
104 * bar (...): -!-
105 * foo (...): ...
106 * bar (...): change 1
83afd62c 107
3697b807 108and in the former:
83afd62c 109
3697b807
DL
110 * foo (...): ...
111 * bar (...): change 1
112 (...): -!-
83afd62c 113
3697b807
DL
114The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
115this variable."
116 :version "20.3"
83afd62c
KH
117 :type 'boolean
118 :group 'change-log)
119
598f34fa
SS
120(defcustom add-log-always-start-new-record nil
121 "*If non-nil, `add-change-log-entry' will always start a new record."
89e7ad59 122 :version "21.4"
598f34fa
SS
123 :type 'boolean
124 :group 'change-log)
125
d68f7f1b
SM
126(defcustom add-log-buffer-file-name-function nil
127 "*If non-nil, function to call to identify the full filename of a buffer.
128This function is called with no argument. If this is nil, the default is to
129use `buffer-file-name'."
cea3855a 130 :type '(choice (const nil) function)
d68f7f1b
SM
131 :group 'change-log)
132
666f4056
RS
133(defcustom add-log-file-name-function nil
134 "*If non-nil, function to call to identify the filename for a ChangeLog entry.
075a6629
DL
135This function is called with one argument, the value of variable
136`buffer-file-name' in that buffer. If this is nil, the default is to
137use the file's name relative to the directory of the change log file."
cea3855a 138 :type '(choice (const nil) function)
666f4056
RS
139 :group 'change-log)
140
776d8e16
GM
141
142(defcustom change-log-version-info-enabled nil
143 "*If non-nil, enable recording version numbers with the changes."
144 :version "21.1"
145 :type 'boolean
146 :group 'change-log)
147
148(defcustom change-log-version-number-regexp-list
149 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
150 (list
151 ;; (defconst ad-version "2.15"
152 (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re)
153 ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
5960adc7 154 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re)))
776d8e16 155 "*List of regexps to search for version number.
5960adc7 156The version number must be in group 1.
776d8e16
GM
157Note: The search is conducted only within 10%, at the beginning of the file."
158 :version "21.1"
159 :type '(repeat regexp)
160 :group 'change-log)
161
163f7b71
GM
162(defface change-log-date-face
163 '((t (:inherit font-lock-string-face)))
164 "Face used to highlight dates in date lines."
165 :version "21.1"
166 :group 'change-log)
167
168(defface change-log-name-face
169 '((t (:inherit font-lock-constant-face)))
170 "Face for highlighting author names."
171 :version "21.1"
172 :group 'change-log)
173
174(defface change-log-email-face
175 '((t (:inherit font-lock-variable-name-face)))
176 "Face for highlighting author email addresses."
177 :version "21.1"
178 :group 'change-log)
179
180(defface change-log-file-face
181 '((t (:inherit font-lock-function-name-face)))
182 "Face for highlighting file names."
183 :version "21.1"
184 :group 'change-log)
185
186(defface change-log-list-face
187 '((t (:inherit font-lock-keyword-face)))
188 "Face for highlighting parenthesized lists of functions or variables."
189 :version "21.1"
190 :group 'change-log)
598f34fa 191
163f7b71
GM
192(defface change-log-conditionals-face
193 '((t (:inherit font-lock-variable-name-face)))
194 "Face for highlighting conditionals of the form `[...]'."
195 :version "21.1"
196 :group 'change-log)
197
198(defface change-log-function-face
199 '((t (:inherit font-lock-variable-name-face)))
200 "Face for highlighting items of the form `<....>'."
201 :version "21.1"
202 :group 'change-log)
203
204(defface change-log-acknowledgement-face
205 '((t (:inherit font-lock-comment-face)))
206 "Face for highlighting acknowledgments."
207 :version "21.1"
208 :group 'change-log)
776d8e16 209
5f562719 210(defvar change-log-font-lock-keywords
5572c1d1
SM
211 '(;;
212 ;; Date lines, new and old styles.
3307b0ca 213 ("^\\sw.........[0-9:+ ]*"
163f7b71 214 (0 'change-log-date-face)
97a3278b 215 ;; Name and e-mail; some people put e-mail in parens, not angles.
8f4ca9a5 216 ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
163f7b71
GM
217 (1 'change-log-name-face)
218 (2 'change-log-email-face)))
5572c1d1
SM
219 ;;
220 ;; File names.
221 ("^\t\\* \\([^ ,:([\n]+\\)"
163f7b71 222 (1 'change-log-file-face)
97a3278b 223 ;; Possibly further names in a list:
163f7b71 224 ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 'change-log-file-face))
97a3278b 225 ;; Possibly a parenthesized list of names:
163f7b71
GM
226 ("\\= (\\([^) ,:\n]+\\)" nil nil (1 'change-log-list-face))
227 ("\\=, *\\([^) ,:\n]+\\)" nil nil (1 'change-log-list-face)))
5572c1d1
SM
228 ;;
229 ;; Function or variable names.
97a3278b 230 ("^\t(\\([^) ,:\n]+\\)"
163f7b71
GM
231 (1 'change-log-list-face)
232 ("\\=, *\\([^) ,:\n]+\\)" nil nil (1 'change-log-list-face)))
5572c1d1
SM
233 ;;
234 ;; Conditionals.
163f7b71 235 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals-face))
5572c1d1 236 ;;
a8d693d8 237 ;; Function of change.
163f7b71 238 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function-face))
a8d693d8 239 ;;
a3cab9f0 240 ;; Acknowledgements.
a8d693d8 241 ("\\(^\t\\| \\)\\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
163f7b71 242 2 'change-log-acknowledgement-face))
5f562719
RS
243 "Additional expressions to highlight in Change Log mode.")
244
5960adc7 245(defvar change-log-mode-map (make-sparse-keymap)
45c50c5d 246 "Keymap for Change Log major mode.")
45c50c5d 247
51bd1843
EN
248(defvar change-log-time-zone-rule nil
249 "Time zone used for calculating change log time stamps.
250It takes the same format as the TZ argument of `set-time-zone-rule'.
251If nil, use local time.")
252
df63ae66 253(defun add-log-iso8601-time-zone (time)
51bd1843
EN
254 (let* ((utc-offset (or (car (current-time-zone time)) 0))
255 (sign (if (< utc-offset 0) ?- ?+))
256 (sec (abs utc-offset))
257 (ss (% sec 60))
258 (min (/ sec 60))
259 (mm (% min 60))
260 (hh (/ min 60)))
261 (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
262 ((not (zerop mm)) "%c%02d:%02d")
263 (t "%c%02d"))
264 sign hh mm ss)))
265
df63ae66
RS
266(defun add-log-iso8601-time-string ()
267 (if change-log-time-zone-rule
268 (let ((tz (getenv "TZ"))
269 (now (current-time)))
270 (unwind-protect
271 (progn
918f4ac3 272 (set-time-zone-rule change-log-time-zone-rule)
df63ae66
RS
273 (concat
274 (format-time-string "%Y-%m-%d " now)
275 (add-log-iso8601-time-zone now)))
276 (set-time-zone-rule tz)))
277 (format-time-string "%Y-%m-%d")))
278
84fc2cfa 279(defun change-log-name ()
075a6629 280 "Return (system-dependent) default name for a change log file."
84fc2cfa 281 (or change-log-default-name
513063cf 282 (if (eq system-type 'vax-vms)
4a047d23
RS
283 "$CHANGE_LOG$.TXT"
284 "ChangeLog")))
84fc2cfa 285
287d149f 286;;;###autoload
84fc2cfa
ER
287(defun prompt-for-change-log-name ()
288 "Prompt for a change log name."
117aaf60
KH
289 (let* ((default (change-log-name))
290 (name (expand-file-name
291 (read-file-name (format "Log file (default %s): " default)
292 nil default))))
293 ;; Handle something that is syntactically a directory name.
294 ;; Look for ChangeLog or whatever in that directory.
295 (if (string= (file-name-nondirectory name) "")
296 (expand-file-name (file-name-nondirectory default)
297 name)
298 ;; Handle specifying a file that is a directory.
299 (if (file-directory-p name)
300 (expand-file-name (file-name-nondirectory default)
301 (file-name-as-directory name))
302 name))))
84fc2cfa 303
776d8e16 304(defun change-log-version-number-search ()
5960adc7
DL
305 "Return version number of current buffer's file.
306This is the value returned by `vc-workfile-version' or, if that is
307nil, by matching `change-log-version-number-regexp-list'."
776d8e16
GM
308 (let* ((size (buffer-size))
309 (end
5960adc7
DL
310 ;; The version number can be anywhere in the file, but
311 ;; restrict search to the file beginning: 10% should be
312 ;; enough to prevent some mishits.
776d8e16 313 ;;
5960adc7
DL
314 ;; Apply percentage only if buffer size is bigger than
315 ;; approx 100 lines.
776d8e16 316 (if (> size (* 100 80))
5960adc7 317 (/ size 10)
776d8e16
GM
318 size))
319 version)
d68f7f1b 320 (or (and buffer-file-name (vc-workfile-version buffer-file-name))
5960adc7
DL
321 (save-restriction
322 (widen)
323 (let ((regexps change-log-version-number-regexp-list))
324 (while regexps
325 (save-excursion
326 (goto-char (point-min))
327 (when (re-search-forward (pop regexps) end t)
328 (setq version (match-string 1)
329 regexps nil)))))))))
776d8e16
GM
330
331
45a13f0d 332;;;###autoload
d68f7f1b 333(defun find-change-log (&optional file-name buffer-file)
45a13f0d 334 "Find a change log file for \\[add-change-log-entry] and return the name.
a82e2ed5
RS
335
336Optional arg FILE-NAME specifies the file to use.
de98fcaf
RS
337If FILE-NAME is nil, use the value of `change-log-default-name'.
338If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
339\(or whatever we use on this operating system).
340
341If 'change-log-default-name' contains a leading directory component, then
513063cf 342simply find it in the current directory. Otherwise, search in the current
de98fcaf 343directory and its successive parents for a file so named.
45a13f0d
RM
344
345Once a file is found, `change-log-default-name' is set locally in the
d68f7f1b
SM
346current buffer to the complete file name.
347Optional arg BUFFER-FILE overrides `buffer-file-name'."
a82e2ed5
RS
348 ;; If user specified a file name or if this buffer knows which one to use,
349 ;; just use that.
45a13f0d 350 (or file-name
de98fcaf
RS
351 (setq file-name (and change-log-default-name
352 (file-name-directory change-log-default-name)
353 change-log-default-name))
a82e2ed5
RS
354 (progn
355 ;; Chase links in the source file
356 ;; and use the change log in the dir where it points.
d68f7f1b 357 (setq file-name (or (and (or buffer-file buffer-file-name)
a82e2ed5 358 (file-name-directory
d68f7f1b
SM
359 (file-chase-links
360 (or buffer-file buffer-file-name))))
a82e2ed5
RS
361 default-directory))
362 (if (file-directory-p file-name)
363 (setq file-name (expand-file-name (change-log-name) file-name)))
364 ;; Chase links before visiting the file.
365 ;; This makes it easier to use a single change log file
366 ;; for several related directories.
367 (setq file-name (file-chase-links file-name))
368 (setq file-name (expand-file-name file-name))
369 ;; Move up in the dir hierarchy till we find a change log file.
370 (let ((file1 file-name)
371 parent-dir)
372 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
373 (progn (setq parent-dir
374 (file-name-directory
375 (directory-file-name
376 (file-name-directory file1))))
377 ;; Give up if we are already at the root dir.
378 (not (string= (file-name-directory file1)
379 parent-dir))))
380 ;; Move up to the parent dir and try again.
513063cf 381 (setq file1 (expand-file-name
a82e2ed5
RS
382 (file-name-nondirectory (change-log-name))
383 parent-dir)))
384 ;; If we found a change log in a parent, use that.
385 (if (or (get-file-buffer file1) (file-exists-p file1))
386 (setq file-name file1)))))
387 ;; Make a local variable in this buffer so we needn't search again.
388 (set (make-local-variable 'change-log-default-name) file-name)
389 file-name)
45a13f0d 390
2eb7ccf4
SM
391(defun add-log-file-name (buffer-file log-file)
392 ;; Never want to add a change log entry for the ChangeLog file itself.
393 (unless (or (null buffer-file) (string= buffer-file log-file))
d68f7f1b
SM
394 (if add-log-file-name-function
395 (funcall add-log-file-name-function buffer-file)
396 (setq buffer-file
397 (if (string-match
398 (concat "^" (regexp-quote (file-name-directory log-file)))
399 buffer-file)
400 (substring buffer-file (match-end 0))
401 (file-name-nondirectory buffer-file)))
402 ;; If we have a backup file, it's presumably because we're
403 ;; comparing old and new versions (e.g. for deleted
404 ;; functions) and we'll want to use the original name.
405 (if (backup-file-name-p buffer-file)
406 (file-name-sans-versions buffer-file)
407 buffer-file))))
2eb7ccf4 408
84fc2cfa 409;;;###autoload
287d149f 410(defun add-change-log-entry (&optional whoami file-name other-window new-entry)
d882f144 411 "Find change log file, and add an entry for today and an item for this file.
83afd62c
KH
412Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
413name and site.
414
d882f144
RS
415Second arg FILE-NAME is file name of the change log.
416If nil, use the value of `change-log-default-name'.
417
287d149f 418Third arg OTHER-WINDOW non-nil means visit in other window.
d882f144 419
287d149f 420Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
3697b807
DL
421never append to an existing entry. Option `add-log-keep-changes-together'
422otherwise affects whether a new entry is created.
423
598f34fa
SS
424Option `add-log-always-start-new-record' non-nil means always create a
425new record, even when the last record was made on the same date and by
426the same person.
427
d882f144
RS
428The change log file can start with a copyright notice and a copying
429permission notice. The first blank line indicates the end of these
430notices.
431
3697b807
DL
432Today's date is calculated according to `change-log-time-zone-rule' if
433non-nil, otherwise in local time."
84fc2cfa
ER
434 (interactive (list current-prefix-arg
435 (prompt-for-change-log-name)))
550d8777
RS
436 (or add-log-full-name
437 (setq add-log-full-name (user-full-name)))
438 (or add-log-mailing-address
439 (setq add-log-mailing-address user-mail-address))
02ec1592
BF
440 (if whoami
441 (progn
442 (setq add-log-full-name (read-input "Full name: " add-log-full-name))
84fc2cfa
ER
443 ;; Note that some sites have room and phone number fields in
444 ;; full name which look silly when inserted. Rather than do
445 ;; anything about that here, let user give prefix argument so that
446 ;; s/he can edit the full name field in prompter if s/he wants.
02ec1592
BF
447 (setq add-log-mailing-address
448 (read-input "Mailing address: " add-log-mailing-address))))
2eb7ccf4 449
d68f7f1b
SM
450 (let* ((defun (add-log-current-defun))
451 (version (and change-log-version-info-enabled
452 (change-log-version-number-search)))
8f530b95
SM
453 (buf-file-name (if add-log-buffer-file-name-function
454 (funcall add-log-buffer-file-name-function)
455 buffer-file-name))
456 (buffer-file (if buf-file-name (expand-file-name buf-file-name)))
d68f7f1b
SM
457 (file-name (expand-file-name
458 (or file-name (find-change-log file-name buffer-file))))
d882f144
RS
459 ;; Set ITEM to the file name to use in the new item.
460 (item (add-log-file-name buffer-file file-name))
d68f7f1b 461 bound)
2eb7ccf4
SM
462
463 (if (or (and other-window (not (equal file-name buffer-file-name)))
464 (window-dedicated-p (selected-window)))
84fc2cfa
ER
465 (find-file-other-window file-name)
466 (find-file file-name))
675a998f
RS
467 (or (eq major-mode 'change-log-mode)
468 (change-log-mode))
84fc2cfa
ER
469 (undo-boundary)
470 (goto-char (point-min))
d882f144
RS
471
472 ;; If file starts with a copyright and permission notice, skip them.
473 ;; Assume they end at first blank line.
474 (when (looking-at "Copyright")
475 (search-forward "\n\n")
476 (skip-chars-forward "\n"))
477
478 ;; Advance into first entry if it is usable; else make new one.
df63ae66 479 (let ((new-entry (concat (funcall add-log-time-format)
51bd1843
EN
480 " " add-log-full-name
481 " <" add-log-mailing-address ">")))
598f34fa
SS
482 (if (and (not add-log-always-start-new-record)
483 (looking-at (regexp-quote new-entry)))
51bd1843 484 (forward-line 1)
d882f144
RS
485 (insert new-entry "\n\n")
486 (forward-line -1)))
82f4acaf 487
d882f144
RS
488 ;; Determine where we should stop searching for a usable
489 ;; item to add to, within this entry.
3697b807 490 (setq bound
d882f144 491 (save-excursion
3697b807
DL
492 (if (looking-at "\n*[^\n* \t]")
493 (skip-chars-forward "\n")
494 (if add-log-keep-changes-together
495 (forward-page) ; page delimits entries for date
496 (forward-paragraph))) ; paragraph delimits entries for file
497 (point)))
d882f144
RS
498
499 ;; Now insert the new line for this item.
3697b807 500 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t)
d882f144
RS
501 ;; Put this file name into the existing empty item.
502 (if item
503 (insert item)))
287d149f 504 ((and (not new-entry)
e172f546
KH
505 (let (case-fold-search)
506 (re-search-forward
d882f144 507 (concat (regexp-quote (concat "* " item))
e172f546
KH
508 ;; Don't accept `foo.bar' when
509 ;; looking for `foo':
510 "\\(\\s \\|[(),:]\\)")
3697b807 511 bound t)))
d882f144 512 ;; Add to the existing item for the same file.
82f4acaf 513 (re-search-forward "^\\s *$\\|^\\s \\*")
e172f546 514 (goto-char (match-beginning 0))
3697b807
DL
515 ;; Delete excess empty lines; make just 2.
516 (while (and (not (eobp)) (looking-at "^\\s *$"))
5960adc7 517 (delete-region (point) (line-beginning-position 2)))
918f4ac3 518 (insert-char ?\n 2)
3697b807
DL
519 (forward-line -2)
520 (indent-relative-maybe))
21d7e080 521 (t
d882f144 522 ;; Make a new item.
dd309224
RM
523 (while (looking-at "\\sW")
524 (forward-line 1))
09b389d0 525 (while (and (not (eobp)) (looking-at "^\\s *$"))
5960adc7 526 (delete-region (point) (line-beginning-position 2)))
918f4ac3 527 (insert-char ?\n 3)
1832dbd1 528 (forward-line -2)
dd309224 529 (indent-to left-margin)
918f4ac3 530 (insert "* ")
d882f144 531 (if item (insert item))))
1832dbd1 532 ;; Now insert the function name, if we have one.
d882f144 533 ;; Point is at the item for this file,
21d7e080
ER
534 ;; either at the end of the line or at the first blank line.
535 (if defun
536 (progn
dd309224 537 ;; Make it easy to get rid of the function name.
21d7e080 538 (undo-boundary)
5960adc7
DL
539 (unless (save-excursion
540 (beginning-of-line 1)
541 (looking-at "\\s *$"))
542 (insert ?\ ))
d882f144
RS
543 ;; See if the prev function name has a message yet or not.
544 ;; If not, merge the two items.
dc29aa6c
SM
545 (let ((pos (point-marker)))
546 (if (and (skip-syntax-backward " ")
547 (skip-chars-backward "):")
548 (looking-at "):")
549 (progn (delete-region (+ 1 (point)) (+ 2 (point))) t)
550 (> fill-column (+ (current-column) (length defun) 3)))
551 (progn (delete-region (point) pos)
552 (insert ", "))
553 (goto-char pos)
554 (insert "("))
555 (set-marker pos nil))
556 (insert defun "): ")
5960adc7
DL
557 (if version
558 (insert version ?\ )))
1832dbd1 559 ;; No function name, so put in a colon unless we have just a star.
5960adc7
DL
560 (unless (save-excursion
561 (beginning-of-line 1)
562 (looking-at "\\s *\\(\\*\\s *\\)?$"))
563 (insert ": ")
564 (if version (insert version ?\ ))))))
84fc2cfa 565
84fc2cfa
ER
566;;;###autoload
567(defun add-change-log-entry-other-window (&optional whoami file-name)
d882f144
RS
568 "Find change log file in other window and add entry and item.
569This is just like `add-change-log-entry' except that it displays
570the change log file in another window."
84fc2cfa
ER
571 (interactive (if current-prefix-arg
572 (list current-prefix-arg
573 (prompt-for-change-log-name))))
574 (add-change-log-entry whoami file-name t))
82f4acaf 575;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
84fc2cfa 576
1da56800 577;;;###autoload
21d7e080 578(defun change-log-mode ()
eb8c3be9 579 "Major mode for editing change logs; like Indented Text Mode.
09b389d0 580Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
3697b807 581New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
5516387c
RM
582Each entry behaves as a paragraph, and the entries for one day as a page.
583Runs `change-log-mode-hook'."
21d7e080
ER
584 (interactive)
585 (kill-all-local-variables)
586 (indented-text-mode)
82f4acaf
RM
587 (setq major-mode 'change-log-mode
588 mode-name "Change Log"
589 left-margin 8
4f675a8c 590 fill-column 74
60f10a06
KH
591 indent-tabs-mode t
592 tab-width 8)
45c50c5d 593 (use-local-map change-log-mode-map)
60f10a06
KH
594 (set (make-local-variable 'fill-paragraph-function)
595 'change-log-fill-paragraph)
4b7d4d0d
DL
596 ;; We really do want "^" in paragraph-start below: it is only the
597 ;; lines that begin at column 0 (despite the left-margin of 8) that
598 ;; we are looking for. Adding `* ' allows eliding the blank line
599 ;; between entries for different files.
c9cfb0f2 600 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
4b7d4d0d 601 (set (make-local-variable 'paragraph-separate) paragraph-start)
2c91c85c
RS
602 ;; Match null string on the date-line so that the date-line
603 ;; is grouped with what follows.
964141f2 604 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
dd309224
RM
605 (set (make-local-variable 'version-control) 'never)
606 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
4b286eca 607 (set (make-local-variable 'font-lock-defaults)
cea3855a 608 '(change-log-font-lock-keywords t nil nil backward-paragraph))
21d7e080
ER
609 (run-hooks 'change-log-mode-hook))
610
287d149f
RM
611;; It might be nice to have a general feature to replace this. The idea I
612;; have is a variable giving a regexp matching text which should not be
613;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
614;; But I don't feel up to implementing that today.
615(defun change-log-fill-paragraph (&optional justify)
616 "Fill the paragraph, but preserve open parentheses at beginning of lines.
617Prefix arg means justify as well."
618 (interactive "P")
8d6467a4
RS
619 (let ((end (progn (forward-paragraph) (point)))
620 (beg (progn (backward-paragraph) (point)))
14ee1953 621 (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
8d6467a4
RS
622 (fill-region beg end justify)
623 t))
287d149f 624\f
fcad5199 625(defcustom add-log-current-defun-header-regexp
a8d693d8 626 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
fcad5199
RS
627 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes."
628 :type 'regexp
629 :group 'change-log)
21d7e080 630
fb644f48
EN
631;;;###autoload
632(defvar add-log-lisp-like-modes
3697b807 633 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode)
fb644f48
EN
634 "*Modes that look like Lisp to `add-log-current-defun'.")
635
636;;;###autoload
637(defvar add-log-c-like-modes
638 '(c-mode c++-mode c++-c-mode objc-mode)
639 "*Modes that look like C to `add-log-current-defun'.")
640
641;;;###autoload
642(defvar add-log-tex-like-modes
643 '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode)
644 "*Modes that look like TeX to `add-log-current-defun'.")
645
e332f80b 646;;;###autoload
21d7e080
ER
647(defun add-log-current-defun ()
648 "Return name of function definition point is in, or nil.
649
63314951 650Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
5960adc7 651Texinfo (@node titles) and Perl.
21d7e080
ER
652
653Other modes are handled by a heuristic that looks in the 10K before
654point for uppercase headings starting in the first column or
5960adc7 655identifiers followed by `:' or `='. See variables
c1356086
GM
656`add-log-current-defun-header-regexp' and
657`add-log-current-defun-function'
21d7e080
ER
658
659Has a preference of looking backwards."
2cc0b765
RS
660 (condition-case nil
661 (save-excursion
662 (let ((location (point)))
5960adc7
DL
663 (cond (add-log-current-defun-function
664 (funcall add-log-current-defun-function))
c1356086 665 ((memq major-mode add-log-lisp-like-modes)
a0151877 666 ;; If we are now precisely at the beginning of a defun,
2cc0b765
RS
667 ;; make sure beginning-of-defun finds that one
668 ;; rather than the previous one.
669 (or (eobp) (forward-char 1))
670 (beginning-of-defun)
5960adc7
DL
671 ;; Make sure we are really inside the defun found,
672 ;; not after it.
42b1fc29
RS
673 (when (and (looking-at "\\s(")
674 (progn (end-of-defun)
675 (< location (point)))
676 (progn (forward-sexp -1)
677 (>= location (point))))
678 (if (looking-at "\\s(")
679 (forward-char 1))
680 ;; Skip the defining construct name, typically "defun"
681 ;; or "defvar".
682 (forward-sexp 1)
683 ;; The second element is usually a symbol being defined.
684 ;; If it is not, use the first symbol in it.
63c7727f 685 (skip-chars-forward " \t\n'(")
5960adc7
DL
686 (buffer-substring-no-properties (point)
687 (progn (forward-sexp 1)
688 (point)))))
fb644f48
EN
689 ((and (memq major-mode add-log-c-like-modes)
690 (save-excursion
691 (beginning-of-line)
692 ;; Use eq instead of = here to avoid
693 ;; error when at bob and char-after
694 ;; returns nil.
695 (while (eq (char-after (- (point) 2)) ?\\)
696 (forward-line -1))
697 (looking-at "[ \t]*#[ \t]*define[ \t]")))
2cc0b765
RS
698 ;; Handle a C macro definition.
699 (beginning-of-line)
700 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
701 (forward-line -1))
702 (search-forward "define")
703 (skip-chars-forward " \t")
5960adc7
DL
704 (buffer-substring-no-properties (point)
705 (progn (forward-sexp 1)
706 (point))))
fb644f48 707 ((memq major-mode add-log-c-like-modes)
2cc0b765
RS
708 (beginning-of-line)
709 ;; See if we are in the beginning part of a function,
710 ;; before the open brace. If so, advance forward.
711 (while (not (looking-at "{\\|\\(\\s *$\\)"))
712 (forward-line 1))
713 (or (eobp)
714 (forward-char 1))
715 (beginning-of-defun)
5960adc7
DL
716 (when (progn (end-of-defun)
717 (< location (point)))
718 (backward-sexp 1)
719 (let (beg tem)
720
721 (forward-line -1)
722 ;; Skip back over typedefs of arglist.
723 (while (and (not (bobp))
724 (looking-at "[ \t\n]"))
725 (forward-line -1))
726 ;; See if this is using the DEFUN macro used in Emacs,
727 ;; or the DEFUN macro used by the C library.
728 (if (condition-case nil
729 (and (save-excursion
730 (end-of-line)
731 (while (= (preceding-char) ?\\)
732 (end-of-line 2))
733 (backward-sexp 1)
734 (beginning-of-line)
735 (setq tem (point))
736 (looking-at "DEFUN\\b"))
737 (>= location tem))
738 (error nil))
739 (progn
740 (goto-char tem)
741 (down-list 1)
742 (if (= (char-after (point)) ?\")
743 (progn
744 (forward-sexp 1)
745 (skip-chars-forward " ,")))
746 (buffer-substring-no-properties
747 (point)
748 (progn (forward-sexp 1)
749 (point))))
750 (if (looking-at "^[+-]")
751 (change-log-get-method-definition)
752 ;; Ordinary C function syntax.
753 (setq beg (point))
754 (if (and
755 ;; Protect against "Unbalanced parens" error.
756 (condition-case nil
757 (progn
758 (down-list 1) ; into arglist
759 (backward-up-list 1)
760 (skip-chars-backward " \t")
761 t)
762 (error nil))
763 ;; Verify initial pos was after
764 ;; real start of function.
765 (save-excursion
766 (goto-char beg)
767 ;; For this purpose, include the line
768 ;; that has the decl keywords. This
769 ;; may also include some of the
770 ;; comments before the function.
771 (while (and (not (bobp))
772 (save-excursion
773 (forward-line -1)
774 (looking-at "[^\n\f]")))
775 (forward-line -1))
776 (>= location (point)))
777 ;; Consistency check: going down and up
778 ;; shouldn't take us back before BEG.
779 (> (point) beg))
780 (let (end middle)
781 ;; Don't include any final whitespace
782 ;; in the name we use.
783 (skip-chars-backward " \t\n")
784 (setq end (point))
785 (backward-sexp 1)
786 ;; Now find the right beginning of the name.
787 ;; Include certain keywords if they
788 ;; precede the name.
789 (setq middle (point))
790 (forward-word -1)
791 ;; Ignore these subparts of a class decl
792 ;; and move back to the class name itself.
793 (while (looking-at "public \\|private ")
794 (skip-chars-backward " \t:")
795 (setq end (point))
796 (backward-sexp 1)
797 (setq middle (point))
798 (forward-word -1))
799 (and (bolp)
800 (looking-at
801 "enum \\|struct \\|union \\|class ")
802 (setq middle (point)))
803 (goto-char end)
804 (when (eq (preceding-char) ?=)
805 (forward-char -1)
806 (skip-chars-backward " \t")
807 (setq end (point)))
808 (buffer-substring-no-properties
809 middle end))))))))
fb644f48 810 ((memq major-mode add-log-tex-like-modes)
2cc0b765 811 (if (re-search-backward
5960adc7
DL
812 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
813 nil t)
2cc0b765
RS
814 (progn
815 (goto-char (match-beginning 0))
5960adc7
DL
816 (buffer-substring-no-properties
817 (1+ (point)) ; without initial backslash
818 (line-end-position)))))
2cc0b765 819 ((eq major-mode 'texinfo-mode)
3071ee28 820 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
5960adc7 821 (match-string-no-properties 1)))
c06e9d8b 822 ((memq major-mode '(perl-mode cperl-mode))
64bd2d51 823 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t)
5960adc7
DL
824 (match-string-no-properties 1)))
825 ;; Emacs's autoconf-mode installs its own
826 ;; `add-log-current-defun-function'. This applies to
827 ;; a different mode apparently for editing .m4
828 ;; autoconf source.
f654865f 829 ((eq major-mode 'autoconf-mode)
5960adc7
DL
830 (if (re-search-backward
831 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t)
832 (match-string-no-properties 3)))
2cc0b765
RS
833 (t
834 ;; If all else fails, try heuristics
c1356086
GM
835 (let (case-fold-search
836 result)
2cc0b765 837 (end-of-line)
c1356086
GM
838 (when (re-search-backward
839 add-log-current-defun-header-regexp
840 (- (point) 10000)
841 t)
5960adc7
DL
842 (setq result (or (match-string-no-properties 1)
843 (match-string-no-properties 0)))
c1356086
GM
844 ;; Strip whitespace away
845 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
846 result)
5960adc7 847 (setq result (match-string-no-properties 1 result)))
c1356086 848 result))))))
2cc0b765 849 (error nil)))
ef15f270 850
83afd62c 851(defvar change-log-get-method-definition-md)
15319a8f 852
83afd62c 853;; Subroutine used within change-log-get-method-definition.
59c1a7de
RS
854;; Add the last match in the buffer to the end of `md',
855;; followed by the string END; move to the end of that match.
83afd62c
KH
856(defun change-log-get-method-definition-1 (end)
857 (setq change-log-get-method-definition-md
858 (concat change-log-get-method-definition-md
5960adc7 859 (match-string 1)
15319a8f 860 end))
59c1a7de
RS
861 (goto-char (match-end 0)))
862
83afd62c 863(defun change-log-get-method-definition ()
075a6629 864"For objective C, return the method name if we are in a method."
83afd62c 865 (let ((change-log-get-method-definition-md "["))
59c1a7de 866 (save-excursion
f27f16ed 867 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
83afd62c 868 (change-log-get-method-definition-1 " ")))
59c1a7de
RS
869 (save-excursion
870 (cond
f27f16ed 871 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
83afd62c 872 (change-log-get-method-definition-1 "")
59c1a7de
RS
873 (while (not (looking-at "[{;]"))
874 (looking-at
f27f16ed 875 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
83afd62c
KH
876 (change-log-get-method-definition-1 ""))
877 (concat change-log-get-method-definition-md "]"))))))
075a6629
DL
878\f
879(defun change-log-sortable-date-at ()
880 "Return date of log entry in a consistent form for sorting.
881Point is assumed to be at the start of the entry."
882 (require 'timezone)
883 (if (looking-at "^\\sw.........[0-9:+ ]*")
884 (let ((date (match-string-no-properties 0)))
885 (if date
886 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
887 (concat (match-string 1 date) (match-string 2 date)
888 (match-string 3 date))
889 (condition-case nil
890 (timezone-make-date-sortable date)
891 (error nil)))))
892 (error "Bad date")))
59c1a7de 893
075a6629
DL
894;;;###autoload
895(defun change-log-merge (other-log)
896 "Merge the contents of ChangeLog file OTHER-LOG with this buffer.
897Both must be found in Change Log mode (since the merging depends on
898the appropriate motion commands).
899
918f4ac3
DL
900Entries are inserted in chronological order. Both the current and
901old-style time formats for entries are supported."
075a6629
DL
902 (interactive "*fLog file name to merge: ")
903 (if (not (eq major-mode 'change-log-mode))
904 (error "Not in Change Log mode"))
905 (let ((other-buf (find-file-noselect other-log))
906 (buf (current-buffer))
907 date1 start end)
908 (save-excursion
909 (goto-char (point-min))
910 (set-buffer other-buf)
911 (goto-char (point-min))
912 (if (not (eq major-mode 'change-log-mode))
913 (error "%s not found in Change Log mode" other-log))
914 ;; Loop through all the entries in OTHER-LOG.
915 (while (not (eobp))
916 (setq date1 (change-log-sortable-date-at))
917 (setq start (point)
918 end (progn (forward-page) (point)))
919 ;; Look for an entry in original buffer that isn't later.
920 (with-current-buffer buf
921 (while (and (not (eobp))
922 (string< date1 (change-log-sortable-date-at)))
923 (forward-page))
924 (if (not (eobp))
925 (insert-buffer-substring other-buf start end)
926 ;; At the end of the original buffer, insert a newline to
927 ;; separate entries and then the rest of the file being
928 ;; merged. Move to the end of it to terminate outer loop.
929 (insert "\n")
930 (insert-buffer-substring other-buf start
931 (with-current-buffer other-buf
932 (goto-char (point-max))
933 (point)))))))))
ef15f270 934
918f4ac3
DL
935;;;###autoload
936(defun change-log-redate ()
937 "Fix any old-style date entries in the current log file to default format."
938 (interactive)
939 (require 'timezone)
940 (save-excursion
941 (goto-char (point-min))
942 (while (re-search-forward "^\\sw.........[0-9:+ ]*" nil t)
943 (unless (= 12 (- (match-end 0) (match-beginning 0)))
944 (let* ((date (save-match-data
945 (timezone-fix-time (match-string 0) nil nil)))
946 (zone (if (consp (aref date 6))
947 (nth 1 (aref date 6)))))
948 (replace-match (format-time-string
949 "%Y-%m-%d "
950 (encode-time (aref date 5)
951 (aref date 4)
952 (aref date 3)
953 (aref date 2)
954 (aref date 1)
955 (aref date 0)
956 zone))))))))
957
1da56800
RS
958(provide 'add-log)
959
fd7fa35a 960;;; add-log.el ends here