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