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].")
33 (defun change-log-name ()
34 (or change-log-default-name
35 (if (eq system-type
'vax-vms
) "$CHANGE_LOG$.TXT" "ChangeLog")))
37 (defun prompt-for-change-log-name ()
38 "Prompt for a change log name."
39 (let ((default (change-log-name)))
41 (read-file-name (format "Log file (default %s): " default
)
45 (defun find-change-log (&optional file-name
)
46 "Find a change log file for \\[add-change-log-entry] and return the name.
47 Optional arg FILE-NAME is a name to try first.
48 If FILE-NAME is nil, use the value of `change-log-default-name' if non-nil.
49 Failing that, use \"ChangeLog\" in the current directory.
50 If the file does not exist in the named directory, successive parent
51 directories are tried.
53 Once a file is found, `change-log-default-name' is set locally in the
54 current buffer to the complete file name."
56 (setq file-name
(or change-log-default-name
57 ;; Chase links in the source file
58 ;; and use the change log in the dir where it points.
61 (file-chase-links buffer-file-name
)))
63 (if (and (eq file-name change-log-default-name
)
64 (assq 'change-log-default-name
(buffer-local-variables)))
65 ;; Don't do the searching if we already have a buffer-local value.
68 (if (file-directory-p file-name
)
69 (setq file-name
(expand-file-name (change-log-name) file-name
)))
70 ;; Chase links before visiting the file.
71 ;; This makes it easier to use a single change log file
72 ;; for several related directories.
73 (setq file-name
(file-chase-links file-name
))
74 (setq file-name
(expand-file-name file-name
))
75 ;; Move up in the dir hierarchy till we find a change log file.
76 (let ((file1 file-name
)
78 (while (and (not (file-exists-p file1
))
79 (progn (setq parent-dir
82 (file-name-directory file1
))))
83 ;; Give up if we are already at the root dir.
84 (not (string= (file-name-directory file1
)
86 ;; Move up to the parent dir and try again.
87 (setq file1
(expand-file-name (change-log-name) parent-dir
)))
88 ;; If we found a change log in a parent, use that.
89 (if (file-exists-p file1
)
90 (setq file-name file1
)))
91 ;; Make a local variable in this buffer so we needn't search again.
92 (set (make-local-variable 'change-log-default-name
) file-name
)
96 (defun add-change-log-entry (&optional whoami file-name other-window
)
97 "Find change log file and add an entry for today.
98 Optional arg (interactive prefix) non-nil means prompt for user name and site.
99 Second arg is file name of change log. If nil, uses `change-log-default-name'.
100 Third arg OTHER-WINDOW non-nil means visit in other window."
101 (interactive (list current-prefix-arg
102 (prompt-for-change-log-name)))
103 (let* ((full-name (if whoami
104 (read-input "Full name: " (user-full-name))
106 ;; Note that some sites have room and phone number fields in
107 ;; full name which look silly when inserted. Rather than do
108 ;; anything about that here, let user give prefix argument so that
109 ;; s/he can edit the full name field in prompter if s/he wants.
110 (login-name (if whoami
111 (read-input "Login name: " (user-login-name))
113 (site-name (if whoami
114 (read-input "Site name: " (system-name))
116 (defun (add-log-current-defun))
119 (setq file-name
(find-change-log file-name
))
121 ;; Set ENTRY to the file name to use in the new entry.
122 (and buffer-file-name
123 ;; Never want to add a change log entry for the ChangeLog file itself.
124 (not (string= buffer-file-name file-name
))
125 (setq entry
(if (string-match
126 (concat "^" (regexp-quote (file-name-directory
129 (substring buffer-file-name
(match-end 0))
130 (file-name-nondirectory buffer-file-name
))))
132 (if (and other-window
(not (equal file-name buffer-file-name
)))
133 (find-file-other-window file-name
)
134 (find-file file-name
))
136 (goto-char (point-min))
137 (if (looking-at (concat (regexp-quote (substring (current-time-string)
139 ".* " (regexp-quote full-name
)
140 " (" (regexp-quote login-name
) "@"))
142 (insert (current-time-string)
144 " (" login-name
"@" site-name
")\n\n"))
146 ;; Search only within the first paragraph.
147 (if (looking-at "\n*[^\n* \t]")
148 (skip-chars-forward "\n")
149 (forward-paragraph 1))
150 (setq paragraph-end
(point))
151 (goto-char (point-min))
153 ;; Now insert the new line for this entry.
154 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t
)
155 ;; Put this file name into the existing empty entry.
158 ((and (re-search-forward
159 (concat (regexp-quote (concat "* " entry
))
160 ;; Don't accept `foo.bar' when
161 ;; looking for `foo':
162 "\\(\\s \\|[(),:]\\)")
164 ;; Add to the existing entry for the same file.
165 (re-search-forward "^\\s *$\\|^\\s \\*")
167 (while (and (not (eobp)) (looking-at "^\\s *$"))
168 (delete-region (point) (save-excursion (forward-line 1) (point))))
171 (indent-relative-maybe))
175 (while (looking-at "\\sW")
177 (while (and (not (eobp)) (looking-at "^\\s *$"))
178 (delete-region (point) (save-excursion (forward-line 1) (point))))
181 (indent-to left-margin
)
182 (insert "* " (or entry
""))))
183 ;; Now insert the function name, if we have one.
184 ;; Point is at the entry for this file,
185 ;; either at the end of the line or at the first blank line.
188 ;; Make it easy to get rid of the function name.
190 (insert (if (save-excursion
191 (beginning-of-line 1)
192 (looking-at "\\s *$"))
196 ;; No function name, so put in a colon unless we have just a star.
197 (if (not (save-excursion
198 (beginning-of-line 1)
199 (looking-at "\\s *\\(\\*\\s *\\)?$")))
203 (defun add-change-log-entry-other-window (&optional whoami file-name
)
204 "Find change log file in other window and add an entry for today.
205 First arg (interactive prefix) non-nil means prompt for user name and site.
206 Second arg is file name of change log.
207 Interactively, with a prefix argument, the file name is prompted for."
208 (interactive (if current-prefix-arg
209 (list current-prefix-arg
210 (prompt-for-change-log-name))))
211 (add-change-log-entry whoami file-name t
))
212 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
215 (defun change-log-mode ()
216 "Major mode for editting change logs; like Indented Text Mode.
217 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
218 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
219 Each entry behaves as a paragraph, and the entries for one day as a page.
220 Runs `change-log-mode-hook'."
222 (kill-all-local-variables)
224 (setq major-mode
'change-log-mode
225 mode-name
"Change Log"
228 ;; Let each entry behave as one paragraph:
229 (set (make-local-variable 'paragraph-start
) "^\\s *$\\|^^L")
230 (set (make-local-variable 'paragraph-separate
) "^\\s *$\\|^^L\\|^\\sw")
231 ;; Let all entries for one day behave as one page.
232 ;; Match null string on the date-line so that the date-line
233 ;; is grouped with what follows.
234 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
235 (set (make-local-variable 'version-control
) 'never
)
236 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
237 (run-hooks 'change-log-mode-hook
))
239 (defvar add-log-current-defun-header-regexp
240 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[a-z_---A-Z]+\\)[ \t]*[:=]"
241 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
243 (defun add-log-current-defun ()
244 "Return name of function definition point is in, or nil.
246 Understands Lisp, LaTeX (\"functions\" are chapters, sections, ...),
247 Texinfo (@node titles), and C.
249 Other modes are handled by a heuristic that looks in the 10K before
250 point for uppercase headings starting in the first column or
251 identifiers followed by `:' or `=', see variable
252 `add-log-current-defun-header-regexp'.
254 Has a preference of looking backwards."
257 (let ((location (point)))
258 (cond ((memq major-mode
'(emacs-lisp-mode lisp-mode scheme-mode
))
259 ;; If we are now precisely a the beginning of a defun,
260 ;; make sure beginning-of-defun finds that one
261 ;; rather than the previous one.
262 (or (eobp) (forward-char 1))
264 ;; Make sure we are really inside the defun found, not after it.
265 (if (and (progn (end-of-defun)
266 (< location
(point)))
267 (progn (forward-sexp -
1)
268 (>= location
(point))))
271 (skip-chars-forward " ")
272 (buffer-substring (point)
273 (progn (forward-sexp 1) (point))))))
274 ((and (memq major-mode
'(c-mode 'c
++-mode
))
275 (save-excursion (beginning-of-line)
276 ;; Use eq instead of = here to avoid
277 ;; error when at bob and char-after
279 (while (eq (char-after (- (point) 2)) ?
\\)
281 (looking-at "[ \t]*#[ \t]*define[ \t]")))
282 ;; Handle a C macro definition.
284 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
286 (search-forward "define")
287 (skip-chars-forward " \t")
288 (buffer-substring (point)
289 (progn (forward-sexp 1) (point))))
290 ((memq major-mode
'(c-mode 'c
++-mode
))
292 ;; See if we are in the beginning part of a function,
293 ;; before the open brace. If so, advance forward.
294 (while (not (looking-at "{\\|\\(\\s *$\\)"))
299 (if (progn (end-of-defun)
300 (< location
(point)))
306 ;; Skip back over typedefs of arglist.
307 (while (and (not (bobp))
308 (looking-at "[ \t\n]"))
310 ;; See if this is using the DEFUN macro used in Emacs,
311 ;; or the DEFUN macro used by the C library.
312 (if (condition-case nil
318 (looking-at "DEFUN\\b"))
324 (if (= (char-after (point)) ?
\")
327 (skip-chars-forward " ,")))
328 (buffer-substring (point)
329 (progn (forward-sexp 1) (point))))
330 ;; Ordinary C function syntax.
332 (if (condition-case nil
333 ;; Protect against "Unbalanced parens" error.
335 (down-list 1) ; into arglist
337 (skip-chars-backward " \t")
340 ;; Verify initial pos was after
341 ;; real start of function.
342 (if (and (save-excursion
344 ;; For this purpose, include the line
345 ;; that has the decl keywords. This
346 ;; may also include some of the
347 ;; comments before the function.
348 (while (and (not (bobp))
351 (looking-at "[^\n\f]")))
353 (>= location
(point)))
354 ;; Consistency check: going down and up
355 ;; shouldn't take us back before BEG.
357 (buffer-substring (point)
358 (progn (backward-sexp 1)
361 '(TeX-mode plain-TeX-mode LaTeX-mode
;; tex-mode.el
362 plain-tex-mode latex-mode
;; cmutex.el
364 (if (re-search-backward
365 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t
)
367 (goto-char (match-beginning 0))
368 (buffer-substring (1+ (point));; without initial backslash
372 ((eq major-mode
'texinfo-mode
)
373 (if (re-search-backward "^@node[ \t]+\\([^,]+\\)," nil t
)
374 (buffer-substring (match-beginning 1)
376 ((eq major-mode
'fortran-mode
)
377 ;; must be inside function body for this to work
378 (beginning-of-fortran-subprogram)
379 (let ((case-fold-search t
)) ; case-insensitive
380 ;; search for fortran subprogram start
381 (if (re-search-forward
382 "^[ \t]*\\(program\\|subroutine\\|function\
383 \\|[ \ta-z0-9*]*[ \t]+function\\)"
386 ;; move to EOL or before first left paren
387 (if (re-search-forward "[(\n]" nil t
)
388 (progn (forward-char -
1)
389 (skip-chars-backward " \t"))
391 ;; Use the name preceding that.
392 (buffer-substring (point)
393 (progn (forward-sexp -
1)
396 ;; If all else fails, try heuristics
397 (let (case-fold-search)
399 (if (re-search-backward add-log-current-defun-header-regexp
402 (buffer-substring (match-beginning 1)
409 ;;; add-log.el ends here