comparison lisp/time-stamp.el @ 17600:0dc8180c2e7a

(time-stamp-old-format-warn): Fix a tag string. (time-stamp-format): Use %Y not %y in default value. (time-stamp): Verify time-stamp-line-limit is a number. (time-stamp-string-preprocess): Handle %F correctly.
author Richard M. Stallman <rms@gnu.org>
date Wed, 30 Apr 1997 01:12:24 +0000
parents 368547942e03
children 7776ee6215de
comparison
equal deleted inserted replaced
17599:dd277f4e6737 17600:0dc8180c2e7a
1 ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs 1 ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs
2 2
3 ;; Copyright 1989, 1993, 1994, 1995 Free Software Foundation, Inc. 3 ;; Copyright 1989, 1993, 1994, 1995 Free Software Foundation, Inc.
4 4
5 ;; Maintainer's Time-stamp: <1996-08-13 14:03:17 gildea> 5 ;; Maintainer's Time-stamp: <1997-04-28 11:51:22 gildea>
6 ;; Maintainer: Stephen Gildea <gildea@lcs.mit.edu> 6 ;; Maintainer: Stephen Gildea <gildea@alum.mit.edu>
7 ;; Keywords: tools 7 ;; Keywords: tools
8 8
9 ;; This file is free software; you can redistribute it and/or modify 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 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) 11 ;; the Free Software Foundation; either version 2, or (at your option)
32 ;; (add-hook 'write-file-hooks 'time-stamp) 32 ;; (add-hook 'write-file-hooks 'time-stamp)
33 ;; Now any time-stamp templates in your files will be updated automatically. 33 ;; Now any time-stamp templates in your files will be updated automatically.
34 34
35 ;; See the documentation for the functions `time-stamp' 35 ;; See the documentation for the functions `time-stamp'
36 ;; and `time-stamp-toggle-active' for details. 36 ;; and `time-stamp-toggle-active' for details.
37
38 ;;; Change Log:
39
40 ;; Originally based on the 19 Dec 88 version of
41 ;; date.el by John Sturdy <mcvax!harlqn.co.uk!jcgs@uunet.uu.net>
42 ;; Version 2, January 1995: replaced functions with %-escapes
43 ;; $Id: time-stamp.el,v 1.24 1996/12/18 02:45:09 rms Exp rms $
44 37
45 ;;; Code: 38 ;;; Code:
46 39
47 (defgroup time-stamp nil 40 (defgroup time-stamp nil
48 "Maintain last change time stamps in files edited by Emacs." 41 "Maintain last change time stamps in files edited by Emacs."
66 (defcustom time-stamp-old-format-warn 'ask 59 (defcustom time-stamp-old-format-warn 'ask
67 "Action to take if `time-stamp-format' is an old-style list. 60 "Action to take if `time-stamp-format' is an old-style list.
68 If `error', the format is not used. If `ask', the user is queried about 61 If `error', the format is not used. If `ask', the user is queried about
69 using the time-stamp-format. If `warn', a warning is displayed. 62 using the time-stamp-format. If `warn', a warning is displayed.
70 If nil, no notification is given." 63 If nil, no notification is given."
71 :type '(choice (const :tag "No modification" nil) 64 :type '(choice (const :tag "No notification" nil)
72 (const :tag "Don't use the format" error) 65 (const :tag "Don't use the format" error)
73 (const ask) (const warn)) 66 (const ask) (const warn))
74 :group 'time-stamp) 67 :group 'time-stamp)
75 68
76 (defcustom time-stamp-format "%02y/%02m/%02d %02H:%02M:%02S %u" 69 (defcustom time-stamp-format "%Y-%02m-%02d %02H:%02M:%02S %u"
77 "*Format of the string inserted by \\[time-stamp]. 70 "*Format of the string inserted by \\[time-stamp].
78 The value may be a string or a list. Lists are supported only for 71 The value may be a string or a list. Lists are supported only for
79 backward compatibility; see variable `time-stamp-old-format-warn'. 72 backward compatibility; see variable `time-stamp-old-format-warn'.
80 73
81 A string is used with `format-time-string'. 74 A string is used with `format-time-string'.
82 For example, to get the format used by the `date' command,
83 use \"%3a %3b %2d %H:%M:%S %Z %y\".
84
85 In addition to the features of `format-time-string', 75 In addition to the features of `format-time-string',
86 you can use the following %-constructs: 76 you can use the following %-constructs:
87 77
88 %f file name without directory 78 %f file name without directory
89 %F full file name 79 %F full file name
90 %h mail host name 80 %h mail host name
91 %s system name 81 %s system name
92 %u user's login name" 82 %u user's login name
83
84 For example, to get the format used by the `date' command,
85 use \"%3a %3b %2d %02H:%02M:%02S %Z %Y\"."
93 :type 'string 86 :type 'string
94 :group 'time-stamp) 87 :group 'time-stamp)
88
89
95 90
96 ;;; Do not change time-stamp-line-limit, time-stamp-start, or 91 ;;; Do not change time-stamp-line-limit, time-stamp-start, or
97 ;;; time-stamp-end in your .emacs or you will be incompatible 92 ;;; time-stamp-end in your .emacs or you will be incompatible
98 ;;; with other people's files! If you must change them, 93 ;;; with other people's files! If you must change them,
99 ;;; do so only in the local variables section of the file itself. 94 ;;; do so only in the local variables section of the file itself.
144 look like one of the following: 139 look like one of the following:
145 Time-stamp: <> 140 Time-stamp: <>
146 Time-stamp: \" \" 141 Time-stamp: \" \"
147 The time stamp is written between the brackets or quotes: 142 The time stamp is written between the brackets or quotes:
148 Time-stamp: <1996-07-18 10:20:51 gildea> 143 Time-stamp: <1996-07-18 10:20:51 gildea>
149 Only updates the time stamp if the variable `time-stamp-active' is non-nil. 144 The time stamp is updated only if the variable `time-stamp-active' is non-nil.
150 The format of the time stamp is set by the variable `time-stamp-format'. 145 The format of the time stamp is set by the variable `time-stamp-format'.
151 The variables `time-stamp-line-limit', `time-stamp-start', 146 The variables `time-stamp-line-limit', `time-stamp-start',
152 and `time-stamp-end' control finding the template." 147 and `time-stamp-end' control finding the template."
153 (interactive) 148 (interactive)
154 (let ((case-fold-search nil) 149 (let ((case-fold-search nil)
155 (start nil) 150 (start nil)
156 (end nil) 151 (end nil)
157 search-limit) 152 search-limit
158 (save-excursion 153 (line-limit time-stamp-line-limit))
159 (save-restriction 154 (cond ((not (integerp line-limit))
160 (widen) 155 (setq line-limit 8)
161 (cond ((> time-stamp-line-limit 0) 156 (message "time-stamp-line-limit is not a number")
162 (goto-char (setq start (point-min))) 157 (sit-for 1)))
163 (forward-line time-stamp-line-limit) 158 (save-excursion
164 (setq search-limit (point))) 159 (save-restriction
165 (t 160 (widen)
166 (goto-char (setq search-limit (point-max))) 161 (cond ((> line-limit 0)
167 (forward-line time-stamp-line-limit) 162 (goto-char (setq start (point-min)))
168 (setq start (point)))) 163 (forward-line line-limit)
164 (setq search-limit (point)))
165 (t
166 (goto-char (setq search-limit (point-max)))
167 (forward-line line-limit)
168 (setq start (point))))
169 (goto-char start)
170 (while (and (< (point) search-limit)
171 (not end)
172 (re-search-forward time-stamp-start search-limit 'move))
173 (setq start (point))
174 (end-of-line)
175 (let ((line-end (point)))
169 (goto-char start) 176 (goto-char start)
170 (while (and (< (point) search-limit) 177 (if (re-search-forward time-stamp-end line-end 'move)
171 (not end) 178 (setq end (match-beginning 0)))))))
172 (re-search-forward time-stamp-start search-limit 'move))
173 (setq start (point))
174 (end-of-line)
175 (let ((line-end (point)))
176 (goto-char start)
177 (if (re-search-forward time-stamp-end line-end 'move)
178 (setq end (match-beginning 0)))))))
179 (if end 179 (if end
180 (progn 180 (progn
181 ;; do all warnings outside save-excursion 181 ;; do all warnings outside save-excursion
182 (cond 182 (cond
183 ((not time-stamp-active) 183 ((not time-stamp-active)
224 (defun time-stamp-string-preprocess (format) 224 (defun time-stamp-string-preprocess (format)
225 "Process occurrences in FORMAT of %f, %F, %h, %s and %u. 225 "Process occurrences in FORMAT of %f, %F, %h, %s and %u.
226 These are replaced with the file name (nondirectory part), 226 These are replaced with the file name (nondirectory part),
227 full file name, host name for mail, system name, and user name. 227 full file name, host name for mail, system name, and user name.
228 Do not alter other %-combinations, and do detect %%." 228 Do not alter other %-combinations, and do detect %%."
229 (let ((result "") (pos 0) (case-fold-search nil) 229 (let ((result "") (pos 0) (case-fold-search nil))
230 (file (or buffer-file-name "(no file)")))
231 (while (string-match "%[%uhfFs]" format pos) 230 (while (string-match "%[%uhfFs]" format pos)
232 (setq result (concat result (substring format pos (match-beginning 0)))) 231 (setq result (concat result (substring format pos (match-beginning 0))))
233 (let ((char (aref format (1+ (match-beginning 0))))) 232 (let ((char (aref format (1+ (match-beginning 0)))))
234 (cond ((= char ?%) 233 (cond ((= char ?%)
235 (setq result (concat result "%%"))) 234 (setq result (concat result "%%")))
236 ((= char ?u) 235 ((= char ?u)
237 (setq result (concat result (user-login-name)))) 236 (setq result (concat result (user-login-name))))
238 ((= char ?f) 237 ((= char ?f)
239 (setq result (concat result (file-name-nondirectory file)))) 238 (setq result (concat result
240 ((= char ?f) 239 (if buffer-file-name
241 (setq result (concat result file))) 240 (file-name-nondirectory buffer-file-name)
241 time-stamp-no-file))))
242 ((= char ?F)
243 (setq result (concat result
244 (or buffer-file-name time-stamp-no-file))))
242 ((= char ?s) 245 ((= char ?s)
243 (setq result (concat result (system-name)))) 246 (setq result (concat result (system-name))))
244 ((= char ?h) 247 ((= char ?h)
245 (setq result (concat result (time-stamp-mail-host-name)))))) 248 (setq result (concat result (time-stamp-mail-host-name))))))
246 (setq pos (match-end 0))) 249 (setq pos (match-end 0)))
247 (concat result (substring format pos)))) 250 (concat result (substring format pos))))
248 251
249 (defun time-stamp-string () 252 (defun time-stamp-string ()
250 "Generate the new string to be inserted by \\[time-stamp]." 253 "Generate the new string to be inserted by \\[time-stamp]."
251 (if (stringp time-stamp-format) 254 (if (stringp time-stamp-format)
252 (format-time-string (time-stamp-string-preprocess time-stamp-format) 255 (format-time-string (time-stamp-string-preprocess time-stamp-format))
253 (current-time))
254 ;; handle version 1 compatibility 256 ;; handle version 1 compatibility
255 (cond ((or (eq time-stamp-old-format-warn 'error) 257 (cond ((or (eq time-stamp-old-format-warn 'error)
256 (and (eq time-stamp-old-format-warn 'ask) 258 (and (eq time-stamp-old-format-warn 'ask)
257 (not (y-or-n-p "Use non-string time-stamp-format? ")))) 259 (not (y-or-n-p "Use non-string time-stamp-format? "))))
258 (message "Warning: no time-stamp: time-stamp-format not a string") 260 (message "Warning: no time-stamp: time-stamp-format not a string")