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