(Man-fontify-manpage, Man-cleanup-manpage):
[emacs.git] / lisp / add-log.el
blobe77b8cdcb35f883c3817b1538114ccc7cb504b42
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
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 (defvar change-log-default-name nil
30 "*Name of a change log file for \\[add-change-log-entry].")
32 (defvar add-log-current-defun-function nil
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.")
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.")
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'.")
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.")
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))
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"))))
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))))
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.
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).
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.
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)
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)
169 (setq file-name (expand-file-name (find-change-log file-name)))
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))))
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"))
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))
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 (let (case-fold-search)
212 (re-search-forward
213 (concat (regexp-quote (concat "* " entry))
214 ;; Don't accept `foo.bar' when
215 ;; looking for `foo':
216 "\\(\\s \\|[(),:]\\)")
217 paragraph-end t)))
218 ;; Add to the existing entry for the same file.
219 (re-search-forward "^\\s *$\\|^\\s \\*")
220 (goto-char (match-beginning 0))
221 ;; Delete excess empty lines; make just 2.
222 (while (and (not (eobp)) (looking-at "^\\s *$"))
223 (delete-region (point) (save-excursion (forward-line 1) (point))))
224 (insert "\n\n")
225 (forward-line -2)
226 (indent-relative-maybe))
228 ;; Make a new entry.
229 (forward-line 1)
230 (while (looking-at "\\sW")
231 (forward-line 1))
232 (while (and (not (eobp)) (looking-at "^\\s *$"))
233 (delete-region (point) (save-excursion (forward-line 1) (point))))
234 (insert "\n\n\n")
235 (forward-line -2)
236 (indent-to left-margin)
237 (insert "* " (or entry ""))))
238 ;; Now insert the function name, if we have one.
239 ;; Point is at the entry for this file,
240 ;; either at the end of the line or at the first blank line.
241 (if defun
242 (progn
243 ;; Make it easy to get rid of the function name.
244 (undo-boundary)
245 (insert (if (save-excursion
246 (beginning-of-line 1)
247 (looking-at "\\s *$"))
249 " ")
250 "(" defun "): "))
251 ;; No function name, so put in a colon unless we have just a star.
252 (if (not (save-excursion
253 (beginning-of-line 1)
254 (looking-at "\\s *\\(\\*\\s *\\)?$")))
255 (insert ": ")))))
257 ;;;###autoload
258 (defun add-change-log-entry-other-window (&optional whoami file-name)
259 "Find change log file in other window and add an entry for today.
260 Optional arg (interactive prefix) non-nil means prompt for user name and site.
261 Second arg is file name of change log. \
262 If nil, uses `change-log-default-name'."
263 (interactive (if current-prefix-arg
264 (list current-prefix-arg
265 (prompt-for-change-log-name))))
266 (add-change-log-entry whoami file-name t))
267 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
269 ;;;###autoload
270 (defun change-log-mode ()
271 "Major mode for editing change logs; like Indented Text Mode.
272 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
273 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
274 Each entry behaves as a paragraph, and the entries for one day as a page.
275 Runs `change-log-mode-hook'."
276 (interactive)
277 (kill-all-local-variables)
278 (indented-text-mode)
279 (setq major-mode 'change-log-mode
280 mode-name "Change Log"
281 left-margin 8
282 fill-column 74)
283 (use-local-map change-log-mode-map)
284 ;; Let each entry behave as one paragraph:
285 ;; We really do want "^" in paragraph-start below: it is only the lines that
286 ;; begin at column 0 (despite the left-margin of 8) that we are looking for.
287 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\sw")
288 (set (make-local-variable 'paragraph-separate) "\\s *$\\|\f\\|^\\sw")
289 ;; Let all entries for one day behave as one page.
290 ;; Match null string on the date-line so that the date-line
291 ;; is grouped with what follows.
292 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
293 (set (make-local-variable 'version-control) 'never)
294 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
295 (set (make-local-variable 'font-lock-defaults)
296 '(change-log-font-lock-keywords t))
297 (run-hooks 'change-log-mode-hook))
299 ;; It might be nice to have a general feature to replace this. The idea I
300 ;; have is a variable giving a regexp matching text which should not be
301 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
302 ;; But I don't feel up to implementing that today.
303 (defun change-log-fill-paragraph (&optional justify)
304 "Fill the paragraph, but preserve open parentheses at beginning of lines.
305 Prefix arg means justify as well."
306 (interactive "P")
307 (let ((end (save-excursion (forward-paragraph) (point)))
308 (beg (save-excursion (backward-paragraph)(point)))
309 (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
310 (fill-region beg end justify)))
312 (defvar add-log-current-defun-header-regexp
313 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
314 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
316 ;;;###autoload
317 (defun add-log-current-defun ()
318 "Return name of function definition point is in, or nil.
320 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
321 Texinfo (@node titles), Perl, and Fortran.
323 Other modes are handled by a heuristic that looks in the 10K before
324 point for uppercase headings starting in the first column or
325 identifiers followed by `:' or `=', see variable
326 `add-log-current-defun-header-regexp'.
328 Has a preference of looking backwards."
329 (condition-case nil
330 (save-excursion
331 (let ((location (point)))
332 (cond ((memq major-mode '(emacs-lisp-mode lisp-mode scheme-mode
333 lisp-interaction-mode))
334 ;; If we are now precisely at the beginning of a defun,
335 ;; make sure beginning-of-defun finds that one
336 ;; rather than the previous one.
337 (or (eobp) (forward-char 1))
338 (beginning-of-defun)
339 ;; Make sure we are really inside the defun found, not after it.
340 (if (and (progn (end-of-defun)
341 (< location (point)))
342 (progn (forward-sexp -1)
343 (>= location (point))))
344 (progn
345 (if (looking-at "\\s(")
346 (forward-char 1))
347 (forward-sexp 1)
348 (skip-chars-forward " '")
349 (buffer-substring (point)
350 (progn (forward-sexp 1) (point))))))
351 ((and (memq major-mode '(c-mode c++-mode c++-c-mode objc-mode))
352 (save-excursion (beginning-of-line)
353 ;; Use eq instead of = here to avoid
354 ;; error when at bob and char-after
355 ;; returns nil.
356 (while (eq (char-after (- (point) 2)) ?\\)
357 (forward-line -1))
358 (looking-at "[ \t]*#[ \t]*define[ \t]")))
359 ;; Handle a C macro definition.
360 (beginning-of-line)
361 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
362 (forward-line -1))
363 (search-forward "define")
364 (skip-chars-forward " \t")
365 (buffer-substring (point)
366 (progn (forward-sexp 1) (point))))
367 ((memq major-mode '(c-mode c++-mode c++-c-mode objc-mode))
368 (beginning-of-line)
369 ;; See if we are in the beginning part of a function,
370 ;; before the open brace. If so, advance forward.
371 (while (not (looking-at "{\\|\\(\\s *$\\)"))
372 (forward-line 1))
373 (or (eobp)
374 (forward-char 1))
375 (beginning-of-defun)
376 (if (progn (end-of-defun)
377 (< location (point)))
378 (progn
379 (backward-sexp 1)
380 (let (beg tem)
382 (forward-line -1)
383 ;; Skip back over typedefs of arglist.
384 (while (and (not (bobp))
385 (looking-at "[ \t\n]"))
386 (forward-line -1))
387 ;; See if this is using the DEFUN macro used in Emacs,
388 ;; or the DEFUN macro used by the C library.
389 (if (condition-case nil
390 (and (save-excursion
391 (end-of-line)
392 (while (= (preceding-char) ?\\)
393 (end-of-line 2))
394 (backward-sexp 1)
395 (beginning-of-line)
396 (setq tem (point))
397 (looking-at "DEFUN\\b"))
398 (>= location tem))
399 (error nil))
400 (progn
401 (goto-char tem)
402 (down-list 1)
403 (if (= (char-after (point)) ?\")
404 (progn
405 (forward-sexp 1)
406 (skip-chars-forward " ,")))
407 (buffer-substring (point)
408 (progn (forward-sexp 1) (point))))
409 (if (looking-at "^[+-]")
410 (get-method-definition)
411 ;; Ordinary C function syntax.
412 (setq beg (point))
413 (if (and (condition-case nil
414 ;; Protect against "Unbalanced parens" error.
415 (progn
416 (down-list 1) ; into arglist
417 (backward-up-list 1)
418 (skip-chars-backward " \t")
420 (error nil))
421 ;; Verify initial pos was after
422 ;; real start of function.
423 (save-excursion
424 (goto-char beg)
425 ;; For this purpose, include the line
426 ;; that has the decl keywords. This
427 ;; may also include some of the
428 ;; comments before the function.
429 (while (and (not (bobp))
430 (save-excursion
431 (forward-line -1)
432 (looking-at "[^\n\f]")))
433 (forward-line -1))
434 (>= location (point)))
435 ;; Consistency check: going down and up
436 ;; shouldn't take us back before BEG.
437 (> (point) beg))
438 (let (end middle)
439 ;; Don't include any final newline
440 ;; in the name we use.
441 (if (= (preceding-char) ?\n)
442 (forward-char -1))
443 (setq end (point))
444 (backward-sexp 1)
445 ;; Now find the right beginning of the name.
446 ;; Include certain keywords if they
447 ;; precede the name.
448 (setq middle (point))
449 (forward-word -1)
450 ;; Ignore these subparts of a class decl
451 ;; and move back to the class name itself.
452 (while (looking-at "public \\|private ")
453 (skip-chars-backward " \t:")
454 (setq end (point))
455 (backward-sexp 1)
456 (setq middle (point))
457 (forward-word -1))
458 (and (bolp)
459 (looking-at "struct \\|union \\|class ")
460 (setq middle (point)))
461 (buffer-substring middle end)))))))))
462 ((memq major-mode
463 '(TeX-mode plain-TeX-mode LaTeX-mode;; tex-mode.el
464 plain-tex-mode latex-mode;; cmutex.el
466 (if (re-search-backward
467 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
468 (progn
469 (goto-char (match-beginning 0))
470 (buffer-substring (1+ (point));; without initial backslash
471 (progn
472 (end-of-line)
473 (point))))))
474 ((eq major-mode 'texinfo-mode)
475 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
476 (buffer-substring (match-beginning 1)
477 (match-end 1))))
478 ((eq major-mode 'perl-mode)
479 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t)
480 (buffer-substring (match-beginning 1)
481 (match-end 1))))
482 ((eq major-mode 'fortran-mode)
483 ;; must be inside function body for this to work
484 (beginning-of-fortran-subprogram)
485 (let ((case-fold-search t)) ; case-insensitive
486 ;; search for fortran subprogram start
487 (if (re-search-forward
488 "^[ \t]*\\(program\\|subroutine\\|function\
489 \\|[ \ta-z0-9*]*[ \t]+function\\)"
490 nil t)
491 (progn
492 ;; move to EOL or before first left paren
493 (if (re-search-forward "[(\n]" nil t)
494 (progn (forward-char -1)
495 (skip-chars-backward " \t"))
496 (end-of-line))
497 ;; Use the name preceding that.
498 (buffer-substring (point)
499 (progn (forward-sexp -1)
500 (point)))))))
502 ;; If all else fails, try heuristics
503 (let (case-fold-search)
504 (end-of-line)
505 (if (re-search-backward add-log-current-defun-header-regexp
506 (- (point) 10000)
508 (buffer-substring (match-beginning 1)
509 (match-end 1))))))))
510 (error nil)))
512 (defvar get-method-definition-md)
514 ;; Subroutine used within get-method-definition.
515 ;; Add the last match in the buffer to the end of `md',
516 ;; followed by the string END; move to the end of that match.
517 (defun get-method-definition-1 (end)
518 (setq get-method-definition-md
519 (concat get-method-definition-md
520 (buffer-substring (match-beginning 1) (match-end 1))
521 end))
522 (goto-char (match-end 0)))
524 ;; For objective C, return the method name if we are in a method.
525 (defun get-method-definition ()
526 (let ((get-method-definition-md "["))
527 (save-excursion
528 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
529 (get-method-definition-1 " ")))
530 (save-excursion
531 (cond
532 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
533 (get-method-definition-1 "")
534 (while (not (looking-at "[{;]"))
535 (looking-at
536 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
537 (get-method-definition-1 ""))
538 (concat get-method-definition-md "]"))))))
541 (provide 'add-log)
543 ;;; add-log.el ends here