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