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
)
68 (defvar change-log-font-lock-keywords
70 ;; Date lines, new and old styles.
71 ("^\\sw.........[0-9: ]*"
72 (0 font-lock-string-face
)
73 ("\\([^<]+\\)<\\([A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\\)>" nil nil
74 (1 font-lock-reference-face
)
75 (2 font-lock-variable-name-face
)))
78 ("^\t\\* \\([^ ,:([\n]+\\)"
79 (1 font-lock-function-name-face
)
80 ("\\=, \\([^ ,:([\n]+\\)" nil nil
(1 font-lock-function-name-face
)))
82 ;; Function or variable names.
84 (1 font-lock-keyword-face
)
85 ("\\=, \\([^ ,:\n]+\\)" nil nil
(1 font-lock-keyword-face
)))
88 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 font-lock-variable-name-face
))
91 ("^\t\\(From\\|Reported by\\)" 1 font-lock-comment-face
)
93 "Additional expressions to highlight in Change Log mode.")
95 (defvar change-log-mode-map nil
96 "Keymap for Change Log major mode.")
97 (if change-log-mode-map
99 (setq change-log-mode-map
(make-sparse-keymap)))
101 (defvar change-log-time-zone-rule nil
102 "Time zone used for calculating change log time stamps.
103 It takes the same format as the TZ argument of `set-time-zone-rule'.
104 If nil, use local time.")
106 (defun iso8601-time-zone (time)
107 (let* ((utc-offset (or (car (current-time-zone time
)) 0))
108 (sign (if (< utc-offset
0) ?- ?
+))
109 (sec (abs utc-offset
))
114 (format (cond ((not (zerop ss
)) "%c%02d:%02d:%02d")
115 ((not (zerop mm
)) "%c%02d:%02d")
119 (defun change-log-name ()
120 (or change-log-default-name
121 (if (eq system-type
'vax-vms
)
126 (defun prompt-for-change-log-name ()
127 "Prompt for a change log name."
128 (let* ((default (change-log-name))
129 (name (expand-file-name
130 (read-file-name (format "Log file (default %s): " default
)
132 ;; Handle something that is syntactically a directory name.
133 ;; Look for ChangeLog or whatever in that directory.
134 (if (string= (file-name-nondirectory name
) "")
135 (expand-file-name (file-name-nondirectory default
)
137 ;; Handle specifying a file that is a directory.
138 (if (file-directory-p name
)
139 (expand-file-name (file-name-nondirectory default
)
140 (file-name-as-directory name
))
144 (defun find-change-log (&optional file-name
)
145 "Find a change log file for \\[add-change-log-entry] and return the name.
147 Optional arg FILE-NAME specifies the file to use.
148 If FILE-NAME is nil, use the value of `change-log-default-name'.
149 If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
150 \(or whatever we use on this operating system).
152 If 'change-log-default-name' contains a leading directory component, then
153 simply find it in the current directory. Otherwise, search in the current
154 directory and its successive parents for a file so named.
156 Once a file is found, `change-log-default-name' is set locally in the
157 current buffer to the complete file name."
158 ;; If user specified a file name or if this buffer knows which one to use,
161 (setq file-name
(and change-log-default-name
162 (file-name-directory change-log-default-name
)
163 change-log-default-name
))
165 ;; Chase links in the source file
166 ;; and use the change log in the dir where it points.
167 (setq file-name
(or (and buffer-file-name
169 (file-chase-links buffer-file-name
)))
171 (if (file-directory-p file-name
)
172 (setq file-name
(expand-file-name (change-log-name) file-name
)))
173 ;; Chase links before visiting the file.
174 ;; This makes it easier to use a single change log file
175 ;; for several related directories.
176 (setq file-name
(file-chase-links file-name
))
177 (setq file-name
(expand-file-name file-name
))
178 ;; Move up in the dir hierarchy till we find a change log file.
179 (let ((file1 file-name
)
181 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
182 (progn (setq parent-dir
185 (file-name-directory file1
))))
186 ;; Give up if we are already at the root dir.
187 (not (string= (file-name-directory file1
)
189 ;; Move up to the parent dir and try again.
190 (setq file1
(expand-file-name
191 (file-name-nondirectory (change-log-name))
193 ;; If we found a change log in a parent, use that.
194 (if (or (get-file-buffer file1
) (file-exists-p file1
))
195 (setq file-name file1
)))))
196 ;; Make a local variable in this buffer so we needn't search again.
197 (set (make-local-variable 'change-log-default-name
) file-name
)
201 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
202 "Find change log file and add an entry for today.
203 Optional arg (interactive prefix) non-nil means prompt for user name and site.
204 Second arg is file name of change log. If nil, uses `change-log-default-name'.
205 Third arg OTHER-WINDOW non-nil means visit in other window.
206 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
207 never append to an existing entry. Today's date is calculated according to
208 `change-log-time-zone-rule' if non-nil, otherwise in local time."
209 (interactive (list current-prefix-arg
210 (prompt-for-change-log-name)))
211 (or add-log-full-name
212 (setq add-log-full-name
(user-full-name)))
213 (or add-log-mailing-address
214 (setq add-log-mailing-address user-mail-address
))
217 (setq add-log-full-name
(read-input "Full name: " add-log-full-name
))
218 ;; Note that some sites have room and phone number fields in
219 ;; full name which look silly when inserted. Rather than do
220 ;; anything about that here, let user give prefix argument so that
221 ;; s/he can edit the full name field in prompter if s/he wants.
222 (setq add-log-mailing-address
223 (read-input "Mailing address: " add-log-mailing-address
))))
224 (let ((defun (funcall (or add-log-current-defun-function
225 'add-log-current-defun
)))
228 (setq file-name
(expand-file-name (find-change-log file-name
)))
230 ;; Set ENTRY to the file name to use in the new entry.
231 (and buffer-file-name
232 ;; Never want to add a change log entry for the ChangeLog file itself.
233 (not (string= buffer-file-name file-name
))
234 (setq entry
(if (string-match
235 (concat "^" (regexp-quote (file-name-directory
238 (substring buffer-file-name
(match-end 0))
239 (file-name-nondirectory buffer-file-name
))))
241 (if (and other-window
(not (equal file-name buffer-file-name
)))
242 (find-file-other-window file-name
)
243 (find-file file-name
))
244 (or (eq major-mode
'change-log-mode
)
247 (goto-char (point-min))
248 (let ((new-entry (concat (if change-log-time-zone-rule
249 (let ((tz (getenv "TZ"))
250 (now (current-time)))
254 change-log-time-zone-rule
)
256 (format-time-string "%Y-%m-%d " now
)
257 (iso8601-time-zone now
)))
258 (set-time-zone-rule tz
)))
259 (format-time-string "%Y-%m-%d"))
260 " " add-log-full-name
261 " <" add-log-mailing-address
">")))
262 (if (looking-at (regexp-quote new-entry
))
264 (insert new-entry
"\n\n")))
266 ;; Search only within the first paragraph.
267 (if (looking-at "\n*[^\n* \t]")
268 (skip-chars-forward "\n")
269 (forward-paragraph 1))
270 (setq paragraph-end
(point))
271 (goto-char (point-min))
273 ;; Now insert the new line for this entry.
274 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t
)
275 ;; Put this file name into the existing empty entry.
278 ((and (not new-entry
)
279 (let (case-fold-search)
281 (concat (regexp-quote (concat "* " entry
))
282 ;; Don't accept `foo.bar' when
283 ;; looking for `foo':
284 "\\(\\s \\|[(),:]\\)")
286 ;; Add to the existing entry for the same file.
287 (re-search-forward "^\\s *$\\|^\\s \\*")
288 (goto-char (match-beginning 0))
289 ;; Delete excess empty lines; make just 2.
290 (while (and (not (eobp)) (looking-at "^\\s *$"))
291 (delete-region (point) (save-excursion (forward-line 1) (point))))
294 (indent-relative-maybe))
298 (while (looking-at "\\sW")
300 (while (and (not (eobp)) (looking-at "^\\s *$"))
301 (delete-region (point) (save-excursion (forward-line 1) (point))))
304 (indent-to left-margin
)
305 (insert "* " (or entry
""))))
306 ;; Now insert the function name, if we have one.
307 ;; Point is at the entry for this file,
308 ;; either at the end of the line or at the first blank line.
311 ;; Make it easy to get rid of the function name.
313 (insert (if (save-excursion
314 (beginning-of-line 1)
315 (looking-at "\\s *$"))
319 ;; No function name, so put in a colon unless we have just a star.
320 (if (not (save-excursion
321 (beginning-of-line 1)
322 (looking-at "\\s *\\(\\*\\s *\\)?$")))
326 (defun add-change-log-entry-other-window (&optional whoami file-name
)
327 "Find change log file in other window and add an entry for today.
328 Optional arg (interactive prefix) non-nil means prompt for user name and site.
329 Second arg is file name of change log. \
330 If nil, uses `change-log-default-name'."
331 (interactive (if current-prefix-arg
332 (list current-prefix-arg
333 (prompt-for-change-log-name))))
334 (add-change-log-entry whoami file-name t
))
335 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
338 (defun change-log-mode ()
339 "Major mode for editing change logs; like Indented Text Mode.
340 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
341 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
342 Each entry behaves as a paragraph, and the entries for one day as a page.
343 Runs `change-log-mode-hook'."
345 (kill-all-local-variables)
347 (setq major-mode
'change-log-mode
348 mode-name
"Change Log"
353 (use-local-map change-log-mode-map
)
354 (set (make-local-variable 'fill-paragraph-function
)
355 'change-log-fill-paragraph
)
356 ;; Let each entry behave as one paragraph:
357 ;; We really do want "^" in paragraph-start below: it is only the lines that
358 ;; begin at column 0 (despite the left-margin of 8) that we are looking for.
359 (set (make-local-variable 'paragraph-start
) "\\s *$\\|\f\\|^\\<")
360 (set (make-local-variable 'paragraph-separate
) "\\s *$\\|\f\\|^\\<")
361 ;; Let all entries for one day behave as one page.
362 ;; Match null string on the date-line so that the date-line
363 ;; is grouped with what follows.
364 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
365 (set (make-local-variable 'version-control
) 'never
)
366 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
367 (set (make-local-variable 'font-lock-defaults
)
368 '(change-log-font-lock-keywords t
))
369 (run-hooks 'change-log-mode-hook
))
371 ;; It might be nice to have a general feature to replace this. The idea I
372 ;; have is a variable giving a regexp matching text which should not be
373 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
374 ;; But I don't feel up to implementing that today.
375 (defun change-log-fill-paragraph (&optional justify
)
376 "Fill the paragraph, but preserve open parentheses at beginning of lines.
377 Prefix arg means justify as well."
379 (let ((end (progn (forward-paragraph) (point)))
380 (beg (progn (backward-paragraph) (point)))
381 (paragraph-start (concat paragraph-start
"\\|\\s *\\s(")))
382 (fill-region beg end justify
)
385 (defcustom add-log-current-defun-header-regexp
386 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
387 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes."
392 (defvar add-log-lisp-like-modes
393 '(emacs-lisp-mode lisp-mode scheme-mode lisp-interaction-mode
)
394 "*Modes that look like Lisp to `add-log-current-defun'.")
397 (defvar add-log-c-like-modes
398 '(c-mode c
++-mode c
++-c-mode objc-mode
)
399 "*Modes that look like C to `add-log-current-defun'.")
402 (defvar add-log-tex-like-modes
403 '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode
)
404 "*Modes that look like TeX to `add-log-current-defun'.")
407 (defun add-log-current-defun ()
408 "Return name of function definition point is in, or nil.
410 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
411 Texinfo (@node titles), Perl, and Fortran.
413 Other modes are handled by a heuristic that looks in the 10K before
414 point for uppercase headings starting in the first column or
415 identifiers followed by `:' or `=', see variable
416 `add-log-current-defun-header-regexp'.
418 Has a preference of looking backwards."
421 (let ((location (point)))
422 (cond ((memq major-mode add-log-lisp-like-modes
)
423 ;; If we are now precisely at the beginning of a defun,
424 ;; make sure beginning-of-defun finds that one
425 ;; rather than the previous one.
426 (or (eobp) (forward-char 1))
428 ;; Make sure we are really inside the defun found, not after it.
429 (if (and (looking-at "\\s(")
430 (progn (end-of-defun)
431 (< location
(point)))
432 (progn (forward-sexp -
1)
433 (>= location
(point))))
435 (if (looking-at "\\s(")
438 (skip-chars-forward " '")
439 (buffer-substring (point)
440 (progn (forward-sexp 1) (point))))))
441 ((and (memq major-mode add-log-c-like-modes
)
444 ;; Use eq instead of = here to avoid
445 ;; error when at bob and char-after
447 (while (eq (char-after (- (point) 2)) ?
\\)
449 (looking-at "[ \t]*#[ \t]*define[ \t]")))
450 ;; Handle a C macro definition.
452 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
454 (search-forward "define")
455 (skip-chars-forward " \t")
456 (buffer-substring (point)
457 (progn (forward-sexp 1) (point))))
458 ((memq major-mode add-log-c-like-modes
)
460 ;; See if we are in the beginning part of a function,
461 ;; before the open brace. If so, advance forward.
462 (while (not (looking-at "{\\|\\(\\s *$\\)"))
467 (if (progn (end-of-defun)
468 (< location
(point)))
474 ;; Skip back over typedefs of arglist.
475 (while (and (not (bobp))
476 (looking-at "[ \t\n]"))
478 ;; See if this is using the DEFUN macro used in Emacs,
479 ;; or the DEFUN macro used by the C library.
480 (if (condition-case nil
483 (while (= (preceding-char) ?
\\)
488 (looking-at "DEFUN\\b"))
494 (if (= (char-after (point)) ?
\")
497 (skip-chars-forward " ,")))
498 (buffer-substring (point)
499 (progn (forward-sexp 1) (point))))
500 (if (looking-at "^[+-]")
501 (get-method-definition)
502 ;; Ordinary C function syntax.
504 (if (and (condition-case nil
505 ;; Protect against "Unbalanced parens" error.
507 (down-list 1) ; into arglist
509 (skip-chars-backward " \t")
512 ;; Verify initial pos was after
513 ;; real start of function.
516 ;; For this purpose, include the line
517 ;; that has the decl keywords. This
518 ;; may also include some of the
519 ;; comments before the function.
520 (while (and (not (bobp))
523 (looking-at "[^\n\f]")))
525 (>= location
(point)))
526 ;; Consistency check: going down and up
527 ;; shouldn't take us back before BEG.
530 ;; Don't include any final newline
531 ;; in the name we use.
532 (if (= (preceding-char) ?
\n)
536 ;; Now find the right beginning of the name.
537 ;; Include certain keywords if they
539 (setq middle
(point))
541 ;; Ignore these subparts of a class decl
542 ;; and move back to the class name itself.
543 (while (looking-at "public \\|private ")
544 (skip-chars-backward " \t:")
547 (setq middle
(point))
550 (looking-at "struct \\|union \\|class ")
551 (setq middle
(point)))
552 (buffer-substring middle end
)))))))))
553 ((memq major-mode add-log-tex-like-modes
)
554 (if (re-search-backward
555 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
557 (goto-char (match-beginning 0))
558 (buffer-substring (1+ (point));; without initial backslash
562 ((eq major-mode
'texinfo-mode
)
563 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
564 (buffer-substring (match-beginning 1)
566 ((eq major-mode
'perl-mode
)
567 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t
)
568 (buffer-substring (match-beginning 1)
570 ((eq major-mode
'fortran-mode
)
571 ;; must be inside function body for this to work
572 (beginning-of-fortran-subprogram)
573 (let ((case-fold-search t
)) ; case-insensitive
574 ;; search for fortran subprogram start
575 (if (re-search-forward
576 "^[ \t]*\\(program\\|subroutine\\|function\
577 \\|[ \ta-z0-9*]*[ \t]+function\\)"
580 ;; move to EOL or before first left paren
581 (if (re-search-forward "[(\n]" nil t
)
582 (progn (forward-char -
1)
583 (skip-chars-backward " \t"))
585 ;; Use the name preceding that.
586 (buffer-substring (point)
587 (progn (forward-sexp -
1)
590 ;; If all else fails, try heuristics
591 (let (case-fold-search)
593 (if (re-search-backward add-log-current-defun-header-regexp
596 (buffer-substring (match-beginning 1)
600 (defvar get-method-definition-md
)
602 ;; Subroutine used within get-method-definition.
603 ;; Add the last match in the buffer to the end of `md',
604 ;; followed by the string END; move to the end of that match.
605 (defun get-method-definition-1 (end)
606 (setq get-method-definition-md
607 (concat get-method-definition-md
608 (buffer-substring (match-beginning 1) (match-end 1))
610 (goto-char (match-end 0)))
612 ;; For objective C, return the method name if we are in a method.
613 (defun get-method-definition ()
614 (let ((get-method-definition-md "["))
616 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
617 (get-method-definition-1 " ")))
620 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
621 (get-method-definition-1 "")
622 (while (not (looking-at "[{;]"))
624 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
625 (get-method-definition-1 ""))
626 (concat get-method-definition-md
"]"))))))
631 ;;; add-log.el ends here