Commit | Line | Data |
---|---|---|
84fc2cfa ER |
1 | ;;; add-log.el --- change log maintenance commands for Emacs |
2 | ||
dd309224 RM |
3 | ;; Copyright (C) 1985, 86, 87, 88, 89, 90, 91, 1992 |
4 | ;; Free Software Foundation, Inc. | |
84fc2cfa ER |
5 | |
6 | ;; This file is part of GNU Emacs. | |
7 | ||
8 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
9 | ;; it under the terms of the GNU General Public License as published by | |
fd7fa35a | 10 | ;; the Free Software Foundation; either version 2, or (at your option) |
84fc2cfa ER |
11 | ;; any later version. |
12 | ||
13 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | ;; GNU General Public License for more details. | |
17 | ||
18 | ;; You should have received a copy of the GNU General Public License | |
19 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
20 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | ||
fd7fa35a | 22 | ;;; Code: |
84fc2cfa ER |
23 | |
24 | ;;;###autoload | |
25 | (defvar change-log-default-name nil | |
26 | "*Name of a change log file for \\[add-change-log-entry].") | |
27 | ||
28 | (defun change-log-name () | |
29 | (or change-log-default-name | |
30 | (if (eq system-type 'vax-vms) "$CHANGE_LOG$.TXT" "ChangeLog"))) | |
31 | ||
32 | (defun prompt-for-change-log-name () | |
33 | "Prompt for a change log name." | |
34 | (let ((default (change-log-name))) | |
35 | (expand-file-name | |
36 | (read-file-name (format "Log file (default %s): " default) | |
37 | nil default)))) | |
38 | ||
39 | ;;;###autoload | |
40 | (defun add-change-log-entry (&optional whoami file-name other-window) | |
41 | "Find change log file and add an entry for today. | |
42 | Optional arg (interactive prefix) non-nil means prompt for user name and site. | |
43 | Second arg is file name of change log. If nil, uses `change-log-default-name'. | |
44 | Third arg OTHER-WINDOW non-nil means visit in other window." | |
45 | (interactive (list current-prefix-arg | |
46 | (prompt-for-change-log-name))) | |
dd309224 | 47 | (let* ((full-name (if whoami |
84fc2cfa ER |
48 | (read-input "Full name: " (user-full-name)) |
49 | (user-full-name))) | |
50 | ;; Note that some sites have room and phone number fields in | |
51 | ;; full name which look silly when inserted. Rather than do | |
52 | ;; anything about that here, let user give prefix argument so that | |
53 | ;; s/he can edit the full name field in prompter if s/he wants. | |
54 | (login-name (if whoami | |
55 | (read-input "Login name: " (user-login-name)) | |
56 | (user-login-name))) | |
57 | (site-name (if whoami | |
58 | (read-input "Site name: " (system-name)) | |
21d7e080 ER |
59 | (system-name))) |
60 | (defun (add-log-current-defun)) | |
dd309224 | 61 | entry entry-position empty-entry) |
84fc2cfa ER |
62 | (or file-name |
63 | (setq file-name (or change-log-default-name | |
64 | default-directory))) | |
dd309224 RM |
65 | (setq file-name (if (file-directory-p file-name) |
66 | (expand-file-name (change-log-name) file-name) | |
67 | (expand-file-name file-name))) | |
09b389d0 RS |
68 | ;; Chase links before visiting the file. |
69 | ;; This makes it easier to use a single change log file | |
70 | ;; for several related directories. | |
71 | (setq file-name (or (file-symlink-p file-name) file-name)) | |
84fc2cfa | 72 | (set (make-local-variable 'change-log-default-name) file-name) |
dd309224 RM |
73 | (if buffer-file-name |
74 | (setq entry (if (string-match | |
75 | (concat "^" (regexp-quote (file-name-directory | |
76 | file-name))) | |
77 | buffer-file-name) | |
78 | (substring buffer-file-name (match-end 0)) | |
79 | (file-name-nondirectory buffer-file-name)))) | |
1832dbd1 RS |
80 | ;; Never want to add a change log entry for the ChangeLog file itself. |
81 | (if (equal entry "ChangeLog") | |
dd309224 RM |
82 | (setq entry nil |
83 | defun nil)) | |
84fc2cfa ER |
84 | (if (and other-window (not (equal file-name buffer-file-name))) |
85 | (find-file-other-window file-name) | |
86 | (find-file file-name)) | |
87 | (undo-boundary) | |
88 | (goto-char (point-min)) | |
89 | (if (not (and (looking-at (substring (current-time-string) 0 10)) | |
90 | (looking-at (concat ".* " full-name " (" login-name "@")))) | |
91 | (progn (insert (current-time-string) | |
92 | " " full-name | |
93 | " (" login-name | |
94 | "@" site-name ")\n\n"))) | |
95 | (goto-char (point-min)) | |
21d7e080 ER |
96 | (setq empty-entry |
97 | (and (search-forward "\n\t* \n" nil t) | |
98 | (1- (point)))) | |
99 | (if (and entry | |
100 | (not empty-entry)) | |
dd309224 | 101 | ;; Look for today's entry for the same file. |
21d7e080 ER |
102 | ;; If there is an empty entry (just a `*'), take the hint and |
103 | ;; use it. This is so that C-x a from the ChangeLog buffer | |
104 | ;; itself can be used to force the next entry to be added at | |
105 | ;; the beginning, even if there are today's entries for the | |
106 | ;; same file (but perhaps different revisions). | |
dd309224 RM |
107 | (let ((entry-boundary (save-excursion |
108 | (and (re-search-forward "\n[A-Z]" nil t) | |
109 | (point))))) | |
110 | (setq entry-position (save-excursion | |
111 | (and (re-search-forward | |
112 | (concat | |
113 | (regexp-quote (concat "* " entry)) | |
114 | ;; don't accept `foo.bar' when | |
115 | ;; looking for `foo': | |
116 | "[ \n\t,:]") | |
117 | entry-boundary | |
118 | t) | |
119 | (1- (match-end 0))))))) | |
1832dbd1 | 120 | ;; Now insert the new line for this entry. |
21d7e080 | 121 | (cond (entry-position |
dd309224 RM |
122 | ;; Move to the existing entry for the same file. |
123 | (goto-char entry-position) | |
124 | (re-search-forward "^\\s *$") | |
1832dbd1 | 125 | (beginning-of-line) |
09b389d0 | 126 | (while (and (not (eobp)) (looking-at "^\\s *$")) |
1832dbd1 RS |
127 | (delete-region (point) (save-excursion (forward-line 1) (point)))) |
128 | (insert "\n\n") | |
129 | (forward-line -2) | |
dd309224 | 130 | (indent-relative-maybe)) |
21d7e080 | 131 | (empty-entry |
dd309224 RM |
132 | ;; Put this file name into the existing empty entry. |
133 | (goto-char empty-entry) | |
134 | (if entry | |
135 | (insert entry))) | |
21d7e080 | 136 | (t |
dd309224 RM |
137 | ;; Make a new entry. |
138 | (forward-line 1) | |
139 | (while (looking-at "\\sW") | |
140 | (forward-line 1)) | |
09b389d0 | 141 | (while (and (not (eobp)) (looking-at "^\\s *$")) |
1832dbd1 RS |
142 | (delete-region (point) (save-excursion (forward-line 1) (point)))) |
143 | (insert "\n\n\n") | |
144 | (forward-line -2) | |
dd309224 RM |
145 | (indent-to left-margin) |
146 | (insert "* " (or entry "")))) | |
1832dbd1 | 147 | ;; Now insert the function name, if we have one. |
21d7e080 ER |
148 | ;; Point is at the entry for this file, |
149 | ;; either at the end of the line or at the first blank line. | |
150 | (if defun | |
151 | (progn | |
dd309224 | 152 | ;; Make it easy to get rid of the function name. |
21d7e080 | 153 | (undo-boundary) |
dd309224 RM |
154 | (insert (if (save-excursion |
155 | (beginning-of-line 1) | |
156 | (looking-at "\\s *$")) | |
157 | "" | |
158 | " ") | |
159 | "(" defun "): ")) | |
1832dbd1 | 160 | ;; No function name, so put in a colon unless we have just a star. |
dd309224 RM |
161 | (if (not (save-excursion |
162 | (beginning-of-line 1) | |
163 | (looking-at "\\s *\\(\\*\\s *\\)?$"))) | |
1832dbd1 | 164 | (insert ": "))))) |
84fc2cfa ER |
165 | |
166 | ;;;###autoload | |
167 | (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window) | |
168 | ||
169 | ;;;###autoload | |
170 | (defun add-change-log-entry-other-window (&optional whoami file-name) | |
171 | "Find change log file in other window and add an entry for today. | |
172 | First arg (interactive prefix) non-nil means prompt for user name and site. | |
173 | Second arg is file name of change log. | |
174 | Interactively, with a prefix argument, the file name is prompted for." | |
175 | (interactive (if current-prefix-arg | |
176 | (list current-prefix-arg | |
177 | (prompt-for-change-log-name)))) | |
178 | (add-change-log-entry whoami file-name t)) | |
179 | ||
21d7e080 | 180 | (defun change-log-mode () |
dd309224 | 181 | "Major mode for editting change logs; like Indented Text Mode. |
09b389d0 | 182 | Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74. |
dd309224 | 183 | New log entries are usually made with \\[add-change-log-entry]." |
21d7e080 ER |
184 | (interactive) |
185 | (kill-all-local-variables) | |
186 | (indented-text-mode) | |
187 | (setq major-mode 'change-log-mode) | |
188 | (setq mode-name "Change Log") | |
fd7fa35a ER |
189 | (setq left-margin 8) |
190 | (setq fill-column 74) | |
21d7e080 | 191 | ;; Let each entry behave as one paragraph: |
dd309224 RM |
192 | (set (make-local-variable 'paragraph-start) "^\\s *$\\|^^L") |
193 | (set (make-local-variable 'paragraph-separate) "^\\s *$\\|^^L\\|^\\sw") | |
21d7e080 ER |
194 | ;; Let all entries for one day behave as one page. |
195 | ;; Note that a page boundary is also a paragraph boundary. | |
196 | ;; Unfortunately the date line of a page actually belongs to | |
197 | ;; the next day, but I don't see how to avoid that since | |
198 | ;; page moving cmds go to the end of the match, and Emacs | |
199 | ;; regexps don't have a context feature. | |
200 | (set (make-local-variable 'page-delimiter) "^[A-Z][a-z][a-z] .*\n\\|^\f") | |
dd309224 RM |
201 | (set (make-local-variable 'version-control) 'never) |
202 | (set (make-local-variable 'adaptive-fill-regexp) "\\s *") | |
21d7e080 ER |
203 | (run-hooks 'change-log-mode-hook)) |
204 | ||
205 | (defvar add-log-current-defun-header-regexp | |
206 | "^\\([A-Z][A-Z_ ]+\\|[a-z_---A-Z]+\\)[ \t]*[:=]" | |
207 | "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.") | |
208 | ||
209 | (defun add-log-current-defun () | |
210 | "Return name of function definition point is in, or nil. | |
211 | ||
212 | Understands Lisp, LaTeX (\"functions\" are chapters, sections, ...), | |
213 | Texinfo (@node titles), and C. | |
214 | ||
215 | Other modes are handled by a heuristic that looks in the 10K before | |
216 | point for uppercase headings starting in the first column or | |
217 | identifiers followed by `:' or `=', see variable | |
218 | `add-log-current-defun-header-regexp'. | |
219 | ||
220 | Has a preference of looking backwards." | |
221 | (save-excursion | |
1832dbd1 RS |
222 | (let ((location (point))) |
223 | (cond ((memq major-mode '(emacs-lisp-mode lisp-mode scheme-mode)) | |
224 | ;; If we are now precisely a the beginning of a defun, | |
225 | ;; make sure beginning-of-defun finds that one | |
226 | ;; rather than the previous one. | |
09b389d0 | 227 | (or (eobp) (forward-char 1)) |
1832dbd1 RS |
228 | (beginning-of-defun) |
229 | ;; Make sure we are really inside the defun found, not after it. | |
09b389d0 RS |
230 | (if (and (progn (end-of-defun) |
231 | (< location (point))) | |
232 | (progn (forward-sexp -1) | |
233 | (>= location (point)))) | |
1832dbd1 RS |
234 | (progn |
235 | (forward-word 1) | |
236 | (skip-chars-forward " ") | |
237 | (buffer-substring (point) | |
238 | (progn (forward-sexp 1) (point)))))) | |
09b389d0 RS |
239 | ((and (or (eq major-mode 'c-mode) |
240 | (eq major-mode 'c++-mode)) | |
241 | (save-excursion (beginning-of-line) | |
242 | (while (= (char-after (- (point) 2)) ?\\) | |
243 | (forward-line -1)) | |
244 | (looking-at "[ \t]*#[ \t]*define[ \t]"))) | |
245 | ;; Handle a C macro definition. | |
246 | (beginning-of-line) | |
247 | (while (= (char-after (- (point) 2)) ?\\) | |
248 | (forward-line -1)) | |
249 | (search-forward "define") | |
250 | (skip-chars-forward " \t") | |
251 | (buffer-substring (point) | |
252 | (progn (forward-sexp 1) (point)))) | |
1832dbd1 RS |
253 | ((or (eq major-mode 'c-mode) |
254 | (eq major-mode 'c++-mode)) | |
255 | ;; See if we are in the beginning part of a function, | |
256 | ;; before the open brace. If so, advance forward. | |
257 | (while (not (or (looking-at "{") | |
258 | (looking-at "\\s *$"))) | |
259 | (forward-line 1)) | |
09b389d0 | 260 | (or (eobp) (forward-char 1)) |
1832dbd1 | 261 | (beginning-of-defun) |
09b389d0 RS |
262 | (if (progn (end-of-defun) |
263 | (< location (point))) | |
1832dbd1 | 264 | (progn |
09b389d0 RS |
265 | (backward-sexp 1) |
266 | (let (beg tem) | |
267 | ||
268 | (forward-line -1) | |
269 | ;; Skip back over typedefs of arglist. | |
270 | (while (and (not (bobp)) | |
271 | (looking-at "[ \t\n]")) | |
272 | (forward-line -1)) | |
273 | ;; See if this is using the DEFUN macro used in Emacs. | |
274 | (if (save-excursion | |
275 | (while (and (not (bobp)) | |
276 | (looking-at "[^\n\f]") | |
277 | (not (looking-at ".*\\*/"))) | |
278 | (forward-line -1)) | |
279 | (forward-line 1) | |
280 | (setq tem (point)) | |
281 | (and (looking-at "DEFUN ") | |
282 | (>= location (point)))) | |
283 | (progn | |
284 | (goto-char tem) | |
285 | (down-list 1) | |
286 | (forward-sexp 1) | |
287 | (skip-chars-forward " ,") | |
288 | (buffer-substring (point) | |
289 | (progn (forward-sexp 1) (point)))) | |
290 | ;; Ordinary C function syntax. | |
291 | (setq beg (point)) | |
292 | (down-list 1) ; into arglist | |
293 | (backward-up-list 1) | |
294 | (skip-chars-backward " \t") | |
295 | ;; Verify initial pos was after real start of function. | |
296 | (if (and (save-excursion | |
297 | (goto-char beg) | |
298 | ;; For this purpose, include the line | |
299 | ;; that has the decl keywords. | |
300 | ;; This may also include some of the comments | |
301 | ;; before the function. | |
302 | (while (and (not (bobp)) | |
303 | (save-excursion (forward-line -1) | |
304 | (looking-at "[^\n\f]"))) | |
305 | (forward-line -1)) | |
306 | (>= location (point))) | |
307 | ;; Consistency check: going down and up | |
308 | ;; shouldn't take us back before BEG. | |
309 | (> (point) beg)) | |
310 | (buffer-substring (point) | |
311 | (progn (backward-sexp 1) | |
312 | (point))))))))) | |
1832dbd1 RS |
313 | ((memq major-mode |
314 | '(TeX-mode plain-TeX-mode LaTeX-mode;; tex-mode.el | |
315 | plain-tex-mode latex-mode;; cmutex.el | |
316 | )) | |
317 | (if (re-search-backward | |
318 | "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t) | |
319 | (progn | |
320 | (goto-char (match-beginning 0)) | |
321 | (buffer-substring (1+ (point));; without initial backslash | |
322 | (progn | |
323 | (end-of-line) | |
324 | (point)))))) | |
325 | ((eq major-mode 'texinfo-mode) | |
326 | (if (re-search-backward "^@node[ \t]+\\([^,]+\\)," nil t) | |
21d7e080 | 327 | (buffer-substring (match-beginning 1) |
1832dbd1 RS |
328 | (match-end 1)))) |
329 | (t | |
330 | ;; If all else fails, try heuristics | |
331 | (let (case-fold-search) | |
332 | (if (re-search-backward add-log-current-defun-header-regexp | |
333 | (- (point) 10000) | |
334 | t) | |
335 | (buffer-substring (match-beginning 1) | |
336 | (match-end 1))))))))) | |
fd7fa35a ER |
337 | |
338 | ;;; add-log.el ends here |