Added gud-speedbar-buttons, and support for GDB buttons.
[emacs.git] / lisp / time-stamp.el
blob93c4f86af1d43b379452bce9220c59abac8a8cfa
1 ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs
3 ;; Copyright 1989, 1993, 1994, 1995, 1997 Free Software Foundation, Inc.
5 ;; Maintainer's Time-stamp: <1997-08-07 14:46:50 gildea>
6 ;; Maintainer: Stephen Gildea <gildea@alum.mit.edu>
7 ;; Keywords: tools
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.
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.
19 ;; You should have received a copy of the GNU General Public License
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.
24 ;;; Commentary:
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.
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.
35 ;; See the documentation for the functions `time-stamp'
36 ;; and `time-stamp-toggle-active' for details.
38 ;;; Code:
40 (defgroup time-stamp nil
41 "Maintain last change time stamps in files edited by Emacs."
42 :group 'data
43 :group 'extensions)
45 (defcustom time-stamp-format "%:y-%02m-%02d %02H:%02M:%02S %u"
46 "*Format of the string inserted by \\[time-stamp].
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'.
50 A string is used verbatim except for character sequences beginning with %:
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'
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
72 %h mail host name
74 Decimal digits between the % and the type character specify the
75 field width. Strings are truncated on the right; years on the left.
76 A leading zero causes numbers to be zero-filled.
78 For example, to get the format used by the `date' command,
79 use \"%3a %3b %2d %02H:%02M:%02S %Z %:y\".
81 In the future these formats will be aligned more with format-time-string.
82 Because of this transition, the default padding for numeric formats will
83 change in a future version. Therefore either a padding width should be
84 specified, or the : modifier should be used to explicitly request the
85 historical default."
86 :type 'string
87 :group 'time-stamp)
89 (defcustom time-stamp-active t
90 "*Non-nil to enable time-stamping of buffers by \\[time-stamp].
91 Can be toggled by \\[time-stamp-toggle-active].
92 See also the variable `time-stamp-warn-inactive'."
93 :type 'boolean
94 :group 'time-stamp)
96 (defcustom time-stamp-warn-inactive t
97 "Non-nil to have \\[time-stamp] warn if a buffer did not get time-stamped.
98 A warning is printed if `time-stamp-active' is nil and the buffer contains
99 a time stamp template that would otherwise have been updated."
100 :type 'boolean
101 :group 'time-stamp)
103 (defcustom time-stamp-old-format-warn 'ask
104 "Action to take if `time-stamp-format' is an old-style list.
105 If `error', the format is not used. If `ask', the user is queried about
106 using the time-stamp-format. If `warn', a warning is displayed.
107 If nil, no notification is given."
108 :type '(choice (const :tag "No notification" nil)
109 (const :tag "Don't use the format" error)
110 (const ask) (const warn))
111 :group 'time-stamp)
113 (defcustom time-stamp-time-zone nil
114 "If non-nil, a string naming the timezone to be used by \\[time-stamp].
115 Format is the same as that used by the environment variable TZ on your system."
116 :type '(choice (const nil) string)
117 :group 'time-stamp)
120 ;;; Do not change time-stamp-line-limit, time-stamp-start, or
121 ;;; time-stamp-end in your .emacs or you will be incompatible
122 ;;; with other people's files! If you must change them,
123 ;;; do so only in the local variables section of the file itself.
126 (defvar time-stamp-line-limit 8 ;Do not change!
127 "Lines of a file searched; positive counts from start, negative from end.
128 The patterns `time-stamp-start' and `time-stamp-end' must be found on one
129 of the first (last) `time-stamp-line-limit' lines of the file for the
130 file to be time-stamped by \\[time-stamp].
132 Do not change `time-stamp-line-limit', `time-stamp-start', or
133 `time-stamp-end' for yourself or you will be incompatible
134 with other people's files! If you must change them for some application,
135 do so in the local variables section of the time-stamped file itself.")
138 (defvar time-stamp-start "Time-stamp:[ \t]+\\\\?[\"<]+" ;Do not change!
139 "Regexp after which the time stamp is written by \\[time-stamp].
140 See also the variables `time-stamp-end' and `time-stamp-line-limit'.
142 Do not change `time-stamp-line-limit', `time-stamp-start', or
143 `time-stamp-end' for yourself or you will be incompatible
144 with other people's files! If you must change them for some application,
145 do so in the local variables section of the time-stamped file itself.")
148 (defvar time-stamp-end "\\\\?[\">]" ;Do not change!
149 "Regexp marking the text after the time stamp.
150 \\[time-stamp] deletes the text between the first match of `time-stamp-start'
151 and the following match of `time-stamp-end' on the same line,
152 then writes the time stamp specified by `time-stamp-format' between them.
154 Do not change `time-stamp-line-limit', `time-stamp-start', or
155 `time-stamp-end' for yourself or you will be incompatible
156 with other people's files! If you must change them for some application,
157 do so in the local variables section of the time-stamped file itself.")
161 ;;;###autoload
162 (defun time-stamp ()
163 "Update the time stamp string in the buffer.
164 A template in a file can be automatically updated with a new time stamp
165 every time you save the file. Add this line to your .emacs file:
166 (add-hook 'write-file-hooks 'time-stamp)
167 Normally the template must appear in the first 8 lines of a file and
168 look like one of the following:
169 Time-stamp: <>
170 Time-stamp: \" \"
171 The time stamp is written between the brackets or quotes:
172 Time-stamp: <1996-07-18 10:20:51 gildea>
173 The time stamp is updated only if the variable `time-stamp-active' is non-nil.
174 The format of the time stamp is set by the variable `time-stamp-format'.
175 The variables `time-stamp-line-limit', `time-stamp-start',
176 and `time-stamp-end' control finding the template."
177 (interactive)
178 (let ((case-fold-search nil)
179 (start nil)
180 (end nil)
181 search-limit
182 (line-limit time-stamp-line-limit))
183 (cond ((not (integerp line-limit))
184 (setq line-limit 8)
185 (message "time-stamp-line-limit is not a number")
186 (sit-for 1)))
187 (save-excursion
188 (save-restriction
189 (widen)
190 (cond ((> line-limit 0)
191 (goto-char (setq start (point-min)))
192 (forward-line line-limit)
193 (setq search-limit (point)))
195 (goto-char (setq search-limit (point-max)))
196 (forward-line line-limit)
197 (setq start (point))))
198 (goto-char start)
199 (while (and (< (point) search-limit)
200 (not end)
201 (re-search-forward time-stamp-start search-limit 'move))
202 (setq start (point))
203 (end-of-line)
204 (let ((line-end (point)))
205 (goto-char start)
206 (if (re-search-forward time-stamp-end line-end 'move)
207 (setq end (match-beginning 0)))))))
208 (if end
209 (progn
210 ;; do all warnings outside save-excursion
211 (cond
212 ((not time-stamp-active)
213 (if time-stamp-warn-inactive
214 ;; don't signal an error in a write-file-hook
215 (progn
216 (message "Warning: time-stamp-active is off; did not time-stamp buffer.")
217 (sit-for 1))))
218 ((not (and (stringp time-stamp-start)
219 (stringp time-stamp-end)))
220 (message "time-stamp-start or time-stamp-end is not a string")
221 (sit-for 1))
223 (let ((new-time-stamp (time-stamp-string)))
224 (if (stringp new-time-stamp)
225 (save-excursion
226 (save-restriction
227 (widen)
228 (delete-region start end)
229 (goto-char start)
230 (insert-and-inherit new-time-stamp)
231 (setq end (point))
232 ;; remove any tabs used to format time stamp
233 (goto-char start)
234 (if (search-forward "\t" end t)
235 (untabify start end)))))))))))
236 ;; be sure to return nil so can be used on write-file-hooks
237 nil)
239 ;;;###autoload
240 (defun time-stamp-toggle-active (&optional arg)
241 "Toggle `time-stamp-active', setting whether \\[time-stamp] updates a buffer.
242 With arg, turn time stamping on if and only if arg is positive."
243 (interactive "P")
244 (setq time-stamp-active
245 (if (null arg)
246 (not time-stamp-active)
247 (> (prefix-numeric-value arg) 0)))
248 (message "time-stamp is now %s." (if time-stamp-active "active" "off")))
250 (defconst time-stamp-no-file "(no file)"
251 "String to use when the buffer is not associated with a file.")
253 ;;; time-stamp is transitioning to using the new, expanded capabilities
254 ;;; of format-time-string. During the process, this function implements
255 ;;; intermediate, compatible formats and complains about old, soon to
256 ;;; be unsupported, formats. This function will get a lot (a LOT) shorter
257 ;;; when the transition is complete and we can just pass most things
258 ;;; straight through to format-time-string.
259 ;;; At all times, all the formats recommended in the doc string
260 ;;; of time-stamp-format will work not only in the current version of
261 ;;; Emacs, but in all versions that have been released within the past
262 ;;; two years.
263 ;;; The : modifier is a temporary conversion feature used to resolve
264 ;;; ambiguous formats--formats that are changing (over time) incompatibly.
265 (defun time-stamp-string-preprocess (format &optional time)
266 ;; Uses a FORMAT to format date, time, file, and user information.
267 ;; Optional second argument TIME is only for testing.
268 ;; Implements non-time extensions to format-time-string
269 ;; and all time-stamp-format compatibility.
270 (let ((fmt-len (length format))
271 (ind 0)
272 cur-char
273 (prev-char nil)
274 (result "")
275 field-index
276 field-width
277 field-result
278 alt-form change-case require-padding
279 (paren-level 0))
280 (while (< ind fmt-len)
281 (setq cur-char (aref format ind))
282 (setq
283 result
284 (concat result
285 (cond
286 ((eq cur-char ?%)
287 ;; eat any additional args to allow for future expansion
288 (setq alt-form nil change-case nil require-padding nil)
289 (while (progn
290 (setq ind (1+ ind))
291 (setq cur-char (if (< ind fmt-len)
292 (aref format ind)
293 ?\0))
294 (or (eq ?. cur-char)
295 (eq ?, cur-char) (eq ?: cur-char) (eq ?@ cur-char)
296 (eq ?- cur-char) (eq ?+ cur-char) (eq ?_ cur-char)
297 (eq ?\ cur-char) (eq ?# cur-char) (eq ?^ cur-char)
298 (and (eq ?\( cur-char)
299 (not (eq prev-char ?\\))
300 (setq paren-level (1+ paren-level)))
301 (if (and (eq ?\) cur-char)
302 (not (eq prev-char ?\\))
303 (> paren-level 0))
304 (setq paren-level (1- paren-level))
305 (and (> paren-level 0)
306 (< ind fmt-len)))))
307 (setq prev-char cur-char)
308 ;; some characters we actually use
309 (cond ((eq cur-char ?:)
310 (setq alt-form t))
311 ((eq cur-char ?#)
312 (setq change-case t))))
313 ;; get format width
314 (setq field-index ind)
315 (setq ind (1- ind))
316 (while (progn
317 (setq ind (1+ ind))
318 (setq cur-char (if (< ind fmt-len)
319 (aref format ind)
320 ?\0))
321 (and (<= ?0 cur-char) (>= ?9 cur-char))))
322 (setq field-width (substring format field-index ind))
323 (setq field-result
324 (cond
325 ((eq cur-char ?%)
326 "%")
327 ((eq cur-char ?a) ;day of week
328 (if change-case
329 (format-time-string "%#A" time)
330 (or alt-form (not (string-equal field-width ""))
331 (time-stamp-conv-warn "%a" "%:a"))
332 (if (and alt-form (not (string-equal field-width "")))
333 "" ;discourage "%:3a"
334 (format-time-string "%A" time))))
335 ((eq cur-char ?A)
336 (if alt-form
337 (format-time-string "%A" time)
338 (or change-case (not (string-equal field-width ""))
339 (time-stamp-conv-warn "%A" "%#A"))
340 (format-time-string "%#A" time)))
341 ((eq cur-char ?b) ;month name
342 (if change-case
343 (format-time-string "%#B" time)
344 (or alt-form (not (string-equal field-width ""))
345 (time-stamp-conv-warn "%b" "%:b"))
346 (if (and alt-form (not (string-equal field-width "")))
347 "" ;discourage "%:3b"
348 (format-time-string "%B" time))))
349 ((eq cur-char ?B)
350 (if alt-form
351 (format-time-string "%B" time)
352 (or change-case (not (string-equal field-width ""))
353 (time-stamp-conv-warn "%B" "%#B"))
354 (format-time-string "%#B" time)))
355 ((eq cur-char ?d) ;day of month, 1-31
356 (time-stamp-do-number cur-char alt-form field-width time))
357 ((eq cur-char ?H) ;hour, 0-23
358 (time-stamp-do-number cur-char alt-form field-width time))
359 ((eq cur-char ?I) ;hour, 1-12
360 (time-stamp-do-number cur-char alt-form field-width time))
361 ((eq cur-char ?m) ;month number, 1-12
362 (time-stamp-do-number cur-char alt-form field-width time))
363 ((eq cur-char ?M) ;minute, 0-59
364 (time-stamp-do-number cur-char alt-form field-width time))
365 ((eq cur-char ?p) ;am or pm
366 (or change-case
367 (time-stamp-conv-warn "%p" "%#p"))
368 (format-time-string "%#p" time))
369 ((eq cur-char ?P) ;AM or PM
370 (format-time-string "%p" time))
371 ((eq cur-char ?S) ;seconds, 00-60
372 (time-stamp-do-number cur-char alt-form field-width time))
373 ((eq cur-char ?w) ;weekday number, Sunday is 0
374 (format-time-string "%w" time))
375 ((eq cur-char ?y) ;year
376 (or alt-form (not (string-equal field-width ""))
377 (time-stamp-conv-warn "%y" "%:y"))
378 (string-to-int (format-time-string "%Y" time)))
379 ((eq cur-char ?Y) ;4-digit year, new style
380 (string-to-int (format-time-string "%Y" time)))
381 ((eq cur-char ?z) ;time zone lower case
382 (if change-case
383 "" ;discourage %z variations
384 (format-time-string "%#Z" time)))
385 ((eq cur-char ?Z)
386 (if change-case
387 (format-time-string "%#Z" time)
388 (format-time-string "%Z" time)))
389 ((eq cur-char ?f) ;buffer-file-name, base name only
390 (if buffer-file-name
391 (file-name-nondirectory buffer-file-name)
392 time-stamp-no-file))
393 ((eq cur-char ?F) ;buffer-file-name, full path
394 (or buffer-file-name
395 time-stamp-no-file))
396 ((eq cur-char ?s) ;system name
397 (system-name))
398 ((eq cur-char ?u) ;user name
399 (user-login-name))
400 ((eq cur-char ?h) ;mail host name
401 (time-stamp-mail-host-name))
403 (if (string-equal field-width "")
404 field-result
405 (let ((padded-result
406 (format (format "%%%s%c"
407 field-width
408 (if (numberp field-result) ?d ?s))
409 (or field-result ""))))
410 (let ((initial-length (length padded-result))
411 (desired-length (string-to-int field-width)))
412 (if (> initial-length desired-length)
413 ;; truncate strings on right, years on left
414 (if (stringp field-result)
415 (substring padded-result 0 desired-length)
416 (if (eq cur-char ?y)
417 (substring padded-result (- desired-length))
418 padded-result)) ;non-year numbers don't truncate
419 padded-result)))))
421 (char-to-string cur-char)))))
422 (setq ind (1+ ind)))
423 result))
425 (defun time-stamp-do-number (format-char alt-form field-width time)
426 ;; Handle a compatible FORMAT-CHAR where only
427 ;; the default width/padding will change.
428 ;; ALT-FORM is whether `#' specified. FIELD-WIDTH is the string
429 ;; width specification or "". TIME is the time to convert.
430 (let ((format-string (concat "%" (char-to-string format-char))))
431 (and (not alt-form) (string-equal field-width "")
432 (time-stamp-conv-warn format-string
433 (format "%%:%c" format-char)))
434 (if (and alt-form (not (string-equal field-width "")))
435 "" ;discourage "%:2d" and the like
436 (string-to-int (format-time-string format-string time)))))
438 (defvar time-stamp-conversion-warn t
439 "Non-nil to warn about soon-to-be-unsupported forms in time-stamp-format.
440 In would be a bad idea to disable these warnings!
441 You really need to update your files instead.
443 The new formats will work with old versions of Emacs.
444 New formats are being recommended now to allow time-stamp-format
445 to change in the future to be compatible with format-time-string.
446 The new forms being recommended now will continue to work then.")
449 (defun time-stamp-conv-warn (old-form new-form)
450 ;; Display a warning about a soon-to-be-obsolete format.
451 (cond
452 (time-stamp-conversion-warn
453 (save-excursion
454 (set-buffer (get-buffer-create "*Time-stamp-compatibility*"))
455 (goto-char (point-max))
456 (if (bobp)
457 (progn
458 (insert
459 "The formats recognized in time-stamp-format will change in a future release\n"
460 "to be compatible with the new, expanded format-time-string function.\n\n"
461 "The following obsolescent time-stamp-format construct(s) were found:\n\n")))
462 (insert "\"" old-form "\" -- use " new-form "\n"))
463 (display-buffer "*Time-stamp-compatibility*"))))
467 (defun time-stamp-string ()
468 "Generate the new string to be inserted by \\[time-stamp]."
469 (if (stringp time-stamp-format)
470 (if (stringp time-stamp-time-zone)
471 (let ((real-time-zone (getenv "TZ")))
472 (unwind-protect
473 (progn
474 (setenv "TZ" time-stamp-time-zone)
475 (format-time-string
476 (time-stamp-string-preprocess time-stamp-format)))
477 (setenv "TZ" real-time-zone)))
478 (format-time-string
479 (time-stamp-string-preprocess time-stamp-format)))
480 ;; handle version 1 compatibility
481 (cond ((or (eq time-stamp-old-format-warn 'error)
482 (and (eq time-stamp-old-format-warn 'ask)
483 (not (y-or-n-p "Use non-string time-stamp-format? "))))
484 (message "Warning: no time-stamp: time-stamp-format not a string")
485 (sit-for 1)
486 nil)
488 (cond ((eq time-stamp-old-format-warn 'warn)
489 (message "Obsolescent time-stamp-format type; should be string")
490 (sit-for 1)))
491 (time-stamp-fconcat time-stamp-format " ")))))
493 (defconst time-stamp-no-file "(no file)"
494 "String to use when the buffer is not associated with a file.")
496 (defun time-stamp-mail-host-name ()
497 "Return the name of the host where the user receives mail.
498 This is the value of `mail-host-address' if bound and a string,
499 otherwise the value of the function system-name."
500 (or (and (boundp 'mail-host-address)
501 (stringp mail-host-address)
502 mail-host-address)
503 (system-name)))
505 ;;; the rest of this file is for version 1 compatibility
507 (defun time-stamp-fconcat (list sep)
508 "Similar to (mapconcat 'funcall LIST SEP) but LIST allows literals.
509 If an element of LIST is a symbol, it is funcalled to get the string to use;
510 the separator SEP is used between two strings obtained by funcalling a
511 symbol. Otherwise the element itself is inserted; no separator is used
512 around literals."
513 (let ((return-string "")
514 (insert-sep-p nil))
515 (while list
516 (cond ((symbolp (car list))
517 (if insert-sep-p
518 (setq return-string (concat return-string sep)))
519 (setq return-string (concat return-string (funcall (car list))))
520 (setq insert-sep-p t))
522 (setq return-string (concat return-string (car list)))
523 (setq insert-sep-p nil)))
524 (setq list (cdr list)))
525 return-string))
527 ;;; Some functions used in time-stamp-format
529 ;;; Could generate most of a message-id with
530 ;;; '(time-stamp-yymmdd "" time-stamp-hhmm "@" time-stamp-mail-host-name)
532 ;;; pretty form, suitable for a title page
534 (defun time-stamp-month-dd-yyyy ()
535 "Return the current date as a string in \"Month DD, YYYY\" form."
536 (format-time-string "%B %e, %Y"))
538 (defun time-stamp-dd/mm/yyyy ()
539 "Return the current date as a string in \"DD/MM/YYYY\" form."
540 (format-time-string "%d/%m/%Y"))
542 ;;; same as __DATE__ in ANSI C
544 (defun time-stamp-mon-dd-yyyy ()
545 "Return the current date as a string in \"Mon DD YYYY\" form.
546 The first character of DD is space if the value is less than 10."
547 (format-time-string "%b %d %Y"))
549 ;;; RFC 822 date
551 (defun time-stamp-dd-mon-yy ()
552 "Return the current date as a string in \"DD Mon YY\" form."
553 (format-time-string "%d %b %y"))
555 ;;; RCS 3 date
557 (defun time-stamp-yy/mm/dd ()
558 "Return the current date as a string in \"YY/MM/DD\" form."
559 (format-time-string "%y/%m/%d"))
561 ;;; RCS 5 date
563 (defun time-stamp-yyyy/mm/dd ()
564 "Return the current date as a string in \"YYYY/MM/DD\" form."
565 (format-time-string "%Y/%m/%d"))
567 ;;; ISO 8601 date
569 (defun time-stamp-yyyy-mm-dd ()
570 "Return the current date as a string in \"YYYY-MM-DD\" form."
571 (format-time-string "%Y-%m-%d"))
573 (defun time-stamp-yymmdd ()
574 "Return the current date as a string in \"YYMMDD\" form."
575 (format-time-string "%y%m%d"))
577 (defun time-stamp-hh:mm:ss ()
578 "Return the current time as a string in \"HH:MM:SS\" form."
579 (format-time-string "%T"))
581 (defun time-stamp-hhmm ()
582 "Return the current time as a string in \"HHMM\" form."
583 (format-time-string "%H%M"))
585 (provide 'time-stamp)
587 ;;; time-stamp.el ends here