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