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