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