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