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