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