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.
72 (0 font-lock-string-face
)
73 ("[A-Z][^\n<]+" nil nil
(0 font-lock-reference-face
)))
76 ("^\t\\* \\([^ ,:([\n]+\\)"
77 (1 font-lock-function-name-face
)
78 ("\\=, \\([^ ,:([\n]+\\)" nil nil
(1 font-lock-function-name-face
)))
80 ;; Function or variable names.
82 (1 font-lock-keyword-face
)
83 ("\\=, \\([^ ,:\n]+\\)" nil nil
(1 font-lock-keyword-face
)))
86 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 font-lock-variable-name-face
))
89 ("^\t\\(From\\|Reported by\\)" 1 font-lock-comment-face
)
91 "Additional expressions to highlight in Change Log mode.")
93 (defvar change-log-mode-map nil
94 "Keymap for Change Log major mode.")
95 (if change-log-mode-map
97 (setq change-log-mode-map
(make-sparse-keymap)))
99 (defvar change-log-time-zone-rule nil
100 "Time zone used for calculating change log time stamps.
101 It takes the same format as the TZ argument of `set-time-zone-rule'.
102 If nil, use local time.")
104 (defun iso8601-time-zone (time)
105 (let* ((utc-offset (or (car (current-time-zone time
)) 0))
106 (sign (if (< utc-offset
0) ?- ?
+))
107 (sec (abs utc-offset
))
112 (format (cond ((not (zerop ss
)) "%c%02d:%02d:%02d")
113 ((not (zerop mm
)) "%c%02d:%02d")
117 (defun change-log-name ()
118 (or change-log-default-name
119 (if (eq system-type
'vax-vms
)
124 (defun prompt-for-change-log-name ()
125 "Prompt for a change log name."
126 (let* ((default (change-log-name))
127 (name (expand-file-name
128 (read-file-name (format "Log file (default %s): " default
)
130 ;; Handle something that is syntactically a directory name.
131 ;; Look for ChangeLog or whatever in that directory.
132 (if (string= (file-name-nondirectory name
) "")
133 (expand-file-name (file-name-nondirectory default
)
135 ;; Handle specifying a file that is a directory.
136 (if (file-directory-p name
)
137 (expand-file-name (file-name-nondirectory default
)
138 (file-name-as-directory name
))
142 (defun find-change-log (&optional file-name
)
143 "Find a change log file for \\[add-change-log-entry] and return the name.
145 Optional arg FILE-NAME specifies the file to use.
146 If FILE-NAME is nil, use the value of `change-log-default-name'.
147 If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
148 \(or whatever we use on this operating system).
150 If 'change-log-default-name' contains a leading directory component, then
151 simply find it in the current directory. Otherwise, search in the current
152 directory and its successive parents for a file so named.
154 Once a file is found, `change-log-default-name' is set locally in the
155 current buffer to the complete file name."
156 ;; If user specified a file name or if this buffer knows which one to use,
159 (setq file-name
(and change-log-default-name
160 (file-name-directory change-log-default-name
)
161 change-log-default-name
))
163 ;; Chase links in the source file
164 ;; and use the change log in the dir where it points.
165 (setq file-name
(or (and buffer-file-name
167 (file-chase-links buffer-file-name
)))
169 (if (file-directory-p file-name
)
170 (setq file-name
(expand-file-name (change-log-name) file-name
)))
171 ;; Chase links before visiting the file.
172 ;; This makes it easier to use a single change log file
173 ;; for several related directories.
174 (setq file-name
(file-chase-links file-name
))
175 (setq file-name
(expand-file-name file-name
))
176 ;; Move up in the dir hierarchy till we find a change log file.
177 (let ((file1 file-name
)
179 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
180 (progn (setq parent-dir
183 (file-name-directory file1
))))
184 ;; Give up if we are already at the root dir.
185 (not (string= (file-name-directory file1
)
187 ;; Move up to the parent dir and try again.
188 (setq file1
(expand-file-name
189 (file-name-nondirectory (change-log-name))
191 ;; If we found a change log in a parent, use that.
192 (if (or (get-file-buffer file1
) (file-exists-p file1
))
193 (setq file-name file1
)))))
194 ;; Make a local variable in this buffer so we needn't search again.
195 (set (make-local-variable 'change-log-default-name
) file-name
)
199 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
200 "Find change log file and add an entry for today.
201 Optional arg (interactive prefix) non-nil means prompt for user name and site.
202 Second arg is file name of change log. If nil, uses `change-log-default-name'.
203 Third arg OTHER-WINDOW non-nil means visit in other window.
204 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
205 never append to an existing entry. Today's date is calculated according to
206 `change-log-time-zone-rule' if non-nil, otherwise in local time."
207 (interactive (list current-prefix-arg
208 (prompt-for-change-log-name)))
209 (or add-log-full-name
210 (setq add-log-full-name
(user-full-name)))
211 (or add-log-mailing-address
212 (setq add-log-mailing-address user-mail-address
))
215 (setq add-log-full-name
(read-input "Full name: " add-log-full-name
))
216 ;; Note that some sites have room and phone number fields in
217 ;; full name which look silly when inserted. Rather than do
218 ;; anything about that here, let user give prefix argument so that
219 ;; s/he can edit the full name field in prompter if s/he wants.
220 (setq add-log-mailing-address
221 (read-input "Mailing address: " add-log-mailing-address
))))
222 (let ((defun (funcall (or add-log-current-defun-function
223 'add-log-current-defun
)))
226 (setq file-name
(expand-file-name (find-change-log file-name
)))
228 ;; Set ENTRY to the file name to use in the new entry.
229 (and buffer-file-name
230 ;; Never want to add a change log entry for the ChangeLog file itself.
231 (not (string= buffer-file-name file-name
))
232 (setq entry
(if (string-match
233 (concat "^" (regexp-quote (file-name-directory
236 (substring buffer-file-name
(match-end 0))
237 (file-name-nondirectory buffer-file-name
))))
239 (if (and other-window
(not (equal file-name buffer-file-name
)))
240 (find-file-other-window file-name
)
241 (find-file file-name
))
242 (or (eq major-mode
'change-log-mode
)
245 (goto-char (point-min))
246 (let ((new-entry (concat (if change-log-time-zone-rule
247 (let ((tz (getenv "TZ"))
248 (now (current-time)))
252 change-log-time-zone-rule
)
254 (format-time-string "%Y-%m-%d " now
)
255 (iso8601-time-zone now
)))
256 (set-time-zone-rule tz
)))
257 (format-time-string "%Y-%m-%d"))
258 " " add-log-full-name
259 " <" add-log-mailing-address
">")))
260 (if (looking-at (regexp-quote new-entry
))
262 (insert new-entry
"\n\n")))
264 ;; Search only within the first paragraph.
265 (if (looking-at "\n*[^\n* \t]")
266 (skip-chars-forward "\n")
267 (forward-paragraph 1))
268 (setq paragraph-end
(point))
269 (goto-char (point-min))
271 ;; Now insert the new line for this entry.
272 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t
)
273 ;; Put this file name into the existing empty entry.
276 ((and (not new-entry
)
277 (let (case-fold-search)
279 (concat (regexp-quote (concat "* " entry
))
280 ;; Don't accept `foo.bar' when
281 ;; looking for `foo':
282 "\\(\\s \\|[(),:]\\)")
284 ;; Add to the existing entry for the same file.
285 (re-search-forward "^\\s *$\\|^\\s \\*")
286 (goto-char (match-beginning 0))
287 ;; Delete excess empty lines; make just 2.
288 (while (and (not (eobp)) (looking-at "^\\s *$"))
289 (delete-region (point) (save-excursion (forward-line 1) (point))))
292 (indent-relative-maybe))
296 (while (looking-at "\\sW")
298 (while (and (not (eobp)) (looking-at "^\\s *$"))
299 (delete-region (point) (save-excursion (forward-line 1) (point))))
302 (indent-to left-margin
)
303 (insert "* " (or entry
""))))
304 ;; Now insert the function name, if we have one.
305 ;; Point is at the entry for this file,
306 ;; either at the end of the line or at the first blank line.
309 ;; Make it easy to get rid of the function name.
311 (insert (if (save-excursion
312 (beginning-of-line 1)
313 (looking-at "\\s *$"))
317 ;; No function name, so put in a colon unless we have just a star.
318 (if (not (save-excursion
319 (beginning-of-line 1)
320 (looking-at "\\s *\\(\\*\\s *\\)?$")))
324 (defun add-change-log-entry-other-window (&optional whoami file-name
)
325 "Find change log file in other window and add an entry for today.
326 Optional arg (interactive prefix) non-nil means prompt for user name and site.
327 Second arg is file name of change log. \
328 If nil, uses `change-log-default-name'."
329 (interactive (if current-prefix-arg
330 (list current-prefix-arg
331 (prompt-for-change-log-name))))
332 (add-change-log-entry whoami file-name t
))
333 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
336 (defun change-log-mode ()
337 "Major mode for editing change logs; like Indented Text Mode.
338 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
339 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
340 Each entry behaves as a paragraph, and the entries for one day as a page.
341 Runs `change-log-mode-hook'."
343 (kill-all-local-variables)
345 (setq major-mode
'change-log-mode
346 mode-name
"Change Log"
351 (use-local-map change-log-mode-map
)
352 (set (make-local-variable 'fill-paragraph-function
)
353 'change-log-fill-paragraph
)
354 ;; Let each entry behave as one paragraph:
355 ;; We really do want "^" in paragraph-start below: it is only the lines that
356 ;; begin at column 0 (despite the left-margin of 8) that we are looking for.
357 (set (make-local-variable 'paragraph-start
) "\\s *$\\|\f\\|^\\<")
358 (set (make-local-variable 'paragraph-separate
) "\\s *$\\|\f\\|^\\<")
359 ;; Let all entries for one day behave as one page.
360 ;; Match null string on the date-line so that the date-line
361 ;; is grouped with what follows.
362 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
363 (set (make-local-variable 'version-control
) 'never
)
364 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
365 (set (make-local-variable 'font-lock-defaults
)
366 '(change-log-font-lock-keywords t
))
367 (run-hooks 'change-log-mode-hook
))
369 ;; It might be nice to have a general feature to replace this. The idea I
370 ;; have is a variable giving a regexp matching text which should not be
371 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
372 ;; But I don't feel up to implementing that today.
373 (defun change-log-fill-paragraph (&optional justify
)
374 "Fill the paragraph, but preserve open parentheses at beginning of lines.
375 Prefix arg means justify as well."
377 (let ((end (progn (forward-paragraph) (point)))
378 (beg (progn (backward-paragraph) (point)))
379 (paragraph-start (concat paragraph-start
"\\|\\s *\\s(")))
380 (fill-region beg end justify
)
383 (defcustom add-log-current-defun-header-regexp
384 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
385 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes."
390 (defun add-log-current-defun ()
391 "Return name of function definition point is in, or nil.
393 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
394 Texinfo (@node titles), Perl, and Fortran.
396 Other modes are handled by a heuristic that looks in the 10K before
397 point for uppercase headings starting in the first column or
398 identifiers followed by `:' or `=', see variable
399 `add-log-current-defun-header-regexp'.
401 Has a preference of looking backwards."
404 (let ((location (point)))
405 (cond ((memq major-mode
'(emacs-lisp-mode lisp-mode scheme-mode
406 lisp-interaction-mode
))
407 ;; If we are now precisely at the beginning of a defun,
408 ;; make sure beginning-of-defun finds that one
409 ;; rather than the previous one.
410 (or (eobp) (forward-char 1))
412 ;; Make sure we are really inside the defun found, not after it.
413 (if (and (looking-at "\\s(")
414 (progn (end-of-defun)
415 (< location
(point)))
416 (progn (forward-sexp -
1)
417 (>= location
(point))))
419 (if (looking-at "\\s(")
422 (skip-chars-forward " '")
423 (buffer-substring (point)
424 (progn (forward-sexp 1) (point))))))
425 ((and (memq major-mode
'(c-mode c
++-mode c
++-c-mode objc-mode
))
426 (save-excursion (beginning-of-line)
427 ;; Use eq instead of = here to avoid
428 ;; error when at bob and char-after
430 (while (eq (char-after (- (point) 2)) ?
\\)
432 (looking-at "[ \t]*#[ \t]*define[ \t]")))
433 ;; Handle a C macro definition.
435 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
437 (search-forward "define")
438 (skip-chars-forward " \t")
439 (buffer-substring (point)
440 (progn (forward-sexp 1) (point))))
441 ((memq major-mode
'(c-mode c
++-mode c
++-c-mode objc-mode
))
443 ;; See if we are in the beginning part of a function,
444 ;; before the open brace. If so, advance forward.
445 (while (not (looking-at "{\\|\\(\\s *$\\)"))
450 (if (progn (end-of-defun)
451 (< location
(point)))
457 ;; Skip back over typedefs of arglist.
458 (while (and (not (bobp))
459 (looking-at "[ \t\n]"))
461 ;; See if this is using the DEFUN macro used in Emacs,
462 ;; or the DEFUN macro used by the C library.
463 (if (condition-case nil
466 (while (= (preceding-char) ?
\\)
471 (looking-at "DEFUN\\b"))
477 (if (= (char-after (point)) ?
\")
480 (skip-chars-forward " ,")))
481 (buffer-substring (point)
482 (progn (forward-sexp 1) (point))))
483 (if (looking-at "^[+-]")
484 (get-method-definition)
485 ;; Ordinary C function syntax.
487 (if (and (condition-case nil
488 ;; Protect against "Unbalanced parens" error.
490 (down-list 1) ; into arglist
492 (skip-chars-backward " \t")
495 ;; Verify initial pos was after
496 ;; real start of function.
499 ;; For this purpose, include the line
500 ;; that has the decl keywords. This
501 ;; may also include some of the
502 ;; comments before the function.
503 (while (and (not (bobp))
506 (looking-at "[^\n\f]")))
508 (>= location
(point)))
509 ;; Consistency check: going down and up
510 ;; shouldn't take us back before BEG.
513 ;; Don't include any final newline
514 ;; in the name we use.
515 (if (= (preceding-char) ?
\n)
519 ;; Now find the right beginning of the name.
520 ;; Include certain keywords if they
522 (setq middle
(point))
524 ;; Ignore these subparts of a class decl
525 ;; and move back to the class name itself.
526 (while (looking-at "public \\|private ")
527 (skip-chars-backward " \t:")
530 (setq middle
(point))
533 (looking-at "struct \\|union \\|class ")
534 (setq middle
(point)))
535 (buffer-substring middle end
)))))))))
537 '(TeX-mode plain-TeX-mode LaTeX-mode
;; tex-mode.el
538 plain-tex-mode latex-mode
;; cmutex.el
540 (if (re-search-backward
541 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
543 (goto-char (match-beginning 0))
544 (buffer-substring (1+ (point));; without initial backslash
548 ((eq major-mode
'texinfo-mode
)
549 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
550 (buffer-substring (match-beginning 1)
552 ((eq major-mode
'perl-mode
)
553 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t
)
554 (buffer-substring (match-beginning 1)
556 ((eq major-mode
'fortran-mode
)
557 ;; must be inside function body for this to work
558 (beginning-of-fortran-subprogram)
559 (let ((case-fold-search t
)) ; case-insensitive
560 ;; search for fortran subprogram start
561 (if (re-search-forward
562 "^[ \t]*\\(program\\|subroutine\\|function\
563 \\|[ \ta-z0-9*]*[ \t]+function\\)"
566 ;; move to EOL or before first left paren
567 (if (re-search-forward "[(\n]" nil t
)
568 (progn (forward-char -
1)
569 (skip-chars-backward " \t"))
571 ;; Use the name preceding that.
572 (buffer-substring (point)
573 (progn (forward-sexp -
1)
576 ;; If all else fails, try heuristics
577 (let (case-fold-search)
579 (if (re-search-backward add-log-current-defun-header-regexp
582 (buffer-substring (match-beginning 1)
586 (defvar get-method-definition-md
)
588 ;; Subroutine used within get-method-definition.
589 ;; Add the last match in the buffer to the end of `md',
590 ;; followed by the string END; move to the end of that match.
591 (defun get-method-definition-1 (end)
592 (setq get-method-definition-md
593 (concat get-method-definition-md
594 (buffer-substring (match-beginning 1) (match-end 1))
596 (goto-char (match-end 0)))
598 ;; For objective C, return the method name if we are in a method.
599 (defun get-method-definition ()
600 (let ((get-method-definition-md "["))
602 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
603 (get-method-definition-1 " ")))
606 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
607 (get-method-definition-1 "")
608 (while (not (looking-at "[{;]"))
610 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
611 (get-method-definition-1 ""))
612 (concat get-method-definition-md
"]"))))))
617 ;;; add-log.el ends here