Commit | Line | Data |
---|---|---|
9565745a | 1 | ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs |
9565745a | 2 | |
fd72ddf6 | 3 | ;; Copyright 1989, 1993, 1994, 1995, 1997 Free Software Foundation, Inc. |
b578f267 | 4 | |
b4b33e01 | 5 | ;; Maintainer's Time-stamp: <1998-03-04 14:14:19 gildea> |
622b7ede | 6 | ;; Maintainer: Stephen Gildea <gildea@alum.mit.edu> |
9565745a RS |
7 | ;; Keywords: tools |
8 | ||
9 | ;; This file is free software; you can redistribute it and/or modify | |
10 | ;; it under the terms of the GNU General Public License as published by | |
11 | ;; the Free Software Foundation; either version 2, or (at your option) | |
12 | ;; any later version. | |
13 | ||
14 | ;; This file is distributed in the hope that it will be useful, | |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
18 | ||
19 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
20 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
21 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | ;; Boston, MA 02111-1307, USA. | |
9565745a RS |
23 | |
24 | ;;; Commentary: | |
25 | ||
03d7856a KH |
26 | ;; A template in a file can be updated with a new time stamp when |
27 | ;; you save the file. For example: | |
28 | ;; static char *ts = "sdmain.c Time-stamp: <1996-08-13 10:20:51 gildea>"; | |
29 | ;; See the top of `time-stamp.el' for another example. | |
30 | ||
31 | ;; To use time-stamping, add this line to your .emacs file: | |
32 | ;; (add-hook 'write-file-hooks 'time-stamp) | |
33 | ;; Now any time-stamp templates in your files will be updated automatically. | |
34 | ||
35 | ;; See the documentation for the functions `time-stamp' | |
36 | ;; and `time-stamp-toggle-active' for details. | |
b578f267 | 37 | |
9565745a RS |
38 | ;;; Code: |
39 | ||
cb35a83c RS |
40 | (defgroup time-stamp nil |
41 | "Maintain last change time stamps in files edited by Emacs." | |
42 | :group 'data | |
43 | :group 'extensions) | |
44 | ||
fd72ddf6 | 45 | (defcustom time-stamp-format "%:y-%02m-%02d %02H:%02M:%02S %u" |
03d7856a | 46 | "*Format of the string inserted by \\[time-stamp]. |
c9c0e4bb RS |
47 | The value may be a string or a list. Lists are supported only for |
48 | backward compatibility; see variable `time-stamp-old-format-warn'. | |
f8d35bf3 | 49 | |
fd72ddf6 RS |
50 | A string is used verbatim except for character sequences beginning with %: |
51 | ||
52 | %:a weekday name: `Monday'. %#A gives uppercase: `MONDAY' | |
53 | %3a abbreviated weekday: `Mon'. %3A gives uppercase: `MON' | |
54 | %:b month name: `January'. %#B gives uppercase: `JANUARY' | |
55 | %3b abbreviated month: `Jan'. %3B gives uppercase: `JAN' | |
56 | %02d day of month | |
57 | %02H 24-hour clock hour | |
58 | %02I 12-hour clock hour | |
59 | %02m month number | |
60 | %02M minute | |
61 | %#p `am' or `pm'. %P gives uppercase: `AM' or `PM' | |
62 | %02S seconds | |
63 | %w day number of week, Sunday is 0 | |
64 | %02y 2-digit year: `97' %:y 4-digit year: `1997' | |
65 | %z time zone name: `est'. %Z gives uppercase: `EST' | |
66 | ||
67 | Non-date items: | |
68 | %% a literal percent character: `%' | |
69 | %f file name without directory %F gives absolute pathname | |
70 | %s system name | |
71 | %u user's login name | |
2c8d5749 | 72 | %U user's full name |
fd72ddf6 RS |
73 | %h mail host name |
74 | ||
75 | Decimal digits between the % and the type character specify the | |
76 | field width. Strings are truncated on the right; years on the left. | |
77 | A leading zero causes numbers to be zero-filled. | |
622b7ede RS |
78 | |
79 | For example, to get the format used by the `date' command, | |
fd72ddf6 RS |
80 | use \"%3a %3b %2d %02H:%02M:%02S %Z %:y\". |
81 | ||
82 | In the future these formats will be aligned more with format-time-string. | |
83 | Because of this transition, the default padding for numeric formats will | |
84 | change in a future version. Therefore either a padding width should be | |
85 | specified, or the : modifier should be used to explicitly request the | |
86 | historical default." | |
cb35a83c RS |
87 | :type 'string |
88 | :group 'time-stamp) | |
03d7856a | 89 | |
95772e85 RS |
90 | (defcustom time-stamp-active t |
91 | "*Non-nil to enable time-stamping of buffers by \\[time-stamp]. | |
92 | Can be toggled by \\[time-stamp-toggle-active]. | |
93 | See also the variable `time-stamp-warn-inactive'." | |
94 | :type 'boolean | |
95 | :group 'time-stamp) | |
96 | ||
97 | (defcustom time-stamp-warn-inactive t | |
98 | "Non-nil to have \\[time-stamp] warn if a buffer did not get time-stamped. | |
99 | A warning is printed if `time-stamp-active' is nil and the buffer contains | |
100 | a time stamp template that would otherwise have been updated." | |
101 | :type 'boolean | |
102 | :group 'time-stamp) | |
103 | ||
104 | (defcustom time-stamp-old-format-warn 'ask | |
105 | "Action to take if `time-stamp-format' is an old-style list. | |
106 | If `error', the format is not used. If `ask', the user is queried about | |
107 | using the time-stamp-format. If `warn', a warning is displayed. | |
108 | If nil, no notification is given." | |
109 | :type '(choice (const :tag "No notification" nil) | |
110 | (const :tag "Don't use the format" error) | |
111 | (const ask) (const warn)) | |
112 | :group 'time-stamp) | |
113 | ||
114 | (defcustom time-stamp-time-zone nil | |
115 | "If non-nil, a string naming the timezone to be used by \\[time-stamp]. | |
116 | Format is the same as that used by the environment variable TZ on your system." | |
7967f8ab | 117 | :type '(choice (const nil) string) |
95772e85 | 118 | :group 'time-stamp) |
622b7ede RS |
119 | |
120 | ||
b4b33e01 RS |
121 | ;;; Do not change time-stamp-line-limit, time-stamp-start, |
122 | ;;; time-stamp-end, or time-stamp-pattern in your .emacs | |
123 | ;;; or you will be incompatible with other people's files! | |
124 | ;;; If you must change them, do so only in the local variables | |
125 | ;;; section of the file itself. | |
9565745a | 126 | |
03d7856a | 127 | |
e1f40b28 | 128 | (defvar time-stamp-line-limit 8 ;Do not change! |
c7c4ddd4 | 129 | "Lines of a file searched; positive counts from start, negative from end. |
e1f40b28 | 130 | The patterns `time-stamp-start' and `time-stamp-end' must be found on one |
c7c4ddd4 KH |
131 | of the first (last) `time-stamp-line-limit' lines of the file for the |
132 | file to be time-stamped by \\[time-stamp]. | |
e1f40b28 RS |
133 | |
134 | Do not change `time-stamp-line-limit', `time-stamp-start', or | |
135 | `time-stamp-end' for yourself or you will be incompatible | |
136 | with other people's files! If you must change them for some application, | |
137 | do so in the local variables section of the time-stamped file itself.") | |
138 | ||
9565745a | 139 | |
36081614 | 140 | (defvar time-stamp-start "Time-stamp:[ \t]+\\\\?[\"<]+" ;Do not change! |
9565745a | 141 | "Regexp after which the time stamp is written by \\[time-stamp]. |
e1f40b28 | 142 | See also the variables `time-stamp-end' and `time-stamp-line-limit'. |
9565745a | 143 | |
e1f40b28 RS |
144 | Do not change `time-stamp-line-limit', `time-stamp-start', or |
145 | `time-stamp-end' for yourself or you will be incompatible | |
9565745a RS |
146 | with other people's files! If you must change them for some application, |
147 | do so in the local variables section of the time-stamped file itself.") | |
148 | ||
149 | ||
e1f40b28 | 150 | (defvar time-stamp-end "\\\\?[\">]" ;Do not change! |
9565745a | 151 | "Regexp marking the text after the time stamp. |
e1f40b28 RS |
152 | \\[time-stamp] deletes the text between the first match of `time-stamp-start' |
153 | and the following match of `time-stamp-end' on the same line, | |
154 | then writes the time stamp specified by `time-stamp-format' between them. | |
155 | ||
156 | Do not change `time-stamp-line-limit', `time-stamp-start', or | |
157 | `time-stamp-end' for yourself or you will be incompatible | |
158 | with other people's files! If you must change them for some application, | |
159 | do so in the local variables section of the time-stamped file itself.") | |
160 | ||
9565745a | 161 | |
b4b33e01 RS |
162 | (defvar time-stamp-pattern "%%" ;Do not change! |
163 | "Convenience variable setting all time-stamp location and format variables. | |
164 | This string has four parts, each of which is optional. | |
165 | These four parts set time-stamp-line-limit, time-stamp-start, | |
166 | time-stamp-format, and time-stamp-end. See the documentation | |
167 | for each of these variables for details. | |
168 | ||
169 | The first part is a number followed by a slash; the number sets the number | |
170 | of lines at the beginning (negative counts from end) of the file searched | |
171 | for the time-stamp. The number and the slash may be omitted to use the | |
172 | normal value. | |
173 | ||
174 | The second part is a regexp identifying the pattern preceding the time stamp. | |
175 | This part may be omitted to use the normal pattern. | |
176 | ||
177 | The third part specifies the format of the time-stamp inserted. See | |
178 | the documentation for time-stamp-format for details. Specify this | |
179 | part as \"%%\" to use the normal format. | |
180 | ||
181 | The fourth part is a regexp identifying the pattern following the time stamp. | |
182 | This part may be omitted to use the normal pattern. | |
183 | ||
184 | As an example, the default behavior can be specified something like this: | |
185 | \"8/Time-stamp: [\\\"<]%:y-%02m-%02d %02H:%02M:%02S %u[\\\">]\" | |
186 | ||
187 | Do not change `time-stamp-pattern' for yourself or you will be incompatible | |
188 | with other people's files! Set it only in the local variables section | |
189 | of the time-stamped file itself.") | |
190 | ||
191 | ||
03d7856a | 192 | |
59b644e8 | 193 | ;;;###autoload |
9565745a RS |
194 | (defun time-stamp () |
195 | "Update the time stamp string in the buffer. | |
03d7856a KH |
196 | A template in a file can be automatically updated with a new time stamp |
197 | every time you save the file. Add this line to your .emacs file: | |
198 | (add-hook 'write-file-hooks 'time-stamp) | |
199 | Normally the template must appear in the first 8 lines of a file and | |
200 | look like one of the following: | |
201 | Time-stamp: <> | |
202 | Time-stamp: \" \" | |
203 | The time stamp is written between the brackets or quotes: | |
b4b33e01 | 204 | Time-stamp: <1998-02-18 10:20:51 gildea> |
622b7ede | 205 | The time stamp is updated only if the variable `time-stamp-active' is non-nil. |
03d7856a KH |
206 | The format of the time stamp is set by the variable `time-stamp-format'. |
207 | The variables `time-stamp-line-limit', `time-stamp-start', | |
208 | and `time-stamp-end' control finding the template." | |
9565745a | 209 | (interactive) |
fe8287c6 | 210 | (let ((case-fold-search nil) |
03d7856a KH |
211 | (start nil) |
212 | (end nil) | |
622b7ede | 213 | search-limit |
b4b33e01 RS |
214 | (line-limit time-stamp-line-limit) |
215 | (ts-start time-stamp-start) | |
216 | (ts-format time-stamp-format) | |
217 | (ts-end time-stamp-end)) | |
218 | (if (stringp time-stamp-pattern) | |
219 | (progn | |
15ceaba4 | 220 | (string-match "\\`\\(\\(-?[0-9]+\\)/\\)?\\([^%]+\\)?\\(\\(.\\|\n\\)*%[-.,:@+_ #^()0-9]*[A-Za-z%]\\)?\\([^%]+\\)?\\'" time-stamp-pattern) |
b4b33e01 RS |
221 | (and (match-beginning 2) |
222 | (setq line-limit | |
223 | (string-to-int (match-string 2 time-stamp-pattern)))) | |
224 | (and (match-beginning 3) | |
225 | (setq ts-start (match-string 3 time-stamp-pattern))) | |
226 | (and (match-beginning 4) | |
227 | (not (string-equal (match-string 4 time-stamp-pattern) "%%")) | |
228 | (setq ts-format (match-string 4 time-stamp-pattern))) | |
15ceaba4 KH |
229 | (and (match-beginning 6) |
230 | (setq ts-end (match-string 6 time-stamp-pattern))))) | |
622b7ede RS |
231 | (cond ((not (integerp line-limit)) |
232 | (setq line-limit 8) | |
b4b33e01 | 233 | (message "time-stamp-line-limit is not an integer") |
622b7ede RS |
234 | (sit-for 1))) |
235 | (save-excursion | |
236 | (save-restriction | |
237 | (widen) | |
238 | (cond ((> line-limit 0) | |
239 | (goto-char (setq start (point-min))) | |
240 | (forward-line line-limit) | |
241 | (setq search-limit (point))) | |
242 | (t | |
243 | (goto-char (setq search-limit (point-max))) | |
244 | (forward-line line-limit) | |
245 | (setq start (point)))) | |
246 | (goto-char start) | |
247 | (while (and (< (point) search-limit) | |
248 | (not end) | |
b4b33e01 | 249 | (re-search-forward ts-start search-limit 'move)) |
622b7ede RS |
250 | (setq start (point)) |
251 | (end-of-line) | |
252 | (let ((line-end (point))) | |
c7c4ddd4 | 253 | (goto-char start) |
b4b33e01 | 254 | (if (re-search-forward ts-end line-end 'move) |
622b7ede | 255 | (setq end (match-beginning 0))))))) |
b4b33e01 RS |
256 | (if end |
257 | (progn | |
258 | ;; do all warnings outside save-excursion | |
259 | (cond | |
260 | ((not time-stamp-active) | |
261 | (if time-stamp-warn-inactive | |
262 | ;; don't signal an error in a write-file-hook | |
263 | (progn | |
264 | (message "Warning: time-stamp-active is off; did not time-stamp buffer.") | |
265 | (sit-for 1)))) | |
266 | ((not (and (stringp ts-start) | |
267 | (stringp ts-end))) | |
268 | (message "time-stamp-start or time-stamp-end is not a string") | |
269 | (sit-for 1)) | |
270 | (t | |
271 | (let ((new-time-stamp (time-stamp-string ts-format))) | |
272 | (if (stringp new-time-stamp) | |
273 | (save-excursion | |
274 | (save-restriction | |
275 | (widen) | |
276 | (delete-region start end) | |
277 | (goto-char start) | |
278 | (insert-and-inherit new-time-stamp) | |
279 | (setq end (point)) | |
280 | ;; remove any tabs used to format time stamp | |
281 | (goto-char start) | |
282 | (if (search-forward "\t" end t) | |
283 | (untabify start end))))))))))) | |
9565745a RS |
284 | ;; be sure to return nil so can be used on write-file-hooks |
285 | nil) | |
286 | ||
b1defad2 RS |
287 | ;;;###autoload |
288 | (defun time-stamp-toggle-active (&optional arg) | |
03d7856a | 289 | "Toggle `time-stamp-active', setting whether \\[time-stamp] updates a buffer. |
b1defad2 RS |
290 | With arg, turn time stamping on if and only if arg is positive." |
291 | (interactive "P") | |
292 | (setq time-stamp-active | |
293 | (if (null arg) | |
294 | (not time-stamp-active) | |
295 | (> (prefix-numeric-value arg) 0))) | |
296 | (message "time-stamp is now %s." (if time-stamp-active "active" "off"))) | |
03d7856a | 297 | |
6946129c RS |
298 | (defconst time-stamp-no-file "(no file)" |
299 | "String to use when the buffer is not associated with a file.") | |
300 | ||
fd72ddf6 RS |
301 | ;;; time-stamp is transitioning to using the new, expanded capabilities |
302 | ;;; of format-time-string. During the process, this function implements | |
303 | ;;; intermediate, compatible formats and complains about old, soon to | |
304 | ;;; be unsupported, formats. This function will get a lot (a LOT) shorter | |
305 | ;;; when the transition is complete and we can just pass most things | |
306 | ;;; straight through to format-time-string. | |
307 | ;;; At all times, all the formats recommended in the doc string | |
308 | ;;; of time-stamp-format will work not only in the current version of | |
309 | ;;; Emacs, but in all versions that have been released within the past | |
310 | ;;; two years. | |
311 | ;;; The : modifier is a temporary conversion feature used to resolve | |
312 | ;;; ambiguous formats--formats that are changing (over time) incompatibly. | |
313 | (defun time-stamp-string-preprocess (format &optional time) | |
314 | ;; Uses a FORMAT to format date, time, file, and user information. | |
315 | ;; Optional second argument TIME is only for testing. | |
316 | ;; Implements non-time extensions to format-time-string | |
317 | ;; and all time-stamp-format compatibility. | |
318 | (let ((fmt-len (length format)) | |
319 | (ind 0) | |
320 | cur-char | |
321 | (prev-char nil) | |
322 | (result "") | |
fd72ddf6 RS |
323 | field-width |
324 | field-result | |
325 | alt-form change-case require-padding | |
326 | (paren-level 0)) | |
327 | (while (< ind fmt-len) | |
328 | (setq cur-char (aref format ind)) | |
329 | (setq | |
330 | result | |
331 | (concat result | |
332 | (cond | |
333 | ((eq cur-char ?%) | |
334 | ;; eat any additional args to allow for future expansion | |
15ceaba4 | 335 | (setq alt-form nil change-case nil require-padding nil field-width "") |
fd72ddf6 RS |
336 | (while (progn |
337 | (setq ind (1+ ind)) | |
338 | (setq cur-char (if (< ind fmt-len) | |
339 | (aref format ind) | |
340 | ?\0)) | |
341 | (or (eq ?. cur-char) | |
342 | (eq ?, cur-char) (eq ?: cur-char) (eq ?@ cur-char) | |
343 | (eq ?- cur-char) (eq ?+ cur-char) (eq ?_ cur-char) | |
344 | (eq ?\ cur-char) (eq ?# cur-char) (eq ?^ cur-char) | |
345 | (and (eq ?\( cur-char) | |
346 | (not (eq prev-char ?\\)) | |
347 | (setq paren-level (1+ paren-level))) | |
348 | (if (and (eq ?\) cur-char) | |
349 | (not (eq prev-char ?\\)) | |
350 | (> paren-level 0)) | |
351 | (setq paren-level (1- paren-level)) | |
352 | (and (> paren-level 0) | |
15ceaba4 KH |
353 | (< ind fmt-len))) |
354 | (if (and (<= ?0 cur-char) (>= ?9 cur-char)) | |
355 | ;; get format width | |
356 | (let ((field-index ind)) | |
357 | (while (progn | |
358 | (setq ind (1+ ind)) | |
359 | (setq cur-char (if (< ind fmt-len) | |
360 | (aref format ind) | |
361 | ?\0)) | |
362 | (and (<= ?0 cur-char) (>= ?9 cur-char)))) | |
363 | (setq field-width (substring format field-index ind)) | |
364 | (setq ind (1- ind)) | |
365 | t)))) | |
fd72ddf6 RS |
366 | (setq prev-char cur-char) |
367 | ;; some characters we actually use | |
368 | (cond ((eq cur-char ?:) | |
369 | (setq alt-form t)) | |
370 | ((eq cur-char ?#) | |
371 | (setq change-case t)))) | |
fd72ddf6 RS |
372 | (setq field-result |
373 | (cond | |
374 | ((eq cur-char ?%) | |
375 | "%") | |
376 | ((eq cur-char ?a) ;day of week | |
377 | (if change-case | |
378 | (format-time-string "%#A" time) | |
379 | (or alt-form (not (string-equal field-width "")) | |
380 | (time-stamp-conv-warn "%a" "%:a")) | |
381 | (if (and alt-form (not (string-equal field-width ""))) | |
382 | "" ;discourage "%:3a" | |
383 | (format-time-string "%A" time)))) | |
384 | ((eq cur-char ?A) | |
385 | (if alt-form | |
386 | (format-time-string "%A" time) | |
387 | (or change-case (not (string-equal field-width "")) | |
388 | (time-stamp-conv-warn "%A" "%#A")) | |
389 | (format-time-string "%#A" time))) | |
390 | ((eq cur-char ?b) ;month name | |
391 | (if change-case | |
392 | (format-time-string "%#B" time) | |
393 | (or alt-form (not (string-equal field-width "")) | |
394 | (time-stamp-conv-warn "%b" "%:b")) | |
395 | (if (and alt-form (not (string-equal field-width ""))) | |
396 | "" ;discourage "%:3b" | |
397 | (format-time-string "%B" time)))) | |
398 | ((eq cur-char ?B) | |
399 | (if alt-form | |
400 | (format-time-string "%B" time) | |
401 | (or change-case (not (string-equal field-width "")) | |
402 | (time-stamp-conv-warn "%B" "%#B")) | |
403 | (format-time-string "%#B" time))) | |
404 | ((eq cur-char ?d) ;day of month, 1-31 | |
fdac7fba | 405 | (time-stamp-do-number cur-char alt-form field-width time)) |
fd72ddf6 | 406 | ((eq cur-char ?H) ;hour, 0-23 |
fdac7fba | 407 | (time-stamp-do-number cur-char alt-form field-width time)) |
fd72ddf6 | 408 | ((eq cur-char ?I) ;hour, 1-12 |
fdac7fba | 409 | (time-stamp-do-number cur-char alt-form field-width time)) |
fd72ddf6 | 410 | ((eq cur-char ?m) ;month number, 1-12 |
fdac7fba | 411 | (time-stamp-do-number cur-char alt-form field-width time)) |
fd72ddf6 | 412 | ((eq cur-char ?M) ;minute, 0-59 |
fdac7fba | 413 | (time-stamp-do-number cur-char alt-form field-width time)) |
fd72ddf6 RS |
414 | ((eq cur-char ?p) ;am or pm |
415 | (or change-case | |
416 | (time-stamp-conv-warn "%p" "%#p")) | |
417 | (format-time-string "%#p" time)) | |
418 | ((eq cur-char ?P) ;AM or PM | |
419 | (format-time-string "%p" time)) | |
420 | ((eq cur-char ?S) ;seconds, 00-60 | |
fdac7fba | 421 | (time-stamp-do-number cur-char alt-form field-width time)) |
fd72ddf6 RS |
422 | ((eq cur-char ?w) ;weekday number, Sunday is 0 |
423 | (format-time-string "%w" time)) | |
424 | ((eq cur-char ?y) ;year | |
425 | (or alt-form (not (string-equal field-width "")) | |
426 | (time-stamp-conv-warn "%y" "%:y")) | |
427 | (string-to-int (format-time-string "%Y" time))) | |
428 | ((eq cur-char ?Y) ;4-digit year, new style | |
429 | (string-to-int (format-time-string "%Y" time))) | |
430 | ((eq cur-char ?z) ;time zone lower case | |
431 | (if change-case | |
432 | "" ;discourage %z variations | |
433 | (format-time-string "%#Z" time))) | |
434 | ((eq cur-char ?Z) | |
435 | (if change-case | |
436 | (format-time-string "%#Z" time) | |
437 | (format-time-string "%Z" time))) | |
438 | ((eq cur-char ?f) ;buffer-file-name, base name only | |
439 | (if buffer-file-name | |
440 | (file-name-nondirectory buffer-file-name) | |
441 | time-stamp-no-file)) | |
442 | ((eq cur-char ?F) ;buffer-file-name, full path | |
443 | (or buffer-file-name | |
444 | time-stamp-no-file)) | |
445 | ((eq cur-char ?s) ;system name | |
446 | (system-name)) | |
447 | ((eq cur-char ?u) ;user name | |
448 | (user-login-name)) | |
2c8d5749 RS |
449 | ((eq cur-char ?U) ;user full name |
450 | (user-full-name)) | |
fd72ddf6 RS |
451 | ((eq cur-char ?h) ;mail host name |
452 | (time-stamp-mail-host-name)) | |
453 | )) | |
454 | (if (string-equal field-width "") | |
455 | field-result | |
456 | (let ((padded-result | |
457 | (format (format "%%%s%c" | |
458 | field-width | |
459 | (if (numberp field-result) ?d ?s)) | |
460 | (or field-result "")))) | |
461 | (let ((initial-length (length padded-result)) | |
462 | (desired-length (string-to-int field-width))) | |
463 | (if (> initial-length desired-length) | |
464 | ;; truncate strings on right, years on left | |
465 | (if (stringp field-result) | |
466 | (substring padded-result 0 desired-length) | |
467 | (if (eq cur-char ?y) | |
468 | (substring padded-result (- desired-length)) | |
469 | padded-result)) ;non-year numbers don't truncate | |
470 | padded-result))))) | |
471 | (t | |
472 | (char-to-string cur-char))))) | |
473 | (setq ind (1+ ind))) | |
474 | result)) | |
475 | ||
fdac7fba RS |
476 | (defun time-stamp-do-number (format-char alt-form field-width time) |
477 | ;; Handle a compatible FORMAT-CHAR where only | |
fd72ddf6 | 478 | ;; the default width/padding will change. |
fdac7fba RS |
479 | ;; ALT-FORM is whether `#' specified. FIELD-WIDTH is the string |
480 | ;; width specification or "". TIME is the time to convert. | |
fd72ddf6 RS |
481 | (let ((format-string (concat "%" (char-to-string format-char)))) |
482 | (and (not alt-form) (string-equal field-width "") | |
483 | (time-stamp-conv-warn format-string | |
484 | (format "%%:%c" format-char))) | |
485 | (if (and alt-form (not (string-equal field-width ""))) | |
486 | "" ;discourage "%:2d" and the like | |
487 | (string-to-int (format-time-string format-string time))))) | |
488 | ||
489 | (defvar time-stamp-conversion-warn t | |
490 | "Non-nil to warn about soon-to-be-unsupported forms in time-stamp-format. | |
491 | In would be a bad idea to disable these warnings! | |
492 | You really need to update your files instead. | |
493 | ||
494 | The new formats will work with old versions of Emacs. | |
495 | New formats are being recommended now to allow time-stamp-format | |
496 | to change in the future to be compatible with format-time-string. | |
497 | The new forms being recommended now will continue to work then.") | |
498 | ||
499 | ||
500 | (defun time-stamp-conv-warn (old-form new-form) | |
501 | ;; Display a warning about a soon-to-be-obsolete format. | |
502 | (cond | |
503 | (time-stamp-conversion-warn | |
504 | (save-excursion | |
505 | (set-buffer (get-buffer-create "*Time-stamp-compatibility*")) | |
506 | (goto-char (point-max)) | |
507 | (if (bobp) | |
508 | (progn | |
509 | (insert | |
510 | "The formats recognized in time-stamp-format will change in a future release\n" | |
511 | "to be compatible with the new, expanded format-time-string function.\n\n" | |
512 | "The following obsolescent time-stamp-format construct(s) were found:\n\n"))) | |
513 | (insert "\"" old-form "\" -- use " new-form "\n")) | |
514 | (display-buffer "*Time-stamp-compatibility*")))) | |
95772e85 | 515 | |
fd72ddf6 | 516 | |
b1defad2 | 517 | |
b4b33e01 RS |
518 | (defun time-stamp-string (&optional ts-format) |
519 | "Generate the new string to be inserted by \\[time-stamp]. | |
520 | Optionally use FORMAT." | |
521 | (or ts-format | |
522 | (setq ts-format time-stamp-format)) | |
523 | (if (stringp ts-format) | |
95772e85 RS |
524 | (if (stringp time-stamp-time-zone) |
525 | (let ((real-time-zone (getenv "TZ"))) | |
526 | (unwind-protect | |
527 | (progn | |
528 | (setenv "TZ" time-stamp-time-zone) | |
529 | (format-time-string | |
b4b33e01 | 530 | (time-stamp-string-preprocess ts-format))) |
95772e85 RS |
531 | (setenv "TZ" real-time-zone))) |
532 | (format-time-string | |
b4b33e01 | 533 | (time-stamp-string-preprocess ts-format))) |
03d7856a KH |
534 | ;; handle version 1 compatibility |
535 | (cond ((or (eq time-stamp-old-format-warn 'error) | |
536 | (and (eq time-stamp-old-format-warn 'ask) | |
537 | (not (y-or-n-p "Use non-string time-stamp-format? ")))) | |
538 | (message "Warning: no time-stamp: time-stamp-format not a string") | |
539 | (sit-for 1) | |
540 | nil) | |
541 | (t | |
542 | (cond ((eq time-stamp-old-format-warn 'warn) | |
543 | (message "Obsolescent time-stamp-format type; should be string") | |
544 | (sit-for 1))) | |
b4b33e01 | 545 | (time-stamp-fconcat ts-format " "))))) |
b1defad2 | 546 | |
c7c4ddd4 KH |
547 | (defconst time-stamp-no-file "(no file)" |
548 | "String to use when the buffer is not associated with a file.") | |
549 | ||
b1defad2 RS |
550 | (defun time-stamp-mail-host-name () |
551 | "Return the name of the host where the user receives mail. | |
552 | This is the value of `mail-host-address' if bound and a string, | |
e654f4b0 | 553 | otherwise the value of the function system-name." |
b1defad2 RS |
554 | (or (and (boundp 'mail-host-address) |
555 | (stringp mail-host-address) | |
556 | mail-host-address) | |
b1defad2 RS |
557 | (system-name))) |
558 | ||
559 | ;;; the rest of this file is for version 1 compatibility | |
9565745a RS |
560 | |
561 | (defun time-stamp-fconcat (list sep) | |
e1f40b28 | 562 | "Similar to (mapconcat 'funcall LIST SEP) but LIST allows literals. |
9565745a RS |
563 | If an element of LIST is a symbol, it is funcalled to get the string to use; |
564 | the separator SEP is used between two strings obtained by funcalling a | |
565 | symbol. Otherwise the element itself is inserted; no separator is used | |
566 | around literals." | |
567 | (let ((return-string "") | |
568 | (insert-sep-p nil)) | |
569 | (while list | |
570 | (cond ((symbolp (car list)) | |
571 | (if insert-sep-p | |
572 | (setq return-string (concat return-string sep))) | |
573 | (setq return-string (concat return-string (funcall (car list)))) | |
574 | (setq insert-sep-p t)) | |
575 | (t | |
576 | (setq return-string (concat return-string (car list))) | |
577 | (setq insert-sep-p nil))) | |
578 | (setq list (cdr list))) | |
579 | return-string)) | |
580 | ||
03d7856a | 581 | ;;; Some functions used in time-stamp-format |
9565745a RS |
582 | |
583 | ;;; Could generate most of a message-id with | |
e1f40b28 | 584 | ;;; '(time-stamp-yymmdd "" time-stamp-hhmm "@" time-stamp-mail-host-name) |
9565745a | 585 | |
9565745a RS |
586 | ;;; pretty form, suitable for a title page |
587 | ||
588 | (defun time-stamp-month-dd-yyyy () | |
e1f40b28 | 589 | "Return the current date as a string in \"Month DD, YYYY\" form." |
b8944601 | 590 | (format-time-string "%B %e, %Y")) |
9565745a | 591 | |
b24173f7 RS |
592 | (defun time-stamp-dd/mm/yyyy () |
593 | "Return the current date as a string in \"DD/MM/YYYY\" form." | |
b8944601 | 594 | (format-time-string "%d/%m/%Y")) |
b24173f7 | 595 | |
9565745a RS |
596 | ;;; same as __DATE__ in ANSI C |
597 | ||
598 | (defun time-stamp-mon-dd-yyyy () | |
e1f40b28 RS |
599 | "Return the current date as a string in \"Mon DD YYYY\" form. |
600 | The first character of DD is space if the value is less than 10." | |
b8944601 | 601 | (format-time-string "%b %d %Y")) |
9565745a RS |
602 | |
603 | ;;; RFC 822 date | |
604 | ||
605 | (defun time-stamp-dd-mon-yy () | |
e1f40b28 | 606 | "Return the current date as a string in \"DD Mon YY\" form." |
b8944601 | 607 | (format-time-string "%d %b %y")) |
9565745a RS |
608 | |
609 | ;;; RCS 3 date | |
610 | ||
611 | (defun time-stamp-yy/mm/dd () | |
e1f40b28 | 612 | "Return the current date as a string in \"YY/MM/DD\" form." |
b8944601 | 613 | (format-time-string "%y/%m/%d")) |
9565745a RS |
614 | |
615 | ;;; RCS 5 date | |
616 | ||
617 | (defun time-stamp-yyyy/mm/dd () | |
e1f40b28 | 618 | "Return the current date as a string in \"YYYY/MM/DD\" form." |
b8944601 | 619 | (format-time-string "%Y/%m/%d")) |
9565745a | 620 | |
e1f40b28 | 621 | ;;; ISO 8601 date |
9565745a | 622 | |
e1f40b28 RS |
623 | (defun time-stamp-yyyy-mm-dd () |
624 | "Return the current date as a string in \"YYYY-MM-DD\" form." | |
b8944601 | 625 | (format-time-string "%Y-%m-%d")) |
9565745a | 626 | |
e1f40b28 RS |
627 | (defun time-stamp-yymmdd () |
628 | "Return the current date as a string in \"YYMMDD\" form." | |
b8944601 | 629 | (format-time-string "%y%m%d")) |
9565745a RS |
630 | |
631 | (defun time-stamp-hh:mm:ss () | |
e1f40b28 | 632 | "Return the current time as a string in \"HH:MM:SS\" form." |
b8944601 | 633 | (format-time-string "%T")) |
9565745a | 634 | |
9565745a | 635 | (defun time-stamp-hhmm () |
e1f40b28 | 636 | "Return the current time as a string in \"HHMM\" form." |
b8944601 | 637 | (format-time-string "%H%M")) |
9565745a RS |
638 | |
639 | (provide 'time-stamp) | |
640 | ||
641 | ;;; time-stamp.el ends here |