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 change-log-mode-hook nil
47 "Normal hook run by `change-log-mode'."
51 (defcustom add-log-current-defun-function nil
53 *If non-nil, function to guess name of current function from surrounding text.
54 \\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
55 instead) with no arguments. It returns a string or nil if it cannot guess."
60 (defcustom add-log-full-name nil
61 "*Full name of user, for inclusion in ChangeLog daily headers.
62 This defaults to the value returned by the `user-full-name' function."
63 :type
'(choice (const :tag
"Default" nil
)
68 (defcustom add-log-mailing-address nil
69 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
70 This defaults to the value of `user-mail-address'."
71 :type
'(choice (const :tag
"Default" nil
)
75 (defcustom add-log-time-format
'add-log-iso8601-time-string
76 "*Function that defines the time format.
77 For example, `add-log-iso8601-time-string', which gives the
78 date in international ISO 8601 format,
79 and `current-time-string' are two valid values."
80 :type
'(radio (const :tag
"International ISO 8601 format"
81 add-log-iso8601-time-string
)
82 (const :tag
"Old format, as returned by `current-time-string'"
84 (function :tag
"Other"))
87 (defcustom add-log-keep-changes-together nil
88 "*If non-nil, normally keep day's log entries for one file together.
90 Log entries for a given file made with \\[add-change-log-entry] or
91 \\[add-change-log-entry-other-window] will only be added to others \
93 today if this variable is non-nil or that file comes first in today's
94 entries. Otherwise another entry for that file will be started. An
100 in the latter case, \\[add-change-log-entry-other-window] in a \
101 buffer visiting `bar', yields:
105 * bar (...): change 1
110 * bar (...): change 1
113 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
119 (defcustom add-log-file-name-function nil
120 "*If non-nil, function to call to identify the filename for a ChangeLog entry.
121 This function is called with one argument, `buffer-file-name' in that buffer.
122 If this is nil, the default is to use the file's name
123 relative to the directory of the change log file."
127 (defvar change-log-font-lock-keywords
129 ;; Date lines, new and old styles.
130 ("^\\sw.........[0-9:+ ]*"
131 (0 font-lock-string-face
)
132 ;; Name and e-mail; some people put e-mail in parens, not angles.
133 ("\\([^<(]+\\)[(<]\\([A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
134 (1 font-lock-constant-face
)
135 (2 font-lock-variable-name-face
)))
138 ("^\t\\* \\([^ ,:([\n]+\\)"
139 (1 font-lock-function-name-face
)
140 ;; Possibly further names in a list:
141 ("\\=, \\([^ ,:([\n]+\\)" nil nil
(1 font-lock-function-name-face
))
142 ;; Possibly a parenthesized list of names:
143 ("\\= (\\([^) ,:\n]+\\)" nil nil
(1 font-lock-keyword-face
))
144 ("\\=, *\\([^) ,:\n]+\\)" nil nil
(1 font-lock-keyword-face
)))
146 ;; Function or variable names.
147 ("^\t(\\([^) ,:\n]+\\)"
148 (1 font-lock-keyword-face
)
149 ("\\=, *\\([^) ,:\n]+\\)" nil nil
(1 font-lock-keyword-face
)))
152 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 font-lock-variable-name-face
))
155 ("^\t\\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
156 1 font-lock-comment-face
)
157 (" \\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
158 1 font-lock-comment-face
))
159 "Additional expressions to highlight in Change Log mode.")
161 (defvar change-log-mode-map nil
162 "Keymap for Change Log major mode.")
163 (if change-log-mode-map
165 (setq change-log-mode-map
(make-sparse-keymap)))
167 (defvar change-log-time-zone-rule nil
168 "Time zone used for calculating change log time stamps.
169 It takes the same format as the TZ argument of `set-time-zone-rule'.
170 If nil, use local time.")
172 (defvar add-log-debugging
)
174 (defun add-log-iso8601-time-zone (time)
175 (let* ((utc-offset (or (car (current-time-zone time
)) 0))
176 (sign (if (< utc-offset
0) ?- ?
+))
177 (sec (abs utc-offset
))
182 (format (cond ((not (zerop ss
)) "%c%02d:%02d:%02d")
183 ((not (zerop mm
)) "%c%02d:%02d")
187 (defun add-log-iso8601-time-string ()
188 (if change-log-time-zone-rule
189 (let ((tz (getenv "TZ"))
190 (now (current-time)))
194 change-log-time-zone-rule
)
196 (format-time-string "%Y-%m-%d " now
)
197 (add-log-iso8601-time-zone now
)))
198 (set-time-zone-rule tz
)))
199 (format-time-string "%Y-%m-%d")))
201 (defun change-log-name ()
202 (or change-log-default-name
203 (if (eq system-type
'vax-vms
)
208 (defun prompt-for-change-log-name ()
209 "Prompt for a change log name."
210 (let* ((default (change-log-name))
211 (name (expand-file-name
212 (read-file-name (format "Log file (default %s): " default
)
214 ;; Handle something that is syntactically a directory name.
215 ;; Look for ChangeLog or whatever in that directory.
216 (if (string= (file-name-nondirectory name
) "")
217 (expand-file-name (file-name-nondirectory default
)
219 ;; Handle specifying a file that is a directory.
220 (if (file-directory-p name
)
221 (expand-file-name (file-name-nondirectory default
)
222 (file-name-as-directory name
))
226 (defun find-change-log (&optional file-name
)
227 "Find a change log file for \\[add-change-log-entry] and return the name.
229 Optional arg FILE-NAME specifies the file to use.
230 If FILE-NAME is nil, use the value of `change-log-default-name'.
231 If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
232 \(or whatever we use on this operating system).
234 If 'change-log-default-name' contains a leading directory component, then
235 simply find it in the current directory. Otherwise, search in the current
236 directory and its successive parents for a file so named.
238 Once a file is found, `change-log-default-name' is set locally in the
239 current buffer to the complete file name."
240 ;; If user specified a file name or if this buffer knows which one to use,
243 (setq file-name
(and change-log-default-name
244 (file-name-directory change-log-default-name
)
245 change-log-default-name
))
247 ;; Chase links in the source file
248 ;; and use the change log in the dir where it points.
249 (setq file-name
(or (and buffer-file-name
251 (file-chase-links buffer-file-name
)))
253 (if (file-directory-p file-name
)
254 (setq file-name
(expand-file-name (change-log-name) file-name
)))
255 ;; Chase links before visiting the file.
256 ;; This makes it easier to use a single change log file
257 ;; for several related directories.
258 (setq file-name
(file-chase-links file-name
))
259 (setq file-name
(expand-file-name file-name
))
260 ;; Move up in the dir hierarchy till we find a change log file.
261 (let ((file1 file-name
)
263 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
264 (progn (setq parent-dir
267 (file-name-directory file1
))))
268 ;; Give up if we are already at the root dir.
269 (not (string= (file-name-directory file1
)
271 ;; Move up to the parent dir and try again.
272 (setq file1
(expand-file-name
273 (file-name-nondirectory (change-log-name))
275 ;; If we found a change log in a parent, use that.
276 (if (or (get-file-buffer file1
) (file-exists-p file1
))
277 (setq file-name file1
)))))
278 ;; Make a local variable in this buffer so we needn't search again.
279 (set (make-local-variable 'change-log-default-name
) file-name
)
283 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
284 "Find change log file and add an entry for today.
285 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
288 Second arg is FILE-NAME of change log. If nil, uses `change-log-default-name'.
289 Third arg OTHER-WINDOW non-nil means visit in other window.
290 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
291 never append to an existing entry. Option `add-log-keep-changes-together'
292 otherwise affects whether a new entry is created.
294 Today's date is calculated according to `change-log-time-zone-rule' if
295 non-nil, otherwise in local time."
296 (interactive (list current-prefix-arg
297 (prompt-for-change-log-name)))
298 (or add-log-full-name
299 (setq add-log-full-name
(user-full-name)))
300 (or add-log-mailing-address
301 (setq add-log-mailing-address user-mail-address
))
304 (setq add-log-full-name
(read-input "Full name: " add-log-full-name
))
305 ;; Note that some sites have room and phone number fields in
306 ;; full name which look silly when inserted. Rather than do
307 ;; anything about that here, let user give prefix argument so that
308 ;; s/he can edit the full name field in prompter if s/he wants.
309 (setq add-log-mailing-address
310 (read-input "Mailing address: " add-log-mailing-address
))))
311 (let ((defun (funcall (or add-log-current-defun-function
312 'add-log-current-defun
)))
315 (setq file-name
(expand-file-name (find-change-log file-name
)))
317 ;; Set ENTRY to the file name to use in the new entry.
318 (and buffer-file-name
319 ;; Never want to add a change log entry for the ChangeLog file itself.
320 (not (string= buffer-file-name file-name
))
322 (if add-log-file-name-function
323 (funcall add-log-file-name-function buffer-file-name
)
325 (concat "^" (regexp-quote (file-name-directory
328 (substring buffer-file-name
(match-end 0))
329 (file-name-nondirectory buffer-file-name
)))))
331 (let ((buffer (find-buffer-visiting file-name
)))
332 (setq add-log-debugging
(list (gap-position) (gap-size))))
333 (if (and other-window
(not (equal file-name buffer-file-name
)))
334 (find-file-other-window file-name
)
335 (find-file file-name
))
336 (or (eq major-mode
'change-log-mode
)
339 (goto-char (point-min))
340 (let ((new-entry (concat (funcall add-log-time-format
)
341 " " add-log-full-name
342 " <" add-log-mailing-address
">")))
343 (if (looking-at (regexp-quote new-entry
))
345 (insert new-entry
"\n\n")))
349 (if (looking-at "\n*[^\n* \t]")
350 (skip-chars-forward "\n")
351 (if add-log-keep-changes-together
352 (forward-page) ; page delimits entries for date
353 (forward-paragraph))) ; paragraph delimits entries for file
355 (goto-char (point-min))
356 ;; Now insert the new line for this entry.
357 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t
)
358 ;; Put this file name into the existing empty entry.
361 ((and (not new-entry
)
362 (let (case-fold-search)
364 (concat (regexp-quote (concat "* " entry
))
365 ;; Don't accept `foo.bar' when
366 ;; looking for `foo':
367 "\\(\\s \\|[(),:]\\)")
369 ;; Add to the existing entry for the same file.
370 (re-search-forward "^\\s *$\\|^\\s \\*")
371 (goto-char (match-beginning 0))
372 ;; Delete excess empty lines; make just 2.
373 (while (and (not (eobp)) (looking-at "^\\s *$"))
374 (delete-region (point) (save-excursion (forward-line 1) (point))))
377 (indent-relative-maybe))
381 (while (looking-at "\\sW")
383 (while (and (not (eobp)) (looking-at "^\\s *$"))
384 (delete-region (point) (save-excursion (forward-line 1) (point))))
387 (indent-to left-margin
)
388 (insert "* " (or entry
""))))
389 ;; Now insert the function name, if we have one.
390 ;; Point is at the entry for this file,
391 ;; either at the end of the line or at the first blank line.
394 ;; Make it easy to get rid of the function name.
396 (insert (if (save-excursion
397 (beginning-of-line 1)
398 (looking-at "\\s *$"))
402 ;; No function name, so put in a colon unless we have just a star.
403 (if (not (save-excursion
404 (beginning-of-line 1)
405 (looking-at "\\s *\\(\\*\\s *\\)?$")))
409 (defun add-change-log-entry-other-window (&optional whoami file-name
)
410 "Find change log file in other window and add an entry for today.
411 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
413 Second optional arg FILE-NAME is file name of change log.
414 If nil, use `change-log-default-name'.
416 Affected by the same options as `add-change-log-entry'."
417 (interactive (if current-prefix-arg
418 (list current-prefix-arg
419 (prompt-for-change-log-name))))
420 (add-change-log-entry whoami file-name t
))
421 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
424 (defun change-log-mode ()
425 "Major mode for editing change logs; like Indented Text Mode.
426 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
427 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
428 Each entry behaves as a paragraph, and the entries for one day as a page.
429 Runs `change-log-mode-hook'."
431 (kill-all-local-variables)
433 (setq major-mode
'change-log-mode
434 mode-name
"Change Log"
439 (use-local-map change-log-mode-map
)
440 (set (make-local-variable 'fill-paragraph-function
)
441 'change-log-fill-paragraph
)
442 ;; We really do want "^" in paragraph-start below: it is only the
443 ;; lines that begin at column 0 (despite the left-margin of 8) that
444 ;; we are looking for. Adding `* ' allows eliding the blank line
445 ;; between entries for different files.
446 (set (make-local-variable 'paragraph-start
) "\\s *$\\|\f\\|^\\<")
447 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
448 ;; Match null string on the date-line so that the date-line
449 ;; is grouped with what follows.
450 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
451 (set (make-local-variable 'version-control
) 'never
)
452 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
453 (set (make-local-variable 'font-lock-defaults
)
454 '(change-log-font-lock-keywords t
))
455 (run-hooks 'change-log-mode-hook
))
457 ;; It might be nice to have a general feature to replace this. The idea I
458 ;; have is a variable giving a regexp matching text which should not be
459 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
460 ;; But I don't feel up to implementing that today.
461 (defun change-log-fill-paragraph (&optional justify
)
462 "Fill the paragraph, but preserve open parentheses at beginning of lines.
463 Prefix arg means justify as well."
465 (let ((end (progn (forward-paragraph) (point)))
466 (beg (progn (backward-paragraph) (point)))
467 (paragraph-start (concat paragraph-start
"\\|\\s *\\s(")))
468 (fill-region beg end justify
)
471 (defcustom add-log-current-defun-header-regexp
472 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
473 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes."
478 (defvar add-log-lisp-like-modes
479 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode
)
480 "*Modes that look like Lisp to `add-log-current-defun'.")
483 (defvar add-log-c-like-modes
484 '(c-mode c
++-mode c
++-c-mode objc-mode
)
485 "*Modes that look like C to `add-log-current-defun'.")
488 (defvar add-log-tex-like-modes
489 '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode
)
490 "*Modes that look like TeX to `add-log-current-defun'.")
493 (defun add-log-current-defun ()
494 "Return name of function definition point is in, or nil.
496 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
497 Texinfo (@node titles), Perl, and Fortran.
499 Other modes are handled by a heuristic that looks in the 10K before
500 point for uppercase headings starting in the first column or
501 identifiers followed by `:' or `=', see variable
502 `add-log-current-defun-header-regexp'.
504 Has a preference of looking backwards."
507 (let ((location (point)))
508 (cond ((memq major-mode add-log-lisp-like-modes
)
509 ;; If we are now precisely at the beginning of a defun,
510 ;; make sure beginning-of-defun finds that one
511 ;; rather than the previous one.
512 (or (eobp) (forward-char 1))
514 ;; Make sure we are really inside the defun found, not after it.
515 (when (and (looking-at "\\s(")
516 (progn (end-of-defun)
517 (< location
(point)))
518 (progn (forward-sexp -
1)
519 (>= location
(point))))
520 (if (looking-at "\\s(")
522 ;; Skip the defining construct name, typically "defun"
525 ;; The second element is usually a symbol being defined.
526 ;; If it is not, use the first symbol in it.
527 (skip-chars-forward " \t\n'(")
528 (buffer-substring (point)
529 (progn (forward-sexp 1)
531 ((and (memq major-mode add-log-c-like-modes
)
534 ;; Use eq instead of = here to avoid
535 ;; error when at bob and char-after
537 (while (eq (char-after (- (point) 2)) ?
\\)
539 (looking-at "[ \t]*#[ \t]*define[ \t]")))
540 ;; Handle a C macro definition.
542 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
544 (search-forward "define")
545 (skip-chars-forward " \t")
546 (buffer-substring (point)
547 (progn (forward-sexp 1) (point))))
548 ((memq major-mode add-log-c-like-modes
)
550 ;; See if we are in the beginning part of a function,
551 ;; before the open brace. If so, advance forward.
552 (while (not (looking-at "{\\|\\(\\s *$\\)"))
557 (if (progn (end-of-defun)
558 (< location
(point)))
564 ;; Skip back over typedefs of arglist.
565 (while (and (not (bobp))
566 (looking-at "[ \t\n]"))
568 ;; See if this is using the DEFUN macro used in Emacs,
569 ;; or the DEFUN macro used by the C library.
570 (if (condition-case nil
573 (while (= (preceding-char) ?
\\)
578 (looking-at "DEFUN\\b"))
584 (if (= (char-after (point)) ?
\")
587 (skip-chars-forward " ,")))
588 (buffer-substring (point)
589 (progn (forward-sexp 1) (point))))
590 (if (looking-at "^[+-]")
591 (change-log-get-method-definition)
592 ;; Ordinary C function syntax.
594 (if (and (condition-case nil
595 ;; Protect against "Unbalanced parens" error.
597 (down-list 1) ; into arglist
599 (skip-chars-backward " \t")
602 ;; Verify initial pos was after
603 ;; real start of function.
606 ;; For this purpose, include the line
607 ;; that has the decl keywords. This
608 ;; may also include some of the
609 ;; comments before the function.
610 (while (and (not (bobp))
613 (looking-at "[^\n\f]")))
615 (>= location
(point)))
616 ;; Consistency check: going down and up
617 ;; shouldn't take us back before BEG.
620 ;; Don't include any final whitespace
621 ;; in the name we use.
622 (skip-chars-backward " \t\n")
625 ;; Now find the right beginning of the name.
626 ;; Include certain keywords if they
628 (setq middle
(point))
630 ;; Ignore these subparts of a class decl
631 ;; and move back to the class name itself.
632 (while (looking-at "public \\|private ")
633 (skip-chars-backward " \t:")
636 (setq middle
(point))
639 (looking-at "enum \\|struct \\|union \\|class ")
640 (setq middle
(point)))
642 (when (eq (preceding-char) ?
=)
644 (skip-chars-backward " \t")
646 (buffer-substring middle end
)))))))))
647 ((memq major-mode add-log-tex-like-modes
)
648 (if (re-search-backward
649 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
651 (goto-char (match-beginning 0))
652 (buffer-substring (1+ (point));; without initial backslash
656 ((eq major-mode
'texinfo-mode
)
657 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
658 (buffer-substring (match-beginning 1)
660 ((eq major-mode
'perl-mode
)
661 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t
)
662 (buffer-substring (match-beginning 1)
664 ((or (eq major-mode
'fortran-mode
)
665 ;; Needs work for f90, but better than nothing.
666 (eq major-mode
'f90-mode
))
667 ;; must be inside function body for this to work
668 (fortran-beginning-of-subprogram)
669 (let ((case-fold-search t
)) ; case-insensitive
670 ;; search for fortran subprogram start
671 (if (re-search-forward
672 "^[ \t]*\\(program\\|subroutine\\|function\
673 \\|[ \ta-z0-9*()]*[ \t]+function\\|\\(block[ \t]*data\\)\\)"
674 (save-excursion (fortran-end-of-subprogram)
679 ;; move to EOL or before first left paren
680 (if (re-search-forward "[(\n]" nil t
)
681 (progn (backward-char)
682 (skip-chars-backward " \t"))
684 ;; Use the name preceding that.
685 (buffer-substring (point)
686 (progn (backward-sexp)
690 ;; If all else fails, try heuristics
691 (let (case-fold-search)
693 (if (re-search-backward add-log-current-defun-header-regexp
696 (buffer-substring (match-beginning 1)
700 (defvar change-log-get-method-definition-md
)
702 ;; Subroutine used within change-log-get-method-definition.
703 ;; Add the last match in the buffer to the end of `md',
704 ;; followed by the string END; move to the end of that match.
705 (defun change-log-get-method-definition-1 (end)
706 (setq change-log-get-method-definition-md
707 (concat change-log-get-method-definition-md
708 (buffer-substring (match-beginning 1) (match-end 1))
710 (goto-char (match-end 0)))
712 ;; For objective C, return the method name if we are in a method.
713 (defun change-log-get-method-definition ()
714 (let ((change-log-get-method-definition-md "["))
716 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
717 (change-log-get-method-definition-1 " ")))
720 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
721 (change-log-get-method-definition-1 "")
722 (while (not (looking-at "[{;]"))
724 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
725 (change-log-get-method-definition-1 ""))
726 (concat change-log-get-method-definition-md
"]"))))))
731 ;;; add-log.el ends here