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