Commit | Line | Data |
---|---|---|
84fc2cfa ER |
1 | ;;; add-log.el --- change log maintenance commands for Emacs |
2 | ||
fcad5199 | 3 | ;; Copyright (C) 1985, 86, 88, 93, 94, 1997 Free Software Foundation, Inc. |
84fc2cfa | 4 | |
df63ae66 | 5 | ;; Keywords: tools |
e9571d2a | 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 | |
b578f267 EN |
20 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
21 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | ;; Boston, MA 02111-1307, USA. | |
84fc2cfa | 23 | |
e41b2db1 ER |
24 | ;;; Commentary: |
25 | ||
26 | ;; This facility is documented in the Emacs Manual. | |
27 | ||
fd7fa35a | 28 | ;;; Code: |
84fc2cfa | 29 | |
fcad5199 RS |
30 | (defgroup change-log nil |
31 | "Change log maintenance" | |
32 | :group 'tools | |
33 | :prefix "change-log-" | |
34 | :prefix "add-log-") | |
84fc2cfa | 35 | |
fcad5199 RS |
36 | |
37 | (defcustom change-log-default-name nil | |
38 | "*Name of a change log file for \\[add-change-log-entry]." | |
39 | :type '(choice (const :tag "default" nil) | |
40 | string) | |
41 | :group 'change-log) | |
42 | ||
43 | (defcustom add-log-current-defun-function nil | |
6258d3af RM |
44 | "\ |
45 | *If non-nil, function to guess name of current function from surrounding text. | |
46 | \\[add-change-log-entry] calls this function (if nil, `add-log-current-defun' | |
fcad5199 | 47 | instead) with no arguments. It returns a string or nil if it cannot guess." |
5cb58f82 | 48 | :type 'function |
fcad5199 | 49 | :group 'change-log) |
6258d3af | 50 | |
29db528b | 51 | ;;;###autoload |
fcad5199 | 52 | (defcustom add-log-full-name nil |
02ec1592 | 53 | "*Full name of user, for inclusion in ChangeLog daily headers. |
fcad5199 RS |
54 | This defaults to the value returned by the `user-full-name' function." |
55 | :type '(choice (const :tag "Default" nil) | |
56 | string) | |
57 | :group 'change-log) | |
02ec1592 | 58 | |
29db528b | 59 | ;;;###autoload |
fcad5199 | 60 | (defcustom add-log-mailing-address nil |
02ec1592 | 61 | "*Electronic mail address of user, for inclusion in ChangeLog daily headers. |
fcad5199 RS |
62 | This defaults to the value of `user-mail-address'." |
63 | :type '(choice (const :tag "Default" nil) | |
64 | string) | |
65 | :group 'change-log) | |
66 | ||
df63ae66 RS |
67 | (defcustom add-log-time-format 'add-log-iso8601-time-string |
68 | "*Function that defines the time format. | |
69 | For example, `add-log-iso8601-time-string', which gives the | |
70 | date in international ISO 8601 format, | |
71 | and `current-time-string' are two valid values." | |
72 | :type '(radio (const :tag "International ISO 8601 format" | |
73 | add-log-iso8601-time-string) | |
74 | (const :tag "Old format, as returned by `current-time-string'" | |
75 | current-time-string) | |
76 | (function :tag "Other")) | |
77 | :group 'change-log) | |
02ec1592 | 78 | |
5f562719 | 79 | (defvar change-log-font-lock-keywords |
5572c1d1 SM |
80 | '(;; |
81 | ;; Date lines, new and old styles. | |
6f6a2641 | 82 | ("^\\sw.........[0-9: ]*" |
5572c1d1 | 83 | (0 font-lock-string-face) |
6f6a2641 EN |
84 | ("\\([^<]+\\)<\\([A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\\)>" nil nil |
85 | (1 font-lock-reference-face) | |
86 | (2 font-lock-variable-name-face))) | |
5572c1d1 SM |
87 | ;; |
88 | ;; File names. | |
89 | ("^\t\\* \\([^ ,:([\n]+\\)" | |
90 | (1 font-lock-function-name-face) | |
91 | ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 font-lock-function-name-face))) | |
92 | ;; | |
93 | ;; Function or variable names. | |
6f187d8d | 94 | ("(\\([^) ,:\n]+\\)" |
5572c1d1 | 95 | (1 font-lock-keyword-face) |
6f187d8d | 96 | ("\\=, *\\([^) ,:\n]+\\)" nil nil (1 font-lock-keyword-face))) |
5572c1d1 SM |
97 | ;; |
98 | ;; Conditionals. | |
99 | ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 font-lock-variable-name-face)) | |
100 | ;; | |
a3cab9f0 | 101 | ;; Acknowledgements. |
095fb03e | 102 | ("^\t\\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)" |
b438e1cb | 103 | 1 font-lock-comment-face) |
095fb03e | 104 | (" \\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)" |
b438e1cb | 105 | 1 font-lock-comment-face)) |
5f562719 RS |
106 | "Additional expressions to highlight in Change Log mode.") |
107 | ||
45c50c5d KH |
108 | (defvar change-log-mode-map nil |
109 | "Keymap for Change Log major mode.") | |
110 | (if change-log-mode-map | |
111 | nil | |
112 | (setq change-log-mode-map (make-sparse-keymap))) | |
113 | ||
51bd1843 EN |
114 | (defvar change-log-time-zone-rule nil |
115 | "Time zone used for calculating change log time stamps. | |
116 | It takes the same format as the TZ argument of `set-time-zone-rule'. | |
117 | If nil, use local time.") | |
118 | ||
df63ae66 | 119 | (defun add-log-iso8601-time-zone (time) |
51bd1843 EN |
120 | (let* ((utc-offset (or (car (current-time-zone time)) 0)) |
121 | (sign (if (< utc-offset 0) ?- ?+)) | |
122 | (sec (abs utc-offset)) | |
123 | (ss (% sec 60)) | |
124 | (min (/ sec 60)) | |
125 | (mm (% min 60)) | |
126 | (hh (/ min 60))) | |
127 | (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d") | |
128 | ((not (zerop mm)) "%c%02d:%02d") | |
129 | (t "%c%02d")) | |
130 | sign hh mm ss))) | |
131 | ||
df63ae66 RS |
132 | (defun add-log-iso8601-time-string () |
133 | (if change-log-time-zone-rule | |
134 | (let ((tz (getenv "TZ")) | |
135 | (now (current-time))) | |
136 | (unwind-protect | |
137 | (progn | |
138 | (set-time-zone-rule | |
139 | change-log-time-zone-rule) | |
140 | (concat | |
141 | (format-time-string "%Y-%m-%d " now) | |
142 | (add-log-iso8601-time-zone now))) | |
143 | (set-time-zone-rule tz))) | |
144 | (format-time-string "%Y-%m-%d"))) | |
145 | ||
84fc2cfa ER |
146 | (defun change-log-name () |
147 | (or change-log-default-name | |
513063cf | 148 | (if (eq system-type 'vax-vms) |
4a047d23 RS |
149 | "$CHANGE_LOG$.TXT" |
150 | "ChangeLog"))) | |
84fc2cfa | 151 | |
287d149f | 152 | ;;;###autoload |
84fc2cfa ER |
153 | (defun prompt-for-change-log-name () |
154 | "Prompt for a change log name." | |
117aaf60 KH |
155 | (let* ((default (change-log-name)) |
156 | (name (expand-file-name | |
157 | (read-file-name (format "Log file (default %s): " default) | |
158 | nil default)))) | |
159 | ;; Handle something that is syntactically a directory name. | |
160 | ;; Look for ChangeLog or whatever in that directory. | |
161 | (if (string= (file-name-nondirectory name) "") | |
162 | (expand-file-name (file-name-nondirectory default) | |
163 | name) | |
164 | ;; Handle specifying a file that is a directory. | |
165 | (if (file-directory-p name) | |
166 | (expand-file-name (file-name-nondirectory default) | |
167 | (file-name-as-directory name)) | |
168 | name)))) | |
84fc2cfa | 169 | |
45a13f0d RM |
170 | ;;;###autoload |
171 | (defun find-change-log (&optional file-name) | |
172 | "Find a change log file for \\[add-change-log-entry] and return the name. | |
a82e2ed5 RS |
173 | |
174 | Optional arg FILE-NAME specifies the file to use. | |
de98fcaf RS |
175 | If FILE-NAME is nil, use the value of `change-log-default-name'. |
176 | If 'change-log-default-name' is nil, behave as though it were 'ChangeLog' | |
177 | \(or whatever we use on this operating system). | |
178 | ||
179 | If 'change-log-default-name' contains a leading directory component, then | |
513063cf | 180 | simply find it in the current directory. Otherwise, search in the current |
de98fcaf | 181 | directory and its successive parents for a file so named. |
45a13f0d RM |
182 | |
183 | Once a file is found, `change-log-default-name' is set locally in the | |
184 | current buffer to the complete file name." | |
a82e2ed5 RS |
185 | ;; If user specified a file name or if this buffer knows which one to use, |
186 | ;; just use that. | |
45a13f0d | 187 | (or file-name |
de98fcaf RS |
188 | (setq file-name (and change-log-default-name |
189 | (file-name-directory change-log-default-name) | |
190 | change-log-default-name)) | |
a82e2ed5 RS |
191 | (progn |
192 | ;; Chase links in the source file | |
193 | ;; and use the change log in the dir where it points. | |
194 | (setq file-name (or (and buffer-file-name | |
195 | (file-name-directory | |
196 | (file-chase-links buffer-file-name))) | |
197 | default-directory)) | |
198 | (if (file-directory-p file-name) | |
199 | (setq file-name (expand-file-name (change-log-name) file-name))) | |
200 | ;; Chase links before visiting the file. | |
201 | ;; This makes it easier to use a single change log file | |
202 | ;; for several related directories. | |
203 | (setq file-name (file-chase-links file-name)) | |
204 | (setq file-name (expand-file-name file-name)) | |
205 | ;; Move up in the dir hierarchy till we find a change log file. | |
206 | (let ((file1 file-name) | |
207 | parent-dir) | |
208 | (while (and (not (or (get-file-buffer file1) (file-exists-p file1))) | |
209 | (progn (setq parent-dir | |
210 | (file-name-directory | |
211 | (directory-file-name | |
212 | (file-name-directory file1)))) | |
213 | ;; Give up if we are already at the root dir. | |
214 | (not (string= (file-name-directory file1) | |
215 | parent-dir)))) | |
216 | ;; Move up to the parent dir and try again. | |
513063cf | 217 | (setq file1 (expand-file-name |
a82e2ed5 RS |
218 | (file-name-nondirectory (change-log-name)) |
219 | parent-dir))) | |
220 | ;; If we found a change log in a parent, use that. | |
221 | (if (or (get-file-buffer file1) (file-exists-p file1)) | |
222 | (setq file-name file1))))) | |
223 | ;; Make a local variable in this buffer so we needn't search again. | |
224 | (set (make-local-variable 'change-log-default-name) file-name) | |
225 | file-name) | |
45a13f0d | 226 | |
df63ae66 | 227 | |
84fc2cfa | 228 | ;;;###autoload |
287d149f | 229 | (defun add-change-log-entry (&optional whoami file-name other-window new-entry) |
84fc2cfa ER |
230 | "Find change log file and add an entry for today. |
231 | Optional arg (interactive prefix) non-nil means prompt for user name and site. | |
232 | Second arg is file name of change log. If nil, uses `change-log-default-name'. | |
287d149f RM |
233 | Third arg OTHER-WINDOW non-nil means visit in other window. |
234 | Fourth arg NEW-ENTRY non-nil means always create a new entry at the front; | |
51bd1843 EN |
235 | never append to an existing entry. Today's date is calculated according to |
236 | `change-log-time-zone-rule' if non-nil, otherwise in local time." | |
84fc2cfa ER |
237 | (interactive (list current-prefix-arg |
238 | (prompt-for-change-log-name))) | |
550d8777 RS |
239 | (or add-log-full-name |
240 | (setq add-log-full-name (user-full-name))) | |
241 | (or add-log-mailing-address | |
242 | (setq add-log-mailing-address user-mail-address)) | |
02ec1592 BF |
243 | (if whoami |
244 | (progn | |
245 | (setq add-log-full-name (read-input "Full name: " add-log-full-name)) | |
84fc2cfa ER |
246 | ;; Note that some sites have room and phone number fields in |
247 | ;; full name which look silly when inserted. Rather than do | |
248 | ;; anything about that here, let user give prefix argument so that | |
249 | ;; s/he can edit the full name field in prompter if s/he wants. | |
02ec1592 BF |
250 | (setq add-log-mailing-address |
251 | (read-input "Mailing address: " add-log-mailing-address)))) | |
252 | (let ((defun (funcall (or add-log-current-defun-function | |
253 | 'add-log-current-defun))) | |
254 | paragraph-end entry) | |
45a13f0d | 255 | |
3e1c918b | 256 | (setq file-name (expand-file-name (find-change-log file-name))) |
82f4acaf RM |
257 | |
258 | ;; Set ENTRY to the file name to use in the new entry. | |
259 | (and buffer-file-name | |
260 | ;; Never want to add a change log entry for the ChangeLog file itself. | |
261 | (not (string= buffer-file-name file-name)) | |
262 | (setq entry (if (string-match | |
263 | (concat "^" (regexp-quote (file-name-directory | |
264 | file-name))) | |
265 | buffer-file-name) | |
266 | (substring buffer-file-name (match-end 0)) | |
267 | (file-name-nondirectory buffer-file-name)))) | |
268 | ||
84fc2cfa ER |
269 | (if (and other-window (not (equal file-name buffer-file-name))) |
270 | (find-file-other-window file-name) | |
271 | (find-file file-name)) | |
675a998f RS |
272 | (or (eq major-mode 'change-log-mode) |
273 | (change-log-mode)) | |
84fc2cfa ER |
274 | (undo-boundary) |
275 | (goto-char (point-min)) | |
df63ae66 | 276 | (let ((new-entry (concat (funcall add-log-time-format) |
51bd1843 EN |
277 | " " add-log-full-name |
278 | " <" add-log-mailing-address ">"))) | |
279 | (if (looking-at (regexp-quote new-entry)) | |
280 | (forward-line 1) | |
281 | (insert new-entry "\n\n"))) | |
82f4acaf | 282 | |
74046d00 | 283 | ;; Search only within the first paragraph. |
716a781e RS |
284 | (if (looking-at "\n*[^\n* \t]") |
285 | (skip-chars-forward "\n") | |
286 | (forward-paragraph 1)) | |
82f4acaf | 287 | (setq paragraph-end (point)) |
84fc2cfa | 288 | (goto-char (point-min)) |
82f4acaf | 289 | |
1832dbd1 | 290 | ;; Now insert the new line for this entry. |
82f4acaf RM |
291 | (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t) |
292 | ;; Put this file name into the existing empty entry. | |
293 | (if entry | |
294 | (insert entry))) | |
287d149f | 295 | ((and (not new-entry) |
e172f546 KH |
296 | (let (case-fold-search) |
297 | (re-search-forward | |
298 | (concat (regexp-quote (concat "* " entry)) | |
299 | ;; Don't accept `foo.bar' when | |
300 | ;; looking for `foo': | |
301 | "\\(\\s \\|[(),:]\\)") | |
302 | paragraph-end t))) | |
82f4acaf RM |
303 | ;; Add to the existing entry for the same file. |
304 | (re-search-forward "^\\s *$\\|^\\s \\*") | |
e172f546 KH |
305 | (goto-char (match-beginning 0)) |
306 | ;; Delete excess empty lines; make just 2. | |
09b389d0 | 307 | (while (and (not (eobp)) (looking-at "^\\s *$")) |
1832dbd1 RS |
308 | (delete-region (point) (save-excursion (forward-line 1) (point)))) |
309 | (insert "\n\n") | |
310 | (forward-line -2) | |
dd309224 | 311 | (indent-relative-maybe)) |
21d7e080 | 312 | (t |
dd309224 RM |
313 | ;; Make a new entry. |
314 | (forward-line 1) | |
315 | (while (looking-at "\\sW") | |
316 | (forward-line 1)) | |
09b389d0 | 317 | (while (and (not (eobp)) (looking-at "^\\s *$")) |
1832dbd1 RS |
318 | (delete-region (point) (save-excursion (forward-line 1) (point)))) |
319 | (insert "\n\n\n") | |
320 | (forward-line -2) | |
dd309224 RM |
321 | (indent-to left-margin) |
322 | (insert "* " (or entry "")))) | |
1832dbd1 | 323 | ;; Now insert the function name, if we have one. |
21d7e080 ER |
324 | ;; Point is at the entry for this file, |
325 | ;; either at the end of the line or at the first blank line. | |
326 | (if defun | |
327 | (progn | |
dd309224 | 328 | ;; Make it easy to get rid of the function name. |
21d7e080 | 329 | (undo-boundary) |
dd309224 RM |
330 | (insert (if (save-excursion |
331 | (beginning-of-line 1) | |
513063cf | 332 | (looking-at "\\s *$")) |
dd309224 RM |
333 | "" |
334 | " ") | |
335 | "(" defun "): ")) | |
1832dbd1 | 336 | ;; No function name, so put in a colon unless we have just a star. |
dd309224 RM |
337 | (if (not (save-excursion |
338 | (beginning-of-line 1) | |
339 | (looking-at "\\s *\\(\\*\\s *\\)?$"))) | |
1832dbd1 | 340 | (insert ": "))))) |
84fc2cfa | 341 | |
84fc2cfa ER |
342 | ;;;###autoload |
343 | (defun add-change-log-entry-other-window (&optional whoami file-name) | |
344 | "Find change log file in other window and add an entry for today. | |
6258d3af RM |
345 | Optional arg (interactive prefix) non-nil means prompt for user name and site. |
346 | Second arg is file name of change log. \ | |
347 | If nil, uses `change-log-default-name'." | |
84fc2cfa ER |
348 | (interactive (if current-prefix-arg |
349 | (list current-prefix-arg | |
350 | (prompt-for-change-log-name)))) | |
351 | (add-change-log-entry whoami file-name t)) | |
82f4acaf | 352 | ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window) |
84fc2cfa | 353 | |
1da56800 | 354 | ;;;###autoload |
21d7e080 | 355 | (defun change-log-mode () |
eb8c3be9 | 356 | "Major mode for editing change logs; like Indented Text Mode. |
09b389d0 | 357 | Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74. |
5516387c RM |
358 | New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window]. |
359 | Each entry behaves as a paragraph, and the entries for one day as a page. | |
360 | Runs `change-log-mode-hook'." | |
21d7e080 ER |
361 | (interactive) |
362 | (kill-all-local-variables) | |
363 | (indented-text-mode) | |
82f4acaf RM |
364 | (setq major-mode 'change-log-mode |
365 | mode-name "Change Log" | |
366 | left-margin 8 | |
4f675a8c | 367 | fill-column 74 |
60f10a06 KH |
368 | indent-tabs-mode t |
369 | tab-width 8) | |
45c50c5d | 370 | (use-local-map change-log-mode-map) |
60f10a06 KH |
371 | (set (make-local-variable 'fill-paragraph-function) |
372 | 'change-log-fill-paragraph) | |
21d7e080 | 373 | ;; Let each entry behave as one paragraph: |
14ee1953 RS |
374 | ;; We really do want "^" in paragraph-start below: it is only the lines that |
375 | ;; begin at column 0 (despite the left-margin of 8) that we are looking for. | |
51bd1843 EN |
376 | (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<") |
377 | (set (make-local-variable 'paragraph-separate) "\\s *$\\|\f\\|^\\<") | |
21d7e080 | 378 | ;; Let all entries for one day behave as one page. |
2c91c85c RS |
379 | ;; Match null string on the date-line so that the date-line |
380 | ;; is grouped with what follows. | |
964141f2 | 381 | (set (make-local-variable 'page-delimiter) "^\\<\\|^\f") |
dd309224 RM |
382 | (set (make-local-variable 'version-control) 'never) |
383 | (set (make-local-variable 'adaptive-fill-regexp) "\\s *") | |
4b286eca SM |
384 | (set (make-local-variable 'font-lock-defaults) |
385 | '(change-log-font-lock-keywords t)) | |
21d7e080 ER |
386 | (run-hooks 'change-log-mode-hook)) |
387 | ||
287d149f RM |
388 | ;; It might be nice to have a general feature to replace this. The idea I |
389 | ;; have is a variable giving a regexp matching text which should not be | |
390 | ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(". | |
391 | ;; But I don't feel up to implementing that today. | |
392 | (defun change-log-fill-paragraph (&optional justify) | |
393 | "Fill the paragraph, but preserve open parentheses at beginning of lines. | |
394 | Prefix arg means justify as well." | |
395 | (interactive "P") | |
8d6467a4 RS |
396 | (let ((end (progn (forward-paragraph) (point))) |
397 | (beg (progn (backward-paragraph) (point))) | |
14ee1953 | 398 | (paragraph-start (concat paragraph-start "\\|\\s *\\s("))) |
8d6467a4 RS |
399 | (fill-region beg end justify) |
400 | t)) | |
287d149f | 401 | \f |
fcad5199 | 402 | (defcustom add-log-current-defun-header-regexp |
26add1bf | 403 | "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]" |
fcad5199 RS |
404 | "*Heuristic regexp used by `add-log-current-defun' for unknown major modes." |
405 | :type 'regexp | |
406 | :group 'change-log) | |
21d7e080 | 407 | |
fb644f48 EN |
408 | ;;;###autoload |
409 | (defvar add-log-lisp-like-modes | |
410 | '(emacs-lisp-mode lisp-mode scheme-mode lisp-interaction-mode) | |
411 | "*Modes that look like Lisp to `add-log-current-defun'.") | |
412 | ||
413 | ;;;###autoload | |
414 | (defvar add-log-c-like-modes | |
415 | '(c-mode c++-mode c++-c-mode objc-mode) | |
416 | "*Modes that look like C to `add-log-current-defun'.") | |
417 | ||
418 | ;;;###autoload | |
419 | (defvar add-log-tex-like-modes | |
420 | '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode) | |
421 | "*Modes that look like TeX to `add-log-current-defun'.") | |
422 | ||
e332f80b | 423 | ;;;###autoload |
21d7e080 ER |
424 | (defun add-log-current-defun () |
425 | "Return name of function definition point is in, or nil. | |
426 | ||
63314951 | 427 | Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...), |
64bd2d51 | 428 | Texinfo (@node titles), Perl, and Fortran. |
21d7e080 ER |
429 | |
430 | Other modes are handled by a heuristic that looks in the 10K before | |
431 | point for uppercase headings starting in the first column or | |
432 | identifiers followed by `:' or `=', see variable | |
433 | `add-log-current-defun-header-regexp'. | |
434 | ||
435 | Has a preference of looking backwards." | |
2cc0b765 RS |
436 | (condition-case nil |
437 | (save-excursion | |
438 | (let ((location (point))) | |
fb644f48 | 439 | (cond ((memq major-mode add-log-lisp-like-modes) |
a0151877 | 440 | ;; If we are now precisely at the beginning of a defun, |
2cc0b765 RS |
441 | ;; make sure beginning-of-defun finds that one |
442 | ;; rather than the previous one. | |
443 | (or (eobp) (forward-char 1)) | |
444 | (beginning-of-defun) | |
445 | ;; Make sure we are really inside the defun found, not after it. | |
947b2743 RS |
446 | (if (and (looking-at "\\s(") |
447 | (progn (end-of-defun) | |
2cc0b765 RS |
448 | (< location (point))) |
449 | (progn (forward-sexp -1) | |
450 | (>= location (point)))) | |
451 | (progn | |
0e933219 JB |
452 | (if (looking-at "\\s(") |
453 | (forward-char 1)) | |
454 | (forward-sexp 1) | |
a0151877 | 455 | (skip-chars-forward " '") |
2cc0b765 RS |
456 | (buffer-substring (point) |
457 | (progn (forward-sexp 1) (point)))))) | |
fb644f48 EN |
458 | ((and (memq major-mode add-log-c-like-modes) |
459 | (save-excursion | |
460 | (beginning-of-line) | |
461 | ;; Use eq instead of = here to avoid | |
462 | ;; error when at bob and char-after | |
463 | ;; returns nil. | |
464 | (while (eq (char-after (- (point) 2)) ?\\) | |
465 | (forward-line -1)) | |
466 | (looking-at "[ \t]*#[ \t]*define[ \t]"))) | |
2cc0b765 RS |
467 | ;; Handle a C macro definition. |
468 | (beginning-of-line) | |
469 | (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above | |
470 | (forward-line -1)) | |
471 | (search-forward "define") | |
472 | (skip-chars-forward " \t") | |
473 | (buffer-substring (point) | |
474 | (progn (forward-sexp 1) (point)))) | |
fb644f48 | 475 | ((memq major-mode add-log-c-like-modes) |
2cc0b765 RS |
476 | (beginning-of-line) |
477 | ;; See if we are in the beginning part of a function, | |
478 | ;; before the open brace. If so, advance forward. | |
479 | (while (not (looking-at "{\\|\\(\\s *$\\)")) | |
480 | (forward-line 1)) | |
481 | (or (eobp) | |
482 | (forward-char 1)) | |
483 | (beginning-of-defun) | |
484 | (if (progn (end-of-defun) | |
485 | (< location (point))) | |
486 | (progn | |
487 | (backward-sexp 1) | |
488 | (let (beg tem) | |
fd7fa35a | 489 | |
2cc0b765 RS |
490 | (forward-line -1) |
491 | ;; Skip back over typedefs of arglist. | |
492 | (while (and (not (bobp)) | |
493 | (looking-at "[ \t\n]")) | |
494 | (forward-line -1)) | |
495 | ;; See if this is using the DEFUN macro used in Emacs, | |
496 | ;; or the DEFUN macro used by the C library. | |
497 | (if (condition-case nil | |
498 | (and (save-excursion | |
019644ee RS |
499 | (end-of-line) |
500 | (while (= (preceding-char) ?\\) | |
501 | (end-of-line 2)) | |
2cc0b765 RS |
502 | (backward-sexp 1) |
503 | (beginning-of-line) | |
504 | (setq tem (point)) | |
505 | (looking-at "DEFUN\\b")) | |
506 | (>= location tem)) | |
507 | (error nil)) | |
508 | (progn | |
509 | (goto-char tem) | |
510 | (down-list 1) | |
511 | (if (= (char-after (point)) ?\") | |
512 | (progn | |
513 | (forward-sexp 1) | |
514 | (skip-chars-forward " ,"))) | |
515 | (buffer-substring (point) | |
516 | (progn (forward-sexp 1) (point)))) | |
32e986d4 RS |
517 | (if (looking-at "^[+-]") |
518 | (get-method-definition) | |
519 | ;; Ordinary C function syntax. | |
520 | (setq beg (point)) | |
e172f546 KH |
521 | (if (and (condition-case nil |
522 | ;; Protect against "Unbalanced parens" error. | |
523 | (progn | |
524 | (down-list 1) ; into arglist | |
525 | (backward-up-list 1) | |
526 | (skip-chars-backward " \t") | |
527 | t) | |
528 | (error nil)) | |
529 | ;; Verify initial pos was after | |
530 | ;; real start of function. | |
531 | (save-excursion | |
532 | (goto-char beg) | |
533 | ;; For this purpose, include the line | |
534 | ;; that has the decl keywords. This | |
535 | ;; may also include some of the | |
536 | ;; comments before the function. | |
537 | (while (and (not (bobp)) | |
538 | (save-excursion | |
539 | (forward-line -1) | |
540 | (looking-at "[^\n\f]"))) | |
541 | (forward-line -1)) | |
542 | (>= location (point))) | |
32e986d4 RS |
543 | ;; Consistency check: going down and up |
544 | ;; shouldn't take us back before BEG. | |
545 | (> (point) beg)) | |
e172f546 KH |
546 | (let (end middle) |
547 | ;; Don't include any final newline | |
548 | ;; in the name we use. | |
549 | (if (= (preceding-char) ?\n) | |
550 | (forward-char -1)) | |
551 | (setq end (point)) | |
552 | (backward-sexp 1) | |
553 | ;; Now find the right beginning of the name. | |
554 | ;; Include certain keywords if they | |
555 | ;; precede the name. | |
556 | (setq middle (point)) | |
557 | (forward-word -1) | |
6dfa1d83 RS |
558 | ;; Ignore these subparts of a class decl |
559 | ;; and move back to the class name itself. | |
560 | (while (looking-at "public \\|private ") | |
561 | (skip-chars-backward " \t:") | |
562 | (setq end (point)) | |
563 | (backward-sexp 1) | |
564 | (setq middle (point)) | |
565 | (forward-word -1)) | |
e172f546 KH |
566 | (and (bolp) |
567 | (looking-at "struct \\|union \\|class ") | |
568 | (setq middle (point))) | |
569 | (buffer-substring middle end))))))))) | |
fb644f48 | 570 | ((memq major-mode add-log-tex-like-modes) |
2cc0b765 RS |
571 | (if (re-search-backward |
572 | "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t) | |
573 | (progn | |
574 | (goto-char (match-beginning 0)) | |
575 | (buffer-substring (1+ (point));; without initial backslash | |
576 | (progn | |
577 | (end-of-line) | |
578 | (point)))))) | |
579 | ((eq major-mode 'texinfo-mode) | |
3071ee28 | 580 | (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t) |
2cc0b765 RS |
581 | (buffer-substring (match-beginning 1) |
582 | (match-end 1)))) | |
64bd2d51 RS |
583 | ((eq major-mode 'perl-mode) |
584 | (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t) | |
585 | (buffer-substring (match-beginning 1) | |
586 | (match-end 1)))) | |
a9e2a7f2 RS |
587 | ((eq major-mode 'fortran-mode) |
588 | ;; must be inside function body for this to work | |
589 | (beginning-of-fortran-subprogram) | |
590 | (let ((case-fold-search t)) ; case-insensitive | |
591 | ;; search for fortran subprogram start | |
592 | (if (re-search-forward | |
593 | "^[ \t]*\\(program\\|subroutine\\|function\ | |
594 | \\|[ \ta-z0-9*]*[ \t]+function\\)" | |
63314951 | 595 | nil t) |
a9e2a7f2 RS |
596 | (progn |
597 | ;; move to EOL or before first left paren | |
598 | (if (re-search-forward "[(\n]" nil t) | |
599 | (progn (forward-char -1) | |
600 | (skip-chars-backward " \t")) | |
601 | (end-of-line)) | |
602 | ;; Use the name preceding that. | |
603 | (buffer-substring (point) | |
604 | (progn (forward-sexp -1) | |
605 | (point))))))) | |
2cc0b765 RS |
606 | (t |
607 | ;; If all else fails, try heuristics | |
608 | (let (case-fold-search) | |
609 | (end-of-line) | |
610 | (if (re-search-backward add-log-current-defun-header-regexp | |
611 | (- (point) 10000) | |
612 | t) | |
613 | (buffer-substring (match-beginning 1) | |
614 | (match-end 1)))))))) | |
615 | (error nil))) | |
ef15f270 | 616 | |
15319a8f RS |
617 | (defvar get-method-definition-md) |
618 | ||
59c1a7de RS |
619 | ;; Subroutine used within get-method-definition. |
620 | ;; Add the last match in the buffer to the end of `md', | |
621 | ;; followed by the string END; move to the end of that match. | |
622 | (defun get-method-definition-1 (end) | |
15319a8f | 623 | (setq get-method-definition-md |
513063cf | 624 | (concat get-method-definition-md |
15319a8f RS |
625 | (buffer-substring (match-beginning 1) (match-end 1)) |
626 | end)) | |
59c1a7de RS |
627 | (goto-char (match-end 0))) |
628 | ||
629 | ;; For objective C, return the method name if we are in a method. | |
630 | (defun get-method-definition () | |
15319a8f | 631 | (let ((get-method-definition-md "[")) |
59c1a7de | 632 | (save-excursion |
f27f16ed | 633 | (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t) |
59c1a7de RS |
634 | (get-method-definition-1 " "))) |
635 | (save-excursion | |
636 | (cond | |
f27f16ed | 637 | ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t) |
59c1a7de RS |
638 | (get-method-definition-1 "") |
639 | (while (not (looking-at "[{;]")) | |
640 | (looking-at | |
f27f16ed | 641 | "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*") |
59c1a7de | 642 | (get-method-definition-1 "")) |
15319a8f | 643 | (concat get-method-definition-md "]")))))) |
59c1a7de | 644 | |
ef15f270 | 645 | |
1da56800 RS |
646 | (provide 'add-log) |
647 | ||
fd7fa35a | 648 | ;;; add-log.el ends here |