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