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.")
39 (defvar add-log-full-name nil
40 "*Full name of user, for inclusion in ChangeLog daily headers.
41 This defaults to the value returned by the `user-full-name' function.")
43 (defvar add-log-mailing-address nil
44 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
45 This defaults to the value of `user-mail-address'.")
47 (defvar change-log-font-lock-keywords
48 '(("^[SMTWF].+" . font-lock-function-name-face
) ; Date line.
49 ("^\t\\* \\([^ :\n]+\\)" 1 font-lock-comment-face
) ; File name.
50 ("\(\\([^)\n]+\\)\)" 1 font-lock-keyword-face
)) ; Function name.
51 "Additional expressions to highlight in Change Log mode.")
53 (defvar change-log-mode-map nil
54 "Keymap for Change Log major mode.")
55 (if change-log-mode-map
57 (setq change-log-mode-map
(make-sparse-keymap))
58 (define-key change-log-mode-map
"\M-q" 'change-log-fill-paragraph
))
60 (defun change-log-name ()
61 (or change-log-default-name
62 (if (eq system-type
'vax-vms
)
64 (if (or (eq system-type
'ms-dos
) (eq system-type
'windows-nt
))
69 (defun prompt-for-change-log-name ()
70 "Prompt for a change log name."
71 (let* ((default (change-log-name))
72 (name (expand-file-name
73 (read-file-name (format "Log file (default %s): " default
)
75 ;; Handle something that is syntactically a directory name.
76 ;; Look for ChangeLog or whatever in that directory.
77 (if (string= (file-name-nondirectory name
) "")
78 (expand-file-name (file-name-nondirectory default
)
80 ;; Handle specifying a file that is a directory.
81 (if (file-directory-p name
)
82 (expand-file-name (file-name-nondirectory default
)
83 (file-name-as-directory name
))
87 (defun find-change-log (&optional file-name
)
88 "Find a change log file for \\[add-change-log-entry] and return the name.
90 Optional arg FILE-NAME specifies the file to use.
91 If FILE-NAME is nil, use the value of `change-log-default-name'.
92 If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
93 \(or whatever we use on this operating system).
95 If 'change-log-default-name' contains a leading directory component, then
96 simply find it in the current directory. Otherwise, search in the current
97 directory and its successive parents for a file so named.
99 Once a file is found, `change-log-default-name' is set locally in the
100 current buffer to the complete file name."
101 ;; If user specified a file name or if this buffer knows which one to use,
104 (setq file-name
(and change-log-default-name
105 (file-name-directory change-log-default-name
)
106 change-log-default-name
))
108 ;; Chase links in the source file
109 ;; and use the change log in the dir where it points.
110 (setq file-name
(or (and buffer-file-name
112 (file-chase-links buffer-file-name
)))
114 (if (file-directory-p file-name
)
115 (setq file-name
(expand-file-name (change-log-name) file-name
)))
116 ;; Chase links before visiting the file.
117 ;; This makes it easier to use a single change log file
118 ;; for several related directories.
119 (setq file-name
(file-chase-links file-name
))
120 (setq file-name
(expand-file-name file-name
))
121 ;; Move up in the dir hierarchy till we find a change log file.
122 (let ((file1 file-name
)
124 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
125 (progn (setq parent-dir
128 (file-name-directory file1
))))
129 ;; Give up if we are already at the root dir.
130 (not (string= (file-name-directory file1
)
132 ;; Move up to the parent dir and try again.
133 (setq file1
(expand-file-name
134 (file-name-nondirectory (change-log-name))
136 ;; If we found a change log in a parent, use that.
137 (if (or (get-file-buffer file1
) (file-exists-p file1
))
138 (setq file-name file1
)))))
139 ;; Make a local variable in this buffer so we needn't search again.
140 (set (make-local-variable 'change-log-default-name
) file-name
)
144 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
145 "Find change log file and add an entry for today.
146 Optional arg (interactive prefix) non-nil means prompt for user name and site.
147 Second arg is file name of change log. If nil, uses `change-log-default-name'.
148 Third arg OTHER-WINDOW non-nil means visit in other window.
149 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
150 never append to an existing entry."
151 (interactive (list current-prefix-arg
152 (prompt-for-change-log-name)))
153 (or add-log-full-name
154 (setq add-log-full-name
(user-full-name)))
155 (or add-log-mailing-address
156 (setq add-log-mailing-address user-mail-address
))
159 (setq add-log-full-name
(read-input "Full name: " add-log-full-name
))
160 ;; Note that some sites have room and phone number fields in
161 ;; full name which look silly when inserted. Rather than do
162 ;; anything about that here, let user give prefix argument so that
163 ;; s/he can edit the full name field in prompter if s/he wants.
164 (setq add-log-mailing-address
165 (read-input "Mailing address: " add-log-mailing-address
))))
166 (let ((defun (funcall (or add-log-current-defun-function
167 'add-log-current-defun
)))
170 (setq file-name
(expand-file-name (find-change-log file-name
)))
172 ;; Set ENTRY to the file name to use in the new entry.
173 (and buffer-file-name
174 ;; Never want to add a change log entry for the ChangeLog file itself.
175 (not (string= buffer-file-name file-name
))
176 (setq entry
(if (string-match
177 (concat "^" (regexp-quote (file-name-directory
180 (substring buffer-file-name
(match-end 0))
181 (file-name-nondirectory buffer-file-name
))))
183 (if (and other-window
(not (equal file-name buffer-file-name
)))
184 (find-file-other-window file-name
)
185 (find-file file-name
))
186 (or (eq major-mode
'change-log-mode
)
189 (goto-char (point-min))
190 (if (looking-at (concat (regexp-quote (substring (current-time-string)
192 ".* " (regexp-quote add-log-full-name
)
193 " <" (regexp-quote add-log-mailing-address
)))
195 (insert (current-time-string)
196 " " add-log-full-name
197 " <" add-log-mailing-address
">\n\n"))
199 ;; Search only within the first paragraph.
200 (if (looking-at "\n*[^\n* \t]")
201 (skip-chars-forward "\n")
202 (forward-paragraph 1))
203 (setq paragraph-end
(point))
204 (goto-char (point-min))
206 ;; Now insert the new line for this entry.
207 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t
)
208 ;; Put this file name into the existing empty entry.
211 ((and (not new-entry
)
212 (let (case-fold-search)
214 (concat (regexp-quote (concat "* " entry
))
215 ;; Don't accept `foo.bar' when
216 ;; looking for `foo':
217 "\\(\\s \\|[(),:]\\)")
219 ;; Add to the existing entry for the same file.
220 (re-search-forward "^\\s *$\\|^\\s \\*")
221 (goto-char (match-beginning 0))
222 ;; Delete excess empty lines; make just 2.
223 (while (and (not (eobp)) (looking-at "^\\s *$"))
224 (delete-region (point) (save-excursion (forward-line 1) (point))))
227 (indent-relative-maybe))
231 (while (looking-at "\\sW")
233 (while (and (not (eobp)) (looking-at "^\\s *$"))
234 (delete-region (point) (save-excursion (forward-line 1) (point))))
237 (indent-to left-margin
)
238 (insert "* " (or entry
""))))
239 ;; Now insert the function name, if we have one.
240 ;; Point is at the entry for this file,
241 ;; either at the end of the line or at the first blank line.
244 ;; Make it easy to get rid of the function name.
246 (insert (if (save-excursion
247 (beginning-of-line 1)
248 (looking-at "\\s *$"))
252 ;; No function name, so put in a colon unless we have just a star.
253 (if (not (save-excursion
254 (beginning-of-line 1)
255 (looking-at "\\s *\\(\\*\\s *\\)?$")))
259 (defun add-change-log-entry-other-window (&optional whoami file-name
)
260 "Find change log file in other window and add an entry for today.
261 Optional arg (interactive prefix) non-nil means prompt for user name and site.
262 Second arg is file name of change log. \
263 If nil, uses `change-log-default-name'."
264 (interactive (if current-prefix-arg
265 (list current-prefix-arg
266 (prompt-for-change-log-name))))
267 (add-change-log-entry whoami file-name t
))
268 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
271 (defun change-log-mode ()
272 "Major mode for editing change logs; like Indented Text Mode.
273 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
274 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
275 Each entry behaves as a paragraph, and the entries for one day as a page.
276 Runs `change-log-mode-hook'."
278 (kill-all-local-variables)
280 (setq major-mode
'change-log-mode
281 mode-name
"Change Log"
286 (use-local-map change-log-mode-map
)
287 ;; Let each entry behave as one paragraph:
288 ;; We really do want "^" in paragraph-start below: it is only the lines that
289 ;; begin at column 0 (despite the left-margin of 8) that we are looking for.
290 (set (make-local-variable 'paragraph-start
) "\\s *$\\|\f\\|^\\sw")
291 (set (make-local-variable 'paragraph-separate
) "\\s *$\\|\f\\|^\\sw")
292 ;; Let all entries for one day behave as one page.
293 ;; Match null string on the date-line so that the date-line
294 ;; is grouped with what follows.
295 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
296 (set (make-local-variable 'version-control
) 'never
)
297 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
298 (set (make-local-variable 'font-lock-defaults
)
299 '(change-log-font-lock-keywords t
))
300 (run-hooks 'change-log-mode-hook
))
302 ;; It might be nice to have a general feature to replace this. The idea I
303 ;; have is a variable giving a regexp matching text which should not be
304 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
305 ;; But I don't feel up to implementing that today.
306 (defun change-log-fill-paragraph (&optional justify
)
307 "Fill the paragraph, but preserve open parentheses at beginning of lines.
308 Prefix arg means justify as well."
310 (let ((end (save-excursion (forward-paragraph) (point)))
311 (beg (save-excursion (backward-paragraph)(point)))
312 (paragraph-start (concat paragraph-start
"\\|\\s *\\s(")))
313 (fill-region beg end justify
)))
315 (defvar add-log-current-defun-header-regexp
316 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
317 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
320 (defun add-log-current-defun ()
321 "Return name of function definition point is in, or nil.
323 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
324 Texinfo (@node titles), Perl, and Fortran.
326 Other modes are handled by a heuristic that looks in the 10K before
327 point for uppercase headings starting in the first column or
328 identifiers followed by `:' or `=', see variable
329 `add-log-current-defun-header-regexp'.
331 Has a preference of looking backwards."
334 (let ((location (point)))
335 (cond ((memq major-mode
'(emacs-lisp-mode lisp-mode scheme-mode
336 lisp-interaction-mode
))
337 ;; If we are now precisely at the beginning of a defun,
338 ;; make sure beginning-of-defun finds that one
339 ;; rather than the previous one.
340 (or (eobp) (forward-char 1))
342 ;; Make sure we are really inside the defun found, not after it.
343 (if (and (looking-at "\\s(")
344 (progn (end-of-defun)
345 (< location
(point)))
346 (progn (forward-sexp -
1)
347 (>= location
(point))))
349 (if (looking-at "\\s(")
352 (skip-chars-forward " '")
353 (buffer-substring (point)
354 (progn (forward-sexp 1) (point))))))
355 ((and (memq major-mode
'(c-mode c
++-mode c
++-c-mode objc-mode
))
356 (save-excursion (beginning-of-line)
357 ;; Use eq instead of = here to avoid
358 ;; error when at bob and char-after
360 (while (eq (char-after (- (point) 2)) ?
\\)
362 (looking-at "[ \t]*#[ \t]*define[ \t]")))
363 ;; Handle a C macro definition.
365 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
367 (search-forward "define")
368 (skip-chars-forward " \t")
369 (buffer-substring (point)
370 (progn (forward-sexp 1) (point))))
371 ((memq major-mode
'(c-mode c
++-mode c
++-c-mode objc-mode
))
373 ;; See if we are in the beginning part of a function,
374 ;; before the open brace. If so, advance forward.
375 (while (not (looking-at "{\\|\\(\\s *$\\)"))
380 (if (progn (end-of-defun)
381 (< location
(point)))
387 ;; Skip back over typedefs of arglist.
388 (while (and (not (bobp))
389 (looking-at "[ \t\n]"))
391 ;; See if this is using the DEFUN macro used in Emacs,
392 ;; or the DEFUN macro used by the C library.
393 (if (condition-case nil
396 (while (= (preceding-char) ?
\\)
401 (looking-at "DEFUN\\b"))
407 (if (= (char-after (point)) ?
\")
410 (skip-chars-forward " ,")))
411 (buffer-substring (point)
412 (progn (forward-sexp 1) (point))))
413 (if (looking-at "^[+-]")
414 (get-method-definition)
415 ;; Ordinary C function syntax.
417 (if (and (condition-case nil
418 ;; Protect against "Unbalanced parens" error.
420 (down-list 1) ; into arglist
422 (skip-chars-backward " \t")
425 ;; Verify initial pos was after
426 ;; real start of function.
429 ;; For this purpose, include the line
430 ;; that has the decl keywords. This
431 ;; may also include some of the
432 ;; comments before the function.
433 (while (and (not (bobp))
436 (looking-at "[^\n\f]")))
438 (>= location
(point)))
439 ;; Consistency check: going down and up
440 ;; shouldn't take us back before BEG.
443 ;; Don't include any final newline
444 ;; in the name we use.
445 (if (= (preceding-char) ?
\n)
449 ;; Now find the right beginning of the name.
450 ;; Include certain keywords if they
452 (setq middle
(point))
454 ;; Ignore these subparts of a class decl
455 ;; and move back to the class name itself.
456 (while (looking-at "public \\|private ")
457 (skip-chars-backward " \t:")
460 (setq middle
(point))
463 (looking-at "struct \\|union \\|class ")
464 (setq middle
(point)))
465 (buffer-substring middle end
)))))))))
467 '(TeX-mode plain-TeX-mode LaTeX-mode
;; tex-mode.el
468 plain-tex-mode latex-mode
;; cmutex.el
470 (if (re-search-backward
471 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
473 (goto-char (match-beginning 0))
474 (buffer-substring (1+ (point));; without initial backslash
478 ((eq major-mode
'texinfo-mode
)
479 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
480 (buffer-substring (match-beginning 1)
482 ((eq major-mode
'perl-mode
)
483 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t
)
484 (buffer-substring (match-beginning 1)
486 ((eq major-mode
'fortran-mode
)
487 ;; must be inside function body for this to work
488 (beginning-of-fortran-subprogram)
489 (let ((case-fold-search t
)) ; case-insensitive
490 ;; search for fortran subprogram start
491 (if (re-search-forward
492 "^[ \t]*\\(program\\|subroutine\\|function\
493 \\|[ \ta-z0-9*]*[ \t]+function\\)"
496 ;; move to EOL or before first left paren
497 (if (re-search-forward "[(\n]" nil t
)
498 (progn (forward-char -
1)
499 (skip-chars-backward " \t"))
501 ;; Use the name preceding that.
502 (buffer-substring (point)
503 (progn (forward-sexp -
1)
506 ;; If all else fails, try heuristics
507 (let (case-fold-search)
509 (if (re-search-backward add-log-current-defun-header-regexp
512 (buffer-substring (match-beginning 1)
516 (defvar get-method-definition-md
)
518 ;; Subroutine used within get-method-definition.
519 ;; Add the last match in the buffer to the end of `md',
520 ;; followed by the string END; move to the end of that match.
521 (defun get-method-definition-1 (end)
522 (setq get-method-definition-md
523 (concat get-method-definition-md
524 (buffer-substring (match-beginning 1) (match-end 1))
526 (goto-char (match-end 0)))
528 ;; For objective C, return the method name if we are in a method.
529 (defun get-method-definition ()
530 (let ((get-method-definition-md "["))
532 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
533 (get-method-definition-1 " ")))
536 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
537 (get-method-definition-1 "")
538 (while (not (looking-at "[{;]"))
540 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
541 (get-method-definition-1 ""))
542 (concat get-method-definition-md
"]"))))))
547 ;;; add-log.el ends here