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