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