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 ;; This MUST not be autoloaded, since user-login-name
39 ;; cannot be known at Emacs dump time.
40 (defvar add-log-full-name
(user-full-name)
41 "*Full name of user, for inclusion in ChangeLog daily headers.
42 This defaults to the value returned by the `user-full-name' function.")
44 ;; This MUST not be autoloaded, since user-login-name
45 ;; cannot be known at Emacs dump time.
46 (defvar add-log-mailing-address user-mail-address
47 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
48 This defaults to the value of `user-mail-address'.")
50 (defun change-log-name ()
51 (or change-log-default-name
52 (if (eq system-type
'vax-vms
)
54 (if (eq system-type
'ms-dos
)
59 (defun prompt-for-change-log-name ()
60 "Prompt for a change log name."
61 (let ((default (change-log-name)))
63 (read-file-name (format "Log file (default %s): " default
)
67 (defun find-change-log (&optional file-name
)
68 "Find a change log file for \\[add-change-log-entry] and return the name.
70 Optional arg FILE-NAME specifies the file to use.
71 If FILE-NAME is nil, use the value of `change-log-default-name' if non-nil.
72 Otherwise, search in the current directory and its successive parents
73 for a file named `ChangeLog' (or whatever we use on this operating system).
75 Once a file is found, `change-log-default-name' is set locally in the
76 current buffer to the complete file name."
77 ;; If user specified a file name or if this buffer knows which one to use,
80 (setq file-name change-log-default-name
)
82 ;; Chase links in the source file
83 ;; and use the change log in the dir where it points.
84 (setq file-name
(or (and buffer-file-name
86 (file-chase-links buffer-file-name
)))
88 (if (file-directory-p file-name
)
89 (setq file-name
(expand-file-name (change-log-name) file-name
)))
90 ;; Chase links before visiting the file.
91 ;; This makes it easier to use a single change log file
92 ;; for several related directories.
93 (setq file-name
(file-chase-links file-name
))
94 (setq file-name
(expand-file-name file-name
))
95 ;; Move up in the dir hierarchy till we find a change log file.
96 (let ((file1 file-name
)
98 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
99 (progn (setq parent-dir
102 (file-name-directory file1
))))
103 ;; Give up if we are already at the root dir.
104 (not (string= (file-name-directory file1
)
106 ;; Move up to the parent dir and try again.
107 (setq file1
(expand-file-name
108 (file-name-nondirectory (change-log-name))
110 ;; If we found a change log in a parent, use that.
111 (if (or (get-file-buffer file1
) (file-exists-p file1
))
112 (setq file-name file1
)))))
113 ;; Make a local variable in this buffer so we needn't search again.
114 (set (make-local-variable 'change-log-default-name
) file-name
)
118 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
119 "Find change log file and add an entry for today.
120 Optional arg (interactive prefix) non-nil means prompt for user name and site.
121 Second arg is file name of change log. If nil, uses `change-log-default-name'.
122 Third arg OTHER-WINDOW non-nil means visit in other window.
123 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
124 never append to an existing entry."
125 (interactive (list current-prefix-arg
126 (prompt-for-change-log-name)))
129 (setq add-log-full-name
(read-input "Full name: " add-log-full-name
))
130 ;; Note that some sites have room and phone number fields in
131 ;; full name which look silly when inserted. Rather than do
132 ;; anything about that here, let user give prefix argument so that
133 ;; s/he can edit the full name field in prompter if s/he wants.
134 (setq add-log-mailing-address
135 (read-input "Mailing address: " add-log-mailing-address
))))
136 (let ((defun (funcall (or add-log-current-defun-function
137 'add-log-current-defun
)))
140 (setq file-name
(expand-file-name (find-change-log file-name
)))
142 ;; Set ENTRY to the file name to use in the new entry.
143 (and buffer-file-name
144 ;; Never want to add a change log entry for the ChangeLog file itself.
145 (not (string= buffer-file-name file-name
))
146 (setq entry
(if (string-match
147 (concat "^" (regexp-quote (file-name-directory
150 (substring buffer-file-name
(match-end 0))
151 (file-name-nondirectory buffer-file-name
))))
153 (if (and other-window
(not (equal file-name buffer-file-name
)))
154 (find-file-other-window file-name
)
155 (find-file file-name
))
156 (or (eq major-mode
'change-log-mode
)
159 (goto-char (point-min))
160 (if (looking-at (concat (regexp-quote (substring (current-time-string)
162 ".* " (regexp-quote add-log-full-name
)
163 " <" (regexp-quote add-log-mailing-address
)))
165 (insert (current-time-string)
166 " " add-log-full-name
167 " <" add-log-mailing-address
">\n\n"))
169 ;; Search only within the first paragraph.
170 (if (looking-at "\n*[^\n* \t]")
171 (skip-chars-forward "\n")
172 (forward-paragraph 1))
173 (setq paragraph-end
(point))
174 (goto-char (point-min))
176 ;; Now insert the new line for this entry.
177 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t
)
178 ;; Put this file name into the existing empty entry.
181 ((and (not new-entry
)
183 (concat (regexp-quote (concat "* " entry
))
184 ;; Don't accept `foo.bar' when
185 ;; looking for `foo':
186 "\\(\\s \\|[(),:]\\)")
188 ;; Add to the existing entry for the same file.
189 (re-search-forward "^\\s *$\\|^\\s \\*")
191 (while (and (not (eobp)) (looking-at "^\\s *$"))
192 (delete-region (point) (save-excursion (forward-line 1) (point))))
195 (indent-relative-maybe))
199 (while (looking-at "\\sW")
201 (while (and (not (eobp)) (looking-at "^\\s *$"))
202 (delete-region (point) (save-excursion (forward-line 1) (point))))
205 (indent-to left-margin
)
206 (insert "* " (or entry
""))))
207 ;; Now insert the function name, if we have one.
208 ;; Point is at the entry for this file,
209 ;; either at the end of the line or at the first blank line.
212 ;; Make it easy to get rid of the function name.
214 (insert (if (save-excursion
215 (beginning-of-line 1)
216 (looking-at "\\s *$"))
220 ;; No function name, so put in a colon unless we have just a star.
221 (if (not (save-excursion
222 (beginning-of-line 1)
223 (looking-at "\\s *\\(\\*\\s *\\)?$")))
227 (defun add-change-log-entry-other-window (&optional whoami file-name
)
228 "Find change log file in other window and add an entry for today.
229 Optional arg (interactive prefix) non-nil means prompt for user name and site.
230 Second arg is file name of change log. \
231 If nil, uses `change-log-default-name'."
232 (interactive (if current-prefix-arg
233 (list current-prefix-arg
234 (prompt-for-change-log-name))))
235 (add-change-log-entry whoami file-name t
))
236 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
239 (defun change-log-mode ()
240 "Major mode for editing change logs; like Indented Text Mode.
241 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
242 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
243 Each entry behaves as a paragraph, and the entries for one day as a page.
244 Runs `change-log-mode-hook'."
246 (kill-all-local-variables)
248 (setq major-mode
'change-log-mode
249 mode-name
"Change Log"
252 (use-local-map change-log-mode-map
)
253 ;; Let each entry behave as one paragraph:
254 (set (make-local-variable 'paragraph-start
) "^\\s *$\\|^\f")
255 (set (make-local-variable 'paragraph-separate
) "^\\s *$\\|^\f\\|^\\sw")
256 ;; Let all entries for one day behave as one page.
257 ;; Match null string on the date-line so that the date-line
258 ;; is grouped with what follows.
259 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
260 (set (make-local-variable 'version-control
) 'never
)
261 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
262 (run-hooks 'change-log-mode-hook
))
264 (defvar change-log-mode-map nil
265 "Keymap for Change Log major mode.")
266 (if change-log-mode-map
268 (setq change-log-mode-map
(make-sparse-keymap))
269 (define-key change-log-mode-map
"\M-q" 'change-log-fill-paragraph
))
271 ;; It might be nice to have a general feature to replace this. The idea I
272 ;; have is a variable giving a regexp matching text which should not be
273 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
274 ;; But I don't feel up to implementing that today.
275 (defun change-log-fill-paragraph (&optional justify
)
276 "Fill the paragraph, but preserve open parentheses at beginning of lines.
277 Prefix arg means justify as well."
279 (let ((paragraph-separate (concat paragraph-separate
"\\|^\\s *\\s("))
280 (paragraph-start (concat paragraph-start
"\\|^\\s *\\s(")))
281 (fill-paragraph justify
)))
283 (defvar add-log-current-defun-header-regexp
284 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
285 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
288 (defun add-log-current-defun ()
289 "Return name of function definition point is in, or nil.
291 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
292 Texinfo (@node titles), and Fortran.
294 Other modes are handled by a heuristic that looks in the 10K before
295 point for uppercase headings starting in the first column or
296 identifiers followed by `:' or `=', see variable
297 `add-log-current-defun-header-regexp'.
299 Has a preference of looking backwards."
302 (let ((location (point)))
303 (cond ((memq major-mode
'(emacs-lisp-mode lisp-mode scheme-mode
))
304 ;; If we are now precisely a the beginning of a defun,
305 ;; make sure beginning-of-defun finds that one
306 ;; rather than the previous one.
307 (or (eobp) (forward-char 1))
309 ;; Make sure we are really inside the defun found, not after it.
310 (if (and (progn (end-of-defun)
311 (< location
(point)))
312 (progn (forward-sexp -
1)
313 (>= location
(point))))
315 (if (looking-at "\\s(")
318 (skip-chars-forward " ")
319 (buffer-substring (point)
320 (progn (forward-sexp 1) (point))))))
321 ((and (memq major-mode
'(c-mode c
++-mode c
++-c-mode
))
322 (save-excursion (beginning-of-line)
323 ;; Use eq instead of = here to avoid
324 ;; error when at bob and char-after
326 (while (eq (char-after (- (point) 2)) ?
\\)
328 (looking-at "[ \t]*#[ \t]*define[ \t]")))
329 ;; Handle a C macro definition.
331 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
333 (search-forward "define")
334 (skip-chars-forward " \t")
335 (buffer-substring (point)
336 (progn (forward-sexp 1) (point))))
337 ((memq major-mode
'(c-mode c
++-mode c
++-c-mode
))
339 ;; See if we are in the beginning part of a function,
340 ;; before the open brace. If so, advance forward.
341 (while (not (looking-at "{\\|\\(\\s *$\\)"))
346 (if (progn (end-of-defun)
347 (< location
(point)))
353 ;; Skip back over typedefs of arglist.
354 (while (and (not (bobp))
355 (looking-at "[ \t\n]"))
357 ;; See if this is using the DEFUN macro used in Emacs,
358 ;; or the DEFUN macro used by the C library.
359 (if (condition-case nil
365 (looking-at "DEFUN\\b"))
371 (if (= (char-after (point)) ?
\")
374 (skip-chars-forward " ,")))
375 (buffer-substring (point)
376 (progn (forward-sexp 1) (point))))
377 ;; Ordinary C function syntax.
379 (if (condition-case nil
380 ;; Protect against "Unbalanced parens" error.
382 (down-list 1) ; into arglist
384 (skip-chars-backward " \t")
387 ;; Verify initial pos was after
388 ;; real start of function.
389 (if (and (save-excursion
391 ;; For this purpose, include the line
392 ;; that has the decl keywords. This
393 ;; may also include some of the
394 ;; comments before the function.
395 (while (and (not (bobp))
398 (looking-at "[^\n\f]")))
400 (>= location
(point)))
401 ;; Consistency check: going down and up
402 ;; shouldn't take us back before BEG.
404 (buffer-substring (point)
405 (progn (backward-sexp 1)
408 '(TeX-mode plain-TeX-mode LaTeX-mode
;; tex-mode.el
409 plain-tex-mode latex-mode
;; cmutex.el
411 (if (re-search-backward
412 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
414 (goto-char (match-beginning 0))
415 (buffer-substring (1+ (point));; without initial backslash
419 ((eq major-mode
'texinfo-mode
)
420 (if (re-search-backward "^@node[ \t]+\\([^,]+\\)," nil t
)
421 (buffer-substring (match-beginning 1)
423 ((eq major-mode
'fortran-mode
)
424 ;; must be inside function body for this to work
425 (beginning-of-fortran-subprogram)
426 (let ((case-fold-search t
)) ; case-insensitive
427 ;; search for fortran subprogram start
428 (if (re-search-forward
429 "^[ \t]*\\(program\\|subroutine\\|function\
430 \\|[ \ta-z0-9*]*[ \t]+function\\)"
433 ;; move to EOL or before first left paren
434 (if (re-search-forward "[(\n]" nil t
)
435 (progn (forward-char -
1)
436 (skip-chars-backward " \t"))
438 ;; Use the name preceding that.
439 (buffer-substring (point)
440 (progn (forward-sexp -
1)
443 ;; If all else fails, try heuristics
444 (let (case-fold-search)
446 (if (re-search-backward add-log-current-defun-header-regexp
449 (buffer-substring (match-beginning 1)
456 ;;; add-log.el ends here