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