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
)
211 (let (case-fold-search)
213 (concat (regexp-quote (concat "* " entry
))
214 ;; Don't accept `foo.bar' when
215 ;; looking for `foo':
216 "\\(\\s \\|[(),:]\\)")
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))))
226 (indent-relative-maybe))
230 (while (looking-at "\\sW")
232 (while (and (not (eobp)) (looking-at "^\\s *$"))
233 (delete-region (point) (save-excursion (forward-line 1) (point))))
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.
243 ;; Make it easy to get rid of the function name.
245 (insert (if (save-excursion
246 (beginning-of-line 1)
247 (looking-at "\\s *$"))
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 *\\)?$")))
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)
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'."
277 (kill-all-local-variables)
279 (setq major-mode
'change-log-mode
280 mode-name
"Change Log"
283 (use-local-map change-log-mode-map
)
284 ;; Let each entry behave as one paragraph:
285 (set (make-local-variable 'paragraph-start
) "^\\s *$\\|^\f")
286 (set (make-local-variable 'paragraph-separate
) "^\\s *$\\|^\f\\|^\\sw")
287 ;; Let all entries for one day behave as one page.
288 ;; Match null string on the date-line so that the date-line
289 ;; is grouped with what follows.
290 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
291 (set (make-local-variable 'version-control
) 'never
)
292 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
293 (set (make-local-variable 'font-lock-defaults
)
294 '(change-log-font-lock-keywords t
))
295 (run-hooks 'change-log-mode-hook
))
297 ;; It might be nice to have a general feature to replace this. The idea I
298 ;; have is a variable giving a regexp matching text which should not be
299 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
300 ;; But I don't feel up to implementing that today.
301 (defun change-log-fill-paragraph (&optional justify
)
302 "Fill the paragraph, but preserve open parentheses at beginning of lines.
303 Prefix arg means justify as well."
305 (let ((paragraph-separate (concat paragraph-separate
"\\|^\\s *\\s("))
306 (paragraph-start (concat paragraph-start
"\\|^\\s *\\s(")))
307 (fill-paragraph justify
)))
309 (defvar add-log-current-defun-header-regexp
310 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
311 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
314 (defun add-log-current-defun ()
315 "Return name of function definition point is in, or nil.
317 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
318 Texinfo (@node titles), Perl, and Fortran.
320 Other modes are handled by a heuristic that looks in the 10K before
321 point for uppercase headings starting in the first column or
322 identifiers followed by `:' or `=', see variable
323 `add-log-current-defun-header-regexp'.
325 Has a preference of looking backwards."
328 (let ((location (point)))
329 (cond ((memq major-mode
'(emacs-lisp-mode lisp-mode scheme-mode
330 lisp-interaction-mode
))
331 ;; If we are now precisely at the beginning of a defun,
332 ;; make sure beginning-of-defun finds that one
333 ;; rather than the previous one.
334 (or (eobp) (forward-char 1))
336 ;; Make sure we are really inside the defun found, not after it.
337 (if (and (progn (end-of-defun)
338 (< location
(point)))
339 (progn (forward-sexp -
1)
340 (>= location
(point))))
342 (if (looking-at "\\s(")
345 (skip-chars-forward " '")
346 (buffer-substring (point)
347 (progn (forward-sexp 1) (point))))))
348 ((and (memq major-mode
'(c-mode c
++-mode c
++-c-mode objc-mode
))
349 (save-excursion (beginning-of-line)
350 ;; Use eq instead of = here to avoid
351 ;; error when at bob and char-after
353 (while (eq (char-after (- (point) 2)) ?
\\)
355 (looking-at "[ \t]*#[ \t]*define[ \t]")))
356 ;; Handle a C macro definition.
358 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
360 (search-forward "define")
361 (skip-chars-forward " \t")
362 (buffer-substring (point)
363 (progn (forward-sexp 1) (point))))
364 ((memq major-mode
'(c-mode c
++-mode c
++-c-mode objc-mode
))
366 ;; See if we are in the beginning part of a function,
367 ;; before the open brace. If so, advance forward.
368 (while (not (looking-at "{\\|\\(\\s *$\\)"))
373 (if (progn (end-of-defun)
374 (< location
(point)))
380 ;; Skip back over typedefs of arglist.
381 (while (and (not (bobp))
382 (looking-at "[ \t\n]"))
384 ;; See if this is using the DEFUN macro used in Emacs,
385 ;; or the DEFUN macro used by the C library.
386 (if (condition-case nil
389 (while (= (preceding-char) ?
\\)
394 (looking-at "DEFUN\\b"))
400 (if (= (char-after (point)) ?
\")
403 (skip-chars-forward " ,")))
404 (buffer-substring (point)
405 (progn (forward-sexp 1) (point))))
406 (if (looking-at "^[+-]")
407 (get-method-definition)
408 ;; Ordinary C function syntax.
410 (if (and (condition-case nil
411 ;; Protect against "Unbalanced parens" error.
413 (down-list 1) ; into arglist
415 (skip-chars-backward " \t")
418 ;; Verify initial pos was after
419 ;; real start of function.
422 ;; For this purpose, include the line
423 ;; that has the decl keywords. This
424 ;; may also include some of the
425 ;; comments before the function.
426 (while (and (not (bobp))
429 (looking-at "[^\n\f]")))
431 (>= location
(point)))
432 ;; Consistency check: going down and up
433 ;; shouldn't take us back before BEG.
436 ;; Don't include any final newline
437 ;; in the name we use.
438 (if (= (preceding-char) ?
\n)
442 ;; Now find the right beginning of the name.
443 ;; Include certain keywords if they
445 (setq middle
(point))
448 (looking-at "struct \\|union \\|class ")
449 (setq middle
(point)))
450 (buffer-substring middle end
)))))))))
452 '(TeX-mode plain-TeX-mode LaTeX-mode
;; tex-mode.el
453 plain-tex-mode latex-mode
;; cmutex.el
455 (if (re-search-backward
456 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
458 (goto-char (match-beginning 0))
459 (buffer-substring (1+ (point));; without initial backslash
463 ((eq major-mode
'texinfo-mode
)
464 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
465 (buffer-substring (match-beginning 1)
467 ((eq major-mode
'perl-mode
)
468 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t
)
469 (buffer-substring (match-beginning 1)
471 ((eq major-mode
'fortran-mode
)
472 ;; must be inside function body for this to work
473 (beginning-of-fortran-subprogram)
474 (let ((case-fold-search t
)) ; case-insensitive
475 ;; search for fortran subprogram start
476 (if (re-search-forward
477 "^[ \t]*\\(program\\|subroutine\\|function\
478 \\|[ \ta-z0-9*]*[ \t]+function\\)"
481 ;; move to EOL or before first left paren
482 (if (re-search-forward "[(\n]" nil t
)
483 (progn (forward-char -
1)
484 (skip-chars-backward " \t"))
486 ;; Use the name preceding that.
487 (buffer-substring (point)
488 (progn (forward-sexp -
1)
491 ;; If all else fails, try heuristics
492 (let (case-fold-search)
494 (if (re-search-backward add-log-current-defun-header-regexp
497 (buffer-substring (match-beginning 1)
501 ;; Subroutine used within get-method-definition.
502 ;; Add the last match in the buffer to the end of `md',
503 ;; followed by the string END; move to the end of that match.
504 (defun get-method-definition-1 (end)
506 (buffer-substring (match-beginning 1) (match-end 1))
508 (goto-char (match-end 0)))
510 ;; For objective C, return the method name if we are in a method.
511 (defun get-method-definition ()
514 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
515 (get-method-definition-1 " ")))
518 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
519 (get-method-definition-1 "")
520 (while (not (looking-at "[{;]"))
522 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
523 (get-method-definition-1 ""))
529 ;;; add-log.el ends here