(make-help-screen): Handle case where *Help* comes up in a separate frame.
[bpt/emacs.git] / lisp / add-log.el
CommitLineData
84fc2cfa
ER
1;;; add-log.el --- change log maintenance commands for Emacs
2
e332f80b 3;; Copyright (C) 1985, 1986, 1988, 1993, 1994 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 28
84fc2cfa
ER
29(defvar change-log-default-name nil
30 "*Name of a change log file for \\[add-change-log-entry].")
31
6258d3af
RM
32(defvar add-log-current-defun-function nil
33 "\
34*If non-nil, function to guess name of current function from surrounding text.
35\\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
36instead) with no arguments. It returns a string or nil if it cannot guess.")
37
75ade469
RS
38;; This MUST not be autoloaded, since user-login-name
39;; cannot be known at Emacs dump time.
02ec1592
BF
40(defvar add-log-full-name (user-full-name)
41 "*Full name of user, for inclusion in ChangeLog daily headers.
42This defaults to the value returned by the `user-full-name' function.")
43
75ade469
RS
44;; This MUST not be autoloaded, since user-login-name
45;; cannot be known at Emacs dump time.
fb38ca86 46(defvar add-log-mailing-address user-mail-address
02ec1592 47 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
fb38ca86 48This defaults to the value of `user-mail-address'.")
02ec1592 49
84fc2cfa
ER
50(defun change-log-name ()
51 (or change-log-default-name
8afc29f0
RS
52 (if (eq system-type 'vax-vms)
53 "$CHANGE_LOG$.TXT"
54 (if (eq system-type 'ms-dos)
5675b1e4 55 "changelo"
8afc29f0 56 "ChangeLog"))))
84fc2cfa 57
287d149f 58;;;###autoload
84fc2cfa
ER
59(defun prompt-for-change-log-name ()
60 "Prompt for a change log name."
61 (let ((default (change-log-name)))
62 (expand-file-name
63 (read-file-name (format "Log file (default %s): " default)
64 nil default))))
65
45a13f0d
RM
66;;;###autoload
67(defun find-change-log (&optional file-name)
68 "Find a change log file for \\[add-change-log-entry] and return the name.
a82e2ed5
RS
69
70Optional arg FILE-NAME specifies the file to use.
45a13f0d 71If FILE-NAME is nil, use the value of `change-log-default-name' if non-nil.
a82e2ed5
RS
72Otherwise, search in the current directory and its successive parents
73for a file named `ChangeLog' (or whatever we use on this operating system).
45a13f0d
RM
74
75Once a file is found, `change-log-default-name' is set locally in the
76current buffer to the complete file name."
a82e2ed5
RS
77 ;; If user specified a file name or if this buffer knows which one to use,
78 ;; just use that.
45a13f0d 79 (or file-name
a82e2ed5
RS
80 (setq file-name change-log-default-name)
81 (progn
82 ;; Chase links in the source file
83 ;; and use the change log in the dir where it points.
84 (setq file-name (or (and buffer-file-name
85 (file-name-directory
86 (file-chase-links buffer-file-name)))
87 default-directory))
88 (if (file-directory-p file-name)
89 (setq file-name (expand-file-name (change-log-name) file-name)))
90 ;; Chase links before visiting the file.
91 ;; This makes it easier to use a single change log file
92 ;; for several related directories.
93 (setq file-name (file-chase-links file-name))
94 (setq file-name (expand-file-name file-name))
95 ;; Move up in the dir hierarchy till we find a change log file.
96 (let ((file1 file-name)
97 parent-dir)
98 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
99 (progn (setq parent-dir
100 (file-name-directory
101 (directory-file-name
102 (file-name-directory file1))))
103 ;; Give up if we are already at the root dir.
104 (not (string= (file-name-directory file1)
105 parent-dir))))
106 ;; Move up to the parent dir and try again.
107 (setq file1 (expand-file-name
108 (file-name-nondirectory (change-log-name))
109 parent-dir)))
110 ;; If we found a change log in a parent, use that.
111 (if (or (get-file-buffer file1) (file-exists-p file1))
112 (setq file-name file1)))))
113 ;; Make a local variable in this buffer so we needn't search again.
114 (set (make-local-variable 'change-log-default-name) file-name)
115 file-name)
45a13f0d 116
84fc2cfa 117;;;###autoload
287d149f 118(defun add-change-log-entry (&optional whoami file-name other-window new-entry)
84fc2cfa
ER
119 "Find change log file and add an entry for today.
120Optional arg (interactive prefix) non-nil means prompt for user name and site.
121Second arg is file name of change log. If nil, uses `change-log-default-name'.
287d149f
RM
122Third arg OTHER-WINDOW non-nil means visit in other window.
123Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
124never append to an existing entry."
84fc2cfa
ER
125 (interactive (list current-prefix-arg
126 (prompt-for-change-log-name)))
02ec1592
BF
127 (if whoami
128 (progn
129 (setq add-log-full-name (read-input "Full name: " add-log-full-name))
84fc2cfa
ER
130 ;; Note that some sites have room and phone number fields in
131 ;; full name which look silly when inserted. Rather than do
132 ;; anything about that here, let user give prefix argument so that
133 ;; s/he can edit the full name field in prompter if s/he wants.
02ec1592
BF
134 (setq add-log-mailing-address
135 (read-input "Mailing address: " add-log-mailing-address))))
136 (let ((defun (funcall (or add-log-current-defun-function
137 'add-log-current-defun)))
138 paragraph-end entry)
45a13f0d 139
3e1c918b 140 (setq file-name (expand-file-name (find-change-log file-name)))
82f4acaf
RM
141
142 ;; Set ENTRY to the file name to use in the new entry.
143 (and buffer-file-name
144 ;; Never want to add a change log entry for the ChangeLog file itself.
145 (not (string= buffer-file-name file-name))
146 (setq entry (if (string-match
147 (concat "^" (regexp-quote (file-name-directory
148 file-name)))
149 buffer-file-name)
150 (substring buffer-file-name (match-end 0))
151 (file-name-nondirectory buffer-file-name))))
152
84fc2cfa
ER
153 (if (and other-window (not (equal file-name buffer-file-name)))
154 (find-file-other-window file-name)
155 (find-file file-name))
675a998f
RS
156 (or (eq major-mode 'change-log-mode)
157 (change-log-mode))
84fc2cfa
ER
158 (undo-boundary)
159 (goto-char (point-min))
ec367ad6 160 (if (looking-at (concat (regexp-quote (substring (current-time-string)
82f4acaf 161 0 10))
02ec1592
BF
162 ".* " (regexp-quote add-log-full-name)
163 " (" (regexp-quote add-log-mailing-address)))
ec367ad6
RS
164 (forward-line 1)
165 (insert (current-time-string)
02ec1592
BF
166 " " add-log-full-name
167 " (" add-log-mailing-address ")\n\n"))
82f4acaf 168
74046d00 169 ;; Search only within the first paragraph.
716a781e
RS
170 (if (looking-at "\n*[^\n* \t]")
171 (skip-chars-forward "\n")
172 (forward-paragraph 1))
82f4acaf 173 (setq paragraph-end (point))
84fc2cfa 174 (goto-char (point-min))
82f4acaf 175
1832dbd1 176 ;; Now insert the new line for this entry.
82f4acaf
RM
177 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t)
178 ;; Put this file name into the existing empty entry.
179 (if entry
180 (insert entry)))
287d149f
RM
181 ((and (not new-entry)
182 (re-search-forward
82f4acaf
RM
183 (concat (regexp-quote (concat "* " entry))
184 ;; Don't accept `foo.bar' when
185 ;; looking for `foo':
186 "\\(\\s \\|[(),:]\\)")
187 paragraph-end t))
188 ;; Add to the existing entry for the same file.
189 (re-search-forward "^\\s *$\\|^\\s \\*")
1832dbd1 190 (beginning-of-line)
09b389d0 191 (while (and (not (eobp)) (looking-at "^\\s *$"))
1832dbd1
RS
192 (delete-region (point) (save-excursion (forward-line 1) (point))))
193 (insert "\n\n")
194 (forward-line -2)
dd309224 195 (indent-relative-maybe))
21d7e080 196 (t
dd309224
RM
197 ;; Make a new entry.
198 (forward-line 1)
199 (while (looking-at "\\sW")
200 (forward-line 1))
09b389d0 201 (while (and (not (eobp)) (looking-at "^\\s *$"))
1832dbd1
RS
202 (delete-region (point) (save-excursion (forward-line 1) (point))))
203 (insert "\n\n\n")
204 (forward-line -2)
dd309224
RM
205 (indent-to left-margin)
206 (insert "* " (or entry ""))))
1832dbd1 207 ;; Now insert the function name, if we have one.
21d7e080
ER
208 ;; Point is at the entry for this file,
209 ;; either at the end of the line or at the first blank line.
210 (if defun
211 (progn
dd309224 212 ;; Make it easy to get rid of the function name.
21d7e080 213 (undo-boundary)
dd309224
RM
214 (insert (if (save-excursion
215 (beginning-of-line 1)
216 (looking-at "\\s *$"))
217 ""
218 " ")
219 "(" defun "): "))
1832dbd1 220 ;; No function name, so put in a colon unless we have just a star.
dd309224
RM
221 (if (not (save-excursion
222 (beginning-of-line 1)
223 (looking-at "\\s *\\(\\*\\s *\\)?$")))
1832dbd1 224 (insert ": ")))))
84fc2cfa 225
84fc2cfa
ER
226;;;###autoload
227(defun add-change-log-entry-other-window (&optional whoami file-name)
228 "Find change log file in other window and add an entry for today.
6258d3af
RM
229Optional arg (interactive prefix) non-nil means prompt for user name and site.
230Second arg is file name of change log. \
231If nil, uses `change-log-default-name'."
84fc2cfa
ER
232 (interactive (if current-prefix-arg
233 (list current-prefix-arg
234 (prompt-for-change-log-name))))
235 (add-change-log-entry whoami file-name t))
82f4acaf 236;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
84fc2cfa 237
1da56800 238;;;###autoload
21d7e080 239(defun change-log-mode ()
eb8c3be9 240 "Major mode for editing change logs; like Indented Text Mode.
09b389d0 241Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
5516387c
RM
242New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
243Each entry behaves as a paragraph, and the entries for one day as a page.
244Runs `change-log-mode-hook'."
21d7e080
ER
245 (interactive)
246 (kill-all-local-variables)
247 (indented-text-mode)
82f4acaf
RM
248 (setq major-mode 'change-log-mode
249 mode-name "Change Log"
250 left-margin 8
251 fill-column 74)
287d149f 252 (use-local-map change-log-mode-map)
21d7e080 253 ;; Let each entry behave as one paragraph:
964141f2
RS
254 (set (make-local-variable 'paragraph-start) "^\\s *$\\|^\f")
255 (set (make-local-variable 'paragraph-separate) "^\\s *$\\|^\f\\|^\\sw")
21d7e080 256 ;; Let all entries for one day behave as one page.
2c91c85c
RS
257 ;; Match null string on the date-line so that the date-line
258 ;; is grouped with what follows.
964141f2 259 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
dd309224
RM
260 (set (make-local-variable 'version-control) 'never)
261 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
21d7e080
ER
262 (run-hooks 'change-log-mode-hook))
263
287d149f
RM
264(defvar change-log-mode-map nil
265 "Keymap for Change Log major mode.")
266(if change-log-mode-map
267 nil
268 (setq change-log-mode-map (make-sparse-keymap))
269 (define-key change-log-mode-map "\M-q" 'change-log-fill-paragraph))
270
271;; It might be nice to have a general feature to replace this. The idea I
272;; have is a variable giving a regexp matching text which should not be
273;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
274;; But I don't feel up to implementing that today.
275(defun change-log-fill-paragraph (&optional justify)
276 "Fill the paragraph, but preserve open parentheses at beginning of lines.
277Prefix arg means justify as well."
278 (interactive "P")
279 (let ((paragraph-separate (concat paragraph-separate "\\|^\\s *\\s("))
280 (paragraph-start (concat paragraph-start "\\|^\\s *\\s(")))
281 (fill-paragraph justify)))
282\f
21d7e080 283(defvar add-log-current-defun-header-regexp
26add1bf 284 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
21d7e080
ER
285 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
286
e332f80b 287;;;###autoload
21d7e080
ER
288(defun add-log-current-defun ()
289 "Return name of function definition point is in, or nil.
290
63314951
RS
291Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
292Texinfo (@node titles), and Fortran.
21d7e080
ER
293
294Other modes are handled by a heuristic that looks in the 10K before
295point for uppercase headings starting in the first column or
296identifiers followed by `:' or `=', see variable
297`add-log-current-defun-header-regexp'.
298
299Has a preference of looking backwards."
2cc0b765
RS
300 (condition-case nil
301 (save-excursion
302 (let ((location (point)))
303 (cond ((memq major-mode '(emacs-lisp-mode lisp-mode scheme-mode))
304 ;; If we are now precisely a the beginning of a defun,
305 ;; make sure beginning-of-defun finds that one
306 ;; rather than the previous one.
307 (or (eobp) (forward-char 1))
308 (beginning-of-defun)
309 ;; Make sure we are really inside the defun found, not after it.
310 (if (and (progn (end-of-defun)
311 (< location (point)))
312 (progn (forward-sexp -1)
313 (>= location (point))))
314 (progn
0e933219
JB
315 (if (looking-at "\\s(")
316 (forward-char 1))
317 (forward-sexp 1)
2cc0b765
RS
318 (skip-chars-forward " ")
319 (buffer-substring (point)
320 (progn (forward-sexp 1) (point))))))
d226f0a6 321 ((and (memq major-mode '(c-mode c++-mode c++-c-mode))
2cc0b765
RS
322 (save-excursion (beginning-of-line)
323 ;; Use eq instead of = here to avoid
324 ;; error when at bob and char-after
325 ;; returns nil.
326 (while (eq (char-after (- (point) 2)) ?\\)
36e6631c 327 (forward-line -1))
2cc0b765
RS
328 (looking-at "[ \t]*#[ \t]*define[ \t]")))
329 ;; Handle a C macro definition.
330 (beginning-of-line)
331 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
332 (forward-line -1))
333 (search-forward "define")
334 (skip-chars-forward " \t")
335 (buffer-substring (point)
336 (progn (forward-sexp 1) (point))))
d226f0a6 337 ((memq major-mode '(c-mode c++-mode c++-c-mode))
2cc0b765
RS
338 (beginning-of-line)
339 ;; See if we are in the beginning part of a function,
340 ;; before the open brace. If so, advance forward.
341 (while (not (looking-at "{\\|\\(\\s *$\\)"))
342 (forward-line 1))
343 (or (eobp)
344 (forward-char 1))
345 (beginning-of-defun)
346 (if (progn (end-of-defun)
347 (< location (point)))
348 (progn
349 (backward-sexp 1)
350 (let (beg tem)
fd7fa35a 351
2cc0b765
RS
352 (forward-line -1)
353 ;; Skip back over typedefs of arglist.
354 (while (and (not (bobp))
355 (looking-at "[ \t\n]"))
356 (forward-line -1))
357 ;; See if this is using the DEFUN macro used in Emacs,
358 ;; or the DEFUN macro used by the C library.
359 (if (condition-case nil
360 (and (save-excursion
361 (forward-line 1)
362 (backward-sexp 1)
363 (beginning-of-line)
364 (setq tem (point))
365 (looking-at "DEFUN\\b"))
366 (>= location tem))
367 (error nil))
368 (progn
369 (goto-char tem)
370 (down-list 1)
371 (if (= (char-after (point)) ?\")
372 (progn
373 (forward-sexp 1)
374 (skip-chars-forward " ,")))
375 (buffer-substring (point)
376 (progn (forward-sexp 1) (point))))
377 ;; Ordinary C function syntax.
378 (setq beg (point))
379 (if (condition-case nil
380 ;; Protect against "Unbalanced parens" error.
381 (progn
382 (down-list 1) ; into arglist
383 (backward-up-list 1)
384 (skip-chars-backward " \t")
385 t)
386 (error nil))
387 ;; Verify initial pos was after
388 ;; real start of function.
389 (if (and (save-excursion
390 (goto-char beg)
391 ;; For this purpose, include the line
392 ;; that has the decl keywords. This
393 ;; may also include some of the
394 ;; comments before the function.
395 (while (and (not (bobp))
396 (save-excursion
397 (forward-line -1)
398 (looking-at "[^\n\f]")))
399 (forward-line -1))
400 (>= location (point)))
401 ;; Consistency check: going down and up
402 ;; shouldn't take us back before BEG.
403 (> (point) beg))
404 (buffer-substring (point)
405 (progn (backward-sexp 1)
406 (point))))))))))
407 ((memq major-mode
408 '(TeX-mode plain-TeX-mode LaTeX-mode;; tex-mode.el
409 plain-tex-mode latex-mode;; cmutex.el
410 ))
411 (if (re-search-backward
412 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
413 (progn
414 (goto-char (match-beginning 0))
415 (buffer-substring (1+ (point));; without initial backslash
416 (progn
417 (end-of-line)
418 (point))))))
419 ((eq major-mode 'texinfo-mode)
420 (if (re-search-backward "^@node[ \t]+\\([^,]+\\)," nil t)
421 (buffer-substring (match-beginning 1)
422 (match-end 1))))
a9e2a7f2
RS
423 ((eq major-mode 'fortran-mode)
424 ;; must be inside function body for this to work
425 (beginning-of-fortran-subprogram)
426 (let ((case-fold-search t)) ; case-insensitive
427 ;; search for fortran subprogram start
428 (if (re-search-forward
429 "^[ \t]*\\(program\\|subroutine\\|function\
430\\|[ \ta-z0-9*]*[ \t]+function\\)"
63314951 431 nil t)
a9e2a7f2
RS
432 (progn
433 ;; move to EOL or before first left paren
434 (if (re-search-forward "[(\n]" nil t)
435 (progn (forward-char -1)
436 (skip-chars-backward " \t"))
437 (end-of-line))
438 ;; Use the name preceding that.
439 (buffer-substring (point)
440 (progn (forward-sexp -1)
441 (point)))))))
2cc0b765
RS
442 (t
443 ;; If all else fails, try heuristics
444 (let (case-fold-search)
445 (end-of-line)
446 (if (re-search-backward add-log-current-defun-header-regexp
447 (- (point) 10000)
448 t)
449 (buffer-substring (match-beginning 1)
450 (match-end 1))))))))
451 (error nil)))
ef15f270
JB
452
453
1da56800
RS
454(provide 'add-log)
455
fd7fa35a 456;;; add-log.el ends here