1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 86, 88, 93, 94, 97, 1998 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 (eval-when-compile (require 'fortran
))
32 (defgroup change-log nil
33 "Change log maintenance"
35 :link
'(custom-manual "(emacs)Change Log")
40 (defcustom change-log-default-name nil
41 "*Name of a change log file for \\[add-change-log-entry]."
42 :type
'(choice (const :tag
"default" nil
)
46 (defcustom add-log-current-defun-function nil
48 *If non-nil, function to guess name of current function from surrounding text.
49 \\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
50 instead) with no arguments. It returns a string or nil if it cannot guess."
55 (defcustom add-log-full-name nil
56 "*Full name of user, for inclusion in ChangeLog daily headers.
57 This defaults to the value returned by the `user-full-name' function."
58 :type
'(choice (const :tag
"Default" nil
)
63 (defcustom add-log-mailing-address nil
64 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
65 This defaults to the value of `user-mail-address'."
66 :type
'(choice (const :tag
"Default" nil
)
70 (defcustom add-log-time-format
'add-log-iso8601-time-string
71 "*Function that defines the time format.
72 For example, `add-log-iso8601-time-string', which gives the
73 date in international ISO 8601 format,
74 and `current-time-string' are two valid values."
75 :type
'(radio (const :tag
"International ISO 8601 format"
76 add-log-iso8601-time-string
)
77 (const :tag
"Old format, as returned by `current-time-string'"
79 (function :tag
"Other"))
82 (defcustom add-log-keep-changes-together nil
83 "*If non-nil, normally keep day's log entries for one file together.
85 Log entries for a given file made with \\[add-change-log-entry] or
86 \\[add-change-log-entry-other-window] will only be added to others \
88 today if this variable is non-nil or that file comes first in today's
89 entries. Otherwise another entry for that file will be started. An
95 in the latter case, \\[add-change-log-entry-other-window] in a \
96 buffer visiting `bar', yields:
100 * bar (...): change 1
105 * bar (...): change 1
108 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
114 (defcustom add-log-file-name-function nil
115 "*If non-nil, function to call to identify the filename for a ChangeLog entry.
116 This function is called with one argument, `buffer-file-name' in that buffer.
117 If this is nil, the default is to use the file's name
118 relative to the directory of the change log file."
122 (defvar change-log-font-lock-keywords
124 ;; Date lines, new and old styles.
125 ("^\\sw.........[0-9:+ ]*"
126 (0 font-lock-string-face
)
127 ;; Name and e-mail; some people put e-mail in parens, not angles.
128 ("\\([^<(]+\\)[(<]\\([A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
129 (1 font-lock-constant-face
)
130 (2 font-lock-variable-name-face
)))
133 ("^\t\\* \\([^ ,:([\n]+\\)"
134 (1 font-lock-function-name-face
)
135 ;; Possibly further names in a list:
136 ("\\=, \\([^ ,:([\n]+\\)" nil nil
(1 font-lock-function-name-face
))
137 ;; Possibly a parenthesized list of names:
138 ("\\= (\\([^) ,:\n]+\\)" nil nil
(1 font-lock-keyword-face
))
139 ("\\=, *\\([^) ,:\n]+\\)" nil nil
(1 font-lock-keyword-face
)))
141 ;; Function or variable names.
142 ("^\t(\\([^) ,:\n]+\\)"
143 (1 font-lock-keyword-face
)
144 ("\\=, *\\([^) ,:\n]+\\)" nil nil
(1 font-lock-keyword-face
)))
147 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 font-lock-variable-name-face
))
150 ("^\t\\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
151 1 font-lock-comment-face
)
152 (" \\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
153 1 font-lock-comment-face
))
154 "Additional expressions to highlight in Change Log mode.")
156 (defvar change-log-mode-map nil
157 "Keymap for Change Log major mode.")
158 (if change-log-mode-map
160 (setq change-log-mode-map
(make-sparse-keymap)))
162 (defvar change-log-time-zone-rule nil
163 "Time zone used for calculating change log time stamps.
164 It takes the same format as the TZ argument of `set-time-zone-rule'.
165 If nil, use local time.")
167 (defvar add-log-debugging
)
169 (defun add-log-iso8601-time-zone (time)
170 (let* ((utc-offset (or (car (current-time-zone time
)) 0))
171 (sign (if (< utc-offset
0) ?- ?
+))
172 (sec (abs utc-offset
))
177 (format (cond ((not (zerop ss
)) "%c%02d:%02d:%02d")
178 ((not (zerop mm
)) "%c%02d:%02d")
182 (defun add-log-iso8601-time-string ()
183 (if change-log-time-zone-rule
184 (let ((tz (getenv "TZ"))
185 (now (current-time)))
189 change-log-time-zone-rule
)
191 (format-time-string "%Y-%m-%d " now
)
192 (add-log-iso8601-time-zone now
)))
193 (set-time-zone-rule tz
)))
194 (format-time-string "%Y-%m-%d")))
196 (defun change-log-name ()
197 (or change-log-default-name
198 (if (eq system-type
'vax-vms
)
203 (defun prompt-for-change-log-name ()
204 "Prompt for a change log name."
205 (let* ((default (change-log-name))
206 (name (expand-file-name
207 (read-file-name (format "Log file (default %s): " default
)
209 ;; Handle something that is syntactically a directory name.
210 ;; Look for ChangeLog or whatever in that directory.
211 (if (string= (file-name-nondirectory name
) "")
212 (expand-file-name (file-name-nondirectory default
)
214 ;; Handle specifying a file that is a directory.
215 (if (file-directory-p name
)
216 (expand-file-name (file-name-nondirectory default
)
217 (file-name-as-directory name
))
221 (defun find-change-log (&optional file-name
)
222 "Find a change log file for \\[add-change-log-entry] and return the name.
224 Optional arg FILE-NAME specifies the file to use.
225 If FILE-NAME is nil, use the value of `change-log-default-name'.
226 If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
227 \(or whatever we use on this operating system).
229 If 'change-log-default-name' contains a leading directory component, then
230 simply find it in the current directory. Otherwise, search in the current
231 directory and its successive parents for a file so named.
233 Once a file is found, `change-log-default-name' is set locally in the
234 current buffer to the complete file name."
235 ;; If user specified a file name or if this buffer knows which one to use,
238 (setq file-name
(and change-log-default-name
239 (file-name-directory change-log-default-name
)
240 change-log-default-name
))
242 ;; Chase links in the source file
243 ;; and use the change log in the dir where it points.
244 (setq file-name
(or (and buffer-file-name
246 (file-chase-links buffer-file-name
)))
248 (if (file-directory-p file-name
)
249 (setq file-name
(expand-file-name (change-log-name) file-name
)))
250 ;; Chase links before visiting the file.
251 ;; This makes it easier to use a single change log file
252 ;; for several related directories.
253 (setq file-name
(file-chase-links file-name
))
254 (setq file-name
(expand-file-name file-name
))
255 ;; Move up in the dir hierarchy till we find a change log file.
256 (let ((file1 file-name
)
258 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
259 (progn (setq parent-dir
262 (file-name-directory file1
))))
263 ;; Give up if we are already at the root dir.
264 (not (string= (file-name-directory file1
)
266 ;; Move up to the parent dir and try again.
267 (setq file1
(expand-file-name
268 (file-name-nondirectory (change-log-name))
270 ;; If we found a change log in a parent, use that.
271 (if (or (get-file-buffer file1
) (file-exists-p file1
))
272 (setq file-name file1
)))))
273 ;; Make a local variable in this buffer so we needn't search again.
274 (set (make-local-variable 'change-log-default-name
) file-name
)
278 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
279 "Find change log file and add an entry for today.
280 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
283 Second arg is FILE-NAME of change log. If nil, uses `change-log-default-name'.
284 Third arg OTHER-WINDOW non-nil means visit in other window.
285 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
286 never append to an existing entry. Option `add-log-keep-changes-together'
287 otherwise affects whether a new entry is created.
289 Today's date is calculated according to `change-log-time-zone-rule' if
290 non-nil, otherwise in local time."
291 (interactive (list current-prefix-arg
292 (prompt-for-change-log-name)))
293 (or add-log-full-name
294 (setq add-log-full-name
(user-full-name)))
295 (or add-log-mailing-address
296 (setq add-log-mailing-address user-mail-address
))
299 (setq add-log-full-name
(read-input "Full name: " add-log-full-name
))
300 ;; Note that some sites have room and phone number fields in
301 ;; full name which look silly when inserted. Rather than do
302 ;; anything about that here, let user give prefix argument so that
303 ;; s/he can edit the full name field in prompter if s/he wants.
304 (setq add-log-mailing-address
305 (read-input "Mailing address: " add-log-mailing-address
))))
306 (let ((defun (funcall (or add-log-current-defun-function
307 'add-log-current-defun
)))
310 (setq file-name
(expand-file-name (find-change-log file-name
)))
312 ;; Set ENTRY to the file name to use in the new entry.
313 (and buffer-file-name
314 ;; Never want to add a change log entry for the ChangeLog file itself.
315 (not (string= buffer-file-name file-name
))
317 (if add-log-file-name-function
318 (funcall add-log-file-name-function buffer-file-name
)
320 (concat "^" (regexp-quote (file-name-directory
323 (substring buffer-file-name
(match-end 0))
324 (file-name-nondirectory buffer-file-name
)))))
326 (let ((buffer (find-buffer-visiting file-name
)))
327 (setq add-log-debugging
(list (gap-position) (gap-size))))
328 (if (and other-window
(not (equal file-name buffer-file-name
)))
329 (find-file-other-window file-name
)
330 (find-file file-name
))
331 (or (eq major-mode
'change-log-mode
)
334 (goto-char (point-min))
335 (let ((new-entry (concat (funcall add-log-time-format
)
336 " " add-log-full-name
337 " <" add-log-mailing-address
">")))
338 (if (looking-at (regexp-quote new-entry
))
340 (insert new-entry
"\n\n")))
344 (if (looking-at "\n*[^\n* \t]")
345 (skip-chars-forward "\n")
346 (if add-log-keep-changes-together
347 (forward-page) ; page delimits entries for date
348 (forward-paragraph))) ; paragraph delimits entries for file
350 (goto-char (point-min))
351 ;; Now insert the new line for this entry.
352 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t
)
353 ;; Put this file name into the existing empty entry.
356 ((and (not new-entry
)
357 (let (case-fold-search)
359 (concat (regexp-quote (concat "* " entry
))
360 ;; Don't accept `foo.bar' when
361 ;; looking for `foo':
362 "\\(\\s \\|[(),:]\\)")
364 ;; Add to the existing entry for the same file.
365 (re-search-forward "^\\s *$\\|^\\s \\*")
366 (goto-char (match-beginning 0))
367 ;; Delete excess empty lines; make just 2.
368 (while (and (not (eobp)) (looking-at "^\\s *$"))
369 (delete-region (point) (save-excursion (forward-line 1) (point))))
372 (indent-relative-maybe))
376 (while (looking-at "\\sW")
378 (while (and (not (eobp)) (looking-at "^\\s *$"))
379 (delete-region (point) (save-excursion (forward-line 1) (point))))
382 (indent-to left-margin
)
383 (insert "* " (or entry
""))))
384 ;; Now insert the function name, if we have one.
385 ;; Point is at the entry for this file,
386 ;; either at the end of the line or at the first blank line.
389 ;; Make it easy to get rid of the function name.
391 (insert (if (save-excursion
392 (beginning-of-line 1)
393 (looking-at "\\s *$"))
397 ;; No function name, so put in a colon unless we have just a star.
398 (if (not (save-excursion
399 (beginning-of-line 1)
400 (looking-at "\\s *\\(\\*\\s *\\)?$")))
404 (defun add-change-log-entry-other-window (&optional whoami file-name
)
405 "Find change log file in other window and add an entry for today.
406 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
408 Second optional arg FILE-NAME is file name of change log.
409 If nil, use `change-log-default-name'.
411 Affected by the same options as `add-change-log-entry'."
412 (interactive (if current-prefix-arg
413 (list current-prefix-arg
414 (prompt-for-change-log-name))))
415 (add-change-log-entry whoami file-name t
))
416 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
419 (defun change-log-mode ()
420 "Major mode for editing change logs; like Indented Text Mode.
421 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
422 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
423 Each entry behaves as a paragraph, and the entries for one day as a page.
424 Runs `change-log-mode-hook'."
426 (kill-all-local-variables)
428 (setq major-mode
'change-log-mode
429 mode-name
"Change Log"
434 (use-local-map change-log-mode-map
)
435 (set (make-local-variable 'fill-paragraph-function
)
436 'change-log-fill-paragraph
)
437 ;; We really do want "^" in paragraph-start below: it is only the
438 ;; lines that begin at column 0 (despite the left-margin of 8) that
439 ;; we are looking for. Adding `* ' allows eliding the blank line
440 ;; between entries for different files.
441 (set (make-local-variable 'paragraph-start
) "\\s *$\\|\f\\|^\\<")
442 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
443 ;; Match null string on the date-line so that the date-line
444 ;; is grouped with what follows.
445 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
446 (set (make-local-variable 'version-control
) 'never
)
447 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
448 (set (make-local-variable 'font-lock-defaults
)
449 '(change-log-font-lock-keywords t
))
450 (run-hooks 'change-log-mode-hook
))
452 ;; It might be nice to have a general feature to replace this. The idea I
453 ;; have is a variable giving a regexp matching text which should not be
454 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
455 ;; But I don't feel up to implementing that today.
456 (defun change-log-fill-paragraph (&optional justify
)
457 "Fill the paragraph, but preserve open parentheses at beginning of lines.
458 Prefix arg means justify as well."
460 (let ((end (progn (forward-paragraph) (point)))
461 (beg (progn (backward-paragraph) (point)))
462 (paragraph-start (concat paragraph-start
"\\|\\s *\\s(")))
463 (fill-region beg end justify
)
466 (defcustom add-log-current-defun-header-regexp
467 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
468 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes."
473 (defvar add-log-lisp-like-modes
474 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode
)
475 "*Modes that look like Lisp to `add-log-current-defun'.")
478 (defvar add-log-c-like-modes
479 '(c-mode c
++-mode c
++-c-mode objc-mode
)
480 "*Modes that look like C to `add-log-current-defun'.")
483 (defvar add-log-tex-like-modes
484 '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode
)
485 "*Modes that look like TeX to `add-log-current-defun'.")
488 (defun add-log-current-defun ()
489 "Return name of function definition point is in, or nil.
491 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
492 Texinfo (@node titles), Perl, and Fortran.
494 Other modes are handled by a heuristic that looks in the 10K before
495 point for uppercase headings starting in the first column or
496 identifiers followed by `:' or `=', see variable
497 `add-log-current-defun-header-regexp'.
499 Has a preference of looking backwards."
502 (let ((location (point)))
503 (cond ((memq major-mode add-log-lisp-like-modes
)
504 ;; If we are now precisely at the beginning of a defun,
505 ;; make sure beginning-of-defun finds that one
506 ;; rather than the previous one.
507 (or (eobp) (forward-char 1))
509 ;; Make sure we are really inside the defun found, not after it.
510 (when (and (looking-at "\\s(")
511 (progn (end-of-defun)
512 (< location
(point)))
513 (progn (forward-sexp -
1)
514 (>= location
(point))))
515 (if (looking-at "\\s(")
517 ;; Skip the defining construct name, typically "defun"
520 ;; The second element is usually a symbol being defined.
521 ;; If it is not, use the first symbol in it.
522 (skip-chars-forward " \t\n'(")
523 (buffer-substring (point)
524 (progn (forward-sexp 1)
526 ((and (memq major-mode add-log-c-like-modes
)
529 ;; Use eq instead of = here to avoid
530 ;; error when at bob and char-after
532 (while (eq (char-after (- (point) 2)) ?
\\)
534 (looking-at "[ \t]*#[ \t]*define[ \t]")))
535 ;; Handle a C macro definition.
537 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
539 (search-forward "define")
540 (skip-chars-forward " \t")
541 (buffer-substring (point)
542 (progn (forward-sexp 1) (point))))
543 ((memq major-mode add-log-c-like-modes
)
545 ;; See if we are in the beginning part of a function,
546 ;; before the open brace. If so, advance forward.
547 (while (not (looking-at "{\\|\\(\\s *$\\)"))
552 (if (progn (end-of-defun)
553 (< location
(point)))
559 ;; Skip back over typedefs of arglist.
560 (while (and (not (bobp))
561 (looking-at "[ \t\n]"))
563 ;; See if this is using the DEFUN macro used in Emacs,
564 ;; or the DEFUN macro used by the C library.
565 (if (condition-case nil
568 (while (= (preceding-char) ?
\\)
573 (looking-at "DEFUN\\b"))
579 (if (= (char-after (point)) ?
\")
582 (skip-chars-forward " ,")))
583 (buffer-substring (point)
584 (progn (forward-sexp 1) (point))))
585 (if (looking-at "^[+-]")
586 (change-log-get-method-definition)
587 ;; Ordinary C function syntax.
589 (if (and (condition-case nil
590 ;; Protect against "Unbalanced parens" error.
592 (down-list 1) ; into arglist
594 (skip-chars-backward " \t")
597 ;; Verify initial pos was after
598 ;; real start of function.
601 ;; For this purpose, include the line
602 ;; that has the decl keywords. This
603 ;; may also include some of the
604 ;; comments before the function.
605 (while (and (not (bobp))
608 (looking-at "[^\n\f]")))
610 (>= location
(point)))
611 ;; Consistency check: going down and up
612 ;; shouldn't take us back before BEG.
615 ;; Don't include any final whitespace
616 ;; in the name we use.
617 (skip-chars-backward " \t\n")
620 ;; Now find the right beginning of the name.
621 ;; Include certain keywords if they
623 (setq middle
(point))
625 ;; Ignore these subparts of a class decl
626 ;; and move back to the class name itself.
627 (while (looking-at "public \\|private ")
628 (skip-chars-backward " \t:")
631 (setq middle
(point))
634 (looking-at "enum \\|struct \\|union \\|class ")
635 (setq middle
(point)))
637 (when (eq (preceding-char) ?
=)
639 (skip-chars-backward " \t")
641 (buffer-substring middle end
)))))))))
642 ((memq major-mode add-log-tex-like-modes
)
643 (if (re-search-backward
644 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
646 (goto-char (match-beginning 0))
647 (buffer-substring (1+ (point));; without initial backslash
651 ((eq major-mode
'texinfo-mode
)
652 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
653 (buffer-substring (match-beginning 1)
655 ((eq major-mode
'perl-mode
)
656 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t
)
657 (buffer-substring (match-beginning 1)
659 ((or (eq major-mode
'fortran-mode
)
660 ;; Needs work for f90, but better than nothing.
661 (eq major-mode
'f90-mode
))
662 ;; must be inside function body for this to work
663 (beginning-of-fortran-subprogram)
664 (let ((case-fold-search t
)) ; case-insensitive
665 ;; search for fortran subprogram start
666 (if (re-search-forward
667 "^[ \t]*\\(program\\|subroutine\\|function\
668 \\|[ \ta-z0-9*()]*[ \t]+function\\|\\(block[ \t]*data\\)\\)"
669 (save-excursion (end-of-fortran-subprogram)
674 ;; move to EOL or before first left paren
675 (if (re-search-forward "[(\n]" nil t
)
676 (progn (backward-char)
677 (skip-chars-backward " \t"))
679 ;; Use the name preceding that.
680 (buffer-substring (point)
681 (progn (backward-sexp)
685 ;; If all else fails, try heuristics
686 (let (case-fold-search)
688 (if (re-search-backward add-log-current-defun-header-regexp
691 (buffer-substring (match-beginning 1)
695 (defvar change-log-get-method-definition-md
)
697 ;; Subroutine used within change-log-get-method-definition.
698 ;; Add the last match in the buffer to the end of `md',
699 ;; followed by the string END; move to the end of that match.
700 (defun change-log-get-method-definition-1 (end)
701 (setq change-log-get-method-definition-md
702 (concat change-log-get-method-definition-md
703 (buffer-substring (match-beginning 1) (match-end 1))
705 (goto-char (match-end 0)))
707 ;; For objective C, return the method name if we are in a method.
708 (defun change-log-get-method-definition ()
709 (let ((change-log-get-method-definition-md "["))
711 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
712 (change-log-get-method-definition-1 " ")))
715 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
716 (change-log-get-method-definition-1 "")
717 (while (not (looking-at "[{;]"))
719 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
720 (change-log-get-method-definition-1 ""))
721 (concat change-log-get-method-definition-md
"]"))))))
726 ;;; add-log.el ends here