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