(vc-comment-to-change-log): Canonicalize following whitespace after
[bpt/emacs.git] / lisp / add-log.el
1 ;;; add-log.el --- change log maintenance commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1988, 1993 Free Software Foundation, Inc.
4
5 ;; Keywords: maint
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
22
23 ;;; Commentary:
24
25 ;; This facility is documented in the Emacs Manual.
26
27 ;;; Code:
28
29 ;;;###autoload
30 (defvar change-log-default-name nil
31 "*Name of a change log file for \\[add-change-log-entry].")
32
33 ;;;###autoload
34 (defvar add-log-current-defun-function nil
35 "\
36 *If non-nil, function to guess name of current function from surrounding text.
37 \\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
38 instead) with no arguments. It returns a string or nil if it cannot guess.")
39
40 (defun change-log-name ()
41 (or change-log-default-name
42 (if (eq system-type 'vax-vms) "$CHANGE_LOG$.TXT" "ChangeLog")))
43
44 (defun prompt-for-change-log-name ()
45 "Prompt for a change log name."
46 (let ((default (change-log-name)))
47 (expand-file-name
48 (read-file-name (format "Log file (default %s): " default)
49 nil default))))
50
51 ;;;###autoload
52 (defun find-change-log (&optional file-name)
53 "Find a change log file for \\[add-change-log-entry] and return the name.
54 Optional arg FILE-NAME is a name to try first.
55 If FILE-NAME is nil, use the value of `change-log-default-name' if non-nil.
56 Failing that, use \"ChangeLog\" in the current directory.
57 If the file does not exist in the named directory, successive parent
58 directories are tried.
59
60 Once a file is found, `change-log-default-name' is set locally in the
61 current buffer to the complete file name."
62 (or file-name
63 (setq file-name (or change-log-default-name
64 ;; Chase links in the source file
65 ;; and use the change log in the dir where it points.
66 (and buffer-file-name
67 (file-name-directory
68 (file-chase-links buffer-file-name)))
69 default-directory)))
70 (if (and (eq file-name change-log-default-name)
71 (assq 'change-log-default-name (buffer-local-variables)))
72 ;; Don't do the searching if we already have a buffer-local value.
73 file-name
74
75 (if (file-directory-p file-name)
76 (setq file-name (expand-file-name (change-log-name) file-name)))
77 ;; Chase links before visiting the file.
78 ;; This makes it easier to use a single change log file
79 ;; for several related directories.
80 (setq file-name (file-chase-links file-name))
81 (setq file-name (expand-file-name file-name))
82 ;; Move up in the dir hierarchy till we find a change log file.
83 (let ((file1 file-name)
84 parent-dir)
85 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
86 (progn (setq parent-dir
87 (file-name-directory
88 (directory-file-name
89 (file-name-directory file1))))
90 ;; Give up if we are already at the root dir.
91 (not (string= (file-name-directory file1)
92 parent-dir))))
93 ;; Move up to the parent dir and try again.
94 (setq file1 (expand-file-name (change-log-name) parent-dir)))
95 ;; If we found a change log in a parent, use that.
96 (if (or (get-file-buffer file1) (file-exists-p file1))
97 (setq file-name file1)))
98 ;; Make a local variable in this buffer so we needn't search again.
99 (set (make-local-variable 'change-log-default-name) file-name)
100 file-name))
101
102 ;;;###autoload
103 (defun add-change-log-entry (&optional whoami file-name other-window)
104 "Find change log file and add an entry for today.
105 Optional arg (interactive prefix) non-nil means prompt for user name and site.
106 Second arg is file name of change log. If nil, uses `change-log-default-name'.
107 Third arg OTHER-WINDOW non-nil means visit in other window."
108 (interactive (list current-prefix-arg
109 (prompt-for-change-log-name)))
110 (let* ((full-name (if whoami
111 (read-input "Full name: " (user-full-name))
112 (user-full-name)))
113 ;; Note that some sites have room and phone number fields in
114 ;; full name which look silly when inserted. Rather than do
115 ;; anything about that here, let user give prefix argument so that
116 ;; s/he can edit the full name field in prompter if s/he wants.
117 (login-name (if whoami
118 (read-input "Login name: " (user-login-name))
119 (user-login-name)))
120 (site-name (if whoami
121 (read-input "Site name: " (system-name))
122 (system-name)))
123 (defun (funcall (or add-log-current-defun-function
124 'add-log-current-defun)))
125 paragraph-end entry)
126
127 (setq file-name (find-change-log file-name))
128
129 ;; Set ENTRY to the file name to use in the new entry.
130 (and buffer-file-name
131 ;; Never want to add a change log entry for the ChangeLog file itself.
132 (not (string= buffer-file-name file-name))
133 (setq entry (if (string-match
134 (concat "^" (regexp-quote (file-name-directory
135 file-name)))
136 buffer-file-name)
137 (substring buffer-file-name (match-end 0))
138 (file-name-nondirectory buffer-file-name))))
139
140 (if (and other-window (not (equal file-name buffer-file-name)))
141 (find-file-other-window file-name)
142 (find-file file-name))
143 (undo-boundary)
144 (goto-char (point-min))
145 (if (looking-at (concat (regexp-quote (substring (current-time-string)
146 0 10))
147 ".* " (regexp-quote full-name)
148 " (" (regexp-quote login-name) "@"))
149 (forward-line 1)
150 (insert (current-time-string)
151 " " full-name
152 " (" login-name "@" site-name ")\n\n"))
153
154 ;; Search only within the first paragraph.
155 (if (looking-at "\n*[^\n* \t]")
156 (skip-chars-forward "\n")
157 (forward-paragraph 1))
158 (setq paragraph-end (point))
159 (goto-char (point-min))
160
161 ;; Now insert the new line for this entry.
162 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t)
163 ;; Put this file name into the existing empty entry.
164 (if entry
165 (insert entry)))
166 ((and (re-search-forward
167 (concat (regexp-quote (concat "* " entry))
168 ;; Don't accept `foo.bar' when
169 ;; looking for `foo':
170 "\\(\\s \\|[(),:]\\)")
171 paragraph-end t))
172 ;; Add to the existing entry for the same file.
173 (re-search-forward "^\\s *$\\|^\\s \\*")
174 (beginning-of-line)
175 (while (and (not (eobp)) (looking-at "^\\s *$"))
176 (delete-region (point) (save-excursion (forward-line 1) (point))))
177 (insert "\n\n")
178 (forward-line -2)
179 (indent-relative-maybe))
180 (t
181 ;; Make a new entry.
182 (forward-line 1)
183 (while (looking-at "\\sW")
184 (forward-line 1))
185 (while (and (not (eobp)) (looking-at "^\\s *$"))
186 (delete-region (point) (save-excursion (forward-line 1) (point))))
187 (insert "\n\n\n")
188 (forward-line -2)
189 (indent-to left-margin)
190 (insert "* " (or entry ""))))
191 ;; Now insert the function name, if we have one.
192 ;; Point is at the entry for this file,
193 ;; either at the end of the line or at the first blank line.
194 (if defun
195 (progn
196 ;; Make it easy to get rid of the function name.
197 (undo-boundary)
198 (insert (if (save-excursion
199 (beginning-of-line 1)
200 (looking-at "\\s *$"))
201 ""
202 " ")
203 "(" defun "): "))
204 ;; No function name, so put in a colon unless we have just a star.
205 (if (not (save-excursion
206 (beginning-of-line 1)
207 (looking-at "\\s *\\(\\*\\s *\\)?$")))
208 (insert ": ")))))
209
210 ;;;###autoload
211 (defun add-change-log-entry-other-window (&optional whoami file-name)
212 "Find change log file in other window and add an entry for today.
213 Optional arg (interactive prefix) non-nil means prompt for user name and site.
214 Second arg is file name of change log. \
215 If nil, uses `change-log-default-name'."
216 (interactive (if current-prefix-arg
217 (list current-prefix-arg
218 (prompt-for-change-log-name))))
219 (add-change-log-entry whoami file-name t))
220 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
221
222 ;;;###autoload
223 (defun change-log-mode ()
224 "Major mode for editing change logs; like Indented Text Mode.
225 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
226 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
227 Each entry behaves as a paragraph, and the entries for one day as a page.
228 Runs `change-log-mode-hook'."
229 (interactive)
230 (kill-all-local-variables)
231 (indented-text-mode)
232 (setq major-mode 'change-log-mode
233 mode-name "Change Log"
234 left-margin 8
235 fill-column 74)
236 ;; Let each entry behave as one paragraph:
237 (set (make-local-variable 'paragraph-start) "^\\s *$\\|^^L")
238 (set (make-local-variable 'paragraph-separate) "^\\s *$\\|^^L\\|^\\sw")
239 ;; Let all entries for one day behave as one page.
240 ;; Match null string on the date-line so that the date-line
241 ;; is grouped with what follows.
242 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
243 (set (make-local-variable 'version-control) 'never)
244 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
245 (run-hooks 'change-log-mode-hook))
246
247 (defvar add-log-current-defun-header-regexp
248 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[a-z_---A-Z]+\\)[ \t]*[:=]"
249 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
250
251 (defun add-log-current-defun ()
252 "Return name of function definition point is in, or nil.
253
254 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
255 Texinfo (@node titles), and Fortran.
256
257 Other modes are handled by a heuristic that looks in the 10K before
258 point for uppercase headings starting in the first column or
259 identifiers followed by `:' or `=', see variable
260 `add-log-current-defun-header-regexp'.
261
262 Has a preference of looking backwards."
263 (condition-case nil
264 (save-excursion
265 (let ((location (point)))
266 (cond ((memq major-mode '(emacs-lisp-mode lisp-mode scheme-mode))
267 ;; If we are now precisely a the beginning of a defun,
268 ;; make sure beginning-of-defun finds that one
269 ;; rather than the previous one.
270 (or (eobp) (forward-char 1))
271 (beginning-of-defun)
272 ;; Make sure we are really inside the defun found, not after it.
273 (if (and (progn (end-of-defun)
274 (< location (point)))
275 (progn (forward-sexp -1)
276 (>= location (point))))
277 (progn
278 (if (looking-at "\\s(")
279 (forward-char 1))
280 (forward-sexp 1)
281 (skip-chars-forward " ")
282 (buffer-substring (point)
283 (progn (forward-sexp 1) (point))))))
284 ((and (memq major-mode '(c-mode 'c++-mode))
285 (save-excursion (beginning-of-line)
286 ;; Use eq instead of = here to avoid
287 ;; error when at bob and char-after
288 ;; returns nil.
289 (while (eq (char-after (- (point) 2)) ?\\)
290 (forward-line -1))
291 (looking-at "[ \t]*#[ \t]*define[ \t]")))
292 ;; Handle a C macro definition.
293 (beginning-of-line)
294 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
295 (forward-line -1))
296 (search-forward "define")
297 (skip-chars-forward " \t")
298 (buffer-substring (point)
299 (progn (forward-sexp 1) (point))))
300 ((memq major-mode '(c-mode 'c++-mode))
301 (beginning-of-line)
302 ;; See if we are in the beginning part of a function,
303 ;; before the open brace. If so, advance forward.
304 (while (not (looking-at "{\\|\\(\\s *$\\)"))
305 (forward-line 1))
306 (or (eobp)
307 (forward-char 1))
308 (beginning-of-defun)
309 (if (progn (end-of-defun)
310 (< location (point)))
311 (progn
312 (backward-sexp 1)
313 (let (beg tem)
314
315 (forward-line -1)
316 ;; Skip back over typedefs of arglist.
317 (while (and (not (bobp))
318 (looking-at "[ \t\n]"))
319 (forward-line -1))
320 ;; See if this is using the DEFUN macro used in Emacs,
321 ;; or the DEFUN macro used by the C library.
322 (if (condition-case nil
323 (and (save-excursion
324 (forward-line 1)
325 (backward-sexp 1)
326 (beginning-of-line)
327 (setq tem (point))
328 (looking-at "DEFUN\\b"))
329 (>= location tem))
330 (error nil))
331 (progn
332 (goto-char tem)
333 (down-list 1)
334 (if (= (char-after (point)) ?\")
335 (progn
336 (forward-sexp 1)
337 (skip-chars-forward " ,")))
338 (buffer-substring (point)
339 (progn (forward-sexp 1) (point))))
340 ;; Ordinary C function syntax.
341 (setq beg (point))
342 (if (condition-case nil
343 ;; Protect against "Unbalanced parens" error.
344 (progn
345 (down-list 1) ; into arglist
346 (backward-up-list 1)
347 (skip-chars-backward " \t")
348 t)
349 (error nil))
350 ;; Verify initial pos was after
351 ;; real start of function.
352 (if (and (save-excursion
353 (goto-char beg)
354 ;; For this purpose, include the line
355 ;; that has the decl keywords. This
356 ;; may also include some of the
357 ;; comments before the function.
358 (while (and (not (bobp))
359 (save-excursion
360 (forward-line -1)
361 (looking-at "[^\n\f]")))
362 (forward-line -1))
363 (>= location (point)))
364 ;; Consistency check: going down and up
365 ;; shouldn't take us back before BEG.
366 (> (point) beg))
367 (buffer-substring (point)
368 (progn (backward-sexp 1)
369 (point))))))))))
370 ((memq major-mode
371 '(TeX-mode plain-TeX-mode LaTeX-mode;; tex-mode.el
372 plain-tex-mode latex-mode;; cmutex.el
373 ))
374 (if (re-search-backward
375 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
376 (progn
377 (goto-char (match-beginning 0))
378 (buffer-substring (1+ (point));; without initial backslash
379 (progn
380 (end-of-line)
381 (point))))))
382 ((eq major-mode 'texinfo-mode)
383 (if (re-search-backward "^@node[ \t]+\\([^,]+\\)," nil t)
384 (buffer-substring (match-beginning 1)
385 (match-end 1))))
386 ((eq major-mode 'fortran-mode)
387 ;; must be inside function body for this to work
388 (beginning-of-fortran-subprogram)
389 (let ((case-fold-search t)) ; case-insensitive
390 ;; search for fortran subprogram start
391 (if (re-search-forward
392 "^[ \t]*\\(program\\|subroutine\\|function\
393 \\|[ \ta-z0-9*]*[ \t]+function\\)"
394 nil t)
395 (progn
396 ;; move to EOL or before first left paren
397 (if (re-search-forward "[(\n]" nil t)
398 (progn (forward-char -1)
399 (skip-chars-backward " \t"))
400 (end-of-line))
401 ;; Use the name preceding that.
402 (buffer-substring (point)
403 (progn (forward-sexp -1)
404 (point)))))))
405 (t
406 ;; If all else fails, try heuristics
407 (let (case-fold-search)
408 (end-of-line)
409 (if (re-search-backward add-log-current-defun-header-regexp
410 (- (point) 10000)
411 t)
412 (buffer-substring (match-beginning 1)
413 (match-end 1))))))))
414 (error nil)))
415
416
417 (provide 'add-log)
418
419 ;;; add-log.el ends here