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