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