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