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