* lisp/vc/vc-svn.el (vc-svn-diff): Use `diff-command' instead of the
[emacs.git] / lisp / vc / add-log.el
blobd75fcb5f4f1cb6a7571122218444394684851ba4
1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985-1986, 1988, 1993-1994, 1997-1998, 2000-2011
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: vc 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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This facility is documented in the Emacs Manual.
28 ;; Todo:
30 ;; - Find/use/create _MTN/log if there's a _MTN directory.
31 ;; - Find/use/create ++log.* if there's an {arch} directory.
32 ;; - Use an open *VC-Log* or *cvs-commit* buffer if it's related to the
33 ;; source file.
34 ;; - Don't add TAB indents (and username?) if inserting entries in those
35 ;; special places.
37 ;;; Code:
39 (defgroup change-log nil
40 "Change log maintenance."
41 :group 'tools
42 :link '(custom-manual "(emacs)Change Log")
43 :prefix "change-log-"
44 :prefix "add-log-")
47 (defcustom change-log-default-name nil
48 "Name of a change log file for \\[add-change-log-entry]."
49 :type '(choice (const :tag "default" nil)
50 string)
51 :group 'change-log)
52 ;;;###autoload
53 (put 'change-log-default-name 'safe-local-variable 'string-or-null-p)
55 (defcustom change-log-mode-hook nil
56 "Normal hook run by `change-log-mode'."
57 :type 'hook
58 :group 'change-log)
60 ;; Many modes set this variable, so avoid warnings.
61 ;;;###autoload
62 (defcustom add-log-current-defun-function nil
63 "If non-nil, function to guess name of surrounding function.
64 It is used by `add-log-current-defun' in preference to built-in rules.
65 Returns function's name as a string, or nil if outside a function."
66 :type '(choice (const nil) function)
67 :group 'change-log)
69 ;;;###autoload
70 (defcustom add-log-full-name nil
71 "Full name of user, for inclusion in ChangeLog daily headers.
72 This defaults to the value returned by the function `user-full-name'."
73 :type '(choice (const :tag "Default" nil)
74 string)
75 :group 'change-log)
77 ;;;###autoload
78 (defcustom add-log-mailing-address nil
79 "Email addresses of user, for inclusion in ChangeLog headers.
80 This defaults to the value of `user-mail-address'. In addition to
81 being a simple string, this value can also be a list. All elements
82 will be recognized as referring to the same user; when creating a new
83 ChangeLog entry, one element will be chosen at random."
84 :type '(choice (const :tag "Default" nil)
85 (string :tag "String")
86 (repeat :tag "List of Strings" string))
87 :group 'change-log)
89 (defcustom add-log-time-format 'add-log-iso8601-time-string
90 "Function that defines the time format.
91 For example, `add-log-iso8601-time-string', which gives the
92 date in international ISO 8601 format,
93 and `current-time-string' are two valid values."
94 :type '(radio (const :tag "International ISO 8601 format"
95 add-log-iso8601-time-string)
96 (const :tag "Old format, as returned by `current-time-string'"
97 current-time-string)
98 (function :tag "Other"))
99 :group 'change-log)
101 (defcustom add-log-keep-changes-together nil
102 "If non-nil, normally keep day's log entries for one file together.
104 Log entries for a given file made with \\[add-change-log-entry] or
105 \\[add-change-log-entry-other-window] will only be added to others \
106 for that file made
107 today if this variable is non-nil or that file comes first in today's
108 entries. Otherwise another entry for that file will be started. An
109 original log:
111 * foo (...): ...
112 * bar (...): change 1
114 in the latter case, \\[add-change-log-entry-other-window] in a \
115 buffer visiting `bar', yields:
117 * bar (...): -!-
118 * foo (...): ...
119 * bar (...): change 1
121 and in the former:
123 * foo (...): ...
124 * bar (...): change 1
125 (...): -!-
127 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
128 this variable."
129 :version "20.3"
130 :type 'boolean
131 :group 'change-log)
133 (defcustom add-log-always-start-new-record nil
134 "If non-nil, `add-change-log-entry' will always start a new record."
135 :version "22.1"
136 :type 'boolean
137 :group 'change-log)
139 (defcustom add-log-buffer-file-name-function nil
140 "If non-nil, function to call to identify the full filename of a buffer.
141 This function is called with no argument. If this is nil, the default is to
142 use `buffer-file-name'."
143 :type '(choice (const nil) function)
144 :group 'change-log)
146 (defcustom add-log-file-name-function nil
147 "If non-nil, function to call to identify the filename for a ChangeLog entry.
148 This function is called with one argument, the value of variable
149 `buffer-file-name' in that buffer. If this is nil, the default is to
150 use the file's name relative to the directory of the change log file."
151 :type '(choice (const nil) function)
152 :group 'change-log)
155 (defcustom change-log-version-info-enabled nil
156 "If non-nil, enable recording version numbers with the changes."
157 :version "21.1"
158 :type 'boolean
159 :group 'change-log)
161 (defcustom change-log-version-number-regexp-list
162 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
163 (list
164 ;; (defconst ad-version "2.15"
165 (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re)
166 ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
167 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re)))
168 "List of regexps to search for version number.
169 The version number must be in group 1.
170 Note: The search is conducted only within 10%, at the beginning of the file."
171 :version "21.1"
172 :type '(repeat regexp)
173 :group 'change-log)
175 (defface change-log-date
176 '((t (:inherit font-lock-string-face)))
177 "Face used to highlight dates in date lines."
178 :version "21.1"
179 :group 'change-log)
180 (define-obsolete-face-alias 'change-log-date-face 'change-log-date "22.1")
182 (defface change-log-name
183 '((t (:inherit font-lock-constant-face)))
184 "Face for highlighting author names."
185 :version "21.1"
186 :group 'change-log)
187 (define-obsolete-face-alias 'change-log-name-face 'change-log-name "22.1")
189 (defface change-log-email
190 '((t (:inherit font-lock-variable-name-face)))
191 "Face for highlighting author email addresses."
192 :version "21.1"
193 :group 'change-log)
194 (define-obsolete-face-alias 'change-log-email-face 'change-log-email "22.1")
196 (defface change-log-file
197 '((t (:inherit font-lock-function-name-face)))
198 "Face for highlighting file names."
199 :version "21.1"
200 :group 'change-log)
201 (define-obsolete-face-alias 'change-log-file-face 'change-log-file "22.1")
203 (defface change-log-list
204 '((t (:inherit font-lock-keyword-face)))
205 "Face for highlighting parenthesized lists of functions or variables."
206 :version "21.1"
207 :group 'change-log)
208 (define-obsolete-face-alias 'change-log-list-face 'change-log-list "22.1")
210 (defface change-log-conditionals
211 '((t (:inherit font-lock-variable-name-face)))
212 "Face for highlighting conditionals of the form `[...]'."
213 :version "21.1"
214 :group 'change-log)
215 (define-obsolete-face-alias 'change-log-conditionals-face
216 'change-log-conditionals "22.1")
218 (defface change-log-function
219 '((t (:inherit font-lock-variable-name-face)))
220 "Face for highlighting items of the form `<....>'."
221 :version "21.1"
222 :group 'change-log)
223 (define-obsolete-face-alias 'change-log-function-face
224 'change-log-function "22.1")
226 (defface change-log-acknowledgement
227 '((t (:inherit font-lock-comment-face)))
228 "Face for highlighting acknowledgments."
229 :version "21.1"
230 :group 'change-log)
231 (define-obsolete-face-alias 'change-log-acknowledgement-face
232 'change-log-acknowledgement "22.1")
234 (defconst change-log-file-names-re "^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)")
235 (defconst change-log-start-entry-re "^\\sw.........[0-9:+ ]*")
237 (defvar change-log-font-lock-keywords
238 `(;;
239 ;; Date lines, new (2000-01-01) and old (Sat Jan 1 00:00:00 2000) styles.
240 ;; Fixme: this regepx is just an approximate one and may match
241 ;; wrongly with a non-date line existing as a random note. In
242 ;; addition, using any kind of fixed setting like this doesn't
243 ;; work if a user customizes add-log-time-format.
244 ("^[0-9-]+ +\\|^ \\{11,\\}\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+"
245 (0 'change-log-date-face)
246 ;; Name and e-mail; some people put e-mail in parens, not angles.
247 ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
248 (1 'change-log-name)
249 (2 'change-log-email)))
251 ;; File names.
252 (,change-log-file-names-re
253 (2 'change-log-file)
254 ;; Possibly further names in a list:
255 ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 'change-log-file))
256 ;; Possibly a parenthesized list of names:
257 ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
258 nil nil (1 'change-log-list))
259 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
260 nil nil (1 'change-log-list)))
262 ;; Function or variable names.
263 ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
264 (2 'change-log-list)
265 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
266 (1 'change-log-list)))
268 ;; Conditionals.
269 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals))
271 ;; Function of change.
272 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function))
274 ;; Acknowledgements.
275 ;; Don't include plain "From" because that is vague;
276 ;; we want to encourage people to say something more specific.
277 ;; Note that the FSF does not use "Patches by"; our convention
278 ;; is to put the name of the author of the changes at the top
279 ;; of the change log entry.
280 ("\\(^\\( +\\|\t\\)\\| \\)\\(Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
281 3 'change-log-acknowledgement))
282 "Additional expressions to highlight in Change Log mode.")
284 (defun change-log-search-file-name (where)
285 "Return the file-name for the change under point."
286 (save-excursion
287 (goto-char where)
288 (beginning-of-line 1)
289 (if (looking-at change-log-start-entry-re)
290 ;; We are at the start of an entry, search forward for a file
291 ;; name.
292 (progn
293 (re-search-forward change-log-file-names-re nil t)
294 (match-string-no-properties 2))
295 (if (looking-at change-log-file-names-re)
296 ;; We found a file name.
297 (match-string-no-properties 2)
298 ;; Look backwards for either a file name or the log entry start.
299 (if (re-search-backward
300 (concat "\\(" change-log-start-entry-re
301 "\\)\\|\\("
302 change-log-file-names-re "\\)") nil t)
303 (if (match-beginning 1)
304 ;; We got the start of the entry, look forward for a
305 ;; file name.
306 (progn
307 (re-search-forward change-log-file-names-re nil t)
308 (match-string-no-properties 2))
309 (match-string-no-properties 4))
310 ;; We must be before any file name, look forward.
311 (re-search-forward change-log-file-names-re nil t)
312 (match-string-no-properties 2))))))
314 (defun change-log-find-file ()
315 "Visit the file for the change under point."
316 (interactive)
317 (let ((file (change-log-search-file-name (point))))
318 (if (and file (file-exists-p file))
319 (find-file file)
320 (message "No such file or directory: %s" file))))
322 (defun change-log-search-tag-name-1 (&optional from)
323 "Search for a tag name within subexpression 1 of last match.
324 Optional argument FROM specifies a buffer position where the tag
325 name should be located. Return value is a cons whose car is the
326 string representing the tag and whose cdr is the position where
327 the tag was found."
328 (save-restriction
329 (narrow-to-region (match-beginning 1) (match-end 1))
330 (when from (goto-char from))
331 ;; The regexp below skips any symbol near `point' (FROM) followed by
332 ;; whitespace and another symbol. This should skip, for example,
333 ;; "struct" in a specification like "(struct buffer)" and move to
334 ;; "buffer". A leading paren is ignored.
335 (when (looking-at
336 "[(]?\\(?:\\(?:\\sw\\|\\s_\\)+\\(?:[ \t]+\\(\\sw\\|\\s_\\)+\\)\\)")
337 (goto-char (match-beginning 1)))
338 (cons (find-tag-default) (point))))
340 (defconst change-log-tag-re
341 "(\\(\\(?:\\sw\\|\\s_\\)+\\(?:[, \t]+\\(?:\\sw\\|\\s_\\)+\\)*\\))"
342 "Regexp matching a tag name in change log entries.")
344 (defun change-log-search-tag-name (&optional at)
345 "Search for a tag name near `point'.
346 Optional argument AT non-nil means search near buffer position AT.
347 Return value is a cons whose car is the string representing
348 the tag and whose cdr is the position where the tag was found."
349 (save-excursion
350 (goto-char (setq at (or at (point))))
351 (save-restriction
352 (widen)
353 (or (condition-case nil
354 ;; Within parenthesized list?
355 (save-excursion
356 (backward-up-list)
357 (when (looking-at change-log-tag-re)
358 (change-log-search-tag-name-1 at)))
359 (error nil))
360 (condition-case nil
361 ;; Before parenthesized list on same line?
362 (save-excursion
363 (when (and (skip-chars-forward " \t")
364 (looking-at change-log-tag-re))
365 (change-log-search-tag-name-1)))
366 (error nil))
367 (condition-case nil
368 ;; Near file name?
369 (save-excursion
370 (when (and (progn
371 (beginning-of-line)
372 (looking-at change-log-file-names-re))
373 (goto-char (match-end 0))
374 (skip-syntax-forward " ")
375 (looking-at change-log-tag-re))
376 (change-log-search-tag-name-1)))
377 (error nil))
378 (condition-case nil
379 ;; Anywhere else within current entry?
380 (let ((from
381 (save-excursion
382 (end-of-line)
383 (if (re-search-backward change-log-start-entry-re nil t)
384 (match-beginning 0)
385 (point-min))))
387 (save-excursion
388 (end-of-line)
389 (if (re-search-forward change-log-start-entry-re nil t)
390 (match-beginning 0)
391 (point-max)))))
392 (when (and (< from to) (<= from at) (<= at to))
393 (save-restriction
394 ;; Narrow to current change log entry.
395 (narrow-to-region from to)
396 (cond
397 ((re-search-backward change-log-tag-re nil t)
398 (narrow-to-region (match-beginning 1) (match-end 1))
399 (goto-char (point-max))
400 (cons (find-tag-default) (point-max)))
401 ((re-search-forward change-log-tag-re nil t)
402 (narrow-to-region (match-beginning 1) (match-end 1))
403 (goto-char (point-min))
404 (cons (find-tag-default) (point-min)))))))
405 (error nil))))))
407 (defvar change-log-find-head nil)
408 (defvar change-log-find-tail nil)
409 (defvar change-log-find-window nil)
411 (defun change-log-goto-source-1 (tag regexp file buffer
412 &optional window first last)
413 "Search for tag TAG in buffer BUFFER visiting file FILE.
414 REGEXP is a regular expression for TAG. The remaining arguments
415 are optional: WINDOW denotes the window to display the results of
416 the search. FIRST is a position in BUFFER denoting the first
417 match from previous searches for TAG. LAST is the position in
418 BUFFER denoting the last match for TAG in the last search."
419 (with-current-buffer buffer
420 (save-excursion
421 (save-restriction
422 (widen)
423 (if last
424 (progn
425 ;; When LAST is set make sure we continue from the next
426 ;; line end to not find the same tag again.
427 (goto-char last)
428 (end-of-line)
429 (condition-case nil
430 ;; Try to go to the end of the current defun to avoid
431 ;; false positives within the current defun's body
432 ;; since these would match `add-log-current-defun'.
433 (end-of-defun)
434 ;; Don't fall behind when `end-of-defun' fails.
435 (error (progn (goto-char last) (end-of-line))))
436 (setq last nil))
437 ;; When LAST was not set start at beginning of BUFFER.
438 (goto-char (point-min)))
439 (let (current-defun)
440 (while (and (not last) (re-search-forward regexp nil t))
441 ;; Verify that `add-log-current-defun' invoked at the end
442 ;; of the match returns TAG. This heuristic works well
443 ;; whenever the name of the defun occurs within the first
444 ;; line of the defun.
445 (setq current-defun (add-log-current-defun))
446 (when (and current-defun (string-equal current-defun tag))
447 ;; Record this as last match.
448 (setq last (line-beginning-position))
449 ;; Record this as first match when there's none.
450 (unless first (setq first last)))))))
451 (if (or last first)
452 (with-selected-window
453 (setq change-log-find-window (or window (display-buffer buffer)))
454 (if last
455 (progn
456 (when (or (< last (point-min)) (> last (point-max)))
457 ;; Widen to show TAG.
458 (widen))
459 (push-mark)
460 (goto-char last))
461 ;; When there are no more matches go (back) to FIRST.
462 (message "No more matches for tag `%s' in file `%s'" tag file)
463 (setq last first)
464 (goto-char first))
465 ;; Return new "tail".
466 (list (selected-window) first last))
467 (message "Source location of tag `%s' not found in file `%s'" tag file)
468 nil)))
470 (defun change-log-goto-source ()
471 "Go to source location of \"change log tag\" near `point'.
472 A change log tag is a symbol within a parenthesized,
473 comma-separated list. If no suitable tag can be found nearby,
474 try to visit the file for the change under `point' instead."
475 (interactive)
476 (if (and (eq last-command 'change-log-goto-source)
477 change-log-find-tail)
478 (setq change-log-find-tail
479 (condition-case nil
480 (apply 'change-log-goto-source-1
481 (append change-log-find-head change-log-find-tail))
482 (error
483 (format "Cannot find more matches for tag `%s' in file `%s'"
484 (car change-log-find-head)
485 (nth 2 change-log-find-head)))))
486 (save-excursion
487 (let* ((at (point))
488 (tag-at (change-log-search-tag-name))
489 (tag (car tag-at))
490 (file (when tag-at (change-log-search-file-name (cdr tag-at))))
491 (file-at (when file (match-beginning 2)))
492 ;; `file-2' is the file `change-log-search-file-name' finds
493 ;; at `point'. We use `file-2' as a fallback when `tag' or
494 ;; `file' are not suitable for some reason.
495 (file-2 (change-log-search-file-name at))
496 (file-2-at (when file-2 (match-beginning 2))))
497 (cond
498 ((and (or (not tag) (not file) (not (file-exists-p file)))
499 (or (not file-2) (not (file-exists-p file-2))))
500 (error "Cannot find tag or file near `point'"))
501 ((and file-2 (file-exists-p file-2)
502 (or (not tag) (not file) (not (file-exists-p file))
503 (and (or (and (< file-at file-2-at) (<= file-2-at at))
504 (and (<= at file-2-at) (< file-2-at file-at))))))
505 ;; We either have not found a suitable file name or `file-2'
506 ;; provides a "better" file name wrt `point'. Go to the
507 ;; buffer of `file-2' instead.
508 (setq change-log-find-window
509 (display-buffer (find-file-noselect file-2))))
511 (setq change-log-find-head
512 (list tag (concat "\\_<" (regexp-quote tag) "\\_>")
513 file (find-file-noselect file)))
514 (condition-case nil
515 (setq change-log-find-tail
516 (apply 'change-log-goto-source-1 change-log-find-head))
517 (error
518 (format "Cannot find matches for tag `%s' in file `%s'"
519 tag file)))))))))
521 (defun change-log-next-error (&optional argp reset)
522 "Move to the Nth (default 1) next match in a ChangeLog buffer.
523 Compatibility function for \\[next-error] invocations."
524 (interactive "p")
525 (let* ((argp (or argp 0))
526 (count (abs argp)) ; how many cycles
527 (down (< argp 0)) ; are we going down? (is argp negative?)
528 (up (not down))
529 (search-function (if up 're-search-forward 're-search-backward)))
531 ;; set the starting position
532 (goto-char (cond (reset (point-min))
533 (down (line-beginning-position))
534 (up (line-end-position))
535 ((point))))
537 (funcall search-function change-log-file-names-re nil t count))
539 (beginning-of-line)
540 ;; if we found a place to visit...
541 (when (looking-at change-log-file-names-re)
542 (let (change-log-find-window)
543 (change-log-goto-source)
544 (when change-log-find-window
545 ;; Select window displaying source file.
546 (select-window change-log-find-window)))))
548 (defvar change-log-mode-map
549 (let ((map (make-sparse-keymap))
550 (menu-map (make-sparse-keymap)))
551 (define-key map [?\C-c ?\C-p] 'add-log-edit-prev-comment)
552 (define-key map [?\C-c ?\C-n] 'add-log-edit-next-comment)
553 (define-key map [?\C-c ?\C-f] 'change-log-find-file)
554 (define-key map [?\C-c ?\C-c] 'change-log-goto-source)
555 (define-key map [menu-bar changelog] (cons "ChangeLog" menu-map))
556 (define-key menu-map [gs]
557 '(menu-item "Go To Source" change-log-goto-source
558 :help "Go to source location of ChangeLog tag near point"))
559 (define-key menu-map [ff]
560 '(menu-item "Find File" change-log-find-file
561 :help "Visit the file for the change under point"))
562 (define-key menu-map [sep] '("--"))
563 (define-key menu-map [nx]
564 '(menu-item "Next Log-Edit Comment" add-log-edit-next-comment
565 :help "Cycle forward through Log-Edit mode comment history"))
566 (define-key menu-map [pr]
567 '(menu-item "Previous Log-Edit Comment" add-log-edit-prev-comment
568 :help "Cycle backward through Log-Edit mode comment history"))
569 map)
570 "Keymap for Change Log major mode.")
572 ;; It used to be called change-log-time-zone-rule but really should be
573 ;; called add-log-time-zone-rule since it's only used from add-log-* code.
574 (defvaralias 'change-log-time-zone-rule 'add-log-time-zone-rule)
575 (defvar add-log-time-zone-rule nil
576 "Time zone used for calculating change log time stamps.
577 It takes the same format as the TZ argument of `set-time-zone-rule'.
578 If nil, use local time.
579 If t, use universal time.")
580 (put 'add-log-time-zone-rule 'safe-local-variable
581 '(lambda (x) (or (booleanp x) (stringp x))))
583 (defun add-log-iso8601-time-zone (&optional time)
584 (let* ((utc-offset (or (car (current-time-zone time)) 0))
585 (sign (if (< utc-offset 0) ?- ?+))
586 (sec (abs utc-offset))
587 (ss (% sec 60))
588 (min (/ sec 60))
589 (mm (% min 60))
590 (hh (/ min 60)))
591 (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
592 ((not (zerop mm)) "%c%02d:%02d")
593 (t "%c%02d"))
594 sign hh mm ss)))
596 (defvar add-log-iso8601-with-time-zone nil)
598 (defun add-log-iso8601-time-string ()
599 (let ((time (format-time-string "%Y-%m-%d"
600 nil (eq t add-log-time-zone-rule))))
601 (if add-log-iso8601-with-time-zone
602 (concat time " " (add-log-iso8601-time-zone))
603 time)))
605 (defun change-log-name ()
606 "Return (system-dependent) default name for a change log file."
607 (or change-log-default-name
608 "ChangeLog"))
610 (defun add-log-edit-prev-comment (arg)
611 "Cycle backward through Log-Edit mode comment history.
612 With a numeric prefix ARG, go back ARG comments."
613 (interactive "*p")
614 (save-restriction
615 (narrow-to-region (point)
616 (if (memq last-command '(add-log-edit-prev-comment
617 add-log-edit-next-comment))
618 (mark) (point)))
619 (when (fboundp 'log-edit-previous-comment)
620 (log-edit-previous-comment arg)
621 (indent-region (point-min) (point-max))
622 (goto-char (point-min))
623 (unless (save-restriction (widen) (bolp))
624 (delete-region (point) (progn (skip-chars-forward " \t\n") (point))))
625 (set-mark (point-min))
626 (goto-char (point-max))
627 (delete-region (point) (progn (skip-chars-backward " \t\n") (point))))))
629 (defun add-log-edit-next-comment (arg)
630 "Cycle forward through Log-Edit mode comment history.
631 With a numeric prefix ARG, go back ARG comments."
632 (interactive "*p")
633 (add-log-edit-prev-comment (- arg)))
635 ;;;###autoload
636 (defun prompt-for-change-log-name ()
637 "Prompt for a change log name."
638 (let* ((default (change-log-name))
639 (name (expand-file-name
640 (read-file-name (format "Log file (default %s): " default)
641 nil default))))
642 ;; Handle something that is syntactically a directory name.
643 ;; Look for ChangeLog or whatever in that directory.
644 (if (string= (file-name-nondirectory name) "")
645 (expand-file-name (file-name-nondirectory default)
646 name)
647 ;; Handle specifying a file that is a directory.
648 (if (file-directory-p name)
649 (expand-file-name (file-name-nondirectory default)
650 (file-name-as-directory name))
651 name))))
653 (defun change-log-version-number-search ()
654 "Return version number of current buffer's file.
655 This is the value returned by `vc-working-revision' or, if that is
656 nil, by matching `change-log-version-number-regexp-list'."
657 (let* ((size (buffer-size))
658 (limit
659 ;; The version number can be anywhere in the file, but
660 ;; restrict search to the file beginning: 10% should be
661 ;; enough to prevent some mishits.
663 ;; Apply percentage only if buffer size is bigger than
664 ;; approx 100 lines.
665 (if (> size (* 100 80)) (+ (point) (/ size 10)))))
666 (or (and buffer-file-name (vc-working-revision buffer-file-name))
667 (save-restriction
668 (widen)
669 (let ((regexps change-log-version-number-regexp-list)
670 version)
671 (while regexps
672 (save-excursion
673 (goto-char (point-min))
674 (when (re-search-forward (pop regexps) limit t)
675 (setq version (match-string 1)
676 regexps nil))))
677 version)))))
679 (declare-function diff-find-source-location "diff-mode"
680 (&optional other-file reverse noprompt))
682 ;;;###autoload
683 (defun find-change-log (&optional file-name buffer-file)
684 "Find a change log file for \\[add-change-log-entry] and return the name.
686 Optional arg FILE-NAME specifies the file to use.
687 If FILE-NAME is nil, use the value of `change-log-default-name'.
688 If `change-log-default-name' is nil, behave as though it were 'ChangeLog'
689 \(or whatever we use on this operating system).
691 If `change-log-default-name' contains a leading directory component, then
692 simply find it in the current directory. Otherwise, search in the current
693 directory and its successive parents for a file so named.
695 Once a file is found, `change-log-default-name' is set locally in the
696 current buffer to the complete file name.
697 Optional arg BUFFER-FILE overrides `buffer-file-name'."
698 ;; If we are called from a diff, first switch to the source buffer;
699 ;; in order to respect buffer-local settings of change-log-default-name, etc.
700 (with-current-buffer (let ((buff (if (derived-mode-p 'diff-mode)
701 (car (ignore-errors
702 (diff-find-source-location))))))
703 (if (buffer-live-p buff) buff
704 (current-buffer)))
705 ;; If user specified a file name or if this buffer knows which one to use,
706 ;; just use that.
707 (or file-name
708 (setq file-name (and change-log-default-name
709 (file-name-directory change-log-default-name)
710 change-log-default-name))
711 (progn
712 ;; Chase links in the source file
713 ;; and use the change log in the dir where it points.
714 (setq file-name (or (and (or buffer-file buffer-file-name)
715 (file-name-directory
716 (file-chase-links
717 (or buffer-file buffer-file-name))))
718 default-directory))
719 (if (file-directory-p file-name)
720 (setq file-name (expand-file-name (change-log-name) file-name)))
721 ;; Chase links before visiting the file.
722 ;; This makes it easier to use a single change log file
723 ;; for several related directories.
724 (setq file-name (file-chase-links file-name))
725 (setq file-name (expand-file-name file-name))
726 ;; Move up in the dir hierarchy till we find a change log file.
727 (let ((file1 file-name)
728 parent-dir)
729 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
730 (progn (setq parent-dir
731 (file-name-directory
732 (directory-file-name
733 (file-name-directory file1))))
734 ;; Give up if we are already at the root dir.
735 (not (string= (file-name-directory file1)
736 parent-dir))))
737 ;; Move up to the parent dir and try again.
738 (setq file1 (expand-file-name
739 (file-name-nondirectory (change-log-name))
740 parent-dir)))
741 ;; If we found a change log in a parent, use that.
742 (if (or (get-file-buffer file1) (file-exists-p file1))
743 (setq file-name file1)))))
744 ;; Make a local variable in this buffer so we needn't search again.
745 (set (make-local-variable 'change-log-default-name) file-name))
746 file-name)
748 (defun add-log-file-name (buffer-file log-file)
749 ;; Never want to add a change log entry for the ChangeLog file itself.
750 (unless (or (null buffer-file) (string= buffer-file log-file))
751 (if add-log-file-name-function
752 (funcall add-log-file-name-function buffer-file)
753 (setq buffer-file
754 (let* ((dir (file-name-directory log-file))
755 (rel (file-relative-name buffer-file dir)))
756 ;; Sometimes with symlinks, the two buffers may have names that
757 ;; appear to belong to different directory trees. So check the
758 ;; file-truenames, to see if we get a better result.
759 (if (not (string-match "\\`\\.\\./" rel))
761 (let ((new (file-relative-name (file-truename buffer-file)
762 (file-truename dir))))
763 (if (< (length new) (length rel))
764 new rel)))))
765 ;; If we have a backup file, it's presumably because we're
766 ;; comparing old and new versions (e.g. for deleted
767 ;; functions) and we'll want to use the original name.
768 (if (backup-file-name-p buffer-file)
769 (file-name-sans-versions buffer-file)
770 buffer-file))))
772 ;;;###autoload
773 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
774 put-new-entry-on-new-line)
775 "Find change log file, and add an entry for today and an item for this file.
776 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
777 name and email (stored in `add-log-full-name' and `add-log-mailing-address').
779 Second arg FILE-NAME is file name of the change log.
780 If nil, use the value of `change-log-default-name'.
782 Third arg OTHER-WINDOW non-nil means visit in other window.
784 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
785 never append to an existing entry. Option `add-log-keep-changes-together'
786 otherwise affects whether a new entry is created.
788 Fifth arg PUT-NEW-ENTRY-ON-NEW-LINE non-nil means that if a new
789 entry is created, put it on a new line by itself, do not put it
790 after a comma on an existing line.
792 Option `add-log-always-start-new-record' non-nil means always create a
793 new record, even when the last record was made on the same date and by
794 the same person.
796 The change log file can start with a copyright notice and a copying
797 permission notice. The first blank line indicates the end of these
798 notices.
800 Today's date is calculated according to `add-log-time-zone-rule' if
801 non-nil, otherwise in local time."
802 (interactive (list current-prefix-arg
803 (prompt-for-change-log-name)))
804 (let* ((defun (add-log-current-defun))
805 (version (and change-log-version-info-enabled
806 (change-log-version-number-search)))
807 (buf-file-name (if add-log-buffer-file-name-function
808 (funcall add-log-buffer-file-name-function)
809 buffer-file-name))
810 (buffer-file (if buf-file-name (expand-file-name buf-file-name)))
811 (file-name (expand-file-name (find-change-log file-name buffer-file)))
812 ;; Set ITEM to the file name to use in the new item.
813 (item (add-log-file-name buffer-file file-name)))
815 (unless (equal file-name buffer-file-name)
816 (cond
817 ((equal file-name (buffer-file-name (window-buffer (selected-window))))
818 ;; If the selected window already shows the desired buffer don't show
819 ;; it again (particularly important if other-window is true).
820 ;; This is important for diff-add-change-log-entries-other-window.
821 (set-buffer (window-buffer (selected-window))))
822 ((or other-window (window-dedicated-p (selected-window)))
823 (find-file-other-window file-name))
824 (t (find-file file-name))))
825 (or (derived-mode-p 'change-log-mode)
826 (change-log-mode))
827 (undo-boundary)
828 (goto-char (point-min))
830 (let ((full-name (or add-log-full-name (user-full-name)))
831 (mailing-address (or add-log-mailing-address user-mail-address)))
833 (when whoami
834 (setq full-name (read-string "Full name: " full-name))
835 ;; Note that some sites have room and phone number fields in
836 ;; full name which look silly when inserted. Rather than do
837 ;; anything about that here, let user give prefix argument so that
838 ;; s/he can edit the full name field in prompter if s/he wants.
839 (setq mailing-address
840 (read-string "Mailing address: " mailing-address)))
842 ;; If file starts with a copyright and permission notice, skip them.
843 ;; Assume they end at first blank line.
844 (when (looking-at "Copyright")
845 (search-forward "\n\n")
846 (skip-chars-forward "\n"))
848 ;; Advance into first entry if it is usable; else make new one.
849 (let ((new-entries
850 (mapcar (lambda (addr)
851 (concat
852 (if (stringp add-log-time-zone-rule)
853 (let ((tz (getenv "TZ")))
854 (unwind-protect
855 (progn
856 (set-time-zone-rule add-log-time-zone-rule)
857 (funcall add-log-time-format))
858 (set-time-zone-rule tz)))
859 (funcall add-log-time-format))
860 " " full-name
861 " <" addr ">"))
862 (if (consp mailing-address)
863 mailing-address
864 (list mailing-address)))))
865 (if (and (not add-log-always-start-new-record)
866 (let ((hit nil))
867 (dolist (entry new-entries hit)
868 (when (looking-at (regexp-quote entry))
869 (setq hit t)))))
870 (forward-line 1)
871 (insert (nth (random (length new-entries))
872 new-entries)
873 (if use-hard-newlines hard-newline "\n")
874 (if use-hard-newlines hard-newline "\n"))
875 (forward-line -1))))
877 ;; Determine where we should stop searching for a usable
878 ;; item to add to, within this entry.
879 (let ((bound
880 (save-excursion
881 (if (looking-at "\n*[^\n* \t]")
882 (skip-chars-forward "\n")
883 (if add-log-keep-changes-together
884 (forward-page) ; page delimits entries for date
885 (forward-paragraph))) ; paragraph delimits entries for file
886 (point))))
888 ;; Now insert the new line for this item.
889 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t)
890 ;; Put this file name into the existing empty item.
891 (if item
892 (insert item)))
893 ((and (not new-entry)
894 (let (case-fold-search)
895 (re-search-forward
896 (concat (regexp-quote (concat "* " item))
897 ;; Don't accept `foo.bar' when
898 ;; looking for `foo':
899 "\\(\\s \\|[(),:]\\)")
900 bound t)))
901 ;; Add to the existing item for the same file.
902 (re-search-forward "^\\s *$\\|^\\s \\*")
903 (goto-char (match-beginning 0))
904 ;; Delete excess empty lines; make just 2.
905 (while (and (not (eobp)) (looking-at "^\\s *$"))
906 (delete-region (point) (line-beginning-position 2)))
907 (insert (if use-hard-newlines hard-newline "\n")
908 (if use-hard-newlines hard-newline "\n"))
909 (forward-line -2)
910 (indent-relative-maybe))
912 ;; Make a new item.
913 (while (looking-at "\\sW")
914 (forward-line 1))
915 (while (and (not (eobp)) (looking-at "^\\s *$"))
916 (delete-region (point) (line-beginning-position 2)))
917 (insert (if use-hard-newlines hard-newline "\n")
918 (if use-hard-newlines hard-newline "\n")
919 (if use-hard-newlines hard-newline "\n"))
920 (forward-line -2)
921 (indent-to left-margin)
922 (insert "* ")
923 (if item (insert item)))))
924 ;; Now insert the function name, if we have one.
925 ;; Point is at the item for this file,
926 ;; either at the end of the line or at the first blank line.
927 (if (not defun)
928 ;; No function name, so put in a colon unless we have just a star.
929 (unless (save-excursion
930 (beginning-of-line 1)
931 (looking-at "\\s *\\(\\*\\s *\\)?$"))
932 (insert ": ")
933 (if version (insert version ?\s)))
934 ;; Make it easy to get rid of the function name.
935 (undo-boundary)
936 (unless (save-excursion
937 (beginning-of-line 1)
938 (looking-at "\\s *$"))
939 (insert ?\s))
940 ;; See if the prev function name has a message yet or not.
941 ;; If not, merge the two items.
942 (let ((pos (point-marker)))
943 (skip-syntax-backward " ")
944 (skip-chars-backward "):")
945 (if (and (not put-new-entry-on-new-line)
946 (looking-at "):")
947 (let ((pos (save-excursion (backward-sexp 1) (point))))
948 (when (equal (buffer-substring pos (point)) defun)
949 (delete-region pos (point)))
950 (> fill-column (+ (current-column) (length defun) 4))))
951 (progn (skip-chars-backward ", ")
952 (delete-region (point) pos)
953 (unless (memq (char-before) '(?\()) (insert ", ")))
954 (when (and (not put-new-entry-on-new-line) (looking-at "):"))
955 (delete-region (+ 1 (point)) (line-end-position)))
956 (goto-char pos)
957 (insert "("))
958 (set-marker pos nil))
959 (insert defun "): ")
960 (if version (insert version ?\s)))))
962 ;;;###autoload
963 (defun add-change-log-entry-other-window (&optional whoami file-name)
964 "Find change log file in other window and add entry and item.
965 This is just like `add-change-log-entry' except that it displays
966 the change log file in another window."
967 (interactive (if current-prefix-arg
968 (list current-prefix-arg
969 (prompt-for-change-log-name))))
970 (add-change-log-entry whoami file-name t))
973 (defvar change-log-indent-text 0)
975 (defun change-log-fill-parenthesized-list ()
976 ;; Fill parenthesized lists of names according to GNU standards.
977 ;; * file-name.ext (very-long-foo, very-long-bar, very-long-foobar):
978 ;; should be filled as
979 ;; * file-name.ext (very-long-foo, very-long-bar)
980 ;; (very-long-foobar):
981 (save-excursion
982 (end-of-line 0)
983 (skip-chars-backward " \t")
984 (when (and (equal (char-before) ?\,)
985 (> (point) (1+ (point-min))))
986 (condition-case nil
987 (when (save-excursion
988 (and (prog2
989 (up-list -1)
990 (equal (char-after) ?\()
991 (skip-chars-backward " \t"))
992 (or (bolp)
993 ;; Skip everything but a whitespace or asterisk.
994 (and (not (zerop (skip-chars-backward "^ \t\n*")))
995 (skip-chars-backward " \t")
996 ;; We want one asterisk here.
997 (= (skip-chars-backward "*") -1)
998 (skip-chars-backward " \t")
999 (bolp)))))
1000 ;; Delete the comma.
1001 (delete-char -1)
1002 ;; Close list on previous line.
1003 (insert ")")
1004 (skip-chars-forward " \t\n")
1005 ;; Start list on new line.
1006 (insert-before-markers "("))
1007 (error nil)))))
1009 (defun change-log-indent ()
1010 (change-log-fill-parenthesized-list)
1011 (let* ((indent
1012 (save-excursion
1013 (beginning-of-line)
1014 (skip-chars-forward " \t")
1015 (cond
1016 ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
1017 ;; Matching the output of add-log-time-format is difficult,
1018 ;; but I'll get it has at least two adjacent digits.
1019 (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
1021 ((looking-at "[^*(]")
1022 (+ (current-left-margin) change-log-indent-text))
1023 (t (current-left-margin)))))
1024 (pos (save-excursion (indent-line-to indent) (point))))
1025 (if (> pos (point)) (goto-char pos))))
1028 (defvar smerge-resolve-function)
1029 (defvar copyright-at-end-flag)
1031 ;;;###autoload
1032 (define-derived-mode change-log-mode text-mode "Change Log"
1033 "Major mode for editing change logs; like Indented Text mode.
1034 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
1035 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
1036 Each entry behaves as a paragraph, and the entries for one day as a page.
1037 Runs `change-log-mode-hook'.
1038 \n\\{change-log-mode-map}"
1039 (setq left-margin 8
1040 fill-column 74
1041 indent-tabs-mode t
1042 tab-width 8
1043 show-trailing-whitespace t)
1044 (set (make-local-variable 'fill-forward-paragraph-function)
1045 'change-log-fill-forward-paragraph)
1046 ;; Make sure we call `change-log-indent' when filling.
1047 (set (make-local-variable 'fill-indent-according-to-mode) t)
1048 ;; Avoid that filling leaves behind a single "*" on a line.
1049 (add-hook 'fill-nobreak-predicate
1050 '(lambda ()
1051 (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
1052 nil t)
1053 (set (make-local-variable 'indent-line-function) 'change-log-indent)
1054 (set (make-local-variable 'tab-always-indent) nil)
1055 (set (make-local-variable 'copyright-at-end-flag) t)
1056 ;; We really do want "^" in paragraph-start below: it is only the
1057 ;; lines that begin at column 0 (despite the left-margin of 8) that
1058 ;; we are looking for. Adding `* ' allows eliding the blank line
1059 ;; between entries for different files.
1060 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
1061 (set (make-local-variable 'paragraph-separate) paragraph-start)
1062 ;; Match null string on the date-line so that the date-line
1063 ;; is grouped with what follows.
1064 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
1065 (set (make-local-variable 'version-control) 'never)
1066 (set (make-local-variable 'smerge-resolve-function)
1067 'change-log-resolve-conflict)
1068 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
1069 (set (make-local-variable 'font-lock-defaults)
1070 '(change-log-font-lock-keywords t nil nil backward-paragraph))
1071 (set (make-local-variable 'multi-isearch-next-buffer-function)
1072 'change-log-next-buffer)
1073 (set (make-local-variable 'beginning-of-defun-function)
1074 'change-log-beginning-of-defun)
1075 (set (make-local-variable 'end-of-defun-function)
1076 'change-log-end-of-defun)
1077 ;; next-error function glue
1078 (setq next-error-function 'change-log-next-error)
1079 (setq next-error-last-buffer (current-buffer)))
1081 (defun change-log-next-buffer (&optional buffer wrap)
1082 "Return the next buffer in the series of ChangeLog file buffers.
1083 This function is used for multiple buffers isearch.
1084 A sequence of buffers is formed by ChangeLog files with decreasing
1085 numeric file name suffixes in the directory of the initial ChangeLog
1086 file were isearch was started."
1087 (let* ((name (change-log-name))
1088 (files (cons name (sort (file-expand-wildcards
1089 (concat name "[-.][0-9]*"))
1090 (lambda (a b)
1091 ;; The file's extension may not have a valid
1092 ;; version form (e.g. VC backup revisions).
1093 (ignore-errors
1094 (version< (substring b (length name))
1095 (substring a (length name))))))))
1096 (files (if isearch-forward files (reverse files))))
1097 (find-file-noselect
1098 (if wrap
1099 (car files)
1100 (cadr (member (file-name-nondirectory (buffer-file-name buffer))
1101 files))))))
1103 (defun change-log-fill-forward-paragraph (n)
1104 "Cut paragraphs so filling preserves open parentheses at beginning of lines."
1105 (let (;; Add lines starting with whitespace followed by a left paren or an
1106 ;; asterisk.
1107 (paragraph-start (concat paragraph-start "\\|\\s *\\(?:\\s(\\|\\*\\)")))
1108 (forward-paragraph n)))
1110 (defcustom add-log-current-defun-header-regexp
1111 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
1112 "Heuristic regexp used by `add-log-current-defun' for unknown major modes.
1113 The regexp's first submatch is placed in the ChangeLog entry, in
1114 parentheses."
1115 :type 'regexp
1116 :group 'change-log)
1118 ;;;###autoload
1119 (defvar add-log-lisp-like-modes
1120 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode)
1121 "*Modes that look like Lisp to `add-log-current-defun'.")
1123 ;;;###autoload
1124 (defvar add-log-c-like-modes
1125 '(c-mode c++-mode c++-c-mode objc-mode)
1126 "*Modes that look like C to `add-log-current-defun'.")
1128 ;;;###autoload
1129 (defvar add-log-tex-like-modes
1130 '(TeX-mode plain-TeX-mode LaTeX-mode tex-mode)
1131 "*Modes that look like TeX to `add-log-current-defun'.")
1133 (declare-function c-cpp-define-name "cc-cmds" ())
1134 (declare-function c-defun-name "cc-cmds" ())
1136 ;;;###autoload
1137 (defun add-log-current-defun ()
1138 "Return name of function definition point is in, or nil.
1140 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
1141 Texinfo (@node titles) and Perl.
1143 Other modes are handled by a heuristic that looks in the 10K before
1144 point for uppercase headings starting in the first column or
1145 identifiers followed by `:' or `='. See variables
1146 `add-log-current-defun-header-regexp' and
1147 `add-log-current-defun-function'.
1149 Has a preference of looking backwards."
1150 (condition-case nil
1151 (save-excursion
1152 (let ((location (point)))
1153 (cond (add-log-current-defun-function
1154 (funcall add-log-current-defun-function))
1155 ((apply 'derived-mode-p add-log-lisp-like-modes)
1156 ;; If we are now precisely at the beginning of a defun,
1157 ;; make sure beginning-of-defun finds that one
1158 ;; rather than the previous one.
1159 (or (eobp) (forward-char 1))
1160 (beginning-of-defun)
1161 ;; Make sure we are really inside the defun found,
1162 ;; not after it.
1163 (when (and (looking-at "\\s(")
1164 (progn (end-of-defun)
1165 (< location (point)))
1166 (progn (forward-sexp -1)
1167 (>= location (point))))
1168 (if (looking-at "\\s(")
1169 (forward-char 1))
1170 ;; Skip the defining construct name, typically "defun"
1171 ;; or "defvar".
1172 (forward-sexp 1)
1173 ;; The second element is usually a symbol being defined.
1174 ;; If it is not, use the first symbol in it.
1175 (skip-chars-forward " \t\n'(")
1176 (buffer-substring-no-properties (point)
1177 (progn (forward-sexp 1)
1178 (point)))))
1179 ((apply 'derived-mode-p add-log-c-like-modes)
1180 (or (c-cpp-define-name)
1181 (c-defun-name)))
1182 ((apply #'derived-mode-p add-log-tex-like-modes)
1183 (if (re-search-backward
1184 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
1185 nil t)
1186 (progn
1187 (goto-char (match-beginning 0))
1188 (buffer-substring-no-properties
1189 (1+ (point)) ; without initial backslash
1190 (line-end-position)))))
1191 ((derived-mode-p 'texinfo-mode)
1192 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
1193 (match-string-no-properties 1)))
1194 ((derived-mode-p 'perl-mode 'cperl-mode)
1195 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t)
1196 (match-string-no-properties 1)))
1197 ;; Emacs's autoconf-mode installs its own
1198 ;; `add-log-current-defun-function'. This applies to
1199 ;; a different mode apparently for editing .m4
1200 ;; autoconf source.
1201 ((derived-mode-p 'autoconf-mode)
1202 (if (re-search-backward
1203 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t)
1204 (match-string-no-properties 3)))
1206 ;; If all else fails, try heuristics
1207 (let (case-fold-search
1208 result)
1209 (end-of-line)
1210 (when (re-search-backward
1211 add-log-current-defun-header-regexp
1212 (- (point) 10000)
1214 (setq result (or (match-string-no-properties 1)
1215 (match-string-no-properties 0)))
1216 ;; Strip whitespace away
1217 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
1218 result)
1219 (setq result (match-string-no-properties 1 result)))
1220 result))))))
1221 (error nil)))
1223 (defvar change-log-get-method-definition-md)
1225 ;; Subroutine used within change-log-get-method-definition.
1226 ;; Add the last match in the buffer to the end of `md',
1227 ;; followed by the string END; move to the end of that match.
1228 (defun change-log-get-method-definition-1 (end)
1229 (setq change-log-get-method-definition-md
1230 (concat change-log-get-method-definition-md
1231 (match-string 1)
1232 end))
1233 (goto-char (match-end 0)))
1235 (defun change-log-get-method-definition ()
1236 "For Objective C, return the method name if we are in a method."
1237 (let ((change-log-get-method-definition-md "["))
1238 (save-excursion
1239 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
1240 (change-log-get-method-definition-1 " ")))
1241 (save-excursion
1242 (cond
1243 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
1244 (change-log-get-method-definition-1 "")
1245 (while (not (looking-at "[{;]"))
1246 (looking-at
1247 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
1248 (change-log-get-method-definition-1 ""))
1249 (concat change-log-get-method-definition-md "]"))))))
1251 (autoload 'timezone-make-date-sortable "timezone")
1253 (defun change-log-sortable-date-at ()
1254 "Return date of log entry in a consistent form for sorting.
1255 Point is assumed to be at the start of the entry."
1256 (if (looking-at change-log-start-entry-re)
1257 (let ((date (match-string-no-properties 0)))
1258 (if date
1259 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
1260 (concat (match-string 1 date) (match-string 2 date)
1261 (match-string 3 date))
1262 (ignore-errors (timezone-make-date-sortable date)))))
1263 (error "Bad date")))
1265 (defun change-log-resolve-conflict ()
1266 "Function to be used in `smerge-resolve-function'."
1267 (save-excursion
1268 (save-restriction
1269 (narrow-to-region (match-beginning 0) (match-end 0))
1270 (let ((mb1 (match-beginning 1))
1271 (me1 (match-end 1))
1272 (mb3 (match-beginning 3))
1273 (me3 (match-end 3))
1274 (tmp1 (generate-new-buffer " *changelog-resolve-1*"))
1275 (tmp2 (generate-new-buffer " *changelog-resolve-2*")))
1276 (unwind-protect
1277 (let ((buf (current-buffer)))
1278 (with-current-buffer tmp1
1279 (change-log-mode)
1280 (insert-buffer-substring buf mb1 me1))
1281 (with-current-buffer tmp2
1282 (change-log-mode)
1283 (insert-buffer-substring buf mb3 me3)
1284 ;; Do the merge here instead of inside `buf' so as to be
1285 ;; more robust in case change-log-merge fails.
1286 (change-log-merge tmp1))
1287 (goto-char (point-max))
1288 (delete-region (point-min)
1289 (prog1 (point)
1290 (insert-buffer-substring tmp2))))
1291 (kill-buffer tmp1)
1292 (kill-buffer tmp2))))))
1294 ;;;###autoload
1295 (defun change-log-merge (other-log)
1296 "Merge the contents of change log file OTHER-LOG with this buffer.
1297 Both must be found in Change Log mode (since the merging depends on
1298 the appropriate motion commands). OTHER-LOG can be either a file name
1299 or a buffer.
1301 Entries are inserted in chronological order. Both the current and
1302 old-style time formats for entries are supported."
1303 (interactive "*fLog file name to merge: ")
1304 (if (not (derived-mode-p 'change-log-mode))
1305 (error "Not in Change Log mode"))
1306 (let ((other-buf (if (bufferp other-log) other-log
1307 (find-file-noselect other-log)))
1308 (buf (current-buffer))
1309 date1 start end)
1310 (save-excursion
1311 (goto-char (point-min))
1312 (set-buffer other-buf)
1313 (goto-char (point-min))
1314 (if (not (derived-mode-p 'change-log-mode))
1315 (error "%s not found in Change Log mode" other-log))
1316 ;; Loop through all the entries in OTHER-LOG.
1317 (while (not (eobp))
1318 (setq date1 (change-log-sortable-date-at))
1319 (setq start (point)
1320 end (progn (forward-page) (point)))
1321 ;; Look for an entry in original buffer that isn't later.
1322 (with-current-buffer buf
1323 (while (and (not (eobp))
1324 (string< date1 (change-log-sortable-date-at)))
1325 (forward-page))
1326 (if (not (eobp))
1327 (insert-buffer-substring other-buf start end)
1328 ;; At the end of the original buffer, insert a newline to
1329 ;; separate entries and then the rest of the file being
1330 ;; merged.
1331 (unless (or (bobp)
1332 (and (= ?\n (char-before))
1333 (or (<= (1- (point)) (point-min))
1334 (= ?\n (char-before (1- (point)))))))
1335 (insert (if use-hard-newlines hard-newline "\n")))
1336 ;; Move to the end of it to terminate outer loop.
1337 (with-current-buffer other-buf
1338 (goto-char (point-max)))
1339 (insert-buffer-substring other-buf start)))))))
1341 (defun change-log-beginning-of-defun ()
1342 (re-search-backward change-log-start-entry-re nil 'move))
1344 (defun change-log-end-of-defun ()
1345 ;; Look back and if there is no entry there it means we are before
1346 ;; the first ChangeLog entry, so go forward until finding one.
1347 (unless (save-excursion (re-search-backward change-log-start-entry-re nil t))
1348 (re-search-forward change-log-start-entry-re nil t))
1350 ;; In case we are at the end of log entry going forward a line will
1351 ;; make us find the next entry when searching. If we are inside of
1352 ;; an entry going forward a line will still keep the point inside
1353 ;; the same entry.
1354 (forward-line 1)
1356 ;; In case we are at the beginning of an entry, move past it.
1357 (when (looking-at change-log-start-entry-re)
1358 (goto-char (match-end 0))
1359 (forward-line 1))
1361 ;; Search for the start of the next log entry. Go to the end of the
1362 ;; buffer if we could not find a next entry.
1363 (when (re-search-forward change-log-start-entry-re nil 'move)
1364 (goto-char (match-beginning 0))
1365 (forward-line -1)))
1367 (provide 'add-log)
1369 ;;; add-log.el ends here