Commit | Line | Data |
---|---|---|
5abdc915 | 1 | ;;; add-log.el --- change log maintenance commands for Emacs |
84fc2cfa | 2 | |
0d30b337 | 3 | ;; Copyright (C) 1985, 1986, 1988, 1993, 1994, 1997, 1998, 2000, 2002, |
97f4e87c | 4 | ;; 2003, 2004, 2005, 2006 Free Software Foundation, Inc. |
84fc2cfa | 5 | |
6228c05b | 6 | ;; Maintainer: FSF |
df63ae66 | 7 | ;; Keywords: tools |
e9571d2a | 8 | |
84fc2cfa ER |
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 | |
fd7fa35a | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
84fc2cfa ER |
14 | ;; 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 | |
b578f267 | 22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
84fc2cfa | 25 | |
e41b2db1 ER |
26 | ;;; Commentary: |
27 | ||
28 | ;; This facility is documented in the Emacs Manual. | |
29 | ||
fd7fa35a | 30 | ;;; Code: |
84fc2cfa | 31 | |
776d8e16 | 32 | (eval-when-compile |
5960adc7 | 33 | (require 'timezone)) |
3697b807 | 34 | |
fcad5199 | 35 | (defgroup change-log nil |
7b297602 | 36 | "Change log maintenance." |
fcad5199 | 37 | :group 'tools |
3697b807 | 38 | :link '(custom-manual "(emacs)Change Log") |
fcad5199 RS |
39 | :prefix "change-log-" |
40 | :prefix "add-log-") | |
84fc2cfa | 41 | |
fcad5199 RS |
42 | |
43 | (defcustom change-log-default-name nil | |
44 | "*Name of a change log file for \\[add-change-log-entry]." | |
45 | :type '(choice (const :tag "default" nil) | |
46 | string) | |
47 | :group 'change-log) | |
48 | ||
c3979f12 DL |
49 | (defcustom change-log-mode-hook nil |
50 | "Normal hook run by `change-log-mode'." | |
51 | :type 'hook | |
52 | :group 'change-log) | |
53 | ||
c882e115 RS |
54 | ;; Many modes set this variable, so avoid warnings. |
55 | ;;;###autoload | |
fcad5199 | 56 | (defcustom add-log-current-defun-function nil |
5960adc7 DL |
57 | "*If non-nil, function to guess name of surrounding function. |
58 | It is used by `add-log-current-defun' in preference to built-in rules. | |
59 | Returns function's name as a string, or nil if outside a function." | |
cea3855a | 60 | :type '(choice (const nil) function) |
fcad5199 | 61 | :group 'change-log) |
6258d3af | 62 | |
29db528b | 63 | ;;;###autoload |
fcad5199 | 64 | (defcustom add-log-full-name nil |
02ec1592 | 65 | "*Full name of user, for inclusion in ChangeLog daily headers. |
075a6629 | 66 | This defaults to the value returned by the function `user-full-name'." |
fcad5199 RS |
67 | :type '(choice (const :tag "Default" nil) |
68 | string) | |
69 | :group 'change-log) | |
02ec1592 | 70 | |
29db528b | 71 | ;;;###autoload |
fcad5199 | 72 | (defcustom add-log-mailing-address nil |
eba72fc1 | 73 | "*Email addresses of user, for inclusion in ChangeLog headers. |
c1b3ae42 CW |
74 | This defaults to the value of `user-mail-address'. In addition to |
75 | being a simple string, this value can also be a list. All elements | |
76 | will be recognized as referring to the same user; when creating a new | |
77 | ChangeLog entry, one element will be chosen at random." | |
fcad5199 | 78 | :type '(choice (const :tag "Default" nil) |
24f4201f MR |
79 | (string :tag "String") |
80 | (repeat :tag "List of Strings" string)) | |
fcad5199 RS |
81 | :group 'change-log) |
82 | ||
df63ae66 RS |
83 | (defcustom add-log-time-format 'add-log-iso8601-time-string |
84 | "*Function that defines the time format. | |
85 | For example, `add-log-iso8601-time-string', which gives the | |
86 | date in international ISO 8601 format, | |
87 | and `current-time-string' are two valid values." | |
88 | :type '(radio (const :tag "International ISO 8601 format" | |
89 | add-log-iso8601-time-string) | |
90 | (const :tag "Old format, as returned by `current-time-string'" | |
91 | current-time-string) | |
92 | (function :tag "Other")) | |
93 | :group 'change-log) | |
02ec1592 | 94 | |
83afd62c | 95 | (defcustom add-log-keep-changes-together nil |
3697b807 DL |
96 | "*If non-nil, normally keep day's log entries for one file together. |
97 | ||
98 | Log entries for a given file made with \\[add-change-log-entry] or | |
99 | \\[add-change-log-entry-other-window] will only be added to others \ | |
100 | for that file made | |
101 | today if this variable is non-nil or that file comes first in today's | |
102 | entries. Otherwise another entry for that file will be started. An | |
103 | original log: | |
104 | ||
105 | * foo (...): ... | |
106 | * bar (...): change 1 | |
83afd62c | 107 | |
3697b807 DL |
108 | in the latter case, \\[add-change-log-entry-other-window] in a \ |
109 | buffer visiting `bar', yields: | |
83afd62c | 110 | |
3697b807 DL |
111 | * bar (...): -!- |
112 | * foo (...): ... | |
113 | * bar (...): change 1 | |
83afd62c | 114 | |
3697b807 | 115 | and in the former: |
83afd62c | 116 | |
3697b807 DL |
117 | * foo (...): ... |
118 | * bar (...): change 1 | |
119 | (...): -!- | |
83afd62c | 120 | |
3697b807 DL |
121 | The NEW-ENTRY arg to `add-change-log-entry' can override the effect of |
122 | this variable." | |
123 | :version "20.3" | |
83afd62c KH |
124 | :type 'boolean |
125 | :group 'change-log) | |
126 | ||
598f34fa SS |
127 | (defcustom add-log-always-start-new-record nil |
128 | "*If non-nil, `add-change-log-entry' will always start a new record." | |
bf247b6e | 129 | :version "22.1" |
598f34fa SS |
130 | :type 'boolean |
131 | :group 'change-log) | |
132 | ||
d68f7f1b SM |
133 | (defcustom add-log-buffer-file-name-function nil |
134 | "*If non-nil, function to call to identify the full filename of a buffer. | |
135 | This function is called with no argument. If this is nil, the default is to | |
136 | use `buffer-file-name'." | |
cea3855a | 137 | :type '(choice (const nil) function) |
d68f7f1b SM |
138 | :group 'change-log) |
139 | ||
666f4056 RS |
140 | (defcustom add-log-file-name-function nil |
141 | "*If non-nil, function to call to identify the filename for a ChangeLog entry. | |
075a6629 DL |
142 | This function is called with one argument, the value of variable |
143 | `buffer-file-name' in that buffer. If this is nil, the default is to | |
144 | use the file's name relative to the directory of the change log file." | |
cea3855a | 145 | :type '(choice (const nil) function) |
666f4056 RS |
146 | :group 'change-log) |
147 | ||
776d8e16 GM |
148 | |
149 | (defcustom change-log-version-info-enabled nil | |
150 | "*If non-nil, enable recording version numbers with the changes." | |
151 | :version "21.1" | |
152 | :type 'boolean | |
153 | :group 'change-log) | |
154 | ||
155 | (defcustom change-log-version-number-regexp-list | |
156 | (let ((re "\\([0-9]+\.[0-9.]+\\)")) | |
157 | (list | |
158 | ;; (defconst ad-version "2.15" | |
159 | (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re) | |
160 | ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp | |
5960adc7 | 161 | (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re))) |
776d8e16 | 162 | "*List of regexps to search for version number. |
5960adc7 | 163 | The version number must be in group 1. |
776d8e16 GM |
164 | Note: The search is conducted only within 10%, at the beginning of the file." |
165 | :version "21.1" | |
166 | :type '(repeat regexp) | |
167 | :group 'change-log) | |
168 | ||
fe735a8d | 169 | (defface change-log-date |
163f7b71 GM |
170 | '((t (:inherit font-lock-string-face))) |
171 | "Face used to highlight dates in date lines." | |
172 | :version "21.1" | |
173 | :group 'change-log) | |
fe735a8d MB |
174 | ;; backward-compatibility alias |
175 | (put 'change-log-date-face 'face-alias 'change-log-date) | |
163f7b71 | 176 | |
fe735a8d | 177 | (defface change-log-name |
163f7b71 GM |
178 | '((t (:inherit font-lock-constant-face))) |
179 | "Face for highlighting author names." | |
180 | :version "21.1" | |
181 | :group 'change-log) | |
fe735a8d MB |
182 | ;; backward-compatibility alias |
183 | (put 'change-log-name-face 'face-alias 'change-log-name) | |
163f7b71 | 184 | |
fe735a8d | 185 | (defface change-log-email |
163f7b71 GM |
186 | '((t (:inherit font-lock-variable-name-face))) |
187 | "Face for highlighting author email addresses." | |
188 | :version "21.1" | |
189 | :group 'change-log) | |
fe735a8d MB |
190 | ;; backward-compatibility alias |
191 | (put 'change-log-email-face 'face-alias 'change-log-email) | |
163f7b71 | 192 | |
fe735a8d | 193 | (defface change-log-file |
163f7b71 GM |
194 | '((t (:inherit font-lock-function-name-face))) |
195 | "Face for highlighting file names." | |
196 | :version "21.1" | |
197 | :group 'change-log) | |
fe735a8d MB |
198 | ;; backward-compatibility alias |
199 | (put 'change-log-file-face 'face-alias 'change-log-file) | |
163f7b71 | 200 | |
fe735a8d | 201 | (defface change-log-list |
163f7b71 GM |
202 | '((t (:inherit font-lock-keyword-face))) |
203 | "Face for highlighting parenthesized lists of functions or variables." | |
204 | :version "21.1" | |
205 | :group 'change-log) | |
fe735a8d MB |
206 | ;; backward-compatibility alias |
207 | (put 'change-log-list-face 'face-alias 'change-log-list) | |
598f34fa | 208 | |
fe735a8d | 209 | (defface change-log-conditionals |
163f7b71 GM |
210 | '((t (:inherit font-lock-variable-name-face))) |
211 | "Face for highlighting conditionals of the form `[...]'." | |
212 | :version "21.1" | |
213 | :group 'change-log) | |
fe735a8d MB |
214 | ;; backward-compatibility alias |
215 | (put 'change-log-conditionals-face 'face-alias 'change-log-conditionals) | |
163f7b71 | 216 | |
fe735a8d | 217 | (defface change-log-function |
163f7b71 GM |
218 | '((t (:inherit font-lock-variable-name-face))) |
219 | "Face for highlighting items of the form `<....>'." | |
220 | :version "21.1" | |
221 | :group 'change-log) | |
fe735a8d MB |
222 | ;; backward-compatibility alias |
223 | (put 'change-log-function-face 'face-alias 'change-log-function) | |
163f7b71 | 224 | |
fe735a8d | 225 | (defface change-log-acknowledgement |
163f7b71 GM |
226 | '((t (:inherit font-lock-comment-face))) |
227 | "Face for highlighting acknowledgments." | |
228 | :version "21.1" | |
229 | :group 'change-log) | |
fe735a8d MB |
230 | ;; backward-compatibility alias |
231 | (put 'change-log-acknowledgement-face 'face-alias 'change-log-acknowledgement) | |
776d8e16 | 232 | |
5f562719 | 233 | (defvar change-log-font-lock-keywords |
5572c1d1 | 234 | '(;; |
080525ae KH |
235 | ;; Date lines, new (2000-01-01) and old (Sat Jan 1 00:00:00 2000) styles. |
236 | ;; Fixme: this regepx is just an approximate one and may match | |
237 | ;; wrongly with a non-date line existing as a random note. In | |
238 | ;; addition, using any kind of fixed setting like this doesn't | |
239 | ;; work if a user customizes add-log-time-format. | |
240 | ("^[0-9-]+ +\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+" | |
241 | (0 'change-log-date-face) | |
97a3278b | 242 | ;; Name and e-mail; some people put e-mail in parens, not angles. |
7397a79f | 243 | ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil |
fe735a8d MB |
244 | (1 'change-log-name) |
245 | (2 'change-log-email))) | |
5572c1d1 SM |
246 | ;; |
247 | ;; File names. | |
9e4b54a0 | 248 | ("^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)" |
fe735a8d | 249 | (2 'change-log-file) |
97a3278b | 250 | ;; Possibly further names in a list: |
fe735a8d | 251 | ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 'change-log-file)) |
97a3278b | 252 | ;; Possibly a parenthesized list of names: |
171c707b | 253 | ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" |
fe735a8d | 254 | nil nil (1 'change-log-list)) |
171c707b | 255 | ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" |
fe735a8d | 256 | nil nil (1 'change-log-list))) |
5572c1d1 SM |
257 | ;; |
258 | ;; Function or variable names. | |
9e4b54a0 | 259 | ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" |
fe735a8d | 260 | (2 'change-log-list) |
171c707b | 261 | ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil |
fe735a8d | 262 | (1 'change-log-list))) |
5572c1d1 SM |
263 | ;; |
264 | ;; Conditionals. | |
fe735a8d | 265 | ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals)) |
5572c1d1 | 266 | ;; |
a8d693d8 | 267 | ;; Function of change. |
fe735a8d | 268 | ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function)) |
a8d693d8 | 269 | ;; |
a3cab9f0 | 270 | ;; Acknowledgements. |
a8e10524 RS |
271 | ;; Don't include plain "From" because that is vague; |
272 | ;; we want to encourage people to say something more specific. | |
c50089c9 RS |
273 | ;; Note that the FSF does not use "Patches by"; our convention |
274 | ;; is to put the name of the author of the changes at the top | |
275 | ;; of the change log entry. | |
9e4b54a0 | 276 | ("\\(^\\( +\\|\t\\)\\| \\)\\(Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)" |
fe735a8d | 277 | 3 'change-log-acknowledgement)) |
5f562719 RS |
278 | "Additional expressions to highlight in Change Log mode.") |
279 | ||
3066d4ad SM |
280 | (defvar change-log-mode-map |
281 | (let ((map (make-sparse-keymap))) | |
282 | (define-key map [?\C-c ?\C-p] 'add-log-edit-prev-comment) | |
283 | (define-key map [?\C-c ?\C-n] 'add-log-edit-next-comment) | |
284 | map) | |
45c50c5d | 285 | "Keymap for Change Log major mode.") |
45c50c5d | 286 | |
51bd1843 EN |
287 | (defvar change-log-time-zone-rule nil |
288 | "Time zone used for calculating change log time stamps. | |
289 | It takes the same format as the TZ argument of `set-time-zone-rule'. | |
290 | If nil, use local time.") | |
291 | ||
df63ae66 | 292 | (defun add-log-iso8601-time-zone (time) |
51bd1843 EN |
293 | (let* ((utc-offset (or (car (current-time-zone time)) 0)) |
294 | (sign (if (< utc-offset 0) ?- ?+)) | |
295 | (sec (abs utc-offset)) | |
296 | (ss (% sec 60)) | |
297 | (min (/ sec 60)) | |
298 | (mm (% min 60)) | |
299 | (hh (/ min 60))) | |
300 | (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d") | |
301 | ((not (zerop mm)) "%c%02d:%02d") | |
302 | (t "%c%02d")) | |
303 | sign hh mm ss))) | |
304 | ||
df63ae66 RS |
305 | (defun add-log-iso8601-time-string () |
306 | (if change-log-time-zone-rule | |
307 | (let ((tz (getenv "TZ")) | |
308 | (now (current-time))) | |
309 | (unwind-protect | |
310 | (progn | |
918f4ac3 | 311 | (set-time-zone-rule change-log-time-zone-rule) |
df63ae66 RS |
312 | (concat |
313 | (format-time-string "%Y-%m-%d " now) | |
314 | (add-log-iso8601-time-zone now))) | |
315 | (set-time-zone-rule tz))) | |
316 | (format-time-string "%Y-%m-%d"))) | |
317 | ||
84fc2cfa | 318 | (defun change-log-name () |
075a6629 | 319 | "Return (system-dependent) default name for a change log file." |
84fc2cfa | 320 | (or change-log-default-name |
513063cf | 321 | (if (eq system-type 'vax-vms) |
4a047d23 RS |
322 | "$CHANGE_LOG$.TXT" |
323 | "ChangeLog"))) | |
84fc2cfa | 324 | |
3066d4ad SM |
325 | (defun add-log-edit-prev-comment (arg) |
326 | "Cycle backward through Log-Edit mode comment history. | |
327 | With a numeric prefix ARG, go back ARG comments." | |
328 | (interactive "*p") | |
329 | (save-restriction | |
330 | (narrow-to-region (point) | |
331 | (if (memq last-command '(add-log-edit-prev-comment | |
332 | add-log-edit-next-comment)) | |
333 | (mark) (point))) | |
334 | (when (fboundp 'log-edit-previous-comment) | |
335 | (log-edit-previous-comment arg) | |
336 | (indent-region (point-min) (point-max)) | |
337 | (goto-char (point-min)) | |
338 | (unless (save-restriction (widen) (bolp)) | |
339 | (delete-region (point) (progn (skip-chars-forward " \t\n") (point)))) | |
340 | (set-mark (point-min)) | |
341 | (goto-char (point-max)) | |
342 | (delete-region (point) (progn (skip-chars-backward " \t\n") (point)))))) | |
343 | ||
344 | (defun add-log-edit-next-comment (arg) | |
345 | "Cycle forward through Log-Edit mode comment history. | |
346 | With a numeric prefix ARG, go back ARG comments." | |
347 | (interactive "*p") | |
348 | (add-log-edit-prev-comment (- arg))) | |
349 | ||
287d149f | 350 | ;;;###autoload |
84fc2cfa ER |
351 | (defun prompt-for-change-log-name () |
352 | "Prompt for a change log name." | |
117aaf60 KH |
353 | (let* ((default (change-log-name)) |
354 | (name (expand-file-name | |
355 | (read-file-name (format "Log file (default %s): " default) | |
356 | nil default)))) | |
357 | ;; Handle something that is syntactically a directory name. | |
358 | ;; Look for ChangeLog or whatever in that directory. | |
359 | (if (string= (file-name-nondirectory name) "") | |
360 | (expand-file-name (file-name-nondirectory default) | |
361 | name) | |
362 | ;; Handle specifying a file that is a directory. | |
363 | (if (file-directory-p name) | |
364 | (expand-file-name (file-name-nondirectory default) | |
365 | (file-name-as-directory name)) | |
366 | name)))) | |
84fc2cfa | 367 | |
776d8e16 | 368 | (defun change-log-version-number-search () |
5960adc7 DL |
369 | "Return version number of current buffer's file. |
370 | This is the value returned by `vc-workfile-version' or, if that is | |
371 | nil, by matching `change-log-version-number-regexp-list'." | |
776d8e16 | 372 | (let* ((size (buffer-size)) |
fc9b0554 | 373 | (limit |
5960adc7 DL |
374 | ;; The version number can be anywhere in the file, but |
375 | ;; restrict search to the file beginning: 10% should be | |
376 | ;; enough to prevent some mishits. | |
776d8e16 | 377 | ;; |
5960adc7 DL |
378 | ;; Apply percentage only if buffer size is bigger than |
379 | ;; approx 100 lines. | |
fc9b0554 | 380 | (if (> size (* 100 80)) (+ (point) (/ size 10))))) |
d68f7f1b | 381 | (or (and buffer-file-name (vc-workfile-version buffer-file-name)) |
5960adc7 DL |
382 | (save-restriction |
383 | (widen) | |
fc9b0554 SM |
384 | (let ((regexps change-log-version-number-regexp-list) |
385 | version) | |
5960adc7 DL |
386 | (while regexps |
387 | (save-excursion | |
388 | (goto-char (point-min)) | |
fc9b0554 | 389 | (when (re-search-forward (pop regexps) limit t) |
5960adc7 | 390 | (setq version (match-string 1) |
fc9b0554 SM |
391 | regexps nil)))) |
392 | version))))) | |
776d8e16 GM |
393 | |
394 | ||
45a13f0d | 395 | ;;;###autoload |
d68f7f1b | 396 | (defun find-change-log (&optional file-name buffer-file) |
45a13f0d | 397 | "Find a change log file for \\[add-change-log-entry] and return the name. |
a82e2ed5 RS |
398 | |
399 | Optional arg FILE-NAME specifies the file to use. | |
de98fcaf | 400 | If FILE-NAME is nil, use the value of `change-log-default-name'. |
218cf475 | 401 | If `change-log-default-name' is nil, behave as though it were 'ChangeLog' |
de98fcaf RS |
402 | \(or whatever we use on this operating system). |
403 | ||
218cf475 | 404 | If `change-log-default-name' contains a leading directory component, then |
513063cf | 405 | simply find it in the current directory. Otherwise, search in the current |
de98fcaf | 406 | directory and its successive parents for a file so named. |
45a13f0d RM |
407 | |
408 | Once a file is found, `change-log-default-name' is set locally in the | |
d68f7f1b SM |
409 | current buffer to the complete file name. |
410 | Optional arg BUFFER-FILE overrides `buffer-file-name'." | |
a82e2ed5 RS |
411 | ;; If user specified a file name or if this buffer knows which one to use, |
412 | ;; just use that. | |
45a13f0d | 413 | (or file-name |
de98fcaf RS |
414 | (setq file-name (and change-log-default-name |
415 | (file-name-directory change-log-default-name) | |
416 | change-log-default-name)) | |
a82e2ed5 RS |
417 | (progn |
418 | ;; Chase links in the source file | |
419 | ;; and use the change log in the dir where it points. | |
d68f7f1b | 420 | (setq file-name (or (and (or buffer-file buffer-file-name) |
a82e2ed5 | 421 | (file-name-directory |
d68f7f1b SM |
422 | (file-chase-links |
423 | (or buffer-file buffer-file-name)))) | |
a82e2ed5 RS |
424 | default-directory)) |
425 | (if (file-directory-p file-name) | |
426 | (setq file-name (expand-file-name (change-log-name) file-name))) | |
427 | ;; Chase links before visiting the file. | |
428 | ;; This makes it easier to use a single change log file | |
429 | ;; for several related directories. | |
430 | (setq file-name (file-chase-links file-name)) | |
431 | (setq file-name (expand-file-name file-name)) | |
432 | ;; Move up in the dir hierarchy till we find a change log file. | |
433 | (let ((file1 file-name) | |
434 | parent-dir) | |
435 | (while (and (not (or (get-file-buffer file1) (file-exists-p file1))) | |
436 | (progn (setq parent-dir | |
437 | (file-name-directory | |
438 | (directory-file-name | |
439 | (file-name-directory file1)))) | |
440 | ;; Give up if we are already at the root dir. | |
441 | (not (string= (file-name-directory file1) | |
442 | parent-dir)))) | |
443 | ;; Move up to the parent dir and try again. | |
513063cf | 444 | (setq file1 (expand-file-name |
a82e2ed5 RS |
445 | (file-name-nondirectory (change-log-name)) |
446 | parent-dir))) | |
447 | ;; If we found a change log in a parent, use that. | |
448 | (if (or (get-file-buffer file1) (file-exists-p file1)) | |
449 | (setq file-name file1))))) | |
450 | ;; Make a local variable in this buffer so we needn't search again. | |
451 | (set (make-local-variable 'change-log-default-name) file-name) | |
452 | file-name) | |
45a13f0d | 453 | |
2eb7ccf4 SM |
454 | (defun add-log-file-name (buffer-file log-file) |
455 | ;; Never want to add a change log entry for the ChangeLog file itself. | |
456 | (unless (or (null buffer-file) (string= buffer-file log-file)) | |
d68f7f1b SM |
457 | (if add-log-file-name-function |
458 | (funcall add-log-file-name-function buffer-file) | |
459 | (setq buffer-file | |
460 | (if (string-match | |
461 | (concat "^" (regexp-quote (file-name-directory log-file))) | |
462 | buffer-file) | |
463 | (substring buffer-file (match-end 0)) | |
464 | (file-name-nondirectory buffer-file))) | |
465 | ;; If we have a backup file, it's presumably because we're | |
466 | ;; comparing old and new versions (e.g. for deleted | |
467 | ;; functions) and we'll want to use the original name. | |
468 | (if (backup-file-name-p buffer-file) | |
469 | (file-name-sans-versions buffer-file) | |
470 | buffer-file)))) | |
2eb7ccf4 | 471 | |
84fc2cfa | 472 | ;;;###autoload |
287d149f | 473 | (defun add-change-log-entry (&optional whoami file-name other-window new-entry) |
d882f144 | 474 | "Find change log file, and add an entry for today and an item for this file. |
83afd62c | 475 | Optional arg WHOAMI (interactive prefix) non-nil means prompt for user |
d9ee5172 | 476 | name and email (stored in `add-log-full-name' and `add-log-mailing-address'). |
83afd62c | 477 | |
d882f144 RS |
478 | Second arg FILE-NAME is file name of the change log. |
479 | If nil, use the value of `change-log-default-name'. | |
480 | ||
287d149f | 481 | Third arg OTHER-WINDOW non-nil means visit in other window. |
d882f144 | 482 | |
287d149f | 483 | Fourth arg NEW-ENTRY non-nil means always create a new entry at the front; |
3697b807 DL |
484 | never append to an existing entry. Option `add-log-keep-changes-together' |
485 | otherwise affects whether a new entry is created. | |
486 | ||
598f34fa SS |
487 | Option `add-log-always-start-new-record' non-nil means always create a |
488 | new record, even when the last record was made on the same date and by | |
489 | the same person. | |
490 | ||
d882f144 RS |
491 | The change log file can start with a copyright notice and a copying |
492 | permission notice. The first blank line indicates the end of these | |
493 | notices. | |
494 | ||
3697b807 DL |
495 | Today's date is calculated according to `change-log-time-zone-rule' if |
496 | non-nil, otherwise in local time." | |
84fc2cfa ER |
497 | (interactive (list current-prefix-arg |
498 | (prompt-for-change-log-name))) | |
d68f7f1b SM |
499 | (let* ((defun (add-log-current-defun)) |
500 | (version (and change-log-version-info-enabled | |
501 | (change-log-version-number-search))) | |
8f530b95 SM |
502 | (buf-file-name (if add-log-buffer-file-name-function |
503 | (funcall add-log-buffer-file-name-function) | |
504 | buffer-file-name)) | |
505 | (buffer-file (if buf-file-name (expand-file-name buf-file-name))) | |
dd816e0d | 506 | (file-name (expand-file-name (find-change-log file-name buffer-file))) |
d882f144 RS |
507 | ;; Set ITEM to the file name to use in the new item. |
508 | (item (add-log-file-name buffer-file file-name)) | |
563631fe RS |
509 | bound |
510 | (full-name (or add-log-full-name (user-full-name))) | |
511 | (mailing-address (or add-log-mailing-address user-mail-address))) | |
512 | ||
513 | (if whoami | |
514 | (progn | |
3981e5b5 | 515 | (setq full-name (read-string "Full name: " full-name)) |
563631fe RS |
516 | ;; Note that some sites have room and phone number fields in |
517 | ;; full name which look silly when inserted. Rather than do | |
518 | ;; anything about that here, let user give prefix argument so that | |
519 | ;; s/he can edit the full name field in prompter if s/he wants. | |
520 | (setq mailing-address | |
3981e5b5 | 521 | (read-string "Mailing address: " mailing-address)))) |
2eb7ccf4 | 522 | |
7cd017ba SM |
523 | (unless (equal file-name buffer-file-name) |
524 | (if (or other-window (window-dedicated-p (selected-window))) | |
525 | (find-file-other-window file-name) | |
526 | (find-file file-name))) | |
675a998f RS |
527 | (or (eq major-mode 'change-log-mode) |
528 | (change-log-mode)) | |
84fc2cfa ER |
529 | (undo-boundary) |
530 | (goto-char (point-min)) | |
d882f144 RS |
531 | |
532 | ;; If file starts with a copyright and permission notice, skip them. | |
533 | ;; Assume they end at first blank line. | |
534 | (when (looking-at "Copyright") | |
535 | (search-forward "\n\n") | |
536 | (skip-chars-forward "\n")) | |
537 | ||
538 | ;; Advance into first entry if it is usable; else make new one. | |
c1b3ae42 CW |
539 | (let ((new-entries (mapcar (lambda (addr) |
540 | (concat (funcall add-log-time-format) | |
563631fe | 541 | " " full-name |
c1b3ae42 | 542 | " <" addr ">")) |
563631fe RS |
543 | (if (consp mailing-address) |
544 | mailing-address | |
545 | (list mailing-address))))) | |
598f34fa | 546 | (if (and (not add-log-always-start-new-record) |
c1b3ae42 CW |
547 | (let ((hit nil)) |
548 | (dolist (entry new-entries hit) | |
549 | (when (looking-at (regexp-quote entry)) | |
550 | (setq hit t))))) | |
51bd1843 | 551 | (forward-line 1) |
c1b3ae42 CW |
552 | (insert (nth (random (length new-entries)) |
553 | new-entries) | |
97f4e87c RF |
554 | (if use-hard-newlines hard-newline "\n") |
555 | (if use-hard-newlines hard-newline "\n")) | |
d882f144 | 556 | (forward-line -1))) |
82f4acaf | 557 | |
d882f144 RS |
558 | ;; Determine where we should stop searching for a usable |
559 | ;; item to add to, within this entry. | |
3697b807 | 560 | (setq bound |
d882f144 | 561 | (save-excursion |
3697b807 DL |
562 | (if (looking-at "\n*[^\n* \t]") |
563 | (skip-chars-forward "\n") | |
564 | (if add-log-keep-changes-together | |
565 | (forward-page) ; page delimits entries for date | |
566 | (forward-paragraph))) ; paragraph delimits entries for file | |
567 | (point))) | |
d882f144 RS |
568 | |
569 | ;; Now insert the new line for this item. | |
3697b807 | 570 | (cond ((re-search-forward "^\\s *\\*\\s *$" bound t) |
d882f144 RS |
571 | ;; Put this file name into the existing empty item. |
572 | (if item | |
573 | (insert item))) | |
287d149f | 574 | ((and (not new-entry) |
e172f546 KH |
575 | (let (case-fold-search) |
576 | (re-search-forward | |
d882f144 | 577 | (concat (regexp-quote (concat "* " item)) |
e172f546 KH |
578 | ;; Don't accept `foo.bar' when |
579 | ;; looking for `foo': | |
580 | "\\(\\s \\|[(),:]\\)") | |
3697b807 | 581 | bound t))) |
d882f144 | 582 | ;; Add to the existing item for the same file. |
82f4acaf | 583 | (re-search-forward "^\\s *$\\|^\\s \\*") |
e172f546 | 584 | (goto-char (match-beginning 0)) |
3697b807 DL |
585 | ;; Delete excess empty lines; make just 2. |
586 | (while (and (not (eobp)) (looking-at "^\\s *$")) | |
5960adc7 | 587 | (delete-region (point) (line-beginning-position 2))) |
97f4e87c RF |
588 | (insert (if use-hard-newlines hard-newline "\n") |
589 | (if use-hard-newlines hard-newline "\n")) | |
3697b807 DL |
590 | (forward-line -2) |
591 | (indent-relative-maybe)) | |
21d7e080 | 592 | (t |
d882f144 | 593 | ;; Make a new item. |
dd309224 RM |
594 | (while (looking-at "\\sW") |
595 | (forward-line 1)) | |
09b389d0 | 596 | (while (and (not (eobp)) (looking-at "^\\s *$")) |
5960adc7 | 597 | (delete-region (point) (line-beginning-position 2))) |
97f4e87c RF |
598 | (insert (if use-hard-newlines hard-newline "\n") |
599 | (if use-hard-newlines hard-newline "\n") | |
600 | (if use-hard-newlines hard-newline "\n")) | |
1832dbd1 | 601 | (forward-line -2) |
dd309224 | 602 | (indent-to left-margin) |
918f4ac3 | 603 | (insert "* ") |
d882f144 | 604 | (if item (insert item)))) |
1832dbd1 | 605 | ;; Now insert the function name, if we have one. |
d882f144 | 606 | ;; Point is at the item for this file, |
21d7e080 | 607 | ;; either at the end of the line or at the first blank line. |
5a9ac14b SM |
608 | (if (not defun) |
609 | ;; No function name, so put in a colon unless we have just a star. | |
610 | (unless (save-excursion | |
611 | (beginning-of-line 1) | |
612 | (looking-at "\\s *\\(\\*\\s *\\)?$")) | |
613 | (insert ": ") | |
7b297602 | 614 | (if version (insert version ?\s))) |
5a9ac14b SM |
615 | ;; Make it easy to get rid of the function name. |
616 | (undo-boundary) | |
5960adc7 DL |
617 | (unless (save-excursion |
618 | (beginning-of-line 1) | |
5a9ac14b | 619 | (looking-at "\\s *$")) |
7b297602 | 620 | (insert ?\s)) |
5a9ac14b SM |
621 | ;; See if the prev function name has a message yet or not. |
622 | ;; If not, merge the two items. | |
623 | (let ((pos (point-marker))) | |
624 | (skip-syntax-backward " ") | |
625 | (skip-chars-backward "):") | |
626 | (if (and (looking-at "):") | |
fc9b0554 SM |
627 | (let ((pos (save-excursion (backward-sexp 1) (point)))) |
628 | (when (equal (buffer-substring pos (point)) defun) | |
629 | (delete-region pos (point))) | |
630 | (> fill-column (+ (current-column) (length defun) 4)))) | |
631 | (progn (skip-chars-backward ", ") | |
632 | (delete-region (point) pos) | |
633 | (unless (memq (char-before) '(?\()) (insert ", "))) | |
5a9ac14b SM |
634 | (if (looking-at "):") |
635 | (delete-region (+ 1 (point)) (line-end-position))) | |
636 | (goto-char pos) | |
637 | (insert "(")) | |
638 | (set-marker pos nil)) | |
639 | (insert defun "): ") | |
7b297602 | 640 | (if version (insert version ?\s))))) |
84fc2cfa | 641 | |
84fc2cfa ER |
642 | ;;;###autoload |
643 | (defun add-change-log-entry-other-window (&optional whoami file-name) | |
d882f144 RS |
644 | "Find change log file in other window and add entry and item. |
645 | This is just like `add-change-log-entry' except that it displays | |
646 | the change log file in another window." | |
84fc2cfa ER |
647 | (interactive (if current-prefix-arg |
648 | (list current-prefix-arg | |
649 | (prompt-for-change-log-name)))) | |
650 | (add-change-log-entry whoami file-name t)) | |
82f4acaf | 651 | ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window) |
84fc2cfa | 652 | |
fc9b0554 SM |
653 | (defvar add-log-indent-text 0) |
654 | ||
655 | (defun add-log-indent () | |
656 | (let* ((indent | |
657 | (save-excursion | |
658 | (beginning-of-line) | |
659 | (skip-chars-forward " \t") | |
660 | (cond | |
661 | ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>$") | |
662 | ;; Matching the output of add-log-time-format is difficult, | |
663 | ;; but I'll get it has at least two adjacent digits. | |
664 | (string-match "[[:digit:]][[:digit:]]" (match-string 1))) | |
665 | 0) | |
666 | ((looking-at "[^*(]") | |
667 | (+ (current-left-margin) add-log-indent-text)) | |
668 | (t (current-left-margin))))) | |
669 | (pos (save-excursion (indent-line-to indent) (point)))) | |
670 | (if (> pos (point)) (goto-char pos)))) | |
671 | ||
672 | ||
3066d4ad SM |
673 | (defvar smerge-resolve-function) |
674 | ||
1da56800 | 675 | ;;;###autoload |
7cd017ba | 676 | (define-derived-mode change-log-mode text-mode "Change Log" |
eb8c3be9 | 677 | "Major mode for editing change logs; like Indented Text Mode. |
09b389d0 | 678 | Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74. |
3697b807 | 679 | New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window]. |
5516387c | 680 | Each entry behaves as a paragraph, and the entries for one day as a page. |
3066d4ad SM |
681 | Runs `change-log-mode-hook'. |
682 | \\{change-log-mode-map}" | |
7cd017ba | 683 | (setq left-margin 8 |
4f675a8c | 684 | fill-column 74 |
60f10a06 KH |
685 | indent-tabs-mode t |
686 | tab-width 8) | |
687 | (set (make-local-variable 'fill-paragraph-function) | |
688 | 'change-log-fill-paragraph) | |
fc9b0554 SM |
689 | (set (make-local-variable 'indent-line-function) 'add-log-indent) |
690 | (set (make-local-variable 'tab-always-indent) nil) | |
4b7d4d0d DL |
691 | ;; We really do want "^" in paragraph-start below: it is only the |
692 | ;; lines that begin at column 0 (despite the left-margin of 8) that | |
693 | ;; we are looking for. Adding `* ' allows eliding the blank line | |
694 | ;; between entries for different files. | |
c9cfb0f2 | 695 | (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<") |
4b7d4d0d | 696 | (set (make-local-variable 'paragraph-separate) paragraph-start) |
2c91c85c RS |
697 | ;; Match null string on the date-line so that the date-line |
698 | ;; is grouped with what follows. | |
964141f2 | 699 | (set (make-local-variable 'page-delimiter) "^\\<\\|^\f") |
dd309224 | 700 | (set (make-local-variable 'version-control) 'never) |
7cd017ba SM |
701 | (set (make-local-variable 'smerge-resolve-function) |
702 | 'change-log-resolve-conflict) | |
dd309224 | 703 | (set (make-local-variable 'adaptive-fill-regexp) "\\s *") |
4b286eca | 704 | (set (make-local-variable 'font-lock-defaults) |
7cd017ba | 705 | '(change-log-font-lock-keywords t nil nil backward-paragraph))) |
21d7e080 | 706 | |
287d149f RM |
707 | ;; It might be nice to have a general feature to replace this. The idea I |
708 | ;; have is a variable giving a regexp matching text which should not be | |
709 | ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(". | |
710 | ;; But I don't feel up to implementing that today. | |
711 | (defun change-log-fill-paragraph (&optional justify) | |
712 | "Fill the paragraph, but preserve open parentheses at beginning of lines. | |
713 | Prefix arg means justify as well." | |
714 | (interactive "P") | |
8d6467a4 RS |
715 | (let ((end (progn (forward-paragraph) (point))) |
716 | (beg (progn (backward-paragraph) (point))) | |
14ee1953 | 717 | (paragraph-start (concat paragraph-start "\\|\\s *\\s("))) |
8d6467a4 RS |
718 | (fill-region beg end justify) |
719 | t)) | |
287d149f | 720 | \f |
fcad5199 | 721 | (defcustom add-log-current-defun-header-regexp |
a8d693d8 | 722 | "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]" |
fcad5199 RS |
723 | "*Heuristic regexp used by `add-log-current-defun' for unknown major modes." |
724 | :type 'regexp | |
725 | :group 'change-log) | |
21d7e080 | 726 | |
fb644f48 EN |
727 | ;;;###autoload |
728 | (defvar add-log-lisp-like-modes | |
3697b807 | 729 | '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode) |
fb644f48 EN |
730 | "*Modes that look like Lisp to `add-log-current-defun'.") |
731 | ||
732 | ;;;###autoload | |
733 | (defvar add-log-c-like-modes | |
734 | '(c-mode c++-mode c++-c-mode objc-mode) | |
735 | "*Modes that look like C to `add-log-current-defun'.") | |
736 | ||
737 | ;;;###autoload | |
738 | (defvar add-log-tex-like-modes | |
739 | '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode) | |
740 | "*Modes that look like TeX to `add-log-current-defun'.") | |
741 | ||
e332f80b | 742 | ;;;###autoload |
21d7e080 ER |
743 | (defun add-log-current-defun () |
744 | "Return name of function definition point is in, or nil. | |
745 | ||
63314951 | 746 | Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...), |
5960adc7 | 747 | Texinfo (@node titles) and Perl. |
21d7e080 ER |
748 | |
749 | Other modes are handled by a heuristic that looks in the 10K before | |
750 | point for uppercase headings starting in the first column or | |
5960adc7 | 751 | identifiers followed by `:' or `='. See variables |
c1356086 | 752 | `add-log-current-defun-header-regexp' and |
5ef08021 | 753 | `add-log-current-defun-function'. |
21d7e080 ER |
754 | |
755 | Has a preference of looking backwards." | |
2cc0b765 RS |
756 | (condition-case nil |
757 | (save-excursion | |
758 | (let ((location (point))) | |
5960adc7 DL |
759 | (cond (add-log-current-defun-function |
760 | (funcall add-log-current-defun-function)) | |
c1356086 | 761 | ((memq major-mode add-log-lisp-like-modes) |
a0151877 | 762 | ;; If we are now precisely at the beginning of a defun, |
2cc0b765 RS |
763 | ;; make sure beginning-of-defun finds that one |
764 | ;; rather than the previous one. | |
765 | (or (eobp) (forward-char 1)) | |
766 | (beginning-of-defun) | |
5960adc7 DL |
767 | ;; Make sure we are really inside the defun found, |
768 | ;; not after it. | |
42b1fc29 RS |
769 | (when (and (looking-at "\\s(") |
770 | (progn (end-of-defun) | |
771 | (< location (point))) | |
772 | (progn (forward-sexp -1) | |
773 | (>= location (point)))) | |
774 | (if (looking-at "\\s(") | |
775 | (forward-char 1)) | |
776 | ;; Skip the defining construct name, typically "defun" | |
777 | ;; or "defvar". | |
778 | (forward-sexp 1) | |
779 | ;; The second element is usually a symbol being defined. | |
780 | ;; If it is not, use the first symbol in it. | |
63c7727f | 781 | (skip-chars-forward " \t\n'(") |
5960adc7 DL |
782 | (buffer-substring-no-properties (point) |
783 | (progn (forward-sexp 1) | |
784 | (point))))) | |
fb644f48 EN |
785 | ((and (memq major-mode add-log-c-like-modes) |
786 | (save-excursion | |
787 | (beginning-of-line) | |
788 | ;; Use eq instead of = here to avoid | |
789 | ;; error when at bob and char-after | |
790 | ;; returns nil. | |
791 | (while (eq (char-after (- (point) 2)) ?\\) | |
792 | (forward-line -1)) | |
793 | (looking-at "[ \t]*#[ \t]*define[ \t]"))) | |
2cc0b765 RS |
794 | ;; Handle a C macro definition. |
795 | (beginning-of-line) | |
796 | (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above | |
797 | (forward-line -1)) | |
798 | (search-forward "define") | |
799 | (skip-chars-forward " \t") | |
5960adc7 DL |
800 | (buffer-substring-no-properties (point) |
801 | (progn (forward-sexp 1) | |
802 | (point)))) | |
fb644f48 | 803 | ((memq major-mode add-log-c-like-modes) |
2cc0b765 RS |
804 | (beginning-of-line) |
805 | ;; See if we are in the beginning part of a function, | |
806 | ;; before the open brace. If so, advance forward. | |
807 | (while (not (looking-at "{\\|\\(\\s *$\\)")) | |
808 | (forward-line 1)) | |
809 | (or (eobp) | |
810 | (forward-char 1)) | |
8eca85ef RS |
811 | (let (maybe-beg) |
812 | ;; Try to find the containing defun. | |
813 | (beginning-of-defun) | |
814 | (end-of-defun) | |
815 | ;; If the defun we found ends before the desired position, | |
816 | ;; see if there's a DEFUN construct | |
817 | ;; between that end and the desired position. | |
818 | (when (save-excursion | |
819 | (and (> location (point)) | |
bf247b6e | 820 | (re-search-forward "^DEFUN" |
8eca85ef RS |
821 | (save-excursion |
822 | (goto-char location) | |
823 | (line-end-position)) | |
824 | t) | |
825 | (re-search-forward "^{" nil t) | |
826 | (setq maybe-beg (point)))) | |
827 | ;; If so, go to the end of that instead. | |
828 | (goto-char maybe-beg) | |
829 | (end-of-defun))) | |
830 | ;; If the desired position is within the defun we found, | |
831 | ;; find the function name. | |
832 | (when (< location (point)) | |
faf142ec | 833 | ;; Move back over function body. |
5960adc7 | 834 | (backward-sexp 1) |
faf142ec RS |
835 | (let (beg) |
836 | ;; Skip back over typedefs and arglist. | |
837 | ;; Stop at the function definition itself | |
838 | ;; or at the line that follows end of function doc string. | |
5960adc7 | 839 | (forward-line -1) |
5960adc7 | 840 | (while (and (not (bobp)) |
faf142ec RS |
841 | (looking-at "[ \t\n]") |
842 | (not (looking-back "[*]/)\n" (- (point) 4)))) | |
5960adc7 | 843 | (forward-line -1)) |
faf142ec RS |
844 | ;; If we found a doc string, this must be the DEFUN macro |
845 | ;; used in Emacs. Move back to the DEFUN line. | |
846 | (when (looking-back "[*]/)\n" (- (point) 4)) | |
847 | (backward-sexp 1) | |
848 | (beginning-of-line)) | |
849 | ;; Is this a DEFUN construct? And is LOCATION in it? | |
850 | (if (and (looking-at "DEFUN\\b") | |
851 | (>= location (point))) | |
568ec09d SS |
852 | ;; DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, ...) ==> Ffile_name_directory |
853 | ;; DEFUN(POSIX::STREAM-LOCK, stream lockp &key BLOCK SHARED START LENGTH) ==> POSIX::STREAM-LOCK | |
5960adc7 | 854 | (progn |
5960adc7 | 855 | (down-list 1) |
568ec09d SS |
856 | (when (= (char-after (point)) ?\") |
857 | (forward-sexp 1) | |
858 | (search-forward ",")) | |
859 | (skip-syntax-forward " ") | |
5960adc7 DL |
860 | (buffer-substring-no-properties |
861 | (point) | |
568ec09d SS |
862 | (progn (search-forward ",") |
863 | (forward-char -1) | |
864 | (skip-syntax-backward " ") | |
5960adc7 DL |
865 | (point)))) |
866 | (if (looking-at "^[+-]") | |
94b073f3 | 867 | ;; Objective-C |
5960adc7 DL |
868 | (change-log-get-method-definition) |
869 | ;; Ordinary C function syntax. | |
870 | (setq beg (point)) | |
871 | (if (and | |
872 | ;; Protect against "Unbalanced parens" error. | |
873 | (condition-case nil | |
874 | (progn | |
875 | (down-list 1) ; into arglist | |
876 | (backward-up-list 1) | |
877 | (skip-chars-backward " \t") | |
878 | t) | |
879 | (error nil)) | |
880 | ;; Verify initial pos was after | |
881 | ;; real start of function. | |
882 | (save-excursion | |
883 | (goto-char beg) | |
884 | ;; For this purpose, include the line | |
885 | ;; that has the decl keywords. This | |
886 | ;; may also include some of the | |
887 | ;; comments before the function. | |
888 | (while (and (not (bobp)) | |
889 | (save-excursion | |
890 | (forward-line -1) | |
891 | (looking-at "[^\n\f]"))) | |
892 | (forward-line -1)) | |
893 | (>= location (point))) | |
894 | ;; Consistency check: going down and up | |
895 | ;; shouldn't take us back before BEG. | |
896 | (> (point) beg)) | |
897 | (let (end middle) | |
898 | ;; Don't include any final whitespace | |
899 | ;; in the name we use. | |
900 | (skip-chars-backward " \t\n") | |
901 | (setq end (point)) | |
902 | (backward-sexp 1) | |
903 | ;; Now find the right beginning of the name. | |
904 | ;; Include certain keywords if they | |
905 | ;; precede the name. | |
906 | (setq middle (point)) | |
907 | (forward-word -1) | |
94b073f3 MY |
908 | ;; Is this C++ method? |
909 | (when (and (< 2 middle) | |
910 | (string= (buffer-substring (- middle 2) | |
911 | middle) | |
912 | "::")) | |
913 | ;; Include "classname::". | |
914 | (setq middle (point))) | |
5960adc7 DL |
915 | ;; Ignore these subparts of a class decl |
916 | ;; and move back to the class name itself. | |
917 | (while (looking-at "public \\|private ") | |
918 | (skip-chars-backward " \t:") | |
919 | (setq end (point)) | |
920 | (backward-sexp 1) | |
921 | (setq middle (point)) | |
922 | (forward-word -1)) | |
923 | (and (bolp) | |
924 | (looking-at | |
925 | "enum \\|struct \\|union \\|class ") | |
926 | (setq middle (point))) | |
927 | (goto-char end) | |
928 | (when (eq (preceding-char) ?=) | |
929 | (forward-char -1) | |
930 | (skip-chars-backward " \t") | |
931 | (setq end (point))) | |
932 | (buffer-substring-no-properties | |
933 | middle end)))))))) | |
fb644f48 | 934 | ((memq major-mode add-log-tex-like-modes) |
2cc0b765 | 935 | (if (re-search-backward |
5960adc7 DL |
936 | "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" |
937 | nil t) | |
2cc0b765 RS |
938 | (progn |
939 | (goto-char (match-beginning 0)) | |
5960adc7 DL |
940 | (buffer-substring-no-properties |
941 | (1+ (point)) ; without initial backslash | |
942 | (line-end-position))))) | |
2cc0b765 | 943 | ((eq major-mode 'texinfo-mode) |
3071ee28 | 944 | (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t) |
5960adc7 | 945 | (match-string-no-properties 1))) |
c06e9d8b | 946 | ((memq major-mode '(perl-mode cperl-mode)) |
5a9ac14b | 947 | (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t) |
5960adc7 DL |
948 | (match-string-no-properties 1))) |
949 | ;; Emacs's autoconf-mode installs its own | |
950 | ;; `add-log-current-defun-function'. This applies to | |
951 | ;; a different mode apparently for editing .m4 | |
952 | ;; autoconf source. | |
f654865f | 953 | ((eq major-mode 'autoconf-mode) |
5960adc7 DL |
954 | (if (re-search-backward |
955 | "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t) | |
956 | (match-string-no-properties 3))) | |
2cc0b765 RS |
957 | (t |
958 | ;; If all else fails, try heuristics | |
c1356086 GM |
959 | (let (case-fold-search |
960 | result) | |
2cc0b765 | 961 | (end-of-line) |
c1356086 GM |
962 | (when (re-search-backward |
963 | add-log-current-defun-header-regexp | |
964 | (- (point) 10000) | |
965 | t) | |
5960adc7 DL |
966 | (setq result (or (match-string-no-properties 1) |
967 | (match-string-no-properties 0))) | |
c1356086 GM |
968 | ;; Strip whitespace away |
969 | (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)" | |
970 | result) | |
5960adc7 | 971 | (setq result (match-string-no-properties 1 result))) |
c1356086 | 972 | result)))))) |
2cc0b765 | 973 | (error nil))) |
ef15f270 | 974 | |
83afd62c | 975 | (defvar change-log-get-method-definition-md) |
15319a8f | 976 | |
83afd62c | 977 | ;; Subroutine used within change-log-get-method-definition. |
59c1a7de RS |
978 | ;; Add the last match in the buffer to the end of `md', |
979 | ;; followed by the string END; move to the end of that match. | |
83afd62c KH |
980 | (defun change-log-get-method-definition-1 (end) |
981 | (setq change-log-get-method-definition-md | |
982 | (concat change-log-get-method-definition-md | |
5960adc7 | 983 | (match-string 1) |
15319a8f | 984 | end)) |
59c1a7de RS |
985 | (goto-char (match-end 0))) |
986 | ||
83afd62c | 987 | (defun change-log-get-method-definition () |
eba72fc1 | 988 | "For Objective C, return the method name if we are in a method." |
83afd62c | 989 | (let ((change-log-get-method-definition-md "[")) |
59c1a7de | 990 | (save-excursion |
f27f16ed | 991 | (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t) |
83afd62c | 992 | (change-log-get-method-definition-1 " "))) |
59c1a7de RS |
993 | (save-excursion |
994 | (cond | |
f27f16ed | 995 | ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t) |
83afd62c | 996 | (change-log-get-method-definition-1 "") |
59c1a7de RS |
997 | (while (not (looking-at "[{;]")) |
998 | (looking-at | |
f27f16ed | 999 | "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*") |
83afd62c KH |
1000 | (change-log-get-method-definition-1 "")) |
1001 | (concat change-log-get-method-definition-md "]")))))) | |
075a6629 DL |
1002 | \f |
1003 | (defun change-log-sortable-date-at () | |
1004 | "Return date of log entry in a consistent form for sorting. | |
1005 | Point is assumed to be at the start of the entry." | |
1006 | (require 'timezone) | |
1007 | (if (looking-at "^\\sw.........[0-9:+ ]*") | |
1008 | (let ((date (match-string-no-properties 0))) | |
1009 | (if date | |
1010 | (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date) | |
1011 | (concat (match-string 1 date) (match-string 2 date) | |
1012 | (match-string 3 date)) | |
1013 | (condition-case nil | |
1014 | (timezone-make-date-sortable date) | |
1015 | (error nil))))) | |
1016 | (error "Bad date"))) | |
59c1a7de | 1017 | |
7cd017ba SM |
1018 | (defun change-log-resolve-conflict () |
1019 | "Function to be used in `smerge-resolve-function'." | |
1020 | (let ((buf (current-buffer))) | |
1021 | (with-temp-buffer | |
1022 | (insert-buffer-substring buf (match-beginning 1) (match-end 1)) | |
1023 | (save-match-data (change-log-mode)) | |
1024 | (let ((other-buf (current-buffer))) | |
1025 | (with-current-buffer buf | |
1026 | (save-excursion | |
1027 | (save-restriction | |
1028 | (narrow-to-region (match-beginning 0) (match-end 0)) | |
1029 | (replace-match (match-string 3) t t) | |
1030 | (change-log-merge other-buf)))))))) | |
1031 | ||
075a6629 DL |
1032 | ;;;###autoload |
1033 | (defun change-log-merge (other-log) | |
eba72fc1 | 1034 | "Merge the contents of change log file OTHER-LOG with this buffer. |
075a6629 | 1035 | Both must be found in Change Log mode (since the merging depends on |
7cd017ba SM |
1036 | the appropriate motion commands). OTHER-LOG can be either a file name |
1037 | or a buffer. | |
075a6629 | 1038 | |
918f4ac3 DL |
1039 | Entries are inserted in chronological order. Both the current and |
1040 | old-style time formats for entries are supported." | |
075a6629 DL |
1041 | (interactive "*fLog file name to merge: ") |
1042 | (if (not (eq major-mode 'change-log-mode)) | |
1043 | (error "Not in Change Log mode")) | |
7cd017ba SM |
1044 | (let ((other-buf (if (bufferp other-log) other-log |
1045 | (find-file-noselect other-log))) | |
075a6629 DL |
1046 | (buf (current-buffer)) |
1047 | date1 start end) | |
1048 | (save-excursion | |
1049 | (goto-char (point-min)) | |
1050 | (set-buffer other-buf) | |
1051 | (goto-char (point-min)) | |
1052 | (if (not (eq major-mode 'change-log-mode)) | |
1053 | (error "%s not found in Change Log mode" other-log)) | |
1054 | ;; Loop through all the entries in OTHER-LOG. | |
1055 | (while (not (eobp)) | |
1056 | (setq date1 (change-log-sortable-date-at)) | |
1057 | (setq start (point) | |
1058 | end (progn (forward-page) (point))) | |
1059 | ;; Look for an entry in original buffer that isn't later. | |
1060 | (with-current-buffer buf | |
1061 | (while (and (not (eobp)) | |
1062 | (string< date1 (change-log-sortable-date-at))) | |
1063 | (forward-page)) | |
1064 | (if (not (eobp)) | |
1065 | (insert-buffer-substring other-buf start end) | |
1066 | ;; At the end of the original buffer, insert a newline to | |
1067 | ;; separate entries and then the rest of the file being | |
7cd017ba SM |
1068 | ;; merged. |
1069 | (unless (or (bobp) | |
1070 | (and (= ?\n (char-before)) | |
1071 | (or (<= (1- (point)) (point-min)) | |
1072 | (= ?\n (char-before (1- (point))))))) | |
97f4e87c | 1073 | (insert (if use-hard-newlines hard-newline "\n"))) |
7cd017ba SM |
1074 | ;; Move to the end of it to terminate outer loop. |
1075 | (with-current-buffer other-buf | |
1076 | (goto-char (point-max))) | |
1077 | (insert-buffer-substring other-buf start))))))) | |
ef15f270 | 1078 | |
918f4ac3 DL |
1079 | ;;;###autoload |
1080 | (defun change-log-redate () | |
1081 | "Fix any old-style date entries in the current log file to default format." | |
1082 | (interactive) | |
1083 | (require 'timezone) | |
1084 | (save-excursion | |
1085 | (goto-char (point-min)) | |
1086 | (while (re-search-forward "^\\sw.........[0-9:+ ]*" nil t) | |
1087 | (unless (= 12 (- (match-end 0) (match-beginning 0))) | |
1088 | (let* ((date (save-match-data | |
1089 | (timezone-fix-time (match-string 0) nil nil))) | |
1090 | (zone (if (consp (aref date 6)) | |
1091 | (nth 1 (aref date 6))))) | |
1092 | (replace-match (format-time-string | |
1093 | "%Y-%m-%d " | |
1094 | (encode-time (aref date 5) | |
1095 | (aref date 4) | |
1096 | (aref date 3) | |
1097 | (aref date 2) | |
1098 | (aref date 1) | |
1099 | (aref date 0) | |
1100 | zone)))))))) | |
1101 | ||
1da56800 RS |
1102 | (provide 'add-log) |
1103 | ||
ab5796a9 | 1104 | ;;; arch-tag: 81eee6fc-088f-4372-a37f-80ad9620e762 |
fd7fa35a | 1105 | ;;; add-log.el ends here |