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