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