3854
|
1 ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs
|
|
2
|
64762
|
3 ;; Copyright (C) 1989, 1993, 1994, 1995, 1997, 2000, 2001, 2002, 2003,
|
68651
|
4 ;; 2004, 2005, 2006 Free Software Foundation, Inc.
|
14169
|
5
|
38436
|
6 ;; This file is part of GNU Emacs.
|
|
7
|
68651
|
8 ;; Maintainer's Time-stamp: <2006-02-06 15:11:58 ttn>
|
39229
|
9 ;; Maintainer: Stephen Gildea <gildea@stop.mail-abuse.org>
|
3854
|
10 ;; Keywords: tools
|
|
11
|
45340
|
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
3854
|
13 ;; it under the terms of the GNU General Public License as published by
|
|
14 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
15 ;; any later version.
|
|
16
|
45341
|
17 ;; GNU Emacs is distributed in the hope that it will be useful,
|
3854
|
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20 ;; GNU General Public License for more details.
|
|
21
|
|
22 ;; You should have received a copy of the GNU General Public License
|
14169
|
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
64091
|
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
25 ;; Boston, MA 02110-1301, USA.
|
3854
|
26
|
|
27 ;;; Commentary:
|
|
28
|
16287
|
29 ;; A template in a file can be updated with a new time stamp when
|
|
30 ;; you save the file. For example:
|
49559
|
31 ;; static char *ts = "sdmain.c Time-stamp: <2001-08-13 10:20:51 gildea>";
|
16287
|
32 ;; See the top of `time-stamp.el' for another example.
|
3854
|
33
|
16287
|
34 ;; To use time-stamping, add this line to your .emacs file:
|
56074
7aab74a19427
(time-stamp): Recommend adding it to `before-save-hook', rather than
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
35 ;; (add-hook 'before-save-hook 'time-stamp)
|
16287
|
36 ;; Now any time-stamp templates in your files will be updated automatically.
|
3854
|
37
|
16287
|
38 ;; See the documentation for the functions `time-stamp'
|
|
39 ;; and `time-stamp-toggle-active' for details.
|
11404
|
40
|
3854
|
41 ;;; Code:
|
|
42
|
17438
|
43 (defgroup time-stamp nil
|
|
44 "Maintain last change time stamps in files edited by Emacs."
|
|
45 :group 'data
|
|
46 :group 'extensions)
|
|
47
|
18170
|
48 (defcustom time-stamp-format "%:y-%02m-%02d %02H:%02M:%02S %u"
|
16287
|
49 "*Format of the string inserted by \\[time-stamp].
|
16674
|
50 The value may be a string or a list. Lists are supported only for
|
|
51 backward compatibility; see variable `time-stamp-old-format-warn'.
|
13356
|
52
|
35627
|
53 A string is used verbatim except for character sequences beginning
|
|
54 with %, as follows. The values of non-numeric formatted items depend
|
37083
15fc82d712ac
(time-stamp-format): Doc fix. From Paul Eggert <eggert@twinsun.com>.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
55 on the locale setting recorded in `system-time-locale' and
|
15fc82d712ac
(time-stamp-format): Doc fix. From Paul Eggert <eggert@twinsun.com>.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
56 `locale-coding-system'. The examples here are for the default
|
45138
6855d0e47c10
(time-stamp-format): Escape open parenthesis in column 0.
Markus Rost <rost@math.uni-bielefeld.de>
diff
changeset
|
57 \(`C') locale.
|
16705
|
58
|
18170
|
59 %:a weekday name: `Monday'. %#A gives uppercase: `MONDAY'
|
|
60 %3a abbreviated weekday: `Mon'. %3A gives uppercase: `MON'
|
|
61 %:b month name: `January'. %#B gives uppercase: `JANUARY'
|
|
62 %3b abbreviated month: `Jan'. %3B gives uppercase: `JAN'
|
|
63 %02d day of month
|
|
64 %02H 24-hour clock hour
|
|
65 %02I 12-hour clock hour
|
|
66 %02m month number
|
|
67 %02M minute
|
|
68 %#p `am' or `pm'. %P gives uppercase: `AM' or `PM'
|
|
69 %02S seconds
|
|
70 %w day number of week, Sunday is 0
|
49559
|
71 %02y 2-digit year: `03' %:y 4-digit year: `2003'
|
18170
|
72 %z time zone name: `est'. %Z gives uppercase: `EST'
|
|
73
|
|
74 Non-date items:
|
|
75 %% a literal percent character: `%'
|
|
76 %f file name without directory %F gives absolute pathname
|
|
77 %s system name
|
24266
|
78 %u user's login name %U user's full name
|
18170
|
79 %h mail host name
|
|
80
|
|
81 Decimal digits between the % and the type character specify the
|
|
82 field width. Strings are truncated on the right; years on the left.
|
25405
|
83 A leading zero in the field width zero-fills a number.
|
17600
|
84
|
|
85 For example, to get the format used by the `date' command,
|
18170
|
86 use \"%3a %3b %2d %02H:%02M:%02S %Z %:y\".
|
|
87
|
25405
|
88 In the future these formats will be aligned more with `format-time-string'.
|
18170
|
89 Because of this transition, the default padding for numeric formats will
|
|
90 change in a future version. Therefore either a padding width should be
|
|
91 specified, or the : modifier should be used to explicitly request the
|
|
92 historical default."
|
17438
|
93 :type 'string
|
49559
|
94 :group 'time-stamp
|
|
95 :version "20.1")
|
16287
|
96
|
19504
|
97 (defcustom time-stamp-active t
|
|
98 "*Non-nil to enable time-stamping of buffers by \\[time-stamp].
|
|
99 Can be toggled by \\[time-stamp-toggle-active].
|
|
100 See also the variable `time-stamp-warn-inactive'."
|
|
101 :type 'boolean
|
|
102 :group 'time-stamp)
|
|
103
|
|
104 (defcustom time-stamp-warn-inactive t
|
32454
|
105 "Have \\[time-stamp] warn if a buffer did not get time-stamped.
|
49559
|
106 If non-nil, a warning is displayed if `time-stamp-active' has
|
|
107 deactivated time stamping and the buffer contains a template that
|
|
108 otherwise would have been updated."
|
19504
|
109 :type 'boolean
|
49559
|
110 :group 'time-stamp
|
|
111 :version "19.29")
|
19504
|
112
|
|
113 (defcustom time-stamp-old-format-warn 'ask
|
32454
|
114 "Action if `time-stamp-format' is an old-style list.
|
19504
|
115 If `error', the format is not used. If `ask', the user is queried about
|
|
116 using the time-stamp-format. If `warn', a warning is displayed.
|
|
117 If nil, no notification is given."
|
34395
|
118 :type '(choice (const :tag "Don't use the format" error)
|
|
119 (const ask)
|
|
120 (const warn)
|
|
121 (const :tag "No notification" nil))
|
19504
|
122 :group 'time-stamp)
|
|
123
|
|
124 (defcustom time-stamp-time-zone nil
|
|
125 "If non-nil, a string naming the timezone to be used by \\[time-stamp].
|
|
126 Format is the same as that used by the environment variable TZ on your system."
|
19791
|
127 :type '(choice (const nil) string)
|
49559
|
128 :group 'time-stamp
|
|
129 :version "20.1")
|
17600
|
130
|
|
131
|
21073
|
132 ;;; Do not change time-stamp-line-limit, time-stamp-start,
|
25405
|
133 ;;; time-stamp-end, time-stamp-pattern, time-stamp-inserts-lines,
|
|
134 ;;; or time-stamp-count in your .emacs or you will be incompatible
|
|
135 ;;; with other people's files! If you must change them, do so only
|
|
136 ;;; in the local variables section of the file itself.
|
3854
|
137
|
16287
|
138
|
5632
|
139 (defvar time-stamp-line-limit 8 ;Do not change!
|
14042
|
140 "Lines of a file searched; positive counts from start, negative from end.
|
25405
|
141 The patterns `time-stamp-start' and `time-stamp-end' must be found in
|
|
142 the first (last) `time-stamp-line-limit' lines of the file for the
|
24266
|
143 file to be time-stamped by \\[time-stamp]. A value of 0 searches the
|
|
144 entire buffer (use with care).
|
5632
|
145
|
65449
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
146 This value can also be set with the variable `time-stamp-pattern'.
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
147
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
148 Do not change `time-stamp-line-limit', `time-stamp-start',
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
149 `time-stamp-end', or `time-stamp-pattern' for yourself or you will be
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
150 incompatible with other people's files! If you must change them for some
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
151 application, do so in the local variables section of the time-stamped file
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
152 itself.")
|
5632
|
153
|
3854
|
154
|
5971
|
155 (defvar time-stamp-start "Time-stamp:[ \t]+\\\\?[\"<]+" ;Do not change!
|
3854
|
156 "Regexp after which the time stamp is written by \\[time-stamp].
|
5632
|
157 See also the variables `time-stamp-end' and `time-stamp-line-limit'.
|
3854
|
158
|
65449
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
159 This value can also be set with the variable `time-stamp-pattern'.
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
160
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
161 Do not change `time-stamp-line-limit', `time-stamp-start',
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
162 `time-stamp-end', or `time-stamp-pattern' for yourself or you will be
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
163 incompatible with other people's files! If you must change them for some
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
164 application, do so in the local variables section of the time-stamped file
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
165 itself.")
|
3854
|
166
|
|
167
|
5632
|
168 (defvar time-stamp-end "\\\\?[\">]" ;Do not change!
|
3854
|
169 "Regexp marking the text after the time stamp.
|
5632
|
170 \\[time-stamp] deletes the text between the first match of `time-stamp-start'
|
25405
|
171 and the following match of `time-stamp-end', then writes the
|
|
172 time stamp specified by `time-stamp-format' between them.
|
5632
|
173
|
65449
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
174 This value can also be set with the variable `time-stamp-pattern'.
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
175
|
25405
|
176 The end text normally starts on the same line as the start text ends,
|
|
177 but if there are any newlines in `time-stamp-format', the same number
|
|
178 of newlines must separate the start and end. \\[time-stamp] tries
|
|
179 to not change the number of lines in the buffer. `time-stamp-inserts-lines'
|
|
180 controls this behavior.
|
|
181
|
65449
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
182 Do not change `time-stamp-start', `time-stamp-end', `time-stamp-pattern',
|
25405
|
183 or `time-stamp-inserts-lines' for yourself or you will be incompatible
|
5632
|
184 with other people's files! If you must change them for some application,
|
|
185 do so in the local variables section of the time-stamped file itself.")
|
|
186
|
3854
|
187
|
25405
|
188 (defvar time-stamp-inserts-lines nil ;Do not change!
|
49559
|
189 "Whether \\[time-stamp] can change the number of lines in a file.
|
25405
|
190 If nil, \\[time-stamp] skips as many lines as there are newlines in
|
|
191 `time-stamp-format' before looking for the `time-stamp-end' pattern,
|
|
192 thus it tries not to change the number of lines in the buffer.
|
|
193 If non-nil, \\[time-stamp] starts looking for the end pattern
|
|
194 immediately after the start pattern. This behavior can cause
|
|
195 unexpected changes in the buffer if used carelessly, but it is useful
|
|
196 for generating repeated time stamps.
|
|
197
|
|
198 Do not change `time-stamp-end' or `time-stamp-inserts-lines' for
|
|
199 yourself or you will be incompatible with other people's files!
|
|
200 If you must change them for some application, do so in the local
|
|
201 variables section of the time-stamped file itself.")
|
|
202
|
|
203
|
|
204 (defvar time-stamp-count 1 ;Do not change!
|
32454
|
205 "How many templates \\[time-stamp] will look for in a buffer.
|
49559
|
206 The same time stamp will be written in each case.
|
25405
|
207
|
|
208 Do not change `time-stamp-count' for yourself or you will be
|
|
209 incompatible with other people's files! If you must change it for
|
|
210 some application, do so in the local variables section of the
|
|
211 time-stamped file itself.")
|
|
212
|
|
213
|
49559
|
214 (defvar time-stamp-pattern nil ;Do not change!
|
25405
|
215 "Convenience variable setting all `time-stamp' location and format values.
|
21073
|
216 This string has four parts, each of which is optional.
|
25405
|
217 These four parts set `time-stamp-line-limit', `time-stamp-start',
|
|
218 `time-stamp-format', and `time-stamp-end'. See the documentation
|
21073
|
219 for each of these variables for details.
|
|
220
|
|
221 The first part is a number followed by a slash; the number sets the number
|
|
222 of lines at the beginning (negative counts from end) of the file searched
|
49559
|
223 for the time stamp. The number and the slash may be omitted to use the
|
21073
|
224 normal value.
|
|
225
|
|
226 The second part is a regexp identifying the pattern preceding the time stamp.
|
|
227 This part may be omitted to use the normal pattern.
|
|
228
|
49559
|
229 The third part specifies the format of the time stamp inserted. See
|
25405
|
230 the documentation for `time-stamp-format' for details. Specify this
|
21073
|
231 part as \"%%\" to use the normal format.
|
|
232
|
|
233 The fourth part is a regexp identifying the pattern following the time stamp.
|
|
234 This part may be omitted to use the normal pattern.
|
|
235
|
49559
|
236 Examples:
|
|
237 \"-10/\"
|
|
238 \"-9/^Last modified: %%$\"
|
|
239 \"@set Time-stamp: %:b %:d, %:y$\"
|
|
240 \"newcommand{\\\\\\\\timestamp}{%%}\"
|
21073
|
241
|
65449
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
242 Do not change `time-stamp-pattern' `time-stamp-line-limit',
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
243 `time-stamp-start', or `time-stamp-end' for yourself or you will be
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
244 incompatible with other people's files! If you must change them for
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
245 some application, do so only in the local variables section of the
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
246 time-stamped file itself.")
|
21073
|
247
|
|
248
|
16287
|
249
|
4327
|
250 ;;;###autoload
|
3854
|
251 (defun time-stamp ()
|
25405
|
252 "Update the time stamp string(s) in the buffer.
|
16287
|
253 A template in a file can be automatically updated with a new time stamp
|
|
254 every time you save the file. Add this line to your .emacs file:
|
56074
7aab74a19427
(time-stamp): Recommend adding it to `before-save-hook', rather than
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
255 (add-hook 'before-save-hook 'time-stamp)
|
56099
|
256 or customize `before-save-hook' through Custom.
|
16287
|
257 Normally the template must appear in the first 8 lines of a file and
|
|
258 look like one of the following:
|
|
259 Time-stamp: <>
|
|
260 Time-stamp: \" \"
|
|
261 The time stamp is written between the brackets or quotes:
|
49559
|
262 Time-stamp: <2001-02-18 10:20:51 gildea>
|
17600
|
263 The time stamp is updated only if the variable `time-stamp-active' is non-nil.
|
65449
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
264 The format of the time stamp is set by the variable `time-stamp-pattern' or
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
265 `time-stamp-format'. The variables `time-stamp-pattern',
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
266 `time-stamp-line-limit', `time-stamp-start', `time-stamp-end',
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
267 `time-stamp-count', and `time-stamp-inserts-lines' control finding
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
268 the template."
|
3854
|
269 (interactive)
|
25405
|
270 (let ((line-limit time-stamp-line-limit)
|
21073
|
271 (ts-start time-stamp-start)
|
|
272 (ts-format time-stamp-format)
|
25405
|
273 (ts-end time-stamp-end)
|
|
274 (ts-count time-stamp-count)
|
|
275 (format-lines 0)
|
|
276 (end-lines 1)
|
|
277 (start nil)
|
|
278 search-limit)
|
21073
|
279 (if (stringp time-stamp-pattern)
|
|
280 (progn
|
49559
|
281 (string-match "\\`\\(\\(-?[0-9]+\\)/\\)?\\([^%]+\\)?\\(\\(%[-.,:@+_ #^()0-9]*[A-Za-z%][^%]*\\)*%[-.,:@+_ #^()0-9]*[A-Za-z%]\\)?\\([^%]+\\)?\\'" time-stamp-pattern)
|
21073
|
282 (and (match-beginning 2)
|
|
283 (setq line-limit
|
62402
|
284 (string-to-number (match-string 2 time-stamp-pattern))))
|
21073
|
285 (and (match-beginning 3)
|
|
286 (setq ts-start (match-string 3 time-stamp-pattern)))
|
|
287 (and (match-beginning 4)
|
|
288 (not (string-equal (match-string 4 time-stamp-pattern) "%%"))
|
|
289 (setq ts-format (match-string 4 time-stamp-pattern)))
|
22508
|
290 (and (match-beginning 6)
|
|
291 (setq ts-end (match-string 6 time-stamp-pattern)))))
|
17600
|
292 (cond ((not (integerp line-limit))
|
|
293 (setq line-limit 8)
|
21073
|
294 (message "time-stamp-line-limit is not an integer")
|
17600
|
295 (sit-for 1)))
|
25405
|
296 (cond ((not (integerp ts-count))
|
|
297 (setq ts-count 1)
|
|
298 (message "time-stamp-count is not an integer")
|
|
299 (sit-for 1))
|
|
300 ((< ts-count 1)
|
|
301 ;; We need to call time-stamp-once at least once
|
|
302 ;; to output any warnings about time-stamp not being active.
|
|
303 (setq ts-count 1)))
|
|
304 ;; Figure out what lines the end should be on.
|
32454
|
305 (if (stringp ts-format)
|
|
306 (let ((nl-start 0))
|
|
307 (while (string-match "\n" ts-format nl-start)
|
|
308 (setq format-lines (1+ format-lines) nl-start (match-end 0)))))
|
25405
|
309 (let ((nl-start 0))
|
|
310 (while (string-match "\n" ts-end nl-start)
|
|
311 (setq end-lines (1+ end-lines) nl-start (match-end 0))))
|
|
312 ;; Find overall what lines to look at
|
17600
|
313 (save-excursion
|
|
314 (save-restriction
|
|
315 (widen)
|
|
316 (cond ((> line-limit 0)
|
|
317 (goto-char (setq start (point-min)))
|
|
318 (forward-line line-limit)
|
|
319 (setq search-limit (point)))
|
24266
|
320 ((< line-limit 0)
|
17600
|
321 (goto-char (setq search-limit (point-max)))
|
|
322 (forward-line line-limit)
|
24266
|
323 (setq start (point)))
|
|
324 (t ;0 => no limit (use with care!)
|
|
325 (setq start (point-min))
|
25405
|
326 (setq search-limit (point-max))))))
|
|
327 (while (and start
|
|
328 (< start search-limit)
|
|
329 (> ts-count 0))
|
|
330 (setq start (time-stamp-once start search-limit ts-start ts-end
|
|
331 ts-format format-lines end-lines))
|
|
332 (setq ts-count (1- ts-count))))
|
|
333 nil)
|
|
334
|
|
335 (defun time-stamp-once (start search-limit ts-start ts-end
|
|
336 ts-format format-lines end-lines)
|
49559
|
337 "Update one time stamp. Internal routine called by \\[time-stamp].
|
25405
|
338 Returns the end point, which is where `time-stamp' begins the next search."
|
|
339 (let ((case-fold-search nil)
|
|
340 (end nil)
|
|
341 end-search-start
|
|
342 (end-length nil))
|
|
343 (save-excursion
|
|
344 (save-restriction
|
|
345 (widen)
|
|
346 ;; Find the location of the time stamp.
|
|
347 (while (and (< (goto-char start) search-limit)
|
17600
|
348 (not end)
|
21073
|
349 (re-search-forward ts-start search-limit 'move))
|
17600
|
350 (setq start (point))
|
25405
|
351 (if (not time-stamp-inserts-lines)
|
|
352 (forward-line format-lines))
|
|
353 (setq end-search-start (max start (point)))
|
|
354 (if (= (forward-line end-lines) 0)
|
|
355 (progn
|
|
356 (and (bolp) (backward-char))
|
|
357 (let ((line-end (min (point) search-limit)))
|
|
358 (if (>= line-end end-search-start)
|
|
359 (progn
|
|
360 (goto-char end-search-start)
|
|
361 (if (re-search-forward ts-end line-end t)
|
|
362 (progn
|
|
363 (setq end (match-beginning 0))
|
|
364 (setq end-length (- (match-end 0) end))))))))))))
|
21073
|
365 (if end
|
|
366 (progn
|
|
367 ;; do all warnings outside save-excursion
|
|
368 (cond
|
|
369 ((not time-stamp-active)
|
|
370 (if time-stamp-warn-inactive
|
|
371 ;; don't signal an error in a write-file-hook
|
|
372 (progn
|
|
373 (message "Warning: time-stamp-active is off; did not time-stamp buffer.")
|
|
374 (sit-for 1))))
|
|
375 ((not (and (stringp ts-start)
|
|
376 (stringp ts-end)))
|
|
377 (message "time-stamp-start or time-stamp-end is not a string")
|
|
378 (sit-for 1))
|
|
379 (t
|
|
380 (let ((new-time-stamp (time-stamp-string ts-format)))
|
24266
|
381 (if (and (stringp new-time-stamp)
|
|
382 (not (string-equal (buffer-substring start end)
|
|
383 new-time-stamp)))
|
21073
|
384 (save-excursion
|
|
385 (save-restriction
|
|
386 (widen)
|
|
387 (delete-region start end)
|
|
388 (goto-char start)
|
|
389 (insert-and-inherit new-time-stamp)
|
|
390 (setq end (point))
|
|
391 ;; remove any tabs used to format time stamp
|
25405
|
392 (if (search-backward "\t" start t)
|
|
393 (progn
|
|
394 (untabify start end)
|
|
395 (setq end (point))))))))))))
|
|
396 ;; return the location after this time stamp, if there was one
|
|
397 (and end end-length
|
|
398 (+ end end-length))))
|
|
399
|
3854
|
400
|
11404
|
401 ;;;###autoload
|
|
402 (defun time-stamp-toggle-active (&optional arg)
|
16287
|
403 "Toggle `time-stamp-active', setting whether \\[time-stamp] updates a buffer.
|
49559
|
404 With ARG, turn time stamping on if and only if arg is positive."
|
11404
|
405 (interactive "P")
|
|
406 (setq time-stamp-active
|
|
407 (if (null arg)
|
|
408 (not time-stamp-active)
|
|
409 (> (prefix-numeric-value arg) 0)))
|
25405
|
410 (message "time-stamp is now %s." (if time-stamp-active "active" "off")))
|
|
411
|
|
412
|
|
413 (defun time-stamp-string (&optional ts-format)
|
|
414 "Generate the new string to be inserted by \\[time-stamp].
|
35695
|
415 Optionally use format TS-FORMAT instead of `time-stamp-format' to
|
|
416 format the string."
|
25405
|
417 (or ts-format
|
|
418 (setq ts-format time-stamp-format))
|
|
419 (if (stringp ts-format)
|
|
420 (if (stringp time-stamp-time-zone)
|
|
421 (let ((ts-real-time-zone (getenv "TZ")))
|
|
422 (unwind-protect
|
|
423 (progn
|
49559
|
424 (set-time-zone-rule time-stamp-time-zone)
|
25405
|
425 (format-time-string
|
|
426 (time-stamp-string-preprocess ts-format)))
|
49559
|
427 (set-time-zone-rule ts-real-time-zone)))
|
25405
|
428 (format-time-string
|
|
429 (time-stamp-string-preprocess ts-format)))
|
|
430 ;; handle version 1 compatibility
|
|
431 (cond ((or (eq time-stamp-old-format-warn 'error)
|
|
432 (and (eq time-stamp-old-format-warn 'ask)
|
|
433 (not (y-or-n-p "Use non-string time-stamp-format? "))))
|
|
434 (message "Warning: no time-stamp: time-stamp-format not a string")
|
|
435 (sit-for 1)
|
|
436 nil)
|
|
437 (t
|
|
438 (cond ((eq time-stamp-old-format-warn 'warn)
|
|
439 (message "Obsolescent time-stamp-format type; should be string")
|
|
440 (sit-for 1)))
|
|
441 (time-stamp-fconcat ts-format " ")))))
|
16287
|
442
|
16705
|
443 (defconst time-stamp-no-file "(no file)"
|
|
444 "String to use when the buffer is not associated with a file.")
|
|
445
|
18170
|
446 ;;; time-stamp is transitioning to using the new, expanded capabilities
|
|
447 ;;; of format-time-string. During the process, this function implements
|
|
448 ;;; intermediate, compatible formats and complains about old, soon to
|
|
449 ;;; be unsupported, formats. This function will get a lot (a LOT) shorter
|
|
450 ;;; when the transition is complete and we can just pass most things
|
|
451 ;;; straight through to format-time-string.
|
|
452 ;;; At all times, all the formats recommended in the doc string
|
|
453 ;;; of time-stamp-format will work not only in the current version of
|
|
454 ;;; Emacs, but in all versions that have been released within the past
|
|
455 ;;; two years.
|
|
456 ;;; The : modifier is a temporary conversion feature used to resolve
|
|
457 ;;; ambiguous formats--formats that are changing (over time) incompatibly.
|
|
458 (defun time-stamp-string-preprocess (format &optional time)
|
35627
|
459 "Use a FORMAT to format date, time, file, and user information.
|
|
460 Optional second argument TIME is only for testing.
|
|
461 Implements non-time extensions to `format-time-string'
|
49559
|
462 and all `time-stamp-format' compatibility."
|
18170
|
463 (let ((fmt-len (length format))
|
|
464 (ind 0)
|
|
465 cur-char
|
|
466 (prev-char nil)
|
|
467 (result "")
|
|
468 field-width
|
|
469 field-result
|
|
470 alt-form change-case require-padding
|
|
471 (paren-level 0))
|
|
472 (while (< ind fmt-len)
|
|
473 (setq cur-char (aref format ind))
|
|
474 (setq
|
|
475 result
|
|
476 (concat result
|
|
477 (cond
|
|
478 ((eq cur-char ?%)
|
|
479 ;; eat any additional args to allow for future expansion
|
22508
|
480 (setq alt-form nil change-case nil require-padding nil field-width "")
|
18170
|
481 (while (progn
|
|
482 (setq ind (1+ ind))
|
|
483 (setq cur-char (if (< ind fmt-len)
|
|
484 (aref format ind)
|
|
485 ?\0))
|
|
486 (or (eq ?. cur-char)
|
|
487 (eq ?, cur-char) (eq ?: cur-char) (eq ?@ cur-char)
|
35627
|
488 (eq ?- cur-char) (eq ?+ cur-char) (eq ?_ cur-char)
|
18170
|
489 (eq ?\ cur-char) (eq ?# cur-char) (eq ?^ cur-char)
|
|
490 (and (eq ?\( cur-char)
|
|
491 (not (eq prev-char ?\\))
|
|
492 (setq paren-level (1+ paren-level)))
|
|
493 (if (and (eq ?\) cur-char)
|
|
494 (not (eq prev-char ?\\))
|
|
495 (> paren-level 0))
|
|
496 (setq paren-level (1- paren-level))
|
|
497 (and (> paren-level 0)
|
22508
|
498 (< ind fmt-len)))
|
|
499 (if (and (<= ?0 cur-char) (>= ?9 cur-char))
|
|
500 ;; get format width
|
|
501 (let ((field-index ind))
|
|
502 (while (progn
|
|
503 (setq ind (1+ ind))
|
|
504 (setq cur-char (if (< ind fmt-len)
|
|
505 (aref format ind)
|
|
506 ?\0))
|
|
507 (and (<= ?0 cur-char) (>= ?9 cur-char))))
|
|
508 (setq field-width (substring format field-index ind))
|
|
509 (setq ind (1- ind))
|
|
510 t))))
|
18170
|
511 (setq prev-char cur-char)
|
|
512 ;; some characters we actually use
|
|
513 (cond ((eq cur-char ?:)
|
|
514 (setq alt-form t))
|
|
515 ((eq cur-char ?#)
|
|
516 (setq change-case t))))
|
|
517 (setq field-result
|
|
518 (cond
|
|
519 ((eq cur-char ?%)
|
25405
|
520 "%%")
|
18170
|
521 ((eq cur-char ?a) ;day of week
|
|
522 (if change-case
|
39371
|
523 (format-time-string "%#a" time)
|
18170
|
524 (or alt-form (not (string-equal field-width ""))
|
|
525 (time-stamp-conv-warn "%a" "%:a"))
|
|
526 (if (and alt-form (not (string-equal field-width "")))
|
|
527 "" ;discourage "%:3a"
|
|
528 (format-time-string "%A" time))))
|
|
529 ((eq cur-char ?A)
|
|
530 (if alt-form
|
|
531 (format-time-string "%A" time)
|
|
532 (or change-case (not (string-equal field-width ""))
|
|
533 (time-stamp-conv-warn "%A" "%#A"))
|
|
534 (format-time-string "%#A" time)))
|
|
535 ((eq cur-char ?b) ;month name
|
|
536 (if change-case
|
39371
|
537 (format-time-string "%#b" time)
|
18170
|
538 (or alt-form (not (string-equal field-width ""))
|
|
539 (time-stamp-conv-warn "%b" "%:b"))
|
|
540 (if (and alt-form (not (string-equal field-width "")))
|
|
541 "" ;discourage "%:3b"
|
|
542 (format-time-string "%B" time))))
|
|
543 ((eq cur-char ?B)
|
|
544 (if alt-form
|
|
545 (format-time-string "%B" time)
|
|
546 (or change-case (not (string-equal field-width ""))
|
|
547 (time-stamp-conv-warn "%B" "%#B"))
|
|
548 (format-time-string "%#B" time)))
|
|
549 ((eq cur-char ?d) ;day of month, 1-31
|
18171
|
550 (time-stamp-do-number cur-char alt-form field-width time))
|
18170
|
551 ((eq cur-char ?H) ;hour, 0-23
|
18171
|
552 (time-stamp-do-number cur-char alt-form field-width time))
|
18170
|
553 ((eq cur-char ?I) ;hour, 1-12
|
18171
|
554 (time-stamp-do-number cur-char alt-form field-width time))
|
18170
|
555 ((eq cur-char ?m) ;month number, 1-12
|
18171
|
556 (time-stamp-do-number cur-char alt-form field-width time))
|
18170
|
557 ((eq cur-char ?M) ;minute, 0-59
|
18171
|
558 (time-stamp-do-number cur-char alt-form field-width time))
|
18170
|
559 ((eq cur-char ?p) ;am or pm
|
|
560 (or change-case
|
|
561 (time-stamp-conv-warn "%p" "%#p"))
|
|
562 (format-time-string "%#p" time))
|
|
563 ((eq cur-char ?P) ;AM or PM
|
|
564 (format-time-string "%p" time))
|
|
565 ((eq cur-char ?S) ;seconds, 00-60
|
18171
|
566 (time-stamp-do-number cur-char alt-form field-width time))
|
18170
|
567 ((eq cur-char ?w) ;weekday number, Sunday is 0
|
|
568 (format-time-string "%w" time))
|
|
569 ((eq cur-char ?y) ;year
|
|
570 (or alt-form (not (string-equal field-width ""))
|
|
571 (time-stamp-conv-warn "%y" "%:y"))
|
62402
|
572 (string-to-number (format-time-string "%Y" time)))
|
18170
|
573 ((eq cur-char ?Y) ;4-digit year, new style
|
62402
|
574 (string-to-number (format-time-string "%Y" time)))
|
18170
|
575 ((eq cur-char ?z) ;time zone lower case
|
|
576 (if change-case
|
|
577 "" ;discourage %z variations
|
|
578 (format-time-string "%#Z" time)))
|
|
579 ((eq cur-char ?Z)
|
|
580 (if change-case
|
|
581 (format-time-string "%#Z" time)
|
|
582 (format-time-string "%Z" time)))
|
|
583 ((eq cur-char ?f) ;buffer-file-name, base name only
|
|
584 (if buffer-file-name
|
|
585 (file-name-nondirectory buffer-file-name)
|
|
586 time-stamp-no-file))
|
|
587 ((eq cur-char ?F) ;buffer-file-name, full path
|
|
588 (or buffer-file-name
|
|
589 time-stamp-no-file))
|
|
590 ((eq cur-char ?s) ;system name
|
|
591 (system-name))
|
|
592 ((eq cur-char ?u) ;user name
|
|
593 (user-login-name))
|
22993
|
594 ((eq cur-char ?U) ;user full name
|
|
595 (user-full-name))
|
25405
|
596 ((eq cur-char ?l) ;logname (undocumented user name alt)
|
|
597 (user-login-name))
|
|
598 ((eq cur-char ?L) ;(undocumented alt user full name)
|
|
599 (user-full-name))
|
18170
|
600 ((eq cur-char ?h) ;mail host name
|
|
601 (time-stamp-mail-host-name))
|
65449
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
602 ((eq cur-char ?q) ;(undocumented unqual hostname)
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
603 (let ((qualname (system-name)))
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
604 (if (string-match "\\." qualname)
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
605 (substring qualname 0 (match-beginning 0))
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
606 qualname)))
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
607 ((eq cur-char ?Q) ;(undocumented fully-qualified host)
|
d3d35c792c79
(time-stamp, time-stamp-line-limit, time-stamp-start, time-stamp-end):
Stephen Gildea <gildea@stop.mail-abuse.org>
diff
changeset
|
608 (system-name))
|
18170
|
609 ))
|
32874
|
610 (let ((padded-result
|
|
611 (format (format "%%%s%c"
|
|
612 field-width
|
|
613 (if (numberp field-result) ?d ?s))
|
|
614 (or field-result ""))))
|
|
615 (let* ((initial-length (length padded-result))
|
|
616 (desired-length (if (string-equal field-width "")
|
|
617 initial-length
|
62402
|
618 (string-to-number field-width))))
|
32874
|
619 (if (> initial-length desired-length)
|
|
620 ;; truncate strings on right, years on left
|
|
621 (if (stringp field-result)
|
|
622 (substring padded-result 0 desired-length)
|
|
623 (if (eq cur-char ?y)
|
|
624 (substring padded-result (- desired-length))
|
|
625 padded-result)) ;non-year numbers don't truncate
|
|
626 padded-result))))
|
18170
|
627 (t
|
|
628 (char-to-string cur-char)))))
|
|
629 (setq ind (1+ ind)))
|
|
630 result))
|
|
631
|
18171
|
632 (defun time-stamp-do-number (format-char alt-form field-width time)
|
35627
|
633 "Handle compatible FORMAT-CHAR where only default width/padding will change.
|
|
634 ALT-FORM is whether `#' specified. FIELD-WIDTH is the string
|
|
635 width specification or \"\". TIME is the time to convert."
|
18170
|
636 (let ((format-string (concat "%" (char-to-string format-char))))
|
|
637 (and (not alt-form) (string-equal field-width "")
|
|
638 (time-stamp-conv-warn format-string
|
|
639 (format "%%:%c" format-char)))
|
|
640 (if (and alt-form (not (string-equal field-width "")))
|
|
641 "" ;discourage "%:2d" and the like
|
62402
|
642 (string-to-number (format-time-string format-string time)))))
|
18170
|
643
|
|
644 (defvar time-stamp-conversion-warn t
|
32454
|
645 "Warn about soon-to-be-unsupported forms in `time-stamp-format'.
|
|
646 If nil, these warnings are disabled, which would be a bad idea!
|
18170
|
647 You really need to update your files instead.
|
|
648
|
|
649 The new formats will work with old versions of Emacs.
|
25405
|
650 New formats are being recommended now to allow `time-stamp-format'
|
|
651 to change in the future to be compatible with `format-time-string'.
|
18170
|
652 The new forms being recommended now will continue to work then.")
|
|
653
|
|
654
|
|
655 (defun time-stamp-conv-warn (old-form new-form)
|
49559
|
656 "Display a warning about a soon-to-be-obsolete format.
|
|
657 Suggests replacing OLD-FORM with NEW-FORM."
|
18170
|
658 (cond
|
|
659 (time-stamp-conversion-warn
|
|
660 (save-excursion
|
|
661 (set-buffer (get-buffer-create "*Time-stamp-compatibility*"))
|
|
662 (goto-char (point-max))
|
|
663 (if (bobp)
|
|
664 (progn
|
|
665 (insert
|
|
666 "The formats recognized in time-stamp-format will change in a future release\n"
|
|
667 "to be compatible with the new, expanded format-time-string function.\n\n"
|
|
668 "The following obsolescent time-stamp-format construct(s) were found:\n\n")))
|
|
669 (insert "\"" old-form "\" -- use " new-form "\n"))
|
|
670 (display-buffer "*Time-stamp-compatibility*"))))
|
19504
|
671
|
18170
|
672
|
11404
|
673
|
|
674 (defun time-stamp-mail-host-name ()
|
|
675 "Return the name of the host where the user receives mail.
|
|
676 This is the value of `mail-host-address' if bound and a string,
|
25405
|
677 otherwise the value of the function `system-name'."
|
11404
|
678 (or (and (boundp 'mail-host-address)
|
|
679 (stringp mail-host-address)
|
|
680 mail-host-address)
|
|
681 (system-name)))
|
|
682
|
|
683 ;;; the rest of this file is for version 1 compatibility
|
3854
|
684
|
|
685 (defun time-stamp-fconcat (list sep)
|
5632
|
686 "Similar to (mapconcat 'funcall LIST SEP) but LIST allows literals.
|
3854
|
687 If an element of LIST is a symbol, it is funcalled to get the string to use;
|
|
688 the separator SEP is used between two strings obtained by funcalling a
|
|
689 symbol. Otherwise the element itself is inserted; no separator is used
|
|
690 around literals."
|
|
691 (let ((return-string "")
|
|
692 (insert-sep-p nil))
|
|
693 (while list
|
|
694 (cond ((symbolp (car list))
|
|
695 (if insert-sep-p
|
|
696 (setq return-string (concat return-string sep)))
|
|
697 (setq return-string (concat return-string (funcall (car list))))
|
|
698 (setq insert-sep-p t))
|
|
699 (t
|
|
700 (setq return-string (concat return-string (car list)))
|
|
701 (setq insert-sep-p nil)))
|
|
702 (setq list (cdr list)))
|
|
703 return-string))
|
|
704
|
16287
|
705 ;;; Some functions used in time-stamp-format
|
3854
|
706
|
49559
|
707 ;;; These functions have been obsolete since 1995
|
|
708 ;;; and will be removed in Emacs 22.
|
|
709 ;;; Meanwhile, discourage other packages from using them.
|
|
710 (let ((obsolete-functions '(time-stamp-month-dd-yyyy
|
|
711 time-stamp-dd/mm/yyyy
|
|
712 time-stamp-mon-dd-yyyy
|
|
713 time-stamp-dd-mon-yy
|
|
714 time-stamp-yy/mm/dd
|
|
715 time-stamp-yyyy/mm/dd
|
|
716 time-stamp-yyyy-mm-dd
|
|
717 time-stamp-yymmdd
|
|
718 time-stamp-hh:mm:ss
|
|
719 time-stamp-hhmm)))
|
|
720 (while obsolete-functions
|
|
721 (make-obsolete (car obsolete-functions)
|
|
722 "use time-stamp-string or format-time-string instead."
|
|
723 "20.1")
|
|
724 (setq obsolete-functions (cdr obsolete-functions))))
|
3854
|
725
|
|
726 ;;; pretty form, suitable for a title page
|
|
727
|
|
728 (defun time-stamp-month-dd-yyyy ()
|
5632
|
729 "Return the current date as a string in \"Month DD, YYYY\" form."
|
16706
|
730 (format-time-string "%B %e, %Y"))
|
3854
|
731
|
16517
|
732 (defun time-stamp-dd/mm/yyyy ()
|
|
733 "Return the current date as a string in \"DD/MM/YYYY\" form."
|
16706
|
734 (format-time-string "%d/%m/%Y"))
|
16517
|
735
|
3854
|
736 ;;; same as __DATE__ in ANSI C
|
|
737
|
|
738 (defun time-stamp-mon-dd-yyyy ()
|
5632
|
739 "Return the current date as a string in \"Mon DD YYYY\" form.
|
|
740 The first character of DD is space if the value is less than 10."
|
16706
|
741 (format-time-string "%b %d %Y"))
|
3854
|
742
|
|
743 ;;; RFC 822 date
|
|
744
|
|
745 (defun time-stamp-dd-mon-yy ()
|
5632
|
746 "Return the current date as a string in \"DD Mon YY\" form."
|
16706
|
747 (format-time-string "%d %b %y"))
|
3854
|
748
|
|
749 ;;; RCS 3 date
|
|
750
|
|
751 (defun time-stamp-yy/mm/dd ()
|
5632
|
752 "Return the current date as a string in \"YY/MM/DD\" form."
|
16706
|
753 (format-time-string "%y/%m/%d"))
|
3854
|
754
|
|
755 ;;; RCS 5 date
|
|
756
|
|
757 (defun time-stamp-yyyy/mm/dd ()
|
5632
|
758 "Return the current date as a string in \"YYYY/MM/DD\" form."
|
16706
|
759 (format-time-string "%Y/%m/%d"))
|
3854
|
760
|
5632
|
761 ;;; ISO 8601 date
|
|
762
|
|
763 (defun time-stamp-yyyy-mm-dd ()
|
|
764 "Return the current date as a string in \"YYYY-MM-DD\" form."
|
16706
|
765 (format-time-string "%Y-%m-%d"))
|
5632
|
766
|
3854
|
767 (defun time-stamp-yymmdd ()
|
5632
|
768 "Return the current date as a string in \"YYMMDD\" form."
|
16706
|
769 (format-time-string "%y%m%d"))
|
3854
|
770
|
|
771 (defun time-stamp-hh:mm:ss ()
|
5632
|
772 "Return the current time as a string in \"HH:MM:SS\" form."
|
16706
|
773 (format-time-string "%T"))
|
3854
|
774
|
|
775 (defun time-stamp-hhmm ()
|
5632
|
776 "Return the current time as a string in \"HHMM\" form."
|
16706
|
777 (format-time-string "%H%M"))
|
3854
|
778
|
|
779 (provide 'time-stamp)
|
|
780
|
52401
|
781 ;;; arch-tag: 8a12c5c3-25d6-4a71-adc5-24b0e025a1e7
|
3854
|
782 ;;; time-stamp.el ends here
|