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
51 ;; Date lines, new and old styles.
53 (0 font-lock-string-face
)
54 ("[A-Z][^\n<]+" nil nil
(0 font-lock-reference-face
)))
57 ("^\t\\* \\([^ ,:([\n]+\\)"
58 (1 font-lock-function-name-face
)
59 ("\\=, \\([^ ,:([\n]+\\)" nil nil
(1 font-lock-function-name-face
)))
61 ;; Function or variable names.
63 (1 font-lock-keyword-face
)
64 ("\\=, \\([^ ,:\n]+\\)" nil nil
(1 font-lock-keyword-face
)))
67 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 font-lock-variable-name-face
))
70 ("^\t\\(From\\|Reported by\\)" 1 font-lock-comment-face
)
72 "Additional expressions to highlight in Change Log mode.")
74 (defvar change-log-mode-map nil
75 "Keymap for Change Log major mode.")
76 (if change-log-mode-map
78 (setq change-log-mode-map
(make-sparse-keymap)))
80 (defvar change-log-time-zone-rule nil
81 "Time zone used for calculating change log time stamps.
82 It takes the same format as the TZ argument of `set-time-zone-rule'.
83 If nil, use local time.")
85 (defun iso8601-time-zone (time)
86 (let* ((utc-offset (or (car (current-time-zone time
)) 0))
87 (sign (if (< utc-offset
0) ?- ?
+))
88 (sec (abs utc-offset
))
93 (format (cond ((not (zerop ss
)) "%c%02d:%02d:%02d")
94 ((not (zerop mm
)) "%c%02d:%02d")
98 (defun change-log-name ()
99 (or change-log-default-name
100 (if (eq system-type
'vax-vms
)
105 (defun prompt-for-change-log-name ()
106 "Prompt for a change log name."
107 (let* ((default (change-log-name))
108 (name (expand-file-name
109 (read-file-name (format "Log file (default %s): " default
)
111 ;; Handle something that is syntactically a directory name.
112 ;; Look for ChangeLog or whatever in that directory.
113 (if (string= (file-name-nondirectory name
) "")
114 (expand-file-name (file-name-nondirectory default
)
116 ;; Handle specifying a file that is a directory.
117 (if (file-directory-p name
)
118 (expand-file-name (file-name-nondirectory default
)
119 (file-name-as-directory name
))
123 (defun find-change-log (&optional file-name
)
124 "Find a change log file for \\[add-change-log-entry] and return the name.
126 Optional arg FILE-NAME specifies the file to use.
127 If FILE-NAME is nil, use the value of `change-log-default-name'.
128 If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
129 \(or whatever we use on this operating system).
131 If 'change-log-default-name' contains a leading directory component, then
132 simply find it in the current directory. Otherwise, search in the current
133 directory and its successive parents for a file so named.
135 Once a file is found, `change-log-default-name' is set locally in the
136 current buffer to the complete file name."
137 ;; If user specified a file name or if this buffer knows which one to use,
140 (setq file-name
(and change-log-default-name
141 (file-name-directory change-log-default-name
)
142 change-log-default-name
))
144 ;; Chase links in the source file
145 ;; and use the change log in the dir where it points.
146 (setq file-name
(or (and buffer-file-name
148 (file-chase-links buffer-file-name
)))
150 (if (file-directory-p file-name
)
151 (setq file-name
(expand-file-name (change-log-name) file-name
)))
152 ;; Chase links before visiting the file.
153 ;; This makes it easier to use a single change log file
154 ;; for several related directories.
155 (setq file-name
(file-chase-links file-name
))
156 (setq file-name
(expand-file-name file-name
))
157 ;; Move up in the dir hierarchy till we find a change log file.
158 (let ((file1 file-name
)
160 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
161 (progn (setq parent-dir
164 (file-name-directory file1
))))
165 ;; Give up if we are already at the root dir.
166 (not (string= (file-name-directory file1
)
168 ;; Move up to the parent dir and try again.
169 (setq file1
(expand-file-name
170 (file-name-nondirectory (change-log-name))
172 ;; If we found a change log in a parent, use that.
173 (if (or (get-file-buffer file1
) (file-exists-p file1
))
174 (setq file-name file1
)))))
175 ;; Make a local variable in this buffer so we needn't search again.
176 (set (make-local-variable 'change-log-default-name
) file-name
)
180 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
181 "Find change log file and add an entry for today.
182 Optional arg (interactive prefix) non-nil means prompt for user name and site.
183 Second arg is file name of change log. If nil, uses `change-log-default-name'.
184 Third arg OTHER-WINDOW non-nil means visit in other window.
185 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
186 never append to an existing entry. Today's date is calculated according to
187 `change-log-time-zone-rule' if non-nil, otherwise in local time."
188 (interactive (list current-prefix-arg
189 (prompt-for-change-log-name)))
190 (or add-log-full-name
191 (setq add-log-full-name
(user-full-name)))
192 (or add-log-mailing-address
193 (setq add-log-mailing-address user-mail-address
))
196 (setq add-log-full-name
(read-input "Full name: " add-log-full-name
))
197 ;; Note that some sites have room and phone number fields in
198 ;; full name which look silly when inserted. Rather than do
199 ;; anything about that here, let user give prefix argument so that
200 ;; s/he can edit the full name field in prompter if s/he wants.
201 (setq add-log-mailing-address
202 (read-input "Mailing address: " add-log-mailing-address
))))
203 (let ((defun (funcall (or add-log-current-defun-function
204 'add-log-current-defun
)))
207 (setq file-name
(expand-file-name (find-change-log file-name
)))
209 ;; Set ENTRY to the file name to use in the new entry.
210 (and buffer-file-name
211 ;; Never want to add a change log entry for the ChangeLog file itself.
212 (not (string= buffer-file-name file-name
))
213 (setq entry
(if (string-match
214 (concat "^" (regexp-quote (file-name-directory
217 (substring buffer-file-name
(match-end 0))
218 (file-name-nondirectory buffer-file-name
))))
220 (if (and other-window
(not (equal file-name buffer-file-name
)))
221 (find-file-other-window file-name
)
222 (find-file file-name
))
223 (or (eq major-mode
'change-log-mode
)
226 (goto-char (point-min))
227 (let ((new-entry (concat (if change-log-time-zone-rule
228 (let ((tz (getenv "TZ"))
229 (now (current-time)))
233 change-log-time-zone-rule
)
235 (format-time-string "%Y-%m-%d " now
)
236 (iso8601-time-zone now
)))
237 (set-time-zone-rule tz
)))
238 (format-time-string "%Y-%m-%d"))
239 " " add-log-full-name
240 " <" add-log-mailing-address
">")))
241 (if (looking-at (regexp-quote new-entry
))
243 (insert new-entry
"\n\n")))
245 ;; Search only within the first paragraph.
246 (if (looking-at "\n*[^\n* \t]")
247 (skip-chars-forward "\n")
248 (forward-paragraph 1))
249 (setq paragraph-end
(point))
250 (goto-char (point-min))
252 ;; Now insert the new line for this entry.
253 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t
)
254 ;; Put this file name into the existing empty entry.
257 ((and (not new-entry
)
258 (let (case-fold-search)
260 (concat (regexp-quote (concat "* " entry
))
261 ;; Don't accept `foo.bar' when
262 ;; looking for `foo':
263 "\\(\\s \\|[(),:]\\)")
265 ;; Add to the existing entry for the same file.
266 (re-search-forward "^\\s *$\\|^\\s \\*")
267 (goto-char (match-beginning 0))
268 ;; Delete excess empty lines; make just 2.
269 (while (and (not (eobp)) (looking-at "^\\s *$"))
270 (delete-region (point) (save-excursion (forward-line 1) (point))))
273 (indent-relative-maybe))
277 (while (looking-at "\\sW")
279 (while (and (not (eobp)) (looking-at "^\\s *$"))
280 (delete-region (point) (save-excursion (forward-line 1) (point))))
283 (indent-to left-margin
)
284 (insert "* " (or entry
""))))
285 ;; Now insert the function name, if we have one.
286 ;; Point is at the entry for this file,
287 ;; either at the end of the line or at the first blank line.
290 ;; Make it easy to get rid of the function name.
292 (insert (if (save-excursion
293 (beginning-of-line 1)
294 (looking-at "\\s *$"))
298 ;; No function name, so put in a colon unless we have just a star.
299 (if (not (save-excursion
300 (beginning-of-line 1)
301 (looking-at "\\s *\\(\\*\\s *\\)?$")))
305 (defun add-change-log-entry-other-window (&optional whoami file-name
)
306 "Find change log file in other window and add an entry for today.
307 Optional arg (interactive prefix) non-nil means prompt for user name and site.
308 Second arg is file name of change log. \
309 If nil, uses `change-log-default-name'."
310 (interactive (if current-prefix-arg
311 (list current-prefix-arg
312 (prompt-for-change-log-name))))
313 (add-change-log-entry whoami file-name t
))
314 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
317 (defun change-log-mode ()
318 "Major mode for editing change logs; like Indented Text Mode.
319 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
320 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
321 Each entry behaves as a paragraph, and the entries for one day as a page.
322 Runs `change-log-mode-hook'."
324 (kill-all-local-variables)
326 (setq major-mode
'change-log-mode
327 mode-name
"Change Log"
332 (use-local-map change-log-mode-map
)
333 (set (make-local-variable 'fill-paragraph-function
)
334 'change-log-fill-paragraph
)
335 ;; Let each entry behave as one paragraph:
336 ;; We really do want "^" in paragraph-start below: it is only the lines that
337 ;; begin at column 0 (despite the left-margin of 8) that we are looking for.
338 (set (make-local-variable 'paragraph-start
) "\\s *$\\|\f\\|^\\<")
339 (set (make-local-variable 'paragraph-separate
) "\\s *$\\|\f\\|^\\<")
340 ;; Let all entries for one day behave as one page.
341 ;; Match null string on the date-line so that the date-line
342 ;; is grouped with what follows.
343 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
344 (set (make-local-variable 'version-control
) 'never
)
345 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
346 (set (make-local-variable 'font-lock-defaults
)
347 '(change-log-font-lock-keywords t
))
348 (run-hooks 'change-log-mode-hook
))
350 ;; It might be nice to have a general feature to replace this. The idea I
351 ;; have is a variable giving a regexp matching text which should not be
352 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
353 ;; But I don't feel up to implementing that today.
354 (defun change-log-fill-paragraph (&optional justify
)
355 "Fill the paragraph, but preserve open parentheses at beginning of lines.
356 Prefix arg means justify as well."
358 (let ((end (progn (forward-paragraph) (point)))
359 (beg (progn (backward-paragraph) (point)))
360 (paragraph-start (concat paragraph-start
"\\|\\s *\\s(")))
361 (fill-region beg end justify
)
364 (defvar add-log-current-defun-header-regexp
365 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
366 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
369 (defun add-log-current-defun ()
370 "Return name of function definition point is in, or nil.
372 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
373 Texinfo (@node titles), Perl, and Fortran.
375 Other modes are handled by a heuristic that looks in the 10K before
376 point for uppercase headings starting in the first column or
377 identifiers followed by `:' or `=', see variable
378 `add-log-current-defun-header-regexp'.
380 Has a preference of looking backwards."
383 (let ((location (point)))
384 (cond ((memq major-mode
'(emacs-lisp-mode lisp-mode scheme-mode
385 lisp-interaction-mode
))
386 ;; If we are now precisely at the beginning of a defun,
387 ;; make sure beginning-of-defun finds that one
388 ;; rather than the previous one.
389 (or (eobp) (forward-char 1))
391 ;; Make sure we are really inside the defun found, not after it.
392 (if (and (looking-at "\\s(")
393 (progn (end-of-defun)
394 (< location
(point)))
395 (progn (forward-sexp -
1)
396 (>= location
(point))))
398 (if (looking-at "\\s(")
401 (skip-chars-forward " '")
402 (buffer-substring (point)
403 (progn (forward-sexp 1) (point))))))
404 ((and (memq major-mode
'(c-mode c
++-mode c
++-c-mode objc-mode
))
405 (save-excursion (beginning-of-line)
406 ;; Use eq instead of = here to avoid
407 ;; error when at bob and char-after
409 (while (eq (char-after (- (point) 2)) ?
\\)
411 (looking-at "[ \t]*#[ \t]*define[ \t]")))
412 ;; Handle a C macro definition.
414 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
416 (search-forward "define")
417 (skip-chars-forward " \t")
418 (buffer-substring (point)
419 (progn (forward-sexp 1) (point))))
420 ((memq major-mode
'(c-mode c
++-mode c
++-c-mode objc-mode
))
422 ;; See if we are in the beginning part of a function,
423 ;; before the open brace. If so, advance forward.
424 (while (not (looking-at "{\\|\\(\\s *$\\)"))
429 (if (progn (end-of-defun)
430 (< location
(point)))
436 ;; Skip back over typedefs of arglist.
437 (while (and (not (bobp))
438 (looking-at "[ \t\n]"))
440 ;; See if this is using the DEFUN macro used in Emacs,
441 ;; or the DEFUN macro used by the C library.
442 (if (condition-case nil
445 (while (= (preceding-char) ?
\\)
450 (looking-at "DEFUN\\b"))
456 (if (= (char-after (point)) ?
\")
459 (skip-chars-forward " ,")))
460 (buffer-substring (point)
461 (progn (forward-sexp 1) (point))))
462 (if (looking-at "^[+-]")
463 (get-method-definition)
464 ;; Ordinary C function syntax.
466 (if (and (condition-case nil
467 ;; Protect against "Unbalanced parens" error.
469 (down-list 1) ; into arglist
471 (skip-chars-backward " \t")
474 ;; Verify initial pos was after
475 ;; real start of function.
478 ;; For this purpose, include the line
479 ;; that has the decl keywords. This
480 ;; may also include some of the
481 ;; comments before the function.
482 (while (and (not (bobp))
485 (looking-at "[^\n\f]")))
487 (>= location
(point)))
488 ;; Consistency check: going down and up
489 ;; shouldn't take us back before BEG.
492 ;; Don't include any final newline
493 ;; in the name we use.
494 (if (= (preceding-char) ?
\n)
498 ;; Now find the right beginning of the name.
499 ;; Include certain keywords if they
501 (setq middle
(point))
503 ;; Ignore these subparts of a class decl
504 ;; and move back to the class name itself.
505 (while (looking-at "public \\|private ")
506 (skip-chars-backward " \t:")
509 (setq middle
(point))
512 (looking-at "struct \\|union \\|class ")
513 (setq middle
(point)))
514 (buffer-substring middle end
)))))))))
516 '(TeX-mode plain-TeX-mode LaTeX-mode
;; tex-mode.el
517 plain-tex-mode latex-mode
;; cmutex.el
519 (if (re-search-backward
520 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
522 (goto-char (match-beginning 0))
523 (buffer-substring (1+ (point));; without initial backslash
527 ((eq major-mode
'texinfo-mode
)
528 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
529 (buffer-substring (match-beginning 1)
531 ((eq major-mode
'perl-mode
)
532 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t
)
533 (buffer-substring (match-beginning 1)
535 ((eq major-mode
'fortran-mode
)
536 ;; must be inside function body for this to work
537 (beginning-of-fortran-subprogram)
538 (let ((case-fold-search t
)) ; case-insensitive
539 ;; search for fortran subprogram start
540 (if (re-search-forward
541 "^[ \t]*\\(program\\|subroutine\\|function\
542 \\|[ \ta-z0-9*]*[ \t]+function\\)"
545 ;; move to EOL or before first left paren
546 (if (re-search-forward "[(\n]" nil t
)
547 (progn (forward-char -
1)
548 (skip-chars-backward " \t"))
550 ;; Use the name preceding that.
551 (buffer-substring (point)
552 (progn (forward-sexp -
1)
555 ;; If all else fails, try heuristics
556 (let (case-fold-search)
558 (if (re-search-backward add-log-current-defun-header-regexp
561 (buffer-substring (match-beginning 1)
565 (defvar get-method-definition-md
)
567 ;; Subroutine used within get-method-definition.
568 ;; Add the last match in the buffer to the end of `md',
569 ;; followed by the string END; move to the end of that match.
570 (defun get-method-definition-1 (end)
571 (setq get-method-definition-md
572 (concat get-method-definition-md
573 (buffer-substring (match-beginning 1) (match-end 1))
575 (goto-char (match-end 0)))
577 ;; For objective C, return the method name if we are in a method.
578 (defun get-method-definition ()
579 (let ((get-method-definition-md "["))
581 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
582 (get-method-definition-1 " ")))
585 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
586 (get-method-definition-1 "")
587 (while (not (looking-at "[{;]"))
589 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
590 (get-method-definition-1 ""))
591 (concat get-method-definition-md
"]"))))))
596 ;;; add-log.el ends here