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