| 1 | ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs |
| 2 | |
| 3 | ;; Copyright 1989, 1993, 1994, 1995, 1997 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Maintainer's Time-stamp: <1999-06-26 15:12:53 gildea> |
| 6 | ;; Maintainer: Stephen Gildea <gildea@alum.mit.edu> |
| 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 |
| 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. |
| 23 | |
| 24 | ;;; Commentary: |
| 25 | |
| 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. |
| 37 | |
| 38 | ;;; Code: |
| 39 | |
| 40 | (defgroup time-stamp nil |
| 41 | "Maintain last change time stamps in files edited by Emacs." |
| 42 | :group 'data |
| 43 | :group 'extensions) |
| 44 | |
| 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'. |
| 49 | |
| 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 %U user's full name |
| 72 | %h mail host name |
| 73 | |
| 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 in the field width zero-fills a number. |
| 77 | |
| 78 | For example, to get the format used by the `date' command, |
| 79 | use \"%3a %3b %2d %02H:%02M:%02S %Z %:y\". |
| 80 | |
| 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) |
| 88 | |
| 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) |
| 95 | |
| 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' disables time stamping and the |
| 99 | buffer contains a template that would otherwise have been updated." |
| 100 | :type 'boolean |
| 101 | :group 'time-stamp) |
| 102 | |
| 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) |
| 112 | |
| 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) |
| 118 | |
| 119 | |
| 120 | ;;; Do not change time-stamp-line-limit, time-stamp-start, |
| 121 | ;;; time-stamp-end, time-stamp-pattern, time-stamp-inserts-lines, |
| 122 | ;;; or time-stamp-count in your .emacs or you will be incompatible |
| 123 | ;;; with other people's files! If you must change them, do so only |
| 124 | ;;; in the local variables section of the file itself. |
| 125 | |
| 126 | |
| 127 | (defvar time-stamp-line-limit 8 ;Do not change! |
| 128 | "Lines of a file searched; positive counts from start, negative from end. |
| 129 | The patterns `time-stamp-start' and `time-stamp-end' must be found in |
| 130 | the first (last) `time-stamp-line-limit' lines of the file for the |
| 131 | file to be time-stamped by \\[time-stamp]. A value of 0 searches the |
| 132 | entire buffer (use with care). |
| 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 | |
| 139 | |
| 140 | (defvar time-stamp-start "Time-stamp:[ \t]+\\\\?[\"<]+" ;Do not change! |
| 141 | "Regexp after which the time stamp is written by \\[time-stamp]. |
| 142 | See also the variables `time-stamp-end' and `time-stamp-line-limit'. |
| 143 | |
| 144 | Do not change `time-stamp-line-limit', `time-stamp-start', or |
| 145 | `time-stamp-end' for yourself or you will be incompatible |
| 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 | |
| 150 | (defvar time-stamp-end "\\\\?[\">]" ;Do not change! |
| 151 | "Regexp marking the text after the time stamp. |
| 152 | \\[time-stamp] deletes the text between the first match of `time-stamp-start' |
| 153 | and the following match of `time-stamp-end', then writes the |
| 154 | time stamp specified by `time-stamp-format' between them. |
| 155 | |
| 156 | The end text normally starts on the same line as the start text ends, |
| 157 | but if there are any newlines in `time-stamp-format', the same number |
| 158 | of newlines must separate the start and end. \\[time-stamp] tries |
| 159 | to not change the number of lines in the buffer. `time-stamp-inserts-lines' |
| 160 | controls this behavior. |
| 161 | |
| 162 | Do not change `time-stamp-line-limit', `time-stamp-start', `time-stamp-end', |
| 163 | or `time-stamp-inserts-lines' for yourself or you will be incompatible |
| 164 | with other people's files! If you must change them for some application, |
| 165 | do so in the local variables section of the time-stamped file itself.") |
| 166 | |
| 167 | |
| 168 | (defvar time-stamp-inserts-lines nil ;Do not change! |
| 169 | "Whether time-stamp can change the number of lines in a file. |
| 170 | If nil, \\[time-stamp] skips as many lines as there are newlines in |
| 171 | `time-stamp-format' before looking for the `time-stamp-end' pattern, |
| 172 | thus it tries not to change the number of lines in the buffer. |
| 173 | If non-nil, \\[time-stamp] starts looking for the end pattern |
| 174 | immediately after the start pattern. This behavior can cause |
| 175 | unexpected changes in the buffer if used carelessly, but it is useful |
| 176 | for generating repeated time stamps. |
| 177 | |
| 178 | Do not change `time-stamp-end' or `time-stamp-inserts-lines' for |
| 179 | yourself or you will be incompatible with other people's files! |
| 180 | If you must change them for some application, do so in the local |
| 181 | variables section of the time-stamped file itself.") |
| 182 | |
| 183 | |
| 184 | (defvar time-stamp-count 1 ;Do not change! |
| 185 | "How many time stamp templates \\[time-stamp] will look for in a buffer. |
| 186 | The same time-stamp will be written in each case. |
| 187 | |
| 188 | Do not change `time-stamp-count' for yourself or you will be |
| 189 | incompatible with other people's files! If you must change it for |
| 190 | some application, do so in the local variables section of the |
| 191 | time-stamped file itself.") |
| 192 | |
| 193 | |
| 194 | (defvar time-stamp-pattern "%%" ;Do not change! |
| 195 | "Convenience variable setting all `time-stamp' location and format values. |
| 196 | This string has four parts, each of which is optional. |
| 197 | These four parts set `time-stamp-line-limit', `time-stamp-start', |
| 198 | `time-stamp-format', and `time-stamp-end'. See the documentation |
| 199 | for each of these variables for details. |
| 200 | |
| 201 | The first part is a number followed by a slash; the number sets the number |
| 202 | of lines at the beginning (negative counts from end) of the file searched |
| 203 | for the time-stamp. The number and the slash may be omitted to use the |
| 204 | normal value. |
| 205 | |
| 206 | The second part is a regexp identifying the pattern preceding the time stamp. |
| 207 | This part may be omitted to use the normal pattern. |
| 208 | |
| 209 | The third part specifies the format of the time-stamp inserted. See |
| 210 | the documentation for `time-stamp-format' for details. Specify this |
| 211 | part as \"%%\" to use the normal format. |
| 212 | |
| 213 | The fourth part is a regexp identifying the pattern following the time stamp. |
| 214 | This part may be omitted to use the normal pattern. |
| 215 | |
| 216 | As an example, the default behavior can be specified something like this: |
| 217 | \"8/Time-stamp: [\\\"<]%:y-%02m-%02d %02H:%02M:%02S %u[\\\">]\" |
| 218 | |
| 219 | Do not change `time-stamp-pattern' for yourself or you will be incompatible |
| 220 | with other people's files! Set it only in the local variables section |
| 221 | of the time-stamped file itself.") |
| 222 | |
| 223 | |
| 224 | |
| 225 | ;;;###autoload |
| 226 | (defun time-stamp () |
| 227 | "Update the time stamp string(s) in the buffer. |
| 228 | A template in a file can be automatically updated with a new time stamp |
| 229 | every time you save the file. Add this line to your .emacs file: |
| 230 | (add-hook 'write-file-hooks 'time-stamp) |
| 231 | Normally the template must appear in the first 8 lines of a file and |
| 232 | look like one of the following: |
| 233 | Time-stamp: <> |
| 234 | Time-stamp: \" \" |
| 235 | The time stamp is written between the brackets or quotes: |
| 236 | Time-stamp: <1998-02-18 10:20:51 gildea> |
| 237 | The time stamp is updated only if the variable `time-stamp-active' is non-nil. |
| 238 | The format of the time stamp is set by the variable `time-stamp-format'. |
| 239 | The variables `time-stamp-line-limit', `time-stamp-start', `time-stamp-end', |
| 240 | `time-stamp-count', and `time-stamp-inserts-lines' control finding the |
| 241 | template." |
| 242 | (interactive) |
| 243 | (let ((line-limit time-stamp-line-limit) |
| 244 | (ts-start time-stamp-start) |
| 245 | (ts-format time-stamp-format) |
| 246 | (ts-end time-stamp-end) |
| 247 | (ts-count time-stamp-count) |
| 248 | (format-lines 0) |
| 249 | (end-lines 1) |
| 250 | (start nil) |
| 251 | search-limit) |
| 252 | (if (stringp time-stamp-pattern) |
| 253 | (progn |
| 254 | (string-match "\\`\\(\\(-?[0-9]+\\)/\\)?\\([^%]+\\)?\\(\\(.\\|\n\\)*%[-.,:@+_ #^()0-9]*[A-Za-z%]\\)?\\([^%]+\\)?\\'" time-stamp-pattern) |
| 255 | (and (match-beginning 2) |
| 256 | (setq line-limit |
| 257 | (string-to-int (match-string 2 time-stamp-pattern)))) |
| 258 | (and (match-beginning 3) |
| 259 | (setq ts-start (match-string 3 time-stamp-pattern))) |
| 260 | (and (match-beginning 4) |
| 261 | (not (string-equal (match-string 4 time-stamp-pattern) "%%")) |
| 262 | (setq ts-format (match-string 4 time-stamp-pattern))) |
| 263 | (and (match-beginning 6) |
| 264 | (setq ts-end (match-string 6 time-stamp-pattern))))) |
| 265 | (cond ((not (integerp line-limit)) |
| 266 | (setq line-limit 8) |
| 267 | (message "time-stamp-line-limit is not an integer") |
| 268 | (sit-for 1))) |
| 269 | (cond ((not (integerp ts-count)) |
| 270 | (setq ts-count 1) |
| 271 | (message "time-stamp-count is not an integer") |
| 272 | (sit-for 1)) |
| 273 | ((< ts-count 1) |
| 274 | ;; We need to call time-stamp-once at least once |
| 275 | ;; to output any warnings about time-stamp not being active. |
| 276 | (setq ts-count 1))) |
| 277 | ;; Figure out what lines the end should be on. |
| 278 | (let ((nl-start 0)) |
| 279 | (while (string-match "\n" ts-format nl-start) |
| 280 | (setq format-lines (1+ format-lines) nl-start (match-end 0)))) |
| 281 | (let ((nl-start 0)) |
| 282 | (while (string-match "\n" ts-end nl-start) |
| 283 | (setq end-lines (1+ end-lines) nl-start (match-end 0)))) |
| 284 | ;; Find overall what lines to look at |
| 285 | (save-excursion |
| 286 | (save-restriction |
| 287 | (widen) |
| 288 | (cond ((> line-limit 0) |
| 289 | (goto-char (setq start (point-min))) |
| 290 | (forward-line line-limit) |
| 291 | (setq search-limit (point))) |
| 292 | ((< line-limit 0) |
| 293 | (goto-char (setq search-limit (point-max))) |
| 294 | (forward-line line-limit) |
| 295 | (setq start (point))) |
| 296 | (t ;0 => no limit (use with care!) |
| 297 | (setq start (point-min)) |
| 298 | (setq search-limit (point-max)))))) |
| 299 | (while (and start |
| 300 | (< start search-limit) |
| 301 | (> ts-count 0)) |
| 302 | (setq start (time-stamp-once start search-limit ts-start ts-end |
| 303 | ts-format format-lines end-lines)) |
| 304 | (setq ts-count (1- ts-count)))) |
| 305 | ;; be sure to return nil so can be used on write-file-hooks |
| 306 | nil) |
| 307 | |
| 308 | (defun time-stamp-once (start search-limit ts-start ts-end |
| 309 | ts-format format-lines end-lines) |
| 310 | "Update one time-stamp. Internal routine called by \\[time-stamp]. |
| 311 | Returns the end point, which is where `time-stamp' begins the next search." |
| 312 | (let ((case-fold-search nil) |
| 313 | (end nil) |
| 314 | end-search-start |
| 315 | (end-length nil)) |
| 316 | (save-excursion |
| 317 | (save-restriction |
| 318 | (widen) |
| 319 | ;; Find the location of the time stamp. |
| 320 | (while (and (< (goto-char start) search-limit) |
| 321 | (not end) |
| 322 | (re-search-forward ts-start search-limit 'move)) |
| 323 | (setq start (point)) |
| 324 | (if (not time-stamp-inserts-lines) |
| 325 | (forward-line format-lines)) |
| 326 | (setq end-search-start (max start (point))) |
| 327 | (if (= (forward-line end-lines) 0) |
| 328 | (progn |
| 329 | (and (bolp) (backward-char)) |
| 330 | (let ((line-end (min (point) search-limit))) |
| 331 | (if (>= line-end end-search-start) |
| 332 | (progn |
| 333 | (goto-char end-search-start) |
| 334 | (if (re-search-forward ts-end line-end t) |
| 335 | (progn |
| 336 | (setq end (match-beginning 0)) |
| 337 | (setq end-length (- (match-end 0) end)))))))))))) |
| 338 | (if end |
| 339 | (progn |
| 340 | ;; do all warnings outside save-excursion |
| 341 | (cond |
| 342 | ((not time-stamp-active) |
| 343 | (if time-stamp-warn-inactive |
| 344 | ;; don't signal an error in a write-file-hook |
| 345 | (progn |
| 346 | (message "Warning: time-stamp-active is off; did not time-stamp buffer.") |
| 347 | (sit-for 1)))) |
| 348 | ((not (and (stringp ts-start) |
| 349 | (stringp ts-end))) |
| 350 | (message "time-stamp-start or time-stamp-end is not a string") |
| 351 | (sit-for 1)) |
| 352 | (t |
| 353 | (let ((new-time-stamp (time-stamp-string ts-format))) |
| 354 | (if (and (stringp new-time-stamp) |
| 355 | (not (string-equal (buffer-substring start end) |
| 356 | new-time-stamp))) |
| 357 | (save-excursion |
| 358 | (save-restriction |
| 359 | (widen) |
| 360 | (delete-region start end) |
| 361 | (goto-char start) |
| 362 | (insert-and-inherit new-time-stamp) |
| 363 | (setq end (point)) |
| 364 | ;; remove any tabs used to format time stamp |
| 365 | (if (search-backward "\t" start t) |
| 366 | (progn |
| 367 | (untabify start end) |
| 368 | (setq end (point)))))))))))) |
| 369 | ;; return the location after this time stamp, if there was one |
| 370 | (and end end-length |
| 371 | (+ end end-length)))) |
| 372 | |
| 373 | |
| 374 | ;;;###autoload |
| 375 | (defun time-stamp-toggle-active (&optional arg) |
| 376 | "Toggle `time-stamp-active', setting whether \\[time-stamp] updates a buffer. |
| 377 | With arg, turn time stamping on if and only if arg is positive." |
| 378 | (interactive "P") |
| 379 | (setq time-stamp-active |
| 380 | (if (null arg) |
| 381 | (not time-stamp-active) |
| 382 | (> (prefix-numeric-value arg) 0))) |
| 383 | (message "time-stamp is now %s." (if time-stamp-active "active" "off"))) |
| 384 | |
| 385 | |
| 386 | (defun time-stamp-string (&optional ts-format) |
| 387 | "Generate the new string to be inserted by \\[time-stamp]. |
| 388 | Optionally use FORMAT." |
| 389 | (or ts-format |
| 390 | (setq ts-format time-stamp-format)) |
| 391 | (if (stringp ts-format) |
| 392 | (if (stringp time-stamp-time-zone) |
| 393 | (let ((ts-real-time-zone (getenv "TZ"))) |
| 394 | (unwind-protect |
| 395 | (progn |
| 396 | (setenv "TZ" time-stamp-time-zone) |
| 397 | (format-time-string |
| 398 | (time-stamp-string-preprocess ts-format))) |
| 399 | (setenv "TZ" ts-real-time-zone))) |
| 400 | (format-time-string |
| 401 | (time-stamp-string-preprocess ts-format))) |
| 402 | ;; handle version 1 compatibility |
| 403 | (cond ((or (eq time-stamp-old-format-warn 'error) |
| 404 | (and (eq time-stamp-old-format-warn 'ask) |
| 405 | (not (y-or-n-p "Use non-string time-stamp-format? ")))) |
| 406 | (message "Warning: no time-stamp: time-stamp-format not a string") |
| 407 | (sit-for 1) |
| 408 | nil) |
| 409 | (t |
| 410 | (cond ((eq time-stamp-old-format-warn 'warn) |
| 411 | (message "Obsolescent time-stamp-format type; should be string") |
| 412 | (sit-for 1))) |
| 413 | (time-stamp-fconcat ts-format " "))))) |
| 414 | |
| 415 | (defconst time-stamp-no-file "(no file)" |
| 416 | "String to use when the buffer is not associated with a file.") |
| 417 | |
| 418 | ;;; time-stamp is transitioning to using the new, expanded capabilities |
| 419 | ;;; of format-time-string. During the process, this function implements |
| 420 | ;;; intermediate, compatible formats and complains about old, soon to |
| 421 | ;;; be unsupported, formats. This function will get a lot (a LOT) shorter |
| 422 | ;;; when the transition is complete and we can just pass most things |
| 423 | ;;; straight through to format-time-string. |
| 424 | ;;; At all times, all the formats recommended in the doc string |
| 425 | ;;; of time-stamp-format will work not only in the current version of |
| 426 | ;;; Emacs, but in all versions that have been released within the past |
| 427 | ;;; two years. |
| 428 | ;;; The : modifier is a temporary conversion feature used to resolve |
| 429 | ;;; ambiguous formats--formats that are changing (over time) incompatibly. |
| 430 | (defun time-stamp-string-preprocess (format &optional time) |
| 431 | ;; Use a FORMAT to format date, time, file, and user information. |
| 432 | ;; Optional second argument TIME is only for testing. |
| 433 | ;; Implements non-time extensions to format-time-string |
| 434 | ;; and all time-stamp-format compatibility. |
| 435 | (let ((fmt-len (length format)) |
| 436 | (ind 0) |
| 437 | cur-char |
| 438 | (prev-char nil) |
| 439 | (result "") |
| 440 | field-width |
| 441 | field-result |
| 442 | alt-form change-case require-padding |
| 443 | (paren-level 0)) |
| 444 | (while (< ind fmt-len) |
| 445 | (setq cur-char (aref format ind)) |
| 446 | (setq |
| 447 | result |
| 448 | (concat result |
| 449 | (cond |
| 450 | ((eq cur-char ?%) |
| 451 | ;; eat any additional args to allow for future expansion |
| 452 | (setq alt-form nil change-case nil require-padding nil field-width "") |
| 453 | (while (progn |
| 454 | (setq ind (1+ ind)) |
| 455 | (setq cur-char (if (< ind fmt-len) |
| 456 | (aref format ind) |
| 457 | ?\0)) |
| 458 | (or (eq ?. cur-char) |
| 459 | (eq ?, cur-char) (eq ?: cur-char) (eq ?@ cur-char) |
| 460 | (eq ?- cur-char) (eq ?+ cur-char) (eq ?_ cur-char) |
| 461 | (eq ?\ cur-char) (eq ?# cur-char) (eq ?^ cur-char) |
| 462 | (and (eq ?\( cur-char) |
| 463 | (not (eq prev-char ?\\)) |
| 464 | (setq paren-level (1+ paren-level))) |
| 465 | (if (and (eq ?\) cur-char) |
| 466 | (not (eq prev-char ?\\)) |
| 467 | (> paren-level 0)) |
| 468 | (setq paren-level (1- paren-level)) |
| 469 | (and (> paren-level 0) |
| 470 | (< ind fmt-len))) |
| 471 | (if (and (<= ?0 cur-char) (>= ?9 cur-char)) |
| 472 | ;; get format width |
| 473 | (let ((field-index ind)) |
| 474 | (while (progn |
| 475 | (setq ind (1+ ind)) |
| 476 | (setq cur-char (if (< ind fmt-len) |
| 477 | (aref format ind) |
| 478 | ?\0)) |
| 479 | (and (<= ?0 cur-char) (>= ?9 cur-char)))) |
| 480 | (setq field-width (substring format field-index ind)) |
| 481 | (setq ind (1- ind)) |
| 482 | t)))) |
| 483 | (setq prev-char cur-char) |
| 484 | ;; some characters we actually use |
| 485 | (cond ((eq cur-char ?:) |
| 486 | (setq alt-form t)) |
| 487 | ((eq cur-char ?#) |
| 488 | (setq change-case t)))) |
| 489 | (setq field-result |
| 490 | (cond |
| 491 | ((eq cur-char ?%) |
| 492 | "%%") |
| 493 | ((eq cur-char ?a) ;day of week |
| 494 | (if change-case |
| 495 | (format-time-string "%#A" time) |
| 496 | (or alt-form (not (string-equal field-width "")) |
| 497 | (time-stamp-conv-warn "%a" "%:a")) |
| 498 | (if (and alt-form (not (string-equal field-width ""))) |
| 499 | "" ;discourage "%:3a" |
| 500 | (format-time-string "%A" time)))) |
| 501 | ((eq cur-char ?A) |
| 502 | (if alt-form |
| 503 | (format-time-string "%A" time) |
| 504 | (or change-case (not (string-equal field-width "")) |
| 505 | (time-stamp-conv-warn "%A" "%#A")) |
| 506 | (format-time-string "%#A" time))) |
| 507 | ((eq cur-char ?b) ;month name |
| 508 | (if change-case |
| 509 | (format-time-string "%#B" time) |
| 510 | (or alt-form (not (string-equal field-width "")) |
| 511 | (time-stamp-conv-warn "%b" "%:b")) |
| 512 | (if (and alt-form (not (string-equal field-width ""))) |
| 513 | "" ;discourage "%:3b" |
| 514 | (format-time-string "%B" time)))) |
| 515 | ((eq cur-char ?B) |
| 516 | (if alt-form |
| 517 | (format-time-string "%B" time) |
| 518 | (or change-case (not (string-equal field-width "")) |
| 519 | (time-stamp-conv-warn "%B" "%#B")) |
| 520 | (format-time-string "%#B" time))) |
| 521 | ((eq cur-char ?d) ;day of month, 1-31 |
| 522 | (time-stamp-do-number cur-char alt-form field-width time)) |
| 523 | ((eq cur-char ?H) ;hour, 0-23 |
| 524 | (time-stamp-do-number cur-char alt-form field-width time)) |
| 525 | ((eq cur-char ?I) ;hour, 1-12 |
| 526 | (time-stamp-do-number cur-char alt-form field-width time)) |
| 527 | ((eq cur-char ?m) ;month number, 1-12 |
| 528 | (time-stamp-do-number cur-char alt-form field-width time)) |
| 529 | ((eq cur-char ?M) ;minute, 0-59 |
| 530 | (time-stamp-do-number cur-char alt-form field-width time)) |
| 531 | ((eq cur-char ?p) ;am or pm |
| 532 | (or change-case |
| 533 | (time-stamp-conv-warn "%p" "%#p")) |
| 534 | (format-time-string "%#p" time)) |
| 535 | ((eq cur-char ?P) ;AM or PM |
| 536 | (format-time-string "%p" time)) |
| 537 | ((eq cur-char ?S) ;seconds, 00-60 |
| 538 | (time-stamp-do-number cur-char alt-form field-width time)) |
| 539 | ((eq cur-char ?w) ;weekday number, Sunday is 0 |
| 540 | (format-time-string "%w" time)) |
| 541 | ((eq cur-char ?y) ;year |
| 542 | (or alt-form (not (string-equal field-width "")) |
| 543 | (time-stamp-conv-warn "%y" "%:y")) |
| 544 | (string-to-int (format-time-string "%Y" time))) |
| 545 | ((eq cur-char ?Y) ;4-digit year, new style |
| 546 | (string-to-int (format-time-string "%Y" time))) |
| 547 | ((eq cur-char ?z) ;time zone lower case |
| 548 | (if change-case |
| 549 | "" ;discourage %z variations |
| 550 | (format-time-string "%#Z" time))) |
| 551 | ((eq cur-char ?Z) |
| 552 | (if change-case |
| 553 | (format-time-string "%#Z" time) |
| 554 | (format-time-string "%Z" time))) |
| 555 | ((eq cur-char ?f) ;buffer-file-name, base name only |
| 556 | (if buffer-file-name |
| 557 | (file-name-nondirectory buffer-file-name) |
| 558 | time-stamp-no-file)) |
| 559 | ((eq cur-char ?F) ;buffer-file-name, full path |
| 560 | (or buffer-file-name |
| 561 | time-stamp-no-file)) |
| 562 | ((eq cur-char ?s) ;system name |
| 563 | (system-name)) |
| 564 | ((eq cur-char ?u) ;user name |
| 565 | (user-login-name)) |
| 566 | ((eq cur-char ?U) ;user full name |
| 567 | (user-full-name)) |
| 568 | ((eq cur-char ?l) ;logname (undocumented user name alt) |
| 569 | (user-login-name)) |
| 570 | ((eq cur-char ?L) ;(undocumented alt user full name) |
| 571 | (user-full-name)) |
| 572 | ((eq cur-char ?h) ;mail host name |
| 573 | (time-stamp-mail-host-name)) |
| 574 | )) |
| 575 | (if (string-equal field-width "") |
| 576 | (format "%s" field-result) |
| 577 | (let ((padded-result |
| 578 | (format (format "%%%s%c" |
| 579 | field-width |
| 580 | (if (numberp field-result) ?d ?s)) |
| 581 | (or field-result "")))) |
| 582 | (let ((initial-length (length padded-result)) |
| 583 | (desired-length (string-to-int field-width))) |
| 584 | (if (> initial-length desired-length) |
| 585 | ;; truncate strings on right, years on left |
| 586 | (if (stringp field-result) |
| 587 | (substring padded-result 0 desired-length) |
| 588 | (if (eq cur-char ?y) |
| 589 | (substring padded-result (- desired-length)) |
| 590 | padded-result)) ;non-year numbers don't truncate |
| 591 | padded-result))))) |
| 592 | (t |
| 593 | (char-to-string cur-char))))) |
| 594 | (setq ind (1+ ind))) |
| 595 | result)) |
| 596 | |
| 597 | (defun time-stamp-do-number (format-char alt-form field-width time) |
| 598 | ;; Handle a compatible FORMAT-CHAR where only |
| 599 | ;; the default width/padding will change. |
| 600 | ;; ALT-FORM is whether `#' specified. FIELD-WIDTH is the string |
| 601 | ;; width specification or "". TIME is the time to convert. |
| 602 | (let ((format-string (concat "%" (char-to-string format-char)))) |
| 603 | (and (not alt-form) (string-equal field-width "") |
| 604 | (time-stamp-conv-warn format-string |
| 605 | (format "%%:%c" format-char))) |
| 606 | (if (and alt-form (not (string-equal field-width ""))) |
| 607 | "" ;discourage "%:2d" and the like |
| 608 | (string-to-int (format-time-string format-string time))))) |
| 609 | |
| 610 | (defvar time-stamp-conversion-warn t |
| 611 | "Non-nil to warn about soon-to-be-unsupported forms in `time-stamp-format'. |
| 612 | In would be a bad idea to disable these warnings! |
| 613 | You really need to update your files instead. |
| 614 | |
| 615 | The new formats will work with old versions of Emacs. |
| 616 | New formats are being recommended now to allow `time-stamp-format' |
| 617 | to change in the future to be compatible with `format-time-string'. |
| 618 | The new forms being recommended now will continue to work then.") |
| 619 | |
| 620 | |
| 621 | (defun time-stamp-conv-warn (old-form new-form) |
| 622 | ;; Display a warning about a soon-to-be-obsolete format. |
| 623 | (cond |
| 624 | (time-stamp-conversion-warn |
| 625 | (save-excursion |
| 626 | (set-buffer (get-buffer-create "*Time-stamp-compatibility*")) |
| 627 | (goto-char (point-max)) |
| 628 | (if (bobp) |
| 629 | (progn |
| 630 | (insert |
| 631 | "The formats recognized in time-stamp-format will change in a future release\n" |
| 632 | "to be compatible with the new, expanded format-time-string function.\n\n" |
| 633 | "The following obsolescent time-stamp-format construct(s) were found:\n\n"))) |
| 634 | (insert "\"" old-form "\" -- use " new-form "\n")) |
| 635 | (display-buffer "*Time-stamp-compatibility*")))) |
| 636 | |
| 637 | |
| 638 | |
| 639 | (defun time-stamp-mail-host-name () |
| 640 | "Return the name of the host where the user receives mail. |
| 641 | This is the value of `mail-host-address' if bound and a string, |
| 642 | otherwise the value of the function `system-name'." |
| 643 | (or (and (boundp 'mail-host-address) |
| 644 | (stringp mail-host-address) |
| 645 | mail-host-address) |
| 646 | (system-name))) |
| 647 | |
| 648 | ;;; the rest of this file is for version 1 compatibility |
| 649 | |
| 650 | (defun time-stamp-fconcat (list sep) |
| 651 | "Similar to (mapconcat 'funcall LIST SEP) but LIST allows literals. |
| 652 | If an element of LIST is a symbol, it is funcalled to get the string to use; |
| 653 | the separator SEP is used between two strings obtained by funcalling a |
| 654 | symbol. Otherwise the element itself is inserted; no separator is used |
| 655 | around literals." |
| 656 | (let ((return-string "") |
| 657 | (insert-sep-p nil)) |
| 658 | (while list |
| 659 | (cond ((symbolp (car list)) |
| 660 | (if insert-sep-p |
| 661 | (setq return-string (concat return-string sep))) |
| 662 | (setq return-string (concat return-string (funcall (car list)))) |
| 663 | (setq insert-sep-p t)) |
| 664 | (t |
| 665 | (setq return-string (concat return-string (car list))) |
| 666 | (setq insert-sep-p nil))) |
| 667 | (setq list (cdr list))) |
| 668 | return-string)) |
| 669 | |
| 670 | ;;; Some functions used in time-stamp-format |
| 671 | |
| 672 | ;;; Could generate most of a message-id with |
| 673 | ;;; '(time-stamp-yymmdd "" time-stamp-hhmm "@" time-stamp-mail-host-name) |
| 674 | |
| 675 | ;;; pretty form, suitable for a title page |
| 676 | |
| 677 | (defun time-stamp-month-dd-yyyy () |
| 678 | "Return the current date as a string in \"Month DD, YYYY\" form." |
| 679 | (format-time-string "%B %e, %Y")) |
| 680 | |
| 681 | (defun time-stamp-dd/mm/yyyy () |
| 682 | "Return the current date as a string in \"DD/MM/YYYY\" form." |
| 683 | (format-time-string "%d/%m/%Y")) |
| 684 | |
| 685 | ;;; same as __DATE__ in ANSI C |
| 686 | |
| 687 | (defun time-stamp-mon-dd-yyyy () |
| 688 | "Return the current date as a string in \"Mon DD YYYY\" form. |
| 689 | The first character of DD is space if the value is less than 10." |
| 690 | (format-time-string "%b %d %Y")) |
| 691 | |
| 692 | ;;; RFC 822 date |
| 693 | |
| 694 | (defun time-stamp-dd-mon-yy () |
| 695 | "Return the current date as a string in \"DD Mon YY\" form." |
| 696 | (format-time-string "%d %b %y")) |
| 697 | |
| 698 | ;;; RCS 3 date |
| 699 | |
| 700 | (defun time-stamp-yy/mm/dd () |
| 701 | "Return the current date as a string in \"YY/MM/DD\" form." |
| 702 | (format-time-string "%y/%m/%d")) |
| 703 | |
| 704 | ;;; RCS 5 date |
| 705 | |
| 706 | (defun time-stamp-yyyy/mm/dd () |
| 707 | "Return the current date as a string in \"YYYY/MM/DD\" form." |
| 708 | (format-time-string "%Y/%m/%d")) |
| 709 | |
| 710 | ;;; ISO 8601 date |
| 711 | |
| 712 | (defun time-stamp-yyyy-mm-dd () |
| 713 | "Return the current date as a string in \"YYYY-MM-DD\" form." |
| 714 | (format-time-string "%Y-%m-%d")) |
| 715 | |
| 716 | (defun time-stamp-yymmdd () |
| 717 | "Return the current date as a string in \"YYMMDD\" form." |
| 718 | (format-time-string "%y%m%d")) |
| 719 | |
| 720 | (defun time-stamp-hh:mm:ss () |
| 721 | "Return the current time as a string in \"HH:MM:SS\" form." |
| 722 | (format-time-string "%T")) |
| 723 | |
| 724 | (defun time-stamp-hhmm () |
| 725 | "Return the current time as a string in \"HHMM\" form." |
| 726 | (format-time-string "%H%M")) |
| 727 | |
| 728 | (provide 'time-stamp) |
| 729 | |
| 730 | ;;; time-stamp.el ends here |