(add-log-lisp-like-modes, add-log-c-like-modes, add-log-tex-like-modes):
[bpt/emacs.git] / lisp / add-log.el
CommitLineData
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
47instead) 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
54This 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
62This 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.
101It takes the same format as the TZ argument of `set-time-zone-rule'.
102If 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
145Optional arg FILE-NAME specifies the file to use.
de98fcaf
RS
146If FILE-NAME is nil, use the value of `change-log-default-name'.
147If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
148\(or whatever we use on this operating system).
149
150If 'change-log-default-name' contains a leading directory component, then
151simply find it in the current directory. Otherwise, search in the current
152directory and its successive parents for a file so named.
45a13f0d
RM
153
154Once a file is found, `change-log-default-name' is set locally in the
155current 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.
201Optional arg (interactive prefix) non-nil means prompt for user name and site.
202Second arg is file name of change log. If nil, uses `change-log-default-name'.
287d149f
RM
203Third arg OTHER-WINDOW non-nil means visit in other window.
204Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
51bd1843
EN
205never 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
326Optional arg (interactive prefix) non-nil means prompt for user name and site.
327Second arg is file name of change log. \
328If 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 338Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
5516387c
RM
339New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
340Each entry behaves as a paragraph, and the entries for one day as a page.
341Runs `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.
375Prefix 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
fb644f48
EN
389;;;###autoload
390(defvar add-log-lisp-like-modes
391 '(emacs-lisp-mode lisp-mode scheme-mode lisp-interaction-mode)
392 "*Modes that look like Lisp to `add-log-current-defun'.")
393
394;;;###autoload
395(defvar add-log-c-like-modes
396 '(c-mode c++-mode c++-c-mode objc-mode)
397 "*Modes that look like C to `add-log-current-defun'.")
398
399;;;###autoload
400(defvar add-log-tex-like-modes
401 '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode)
402 "*Modes that look like TeX to `add-log-current-defun'.")
403
e332f80b 404;;;###autoload
21d7e080
ER
405(defun add-log-current-defun ()
406 "Return name of function definition point is in, or nil.
407
63314951 408Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
64bd2d51 409Texinfo (@node titles), Perl, and Fortran.
21d7e080
ER
410
411Other modes are handled by a heuristic that looks in the 10K before
412point for uppercase headings starting in the first column or
413identifiers followed by `:' or `=', see variable
414`add-log-current-defun-header-regexp'.
415
416Has a preference of looking backwards."
2cc0b765
RS
417 (condition-case nil
418 (save-excursion
419 (let ((location (point)))
fb644f48 420 (cond ((memq major-mode add-log-lisp-like-modes)
a0151877 421 ;; If we are now precisely at the beginning of a defun,
2cc0b765
RS
422 ;; make sure beginning-of-defun finds that one
423 ;; rather than the previous one.
424 (or (eobp) (forward-char 1))
425 (beginning-of-defun)
426 ;; Make sure we are really inside the defun found, not after it.
947b2743
RS
427 (if (and (looking-at "\\s(")
428 (progn (end-of-defun)
2cc0b765
RS
429 (< location (point)))
430 (progn (forward-sexp -1)
431 (>= location (point))))
432 (progn
0e933219
JB
433 (if (looking-at "\\s(")
434 (forward-char 1))
435 (forward-sexp 1)
a0151877 436 (skip-chars-forward " '")
2cc0b765
RS
437 (buffer-substring (point)
438 (progn (forward-sexp 1) (point))))))
fb644f48
EN
439 ((and (memq major-mode add-log-c-like-modes)
440 (save-excursion
441 (beginning-of-line)
442 ;; Use eq instead of = here to avoid
443 ;; error when at bob and char-after
444 ;; returns nil.
445 (while (eq (char-after (- (point) 2)) ?\\)
446 (forward-line -1))
447 (looking-at "[ \t]*#[ \t]*define[ \t]")))
2cc0b765
RS
448 ;; Handle a C macro definition.
449 (beginning-of-line)
450 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
451 (forward-line -1))
452 (search-forward "define")
453 (skip-chars-forward " \t")
454 (buffer-substring (point)
455 (progn (forward-sexp 1) (point))))
fb644f48 456 ((memq major-mode add-log-c-like-modes)
2cc0b765
RS
457 (beginning-of-line)
458 ;; See if we are in the beginning part of a function,
459 ;; before the open brace. If so, advance forward.
460 (while (not (looking-at "{\\|\\(\\s *$\\)"))
461 (forward-line 1))
462 (or (eobp)
463 (forward-char 1))
464 (beginning-of-defun)
465 (if (progn (end-of-defun)
466 (< location (point)))
467 (progn
468 (backward-sexp 1)
469 (let (beg tem)
fd7fa35a 470
2cc0b765
RS
471 (forward-line -1)
472 ;; Skip back over typedefs of arglist.
473 (while (and (not (bobp))
474 (looking-at "[ \t\n]"))
475 (forward-line -1))
476 ;; See if this is using the DEFUN macro used in Emacs,
477 ;; or the DEFUN macro used by the C library.
478 (if (condition-case nil
479 (and (save-excursion
019644ee
RS
480 (end-of-line)
481 (while (= (preceding-char) ?\\)
482 (end-of-line 2))
2cc0b765
RS
483 (backward-sexp 1)
484 (beginning-of-line)
485 (setq tem (point))
486 (looking-at "DEFUN\\b"))
487 (>= location tem))
488 (error nil))
489 (progn
490 (goto-char tem)
491 (down-list 1)
492 (if (= (char-after (point)) ?\")
493 (progn
494 (forward-sexp 1)
495 (skip-chars-forward " ,")))
496 (buffer-substring (point)
497 (progn (forward-sexp 1) (point))))
32e986d4
RS
498 (if (looking-at "^[+-]")
499 (get-method-definition)
500 ;; Ordinary C function syntax.
501 (setq beg (point))
e172f546
KH
502 (if (and (condition-case nil
503 ;; Protect against "Unbalanced parens" error.
504 (progn
505 (down-list 1) ; into arglist
506 (backward-up-list 1)
507 (skip-chars-backward " \t")
508 t)
509 (error nil))
510 ;; Verify initial pos was after
511 ;; real start of function.
512 (save-excursion
513 (goto-char beg)
514 ;; For this purpose, include the line
515 ;; that has the decl keywords. This
516 ;; may also include some of the
517 ;; comments before the function.
518 (while (and (not (bobp))
519 (save-excursion
520 (forward-line -1)
521 (looking-at "[^\n\f]")))
522 (forward-line -1))
523 (>= location (point)))
32e986d4
RS
524 ;; Consistency check: going down and up
525 ;; shouldn't take us back before BEG.
526 (> (point) beg))
e172f546
KH
527 (let (end middle)
528 ;; Don't include any final newline
529 ;; in the name we use.
530 (if (= (preceding-char) ?\n)
531 (forward-char -1))
532 (setq end (point))
533 (backward-sexp 1)
534 ;; Now find the right beginning of the name.
535 ;; Include certain keywords if they
536 ;; precede the name.
537 (setq middle (point))
538 (forward-word -1)
6dfa1d83
RS
539 ;; Ignore these subparts of a class decl
540 ;; and move back to the class name itself.
541 (while (looking-at "public \\|private ")
542 (skip-chars-backward " \t:")
543 (setq end (point))
544 (backward-sexp 1)
545 (setq middle (point))
546 (forward-word -1))
e172f546
KH
547 (and (bolp)
548 (looking-at "struct \\|union \\|class ")
549 (setq middle (point)))
550 (buffer-substring middle end)))))))))
fb644f48 551 ((memq major-mode add-log-tex-like-modes)
2cc0b765
RS
552 (if (re-search-backward
553 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
554 (progn
555 (goto-char (match-beginning 0))
556 (buffer-substring (1+ (point));; without initial backslash
557 (progn
558 (end-of-line)
559 (point))))))
560 ((eq major-mode 'texinfo-mode)
3071ee28 561 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
2cc0b765
RS
562 (buffer-substring (match-beginning 1)
563 (match-end 1))))
64bd2d51
RS
564 ((eq major-mode 'perl-mode)
565 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t)
566 (buffer-substring (match-beginning 1)
567 (match-end 1))))
a9e2a7f2
RS
568 ((eq major-mode 'fortran-mode)
569 ;; must be inside function body for this to work
570 (beginning-of-fortran-subprogram)
571 (let ((case-fold-search t)) ; case-insensitive
572 ;; search for fortran subprogram start
573 (if (re-search-forward
574 "^[ \t]*\\(program\\|subroutine\\|function\
575\\|[ \ta-z0-9*]*[ \t]+function\\)"
63314951 576 nil t)
a9e2a7f2
RS
577 (progn
578 ;; move to EOL or before first left paren
579 (if (re-search-forward "[(\n]" nil t)
580 (progn (forward-char -1)
581 (skip-chars-backward " \t"))
582 (end-of-line))
583 ;; Use the name preceding that.
584 (buffer-substring (point)
585 (progn (forward-sexp -1)
586 (point)))))))
2cc0b765
RS
587 (t
588 ;; If all else fails, try heuristics
589 (let (case-fold-search)
590 (end-of-line)
591 (if (re-search-backward add-log-current-defun-header-regexp
592 (- (point) 10000)
593 t)
594 (buffer-substring (match-beginning 1)
595 (match-end 1))))))))
596 (error nil)))
ef15f270 597
15319a8f
RS
598(defvar get-method-definition-md)
599
59c1a7de
RS
600;; Subroutine used within get-method-definition.
601;; Add the last match in the buffer to the end of `md',
602;; followed by the string END; move to the end of that match.
603(defun get-method-definition-1 (end)
15319a8f
RS
604 (setq get-method-definition-md
605 (concat get-method-definition-md
606 (buffer-substring (match-beginning 1) (match-end 1))
607 end))
59c1a7de
RS
608 (goto-char (match-end 0)))
609
610;; For objective C, return the method name if we are in a method.
611(defun get-method-definition ()
15319a8f 612 (let ((get-method-definition-md "["))
59c1a7de 613 (save-excursion
f27f16ed 614 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
59c1a7de
RS
615 (get-method-definition-1 " ")))
616 (save-excursion
617 (cond
f27f16ed 618 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
59c1a7de
RS
619 (get-method-definition-1 "")
620 (while (not (looking-at "[{;]"))
621 (looking-at
f27f16ed 622 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
59c1a7de 623 (get-method-definition-1 ""))
15319a8f 624 (concat get-method-definition-md "]"))))))
59c1a7de 625
ef15f270 626
1da56800
RS
627(provide 'add-log)
628
fd7fa35a 629;;; add-log.el ends here