(file-chase-links): New function.
[bpt/emacs.git] / lisp / add-log.el
CommitLineData
84fc2cfa
ER
1;;; add-log.el --- change log maintenance commands for Emacs
2
ec367ad6 3;; Copyright (C) 1985, 1986, 1988, 1993 Free Software Foundation, Inc.
84fc2cfa 4
e9571d2a
ER
5;; Keywords: maint
6
84fc2cfa
ER
7;; This file is part of GNU Emacs.
8
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
fd7fa35a 11;; the Free Software Foundation; either version 2, or (at your option)
84fc2cfa
ER
12;; any later version.
13
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.
18
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.
22
e41b2db1
ER
23;;; Commentary:
24
25;; This facility is documented in the Emacs Manual.
26
fd7fa35a 27;;; Code:
84fc2cfa
ER
28
29;;;###autoload
30(defvar change-log-default-name nil
31 "*Name of a change log file for \\[add-change-log-entry].")
32
33(defun change-log-name ()
34 (or change-log-default-name
35 (if (eq system-type 'vax-vms) "$CHANGE_LOG$.TXT" "ChangeLog")))
36
37(defun prompt-for-change-log-name ()
38 "Prompt for a change log name."
39 (let ((default (change-log-name)))
40 (expand-file-name
41 (read-file-name (format "Log file (default %s): " default)
42 nil default))))
43
45a13f0d
RM
44;;;###autoload
45(defun find-change-log (&optional file-name)
46 "Find a change log file for \\[add-change-log-entry] and return the name.
47Optional arg FILE-NAME is a name to try first.
48If FILE-NAME is nil, use the value of `change-log-default-name' if non-nil.
49Failing that, use \"ChangeLog\" in the current directory.
50If the file does not exist in the named directory, successive parent
51directories are tried.
52
53Once a file is found, `change-log-default-name' is set locally in the
54current buffer to the complete file name."
55 (or file-name
56 (setq file-name (or change-log-default-name
411d5924
RS
57 ;; Chase links in the source file
58 ;; and use the change log in the dir where it points.
59 (and buffer-file-name
60 (let (temp (file buffer-file-name))
61 (while (setq temp (file-symlink-p file))
9a6015e8
RS
62 (setq file
63 (expand-file-name
64 temp (file-name-directory file))))
411d5924 65 (file-name-directory file)))
45a13f0d 66 default-directory)))
723472f1
RM
67 (if (and (eq file-name change-log-default-name)
68 (assq 'change-log-default-name (buffer-local-variables)))
69 ;; Don't do the searching if we already have a buffer-local value.
70 file-name
71
72 (if (file-directory-p file-name)
73 (setq file-name (expand-file-name (change-log-name) file-name)))
74 ;; Chase links before visiting the file.
75 ;; This makes it easier to use a single change log file
76 ;; for several related directories.
4e44c03b
RS
77 (let (temp)
78 (while (setq temp (file-symlink-p file-name))
9a6015e8
RS
79 (setq file-name
80 (expand-file-name temp (file-name-directory file-name)))))
4e44c03b 81 (setq file-name (expand-file-name file-name))
723472f1
RM
82 ;; Move up in the dir hierarchy till we find a change log file.
83 (let ((file1 file-name)
84 parent-dir)
85 (while (and (not (file-exists-p file1))
86 (progn (setq parent-dir
87 (file-name-directory
88 (directory-file-name
89 (file-name-directory file1))))
90 ;; Give up if we are already at the root dir.
91 (not (string= (file-name-directory file1)
92 parent-dir))))
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 (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)
100 file-name))
45a13f0d 101
84fc2cfa
ER
102;;;###autoload
103(defun add-change-log-entry (&optional whoami file-name other-window)
104 "Find change log file and add an entry for today.
105Optional arg (interactive prefix) non-nil means prompt for user name and site.
106Second arg is file name of change log. If nil, uses `change-log-default-name'.
107Third arg OTHER-WINDOW non-nil means visit in other window."
108 (interactive (list current-prefix-arg
109 (prompt-for-change-log-name)))
dd309224 110 (let* ((full-name (if whoami
84fc2cfa
ER
111 (read-input "Full name: " (user-full-name))
112 (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))
119 (user-login-name)))
120 (site-name (if whoami
121 (read-input "Site name: " (system-name))
21d7e080
ER
122 (system-name)))
123 (defun (add-log-current-defun))
82f4acaf 124 paragraph-end entry)
45a13f0d
RM
125
126 (setq file-name (find-change-log file-name))
82f4acaf
RM
127
128 ;; Set ENTRY to the file name to use in the new entry.
129 (and buffer-file-name
130 ;; Never want to add a change log entry for the ChangeLog file itself.
131 (not (string= buffer-file-name file-name))
132 (setq entry (if (string-match
133 (concat "^" (regexp-quote (file-name-directory
134 file-name)))
135 buffer-file-name)
136 (substring buffer-file-name (match-end 0))
137 (file-name-nondirectory buffer-file-name))))
138
84fc2cfa
ER
139 (if (and other-window (not (equal file-name buffer-file-name)))
140 (find-file-other-window file-name)
141 (find-file file-name))
142 (undo-boundary)
143 (goto-char (point-min))
ec367ad6 144 (if (looking-at (concat (regexp-quote (substring (current-time-string)
82f4acaf
RM
145 0 10))
146 ".* " (regexp-quote full-name)
147 " (" (regexp-quote login-name) "@"))
ec367ad6
RS
148 (forward-line 1)
149 (insert (current-time-string)
150 " " full-name
151 " (" login-name "@" site-name ")\n\n"))
82f4acaf 152
74046d00 153 ;; Search only within the first paragraph.
716a781e
RS
154 (if (looking-at "\n*[^\n* \t]")
155 (skip-chars-forward "\n")
156 (forward-paragraph 1))
82f4acaf 157 (setq paragraph-end (point))
84fc2cfa 158 (goto-char (point-min))
82f4acaf 159
1832dbd1 160 ;; Now insert the new line for this entry.
82f4acaf
RM
161 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t)
162 ;; Put this file name into the existing empty entry.
163 (if entry
164 (insert entry)))
165 ((and (re-search-forward
166 (concat (regexp-quote (concat "* " entry))
167 ;; Don't accept `foo.bar' when
168 ;; looking for `foo':
169 "\\(\\s \\|[(),:]\\)")
170 paragraph-end t))
171 ;; Add to the existing entry for the same file.
172 (re-search-forward "^\\s *$\\|^\\s \\*")
1832dbd1 173 (beginning-of-line)
09b389d0 174 (while (and (not (eobp)) (looking-at "^\\s *$"))
1832dbd1
RS
175 (delete-region (point) (save-excursion (forward-line 1) (point))))
176 (insert "\n\n")
177 (forward-line -2)
dd309224 178 (indent-relative-maybe))
21d7e080 179 (t
dd309224
RM
180 ;; Make a new entry.
181 (forward-line 1)
182 (while (looking-at "\\sW")
183 (forward-line 1))
09b389d0 184 (while (and (not (eobp)) (looking-at "^\\s *$"))
1832dbd1
RS
185 (delete-region (point) (save-excursion (forward-line 1) (point))))
186 (insert "\n\n\n")
187 (forward-line -2)
dd309224
RM
188 (indent-to left-margin)
189 (insert "* " (or entry ""))))
1832dbd1 190 ;; Now insert the function name, if we have one.
21d7e080
ER
191 ;; Point is at the entry for this file,
192 ;; either at the end of the line or at the first blank line.
193 (if defun
194 (progn
dd309224 195 ;; Make it easy to get rid of the function name.
21d7e080 196 (undo-boundary)
dd309224
RM
197 (insert (if (save-excursion
198 (beginning-of-line 1)
199 (looking-at "\\s *$"))
200 ""
201 " ")
202 "(" defun "): "))
1832dbd1 203 ;; No function name, so put in a colon unless we have just a star.
dd309224
RM
204 (if (not (save-excursion
205 (beginning-of-line 1)
206 (looking-at "\\s *\\(\\*\\s *\\)?$")))
1832dbd1 207 (insert ": ")))))
84fc2cfa 208
84fc2cfa
ER
209;;;###autoload
210(defun add-change-log-entry-other-window (&optional whoami file-name)
211 "Find change log file in other window and add an entry for today.
212First arg (interactive prefix) non-nil means prompt for user name and site.
213Second arg is file name of change log.
214Interactively, with a prefix argument, the file name is prompted for."
215 (interactive (if current-prefix-arg
216 (list current-prefix-arg
217 (prompt-for-change-log-name))))
218 (add-change-log-entry whoami file-name t))
82f4acaf 219;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
84fc2cfa 220
1da56800 221;;;###autoload
21d7e080 222(defun change-log-mode ()
dd309224 223 "Major mode for editting change logs; like Indented Text Mode.
09b389d0 224Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
5516387c
RM
225New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
226Each entry behaves as a paragraph, and the entries for one day as a page.
227Runs `change-log-mode-hook'."
21d7e080
ER
228 (interactive)
229 (kill-all-local-variables)
230 (indented-text-mode)
82f4acaf
RM
231 (setq major-mode 'change-log-mode
232 mode-name "Change Log"
233 left-margin 8
234 fill-column 74)
21d7e080 235 ;; Let each entry behave as one paragraph:
dd309224
RM
236 (set (make-local-variable 'paragraph-start) "^\\s *$\\|^^L")
237 (set (make-local-variable 'paragraph-separate) "^\\s *$\\|^^L\\|^\\sw")
21d7e080 238 ;; Let all entries for one day behave as one page.
2c91c85c
RS
239 ;; Match null string on the date-line so that the date-line
240 ;; is grouped with what follows.
241 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
dd309224
RM
242 (set (make-local-variable 'version-control) 'never)
243 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
21d7e080
ER
244 (run-hooks 'change-log-mode-hook))
245
246(defvar add-log-current-defun-header-regexp
d80788d8 247 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[a-z_---A-Z]+\\)[ \t]*[:=]"
21d7e080
ER
248 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
249
250(defun add-log-current-defun ()
251 "Return name of function definition point is in, or nil.
252
253Understands Lisp, LaTeX (\"functions\" are chapters, sections, ...),
254Texinfo (@node titles), and C.
255
256Other modes are handled by a heuristic that looks in the 10K before
257point for uppercase headings starting in the first column or
258identifiers followed by `:' or `=', see variable
259`add-log-current-defun-header-regexp'.
260
261Has a preference of looking backwards."
2cc0b765
RS
262 (condition-case nil
263 (save-excursion
264 (let ((location (point)))
265 (cond ((memq major-mode '(emacs-lisp-mode lisp-mode scheme-mode))
266 ;; If we are now precisely a the beginning of a defun,
267 ;; make sure beginning-of-defun finds that one
268 ;; rather than the previous one.
269 (or (eobp) (forward-char 1))
270 (beginning-of-defun)
271 ;; Make sure we are really inside the defun found, not after it.
272 (if (and (progn (end-of-defun)
273 (< location (point)))
274 (progn (forward-sexp -1)
275 (>= location (point))))
276 (progn
277 (forward-word 1)
278 (skip-chars-forward " ")
279 (buffer-substring (point)
280 (progn (forward-sexp 1) (point))))))
281 ((and (memq major-mode '(c-mode 'c++-mode))
282 (save-excursion (beginning-of-line)
283 ;; Use eq instead of = here to avoid
284 ;; error when at bob and char-after
285 ;; returns nil.
286 (while (eq (char-after (- (point) 2)) ?\\)
36e6631c 287 (forward-line -1))
2cc0b765
RS
288 (looking-at "[ \t]*#[ \t]*define[ \t]")))
289 ;; Handle a C macro definition.
290 (beginning-of-line)
291 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
292 (forward-line -1))
293 (search-forward "define")
294 (skip-chars-forward " \t")
295 (buffer-substring (point)
296 (progn (forward-sexp 1) (point))))
297 ((memq major-mode '(c-mode 'c++-mode))
298 (beginning-of-line)
299 ;; See if we are in the beginning part of a function,
300 ;; before the open brace. If so, advance forward.
301 (while (not (looking-at "{\\|\\(\\s *$\\)"))
302 (forward-line 1))
303 (or (eobp)
304 (forward-char 1))
305 (beginning-of-defun)
306 (if (progn (end-of-defun)
307 (< location (point)))
308 (progn
309 (backward-sexp 1)
310 (let (beg tem)
fd7fa35a 311
2cc0b765
RS
312 (forward-line -1)
313 ;; Skip back over typedefs of arglist.
314 (while (and (not (bobp))
315 (looking-at "[ \t\n]"))
316 (forward-line -1))
317 ;; See if this is using the DEFUN macro used in Emacs,
318 ;; or the DEFUN macro used by the C library.
319 (if (condition-case nil
320 (and (save-excursion
321 (forward-line 1)
322 (backward-sexp 1)
323 (beginning-of-line)
324 (setq tem (point))
325 (looking-at "DEFUN\\b"))
326 (>= location tem))
327 (error nil))
328 (progn
329 (goto-char tem)
330 (down-list 1)
331 (if (= (char-after (point)) ?\")
332 (progn
333 (forward-sexp 1)
334 (skip-chars-forward " ,")))
335 (buffer-substring (point)
336 (progn (forward-sexp 1) (point))))
337 ;; Ordinary C function syntax.
338 (setq beg (point))
339 (if (condition-case nil
340 ;; Protect against "Unbalanced parens" error.
341 (progn
342 (down-list 1) ; into arglist
343 (backward-up-list 1)
344 (skip-chars-backward " \t")
345 t)
346 (error nil))
347 ;; Verify initial pos was after
348 ;; real start of function.
349 (if (and (save-excursion
350 (goto-char beg)
351 ;; For this purpose, include the line
352 ;; that has the decl keywords. This
353 ;; may also include some of the
354 ;; comments before the function.
355 (while (and (not (bobp))
356 (save-excursion
357 (forward-line -1)
358 (looking-at "[^\n\f]")))
359 (forward-line -1))
360 (>= location (point)))
361 ;; Consistency check: going down and up
362 ;; shouldn't take us back before BEG.
363 (> (point) beg))
364 (buffer-substring (point)
365 (progn (backward-sexp 1)
366 (point))))))))))
367 ((memq major-mode
368 '(TeX-mode plain-TeX-mode LaTeX-mode;; tex-mode.el
369 plain-tex-mode latex-mode;; cmutex.el
370 ))
371 (if (re-search-backward
372 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
373 (progn
374 (goto-char (match-beginning 0))
375 (buffer-substring (1+ (point));; without initial backslash
376 (progn
377 (end-of-line)
378 (point))))))
379 ((eq major-mode 'texinfo-mode)
380 (if (re-search-backward "^@node[ \t]+\\([^,]+\\)," nil t)
381 (buffer-substring (match-beginning 1)
382 (match-end 1))))
383 (t
384 ;; If all else fails, try heuristics
385 (let (case-fold-search)
386 (end-of-line)
387 (if (re-search-backward add-log-current-defun-header-regexp
388 (- (point) 10000)
389 t)
390 (buffer-substring (match-beginning 1)
391 (match-end 1))))))))
392 (error nil)))
ef15f270
JB
393
394
1da56800
RS
395(provide 'add-log)
396
fd7fa35a 397;;; add-log.el ends here