1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1988, 1993, 1994, 1997, 1998, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
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)
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.
28 ;; This facility is documented in the Emacs Manual.
35 (defgroup change-log nil
36 "Change log maintenance."
38 :link
'(custom-manual "(emacs)Change 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
)
48 (put 'change-log-default-name
'safe-local-variable
'string-or-null-p
)
50 (defcustom change-log-mode-hook nil
51 "Normal hook run by `change-log-mode'."
55 ;; Many modes set this variable, so avoid warnings.
57 (defcustom add-log-current-defun-function nil
58 "If non-nil, function to guess name of surrounding function.
59 It is used by `add-log-current-defun' in preference to built-in rules.
60 Returns function's name as a string, or nil if outside a function."
61 :type
'(choice (const nil
) function
)
65 (defcustom add-log-full-name nil
66 "Full name of user, for inclusion in ChangeLog daily headers.
67 This defaults to the value returned by the function `user-full-name'."
68 :type
'(choice (const :tag
"Default" nil
)
73 (defcustom add-log-mailing-address nil
74 "Email addresses of user, for inclusion in ChangeLog headers.
75 This defaults to the value of `user-mail-address'. In addition to
76 being a simple string, this value can also be a list. All elements
77 will be recognized as referring to the same user; when creating a new
78 ChangeLog entry, one element will be chosen at random."
79 :type
'(choice (const :tag
"Default" nil
)
80 (string :tag
"String")
81 (repeat :tag
"List of Strings" string
))
84 (defcustom add-log-time-format
'add-log-iso8601-time-string
85 "Function that defines the time format.
86 For example, `add-log-iso8601-time-string', which gives the
87 date in international ISO 8601 format,
88 and `current-time-string' are two valid values."
89 :type
'(radio (const :tag
"International ISO 8601 format"
90 add-log-iso8601-time-string
)
91 (const :tag
"Old format, as returned by `current-time-string'"
93 (function :tag
"Other"))
96 (defcustom add-log-keep-changes-together nil
97 "If non-nil, normally keep day's log entries for one file together.
99 Log entries for a given file made with \\[add-change-log-entry] or
100 \\[add-change-log-entry-other-window] will only be added to others \
102 today if this variable is non-nil or that file comes first in today's
103 entries. Otherwise another entry for that file will be started. An
107 * bar (...): change 1
109 in the latter case, \\[add-change-log-entry-other-window] in a \
110 buffer visiting `bar', yields:
114 * bar (...): change 1
119 * bar (...): change 1
122 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
128 (defcustom add-log-always-start-new-record nil
129 "If non-nil, `add-change-log-entry' will always start a new record."
134 (defcustom add-log-buffer-file-name-function nil
135 "If non-nil, function to call to identify the full filename of a buffer.
136 This function is called with no argument. If this is nil, the default is to
137 use `buffer-file-name'."
138 :type
'(choice (const nil
) function
)
141 (defcustom add-log-file-name-function nil
142 "If non-nil, function to call to identify the filename for a ChangeLog entry.
143 This function is called with one argument, the value of variable
144 `buffer-file-name' in that buffer. If this is nil, the default is to
145 use the file's name relative to the directory of the change log file."
146 :type
'(choice (const nil
) function
)
150 (defcustom change-log-version-info-enabled nil
151 "If non-nil, enable recording version numbers with the changes."
156 (defcustom change-log-version-number-regexp-list
157 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
159 ;; (defconst ad-version "2.15"
160 (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re
)
161 ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
162 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re
)))
163 "List of regexps to search for version number.
164 The version number must be in group 1.
165 Note: The search is conducted only within 10%, at the beginning of the file."
167 :type
'(repeat regexp
)
170 (defface change-log-date
171 '((t (:inherit font-lock-string-face
)))
172 "Face used to highlight dates in date lines."
175 ;; backward-compatibility alias
176 (put 'change-log-date-face
'face-alias
'change-log-date
)
178 (defface change-log-name
179 '((t (:inherit font-lock-constant-face
)))
180 "Face for highlighting author names."
183 ;; backward-compatibility alias
184 (put 'change-log-name-face
'face-alias
'change-log-name
)
186 (defface change-log-email
187 '((t (:inherit font-lock-variable-name-face
)))
188 "Face for highlighting author email addresses."
191 ;; backward-compatibility alias
192 (put 'change-log-email-face
'face-alias
'change-log-email
)
194 (defface change-log-file
195 '((t (:inherit font-lock-function-name-face
)))
196 "Face for highlighting file names."
199 ;; backward-compatibility alias
200 (put 'change-log-file-face
'face-alias
'change-log-file
)
202 (defface change-log-list
203 '((t (:inherit font-lock-keyword-face
)))
204 "Face for highlighting parenthesized lists of functions or variables."
207 ;; backward-compatibility alias
208 (put 'change-log-list-face
'face-alias
'change-log-list
)
210 (defface change-log-conditionals
211 '((t (:inherit font-lock-variable-name-face
)))
212 "Face for highlighting conditionals of the form `[...]'."
215 ;; backward-compatibility alias
216 (put 'change-log-conditionals-face
'face-alias
'change-log-conditionals
)
218 (defface change-log-function
219 '((t (:inherit font-lock-variable-name-face
)))
220 "Face for highlighting items of the form `<....>'."
223 ;; backward-compatibility alias
224 (put 'change-log-function-face
'face-alias
'change-log-function
)
226 (defface change-log-acknowledgement
227 '((t (:inherit font-lock-comment-face
)))
228 "Face for highlighting acknowledgments."
231 ;; backward-compatibility alias
232 (put 'change-log-acknowledgement-face
'face-alias
'change-log-acknowledgement
)
234 (defvar change-log-font-lock-keywords
236 ;; Date lines, new (2000-01-01) and old (Sat Jan 1 00:00:00 2000) styles.
237 ;; Fixme: this regepx is just an approximate one and may match
238 ;; wrongly with a non-date line existing as a random note. In
239 ;; addition, using any kind of fixed setting like this doesn't
240 ;; work if a user customizes add-log-time-format.
241 ("^[0-9-]+ +\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+"
242 (0 'change-log-date-face
)
243 ;; Name and e-mail; some people put e-mail in parens, not angles.
244 ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
246 (2 'change-log-email
)))
249 ("^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)"
251 ;; Possibly further names in a list:
252 ("\\=, \\([^ ,:([\n]+\\)" nil nil
(1 'change-log-file
))
253 ;; Possibly a parenthesized list of names:
254 ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
255 nil nil
(1 'change-log-list
))
256 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
257 nil nil
(1 'change-log-list
)))
259 ;; Function or variable names.
260 ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
262 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
263 (1 'change-log-list
)))
266 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals
))
268 ;; Function of change.
269 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function
))
272 ;; Don't include plain "From" because that is vague;
273 ;; we want to encourage people to say something more specific.
274 ;; Note that the FSF does not use "Patches by"; our convention
275 ;; is to put the name of the author of the changes at the top
276 ;; of the change log entry.
277 ("\\(^\\( +\\|\t\\)\\| \\)\\(Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
278 3 'change-log-acknowledgement
))
279 "Additional expressions to highlight in Change Log mode.")
281 (defvar change-log-mode-map
282 (let ((map (make-sparse-keymap)))
283 (define-key map
[?\C-c ?\C-p
] 'add-log-edit-prev-comment
)
284 (define-key map
[?\C-c ?\C-n
] 'add-log-edit-next-comment
)
286 "Keymap for Change Log major mode.")
288 ;; It used to be called change-log-time-zone-rule but really should be
289 ;; called add-log-time-zone-rule since it's only used from add-log-* code.
290 (defvaralias 'change-log-time-zone-rule
'add-log-time-zone-rule
)
291 (defvar add-log-time-zone-rule nil
292 "Time zone used for calculating change log time stamps.
293 It takes the same format as the TZ argument of `set-time-zone-rule'.
294 If nil, use local time.
295 If t, use universal time.")
296 (put 'add-log-time-zone-rule
'safe-local-variable
297 '(lambda (x) (or (booleanp x
) (stringp x
))))
299 (defun add-log-iso8601-time-zone (&optional time
)
300 (let* ((utc-offset (or (car (current-time-zone time
)) 0))
301 (sign (if (< utc-offset
0) ?- ?
+))
302 (sec (abs utc-offset
))
307 (format (cond ((not (zerop ss
)) "%c%02d:%02d:%02d")
308 ((not (zerop mm
)) "%c%02d:%02d")
312 (defvar add-log-iso8601-with-time-zone nil
)
314 (defun add-log-iso8601-time-string ()
315 (let ((time (format-time-string "%Y-%m-%d"
316 nil
(eq t add-log-time-zone-rule
))))
317 (if add-log-iso8601-with-time-zone
318 (concat time
" " (add-log-iso8601-time-zone))
321 (defun change-log-name ()
322 "Return (system-dependent) default name for a change log file."
323 (or change-log-default-name
324 (if (eq system-type
'vax-vms
)
328 (defun add-log-edit-prev-comment (arg)
329 "Cycle backward through Log-Edit mode comment history.
330 With a numeric prefix ARG, go back ARG comments."
333 (narrow-to-region (point)
334 (if (memq last-command
'(add-log-edit-prev-comment
335 add-log-edit-next-comment
))
337 (when (fboundp 'log-edit-previous-comment
)
338 (log-edit-previous-comment arg
)
339 (indent-region (point-min) (point-max))
340 (goto-char (point-min))
341 (unless (save-restriction (widen) (bolp))
342 (delete-region (point) (progn (skip-chars-forward " \t\n") (point))))
343 (set-mark (point-min))
344 (goto-char (point-max))
345 (delete-region (point) (progn (skip-chars-backward " \t\n") (point))))))
347 (defun add-log-edit-next-comment (arg)
348 "Cycle forward through Log-Edit mode comment history.
349 With a numeric prefix ARG, go back ARG comments."
351 (add-log-edit-prev-comment (- arg
)))
354 (defun prompt-for-change-log-name ()
355 "Prompt for a change log name."
356 (let* ((default (change-log-name))
357 (name (expand-file-name
358 (read-file-name (format "Log file (default %s): " default
)
360 ;; Handle something that is syntactically a directory name.
361 ;; Look for ChangeLog or whatever in that directory.
362 (if (string= (file-name-nondirectory name
) "")
363 (expand-file-name (file-name-nondirectory default
)
365 ;; Handle specifying a file that is a directory.
366 (if (file-directory-p name
)
367 (expand-file-name (file-name-nondirectory default
)
368 (file-name-as-directory name
))
371 (defun change-log-version-number-search ()
372 "Return version number of current buffer's file.
373 This is the value returned by `vc-workfile-version' or, if that is
374 nil, by matching `change-log-version-number-regexp-list'."
375 (let* ((size (buffer-size))
377 ;; The version number can be anywhere in the file, but
378 ;; restrict search to the file beginning: 10% should be
379 ;; enough to prevent some mishits.
381 ;; Apply percentage only if buffer size is bigger than
383 (if (> size
(* 100 80)) (+ (point) (/ size
10)))))
384 (or (and buffer-file-name
(vc-workfile-version buffer-file-name
))
387 (let ((regexps change-log-version-number-regexp-list
)
391 (goto-char (point-min))
392 (when (re-search-forward (pop regexps
) limit t
)
393 (setq version
(match-string 1)
399 (defun find-change-log (&optional file-name buffer-file
)
400 "Find a change log file for \\[add-change-log-entry] and return the name.
402 Optional arg FILE-NAME specifies the file to use.
403 If FILE-NAME is nil, use the value of `change-log-default-name'.
404 If `change-log-default-name' is nil, behave as though it were 'ChangeLog'
405 \(or whatever we use on this operating system).
407 If `change-log-default-name' contains a leading directory component, then
408 simply find it in the current directory. Otherwise, search in the current
409 directory and its successive parents for a file so named.
411 Once a file is found, `change-log-default-name' is set locally in the
412 current buffer to the complete file name.
413 Optional arg BUFFER-FILE overrides `buffer-file-name'."
414 ;; If user specified a file name or if this buffer knows which one to use,
417 (setq file-name
(and change-log-default-name
418 (file-name-directory change-log-default-name
)
419 change-log-default-name
))
421 ;; Chase links in the source file
422 ;; and use the change log in the dir where it points.
423 (setq file-name
(or (and (or buffer-file buffer-file-name
)
426 (or buffer-file buffer-file-name
))))
428 (if (file-directory-p file-name
)
429 (setq file-name
(expand-file-name (change-log-name) file-name
)))
430 ;; Chase links before visiting the file.
431 ;; This makes it easier to use a single change log file
432 ;; for several related directories.
433 (setq file-name
(file-chase-links file-name
))
434 (setq file-name
(expand-file-name file-name
))
435 ;; Move up in the dir hierarchy till we find a change log file.
436 (let ((file1 file-name
)
438 (while (and (not (or (get-file-buffer file1
) (file-exists-p file1
)))
439 (progn (setq parent-dir
442 (file-name-directory file1
))))
443 ;; Give up if we are already at the root dir.
444 (not (string= (file-name-directory file1
)
446 ;; Move up to the parent dir and try again.
447 (setq file1
(expand-file-name
448 (file-name-nondirectory (change-log-name))
450 ;; If we found a change log in a parent, use that.
451 (if (or (get-file-buffer file1
) (file-exists-p file1
))
452 (setq file-name file1
)))))
453 ;; Make a local variable in this buffer so we needn't search again.
454 (set (make-local-variable 'change-log-default-name
) file-name
)
457 (defun add-log-file-name (buffer-file log-file
)
458 ;; Never want to add a change log entry for the ChangeLog file itself.
459 (unless (or (null buffer-file
) (string= buffer-file log-file
))
460 (if add-log-file-name-function
461 (funcall add-log-file-name-function buffer-file
)
463 (file-relative-name buffer-file
(file-name-directory log-file
)))
464 ;; If we have a backup file, it's presumably because we're
465 ;; comparing old and new versions (e.g. for deleted
466 ;; functions) and we'll want to use the original name.
467 (if (backup-file-name-p buffer-file
)
468 (file-name-sans-versions buffer-file
)
472 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
)
473 "Find change log file, and add an entry for today and an item for this file.
474 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
475 name and email (stored in `add-log-full-name' and `add-log-mailing-address').
477 Second arg FILE-NAME is file name of the change log.
478 If nil, use the value of `change-log-default-name'.
480 Third arg OTHER-WINDOW non-nil means visit in other window.
482 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
483 never append to an existing entry. Option `add-log-keep-changes-together'
484 otherwise affects whether a new entry is created.
486 Option `add-log-always-start-new-record' non-nil means always create a
487 new record, even when the last record was made on the same date and by
490 The change log file can start with a copyright notice and a copying
491 permission notice. The first blank line indicates the end of these
494 Today's date is calculated according to `add-log-time-zone-rule' if
495 non-nil, otherwise in local time."
496 (interactive (list current-prefix-arg
497 (prompt-for-change-log-name)))
498 (let* ((defun (add-log-current-defun))
499 (version (and change-log-version-info-enabled
500 (change-log-version-number-search)))
501 (buf-file-name (if add-log-buffer-file-name-function
502 (funcall add-log-buffer-file-name-function
)
504 (buffer-file (if buf-file-name
(expand-file-name buf-file-name
)))
505 (file-name (expand-file-name (find-change-log file-name buffer-file
)))
506 ;; Set ITEM to the file name to use in the new item.
507 (item (add-log-file-name buffer-file file-name
)))
509 (unless (equal file-name buffer-file-name
)
510 (if (or other-window
(window-dedicated-p (selected-window)))
511 (find-file-other-window file-name
)
512 (find-file file-name
)))
513 (or (derived-mode-p 'change-log-mode
)
516 (goto-char (point-min))
518 (let ((full-name (or add-log-full-name
(user-full-name)))
519 (mailing-address (or add-log-mailing-address user-mail-address
)))
522 (setq full-name
(read-string "Full name: "
523 (or add-log-full-name
(user-full-name))))
524 ;; Note that some sites have room and phone number fields in
525 ;; full name which look silly when inserted. Rather than do
526 ;; anything about that here, let user give prefix argument so that
527 ;; s/he can edit the full name field in prompter if s/he wants.
528 (setq mailing-address
529 (read-string "Mailing address: "
530 (or add-log-mailing-address user-mail-address
))))
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.
540 (mapcar (lambda (addr)
542 (if (stringp add-log-time-zone-rule
)
543 (let ((tz (getenv "TZ")))
546 (set-time-zone-rule add-log-time-zone-rule
)
547 (funcall add-log-time-format
))
548 (set-time-zone-rule tz
)))
549 (funcall add-log-time-format
))
552 (if (consp mailing-address
)
554 (list mailing-address
)))))
555 (if (and (not add-log-always-start-new-record
)
557 (dolist (entry new-entries hit
)
558 (when (looking-at (regexp-quote entry
))
561 (insert (nth (random (length new-entries
))
563 (if use-hard-newlines hard-newline
"\n")
564 (if use-hard-newlines hard-newline
"\n"))
567 ;; Determine where we should stop searching for a usable
568 ;; item to add to, within this entry.
571 (if (looking-at "\n*[^\n* \t]")
572 (skip-chars-forward "\n")
573 (if add-log-keep-changes-together
574 (forward-page) ; page delimits entries for date
575 (forward-paragraph))) ; paragraph delimits entries for file
578 ;; Now insert the new line for this item.
579 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t
)
580 ;; Put this file name into the existing empty item.
583 ((and (not new-entry
)
584 (let (case-fold-search)
586 (concat (regexp-quote (concat "* " item
))
587 ;; Don't accept `foo.bar' when
588 ;; looking for `foo':
589 "\\(\\s \\|[(),:]\\)")
591 ;; Add to the existing item for the same file.
592 (re-search-forward "^\\s *$\\|^\\s \\*")
593 (goto-char (match-beginning 0))
594 ;; Delete excess empty lines; make just 2.
595 (while (and (not (eobp)) (looking-at "^\\s *$"))
596 (delete-region (point) (line-beginning-position 2)))
597 (insert (if use-hard-newlines hard-newline
"\n")
598 (if use-hard-newlines hard-newline
"\n"))
600 (indent-relative-maybe))
603 (while (looking-at "\\sW")
605 (while (and (not (eobp)) (looking-at "^\\s *$"))
606 (delete-region (point) (line-beginning-position 2)))
607 (insert (if use-hard-newlines hard-newline
"\n")
608 (if use-hard-newlines hard-newline
"\n")
609 (if use-hard-newlines hard-newline
"\n"))
611 (indent-to left-margin
)
613 (if item
(insert item
)))))
614 ;; Now insert the function name, if we have one.
615 ;; Point is at the item for this file,
616 ;; either at the end of the line or at the first blank line.
618 ;; No function name, so put in a colon unless we have just a star.
619 (unless (save-excursion
620 (beginning-of-line 1)
621 (looking-at "\\s *\\(\\*\\s *\\)?$"))
623 (if version
(insert version ?\s
)))
624 ;; Make it easy to get rid of the function name.
626 (unless (save-excursion
627 (beginning-of-line 1)
628 (looking-at "\\s *$"))
630 ;; See if the prev function name has a message yet or not.
631 ;; If not, merge the two items.
632 (let ((pos (point-marker)))
633 (skip-syntax-backward " ")
634 (skip-chars-backward "):")
635 (if (and (looking-at "):")
636 (let ((pos (save-excursion (backward-sexp 1) (point))))
637 (when (equal (buffer-substring pos
(point)) defun
)
638 (delete-region pos
(point)))
639 (> fill-column
(+ (current-column) (length defun
) 4))))
640 (progn (skip-chars-backward ", ")
641 (delete-region (point) pos
)
642 (unless (memq (char-before) '(?\
()) (insert ", ")))
643 (if (looking-at "):")
644 (delete-region (+ 1 (point)) (line-end-position)))
647 (set-marker pos nil
))
649 (if version
(insert version ?\s
)))))
652 (defun add-change-log-entry-other-window (&optional whoami file-name
)
653 "Find change log file in other window and add entry and item.
654 This is just like `add-change-log-entry' except that it displays
655 the change log file in another window."
656 (interactive (if current-prefix-arg
657 (list current-prefix-arg
658 (prompt-for-change-log-name))))
659 (add-change-log-entry whoami file-name t
))
660 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
662 (defvar change-log-indent-text
0)
664 (defun change-log-indent ()
668 (skip-chars-forward " \t")
670 ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
671 ;; Matching the output of add-log-time-format is difficult,
672 ;; but I'll get it has at least two adjacent digits.
673 (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
675 ((looking-at "[^*(]")
676 (+ (current-left-margin) change-log-indent-text
))
677 (t (current-left-margin)))))
678 (pos (save-excursion (indent-line-to indent
) (point))))
679 (if (> pos
(point)) (goto-char pos
))))
682 (defvar smerge-resolve-function
)
685 (define-derived-mode change-log-mode text-mode
"Change Log"
686 "Major mode for editing change logs; like Indented Text Mode.
687 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
688 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
689 Each entry behaves as a paragraph, and the entries for one day as a page.
690 Runs `change-log-mode-hook'.
691 \\{change-log-mode-map}"
696 show-trailing-whitespace t
)
697 (set (make-local-variable 'fill-paragraph-function
)
698 'change-log-fill-paragraph
)
699 ;; Avoid that filling leaves behind a single "*" on a line.
700 (add-hook 'fill-nobreak-predicate
702 (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
704 (set (make-local-variable 'indent-line-function
) 'change-log-indent
)
705 (set (make-local-variable 'tab-always-indent
) nil
)
706 ;; We really do want "^" in paragraph-start below: it is only the
707 ;; lines that begin at column 0 (despite the left-margin of 8) that
708 ;; we are looking for. Adding `* ' allows eliding the blank line
709 ;; between entries for different files.
710 (set (make-local-variable 'paragraph-start
) "\\s *$\\|\f\\|^\\<")
711 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
712 ;; Match null string on the date-line so that the date-line
713 ;; is grouped with what follows.
714 (set (make-local-variable 'page-delimiter
) "^\\<\\|^\f")
715 (set (make-local-variable 'version-control
) 'never
)
716 (set (make-local-variable 'smerge-resolve-function
)
717 'change-log-resolve-conflict
)
718 (set (make-local-variable 'adaptive-fill-regexp
) "\\s *")
719 (set (make-local-variable 'font-lock-defaults
)
720 '(change-log-font-lock-keywords t nil nil backward-paragraph
)))
722 ;; It might be nice to have a general feature to replace this. The idea I
723 ;; have is a variable giving a regexp matching text which should not be
724 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
725 ;; But I don't feel up to implementing that today.
726 (defun change-log-fill-paragraph (&optional justify
)
727 "Fill the paragraph, but preserve open parentheses at beginning of lines.
728 Prefix arg means justify as well."
730 (let ((end (progn (forward-paragraph) (point)))
731 (beg (progn (backward-paragraph) (point)))
732 (paragraph-start (concat paragraph-start
"\\|\\s *\\s(")))
733 (fill-region beg end justify
)
736 (defcustom add-log-current-defun-header-regexp
737 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
738 "Heuristic regexp used by `add-log-current-defun' for unknown major modes."
743 (defvar add-log-lisp-like-modes
744 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode
)
745 "*Modes that look like Lisp to `add-log-current-defun'.")
748 (defvar add-log-c-like-modes
749 '(c-mode c
++-mode c
++-c-mode objc-mode
)
750 "*Modes that look like C to `add-log-current-defun'.")
753 (defvar add-log-tex-like-modes
754 '(TeX-mode plain-TeX-mode LaTeX-mode tex-mode
)
755 "*Modes that look like TeX to `add-log-current-defun'.")
758 (defun add-log-current-defun ()
759 "Return name of function definition point is in, or nil.
761 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
762 Texinfo (@node titles) and Perl.
764 Other modes are handled by a heuristic that looks in the 10K before
765 point for uppercase headings starting in the first column or
766 identifiers followed by `:' or `='. See variables
767 `add-log-current-defun-header-regexp' and
768 `add-log-current-defun-function'.
770 Has a preference of looking backwards."
773 (let ((location (point)))
774 (cond (add-log-current-defun-function
775 (funcall add-log-current-defun-function
))
776 ((apply 'derived-mode-p add-log-lisp-like-modes
)
777 ;; If we are now precisely at the beginning of a defun,
778 ;; make sure beginning-of-defun finds that one
779 ;; rather than the previous one.
780 (or (eobp) (forward-char 1))
782 ;; Make sure we are really inside the defun found,
784 (when (and (looking-at "\\s(")
785 (progn (end-of-defun)
786 (< location
(point)))
787 (progn (forward-sexp -
1)
788 (>= location
(point))))
789 (if (looking-at "\\s(")
791 ;; Skip the defining construct name, typically "defun"
794 ;; The second element is usually a symbol being defined.
795 ;; If it is not, use the first symbol in it.
796 (skip-chars-forward " \t\n'(")
797 (buffer-substring-no-properties (point)
798 (progn (forward-sexp 1)
800 ((and (apply 'derived-mode-p add-log-c-like-modes
)
803 ;; Use eq instead of = here to avoid
804 ;; error when at bob and char-after
806 (while (eq (char-after (- (point) 2)) ?
\\)
808 (looking-at "[ \t]*#[ \t]*define[ \t]")))
809 ;; Handle a C macro definition.
811 (while (eq (char-after (- (point) 2)) ?
\\) ;not =; note above
813 (search-forward "define")
814 (skip-chars-forward " \t")
815 (buffer-substring-no-properties (point)
816 (progn (forward-sexp 1)
818 ((apply 'derived-mode-p add-log-c-like-modes
)
819 ;; See whether the point is inside a defun.
820 (let (having-previous-defun
823 next-defun-beginning
)
826 (setq having-previous-defun
827 (c-beginning-of-defun))
829 ;; `c-end-of-defun' moves point to the line after
830 ;; the function close, but the position we prefer
831 ;; here is the position after the final }.
834 ;; Skip the semicolon ``;'' for
835 ;; enum/union/struct/class definition.
836 (if (= (char-after (point)) ?\
;)
838 (setq previous-defun-end
(point)))
841 (setq having-next-defun
843 (c-beginning-of-defun)
844 (setq next-defun-beginning
(point)))
846 (if (and having-next-defun
847 (< location next-defun-beginning
))
848 (skip-syntax-forward " "))
849 (if (and having-previous-defun
850 (> location previous-defun-end
))
851 (skip-syntax-backward " "))
853 ;; When there is no previous defun, the
854 ;; point is not in a defun if it is not at
855 ;; the beginning of the next defun.
856 (and (not having-previous-defun
)
858 next-defun-beginning
)))
859 ;; When there is no next defun, the point
860 ;; is not in a defun if it is not at the
861 ;; end of the previous defun.
862 (and (not having-next-defun
)
864 previous-defun-end
)))
865 ;; If the point is between two defuns, it
866 ;; is not in a defun.
867 (and (> (point) previous-defun-end
)
868 (< (point) next-defun-beginning
)))
869 ;; If the point is already at the beginning of a
870 ;; defun, there is no need to move point again.
871 (if (not (= (point) next-defun-beginning
))
872 (c-beginning-of-defun))
873 ;; Is this a DEFUN construct? And is LOCATION in it?
874 (if (and (looking-at "DEFUN\\b")
875 (>= location
(point)))
876 ;; DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, ...) ==> Ffile_name_directory
877 ;; DEFUN(POSIX::STREAM-LOCK, stream lockp &key BLOCK SHARED START LENGTH) ==> POSIX::STREAM-LOCK
880 (when (= (char-after (point)) ?
\")
882 (search-forward ","))
883 (skip-syntax-forward " ")
884 (buffer-substring-no-properties
886 (progn (search-forward ",")
888 (skip-syntax-backward " ")
890 (if (looking-at "^[+-]")
892 (change-log-get-method-definition)
893 ;; Ordinary C function syntax.
896 ;; Protect against "Unbalanced parens" error.
899 (down-list 1) ; into arglist
901 (skip-chars-backward " \t")
904 ;; Verify initial pos was after
905 ;; real start of function.
908 ;; For this purpose, include the line
909 ;; that has the decl keywords. This
910 ;; may also include some of the
911 ;; comments before the function.
912 (while (and (not (bobp))
915 (looking-at "[^\n\f]")))
917 (>= location
(point)))
918 ;; Consistency check: going down and up
919 ;; shouldn't take us back before BEG.
922 ;; Don't include any final whitespace
923 ;; in the name we use.
924 (skip-chars-backward " \t\n")
927 ;; Now find the right beginning of the name.
928 ;; Include certain keywords if they
930 (setq middle
(point))
931 ;; We tried calling `forward-sexp' in a loop
932 ;; but it causes inconsistency for C names.
934 ;; Is this C++ method?
935 (when (and (< 2 middle
)
936 (string= (buffer-substring (- middle
2)
939 ;; Include "classname::".
940 (setq middle
(point)))
941 ;; Ignore these subparts of a class decl
942 ;; and move back to the class name itself.
943 (while (looking-at "public \\|private ")
944 (skip-chars-backward " \t:")
947 (setq middle
(point))
951 "enum \\|struct \\|union \\|class ")
952 (setq middle
(point)))
954 (when (eq (preceding-char) ?
=)
956 (skip-chars-backward " \t")
958 (buffer-substring-no-properties
960 ((apply 'derived-mode-p add-log-tex-like-modes
)
961 (if (re-search-backward
962 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
965 (goto-char (match-beginning 0))
966 (buffer-substring-no-properties
967 (1+ (point)) ; without initial backslash
968 (line-end-position)))))
969 ((derived-mode-p 'texinfo-mode
)
970 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t
)
971 (match-string-no-properties 1)))
972 ((derived-mode-p '(perl-mode cperl-mode
))
973 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t
)
974 (match-string-no-properties 1)))
975 ;; Emacs's autoconf-mode installs its own
976 ;; `add-log-current-defun-function'. This applies to
977 ;; a different mode apparently for editing .m4
979 ((derived-mode-p 'autoconf-mode
)
980 (if (re-search-backward
981 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t
)
982 (match-string-no-properties 3)))
984 ;; If all else fails, try heuristics
985 (let (case-fold-search
988 (when (re-search-backward
989 add-log-current-defun-header-regexp
992 (setq result
(or (match-string-no-properties 1)
993 (match-string-no-properties 0)))
994 ;; Strip whitespace away
995 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
997 (setq result
(match-string-no-properties 1 result
)))
1001 (defvar change-log-get-method-definition-md
)
1003 ;; Subroutine used within change-log-get-method-definition.
1004 ;; Add the last match in the buffer to the end of `md',
1005 ;; followed by the string END; move to the end of that match.
1006 (defun change-log-get-method-definition-1 (end)
1007 (setq change-log-get-method-definition-md
1008 (concat change-log-get-method-definition-md
1011 (goto-char (match-end 0)))
1013 (defun change-log-get-method-definition ()
1014 "For Objective C, return the method name if we are in a method."
1015 (let ((change-log-get-method-definition-md "["))
1017 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t
)
1018 (change-log-get-method-definition-1 " ")))
1021 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t
)
1022 (change-log-get-method-definition-1 "")
1023 (while (not (looking-at "[{;]"))
1025 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
1026 (change-log-get-method-definition-1 ""))
1027 (concat change-log-get-method-definition-md
"]"))))))
1029 (defun change-log-sortable-date-at ()
1030 "Return date of log entry in a consistent form for sorting.
1031 Point is assumed to be at the start of the entry."
1033 (if (looking-at "^\\sw.........[0-9:+ ]*")
1034 (let ((date (match-string-no-properties 0)))
1036 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date
)
1037 (concat (match-string 1 date
) (match-string 2 date
)
1038 (match-string 3 date
))
1040 (timezone-make-date-sortable date
)
1042 (error "Bad date")))
1044 (defun change-log-resolve-conflict ()
1045 "Function to be used in `smerge-resolve-function'."
1046 (let ((buf (current-buffer)))
1048 (insert-buffer-substring buf
(match-beginning 1) (match-end 1))
1049 (save-match-data (change-log-mode))
1050 (let ((other-buf (current-buffer)))
1051 (with-current-buffer buf
1054 (narrow-to-region (match-beginning 0) (match-end 0))
1055 (replace-match (match-string 3) t t
)
1056 (change-log-merge other-buf
))))))))
1059 (defun change-log-merge (other-log)
1060 "Merge the contents of change log file OTHER-LOG with this buffer.
1061 Both must be found in Change Log mode (since the merging depends on
1062 the appropriate motion commands). OTHER-LOG can be either a file name
1065 Entries are inserted in chronological order. Both the current and
1066 old-style time formats for entries are supported."
1067 (interactive "*fLog file name to merge: ")
1068 (if (not (derived-mode-p 'change-log-mode
))
1069 (error "Not in Change Log mode"))
1070 (let ((other-buf (if (bufferp other-log
) other-log
1071 (find-file-noselect other-log
)))
1072 (buf (current-buffer))
1075 (goto-char (point-min))
1076 (set-buffer other-buf
)
1077 (goto-char (point-min))
1078 (if (not (derived-mode-p 'change-log-mode
))
1079 (error "%s not found in Change Log mode" other-log
))
1080 ;; Loop through all the entries in OTHER-LOG.
1082 (setq date1
(change-log-sortable-date-at))
1084 end
(progn (forward-page) (point)))
1085 ;; Look for an entry in original buffer that isn't later.
1086 (with-current-buffer buf
1087 (while (and (not (eobp))
1088 (string< date1
(change-log-sortable-date-at)))
1091 (insert-buffer-substring other-buf start end
)
1092 ;; At the end of the original buffer, insert a newline to
1093 ;; separate entries and then the rest of the file being
1096 (and (= ?
\n (char-before))
1097 (or (<= (1- (point)) (point-min))
1098 (= ?
\n (char-before (1- (point)))))))
1099 (insert (if use-hard-newlines hard-newline
"\n")))
1100 ;; Move to the end of it to terminate outer loop.
1101 (with-current-buffer other-buf
1102 (goto-char (point-max)))
1103 (insert-buffer-substring other-buf start
)))))))
1106 (defun change-log-redate ()
1107 "Fix any old-style date entries in the current log file to default format."
1111 (goto-char (point-min))
1112 (while (re-search-forward "^\\sw.........[0-9:+ ]*" nil t
)
1113 (unless (= 12 (- (match-end 0) (match-beginning 0)))
1114 (let* ((date (save-match-data
1115 (timezone-fix-time (match-string 0) nil nil
)))
1116 (zone (if (consp (aref date
6))
1117 (nth 1 (aref date
6)))))
1118 (replace-match (format-time-string
1120 (encode-time (aref date
5)
1130 ;; arch-tag: 81eee6fc-088f-4372-a37f-80ad9620e762
1131 ;;; add-log.el ends here