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 (or (eq system-type
'ms-dos
) (eq system-type
'windows-nt
))
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
)
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
)
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
))
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,
103 (setq file-name
(and change-log-default-name
104 (file-name-directory change-log-default-name
)
105 change-log-default-name
))
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
111 (file-chase-links buffer-file-name
)))
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
)
123 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
124 (progn (setq parent-dir
127 (file-name-directory file1
))))
128 ;; Give up if we are already at the root dir.
129 (not (string= (file-name-directory file1
)
131 ;; Move up to the parent dir and try again.
132 (setq file1
(expand-file-name
133 (file-name-nondirectory (change-log-name))
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
)
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)))
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
)))
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
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
)
188 (goto-char (point-min))
189 (if (looking-at (concat (regexp-quote (substring (current-time-string)
191 ".* " (regexp-quote add-log-full-name
)
192 " <" (regexp-quote add-log-mailing-address
)))
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.
210 ((and (not new-entry
)
212 (concat (regexp-quote (concat "* " entry
))
213 ;; Don't accept `foo.bar' when
214 ;; looking for `foo':
215 "\\(\\s \\|[(),:]\\)")
217 ;; Add to the existing entry for the same file.
218 (re-search-forward "^\\s *$\\|^\\s \\*")
220 (while (and (not (eobp)) (looking-at "^\\s *$"))
221 (delete-region (point) (save-excursion (forward-line 1) (point))))
224 (indent-relative-maybe))
228 (while (looking-at "\\sW")
230 (while (and (not (eobp)) (looking-at "^\\s *$"))
231 (delete-region (point) (save-excursion (forward-line 1) (point))))
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.
241 ;; Make it easy to get rid of the function name.
243 (insert (if (save-excursion
244 (beginning-of-line 1)
245 (looking-at "\\s *$"))
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 *\\)?$")))
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)
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'."
275 (kill-all-local-variables)
277 (setq major-mode
'change-log-mode
278 mode-name
"Change Log"
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
))
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."
303 (let ((paragraph-separate (concat paragraph-separate
"\\|^\\s *\\s("))
304 (paragraph-start (concat paragraph-start
"\\|^\\s *\\s(")))
305 (fill-paragraph justify
)))
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.")
312 (defun add-log-current-defun ()
313 "Return name of function definition point is in, or nil.
315 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
316 Texinfo (@node titles), Perl, and Fortran.
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'.
323 Has a preference of looking backwards."
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))
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))))
340 (if (looking-at "\\s(")
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
351 (while (eq (char-after (- (point) 2)) ?
\\)
353 (looking-at "[ \t]*#[ \t]*define[ \t]")))
354 ;; Handle a C macro definition.
356 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
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
))
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 *$\\)"))
371 (if (progn (end-of-defun)
372 (< location
(point)))
378 ;; Skip back over typedefs of arglist.
379 (while (and (not (bobp))
380 (looking-at "[ \t\n]"))
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
387 (while (= (preceding-char) ?
\\)
392 (looking-at "DEFUN\\b"))
398 (if (= (char-after (point)) ?
\")
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.
408 (if (condition-case nil
409 ;; Protect against "Unbalanced parens" error.
411 (down-list 1) ; into arglist
413 (skip-chars-backward " \t")
416 ;; Verify initial pos was after
417 ;; real start of function.
418 (if (and (save-excursion
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))
427 (looking-at "[^\n\f]")))
429 (>= location
(point)))
430 ;; Consistency check: going down and up
431 ;; shouldn't take us back before BEG.
433 (buffer-substring (point)
434 (progn (backward-sexp 1)
437 '(TeX-mode plain-TeX-mode LaTeX-mode
;; tex-mode.el
438 plain-tex-mode latex-mode
;; cmutex.el
440 (if (re-search-backward
441 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
443 (goto-char (match-beginning 0))
444 (buffer-substring (1+ (point));; without initial backslash
448 ((eq major-mode
'texinfo-mode
)
449 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
450 (buffer-substring (match-beginning 1)
452 ((eq major-mode
'perl-mode
)
453 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t
)
454 (buffer-substring (match-beginning 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\\)"
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"))
471 ;; Use the name preceding that.
472 (buffer-substring (point)
473 (progn (forward-sexp -
1)
476 ;; If all else fails, try heuristics
477 (let (case-fold-search)
479 (if (re-search-backward add-log-current-defun-header-regexp
482 (buffer-substring (match-beginning 1)
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)
491 (buffer-substring (match-beginning 1) (match-end 1))
493 (goto-char (match-end 0)))
495 ;; For objective C, return the method name if we are in a method.
496 (defun get-method-definition ()
499 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
500 (get-method-definition-1 " ")))
503 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
504 (get-method-definition-1 "")
505 (while (not (looking-at "[{;]"))
507 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
508 (get-method-definition-1 ""))
514 ;;; add-log.el ends here