1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1988, 1993 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
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;; 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].")
34 (defvar add-log-current-defun-function nil
36 *If non-nil, function to guess name of current function from surrounding text.
37 \\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
38 instead) with no arguments. It returns a string or nil if it cannot guess.")
40 (defun change-log-name ()
41 (or change-log-default-name
42 (if (eq system-type
'vax-vms
) "$CHANGE_LOG$.TXT" "ChangeLog")))
44 (defun prompt-for-change-log-name ()
45 "Prompt for a change log name."
46 (let ((default (change-log-name)))
48 (read-file-name (format "Log file (default %s): " default
)
52 (defun find-change-log (&optional file-name
)
53 "Find a change log file for \\[add-change-log-entry] and return the name.
54 Optional arg FILE-NAME is a name to try first.
55 If FILE-NAME is nil, use the value of `change-log-default-name' if non-nil.
56 Failing that, use \"ChangeLog\" in the current directory.
57 If the file does not exist in the named directory, successive parent
58 directories are tried.
60 Once a file is found, `change-log-default-name' is set locally in the
61 current buffer to the complete file name."
63 (setq file-name
(or change-log-default-name
64 ;; Chase links in the source file
65 ;; and use the change log in the dir where it points.
68 (file-chase-links buffer-file-name
)))
70 (if (and (eq file-name change-log-default-name
)
71 (assq 'change-log-default-name
(buffer-local-variables)))
72 ;; Don't do the searching if we already have a buffer-local value.
75 (if (file-directory-p file-name
)
76 (setq file-name
(expand-file-name (change-log-name) file-name
)))
77 ;; Chase links before visiting the file.
78 ;; This makes it easier to use a single change log file
79 ;; for several related directories.
80 (setq file-name
(file-chase-links file-name
))
81 (setq file-name
(expand-file-name file-name
))
82 ;; Move up in the dir hierarchy till we find a change log file.
83 (let ((file1 file-name
)
85 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
86 (progn (setq parent-dir
89 (file-name-directory file1
))))
90 ;; Give up if we are already at the root dir.
91 (not (string= (file-name-directory file1
)
93 ;; Move up to the parent dir and try again.
94 (setq file1
(expand-file-name (change-log-name) parent-dir
)))
95 ;; If we found a change log in a parent, use that.
96 (if (or (get-file-buffer file1
) (file-exists-p file1
))
97 (setq file-name file1
)))
98 ;; Make a local variable in this buffer so we needn't search again.
99 (set (make-local-variable 'change-log-default-name
) file-name
)
103 (defun add-change-log-entry (&optional whoami file-name other-window
)
104 "Find change log file and add an entry for today.
105 Optional arg (interactive prefix) non-nil means prompt for user name and site.
106 Second arg is file name of change log. If nil, uses `change-log-default-name'.
107 Third arg OTHER-WINDOW non-nil means visit in other window."
108 (interactive (list current-prefix-arg
109 (prompt-for-change-log-name)))
110 (let* ((full-name (if whoami
111 (read-input "Full name: " (user-full-name))
113 ;; Note that some sites have room and phone number fields in
114 ;; full name which look silly when inserted. Rather than do
115 ;; anything about that here, let user give prefix argument so that
116 ;; s/he can edit the full name field in prompter if s/he wants.
117 (login-name (if whoami
118 (read-input "Login name: " (user-login-name))
120 (site-name (if whoami
121 (read-input "Site name: " (system-name))
123 (defun (funcall (or add-log-current-defun-function
124 'add-log-current-defun
)))
127 (setq file-name
(find-change-log file-name
))
129 ;; Set ENTRY to the file name to use in the new entry.
130 (and buffer-file-name
131 ;; Never want to add a change log entry for the ChangeLog file itself.
132 (not (string= buffer-file-name file-name
))
133 (setq entry
(if (string-match
134 (concat "^" (regexp-quote (file-name-directory
137 (substring buffer-file-name
(match-end 0))
138 (file-name-nondirectory buffer-file-name
))))
140 (if (and other-window
(not (equal file-name buffer-file-name
)))
141 (find-file-other-window file-name
)
142 (find-file file-name
))
144 (goto-char (point-min))
145 (if (looking-at (concat (regexp-quote (substring (current-time-string)
147 ".* " (regexp-quote full-name
)
148 " (" (regexp-quote login-name
) "@"))
150 (insert (current-time-string)
152 " (" login-name
"@" site-name
")\n\n"))
154 ;; Search only within the first paragraph.
155 (if (looking-at "\n*[^\n* \t]")
156 (skip-chars-forward "\n")
157 (forward-paragraph 1))
158 (setq paragraph-end
(point))
159 (goto-char (point-min))
161 ;; Now insert the new line for this entry.
162 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t
)
163 ;; Put this file name into the existing empty entry.
166 ((and (re-search-forward
167 (concat (regexp-quote (concat "* " entry
))
168 ;; Don't accept `foo.bar' when
169 ;; looking for `foo':
170 "\\(\\s \\|[(),:]\\)")
172 ;; Add to the existing entry for the same file.
173 (re-search-forward "^\\s *$\\|^\\s \\*")
175 (while (and (not (eobp)) (looking-at "^\\s *$"))
176 (delete-region (point) (save-excursion (forward-line 1) (point))))
179 (indent-relative-maybe))
183 (while (looking-at "\\sW")
185 (while (and (not (eobp)) (looking-at "^\\s *$"))
186 (delete-region (point) (save-excursion (forward-line 1) (point))))
189 (indent-to left-margin
)
190 (insert "* " (or entry
""))))
191 ;; Now insert the function name, if we have one.
192 ;; Point is at the entry for this file,
193 ;; either at the end of the line or at the first blank line.
196 ;; Make it easy to get rid of the function name.
198 (insert (if (save-excursion
199 (beginning-of-line 1)
200 (looking-at "\\s *$"))
204 ;; No function name, so put in a colon unless we have just a star.
205 (if (not (save-excursion
206 (beginning-of-line 1)
207 (looking-at "\\s *\\(\\*\\s *\\)?$")))
211 (defun add-change-log-entry-other-window (&optional whoami file-name
)
212 "Find change log file in other window and add an entry for today.
213 Optional arg (interactive prefix) non-nil means prompt for user name and site.
214 Second arg is file name of change log. \
215 If nil, uses `change-log-default-name'."
216 (interactive (if current-prefix-arg
217 (list current-prefix-arg
218 (prompt-for-change-log-name))))
219 (add-change-log-entry whoami file-name t
))
220 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
223 (defun change-log-mode ()
224 "Major mode for editing change logs; like Indented Text Mode.
225 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
226 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
227 Each entry behaves as a paragraph, and the entries for one day as a page.
228 Runs `change-log-mode-hook'."
230 (kill-all-local-variables)
232 (setq major-mode
'change-log-mode
233 mode-name
"Change Log"
236 ;; Let each entry behave as one paragraph:
237 (set (make-local-variable 'paragraph-start
) "^\\s *$\\|^^L")
238 (set (make-local-variable 'paragraph-separate
) "^\\s *$\\|^^L\\|^\\sw")
239 ;; Let all entries for one day behave as one page.
240 ;; Match null string on the date-line so that the date-line
241 ;; is grouped with what follows.
242 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
243 (set (make-local-variable 'version-control
) 'never
)
244 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
245 (run-hooks 'change-log-mode-hook
))
247 (defvar add-log-current-defun-header-regexp
248 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[a-z_---A-Z]+\\)[ \t]*[:=]"
249 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
251 (defun add-log-current-defun ()
252 "Return name of function definition point is in, or nil.
254 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
255 Texinfo (@node titles), and Fortran.
257 Other modes are handled by a heuristic that looks in the 10K before
258 point for uppercase headings starting in the first column or
259 identifiers followed by `:' or `=', see variable
260 `add-log-current-defun-header-regexp'.
262 Has a preference of looking backwards."
265 (let ((location (point)))
266 (cond ((memq major-mode
'(emacs-lisp-mode lisp-mode scheme-mode
))
267 ;; If we are now precisely a the beginning of a defun,
268 ;; make sure beginning-of-defun finds that one
269 ;; rather than the previous one.
270 (or (eobp) (forward-char 1))
272 ;; Make sure we are really inside the defun found, not after it.
273 (if (and (progn (end-of-defun)
274 (< location
(point)))
275 (progn (forward-sexp -
1)
276 (>= location
(point))))
278 (if (looking-at "\\s(")
281 (skip-chars-forward " ")
282 (buffer-substring (point)
283 (progn (forward-sexp 1) (point))))))
284 ((and (memq major-mode
'(c-mode 'c
++-mode
))
285 (save-excursion (beginning-of-line)
286 ;; Use eq instead of = here to avoid
287 ;; error when at bob and char-after
289 (while (eq (char-after (- (point) 2)) ?
\\)
291 (looking-at "[ \t]*#[ \t]*define[ \t]")))
292 ;; Handle a C macro definition.
294 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
296 (search-forward "define")
297 (skip-chars-forward " \t")
298 (buffer-substring (point)
299 (progn (forward-sexp 1) (point))))
300 ((memq major-mode
'(c-mode 'c
++-mode
))
302 ;; See if we are in the beginning part of a function,
303 ;; before the open brace. If so, advance forward.
304 (while (not (looking-at "{\\|\\(\\s *$\\)"))
309 (if (progn (end-of-defun)
310 (< location
(point)))
316 ;; Skip back over typedefs of arglist.
317 (while (and (not (bobp))
318 (looking-at "[ \t\n]"))
320 ;; See if this is using the DEFUN macro used in Emacs,
321 ;; or the DEFUN macro used by the C library.
322 (if (condition-case nil
328 (looking-at "DEFUN\\b"))
334 (if (= (char-after (point)) ?
\")
337 (skip-chars-forward " ,")))
338 (buffer-substring (point)
339 (progn (forward-sexp 1) (point))))
340 ;; Ordinary C function syntax.
342 (if (condition-case nil
343 ;; Protect against "Unbalanced parens" error.
345 (down-list 1) ; into arglist
347 (skip-chars-backward " \t")
350 ;; Verify initial pos was after
351 ;; real start of function.
352 (if (and (save-excursion
354 ;; For this purpose, include the line
355 ;; that has the decl keywords. This
356 ;; may also include some of the
357 ;; comments before the function.
358 (while (and (not (bobp))
361 (looking-at "[^\n\f]")))
363 (>= location
(point)))
364 ;; Consistency check: going down and up
365 ;; shouldn't take us back before BEG.
367 (buffer-substring (point)
368 (progn (backward-sexp 1)
371 '(TeX-mode plain-TeX-mode LaTeX-mode
;; tex-mode.el
372 plain-tex-mode latex-mode
;; cmutex.el
374 (if (re-search-backward
375 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
377 (goto-char (match-beginning 0))
378 (buffer-substring (1+ (point));; without initial backslash
382 ((eq major-mode
'texinfo-mode
)
383 (if (re-search-backward "^@node[ \t]+\\([^,]+\\)," nil t
)
384 (buffer-substring (match-beginning 1)
386 ((eq major-mode
'fortran-mode
)
387 ;; must be inside function body for this to work
388 (beginning-of-fortran-subprogram)
389 (let ((case-fold-search t
)) ; case-insensitive
390 ;; search for fortran subprogram start
391 (if (re-search-forward
392 "^[ \t]*\\(program\\|subroutine\\|function\
393 \\|[ \ta-z0-9*]*[ \t]+function\\)"
396 ;; move to EOL or before first left paren
397 (if (re-search-forward "[(\n]" nil t
)
398 (progn (forward-char -
1)
399 (skip-chars-backward " \t"))
401 ;; Use the name preceding that.
402 (buffer-substring (point)
403 (progn (forward-sexp -
1)
406 ;; If all else fails, try heuristics
407 (let (case-fold-search)
409 (if (re-search-backward add-log-current-defun-header-regexp
412 (buffer-substring (match-beginning 1)
419 ;;; add-log.el ends here