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