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