1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1988, 1993 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
(concat (user-login-name) "@" (system-name))
47 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
48 This defaults to the value returned by `user-login-name', followed by
49 an `@' character, followed by the value returned by `system-name'.")
51 (defun change-log-name ()
52 (or change-log-default-name
53 (if (eq system-type
'vax-vms
)
55 (if (eq system-type
'ms-dos
)
60 (defun prompt-for-change-log-name ()
61 "Prompt for a change log name."
62 (let ((default (change-log-name)))
64 (read-file-name (format "Log file (default %s): " default
)
68 (defun find-change-log (&optional file-name
)
69 "Find a change log file for \\[add-change-log-entry] and return the name.
70 Optional arg FILE-NAME is a name to try first.
71 If FILE-NAME is nil, use the value of `change-log-default-name' if non-nil.
72 Failing that, use \"ChangeLog\" in the current directory.
73 If the file does not exist in the named directory, successive parent
74 directories are tried.
76 Once a file is found, `change-log-default-name' is set locally in the
77 current buffer to the complete file name."
79 (setq file-name
(or change-log-default-name
80 ;; Chase links in the source file
81 ;; and use the change log in the dir where it points.
84 (file-chase-links buffer-file-name
)))
86 (if (and (eq file-name change-log-default-name
)
87 (assq 'change-log-default-name
(buffer-local-variables)))
88 ;; Don't do the searching if we already have a buffer-local value.
91 (if (file-directory-p file-name
)
92 (setq file-name
(expand-file-name (change-log-name) file-name
)))
93 ;; Chase links before visiting the file.
94 ;; This makes it easier to use a single change log file
95 ;; for several related directories.
96 (setq file-name
(file-chase-links file-name
))
97 (setq file-name
(expand-file-name file-name
))
98 ;; Move up in the dir hierarchy till we find a change log file.
99 (let ((file1 file-name
)
101 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
102 (progn (setq parent-dir
105 (file-name-directory file1
))))
106 ;; Give up if we are already at the root dir.
107 (not (string= (file-name-directory file1
)
109 ;; Move up to the parent dir and try again.
110 (setq file1
(expand-file-name
111 (file-name-nondirectory (change-log-name))
113 ;; If we found a change log in a parent, use that.
114 (if (or (get-file-buffer file1
) (file-exists-p file1
))
115 (setq file-name file1
)))
116 ;; Make a local variable in this buffer so we needn't search again.
117 (set (make-local-variable 'change-log-default-name
) file-name
)
121 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
122 "Find change log file and add an entry for today.
123 Optional arg (interactive prefix) non-nil means prompt for user name and site.
124 Second arg is file name of change log. If nil, uses `change-log-default-name'.
125 Third arg OTHER-WINDOW non-nil means visit in other window.
126 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
127 never append to an existing entry."
128 (interactive (list current-prefix-arg
129 (prompt-for-change-log-name)))
132 (setq add-log-full-name
(read-input "Full name: " add-log-full-name
))
133 ;; Note that some sites have room and phone number fields in
134 ;; full name which look silly when inserted. Rather than do
135 ;; anything about that here, let user give prefix argument so that
136 ;; s/he can edit the full name field in prompter if s/he wants.
137 (setq add-log-mailing-address
138 (read-input "Mailing address: " add-log-mailing-address
))))
139 (let ((defun (funcall (or add-log-current-defun-function
140 'add-log-current-defun
)))
143 (setq file-name
(find-change-log file-name
))
145 ;; Set ENTRY to the file name to use in the new entry.
146 (and buffer-file-name
147 ;; Never want to add a change log entry for the ChangeLog file itself.
148 (not (string= buffer-file-name file-name
))
149 (setq entry
(if (string-match
150 (concat "^" (regexp-quote (file-name-directory
153 (substring buffer-file-name
(match-end 0))
154 (file-name-nondirectory buffer-file-name
))))
156 (if (and other-window
(not (equal file-name buffer-file-name
)))
157 (find-file-other-window file-name
)
158 (find-file file-name
))
159 (or (eq major-mode
'change-log-mode
)
162 (goto-char (point-min))
163 (if (looking-at (concat (regexp-quote (substring (current-time-string)
165 ".* " (regexp-quote add-log-full-name
)
166 " (" (regexp-quote add-log-mailing-address
)))
168 (insert (current-time-string)
169 " " add-log-full-name
170 " (" add-log-mailing-address
")\n\n"))
172 ;; Search only within the first paragraph.
173 (if (looking-at "\n*[^\n* \t]")
174 (skip-chars-forward "\n")
175 (forward-paragraph 1))
176 (setq paragraph-end
(point))
177 (goto-char (point-min))
179 ;; Now insert the new line for this entry.
180 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t
)
181 ;; Put this file name into the existing empty entry.
184 ((and (not new-entry
)
186 (concat (regexp-quote (concat "* " entry
))
187 ;; Don't accept `foo.bar' when
188 ;; looking for `foo':
189 "\\(\\s \\|[(),:]\\)")
191 ;; Add to the existing entry for the same file.
192 (re-search-forward "^\\s *$\\|^\\s \\*")
194 (while (and (not (eobp)) (looking-at "^\\s *$"))
195 (delete-region (point) (save-excursion (forward-line 1) (point))))
198 (indent-relative-maybe))
202 (while (looking-at "\\sW")
204 (while (and (not (eobp)) (looking-at "^\\s *$"))
205 (delete-region (point) (save-excursion (forward-line 1) (point))))
208 (indent-to left-margin
)
209 (insert "* " (or entry
""))))
210 ;; Now insert the function name, if we have one.
211 ;; Point is at the entry for this file,
212 ;; either at the end of the line or at the first blank line.
215 ;; Make it easy to get rid of the function name.
217 (insert (if (save-excursion
218 (beginning-of-line 1)
219 (looking-at "\\s *$"))
223 ;; No function name, so put in a colon unless we have just a star.
224 (if (not (save-excursion
225 (beginning-of-line 1)
226 (looking-at "\\s *\\(\\*\\s *\\)?$")))
230 (defun add-change-log-entry-other-window (&optional whoami file-name
)
231 "Find change log file in other window and add an entry for today.
232 Optional arg (interactive prefix) non-nil means prompt for user name and site.
233 Second arg is file name of change log. \
234 If nil, uses `change-log-default-name'."
235 (interactive (if current-prefix-arg
236 (list current-prefix-arg
237 (prompt-for-change-log-name))))
238 (add-change-log-entry whoami file-name t
))
239 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
242 (defun change-log-mode ()
243 "Major mode for editing change logs; like Indented Text Mode.
244 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
245 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
246 Each entry behaves as a paragraph, and the entries for one day as a page.
247 Runs `change-log-mode-hook'."
249 (kill-all-local-variables)
251 (setq major-mode
'change-log-mode
252 mode-name
"Change Log"
255 (use-local-map change-log-mode-map
)
256 ;; Let each entry behave as one paragraph:
257 (set (make-local-variable 'paragraph-start
) "^\\s *$\\|^\f")
258 (set (make-local-variable 'paragraph-separate
) "^\\s *$\\|^\f\\|^\\sw")
259 ;; Let all entries for one day behave as one page.
260 ;; Match null string on the date-line so that the date-line
261 ;; is grouped with what follows.
262 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
263 (set (make-local-variable 'version-control
) 'never
)
264 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
265 (run-hooks 'change-log-mode-hook
))
267 (defvar change-log-mode-map nil
268 "Keymap for Change Log major mode.")
269 (if change-log-mode-map
271 (setq change-log-mode-map
(make-sparse-keymap))
272 (define-key change-log-mode-map
"\M-q" 'change-log-fill-paragraph
))
274 ;; It might be nice to have a general feature to replace this. The idea I
275 ;; have is a variable giving a regexp matching text which should not be
276 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
277 ;; But I don't feel up to implementing that today.
278 (defun change-log-fill-paragraph (&optional justify
)
279 "Fill the paragraph, but preserve open parentheses at beginning of lines.
280 Prefix arg means justify as well."
282 (let ((paragraph-separate (concat paragraph-separate
"\\|^\\s *\\s("))
283 (paragraph-start (concat paragraph-start
"\\|^\\s *\\s(")))
284 (fill-paragraph justify
)))
286 (defvar add-log-current-defun-header-regexp
287 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
288 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
290 (defun add-log-current-defun ()
291 "Return name of function definition point is in, or nil.
293 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
294 Texinfo (@node titles), and Fortran.
296 Other modes are handled by a heuristic that looks in the 10K before
297 point for uppercase headings starting in the first column or
298 identifiers followed by `:' or `=', see variable
299 `add-log-current-defun-header-regexp'.
301 Has a preference of looking backwards."
304 (let ((location (point)))
305 (cond ((memq major-mode
'(emacs-lisp-mode lisp-mode scheme-mode
))
306 ;; If we are now precisely a the beginning of a defun,
307 ;; make sure beginning-of-defun finds that one
308 ;; rather than the previous one.
309 (or (eobp) (forward-char 1))
311 ;; Make sure we are really inside the defun found, not after it.
312 (if (and (progn (end-of-defun)
313 (< location
(point)))
314 (progn (forward-sexp -
1)
315 (>= location
(point))))
317 (if (looking-at "\\s(")
320 (skip-chars-forward " ")
321 (buffer-substring (point)
322 (progn (forward-sexp 1) (point))))))
323 ((and (memq major-mode
'(c-mode c
++-mode c
++-c-mode
))
324 (save-excursion (beginning-of-line)
325 ;; Use eq instead of = here to avoid
326 ;; error when at bob and char-after
328 (while (eq (char-after (- (point) 2)) ?
\\)
330 (looking-at "[ \t]*#[ \t]*define[ \t]")))
331 ;; Handle a C macro definition.
333 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
335 (search-forward "define")
336 (skip-chars-forward " \t")
337 (buffer-substring (point)
338 (progn (forward-sexp 1) (point))))
339 ((memq major-mode
'(c-mode c
++-mode c
++-c-mode
))
341 ;; See if we are in the beginning part of a function,
342 ;; before the open brace. If so, advance forward.
343 (while (not (looking-at "{\\|\\(\\s *$\\)"))
348 (if (progn (end-of-defun)
349 (< location
(point)))
355 ;; Skip back over typedefs of arglist.
356 (while (and (not (bobp))
357 (looking-at "[ \t\n]"))
359 ;; See if this is using the DEFUN macro used in Emacs,
360 ;; or the DEFUN macro used by the C library.
361 (if (condition-case nil
367 (looking-at "DEFUN\\b"))
373 (if (= (char-after (point)) ?
\")
376 (skip-chars-forward " ,")))
377 (buffer-substring (point)
378 (progn (forward-sexp 1) (point))))
379 ;; Ordinary C function syntax.
381 (if (condition-case nil
382 ;; Protect against "Unbalanced parens" error.
384 (down-list 1) ; into arglist
386 (skip-chars-backward " \t")
389 ;; Verify initial pos was after
390 ;; real start of function.
391 (if (and (save-excursion
393 ;; For this purpose, include the line
394 ;; that has the decl keywords. This
395 ;; may also include some of the
396 ;; comments before the function.
397 (while (and (not (bobp))
400 (looking-at "[^\n\f]")))
402 (>= location
(point)))
403 ;; Consistency check: going down and up
404 ;; shouldn't take us back before BEG.
406 (buffer-substring (point)
407 (progn (backward-sexp 1)
410 '(TeX-mode plain-TeX-mode LaTeX-mode
;; tex-mode.el
411 plain-tex-mode latex-mode
;; cmutex.el
413 (if (re-search-backward
414 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
416 (goto-char (match-beginning 0))
417 (buffer-substring (1+ (point));; without initial backslash
421 ((eq major-mode
'texinfo-mode
)
422 (if (re-search-backward "^@node[ \t]+\\([^,]+\\)," nil t
)
423 (buffer-substring (match-beginning 1)
425 ((eq major-mode
'fortran-mode
)
426 ;; must be inside function body for this to work
427 (beginning-of-fortran-subprogram)
428 (let ((case-fold-search t
)) ; case-insensitive
429 ;; search for fortran subprogram start
430 (if (re-search-forward
431 "^[ \t]*\\(program\\|subroutine\\|function\
432 \\|[ \ta-z0-9*]*[ \t]+function\\)"
435 ;; move to EOL or before first left paren
436 (if (re-search-forward "[(\n]" nil t
)
437 (progn (forward-char -
1)
438 (skip-chars-backward " \t"))
440 ;; Use the name preceding that.
441 (buffer-substring (point)
442 (progn (forward-sexp -
1)
445 ;; If all else fails, try heuristics
446 (let (case-fold-search)
448 (if (re-search-backward add-log-current-defun-header-regexp
451 (buffer-substring (match-beginning 1)
458 ;;; add-log.el ends here