]> code.delx.au - gnu-emacs/blob - lisp/add-log.el
(substitute-key-definition): Add comment describing
[gnu-emacs] / lisp / add-log.el
1 ;;; add-log.el --- change log maintenance commands for Emacs
2
3 ;; Copyright (C) 1985, 86, 88, 93, 94, 97, 98, 2000 Free Software Foundation, Inc.
4
5 ;; Keywords: tools
6
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
11 ;; the Free Software Foundation; either version 2, or (at your option)
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
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.
23
24 ;;; Commentary:
25
26 ;; This facility is documented in the Emacs Manual.
27
28 ;;; Code:
29
30 (eval-when-compile
31 (require 'timezone))
32
33 (defgroup change-log nil
34 "Change log maintenance"
35 :group 'tools
36 :link '(custom-manual "(emacs)Change Log")
37 :prefix "change-log-"
38 :prefix "add-log-")
39
40
41 (defcustom change-log-default-name nil
42 "*Name of a change log file for \\[add-change-log-entry]."
43 :type '(choice (const :tag "default" nil)
44 string)
45 :group 'change-log)
46
47 (defcustom change-log-mode-hook nil
48 "Normal hook run by `change-log-mode'."
49 :type 'hook
50 :group 'change-log)
51
52 (defcustom add-log-current-defun-function nil
53 "*If non-nil, function to guess name of surrounding function.
54 It is used by `add-log-current-defun' in preference to built-in rules.
55 Returns function's name as a string, or nil if outside a function."
56 :type 'function
57 :group 'change-log)
58
59 ;;;###autoload
60 (defcustom add-log-full-name nil
61 "*Full name of user, for inclusion in ChangeLog daily headers.
62 This defaults to the value returned by the function `user-full-name'."
63 :type '(choice (const :tag "Default" nil)
64 string)
65 :group 'change-log)
66
67 ;;;###autoload
68 (defcustom add-log-mailing-address nil
69 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
70 This defaults to the value of `user-mail-address'."
71 :type '(choice (const :tag "Default" nil)
72 string)
73 :group 'change-log)
74
75 (defcustom add-log-time-format 'add-log-iso8601-time-string
76 "*Function that defines the time format.
77 For example, `add-log-iso8601-time-string', which gives the
78 date in international ISO 8601 format,
79 and `current-time-string' are two valid values."
80 :type '(radio (const :tag "International ISO 8601 format"
81 add-log-iso8601-time-string)
82 (const :tag "Old format, as returned by `current-time-string'"
83 current-time-string)
84 (function :tag "Other"))
85 :group 'change-log)
86
87 (defcustom add-log-keep-changes-together nil
88 "*If non-nil, normally keep day's log entries for one file together.
89
90 Log entries for a given file made with \\[add-change-log-entry] or
91 \\[add-change-log-entry-other-window] will only be added to others \
92 for that file made
93 today if this variable is non-nil or that file comes first in today's
94 entries. Otherwise another entry for that file will be started. An
95 original log:
96
97 * foo (...): ...
98 * bar (...): change 1
99
100 in the latter case, \\[add-change-log-entry-other-window] in a \
101 buffer visiting `bar', yields:
102
103 * bar (...): -!-
104 * foo (...): ...
105 * bar (...): change 1
106
107 and in the former:
108
109 * foo (...): ...
110 * bar (...): change 1
111 (...): -!-
112
113 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
114 this variable."
115 :version "20.3"
116 :type 'boolean
117 :group 'change-log)
118
119 (defcustom add-log-file-name-function nil
120 "*If non-nil, function to call to identify the filename for a ChangeLog entry.
121 This function is called with one argument, the value of variable
122 `buffer-file-name' in that buffer. If this is nil, the default is to
123 use the file's name relative to the directory of the change log file."
124 :type 'function
125 :group 'change-log)
126
127
128 (defcustom change-log-version-info-enabled nil
129 "*If non-nil, enable recording version numbers with the changes."
130 :version "21.1"
131 :type 'boolean
132 :group 'change-log)
133
134 (defcustom change-log-version-number-regexp-list
135 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
136 (list
137 ;; (defconst ad-version "2.15"
138 (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re)
139 ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
140 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re)))
141 "*List of regexps to search for version number.
142 The version number must be in group 1.
143 Note: The search is conducted only within 10%, at the beginning of the file."
144 :version "21.1"
145 :type '(repeat regexp)
146 :group 'change-log)
147
148
149 (defvar change-log-font-lock-keywords
150 '(;;
151 ;; Date lines, new and old styles.
152 ("^\\sw.........[0-9:+ ]*"
153 (0 font-lock-string-face)
154 ;; Name and e-mail; some people put e-mail in parens, not angles.
155 ("\\([^<(]+\\)[(<]\\([A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
156 (1 font-lock-constant-face)
157 (2 font-lock-variable-name-face)))
158 ;;
159 ;; File names.
160 ("^\t\\* \\([^ ,:([\n]+\\)"
161 (1 font-lock-function-name-face)
162 ;; Possibly further names in a list:
163 ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 font-lock-function-name-face))
164 ;; Possibly a parenthesized list of names:
165 ("\\= (\\([^) ,:\n]+\\)" nil nil (1 font-lock-keyword-face))
166 ("\\=, *\\([^) ,:\n]+\\)" nil nil (1 font-lock-keyword-face)))
167 ;;
168 ;; Function or variable names.
169 ("^\t(\\([^) ,:\n]+\\)"
170 (1 font-lock-keyword-face)
171 ("\\=, *\\([^) ,:\n]+\\)" nil nil (1 font-lock-keyword-face)))
172 ;;
173 ;; Conditionals.
174 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 font-lock-variable-name-face))
175 ;;
176 ;; Acknowledgements.
177 ("^\t\\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
178 1 font-lock-comment-face)
179 (" \\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
180 1 font-lock-comment-face))
181 "Additional expressions to highlight in Change Log mode.")
182
183 (defvar change-log-mode-map (make-sparse-keymap)
184 "Keymap for Change Log major mode.")
185
186 (defvar change-log-time-zone-rule nil
187 "Time zone used for calculating change log time stamps.
188 It takes the same format as the TZ argument of `set-time-zone-rule'.
189 If nil, use local time.")
190
191 (defun add-log-iso8601-time-zone (time)
192 (let* ((utc-offset (or (car (current-time-zone time)) 0))
193 (sign (if (< utc-offset 0) ?- ?+))
194 (sec (abs utc-offset))
195 (ss (% sec 60))
196 (min (/ sec 60))
197 (mm (% min 60))
198 (hh (/ min 60)))
199 (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
200 ((not (zerop mm)) "%c%02d:%02d")
201 (t "%c%02d"))
202 sign hh mm ss)))
203
204 (defun add-log-iso8601-time-string ()
205 (if change-log-time-zone-rule
206 (let ((tz (getenv "TZ"))
207 (now (current-time)))
208 (unwind-protect
209 (progn
210 (set-time-zone-rule
211 change-log-time-zone-rule)
212 (concat
213 (format-time-string "%Y-%m-%d " now)
214 (add-log-iso8601-time-zone now)))
215 (set-time-zone-rule tz)))
216 (format-time-string "%Y-%m-%d")))
217
218 (defun change-log-name ()
219 "Return (system-dependent) default name for a change log file."
220 (or change-log-default-name
221 (if (eq system-type 'vax-vms)
222 "$CHANGE_LOG$.TXT"
223 "ChangeLog")))
224
225 ;;;###autoload
226 (defun prompt-for-change-log-name ()
227 "Prompt for a change log name."
228 (let* ((default (change-log-name))
229 (name (expand-file-name
230 (read-file-name (format "Log file (default %s): " default)
231 nil default))))
232 ;; Handle something that is syntactically a directory name.
233 ;; Look for ChangeLog or whatever in that directory.
234 (if (string= (file-name-nondirectory name) "")
235 (expand-file-name (file-name-nondirectory default)
236 name)
237 ;; Handle specifying a file that is a directory.
238 (if (file-directory-p name)
239 (expand-file-name (file-name-nondirectory default)
240 (file-name-as-directory name))
241 name))))
242
243 (defun change-log-version-number-search ()
244 "Return version number of current buffer's file.
245 This is the value returned by `vc-workfile-version' or, if that is
246 nil, by matching `change-log-version-number-regexp-list'."
247 (let* ((size (buffer-size))
248 (end
249 ;; The version number can be anywhere in the file, but
250 ;; restrict search to the file beginning: 10% should be
251 ;; enough to prevent some mishits.
252 ;;
253 ;; Apply percentage only if buffer size is bigger than
254 ;; approx 100 lines.
255 (if (> size (* 100 80))
256 (/ size 10)
257 size))
258 version)
259 (or (and buffer-file-name
260 (vc-workfile-version buffer-file-name))
261 (save-restriction
262 (widen)
263 (let ((regexps change-log-version-number-regexp-list))
264 (while regexps
265 (save-excursion
266 (goto-char (point-min))
267 (when (re-search-forward (pop regexps) end t)
268 (setq version (match-string 1)
269 regexps nil)))))))))
270
271
272 ;;;###autoload
273 (defun find-change-log (&optional file-name)
274 "Find a change log file for \\[add-change-log-entry] and return the name.
275
276 Optional arg FILE-NAME specifies the file to use.
277 If FILE-NAME is nil, use the value of `change-log-default-name'.
278 If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
279 \(or whatever we use on this operating system).
280
281 If 'change-log-default-name' contains a leading directory component, then
282 simply find it in the current directory. Otherwise, search in the current
283 directory and its successive parents for a file so named.
284
285 Once a file is found, `change-log-default-name' is set locally in the
286 current buffer to the complete file name."
287 ;; If user specified a file name or if this buffer knows which one to use,
288 ;; just use that.
289 (or file-name
290 (setq file-name (and change-log-default-name
291 (file-name-directory change-log-default-name)
292 change-log-default-name))
293 (progn
294 ;; Chase links in the source file
295 ;; and use the change log in the dir where it points.
296 (setq file-name (or (and buffer-file-name
297 (file-name-directory
298 (file-chase-links buffer-file-name)))
299 default-directory))
300 (if (file-directory-p file-name)
301 (setq file-name (expand-file-name (change-log-name) file-name)))
302 ;; Chase links before visiting the file.
303 ;; This makes it easier to use a single change log file
304 ;; for several related directories.
305 (setq file-name (file-chase-links file-name))
306 (setq file-name (expand-file-name file-name))
307 ;; Move up in the dir hierarchy till we find a change log file.
308 (let ((file1 file-name)
309 parent-dir)
310 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
311 (progn (setq parent-dir
312 (file-name-directory
313 (directory-file-name
314 (file-name-directory file1))))
315 ;; Give up if we are already at the root dir.
316 (not (string= (file-name-directory file1)
317 parent-dir))))
318 ;; Move up to the parent dir and try again.
319 (setq file1 (expand-file-name
320 (file-name-nondirectory (change-log-name))
321 parent-dir)))
322 ;; If we found a change log in a parent, use that.
323 (if (or (get-file-buffer file1) (file-exists-p file1))
324 (setq file-name file1)))))
325 ;; Make a local variable in this buffer so we needn't search again.
326 (set (make-local-variable 'change-log-default-name) file-name)
327 file-name)
328
329 ;;;###autoload
330 (defun add-change-log-entry (&optional whoami file-name other-window new-entry)
331 "Find change log file and add an entry for today.
332 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
333 name and site.
334
335 Second arg is FILE-NAME of change log. If nil, uses `change-log-default-name'.
336 Third arg OTHER-WINDOW non-nil means visit in other window.
337 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
338 never append to an existing entry. Option `add-log-keep-changes-together'
339 otherwise affects whether a new entry is created.
340
341 Today's date is calculated according to `change-log-time-zone-rule' if
342 non-nil, otherwise in local time."
343 (interactive (list current-prefix-arg
344 (prompt-for-change-log-name)))
345 (or add-log-full-name
346 (setq add-log-full-name (user-full-name)))
347 (or add-log-mailing-address
348 (setq add-log-mailing-address user-mail-address))
349 (if whoami
350 (progn
351 (setq add-log-full-name (read-input "Full name: " add-log-full-name))
352 ;; Note that some sites have room and phone number fields in
353 ;; full name which look silly when inserted. Rather than do
354 ;; anything about that here, let user give prefix argument so that
355 ;; s/he can edit the full name field in prompter if s/he wants.
356 (setq add-log-mailing-address
357 (read-input "Mailing address: " add-log-mailing-address))))
358 (let ((defun (add-log-current-defun))
359 (version (and change-log-version-info-enabled
360 (change-log-version-number-search)))
361 bound entry)
362
363 (setq file-name (expand-file-name (find-change-log file-name)))
364
365 ;; Set ENTRY to the file name to use in the new entry.
366 (and buffer-file-name
367 ;; Never want to add a change log entry for the ChangeLog file itself.
368 (not (string= buffer-file-name file-name))
369 (if add-log-file-name-function
370 (setq entry
371 (funcall add-log-file-name-function buffer-file-name))
372 (setq entry
373 (if (string-match
374 (concat "^" (regexp-quote (file-name-directory
375 file-name)))
376 buffer-file-name)
377 (substring buffer-file-name (match-end 0))
378 (file-name-nondirectory buffer-file-name)))
379 ;; If we have a backup file, it's presumably because we're
380 ;; comparing old and new versions (e.g. for deleted
381 ;; functions) and we'll want to use the original name.
382 (if (backup-file-name-p entry)
383 (setq entry (file-name-sans-versions entry)))))
384
385 (if (and other-window (not (equal file-name buffer-file-name)))
386 (find-file-other-window file-name)
387 (find-file file-name))
388 (or (eq major-mode 'change-log-mode)
389 (change-log-mode))
390 (undo-boundary)
391 (goto-char (point-min))
392 (let ((new-entry (concat (funcall add-log-time-format)
393 " " add-log-full-name
394 " <" add-log-mailing-address ">")))
395 (if (looking-at (regexp-quote new-entry))
396 (forward-line 1)
397 (insert new-entry "\n\n")))
398
399 (setq bound
400 (progn
401 (if (looking-at "\n*[^\n* \t]")
402 (skip-chars-forward "\n")
403 (if add-log-keep-changes-together
404 (forward-page) ; page delimits entries for date
405 (forward-paragraph))) ; paragraph delimits entries for file
406 (point)))
407 (goto-char (point-min))
408 ;; Now insert the new line for this entry.
409 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t)
410 ;; Put this file name into the existing empty entry.
411 (if entry
412 (insert entry)))
413 ((and (not new-entry)
414 (let (case-fold-search)
415 (re-search-forward
416 (concat (regexp-quote (concat "* " entry))
417 ;; Don't accept `foo.bar' when
418 ;; looking for `foo':
419 "\\(\\s \\|[(),:]\\)")
420 bound t)))
421 ;; Add to the existing entry for the same file.
422 (re-search-forward "^\\s *$\\|^\\s \\*")
423 (goto-char (match-beginning 0))
424 ;; Delete excess empty lines; make just 2.
425 (while (and (not (eobp)) (looking-at "^\\s *$"))
426 (delete-region (point) (line-beginning-position 2)))
427 (insert "\n\n")
428 (forward-line -2)
429 (indent-relative-maybe))
430 (t
431 ;; Make a new entry.
432 (forward-line 1)
433 (while (looking-at "\\sW")
434 (forward-line 1))
435 (while (and (not (eobp)) (looking-at "^\\s *$"))
436 (delete-region (point) (line-beginning-position 2)))
437 (insert "\n\n\n")
438 (forward-line -2)
439 (indent-to left-margin)
440 (insert "* " (or entry ""))))
441 ;; Now insert the function name, if we have one.
442 ;; Point is at the entry for this file,
443 ;; either at the end of the line or at the first blank line.
444 (if defun
445 (progn
446 ;; Make it easy to get rid of the function name.
447 (undo-boundary)
448 (unless (save-excursion
449 (beginning-of-line 1)
450 (looking-at "\\s *$"))
451 (insert ?\ ))
452 (insert "(" defun "): ")
453 (if version
454 (insert version ?\ )))
455 ;; No function name, so put in a colon unless we have just a star.
456 (unless (save-excursion
457 (beginning-of-line 1)
458 (looking-at "\\s *\\(\\*\\s *\\)?$"))
459 (insert ": ")
460 (if version (insert version ?\ ))))))
461
462 ;;;###autoload
463 (defun add-change-log-entry-other-window (&optional whoami file-name)
464 "Find change log file in other window and add an entry for today.
465 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
466 name and site.
467 Second optional arg FILE-NAME is file name of change log.
468 If nil, use `change-log-default-name'.
469
470 Affected by the same options as `add-change-log-entry'."
471 (interactive (if current-prefix-arg
472 (list current-prefix-arg
473 (prompt-for-change-log-name))))
474 (add-change-log-entry whoami file-name t))
475 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
476
477 ;;;###autoload
478 (defun change-log-mode ()
479 "Major mode for editing change logs; like Indented Text Mode.
480 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
481 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
482 Each entry behaves as a paragraph, and the entries for one day as a page.
483 Runs `change-log-mode-hook'."
484 (interactive)
485 (kill-all-local-variables)
486 (indented-text-mode)
487 (setq major-mode 'change-log-mode
488 mode-name "Change Log"
489 left-margin 8
490 fill-column 74
491 indent-tabs-mode t
492 tab-width 8)
493 (use-local-map change-log-mode-map)
494 (set (make-local-variable 'fill-paragraph-function)
495 'change-log-fill-paragraph)
496 ;; We really do want "^" in paragraph-start below: it is only the
497 ;; lines that begin at column 0 (despite the left-margin of 8) that
498 ;; we are looking for. Adding `* ' allows eliding the blank line
499 ;; between entries for different files.
500 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
501 (set (make-local-variable 'paragraph-separate) paragraph-start)
502 ;; Match null string on the date-line so that the date-line
503 ;; is grouped with what follows.
504 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
505 (set (make-local-variable 'version-control) 'never)
506 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
507 (set (make-local-variable 'font-lock-defaults)
508 '(change-log-font-lock-keywords t))
509 (run-hooks 'change-log-mode-hook))
510
511 ;; It might be nice to have a general feature to replace this. The idea I
512 ;; have is a variable giving a regexp matching text which should not be
513 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
514 ;; But I don't feel up to implementing that today.
515 (defun change-log-fill-paragraph (&optional justify)
516 "Fill the paragraph, but preserve open parentheses at beginning of lines.
517 Prefix arg means justify as well."
518 (interactive "P")
519 (let ((end (progn (forward-paragraph) (point)))
520 (beg (progn (backward-paragraph) (point)))
521 (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
522 (fill-region beg end justify)
523 t))
524 \f
525 (defcustom add-log-current-defun-header-regexp
526 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
527 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes."
528 :type 'regexp
529 :group 'change-log)
530
531 ;;;###autoload
532 (defvar add-log-lisp-like-modes
533 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode)
534 "*Modes that look like Lisp to `add-log-current-defun'.")
535
536 ;;;###autoload
537 (defvar add-log-c-like-modes
538 '(c-mode c++-mode c++-c-mode objc-mode)
539 "*Modes that look like C to `add-log-current-defun'.")
540
541 ;;;###autoload
542 (defvar add-log-tex-like-modes
543 '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode)
544 "*Modes that look like TeX to `add-log-current-defun'.")
545
546 ;;;###autoload
547 (defun add-log-current-defun ()
548 "Return name of function definition point is in, or nil.
549
550 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
551 Texinfo (@node titles) and Perl.
552
553 Other modes are handled by a heuristic that looks in the 10K before
554 point for uppercase headings starting in the first column or
555 identifiers followed by `:' or `='. See variables
556 `add-log-current-defun-header-regexp' and
557 `add-log-current-defun-function'
558
559 Has a preference of looking backwards."
560 (condition-case nil
561 (save-excursion
562 (let ((location (point)))
563 (cond (add-log-current-defun-function
564 (funcall add-log-current-defun-function))
565 ((memq major-mode add-log-lisp-like-modes)
566 ;; If we are now precisely at the beginning of a defun,
567 ;; make sure beginning-of-defun finds that one
568 ;; rather than the previous one.
569 (or (eobp) (forward-char 1))
570 (beginning-of-defun)
571 ;; Make sure we are really inside the defun found,
572 ;; not after it.
573 (when (and (looking-at "\\s(")
574 (progn (end-of-defun)
575 (< location (point)))
576 (progn (forward-sexp -1)
577 (>= location (point))))
578 (if (looking-at "\\s(")
579 (forward-char 1))
580 ;; Skip the defining construct name, typically "defun"
581 ;; or "defvar".
582 (forward-sexp 1)
583 ;; The second element is usually a symbol being defined.
584 ;; If it is not, use the first symbol in it.
585 (skip-chars-forward " \t\n'(")
586 (buffer-substring-no-properties (point)
587 (progn (forward-sexp 1)
588 (point)))))
589 ((and (memq major-mode add-log-c-like-modes)
590 (save-excursion
591 (beginning-of-line)
592 ;; Use eq instead of = here to avoid
593 ;; error when at bob and char-after
594 ;; returns nil.
595 (while (eq (char-after (- (point) 2)) ?\\)
596 (forward-line -1))
597 (looking-at "[ \t]*#[ \t]*define[ \t]")))
598 ;; Handle a C macro definition.
599 (beginning-of-line)
600 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
601 (forward-line -1))
602 (search-forward "define")
603 (skip-chars-forward " \t")
604 (buffer-substring-no-properties (point)
605 (progn (forward-sexp 1)
606 (point))))
607 ((memq major-mode add-log-c-like-modes)
608 (beginning-of-line)
609 ;; See if we are in the beginning part of a function,
610 ;; before the open brace. If so, advance forward.
611 (while (not (looking-at "{\\|\\(\\s *$\\)"))
612 (forward-line 1))
613 (or (eobp)
614 (forward-char 1))
615 (beginning-of-defun)
616 (when (progn (end-of-defun)
617 (< location (point)))
618 (backward-sexp 1)
619 (let (beg tem)
620
621 (forward-line -1)
622 ;; Skip back over typedefs of arglist.
623 (while (and (not (bobp))
624 (looking-at "[ \t\n]"))
625 (forward-line -1))
626 ;; See if this is using the DEFUN macro used in Emacs,
627 ;; or the DEFUN macro used by the C library.
628 (if (condition-case nil
629 (and (save-excursion
630 (end-of-line)
631 (while (= (preceding-char) ?\\)
632 (end-of-line 2))
633 (backward-sexp 1)
634 (beginning-of-line)
635 (setq tem (point))
636 (looking-at "DEFUN\\b"))
637 (>= location tem))
638 (error nil))
639 (progn
640 (goto-char tem)
641 (down-list 1)
642 (if (= (char-after (point)) ?\")
643 (progn
644 (forward-sexp 1)
645 (skip-chars-forward " ,")))
646 (buffer-substring-no-properties
647 (point)
648 (progn (forward-sexp 1)
649 (point))))
650 (if (looking-at "^[+-]")
651 (change-log-get-method-definition)
652 ;; Ordinary C function syntax.
653 (setq beg (point))
654 (if (and
655 ;; Protect against "Unbalanced parens" error.
656 (condition-case nil
657 (progn
658 (down-list 1) ; into arglist
659 (backward-up-list 1)
660 (skip-chars-backward " \t")
661 t)
662 (error nil))
663 ;; Verify initial pos was after
664 ;; real start of function.
665 (save-excursion
666 (goto-char beg)
667 ;; For this purpose, include the line
668 ;; that has the decl keywords. This
669 ;; may also include some of the
670 ;; comments before the function.
671 (while (and (not (bobp))
672 (save-excursion
673 (forward-line -1)
674 (looking-at "[^\n\f]")))
675 (forward-line -1))
676 (>= location (point)))
677 ;; Consistency check: going down and up
678 ;; shouldn't take us back before BEG.
679 (> (point) beg))
680 (let (end middle)
681 ;; Don't include any final whitespace
682 ;; in the name we use.
683 (skip-chars-backward " \t\n")
684 (setq end (point))
685 (backward-sexp 1)
686 ;; Now find the right beginning of the name.
687 ;; Include certain keywords if they
688 ;; precede the name.
689 (setq middle (point))
690 (forward-word -1)
691 ;; Ignore these subparts of a class decl
692 ;; and move back to the class name itself.
693 (while (looking-at "public \\|private ")
694 (skip-chars-backward " \t:")
695 (setq end (point))
696 (backward-sexp 1)
697 (setq middle (point))
698 (forward-word -1))
699 (and (bolp)
700 (looking-at
701 "enum \\|struct \\|union \\|class ")
702 (setq middle (point)))
703 (goto-char end)
704 (when (eq (preceding-char) ?=)
705 (forward-char -1)
706 (skip-chars-backward " \t")
707 (setq end (point)))
708 (buffer-substring-no-properties
709 middle end))))))))
710 ((memq major-mode add-log-tex-like-modes)
711 (if (re-search-backward
712 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
713 nil t)
714 (progn
715 (goto-char (match-beginning 0))
716 (buffer-substring-no-properties
717 (1+ (point)) ; without initial backslash
718 (line-end-position)))))
719 ((eq major-mode 'texinfo-mode)
720 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
721 (match-string-no-properties 1)))
722 ((eq major-mode 'perl-mode)
723 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t)
724 (match-string-no-properties 1)))
725 ;; Emacs's autoconf-mode installs its own
726 ;; `add-log-current-defun-function'. This applies to
727 ;; a different mode apparently for editing .m4
728 ;; autoconf source.
729 ((eq major-mode 'autoconf-mode)
730 (if (re-search-backward
731 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t)
732 (match-string-no-properties 3)))
733 (t
734 ;; If all else fails, try heuristics
735 (let (case-fold-search
736 result)
737 (end-of-line)
738 (when (re-search-backward
739 add-log-current-defun-header-regexp
740 (- (point) 10000)
741 t)
742 (setq result (or (match-string-no-properties 1)
743 (match-string-no-properties 0)))
744 ;; Strip whitespace away
745 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
746 result)
747 (setq result (match-string-no-properties 1 result)))
748 result))))))
749 (error nil)))
750
751 (defvar change-log-get-method-definition-md)
752
753 ;; Subroutine used within change-log-get-method-definition.
754 ;; Add the last match in the buffer to the end of `md',
755 ;; followed by the string END; move to the end of that match.
756 (defun change-log-get-method-definition-1 (end)
757 (setq change-log-get-method-definition-md
758 (concat change-log-get-method-definition-md
759 (match-string 1)
760 end))
761 (goto-char (match-end 0)))
762
763 (defun change-log-get-method-definition ()
764 "For objective C, return the method name if we are in a method."
765 (let ((change-log-get-method-definition-md "["))
766 (save-excursion
767 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
768 (change-log-get-method-definition-1 " ")))
769 (save-excursion
770 (cond
771 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
772 (change-log-get-method-definition-1 "")
773 (while (not (looking-at "[{;]"))
774 (looking-at
775 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
776 (change-log-get-method-definition-1 ""))
777 (concat change-log-get-method-definition-md "]"))))))
778 \f
779 (defun change-log-sortable-date-at ()
780 "Return date of log entry in a consistent form for sorting.
781 Point is assumed to be at the start of the entry."
782 (require 'timezone)
783 (if (looking-at "^\\sw.........[0-9:+ ]*")
784 (let ((date (match-string-no-properties 0)))
785 (if date
786 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
787 (concat (match-string 1 date) (match-string 2 date)
788 (match-string 3 date))
789 (condition-case nil
790 (timezone-make-date-sortable date)
791 (error nil)))))
792 (error "Bad date")))
793
794 ;;;###autoload
795 (defun change-log-merge (other-log)
796 "Merge the contents of ChangeLog file OTHER-LOG with this buffer.
797 Both must be found in Change Log mode (since the merging depends on
798 the appropriate motion commands).
799
800 Entries are inserted in chronological order.
801
802 Both the current and old-style time formats for entries are supported,
803 so this command could be used to convert old-style logs by merging
804 with an empty log."
805 (interactive "*fLog file name to merge: ")
806 (if (not (eq major-mode 'change-log-mode))
807 (error "Not in Change Log mode"))
808 (let ((other-buf (find-file-noselect other-log))
809 (buf (current-buffer))
810 date1 start end)
811 (save-excursion
812 (goto-char (point-min))
813 (set-buffer other-buf)
814 (goto-char (point-min))
815 (if (not (eq major-mode 'change-log-mode))
816 (error "%s not found in Change Log mode" other-log))
817 ;; Loop through all the entries in OTHER-LOG.
818 (while (not (eobp))
819 (setq date1 (change-log-sortable-date-at))
820 (setq start (point)
821 end (progn (forward-page) (point)))
822 ;; Look for an entry in original buffer that isn't later.
823 (with-current-buffer buf
824 (while (and (not (eobp))
825 (string< date1 (change-log-sortable-date-at)))
826 (forward-page))
827 (if (not (eobp))
828 (insert-buffer-substring other-buf start end)
829 ;; At the end of the original buffer, insert a newline to
830 ;; separate entries and then the rest of the file being
831 ;; merged. Move to the end of it to terminate outer loop.
832 (insert "\n")
833 (insert-buffer-substring other-buf start
834 (with-current-buffer other-buf
835 (goto-char (point-max))
836 (point)))))))))
837
838 (provide 'add-log)
839
840 ;;; add-log.el ends here