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