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