1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 86, 88, 93, 94, 1997 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 the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; This facility is documented in the Emacs Manual.
30 (defgroup change-log nil
31 "Change log maintenance"
37 (defcustom change-log-default-name nil
38 "*Name of a change log file for \\[add-change-log-entry]."
39 :type
'(choice (const :tag
"default" nil
)
43 (defcustom add-log-current-defun-function nil
45 *If non-nil, function to guess name of current function from surrounding text.
46 \\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
47 instead) with no arguments. It returns a string or nil if it cannot guess."
52 (defcustom add-log-full-name nil
53 "*Full name of user, for inclusion in ChangeLog daily headers.
54 This defaults to the value returned by the `user-full-name' function."
55 :type
'(choice (const :tag
"Default" nil
)
60 (defcustom add-log-mailing-address nil
61 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
62 This defaults to the value of `user-mail-address'."
63 :type
'(choice (const :tag
"Default" nil
)
67 (defcustom add-log-time-format
'add-log-iso8601-time-string
68 "*Function that defines the time format.
69 For example, `add-log-iso8601-time-string', which gives the
70 date in international ISO 8601 format,
71 and `current-time-string' are two valid values."
72 :type
'(radio (const :tag
"International ISO 8601 format"
73 add-log-iso8601-time-string
)
74 (const :tag
"Old format, as returned by `current-time-string'"
76 (function :tag
"Other"))
79 (defvar change-log-font-lock-keywords
81 ;; Date lines, new and old styles.
82 ("^\\sw.........[0-9: ]*"
83 (0 font-lock-string-face
)
84 ("\\([^<]+\\)<\\([A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\\)>" nil nil
85 (1 font-lock-reference-face
)
86 (2 font-lock-variable-name-face
)))
89 ("^\t\\* \\([^ ,:([\n]+\\)"
90 (1 font-lock-function-name-face
)
91 ("\\=, \\([^ ,:([\n]+\\)" nil nil
(1 font-lock-function-name-face
)))
93 ;; Function or variable names.
95 (1 font-lock-keyword-face
)
96 ("\\=, \\([^ ,:\n]+\\)" nil nil
(1 font-lock-keyword-face
)))
99 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 font-lock-variable-name-face
))
102 ("^\t\\(From\\|Reported by\\)" 1 font-lock-comment-face
)
104 "Additional expressions to highlight in Change Log mode.")
106 (defvar change-log-mode-map nil
107 "Keymap for Change Log major mode.")
108 (if change-log-mode-map
110 (setq change-log-mode-map
(make-sparse-keymap)))
112 (defvar change-log-time-zone-rule nil
113 "Time zone used for calculating change log time stamps.
114 It takes the same format as the TZ argument of `set-time-zone-rule'.
115 If nil, use local time.")
117 (defun add-log-iso8601-time-zone (time)
118 (let* ((utc-offset (or (car (current-time-zone time
)) 0))
119 (sign (if (< utc-offset
0) ?- ?
+))
120 (sec (abs utc-offset
))
125 (format (cond ((not (zerop ss
)) "%c%02d:%02d:%02d")
126 ((not (zerop mm
)) "%c%02d:%02d")
130 (defun add-log-iso8601-time-string ()
131 (if change-log-time-zone-rule
132 (let ((tz (getenv "TZ"))
133 (now (current-time)))
137 change-log-time-zone-rule
)
139 (format-time-string "%Y-%m-%d " now
)
140 (add-log-iso8601-time-zone now
)))
141 (set-time-zone-rule tz
)))
142 (format-time-string "%Y-%m-%d")))
144 (defun change-log-name ()
145 (or change-log-default-name
146 (if (eq system-type
'vax-vms
)
151 (defun prompt-for-change-log-name ()
152 "Prompt for a change log name."
153 (let* ((default (change-log-name))
154 (name (expand-file-name
155 (read-file-name (format "Log file (default %s): " default
)
157 ;; Handle something that is syntactically a directory name.
158 ;; Look for ChangeLog or whatever in that directory.
159 (if (string= (file-name-nondirectory name
) "")
160 (expand-file-name (file-name-nondirectory default
)
162 ;; Handle specifying a file that is a directory.
163 (if (file-directory-p name
)
164 (expand-file-name (file-name-nondirectory default
)
165 (file-name-as-directory name
))
169 (defun find-change-log (&optional file-name
)
170 "Find a change log file for \\[add-change-log-entry] and return the name.
172 Optional arg FILE-NAME specifies the file to use.
173 If FILE-NAME is nil, use the value of `change-log-default-name'.
174 If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
175 \(or whatever we use on this operating system).
177 If 'change-log-default-name' contains a leading directory component, then
178 simply find it in the current directory. Otherwise, search in the current
179 directory and its successive parents for a file so named.
181 Once a file is found, `change-log-default-name' is set locally in the
182 current buffer to the complete file name."
183 ;; If user specified a file name or if this buffer knows which one to use,
186 (setq file-name
(and change-log-default-name
187 (file-name-directory change-log-default-name
)
188 change-log-default-name
))
190 ;; Chase links in the source file
191 ;; and use the change log in the dir where it points.
192 (setq file-name
(or (and buffer-file-name
194 (file-chase-links buffer-file-name
)))
196 (if (file-directory-p file-name
)
197 (setq file-name
(expand-file-name (change-log-name) file-name
)))
198 ;; Chase links before visiting the file.
199 ;; This makes it easier to use a single change log file
200 ;; for several related directories.
201 (setq file-name
(file-chase-links file-name
))
202 (setq file-name
(expand-file-name file-name
))
203 ;; Move up in the dir hierarchy till we find a change log file.
204 (let ((file1 file-name
)
206 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
207 (progn (setq parent-dir
210 (file-name-directory file1
))))
211 ;; Give up if we are already at the root dir.
212 (not (string= (file-name-directory file1
)
214 ;; Move up to the parent dir and try again.
215 (setq file1
(expand-file-name
216 (file-name-nondirectory (change-log-name))
218 ;; If we found a change log in a parent, use that.
219 (if (or (get-file-buffer file1
) (file-exists-p file1
))
220 (setq file-name file1
)))))
221 ;; Make a local variable in this buffer so we needn't search again.
222 (set (make-local-variable 'change-log-default-name
) file-name
)
227 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
228 "Find change log file 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. If nil, uses `change-log-default-name'.
231 Third arg OTHER-WINDOW non-nil means visit in other window.
232 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
233 never append to an existing entry. Today's date is calculated according to
234 `change-log-time-zone-rule' if non-nil, otherwise in local time."
235 (interactive (list current-prefix-arg
236 (prompt-for-change-log-name)))
237 (or add-log-full-name
238 (setq add-log-full-name
(user-full-name)))
239 (or add-log-mailing-address
240 (setq add-log-mailing-address user-mail-address
))
243 (setq add-log-full-name
(read-input "Full name: " add-log-full-name
))
244 ;; Note that some sites have room and phone number fields in
245 ;; full name which look silly when inserted. Rather than do
246 ;; anything about that here, let user give prefix argument so that
247 ;; s/he can edit the full name field in prompter if s/he wants.
248 (setq add-log-mailing-address
249 (read-input "Mailing address: " add-log-mailing-address
))))
250 (let ((defun (funcall (or add-log-current-defun-function
251 'add-log-current-defun
)))
254 (setq file-name
(expand-file-name (find-change-log file-name
)))
256 ;; Set ENTRY to the file name to use in the new entry.
257 (and buffer-file-name
258 ;; Never want to add a change log entry for the ChangeLog file itself.
259 (not (string= buffer-file-name file-name
))
260 (setq entry
(if (string-match
261 (concat "^" (regexp-quote (file-name-directory
264 (substring buffer-file-name
(match-end 0))
265 (file-name-nondirectory buffer-file-name
))))
267 (if (and other-window
(not (equal file-name buffer-file-name
)))
268 (find-file-other-window file-name
)
269 (find-file file-name
))
270 (or (eq major-mode
'change-log-mode
)
273 (goto-char (point-min))
274 (let ((new-entry (concat (funcall add-log-time-format
)
275 " " add-log-full-name
276 " <" add-log-mailing-address
">")))
277 (if (looking-at (regexp-quote new-entry
))
279 (insert new-entry
"\n\n")))
281 ;; Search only within the first paragraph.
282 (if (looking-at "\n*[^\n* \t]")
283 (skip-chars-forward "\n")
284 (forward-paragraph 1))
285 (setq paragraph-end
(point))
286 (goto-char (point-min))
288 ;; Now insert the new line for this entry.
289 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t
)
290 ;; Put this file name into the existing empty entry.
293 ((and (not new-entry
)
294 (let (case-fold-search)
296 (concat (regexp-quote (concat "* " entry
))
297 ;; Don't accept `foo.bar' when
298 ;; looking for `foo':
299 "\\(\\s \\|[(),:]\\)")
301 ;; Add to the existing entry for the same file.
302 (re-search-forward "^\\s *$\\|^\\s \\*")
303 (goto-char (match-beginning 0))
304 ;; Delete excess empty lines; make just 2.
305 (while (and (not (eobp)) (looking-at "^\\s *$"))
306 (delete-region (point) (save-excursion (forward-line 1) (point))))
309 (indent-relative-maybe))
313 (while (looking-at "\\sW")
315 (while (and (not (eobp)) (looking-at "^\\s *$"))
316 (delete-region (point) (save-excursion (forward-line 1) (point))))
319 (indent-to left-margin
)
320 (insert "* " (or entry
""))))
321 ;; Now insert the function name, if we have one.
322 ;; Point is at the entry for this file,
323 ;; either at the end of the line or at the first blank line.
326 ;; Make it easy to get rid of the function name.
328 (insert (if (save-excursion
329 (beginning-of-line 1)
330 (looking-at "\\s *$"))
334 ;; No function name, so put in a colon unless we have just a star.
335 (if (not (save-excursion
336 (beginning-of-line 1)
337 (looking-at "\\s *\\(\\*\\s *\\)?$")))
341 (defun add-change-log-entry-other-window (&optional whoami file-name
)
342 "Find change log file in other window and add an entry for today.
343 Optional arg (interactive prefix) non-nil means prompt for user name and site.
344 Second arg is file name of change log. \
345 If nil, uses `change-log-default-name'."
346 (interactive (if current-prefix-arg
347 (list current-prefix-arg
348 (prompt-for-change-log-name))))
349 (add-change-log-entry whoami file-name t
))
350 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
353 (defun change-log-mode ()
354 "Major mode for editing change logs; like Indented Text Mode.
355 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
356 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
357 Each entry behaves as a paragraph, and the entries for one day as a page.
358 Runs `change-log-mode-hook'."
360 (kill-all-local-variables)
362 (setq major-mode
'change-log-mode
363 mode-name
"Change Log"
368 (use-local-map change-log-mode-map
)
369 (set (make-local-variable 'fill-paragraph-function
)
370 'change-log-fill-paragraph
)
371 ;; Let each entry behave as one paragraph:
372 ;; We really do want "^" in paragraph-start below: it is only the lines that
373 ;; begin at column 0 (despite the left-margin of 8) that we are looking for.
374 (set (make-local-variable 'paragraph-start
) "\\s *$\\|\f\\|^\\<")
375 (set (make-local-variable 'paragraph-separate
) "\\s *$\\|\f\\|^\\<")
376 ;; Let all entries for one day behave as one page.
377 ;; Match null string on the date-line so that the date-line
378 ;; is grouped with what follows.
379 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
380 (set (make-local-variable 'version-control
) 'never
)
381 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
382 (set (make-local-variable 'font-lock-defaults
)
383 '(change-log-font-lock-keywords t
))
384 (run-hooks 'change-log-mode-hook
))
386 ;; It might be nice to have a general feature to replace this. The idea I
387 ;; have is a variable giving a regexp matching text which should not be
388 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
389 ;; But I don't feel up to implementing that today.
390 (defun change-log-fill-paragraph (&optional justify
)
391 "Fill the paragraph, but preserve open parentheses at beginning of lines.
392 Prefix arg means justify as well."
394 (let ((end (progn (forward-paragraph) (point)))
395 (beg (progn (backward-paragraph) (point)))
396 (paragraph-start (concat paragraph-start
"\\|\\s *\\s(")))
397 (fill-region beg end justify
)
400 (defcustom add-log-current-defun-header-regexp
401 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
402 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes."
407 (defvar add-log-lisp-like-modes
408 '(emacs-lisp-mode lisp-mode scheme-mode lisp-interaction-mode
)
409 "*Modes that look like Lisp to `add-log-current-defun'.")
412 (defvar add-log-c-like-modes
413 '(c-mode c
++-mode c
++-c-mode objc-mode
)
414 "*Modes that look like C to `add-log-current-defun'.")
417 (defvar add-log-tex-like-modes
418 '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode
)
419 "*Modes that look like TeX to `add-log-current-defun'.")
422 (defun add-log-current-defun ()
423 "Return name of function definition point is in, or nil.
425 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
426 Texinfo (@node titles), Perl, and Fortran.
428 Other modes are handled by a heuristic that looks in the 10K before
429 point for uppercase headings starting in the first column or
430 identifiers followed by `:' or `=', see variable
431 `add-log-current-defun-header-regexp'.
433 Has a preference of looking backwards."
436 (let ((location (point)))
437 (cond ((memq major-mode add-log-lisp-like-modes
)
438 ;; If we are now precisely at the beginning of a defun,
439 ;; make sure beginning-of-defun finds that one
440 ;; rather than the previous one.
441 (or (eobp) (forward-char 1))
443 ;; Make sure we are really inside the defun found, not after it.
444 (if (and (looking-at "\\s(")
445 (progn (end-of-defun)
446 (< location
(point)))
447 (progn (forward-sexp -
1)
448 (>= location
(point))))
450 (if (looking-at "\\s(")
453 (skip-chars-forward " '")
454 (buffer-substring (point)
455 (progn (forward-sexp 1) (point))))))
456 ((and (memq major-mode add-log-c-like-modes
)
459 ;; Use eq instead of = here to avoid
460 ;; error when at bob and char-after
462 (while (eq (char-after (- (point) 2)) ?
\\)
464 (looking-at "[ \t]*#[ \t]*define[ \t]")))
465 ;; Handle a C macro definition.
467 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
469 (search-forward "define")
470 (skip-chars-forward " \t")
471 (buffer-substring (point)
472 (progn (forward-sexp 1) (point))))
473 ((memq major-mode add-log-c-like-modes
)
475 ;; See if we are in the beginning part of a function,
476 ;; before the open brace. If so, advance forward.
477 (while (not (looking-at "{\\|\\(\\s *$\\)"))
482 (if (progn (end-of-defun)
483 (< location
(point)))
489 ;; Skip back over typedefs of arglist.
490 (while (and (not (bobp))
491 (looking-at "[ \t\n]"))
493 ;; See if this is using the DEFUN macro used in Emacs,
494 ;; or the DEFUN macro used by the C library.
495 (if (condition-case nil
498 (while (= (preceding-char) ?
\\)
503 (looking-at "DEFUN\\b"))
509 (if (= (char-after (point)) ?
\")
512 (skip-chars-forward " ,")))
513 (buffer-substring (point)
514 (progn (forward-sexp 1) (point))))
515 (if (looking-at "^[+-]")
516 (get-method-definition)
517 ;; Ordinary C function syntax.
519 (if (and (condition-case nil
520 ;; Protect against "Unbalanced parens" error.
522 (down-list 1) ; into arglist
524 (skip-chars-backward " \t")
527 ;; Verify initial pos was after
528 ;; real start of function.
531 ;; For this purpose, include the line
532 ;; that has the decl keywords. This
533 ;; may also include some of the
534 ;; comments before the function.
535 (while (and (not (bobp))
538 (looking-at "[^\n\f]")))
540 (>= location
(point)))
541 ;; Consistency check: going down and up
542 ;; shouldn't take us back before BEG.
545 ;; Don't include any final newline
546 ;; in the name we use.
547 (if (= (preceding-char) ?
\n)
551 ;; Now find the right beginning of the name.
552 ;; Include certain keywords if they
554 (setq middle
(point))
556 ;; Ignore these subparts of a class decl
557 ;; and move back to the class name itself.
558 (while (looking-at "public \\|private ")
559 (skip-chars-backward " \t:")
562 (setq middle
(point))
565 (looking-at "struct \\|union \\|class ")
566 (setq middle
(point)))
567 (buffer-substring middle end
)))))))))
568 ((memq major-mode add-log-tex-like-modes
)
569 (if (re-search-backward
570 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
572 (goto-char (match-beginning 0))
573 (buffer-substring (1+ (point));; without initial backslash
577 ((eq major-mode
'texinfo-mode
)
578 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
579 (buffer-substring (match-beginning 1)
581 ((eq major-mode
'perl-mode
)
582 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t
)
583 (buffer-substring (match-beginning 1)
585 ((eq major-mode
'fortran-mode
)
586 ;; must be inside function body for this to work
587 (beginning-of-fortran-subprogram)
588 (let ((case-fold-search t
)) ; case-insensitive
589 ;; search for fortran subprogram start
590 (if (re-search-forward
591 "^[ \t]*\\(program\\|subroutine\\|function\
592 \\|[ \ta-z0-9*]*[ \t]+function\\)"
595 ;; move to EOL or before first left paren
596 (if (re-search-forward "[(\n]" nil t
)
597 (progn (forward-char -
1)
598 (skip-chars-backward " \t"))
600 ;; Use the name preceding that.
601 (buffer-substring (point)
602 (progn (forward-sexp -
1)
605 ;; If all else fails, try heuristics
606 (let (case-fold-search)
608 (if (re-search-backward add-log-current-defun-header-regexp
611 (buffer-substring (match-beginning 1)
615 (defvar get-method-definition-md
)
617 ;; Subroutine used within get-method-definition.
618 ;; Add the last match in the buffer to the end of `md',
619 ;; followed by the string END; move to the end of that match.
620 (defun get-method-definition-1 (end)
621 (setq get-method-definition-md
622 (concat get-method-definition-md
623 (buffer-substring (match-beginning 1) (match-end 1))
625 (goto-char (match-end 0)))
627 ;; For objective C, return the method name if we are in a method.
628 (defun get-method-definition ()
629 (let ((get-method-definition-md "["))
631 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
632 (get-method-definition-1 " ")))
635 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
636 (get-method-definition-1 "")
637 (while (not (looking-at "[{;]"))
639 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
640 (get-method-definition-1 ""))
641 (concat get-method-definition-md
"]"))))))
646 ;;; add-log.el ends here