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 '(("^[SMTWF].+" . 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))
60 (define-key change-log-mode-map
"\M-q" 'change-log-fill-paragraph
))
62 (defun change-log-name ()
63 (or change-log-default-name
64 (if (eq system-type
'vax-vms
)
66 (if (or (eq system-type
'ms-dos
) (eq system-type
'windows-nt
))
71 (defun prompt-for-change-log-name ()
72 "Prompt for a change log name."
73 (let* ((default (change-log-name))
74 (name (expand-file-name
75 (read-file-name (format "Log file (default %s): " default
)
77 ;; Handle something that is syntactically a directory name.
78 ;; Look for ChangeLog or whatever in that directory.
79 (if (string= (file-name-nondirectory name
) "")
80 (expand-file-name (file-name-nondirectory default
)
82 ;; Handle specifying a file that is a directory.
83 (if (file-directory-p name
)
84 (expand-file-name (file-name-nondirectory default
)
85 (file-name-as-directory name
))
89 (defun find-change-log (&optional file-name
)
90 "Find a change log file for \\[add-change-log-entry] and return the name.
92 Optional arg FILE-NAME specifies the file to use.
93 If FILE-NAME is nil, use the value of `change-log-default-name'.
94 If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
95 \(or whatever we use on this operating system).
97 If 'change-log-default-name' contains a leading directory component, then
98 simply find it in the current directory. Otherwise, search in the current
99 directory and its successive parents for a file so named.
101 Once a file is found, `change-log-default-name' is set locally in the
102 current buffer to the complete file name."
103 ;; If user specified a file name or if this buffer knows which one to use,
106 (setq file-name
(and change-log-default-name
107 (file-name-directory change-log-default-name
)
108 change-log-default-name
))
110 ;; Chase links in the source file
111 ;; and use the change log in the dir where it points.
112 (setq file-name
(or (and buffer-file-name
114 (file-chase-links buffer-file-name
)))
116 (if (file-directory-p file-name
)
117 (setq file-name
(expand-file-name (change-log-name) file-name
)))
118 ;; Chase links before visiting the file.
119 ;; This makes it easier to use a single change log file
120 ;; for several related directories.
121 (setq file-name
(file-chase-links file-name
))
122 (setq file-name
(expand-file-name file-name
))
123 ;; Move up in the dir hierarchy till we find a change log file.
124 (let ((file1 file-name
)
126 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
127 (progn (setq parent-dir
130 (file-name-directory file1
))))
131 ;; Give up if we are already at the root dir.
132 (not (string= (file-name-directory file1
)
134 ;; Move up to the parent dir and try again.
135 (setq file1
(expand-file-name
136 (file-name-nondirectory (change-log-name))
138 ;; If we found a change log in a parent, use that.
139 (if (or (get-file-buffer file1
) (file-exists-p file1
))
140 (setq file-name file1
)))))
141 ;; Make a local variable in this buffer so we needn't search again.
142 (set (make-local-variable 'change-log-default-name
) file-name
)
146 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
147 "Find change log file and add an entry for today.
148 Optional arg (interactive prefix) non-nil means prompt for user name and site.
149 Second arg is file name of change log. If nil, uses `change-log-default-name'.
150 Third arg OTHER-WINDOW non-nil means visit in other window.
151 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
152 never append to an existing entry."
153 (interactive (list current-prefix-arg
154 (prompt-for-change-log-name)))
155 (or add-log-full-name
156 (setq add-log-full-name
(user-full-name)))
157 (or add-log-mailing-address
158 (setq add-log-mailing-address user-mail-address
))
161 (setq add-log-full-name
(read-input "Full name: " add-log-full-name
))
162 ;; Note that some sites have room and phone number fields in
163 ;; full name which look silly when inserted. Rather than do
164 ;; anything about that here, let user give prefix argument so that
165 ;; s/he can edit the full name field in prompter if s/he wants.
166 (setq add-log-mailing-address
167 (read-input "Mailing address: " add-log-mailing-address
))))
168 (let ((defun (funcall (or add-log-current-defun-function
169 'add-log-current-defun
)))
172 (setq file-name
(expand-file-name (find-change-log file-name
)))
174 ;; Set ENTRY to the file name to use in the new entry.
175 (and buffer-file-name
176 ;; Never want to add a change log entry for the ChangeLog file itself.
177 (not (string= buffer-file-name file-name
))
178 (setq entry
(if (string-match
179 (concat "^" (regexp-quote (file-name-directory
182 (substring buffer-file-name
(match-end 0))
183 (file-name-nondirectory buffer-file-name
))))
185 (if (and other-window
(not (equal file-name buffer-file-name
)))
186 (find-file-other-window file-name
)
187 (find-file file-name
))
188 (or (eq major-mode
'change-log-mode
)
191 (goto-char (point-min))
192 (if (looking-at (concat (regexp-quote (substring (current-time-string)
194 ".* " (regexp-quote add-log-full-name
)
195 " <" (regexp-quote add-log-mailing-address
)))
197 (insert (current-time-string)
198 " " add-log-full-name
199 " <" add-log-mailing-address
">\n\n"))
201 ;; Search only within the first paragraph.
202 (if (looking-at "\n*[^\n* \t]")
203 (skip-chars-forward "\n")
204 (forward-paragraph 1))
205 (setq paragraph-end
(point))
206 (goto-char (point-min))
208 ;; Now insert the new line for this entry.
209 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t
)
210 ;; Put this file name into the existing empty entry.
213 ((and (not new-entry
)
214 (let (case-fold-search)
216 (concat (regexp-quote (concat "* " entry
))
217 ;; Don't accept `foo.bar' when
218 ;; looking for `foo':
219 "\\(\\s \\|[(),:]\\)")
221 ;; Add to the existing entry for the same file.
222 (re-search-forward "^\\s *$\\|^\\s \\*")
223 (goto-char (match-beginning 0))
224 ;; Delete excess empty lines; make just 2.
225 (while (and (not (eobp)) (looking-at "^\\s *$"))
226 (delete-region (point) (save-excursion (forward-line 1) (point))))
229 (indent-relative-maybe))
233 (while (looking-at "\\sW")
235 (while (and (not (eobp)) (looking-at "^\\s *$"))
236 (delete-region (point) (save-excursion (forward-line 1) (point))))
239 (indent-to left-margin
)
240 (insert "* " (or entry
""))))
241 ;; Now insert the function name, if we have one.
242 ;; Point is at the entry for this file,
243 ;; either at the end of the line or at the first blank line.
246 ;; Make it easy to get rid of the function name.
248 (insert (if (save-excursion
249 (beginning-of-line 1)
250 (looking-at "\\s *$"))
254 ;; No function name, so put in a colon unless we have just a star.
255 (if (not (save-excursion
256 (beginning-of-line 1)
257 (looking-at "\\s *\\(\\*\\s *\\)?$")))
261 (defun add-change-log-entry-other-window (&optional whoami file-name
)
262 "Find change log file in other window and add an entry for today.
263 Optional arg (interactive prefix) non-nil means prompt for user name and site.
264 Second arg is file name of change log. \
265 If nil, uses `change-log-default-name'."
266 (interactive (if current-prefix-arg
267 (list current-prefix-arg
268 (prompt-for-change-log-name))))
269 (add-change-log-entry whoami file-name t
))
270 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
273 (defun change-log-mode ()
274 "Major mode for editing change logs; like Indented Text Mode.
275 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
276 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
277 Each entry behaves as a paragraph, and the entries for one day as a page.
278 Runs `change-log-mode-hook'."
280 (kill-all-local-variables)
282 (setq major-mode
'change-log-mode
283 mode-name
"Change Log"
288 (use-local-map change-log-mode-map
)
289 ;; Let each entry behave as one paragraph:
290 ;; We really do want "^" in paragraph-start below: it is only the lines that
291 ;; begin at column 0 (despite the left-margin of 8) that we are looking for.
292 (set (make-local-variable 'paragraph-start
) "\\s *$\\|\f\\|^\\sw")
293 (set (make-local-variable 'paragraph-separate
) "\\s *$\\|\f\\|^\\sw")
294 ;; Let all entries for one day behave as one page.
295 ;; Match null string on the date-line so that the date-line
296 ;; is grouped with what follows.
297 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
298 (set (make-local-variable 'version-control
) 'never
)
299 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
300 (set (make-local-variable 'font-lock-defaults
)
301 '(change-log-font-lock-keywords t
))
302 (run-hooks 'change-log-mode-hook
))
304 ;; It might be nice to have a general feature to replace this. The idea I
305 ;; have is a variable giving a regexp matching text which should not be
306 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
307 ;; But I don't feel up to implementing that today.
308 (defun change-log-fill-paragraph (&optional justify
)
309 "Fill the paragraph, but preserve open parentheses at beginning of lines.
310 Prefix arg means justify as well."
312 (let ((end (save-excursion (forward-paragraph) (point)))
313 (beg (save-excursion (backward-paragraph)(point)))
314 (paragraph-start (concat paragraph-start
"\\|\\s *\\s(")))
315 (fill-region beg end justify
)))
317 (defvar add-log-current-defun-header-regexp
318 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
319 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
322 (defun add-log-current-defun ()
323 "Return name of function definition point is in, or nil.
325 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
326 Texinfo (@node titles), Perl, and Fortran.
328 Other modes are handled by a heuristic that looks in the 10K before
329 point for uppercase headings starting in the first column or
330 identifiers followed by `:' or `=', see variable
331 `add-log-current-defun-header-regexp'.
333 Has a preference of looking backwards."
336 (let ((location (point)))
337 (cond ((memq major-mode
'(emacs-lisp-mode lisp-mode scheme-mode
338 lisp-interaction-mode
))
339 ;; If we are now precisely at the beginning of a defun,
340 ;; make sure beginning-of-defun finds that one
341 ;; rather than the previous one.
342 (or (eobp) (forward-char 1))
344 ;; Make sure we are really inside the defun found, not after it.
345 (if (and (looking-at "\\s(")
346 (progn (end-of-defun)
347 (< location
(point)))
348 (progn (forward-sexp -
1)
349 (>= location
(point))))
351 (if (looking-at "\\s(")
354 (skip-chars-forward " '")
355 (buffer-substring (point)
356 (progn (forward-sexp 1) (point))))))
357 ((and (memq major-mode
'(c-mode c
++-mode c
++-c-mode objc-mode
))
358 (save-excursion (beginning-of-line)
359 ;; Use eq instead of = here to avoid
360 ;; error when at bob and char-after
362 (while (eq (char-after (- (point) 2)) ?
\\)
364 (looking-at "[ \t]*#[ \t]*define[ \t]")))
365 ;; Handle a C macro definition.
367 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
369 (search-forward "define")
370 (skip-chars-forward " \t")
371 (buffer-substring (point)
372 (progn (forward-sexp 1) (point))))
373 ((memq major-mode
'(c-mode c
++-mode c
++-c-mode objc-mode
))
375 ;; See if we are in the beginning part of a function,
376 ;; before the open brace. If so, advance forward.
377 (while (not (looking-at "{\\|\\(\\s *$\\)"))
382 (if (progn (end-of-defun)
383 (< location
(point)))
389 ;; Skip back over typedefs of arglist.
390 (while (and (not (bobp))
391 (looking-at "[ \t\n]"))
393 ;; See if this is using the DEFUN macro used in Emacs,
394 ;; or the DEFUN macro used by the C library.
395 (if (condition-case nil
398 (while (= (preceding-char) ?
\\)
403 (looking-at "DEFUN\\b"))
409 (if (= (char-after (point)) ?
\")
412 (skip-chars-forward " ,")))
413 (buffer-substring (point)
414 (progn (forward-sexp 1) (point))))
415 (if (looking-at "^[+-]")
416 (get-method-definition)
417 ;; Ordinary C function syntax.
419 (if (and (condition-case nil
420 ;; Protect against "Unbalanced parens" error.
422 (down-list 1) ; into arglist
424 (skip-chars-backward " \t")
427 ;; Verify initial pos was after
428 ;; real start of function.
431 ;; For this purpose, include the line
432 ;; that has the decl keywords. This
433 ;; may also include some of the
434 ;; comments before the function.
435 (while (and (not (bobp))
438 (looking-at "[^\n\f]")))
440 (>= location
(point)))
441 ;; Consistency check: going down and up
442 ;; shouldn't take us back before BEG.
445 ;; Don't include any final newline
446 ;; in the name we use.
447 (if (= (preceding-char) ?
\n)
451 ;; Now find the right beginning of the name.
452 ;; Include certain keywords if they
454 (setq middle
(point))
456 ;; Ignore these subparts of a class decl
457 ;; and move back to the class name itself.
458 (while (looking-at "public \\|private ")
459 (skip-chars-backward " \t:")
462 (setq middle
(point))
465 (looking-at "struct \\|union \\|class ")
466 (setq middle
(point)))
467 (buffer-substring middle end
)))))))))
469 '(TeX-mode plain-TeX-mode LaTeX-mode
;; tex-mode.el
470 plain-tex-mode latex-mode
;; cmutex.el
472 (if (re-search-backward
473 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
475 (goto-char (match-beginning 0))
476 (buffer-substring (1+ (point));; without initial backslash
480 ((eq major-mode
'texinfo-mode
)
481 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
482 (buffer-substring (match-beginning 1)
484 ((eq major-mode
'perl-mode
)
485 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t
)
486 (buffer-substring (match-beginning 1)
488 ((eq major-mode
'fortran-mode
)
489 ;; must be inside function body for this to work
490 (beginning-of-fortran-subprogram)
491 (let ((case-fold-search t
)) ; case-insensitive
492 ;; search for fortran subprogram start
493 (if (re-search-forward
494 "^[ \t]*\\(program\\|subroutine\\|function\
495 \\|[ \ta-z0-9*]*[ \t]+function\\)"
498 ;; move to EOL or before first left paren
499 (if (re-search-forward "[(\n]" nil t
)
500 (progn (forward-char -
1)
501 (skip-chars-backward " \t"))
503 ;; Use the name preceding that.
504 (buffer-substring (point)
505 (progn (forward-sexp -
1)
508 ;; If all else fails, try heuristics
509 (let (case-fold-search)
511 (if (re-search-backward add-log-current-defun-header-regexp
514 (buffer-substring (match-beginning 1)
518 (defvar get-method-definition-md
)
520 ;; Subroutine used within get-method-definition.
521 ;; Add the last match in the buffer to the end of `md',
522 ;; followed by the string END; move to the end of that match.
523 (defun get-method-definition-1 (end)
524 (setq get-method-definition-md
525 (concat get-method-definition-md
526 (buffer-substring (match-beginning 1) (match-end 1))
528 (goto-char (match-end 0)))
530 ;; For objective C, return the method name if we are in a method.
531 (defun get-method-definition ()
532 (let ((get-method-definition-md "["))
534 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
535 (get-method-definition-1 " ")))
538 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
539 (get-method-definition-1 "")
540 (while (not (looking-at "[{;]"))
542 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
543 (get-method-definition-1 ""))
544 (concat get-method-definition-md
"]"))))))
549 ;;; add-log.el ends here