1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1988, 1993, 1994 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 (defvar change-log-default-name nil
31 "*Name of a change log file for \\[add-change-log-entry].")
33 (defvar add-log-current-defun-function nil
35 *If non-nil, function to guess name of current function from surrounding text.
36 \\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
37 instead) with no arguments. It returns a string or nil if it cannot guess.")
40 (defvar add-log-full-name nil
41 "*Full name of user, for inclusion in ChangeLog daily headers.
42 This defaults to the value returned by the `user-full-name' function.")
45 (defvar add-log-mailing-address nil
46 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
47 This defaults to the value of `user-mail-address'.")
49 (defvar change-log-font-lock-keywords
50 '(("^[12].+" . font-lock-function-name-face
) ; Date line.
51 ("^\t\\* \\([^ :\n]+\\)" 1 font-lock-comment-face
) ; File name.
52 ("(\\([^)\n]+\\)):" 1 font-lock-keyword-face
)) ; Function name.
53 "Additional expressions to highlight in Change Log mode.")
55 (defvar change-log-mode-map nil
56 "Keymap for Change Log major mode.")
57 (if change-log-mode-map
59 (setq change-log-mode-map
(make-sparse-keymap)))
61 (defvar change-log-time-zone-rule nil
62 "Time zone used for calculating change log time stamps.
63 It takes the same format as the TZ argument of `set-time-zone-rule'.
64 If nil, use local time.")
66 (defun iso8601-time-zone (time)
67 (let* ((utc-offset (or (car (current-time-zone time
)) 0))
68 (sign (if (< utc-offset
0) ?- ?
+))
69 (sec (abs utc-offset
))
74 (format (cond ((not (zerop ss
)) "%c%02d:%02d:%02d")
75 ((not (zerop mm
)) "%c%02d:%02d")
79 (defun change-log-name ()
80 (or change-log-default-name
81 (if (eq system-type
'vax-vms
)
86 (defun prompt-for-change-log-name ()
87 "Prompt for a change log name."
88 (let* ((default (change-log-name))
89 (name (expand-file-name
90 (read-file-name (format "Log file (default %s): " default
)
92 ;; Handle something that is syntactically a directory name.
93 ;; Look for ChangeLog or whatever in that directory.
94 (if (string= (file-name-nondirectory name
) "")
95 (expand-file-name (file-name-nondirectory default
)
97 ;; Handle specifying a file that is a directory.
98 (if (file-directory-p name
)
99 (expand-file-name (file-name-nondirectory default
)
100 (file-name-as-directory name
))
104 (defun find-change-log (&optional file-name
)
105 "Find a change log file for \\[add-change-log-entry] and return the name.
107 Optional arg FILE-NAME specifies the file to use.
108 If FILE-NAME is nil, use the value of `change-log-default-name'.
109 If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
110 \(or whatever we use on this operating system).
112 If 'change-log-default-name' contains a leading directory component, then
113 simply find it in the current directory. Otherwise, search in the current
114 directory and its successive parents for a file so named.
116 Once a file is found, `change-log-default-name' is set locally in the
117 current buffer to the complete file name."
118 ;; If user specified a file name or if this buffer knows which one to use,
121 (setq file-name
(and change-log-default-name
122 (file-name-directory change-log-default-name
)
123 change-log-default-name
))
125 ;; Chase links in the source file
126 ;; and use the change log in the dir where it points.
127 (setq file-name
(or (and buffer-file-name
129 (file-chase-links buffer-file-name
)))
131 (if (file-directory-p file-name
)
132 (setq file-name
(expand-file-name (change-log-name) file-name
)))
133 ;; Chase links before visiting the file.
134 ;; This makes it easier to use a single change log file
135 ;; for several related directories.
136 (setq file-name
(file-chase-links file-name
))
137 (setq file-name
(expand-file-name file-name
))
138 ;; Move up in the dir hierarchy till we find a change log file.
139 (let ((file1 file-name
)
141 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
142 (progn (setq parent-dir
145 (file-name-directory file1
))))
146 ;; Give up if we are already at the root dir.
147 (not (string= (file-name-directory file1
)
149 ;; Move up to the parent dir and try again.
150 (setq file1
(expand-file-name
151 (file-name-nondirectory (change-log-name))
153 ;; If we found a change log in a parent, use that.
154 (if (or (get-file-buffer file1
) (file-exists-p file1
))
155 (setq file-name file1
)))))
156 ;; Make a local variable in this buffer so we needn't search again.
157 (set (make-local-variable 'change-log-default-name
) file-name
)
161 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
162 "Find change log file and add an entry for today.
163 Optional arg (interactive prefix) non-nil means prompt for user name and site.
164 Second arg is file name of change log. If nil, uses `change-log-default-name'.
165 Third arg OTHER-WINDOW non-nil means visit in other window.
166 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
167 never append to an existing entry. Today's date is calculated according to
168 `change-log-time-zone-rule' if non-nil, otherwise in local time."
169 (interactive (list current-prefix-arg
170 (prompt-for-change-log-name)))
171 (or add-log-full-name
172 (setq add-log-full-name
(user-full-name)))
173 (or add-log-mailing-address
174 (setq add-log-mailing-address user-mail-address
))
177 (setq add-log-full-name
(read-input "Full name: " add-log-full-name
))
178 ;; Note that some sites have room and phone number fields in
179 ;; full name which look silly when inserted. Rather than do
180 ;; anything about that here, let user give prefix argument so that
181 ;; s/he can edit the full name field in prompter if s/he wants.
182 (setq add-log-mailing-address
183 (read-input "Mailing address: " add-log-mailing-address
))))
184 (let ((defun (funcall (or add-log-current-defun-function
185 'add-log-current-defun
)))
188 (setq file-name
(expand-file-name (find-change-log file-name
)))
190 ;; Set ENTRY to the file name to use in the new entry.
191 (and buffer-file-name
192 ;; Never want to add a change log entry for the ChangeLog file itself.
193 (not (string= buffer-file-name file-name
))
194 (setq entry
(if (string-match
195 (concat "^" (regexp-quote (file-name-directory
198 (substring buffer-file-name
(match-end 0))
199 (file-name-nondirectory buffer-file-name
))))
201 (if (and other-window
(not (equal file-name buffer-file-name
)))
202 (find-file-other-window file-name
)
203 (find-file file-name
))
204 (or (eq major-mode
'change-log-mode
)
207 (goto-char (point-min))
208 (let ((new-entry (concat (if change-log-time-zone-rule
209 (let ((tz (getenv "TZ"))
210 (now (current-time)))
214 change-log-time-zone-rule
)
216 (format-time-string "%Y-%m-%d " now
)
217 (iso8601-time-zone now
)))
218 (set-time-zone-rule tz
)))
219 (format-time-string "%Y-%m-%d"))
220 " " add-log-full-name
221 " <" add-log-mailing-address
">")))
222 (if (looking-at (regexp-quote new-entry
))
224 (insert new-entry
"\n\n")))
226 ;; Search only within the first paragraph.
227 (if (looking-at "\n*[^\n* \t]")
228 (skip-chars-forward "\n")
229 (forward-paragraph 1))
230 (setq paragraph-end
(point))
231 (goto-char (point-min))
233 ;; Now insert the new line for this entry.
234 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t
)
235 ;; Put this file name into the existing empty entry.
238 ((and (not new-entry
)
239 (let (case-fold-search)
241 (concat (regexp-quote (concat "* " entry
))
242 ;; Don't accept `foo.bar' when
243 ;; looking for `foo':
244 "\\(\\s \\|[(),:]\\)")
246 ;; Add to the existing entry for the same file.
247 (re-search-forward "^\\s *$\\|^\\s \\*")
248 (goto-char (match-beginning 0))
249 ;; Delete excess empty lines; make just 2.
250 (while (and (not (eobp)) (looking-at "^\\s *$"))
251 (delete-region (point) (save-excursion (forward-line 1) (point))))
254 (indent-relative-maybe))
258 (while (looking-at "\\sW")
260 (while (and (not (eobp)) (looking-at "^\\s *$"))
261 (delete-region (point) (save-excursion (forward-line 1) (point))))
264 (indent-to left-margin
)
265 (insert "* " (or entry
""))))
266 ;; Now insert the function name, if we have one.
267 ;; Point is at the entry for this file,
268 ;; either at the end of the line or at the first blank line.
271 ;; Make it easy to get rid of the function name.
273 (insert (if (save-excursion
274 (beginning-of-line 1)
275 (looking-at "\\s *$"))
279 ;; No function name, so put in a colon unless we have just a star.
280 (if (not (save-excursion
281 (beginning-of-line 1)
282 (looking-at "\\s *\\(\\*\\s *\\)?$")))
286 (defun add-change-log-entry-other-window (&optional whoami file-name
)
287 "Find change log file in other window and add an entry for today.
288 Optional arg (interactive prefix) non-nil means prompt for user name and site.
289 Second arg is file name of change log. \
290 If nil, uses `change-log-default-name'."
291 (interactive (if current-prefix-arg
292 (list current-prefix-arg
293 (prompt-for-change-log-name))))
294 (add-change-log-entry whoami file-name t
))
295 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
298 (defun change-log-mode ()
299 "Major mode for editing change logs; like Indented Text Mode.
300 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
301 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
302 Each entry behaves as a paragraph, and the entries for one day as a page.
303 Runs `change-log-mode-hook'."
305 (kill-all-local-variables)
307 (setq major-mode
'change-log-mode
308 mode-name
"Change Log"
313 (use-local-map change-log-mode-map
)
314 (set (make-local-variable 'fill-paragraph-function
)
315 'change-log-fill-paragraph
)
316 ;; Let each entry behave as one paragraph:
317 ;; We really do want "^" in paragraph-start below: it is only the lines that
318 ;; begin at column 0 (despite the left-margin of 8) that we are looking for.
319 (set (make-local-variable 'paragraph-start
) "\\s *$\\|\f\\|^\\<")
320 (set (make-local-variable 'paragraph-separate
) "\\s *$\\|\f\\|^\\<")
321 ;; Let all entries for one day behave as one page.
322 ;; Match null string on the date-line so that the date-line
323 ;; is grouped with what follows.
324 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
325 (set (make-local-variable 'version-control
) 'never
)
326 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
327 (set (make-local-variable 'font-lock-defaults
)
328 '(change-log-font-lock-keywords t
))
329 (run-hooks 'change-log-mode-hook
))
331 ;; It might be nice to have a general feature to replace this. The idea I
332 ;; have is a variable giving a regexp matching text which should not be
333 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
334 ;; But I don't feel up to implementing that today.
335 (defun change-log-fill-paragraph (&optional justify
)
336 "Fill the paragraph, but preserve open parentheses at beginning of lines.
337 Prefix arg means justify as well."
339 (let ((end (save-excursion (forward-paragraph) (point)))
340 (beg (save-excursion (backward-paragraph)(point)))
341 (paragraph-start (concat paragraph-start
"\\|\\s *\\s(")))
342 (fill-region beg end justify
)))
344 (defvar add-log-current-defun-header-regexp
345 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
346 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
349 (defun add-log-current-defun ()
350 "Return name of function definition point is in, or nil.
352 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
353 Texinfo (@node titles), Perl, and Fortran.
355 Other modes are handled by a heuristic that looks in the 10K before
356 point for uppercase headings starting in the first column or
357 identifiers followed by `:' or `=', see variable
358 `add-log-current-defun-header-regexp'.
360 Has a preference of looking backwards."
363 (let ((location (point)))
364 (cond ((memq major-mode
'(emacs-lisp-mode lisp-mode scheme-mode
365 lisp-interaction-mode
))
366 ;; If we are now precisely at the beginning of a defun,
367 ;; make sure beginning-of-defun finds that one
368 ;; rather than the previous one.
369 (or (eobp) (forward-char 1))
371 ;; Make sure we are really inside the defun found, not after it.
372 (if (and (looking-at "\\s(")
373 (progn (end-of-defun)
374 (< location
(point)))
375 (progn (forward-sexp -
1)
376 (>= location
(point))))
378 (if (looking-at "\\s(")
381 (skip-chars-forward " '")
382 (buffer-substring (point)
383 (progn (forward-sexp 1) (point))))))
384 ((and (memq major-mode
'(c-mode c
++-mode c
++-c-mode objc-mode
))
385 (save-excursion (beginning-of-line)
386 ;; Use eq instead of = here to avoid
387 ;; error when at bob and char-after
389 (while (eq (char-after (- (point) 2)) ?
\\)
391 (looking-at "[ \t]*#[ \t]*define[ \t]")))
392 ;; Handle a C macro definition.
394 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
396 (search-forward "define")
397 (skip-chars-forward " \t")
398 (buffer-substring (point)
399 (progn (forward-sexp 1) (point))))
400 ((memq major-mode
'(c-mode c
++-mode c
++-c-mode objc-mode
))
402 ;; See if we are in the beginning part of a function,
403 ;; before the open brace. If so, advance forward.
404 (while (not (looking-at "{\\|\\(\\s *$\\)"))
409 (if (progn (end-of-defun)
410 (< location
(point)))
416 ;; Skip back over typedefs of arglist.
417 (while (and (not (bobp))
418 (looking-at "[ \t\n]"))
420 ;; See if this is using the DEFUN macro used in Emacs,
421 ;; or the DEFUN macro used by the C library.
422 (if (condition-case nil
425 (while (= (preceding-char) ?
\\)
430 (looking-at "DEFUN\\b"))
436 (if (= (char-after (point)) ?
\")
439 (skip-chars-forward " ,")))
440 (buffer-substring (point)
441 (progn (forward-sexp 1) (point))))
442 (if (looking-at "^[+-]")
443 (get-method-definition)
444 ;; Ordinary C function syntax.
446 (if (and (condition-case nil
447 ;; Protect against "Unbalanced parens" error.
449 (down-list 1) ; into arglist
451 (skip-chars-backward " \t")
454 ;; Verify initial pos was after
455 ;; real start of function.
458 ;; For this purpose, include the line
459 ;; that has the decl keywords. This
460 ;; may also include some of the
461 ;; comments before the function.
462 (while (and (not (bobp))
465 (looking-at "[^\n\f]")))
467 (>= location
(point)))
468 ;; Consistency check: going down and up
469 ;; shouldn't take us back before BEG.
472 ;; Don't include any final newline
473 ;; in the name we use.
474 (if (= (preceding-char) ?
\n)
478 ;; Now find the right beginning of the name.
479 ;; Include certain keywords if they
481 (setq middle
(point))
483 ;; Ignore these subparts of a class decl
484 ;; and move back to the class name itself.
485 (while (looking-at "public \\|private ")
486 (skip-chars-backward " \t:")
489 (setq middle
(point))
492 (looking-at "struct \\|union \\|class ")
493 (setq middle
(point)))
494 (buffer-substring middle end
)))))))))
496 '(TeX-mode plain-TeX-mode LaTeX-mode
;; tex-mode.el
497 plain-tex-mode latex-mode
;; cmutex.el
499 (if (re-search-backward
500 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
502 (goto-char (match-beginning 0))
503 (buffer-substring (1+ (point));; without initial backslash
507 ((eq major-mode
'texinfo-mode
)
508 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
509 (buffer-substring (match-beginning 1)
511 ((eq major-mode
'perl-mode
)
512 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t
)
513 (buffer-substring (match-beginning 1)
515 ((eq major-mode
'fortran-mode
)
516 ;; must be inside function body for this to work
517 (beginning-of-fortran-subprogram)
518 (let ((case-fold-search t
)) ; case-insensitive
519 ;; search for fortran subprogram start
520 (if (re-search-forward
521 "^[ \t]*\\(program\\|subroutine\\|function\
522 \\|[ \ta-z0-9*]*[ \t]+function\\)"
525 ;; move to EOL or before first left paren
526 (if (re-search-forward "[(\n]" nil t
)
527 (progn (forward-char -
1)
528 (skip-chars-backward " \t"))
530 ;; Use the name preceding that.
531 (buffer-substring (point)
532 (progn (forward-sexp -
1)
535 ;; If all else fails, try heuristics
536 (let (case-fold-search)
538 (if (re-search-backward add-log-current-defun-header-regexp
541 (buffer-substring (match-beginning 1)
545 (defvar get-method-definition-md
)
547 ;; Subroutine used within get-method-definition.
548 ;; Add the last match in the buffer to the end of `md',
549 ;; followed by the string END; move to the end of that match.
550 (defun get-method-definition-1 (end)
551 (setq get-method-definition-md
552 (concat get-method-definition-md
553 (buffer-substring (match-beginning 1) (match-end 1))
555 (goto-char (match-end 0)))
557 ;; For objective C, return the method name if we are in a method.
558 (defun get-method-definition ()
559 (let ((get-method-definition-md "["))
561 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
562 (get-method-definition-1 " ")))
565 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
566 (get-method-definition-1 "")
567 (while (not (looking-at "[{;]"))
569 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
570 (get-method-definition-1 ""))
571 (concat get-method-definition-md
"]"))))))
576 ;;; add-log.el ends here