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