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