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