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