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