1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
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)
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.
25 ;; This facility is documented in the Emacs Manual.
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
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)
63 (if (eq system-type 'ms-dos)
68 (defun prompt-for-change-log-name ()
69 "Prompt for a change log name."
70 (let ((default (change-log-name)))
72 (read-file-name (format "Log file (default %s): " default)
76 (defun find-change-log (&optional file-name)
77 "Find a change log file for \\[add-change-log-entry] and return the name.
79 Optional arg FILE-NAME specifies the file to use.
80 If FILE-NAME is nil, use the value of `change-log-default-name' if non-nil.
81 Otherwise, search in the current directory and its successive parents
82 for a file named `ChangeLog' (or whatever we use on this operating system).
84 Once a file is found, `change-log-default-name' is set locally in the
85 current buffer to the complete file name."
86 ;; If user specified a file name or if this buffer knows which one to use,
89 (setq file-name change-log-default-name)
91 ;; Chase links in the source file
92 ;; and use the change log in the dir where it points.
93 (setq file-name (or (and buffer-file-name
95 (file-chase-links buffer-file-name)))
97 (if (file-directory-p file-name)
98 (setq file-name (expand-file-name (change-log-name) file-name)))
99 ;; Chase links before visiting the file.
100 ;; This makes it easier to use a single change log file
101 ;; for several related directories.
102 (setq file-name (file-chase-links file-name))
103 (setq file-name (expand-file-name file-name))
104 ;; Move up in the dir hierarchy till we find a change log file.
105 (let ((file1 file-name)
107 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
108 (progn (setq parent-dir
111 (file-name-directory file1))))
112 ;; Give up if we are already at the root dir.
113 (not (string= (file-name-directory file1)
115 ;; Move up to the parent dir and try again.
116 (setq file1 (expand-file-name
117 (file-name-nondirectory (change-log-name))
119 ;; If we found a change log in a parent, use that.
120 (if (or (get-file-buffer file1) (file-exists-p file1))
121 (setq file-name file1)))))
122 ;; Make a local variable in this buffer so we needn't search again.
123 (set (make-local-variable 'change-log-default-name) file-name)
127 (defun add-change-log-entry (&optional whoami file-name other-window new-entry)
128 "Find change log file and add an entry for today.
129 Optional arg (interactive prefix) non-nil means prompt for user name and site.
130 Second arg is file name of change log. If nil, uses `change-log-default-name'.
131 Third arg OTHER-WINDOW non-nil means visit in other window.
132 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
133 never append to an existing entry."
134 (interactive (list current-prefix-arg
135 (prompt-for-change-log-name)))
138 (setq add-log-full-name (read-input "Full name: " add-log-full-name))
139 ;; Note that some sites have room and phone number fields in
140 ;; full name which look silly when inserted. Rather than do
141 ;; anything about that here, let user give prefix argument so that
142 ;; s/he can edit the full name field in prompter if s/he wants.
143 (setq add-log-mailing-address
144 (read-input "Mailing address: " add-log-mailing-address))))
145 (or add-log-full-name
146 (setq add-log-full-name (user-full-name)))
147 (or add-log-mailing-address
148 (setq add-log-mailing-address user-mail-address))
149 (let ((defun (funcall (or add-log-current-defun-function
150 'add-log-current-defun)))
153 (setq file-name (expand-file-name (find-change-log file-name)))
155 ;; Set ENTRY to the file name to use in the new entry.
156 (and buffer-file-name
157 ;; Never want to add a change log entry for the ChangeLog file itself.
158 (not (string= buffer-file-name file-name))
159 (setq entry (if (string-match
160 (concat "^" (regexp-quote (file-name-directory
163 (substring buffer-file-name (match-end 0))
164 (file-name-nondirectory buffer-file-name))))
166 (if (and other-window (not (equal file-name buffer-file-name)))
167 (find-file-other-window file-name)
168 (find-file file-name))
169 (or (eq major-mode 'change-log-mode)
172 (goto-char (point-min))
173 (if (looking-at (concat (regexp-quote (substring (current-time-string)
175 ".* " (regexp-quote add-log-full-name)
176 " <" (regexp-quote add-log-mailing-address)))
178 (insert (current-time-string)
179 " " add-log-full-name
180 " <" add-log-mailing-address ">\n\n"))
182 ;; Search only within the first paragraph.
183 (if (looking-at "\n*[^\n* \t]")
184 (skip-chars-forward "\n")
185 (forward-paragraph 1))
186 (setq paragraph-end (point))
187 (goto-char (point-min))
189 ;; Now insert the new line for this entry.
190 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t)
191 ;; Put this file name into the existing empty entry.
194 ((and (not new-entry)
196 (concat (regexp-quote (concat "* " entry))
197 ;; Don't accept `foo.bar' when
198 ;; looking for `foo':
199 "\\(\\s \\|[(),:]\\)")
201 ;; Add to the existing entry for the same file.
202 (re-search-forward "^\\s *$\\|^\\s \\*")
204 (while (and (not (eobp)) (looking-at "^\\s *$"))
205 (delete-region (point) (save-excursion (forward-line 1) (point))))
208 (indent-relative-maybe))
212 (while (looking-at "\\sW")
214 (while (and (not (eobp)) (looking-at "^\\s *$"))
215 (delete-region (point) (save-excursion (forward-line 1) (point))))
218 (indent-to left-margin)
219 (insert "* " (or entry ""))))
220 ;; Now insert the function name, if we have one.
221 ;; Point is at the entry for this file,
222 ;; either at the end of the line or at the first blank line.
225 ;; Make it easy to get rid of the function name.
227 (insert (if (save-excursion
228 (beginning-of-line 1)
229 (looking-at "\\s *$"))
233 ;; No function name, so put in a colon unless we have just a star.
234 (if (not (save-excursion
235 (beginning-of-line 1)
236 (looking-at "\\s *\\(\\*\\s *\\)?$")))
240 (defun add-change-log-entry-other-window (&optional whoami file-name)
241 "Find change log file in other window and add an entry for today.
242 Optional arg (interactive prefix) non-nil means prompt for user name and site.
243 Second arg is file name of change log. \
244 If nil, uses `change-log-default-name'."
245 (interactive (if current-prefix-arg
246 (list current-prefix-arg
247 (prompt-for-change-log-name))))
248 (add-change-log-entry whoami file-name t))
249 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
252 (defun change-log-mode ()
253 "Major mode for editing change logs; like Indented Text Mode.
254 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
255 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
256 Each entry behaves as a paragraph, and the entries for one day as a page.
257 Runs `change-log-mode-hook'."
259 (kill-all-local-variables)
261 (setq major-mode 'change-log-mode
262 mode-name "Change Log"
265 (use-local-map change-log-mode-map)
266 ;; Let each entry behave as one paragraph:
267 (set (make-local-variable 'paragraph-start) "^\\s *$\\|^\f")
268 (set (make-local-variable 'paragraph-separate) "^\\s *$\\|^\f\\|^\\sw")
269 ;; Let all entries for one day behave as one page.
270 ;; Match null string on the date-line so that the date-line
271 ;; is grouped with what follows.
272 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
273 (set (make-local-variable 'version-control) 'never)
274 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
275 (set (make-local-variable 'font-lock-defaults)
276 '(change-log-font-lock-keywords t))
277 (run-hooks 'change-log-mode-hook))
279 ;; It might be nice to have a general feature to replace this. The idea I
280 ;; have is a variable giving a regexp matching text which should not be
281 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
282 ;; But I don't feel up to implementing that today.
283 (defun change-log-fill-paragraph (&optional justify)
284 "Fill the paragraph, but preserve open parentheses at beginning of lines.
285 Prefix arg means justify as well."
287 (let ((paragraph-separate (concat paragraph-separate "\\|^\\s *\\s("))
288 (paragraph-start (concat paragraph-start "\\|^\\s *\\s(")))
289 (fill-paragraph justify)))
291 (defvar add-log-current-defun-header-regexp
292 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
293 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
296 (defun add-log-current-defun ()
297 "Return name of function definition point is in, or nil.
299 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
300 Texinfo (@node titles), Perl, and Fortran.
302 Other modes are handled by a heuristic that looks in the 10K before
303 point for uppercase headings starting in the first column or
304 identifiers followed by `:' or `=', see variable
305 `add-log-current-defun-header-regexp'.
307 Has a preference of looking backwards."
310 (let ((location (point)))
311 (cond ((memq major-mode '(emacs-lisp-mode lisp-mode scheme-mode
312 lisp-interaction-mode))
313 ;; If we are now precisely a the beginning of a defun,
314 ;; make sure beginning-of-defun finds that one
315 ;; rather than the previous one.
316 (or (eobp) (forward-char 1))
318 ;; Make sure we are really inside the defun found, not after it.
319 (if (and (progn (end-of-defun)
320 (< location (point)))
321 (progn (forward-sexp -1)
322 (>= location (point))))
324 (if (looking-at "\\s(")
327 (skip-chars-forward " ")
328 (buffer-substring (point)
329 (progn (forward-sexp 1) (point))))))
330 ((and (memq major-mode '(c-mode c++-mode c++-c-mode))
331 (save-excursion (beginning-of-line)
332 ;; Use eq instead of = here to avoid
333 ;; error when at bob and char-after
335 (while (eq (char-after (- (point) 2)) ?\\)
337 (looking-at "[ \t]*#[ \t]*define[ \t]")))
338 ;; Handle a C macro definition.
340 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
342 (search-forward "define")
343 (skip-chars-forward " \t")
344 (buffer-substring (point)
345 (progn (forward-sexp 1) (point))))
346 ((memq major-mode '(c-mode c++-mode c++-c-mode))
348 ;; See if we are in the beginning part of a function,
349 ;; before the open brace. If so, advance forward.
350 (while (not (looking-at "{\\|\\(\\s *$\\)"))
355 (if (progn (end-of-defun)
356 (< location (point)))
362 ;; Skip back over typedefs of arglist.
363 (while (and (not (bobp))
364 (looking-at "[ \t\n]"))
366 ;; See if this is using the DEFUN macro used in Emacs,
367 ;; or the DEFUN macro used by the C library.
368 (if (condition-case nil
371 (while (= (preceding-char) ?\\)
376 (looking-at "DEFUN\\b"))
382 (if (= (char-after (point)) ?\")
385 (skip-chars-forward " ,")))
386 (buffer-substring (point)
387 (progn (forward-sexp 1) (point))))
388 ;; Ordinary C function syntax.
390 (if (condition-case nil
391 ;; Protect against "Unbalanced parens" error.
393 (down-list 1) ; into arglist
395 (skip-chars-backward " \t")
398 ;; Verify initial pos was after
399 ;; real start of function.
400 (if (and (save-excursion
402 ;; For this purpose, include the line
403 ;; that has the decl keywords. This
404 ;; may also include some of the
405 ;; comments before the function.
406 (while (and (not (bobp))
409 (looking-at "[^\n\f]")))
411 (>= location (point)))
412 ;; Consistency check: going down and up
413 ;; shouldn't take us back before BEG.
415 (buffer-substring (point)
416 (progn (backward-sexp 1)
419 '(TeX-mode plain-TeX-mode LaTeX-mode;; tex-mode.el
420 plain-tex-mode latex-mode;; cmutex.el
422 (if (re-search-backward
423 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
425 (goto-char (match-beginning 0))
426 (buffer-substring (1+ (point));; without initial backslash
430 ((eq major-mode 'texinfo-mode)
431 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
432 (buffer-substring (match-beginning 1)
434 ((eq major-mode 'perl-mode)
435 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t)
436 (buffer-substring (match-beginning 1)
438 ((eq major-mode 'fortran-mode)
439 ;; must be inside function body for this to work
440 (beginning-of-fortran-subprogram)
441 (let ((case-fold-search t)) ; case-insensitive
442 ;; search for fortran subprogram start
443 (if (re-search-forward
444 "^[ \t]*\\(program\\|subroutine\\|function\
445 \\|[ \ta-z0-9*]*[ \t]+function\\)"
448 ;; move to EOL or before first left paren
449 (if (re-search-forward "[(\n]" nil t)
450 (progn (forward-char -1)
451 (skip-chars-backward " \t"))
453 ;; Use the name preceding that.
454 (buffer-substring (point)
455 (progn (forward-sexp -1)
458 ;; If all else fails, try heuristics
459 (let (case-fold-search)
461 (if (re-search-backward add-log-current-defun-header-regexp
464 (buffer-substring (match-beginning 1)
471 ;;; add-log.el ends here