(Info-mode): Doco fix.
[bpt/emacs.git] / lisp / add-log.el
CommitLineData
84fc2cfa
ER
1;;; add-log.el --- change log maintenance commands for Emacs
2
e332f80b 3;; Copyright (C) 1985, 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
84fc2cfa 4
e9571d2a
ER
5;; Keywords: maint
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
20;; along with GNU Emacs; see the file COPYING. If not, write to
21;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
e41b2db1
ER
23;;; Commentary:
24
25;; This facility is documented in the Emacs Manual.
26
fd7fa35a 27;;; Code:
84fc2cfa 28
84fc2cfa
ER
29(defvar change-log-default-name nil
30 "*Name of a change log file for \\[add-change-log-entry].")
31
6258d3af
RM
32(defvar add-log-current-defun-function nil
33 "\
34*If non-nil, function to guess name of current function from surrounding text.
35\\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
36instead) with no arguments. It returns a string or nil if it cannot guess.")
37
59ac2ce6 38(defvar add-log-full-name nil
02ec1592
BF
39 "*Full name of user, for inclusion in ChangeLog daily headers.
40This defaults to the value returned by the `user-full-name' function.")
41
59ac2ce6 42(defvar add-log-mailing-address nil
02ec1592 43 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
fb38ca86 44This defaults to the value of `user-mail-address'.")
02ec1592 45
5f562719 46(defvar change-log-font-lock-keywords
b9e5d99e 47 '(("^[SMTWF].+" . font-lock-function-name-face) ; Date line.
5f562719
RS
48 ("^\t\\* \\([^ :\n]+\\)" 1 font-lock-comment-face) ; File name.
49 ("\(\\([^)\n]+\\)\)" 1 font-lock-keyword-face)) ; Function name.
50 "Additional expressions to highlight in Change Log mode.")
51
dab0cb95
RS
52(defvar change-log-mode-map nil
53 "Keymap for Change Log major mode.")
54(if change-log-mode-map
55 nil
56 (setq change-log-mode-map (make-sparse-keymap))
57 (define-key change-log-mode-map "\M-q" 'change-log-fill-paragraph))
58
84fc2cfa
ER
59(defun change-log-name ()
60 (or change-log-default-name
8afc29f0
RS
61 (if (eq system-type 'vax-vms)
62 "$CHANGE_LOG$.TXT"
3912227e 63 (if (or (eq system-type 'ms-dos) (eq system-type 'windows-nt))
5675b1e4 64 "changelo"
8afc29f0 65 "ChangeLog"))))
84fc2cfa 66
287d149f 67;;;###autoload
84fc2cfa
ER
68(defun prompt-for-change-log-name ()
69 "Prompt for a change log name."
70 (let ((default (change-log-name)))
71 (expand-file-name
72 (read-file-name (format "Log file (default %s): " default)
73 nil default))))
74
45a13f0d
RM
75;;;###autoload
76(defun find-change-log (&optional file-name)
77 "Find a change log file for \\[add-change-log-entry] and return the name.
a82e2ed5
RS
78
79Optional arg FILE-NAME specifies the file to use.
de98fcaf
RS
80If FILE-NAME is nil, use the value of `change-log-default-name'.
81If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
82\(or whatever we use on this operating system).
83
84If 'change-log-default-name' contains a leading directory component, then
85simply find it in the current directory. Otherwise, search in the current
86directory and its successive parents for a file so named.
45a13f0d
RM
87
88Once a file is found, `change-log-default-name' is set locally in the
89current buffer to the complete file name."
a82e2ed5
RS
90 ;; If user specified a file name or if this buffer knows which one to use,
91 ;; just use that.
45a13f0d 92 (or file-name
de98fcaf
RS
93 (setq file-name (and change-log-default-name
94 (file-name-directory change-log-default-name)
95 change-log-default-name))
a82e2ed5
RS
96 (progn
97 ;; Chase links in the source file
98 ;; and use the change log in the dir where it points.
99 (setq file-name (or (and buffer-file-name
100 (file-name-directory
101 (file-chase-links buffer-file-name)))
102 default-directory))
103 (if (file-directory-p file-name)
104 (setq file-name (expand-file-name (change-log-name) file-name)))
105 ;; Chase links before visiting the file.
106 ;; This makes it easier to use a single change log file
107 ;; for several related directories.
108 (setq file-name (file-chase-links file-name))
109 (setq file-name (expand-file-name file-name))
110 ;; Move up in the dir hierarchy till we find a change log file.
111 (let ((file1 file-name)
112 parent-dir)
113 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
114 (progn (setq parent-dir
115 (file-name-directory
116 (directory-file-name
117 (file-name-directory file1))))
118 ;; Give up if we are already at the root dir.
119 (not (string= (file-name-directory file1)
120 parent-dir))))
121 ;; Move up to the parent dir and try again.
122 (setq file1 (expand-file-name
123 (file-name-nondirectory (change-log-name))
124 parent-dir)))
125 ;; If we found a change log in a parent, use that.
126 (if (or (get-file-buffer file1) (file-exists-p file1))
127 (setq file-name file1)))))
128 ;; Make a local variable in this buffer so we needn't search again.
129 (set (make-local-variable 'change-log-default-name) file-name)
130 file-name)
45a13f0d 131
84fc2cfa 132;;;###autoload
287d149f 133(defun add-change-log-entry (&optional whoami file-name other-window new-entry)
84fc2cfa
ER
134 "Find change log file and add an entry for today.
135Optional arg (interactive prefix) non-nil means prompt for user name and site.
136Second arg is file name of change log. If nil, uses `change-log-default-name'.
287d149f
RM
137Third arg OTHER-WINDOW non-nil means visit in other window.
138Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
139never append to an existing entry."
84fc2cfa
ER
140 (interactive (list current-prefix-arg
141 (prompt-for-change-log-name)))
02ec1592
BF
142 (if whoami
143 (progn
144 (setq add-log-full-name (read-input "Full name: " add-log-full-name))
84fc2cfa
ER
145 ;; Note that some sites have room and phone number fields in
146 ;; full name which look silly when inserted. Rather than do
147 ;; anything about that here, let user give prefix argument so that
148 ;; s/he can edit the full name field in prompter if s/he wants.
02ec1592
BF
149 (setq add-log-mailing-address
150 (read-input "Mailing address: " add-log-mailing-address))))
59ac2ce6
RS
151 (or add-log-full-name
152 (setq add-log-full-name (user-full-name)))
153 (or add-log-mailing-address
154 (setq add-log-mailing-address user-mail-address))
02ec1592
BF
155 (let ((defun (funcall (or add-log-current-defun-function
156 'add-log-current-defun)))
157 paragraph-end entry)
45a13f0d 158
3e1c918b 159 (setq file-name (expand-file-name (find-change-log file-name)))
82f4acaf
RM
160
161 ;; Set ENTRY to the file name to use in the new entry.
162 (and buffer-file-name
163 ;; Never want to add a change log entry for the ChangeLog file itself.
164 (not (string= buffer-file-name file-name))
165 (setq entry (if (string-match
166 (concat "^" (regexp-quote (file-name-directory
167 file-name)))
168 buffer-file-name)
169 (substring buffer-file-name (match-end 0))
170 (file-name-nondirectory buffer-file-name))))
171
84fc2cfa
ER
172 (if (and other-window (not (equal file-name buffer-file-name)))
173 (find-file-other-window file-name)
174 (find-file file-name))
675a998f
RS
175 (or (eq major-mode 'change-log-mode)
176 (change-log-mode))
84fc2cfa
ER
177 (undo-boundary)
178 (goto-char (point-min))
ec367ad6 179 (if (looking-at (concat (regexp-quote (substring (current-time-string)
82f4acaf 180 0 10))
02ec1592 181 ".* " (regexp-quote add-log-full-name)
4daaec5d 182 " <" (regexp-quote add-log-mailing-address)))
ec367ad6
RS
183 (forward-line 1)
184 (insert (current-time-string)
02ec1592 185 " " add-log-full-name
4daaec5d 186 " <" add-log-mailing-address ">\n\n"))
82f4acaf 187
74046d00 188 ;; Search only within the first paragraph.
716a781e
RS
189 (if (looking-at "\n*[^\n* \t]")
190 (skip-chars-forward "\n")
191 (forward-paragraph 1))
82f4acaf 192 (setq paragraph-end (point))
84fc2cfa 193 (goto-char (point-min))
82f4acaf 194
1832dbd1 195 ;; Now insert the new line for this entry.
82f4acaf
RM
196 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t)
197 ;; Put this file name into the existing empty entry.
198 (if entry
199 (insert entry)))
287d149f
RM
200 ((and (not new-entry)
201 (re-search-forward
82f4acaf
RM
202 (concat (regexp-quote (concat "* " entry))
203 ;; Don't accept `foo.bar' when
204 ;; looking for `foo':
205 "\\(\\s \\|[(),:]\\)")
206 paragraph-end t))
207 ;; Add to the existing entry for the same file.
208 (re-search-forward "^\\s *$\\|^\\s \\*")
1832dbd1 209 (beginning-of-line)
09b389d0 210 (while (and (not (eobp)) (looking-at "^\\s *$"))
1832dbd1
RS
211 (delete-region (point) (save-excursion (forward-line 1) (point))))
212 (insert "\n\n")
213 (forward-line -2)
dd309224 214 (indent-relative-maybe))
21d7e080 215 (t
dd309224
RM
216 ;; Make a new entry.
217 (forward-line 1)
218 (while (looking-at "\\sW")
219 (forward-line 1))
09b389d0 220 (while (and (not (eobp)) (looking-at "^\\s *$"))
1832dbd1
RS
221 (delete-region (point) (save-excursion (forward-line 1) (point))))
222 (insert "\n\n\n")
223 (forward-line -2)
dd309224
RM
224 (indent-to left-margin)
225 (insert "* " (or entry ""))))
1832dbd1 226 ;; Now insert the function name, if we have one.
21d7e080
ER
227 ;; Point is at the entry for this file,
228 ;; either at the end of the line or at the first blank line.
229 (if defun
230 (progn
dd309224 231 ;; Make it easy to get rid of the function name.
21d7e080 232 (undo-boundary)
dd309224
RM
233 (insert (if (save-excursion
234 (beginning-of-line 1)
235 (looking-at "\\s *$"))
236 ""
237 " ")
238 "(" defun "): "))
1832dbd1 239 ;; No function name, so put in a colon unless we have just a star.
dd309224
RM
240 (if (not (save-excursion
241 (beginning-of-line 1)
242 (looking-at "\\s *\\(\\*\\s *\\)?$")))
1832dbd1 243 (insert ": ")))))
84fc2cfa 244
84fc2cfa
ER
245;;;###autoload
246(defun add-change-log-entry-other-window (&optional whoami file-name)
247 "Find change log file in other window and add an entry for today.
6258d3af
RM
248Optional arg (interactive prefix) non-nil means prompt for user name and site.
249Second arg is file name of change log. \
250If nil, uses `change-log-default-name'."
84fc2cfa
ER
251 (interactive (if current-prefix-arg
252 (list current-prefix-arg
253 (prompt-for-change-log-name))))
254 (add-change-log-entry whoami file-name t))
82f4acaf 255;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
84fc2cfa 256
1da56800 257;;;###autoload
21d7e080 258(defun change-log-mode ()
eb8c3be9 259 "Major mode for editing change logs; like Indented Text Mode.
09b389d0 260Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
5516387c
RM
261New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
262Each entry behaves as a paragraph, and the entries for one day as a page.
263Runs `change-log-mode-hook'."
21d7e080
ER
264 (interactive)
265 (kill-all-local-variables)
266 (indented-text-mode)
82f4acaf
RM
267 (setq major-mode 'change-log-mode
268 mode-name "Change Log"
269 left-margin 8
270 fill-column 74)
287d149f 271 (use-local-map change-log-mode-map)
21d7e080 272 ;; Let each entry behave as one paragraph:
964141f2
RS
273 (set (make-local-variable 'paragraph-start) "^\\s *$\\|^\f")
274 (set (make-local-variable 'paragraph-separate) "^\\s *$\\|^\f\\|^\\sw")
21d7e080 275 ;; Let all entries for one day behave as one page.
2c91c85c
RS
276 ;; Match null string on the date-line so that the date-line
277 ;; is grouped with what follows.
964141f2 278 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
dd309224
RM
279 (set (make-local-variable 'version-control) 'never)
280 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
4b286eca
SM
281 (set (make-local-variable 'font-lock-defaults)
282 '(change-log-font-lock-keywords t))
21d7e080
ER
283 (run-hooks 'change-log-mode-hook))
284
287d149f
RM
285;; It might be nice to have a general feature to replace this. The idea I
286;; have is a variable giving a regexp matching text which should not be
287;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
288;; But I don't feel up to implementing that today.
289(defun change-log-fill-paragraph (&optional justify)
290 "Fill the paragraph, but preserve open parentheses at beginning of lines.
291Prefix arg means justify as well."
292 (interactive "P")
293 (let ((paragraph-separate (concat paragraph-separate "\\|^\\s *\\s("))
294 (paragraph-start (concat paragraph-start "\\|^\\s *\\s(")))
295 (fill-paragraph justify)))
296\f
21d7e080 297(defvar add-log-current-defun-header-regexp
26add1bf 298 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
21d7e080
ER
299 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
300
e332f80b 301;;;###autoload
21d7e080
ER
302(defun add-log-current-defun ()
303 "Return name of function definition point is in, or nil.
304
63314951 305Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
64bd2d51 306Texinfo (@node titles), Perl, and Fortran.
21d7e080
ER
307
308Other modes are handled by a heuristic that looks in the 10K before
309point for uppercase headings starting in the first column or
310identifiers followed by `:' or `=', see variable
311`add-log-current-defun-header-regexp'.
312
313Has a preference of looking backwards."
2cc0b765
RS
314 (condition-case nil
315 (save-excursion
316 (let ((location (point)))
3fec4c1b
RS
317 (cond ((memq major-mode '(emacs-lisp-mode lisp-mode scheme-mode
318 lisp-interaction-mode))
2cc0b765
RS
319 ;; If we are now precisely a the beginning of a defun,
320 ;; make sure beginning-of-defun finds that one
321 ;; rather than the previous one.
322 (or (eobp) (forward-char 1))
323 (beginning-of-defun)
324 ;; Make sure we are really inside the defun found, not after it.
325 (if (and (progn (end-of-defun)
326 (< location (point)))
327 (progn (forward-sexp -1)
328 (>= location (point))))
329 (progn
0e933219
JB
330 (if (looking-at "\\s(")
331 (forward-char 1))
332 (forward-sexp 1)
2cc0b765
RS
333 (skip-chars-forward " ")
334 (buffer-substring (point)
335 (progn (forward-sexp 1) (point))))))
32e986d4 336 ((and (memq major-mode '(c-mode c++-mode c++-c-mode objc-mode))
2cc0b765
RS
337 (save-excursion (beginning-of-line)
338 ;; Use eq instead of = here to avoid
339 ;; error when at bob and char-after
340 ;; returns nil.
341 (while (eq (char-after (- (point) 2)) ?\\)
36e6631c 342 (forward-line -1))
2cc0b765
RS
343 (looking-at "[ \t]*#[ \t]*define[ \t]")))
344 ;; Handle a C macro definition.
345 (beginning-of-line)
346 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
347 (forward-line -1))
348 (search-forward "define")
349 (skip-chars-forward " \t")
350 (buffer-substring (point)
351 (progn (forward-sexp 1) (point))))
32e986d4 352 ((memq major-mode '(c-mode c++-mode c++-c-mode objc-mode))
2cc0b765
RS
353 (beginning-of-line)
354 ;; See if we are in the beginning part of a function,
355 ;; before the open brace. If so, advance forward.
356 (while (not (looking-at "{\\|\\(\\s *$\\)"))
357 (forward-line 1))
358 (or (eobp)
359 (forward-char 1))
360 (beginning-of-defun)
361 (if (progn (end-of-defun)
362 (< location (point)))
363 (progn
364 (backward-sexp 1)
365 (let (beg tem)
fd7fa35a 366
2cc0b765
RS
367 (forward-line -1)
368 ;; Skip back over typedefs of arglist.
369 (while (and (not (bobp))
370 (looking-at "[ \t\n]"))
371 (forward-line -1))
372 ;; See if this is using the DEFUN macro used in Emacs,
373 ;; or the DEFUN macro used by the C library.
374 (if (condition-case nil
375 (and (save-excursion
019644ee
RS
376 (end-of-line)
377 (while (= (preceding-char) ?\\)
378 (end-of-line 2))
2cc0b765
RS
379 (backward-sexp 1)
380 (beginning-of-line)
381 (setq tem (point))
382 (looking-at "DEFUN\\b"))
383 (>= location tem))
384 (error nil))
385 (progn
386 (goto-char tem)
387 (down-list 1)
388 (if (= (char-after (point)) ?\")
389 (progn
390 (forward-sexp 1)
391 (skip-chars-forward " ,")))
392 (buffer-substring (point)
393 (progn (forward-sexp 1) (point))))
32e986d4
RS
394 (if (looking-at "^[+-]")
395 (get-method-definition)
396 ;; Ordinary C function syntax.
397 (setq beg (point))
398 (if (condition-case nil
399 ;; Protect against "Unbalanced parens" error.
400 (progn
401 (down-list 1) ; into arglist
402 (backward-up-list 1)
403 (skip-chars-backward " \t")
404 t)
405 (error nil))
406 ;; Verify initial pos was after
407 ;; real start of function.
408 (if (and (save-excursion
409 (goto-char beg)
410 ;; For this purpose, include the line
411 ;; that has the decl keywords. This
412 ;; may also include some of the
413 ;; comments before the function.
414 (while (and (not (bobp))
415 (save-excursion
416 (forward-line -1)
417 (looking-at "[^\n\f]")))
418 (forward-line -1))
419 (>= location (point)))
420 ;; Consistency check: going down and up
421 ;; shouldn't take us back before BEG.
422 (> (point) beg))
423 (buffer-substring (point)
424 (progn (backward-sexp 1)
425 (point)))))))))))
2cc0b765
RS
426 ((memq major-mode
427 '(TeX-mode plain-TeX-mode LaTeX-mode;; tex-mode.el
428 plain-tex-mode latex-mode;; cmutex.el
429 ))
430 (if (re-search-backward
431 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
432 (progn
433 (goto-char (match-beginning 0))
434 (buffer-substring (1+ (point));; without initial backslash
435 (progn
436 (end-of-line)
437 (point))))))
438 ((eq major-mode 'texinfo-mode)
3071ee28 439 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
2cc0b765
RS
440 (buffer-substring (match-beginning 1)
441 (match-end 1))))
64bd2d51
RS
442 ((eq major-mode 'perl-mode)
443 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t)
444 (buffer-substring (match-beginning 1)
445 (match-end 1))))
a9e2a7f2
RS
446 ((eq major-mode 'fortran-mode)
447 ;; must be inside function body for this to work
448 (beginning-of-fortran-subprogram)
449 (let ((case-fold-search t)) ; case-insensitive
450 ;; search for fortran subprogram start
451 (if (re-search-forward
452 "^[ \t]*\\(program\\|subroutine\\|function\
453\\|[ \ta-z0-9*]*[ \t]+function\\)"
63314951 454 nil t)
a9e2a7f2
RS
455 (progn
456 ;; move to EOL or before first left paren
457 (if (re-search-forward "[(\n]" nil t)
458 (progn (forward-char -1)
459 (skip-chars-backward " \t"))
460 (end-of-line))
461 ;; Use the name preceding that.
462 (buffer-substring (point)
463 (progn (forward-sexp -1)
464 (point)))))))
2cc0b765
RS
465 (t
466 ;; If all else fails, try heuristics
467 (let (case-fold-search)
468 (end-of-line)
469 (if (re-search-backward add-log-current-defun-header-regexp
470 (- (point) 10000)
471 t)
472 (buffer-substring (match-beginning 1)
473 (match-end 1))))))))
474 (error nil)))
ef15f270 475
59c1a7de
RS
476;; Subroutine used within get-method-definition.
477;; Add the last match in the buffer to the end of `md',
478;; followed by the string END; move to the end of that match.
479(defun get-method-definition-1 (end)
480 (setq md (concat md
481 (buffer-substring (match-beginning 1) (match-end 1))
482 end))
483 (goto-char (match-end 0)))
484
485;; For objective C, return the method name if we are in a method.
486(defun get-method-definition ()
487 (let ((md "["))
488 (save-excursion
f27f16ed 489 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
59c1a7de
RS
490 (get-method-definition-1 " ")))
491 (save-excursion
492 (cond
f27f16ed 493 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
59c1a7de
RS
494 (get-method-definition-1 "")
495 (while (not (looking-at "[{;]"))
496 (looking-at
f27f16ed 497 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
59c1a7de
RS
498 (get-method-definition-1 ""))
499 (concat md "]"))))))
500
ef15f270 501
1da56800
RS
502(provide 'add-log)
503
fd7fa35a 504;;; add-log.el ends here