lisp/cedet/semantic/db-typecache.el (semanticdb-typecache)
[bpt/emacs.git] / lisp / time.el
CommitLineData
b7eb6e8a 1;;; time.el --- display time, load and mail indicator in mode line of Emacs -*-coding: utf-8 -*-
c2acf685 2
0d30b337 3;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1996, 2000, 2001, 2002,
ae940284 4;; 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
c2acf685
SM
5
6;; Maintainer: FSF
7
8;; This file is part of GNU Emacs.
9
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
c2acf685 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
c2acf685
SM
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c2acf685
SM
22
23;;; Commentary:
24
25;; Facilities to display current time/date and a new-mail indicator
aff2ba04
RS
26;; in the Emacs mode line. The entry point is `display-time'.
27
28;; Display time world in a buffer, the entry point is
29;; `display-time-world'.
c2acf685
SM
30
31;;; Code:
32
33(defgroup display-time nil
34 "Display time and load in mode line of Emacs."
97904e51 35 :group 'mode-line
c2acf685
SM
36 :group 'mail)
37
38
c2acf685 39(defcustom display-time-mail-file nil
9201cc28 40 "File name of mail inbox file, for indicating existence of new mail.
cbb7527f 41Non-nil and not a string means don't check for mail; nil means use
c2acf685 42default, which is system-dependent, and is the same as used by Rmail."
fe6456af 43 :type '(choice (const :tag "None" none)
c2acf685
SM
44 (const :tag "Default" nil)
45 (file :format "%v"))
46 :group 'display-time)
47
cc351ed4 48(defcustom display-time-mail-directory nil
9201cc28 49 "Name of mail inbox directory, for indicating existence of new mail.
ad68bb68
RS
50Any nonempty regular file in the directory is regarded as newly arrived mail.
51If nil, do not check a directory for arriving mail."
cc351ed4
PJ
52 :type '(choice (const :tag "None" nil)
53 (directory :format "%v"))
54 :group 'display-time)
55
c2acf685 56(defcustom display-time-mail-function nil
9201cc28 57 "Function to call, for indicating existence of new mail.
ad68bb68
RS
58If nil, that means use the default method: check that the file
59specified by `display-time-mail-file' is nonempty or that the
60directory `display-time-mail-directory' contains nonempty files."
c2acf685
SM
61 :type '(choice (const :tag "Default" nil)
62 (function))
63 :group 'display-time)
64
4b05e68d 65(defcustom display-time-default-load-average 0
9201cc28 66 "Which load average value will be shown in the mode line.
4b05e68d 67Almost every system can provide values of load for past 1 minute, past 5 or
f2f56691 68past 15 minutes. The default is to display 1 minute load average.
3deeb46b
GM
69The value can be one of:
70
71 0 => 1 minute load
72 1 => 5 minutes load
73 2 => 15 minutes load"
4b05e68d
PJ
74 :type '(choice (const :tag "1 minute load" 0)
75 (const :tag "5 minutes load" 1)
fe6456af
RS
76 (const :tag "15 minutes load" 2)
77 (const :tag "None" nil))
4b05e68d
PJ
78 :group 'display-time)
79
fe6456af 80(defvar display-time-load-average nil
b7eb6e8a 81 "Load average currently being shown in mode line.")
4b05e68d
PJ
82
83(defcustom display-time-load-average-threshold 0.1
9201cc28 84 "Load-average values below this value won't be shown in the mode line."
b7b4e4d4
PJ
85 :type 'number
86 :group 'display-time)
4b05e68d 87
c2acf685
SM
88;;;###autoload
89(defcustom display-time-day-and-date nil "\
90*Non-nil means \\[display-time] should display day and date as well as time."
91 :type 'boolean
92 :group 'display-time)
93
94(defvar display-time-timer nil)
95
96(defcustom display-time-interval 60
9201cc28 97 "Seconds between updates of time in the mode line."
c2acf685
SM
98 :type 'integer
99 :group 'display-time)
100
101(defcustom display-time-24hr-format nil
9201cc28 102 "Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
7d326782 103A value of nil means 1 <= hh <= 12, and an AM/PM suffix is used."
c2acf685
SM
104 :type 'boolean
105 :group 'display-time)
106
107(defvar display-time-string nil)
108
109(defcustom display-time-hook nil
9201cc28 110 "List of functions to be called when the time is updated on the mode line."
c2acf685
SM
111 :type 'hook
112 :group 'display-time)
113
114(defvar display-time-server-down-time nil
115 "Time when mail file's file system was recorded to be down.
116If that file system seems to be up, the value is nil.")
117
dca2168c 118(defcustom zoneinfo-style-world-list
aff2ba04
RS
119 '(("America/Los_Angeles" "Seattle")
120 ("America/New_York" "New York")
121 ("Europe/London" "London")
122 ("Europe/Paris" "Paris")
123 ("Asia/Calcutta" "Bangalore")
124 ("Asia/Tokyo" "Tokyo"))
dca2168c
EZ
125 "Alist of zoneinfo-style time zones and places for `display-time-world'.
126Each element has the form (TIMEZONE LABEL).
127TIMEZONE should be a string of the form AREA/LOCATION, where AREA is
128the name of a region -- a continent or ocean, and LOCATION is the name
129of a specific location, e.g., a city, within that region.
130LABEL is a string to display as the label of that TIMEZONE's time."
131 :group 'display-time
132 :type '(repeat (list string string))
133 :version "23.1")
134
135(defcustom legacy-style-world-list
136 '(("PST8PDT" "Seattle")
137 ("EST5EDT" "New York")
9db0c29b 138 ("GMT0BST" "London")
dca2168c 139 ("CET-1CDT" "Paris")
9db0c29b
EZ
140 ("IST-5:30" "Bangalore")
141 ("JST-9" "Tokyo"))
dca2168c
EZ
142 "Alist of traditional-style time zones and places for `display-time-world'.
143Each element has the form (TIMEZONE LABEL).
144TIMEZONE should be a string of the form:
145
146 std[+|-]offset[dst[offset][,date[/time],date[/time]]]
147
148See the documentation of the TZ environment variable on your system,
149for more details about the format of TIMEZONE.
150LABEL is a string to display as the label of that TIMEZONE's time."
151 :group 'display-time
152 :type '(repeat (list string string))
153 :version "23.1")
154
155(defcustom display-time-world-list
36d4b145
JR
156 ;; Determine if zoneinfo style timezones are supported by testing that
157 ;; America/New York and Europe/London return different timezones.
158 (let (gmt nyt)
159 (set-time-zone-rule "America/New York")
160 (setq nyt (format-time-string "%z"))
161 (set-time-zone-rule "Europe/London")
162 (setq gmt (format-time-string "%z"))
163 (set-time-zone-rule nil)
164 (if (string-equal nyt gmt)
165 legacy-style-world-list
166 zoneinfo-style-world-list))
dca2168c 167 "Alist of time zones and places for `display-time-world' to display.
aff2ba04 168Each element has the form (TIMEZONE LABEL).
dca2168c
EZ
169TIMEZONE should be in the format supported by `set-time-zone-rule' on
170your system. See the documentation of `zoneinfo-style-world-list' and
171\`legacy-style-world-list' for two widely used formats.
172LABEL is a string to display as the label of that TIMEZONE's time."
aff2ba04
RS
173 :group 'display-time
174 :type '(repeat (list string string))
175 :version "23.1")
176
658dcd47 177(defcustom display-time-world-time-format "%A %d %B %R %Z"
aff2ba04
RS
178 "Format of the time displayed, see `format-time-string'."
179 :group 'display-time
180 :type 'string
181 :version "23.1")
182
183(defcustom display-time-world-buffer-name "*wclock*"
184 "Name of the wclock buffer."
185 :group 'display-time
186 :type 'string
187 :version "23.1")
188
189(defcustom display-time-world-timer-enable t
190 "If non-nil, a timer will update the world clock."
191 :group 'display-time
192 :type 'boolean
193 :version "23.1")
194
195(defcustom display-time-world-timer-second 60
196 "Interval in seconds for updating the world clock."
197 :group 'display-time
198 :type 'integer
199 :version "23.1")
200
201(defvar display-time-world-mode-map
202 (let ((map (make-sparse-keymap)))
203 (define-key map "q" 'kill-this-buffer)
204 map)
205 "Keymap of Display Time World mode")
206
c2acf685
SM
207;;;###autoload
208(defun display-time ()
209 "Enable display of time, load level, and mail flag in mode lines.
210This display updates automatically every minute.
211If `display-time-day-and-date' is non-nil, the current day and date
212are displayed as well.
213This runs the normal hook `display-time-hook' after each update."
214 (interactive)
215 (display-time-mode 1))
216
b7eb6e8a
DL
217;; This business used to be simpler when all mode lines had the same
218;; face and the image could just be pbm. Now we try to rely on an xpm
219;; image with a transparent background. Otherwise, set the background
220;; for pbm.
221
222(defcustom display-time-mail-face nil
c2acf685 223 "Face to use for `display-time-mail-string'.
b7eb6e8a 224If `display-time-use-mail-icon' is non-nil, the image's
6bd47b39
JB
225background color is the background of this face. Set this to
226make the mail indicator stand out on a color display."
83033a3a 227 :group 'mode-line-faces
c2acf685 228 :group 'display-time
bf247b6e 229 :version "22.1"
b7eb6e8a 230 :type '(choice (const :tag "None" nil) face))
c2acf685
SM
231
232(defvar display-time-mail-icon
cc351ed4 233 (find-image '((:type xpm :file "letter.xpm" :ascent center)
b7eb6e8a
DL
234 (:type pbm :file "letter.pbm" :ascent center)))
235 "Image specification to offer as the mail indicator on a graphic display.
236See `display-time-use-mail-icon' and `display-time-mail-face'.")
c2acf685 237
b7eb6e8a 238;; Fixme: Default to icon on graphical display?
c2acf685 239(defcustom display-time-use-mail-icon nil
d7baab9c 240 "Non-nil means use an icon as mail indicator on a graphic display.
b7eb6e8a
DL
241Otherwise use `display-time-mail-string'. The icon may consume less
242of the mode line. It is specified by `display-time-mail-icon'."
c2acf685
SM
243 :group 'display-time
244 :type 'boolean)
245
b7eb6e8a
DL
246;; Fixme: maybe default to the character if we can display Unicode.
247(defcustom display-time-mail-string "Mail"
248 "String to use as the mail indicator in `display-time-string-forms'.
249This can use the Unicode letter character if you can display it."
250 :group 'display-time
bf247b6e 251 :version "22.1"
b7eb6e8a
DL
252 :type '(choice (const "Mail")
253 ;; Use :tag here because the Lucid menu won't display
254 ;; multibyte text.
255 (const :tag "Unicode letter character" "✉")
256 string))
257
c2acf685 258(defcustom display-time-format nil
9201cc28 259 "String specifying format for displaying the time in the mode line.
c2acf685
SM
260See the function `format-time-string' for an explanation of
261how to write this string. If this is nil, the defaults
262depend on `display-time-day-and-date' and `display-time-24hr-format'."
263 :type '(choice (const :tag "Default" nil)
264 string)
265 :group 'display-time)
266
267(defcustom display-time-string-forms
268 '((if (and (not display-time-format) display-time-day-and-date)
269 (format-time-string "%a %b %e " now)
270 "")
e93b2f39
SM
271 (propertize
272 (format-time-string (or display-time-format
273 (if display-time-24hr-format "%H:%M" "%-I:%M%p"))
274 now)
ef3e9175 275 'help-echo (format-time-string "%a %b %e, %Y" now))
c2acf685
SM
276 load
277 (if mail
278 ;; Build the string every time to act on customization.
b7eb6e8a
DL
279 ;; :set-after doesn't help for `customize-option'. I think it
280 ;; should.
281 (concat
282 " "
283 (propertize
284 display-time-mail-string
285 'display `(when (and display-time-use-mail-icon
286 (display-graphic-p))
287 ,@display-time-mail-icon
288 ,@(if (and display-time-mail-face
289 (memq (plist-get (cdr display-time-mail-icon)
290 :type)
291 '(pbm xbm)))
292 (let ((bg (face-attribute display-time-mail-face
293 :background)))
294 (if (stringp bg)
295 (list :background bg)))))
296 'face display-time-mail-face
297 'help-echo "You have new mail; mouse-2: Read mail"
3a02b63e 298 'mouse-face 'mode-line-highlight
b7eb6e8a
DL
299 'local-map (make-mode-line-mouse-map 'mouse-2
300 read-mail-command)))
c2acf685 301 ""))
9201cc28 302 "List of expressions governing display of the time in the mode line.
c2acf685
SM
303For most purposes, you can control the time format using `display-time-format'
304which is a more standard interface.
305
306This expression is a list of expressions that can involve the keywords
307`load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
308`seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
309and `time-zone' all alphabetic strings, and `mail' a true/nil value.
310
311For example, the form
312
313 '((substring year -2) \"/\" month \"/\" day
314 \" \" 24-hours \":\" minutes \":\" seconds
315 (if time-zone \" (\") time-zone (if time-zone \")\")
316 (if mail \" Mail\" \"\"))
317
318would give mode line times like `94/12/30 21:07:48 (UTC)'."
319 :type 'sexp
320 :group 'display-time)
321
322(defun display-time-event-handler ()
323 (display-time-update)
324 ;; Do redisplay right now, if no input pending.
325 (sit-for 0)
326 (let* ((current (current-time))
327 (timer display-time-timer)
328 ;; Compute the time when this timer will run again, next.
329 (next-time (timer-relative-time
330 (list (aref timer 1) (aref timer 2) (aref timer 3))
331 (* 5 (aref timer 4)) 0)))
332 ;; If the activation time is far in the past,
333 ;; skip executions until we reach a time in the future.
334 ;; This avoids a long pause if Emacs has been suspended for hours.
335 (or (> (nth 0 next-time) (nth 0 current))
336 (and (= (nth 0 next-time) (nth 0 current))
337 (> (nth 1 next-time) (nth 1 current)))
338 (and (= (nth 0 next-time) (nth 0 current))
339 (= (nth 1 next-time) (nth 1 current))
340 (> (nth 2 next-time) (nth 2 current)))
341 (progn
342 (timer-set-time timer (timer-next-integral-multiple-of-time
343 current display-time-interval)
344 display-time-interval)
345 (timer-activate timer)))))
346
4b05e68d
PJ
347(defun display-time-next-load-average ()
348 (interactive)
349 (if (= 3 (setq display-time-load-average (1+ display-time-load-average)))
350 (setq display-time-load-average 0))
351 (display-time-update)
352 (sit-for 0))
353
cc351ed4
PJ
354(defun display-time-mail-check-directory ()
355 (let ((mail-files (directory-files display-time-mail-directory t))
356 (size 0))
357 (while (and mail-files (= size 0))
358 ;; Count size of regular files only.
359 (setq size (+ size (or (and (file-regular-p (car mail-files))
360 (nth 7 (file-attributes (car mail-files))))
361 0)))
362 (setq mail-files (cdr mail-files)))
363 (if (> size 0)
364 size
365 nil)))
366
c2acf685 367(defun display-time-update ()
b7eb6e8a
DL
368 "Update the display-time info for the mode line.
369However, don't redisplay right now.
370
371This is used for things like Rmail `g' that want to force an
372update which can wait for the next redisplay."
c2acf685
SM
373 (let* ((now (current-time))
374 (time (current-time-string now))
fe6456af
RS
375 (load (if (null display-time-load-average)
376 ""
377 (condition-case ()
378 ;; Do not show values less than
379 ;; `display-time-load-average-threshold'.
380 (if (> (* display-time-load-average-threshold 100)
381 (nth display-time-load-average (load-average)))
382 ""
383 ;; The load average number is mysterious, so
384 ;; provide some help.
b7eb6e8a
DL
385 (let ((str (format " %03d"
386 (nth display-time-load-average
387 (load-average)))))
fe6456af
RS
388 (propertize
389 (concat (substring str 0 -2) "." (substring str -2))
b7eb6e8a
DL
390 'local-map (make-mode-line-mouse-map
391 'mouse-2 'display-time-next-load-average)
3a02b63e 392 'mouse-face 'mode-line-highlight
b7eb6e8a
DL
393 'help-echo (concat
394 "System load average for past "
395 (if (= 0 display-time-load-average)
396 "1 minute"
397 (if (= 1 display-time-load-average)
398 "5 minutes"
399 "15 minutes"))
400 "; mouse-2: next"))))
fe6456af 401 (error ""))))
c2acf685
SM
402 (mail-spool-file (or display-time-mail-file
403 (getenv "MAIL")
404 (concat rmail-spool-directory
405 (user-login-name))))
406 (mail (or (and display-time-mail-function
407 (funcall display-time-mail-function))
cc351ed4
PJ
408 (and display-time-mail-directory
409 (display-time-mail-check-directory))
c2acf685
SM
410 (and (stringp mail-spool-file)
411 (or (null display-time-server-down-time)
412 ;; If have been down for 20 min, try again.
413 (> (- (nth 1 now) display-time-server-down-time)
414 1200)
415 (and (< (nth 1 now) display-time-server-down-time)
b7eb6e8a
DL
416 (> (- (nth 1 now)
417 display-time-server-down-time)
c2acf685
SM
418 -64336)))
419 (let ((start-time (current-time)))
420 (prog1
421 (display-time-file-nonempty-p mail-spool-file)
b7eb6e8a
DL
422 (if (> (- (nth 1 (current-time))
423 (nth 1 start-time))
c2acf685
SM
424 20)
425 ;; Record that mail file is not accessible.
426 (setq display-time-server-down-time
427 (nth 1 (current-time)))
428 ;; Record that mail file is accessible.
429 (setq display-time-server-down-time nil)))))))
430 (24-hours (substring time 11 13))
027a4b6b 431 (hour (string-to-number 24-hours))
c2acf685
SM
432 (12-hours (int-to-string (1+ (% (+ hour 11) 12))))
433 (am-pm (if (>= hour 12) "pm" "am"))
434 (minutes (substring time 14 16))
435 (seconds (substring time 17 19))
436 (time-zone (car (cdr (current-time-zone now))))
437 (day (substring time 8 10))
438 (year (substring time 20 24))
439 (monthname (substring time 4 7))
440 (month
441 (cdr
442 (assoc
443 monthname
444 '(("Jan" . "1") ("Feb" . "2") ("Mar" . "3") ("Apr" . "4")
445 ("May" . "5") ("Jun" . "6") ("Jul" . "7") ("Aug" . "8")
446 ("Sep" . "9") ("Oct" . "10") ("Nov" . "11") ("Dec" . "12")))))
447 (dayname (substring time 0 3)))
448 (setq display-time-string
449 (mapconcat 'eval display-time-string-forms ""))
450 ;; This is inside the let binding, but we are not going to document
451 ;; what variables are available.
452 (run-hooks 'display-time-hook))
453 (force-mode-line-update))
454
455(defun display-time-file-nonempty-p (file)
456 (and (file-exists-p file)
457 (< 0 (nth 7 (file-attributes (file-chase-links file))))))
458
57ce63c4
SM
459;;;###autoload
460(define-minor-mode display-time-mode
461 "Toggle display of time, load level, and mail flag in mode lines.
462With a numeric arg, enable this display if arg is positive.
463
464When this display is enabled, it updates automatically every minute.
465If `display-time-day-and-date' is non-nil, the current day and date
466are displayed as well.
467This runs the normal hook `display-time-hook' after each update."
bb7a71c5 468 :global t :group 'display-time
fe6456af
RS
469 (and display-time-timer (cancel-timer display-time-timer))
470 (setq display-time-timer nil)
471 (setq display-time-string "")
472 (or global-mode-string (setq global-mode-string '("")))
473 (setq display-time-load-average display-time-default-load-average)
474 (if display-time-mode
475 (progn
476 (or (memq 'display-time-string global-mode-string)
477 (setq global-mode-string
478 (append global-mode-string '(display-time-string))))
479 ;; Set up the time timer.
480 (setq display-time-timer
481 (run-at-time t display-time-interval
482 'display-time-event-handler))
483 ;; Make the time appear right away.
484 (display-time-update)
485 ;; When you get new mail, clear "Mail" from the mode line.
486 (add-hook 'rmail-after-get-new-mail-hook
487 'display-time-event-handler))
488 (remove-hook 'rmail-after-get-new-mail-hook
489 'display-time-event-handler)))
c2acf685 490
aff2ba04
RS
491
492(defun display-time-world-mode ()
493 "Major mode for buffer that displays times in various time zones.
494See `display-time-world'."
495 (interactive)
496 (kill-all-local-variables)
497 (setq
498 major-mode 'display-time-world-mode
499 mode-name "World clock")
500 (use-local-map display-time-world-mode-map))
501
502(defun display-time-world-display (alist)
503 "Replace current buffer text with times in various zones, based on ALIST."
504 (let ((inhibit-read-only t)
505 (buffer-undo-list t))
506 (erase-buffer)
507 (let ((max-width 0)
508 (result ()))
509 (unwind-protect
510 (dolist (zone alist)
511 (let* ((label (cadr zone))
512 (width (string-width label)))
513 (set-time-zone-rule (car zone))
514 (setq result
515 (append result
516 (list
517 label width
518 (format-time-string display-time-world-time-format))))
519 (when (> width max-width)
520 (setq max-width width))))
521 (set-time-zone-rule nil))
522 (while result
523 (insert (pop result)
524 (make-string (1+ (- max-width (pop result))) ?\s)
525 (pop result) "\n")))
526 (delete-backward-char 1)))
527
528;;;###autoload
529(defun display-time-world ()
530 "Enable updating display of times in various time zones.
531`display-time-world-list' specifies the zones.
532To turn off the world time display, go to that window and type `q'."
533 (interactive)
534 (when (and display-time-world-timer-enable
535 (not (get-buffer display-time-world-buffer-name)))
536 (run-at-time t display-time-world-timer-second 'display-time-world-timer))
537 (with-current-buffer (get-buffer-create display-time-world-buffer-name)
538 (display-time-world-display display-time-world-list))
539 (pop-to-buffer display-time-world-buffer-name)
540 (fit-window-to-buffer)
541 (display-time-world-mode))
542
543(defun display-time-world-timer ()
544 (if (get-buffer display-time-world-buffer-name)
545 (with-current-buffer (get-buffer display-time-world-buffer-name)
546 (display-time-world-display display-time-world-list))
547 ;; cancel timer
548 (let ((list timer-list))
549 (while list
550 (let ((elt (pop list)))
551 (when (equal (symbol-name (aref elt 5)) "display-time-world-timer")
552 (cancel-timer elt)))))))
553
2736785a 554;;;###autoload
04e56139
GM
555(defun emacs-uptime (&optional format)
556 "Return a string giving the uptime of this instance of Emacs.
557FORMAT is a string to format the result, using `format-seconds'.
558For example, the Unix uptime command format is \"%D, %z%2h:%.2m\"."
2736785a
GM
559 (interactive)
560 (let ((str
04e56139 561 (format-seconds (or format "%Y, %D, %H, %M, %z%S")
2736785a 562 (time-to-seconds
e279593d 563 (time-subtract (current-time) before-init-time)))))
2736785a
GM
564 (if (interactive-p)
565 (message "%s" str)
566 str)))
567
24d6c292
JL
568;;;###autoload
569(defun emacs-init-time ()
570 "Return a string giving the duration of the Emacs initialization."
571 (interactive)
572 (let ((str
ac1efd51
JL
573 (format "%.1f seconds"
574 (time-to-seconds
575 (time-subtract after-init-time before-init-time)))))
24d6c292
JL
576 (if (interactive-p)
577 (message "%s" str)
578 str)))
579
c2acf685
SM
580(provide 'time)
581
cbee283d 582;; arch-tag: b9c1623f-b5cb-48e4-b650-482a4d23c5a6
c2acf685 583;;; time.el ends here