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