Trivial ert.texi update for cl-lib namespace
[emacs.git] / lisp / battery.el
blob69d25643bb910d4f2399a634bdda272bd8cf43d0
1 ;;; battery.el --- display battery status information -*- coding: iso-8859-1 -*-
3 ;; Copyright (C) 1997-1998, 2000-2012 Free Software Foundation, Inc.
5 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
6 ;; Keywords: hardware
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; There is at present support for GNU/Linux, OS X and Windows. This
26 ;; library supports both the `/proc/apm' file format of Linux version
27 ;; 1.3.58 or newer and the `/proc/acpi/' directory structure of Linux
28 ;; 2.4.20 and 2.6. Darwin (OS X) is supported by using the `pmset'
29 ;; program. Windows is supported by the GetSystemPowerStatus API call.
31 ;;; Code:
33 (require 'timer)
34 (eval-when-compile (require 'cl-lib))
36 (defgroup battery nil
37 "Display battery status information."
38 :prefix "battery-"
39 :group 'hardware)
41 ;; Either BATn or yeeloong-bat, basically.
42 (defconst battery--linux-sysfs-regexp "[bB][aA][tT][0-9]?$")
44 (defcustom battery-status-function
45 (cond ((and (eq system-type 'gnu/linux)
46 (file-readable-p "/proc/apm"))
47 'battery-linux-proc-apm)
48 ((and (eq system-type 'gnu/linux)
49 (file-directory-p "/proc/acpi/battery"))
50 'battery-linux-proc-acpi)
51 ((and (eq system-type 'gnu/linux)
52 (file-directory-p "/sys/class/power_supply/")
53 (directory-files "/sys/class/power_supply/" nil
54 battery--linux-sysfs-regexp))
55 'battery-linux-sysfs)
56 ((and (eq system-type 'darwin)
57 (condition-case nil
58 (with-temp-buffer
59 (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
60 (> (buffer-size) 0)))
61 (error nil)))
62 'battery-pmset)
63 ((fboundp 'w32-battery-status)
64 'w32-battery-status))
65 "Function for getting battery status information.
66 The function has to return an alist of conversion definitions.
67 Its cons cells are of the form
69 (CONVERSION . REPLACEMENT-TEXT)
71 CONVERSION is the character code of a \"conversion specification\"
72 introduced by a `%' character in a control string."
73 :type '(choice (const nil) function)
74 :group 'battery)
76 (defcustom battery-echo-area-format
77 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
78 "Power %L, battery %B at %r (%p%% load, remaining time %t)")
79 ((eq battery-status-function 'battery-linux-sysfs)
80 "Power %L, battery %B (%p%% load, remaining time %t)")
81 ((eq battery-status-function 'battery-pmset)
82 "%L power, battery %B (%p%% load, remaining time %t)")
83 (battery-status-function
84 "Power %L, battery %B (%p%% load, remaining time %t)"))
85 "Control string formatting the string to display in the echo area.
86 Ordinary characters in the control string are printed as-is, while
87 conversion specifications introduced by a `%' character in the control
88 string are substituted as defined by the current value of the variable
89 `battery-status-function'. Here are the ones generally available:
90 %c Current capacity (mAh or mWh)
91 %r Current rate of charge or discharge
92 %B Battery status (verbose)
93 %b Battery status: empty means high, `-' means low,
94 `!' means critical, and `+' means charging
95 %d Temperature (in degrees Celsius)
96 %L AC line status (verbose)
97 %p Battery load percentage
98 %m Remaining time (to charge or discharge) in minutes
99 %h Remaining time (to charge or discharge) in hours
100 %t Remaining time (to charge or discharge) in the form `h:min'"
101 :type '(choice string (const nil))
102 :group 'battery)
104 (defvar battery-mode-line-string nil
105 "String to display in the mode line.")
106 ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
108 (defcustom battery-mode-line-limit 100
109 "Percentage of full battery load below which display battery status"
110 :version "24.1"
111 :type 'integer
112 :group 'battery)
114 (defcustom battery-mode-line-format
115 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
116 "[%b%p%%,%d°C]")
117 (battery-status-function
118 "[%b%p%%]"))
119 "Control string formatting the string to display in the mode line.
120 Ordinary characters in the control string are printed as-is, while
121 conversion specifications introduced by a `%' character in the control
122 string are substituted as defined by the current value of the variable
123 `battery-status-function'. Here are the ones generally available:
124 %c Current capacity (mAh or mWh)
125 %r Current rate of charge or discharge
126 %B Battery status (verbose)
127 %b Battery status: empty means high, `-' means low,
128 `!' means critical, and `+' means charging
129 %d Temperature (in degrees Celsius)
130 %L AC line status (verbose)
131 %p Battery load percentage
132 %m Remaining time (to charge or discharge) in minutes
133 %h Remaining time (to charge or discharge) in hours
134 %t Remaining time (to charge or discharge) in the form `h:min'"
135 :type '(choice string (const nil))
136 :group 'battery)
138 (defcustom battery-update-interval 60
139 "Seconds after which the battery status will be updated."
140 :type 'integer
141 :group 'battery)
143 (defcustom battery-load-low 25
144 "Upper bound of low battery load percentage.
145 A battery load percentage below this number is considered low."
146 :type 'integer
147 :group 'battery)
149 (defcustom battery-load-critical 10
150 "Upper bound of critical battery load percentage.
151 A battery load percentage below this number is considered critical."
152 :type 'integer
153 :group 'battery)
155 (defvar battery-update-timer nil
156 "Interval timer object.")
158 ;;;###autoload
159 (defun battery ()
160 "Display battery status information in the echo area.
161 The text being displayed in the echo area is controlled by the variables
162 `battery-echo-area-format' and `battery-status-function'."
163 (interactive)
164 (message "%s" (if (and battery-echo-area-format battery-status-function)
165 (battery-format battery-echo-area-format
166 (funcall battery-status-function))
167 "Battery status not available")))
169 ;;;###autoload
170 (define-minor-mode display-battery-mode
171 "Toggle battery status display in mode line (Display Battery mode).
172 With a prefix argument ARG, enable Display Battery mode if ARG is
173 positive, and disable it otherwise. If called from Lisp, enable
174 the mode if ARG is omitted or nil.
176 The text displayed in the mode line is controlled by
177 `battery-mode-line-format' and `battery-status-function'.
178 The mode line is be updated every `battery-update-interval'
179 seconds."
180 :global t :group 'battery
181 (setq battery-mode-line-string "")
182 (or global-mode-string (setq global-mode-string '("")))
183 (and battery-update-timer (cancel-timer battery-update-timer))
184 (if (and battery-status-function battery-mode-line-format)
185 (if (not display-battery-mode)
186 (setq global-mode-string
187 (delq 'battery-mode-line-string global-mode-string))
188 (add-to-list 'global-mode-string 'battery-mode-line-string t)
189 (setq battery-update-timer (run-at-time nil battery-update-interval
190 'battery-update-handler))
191 (battery-update))
192 (message "Battery status not available")
193 (setq display-battery-mode nil)))
195 (defun battery-update-handler ()
196 (battery-update)
197 (sit-for 0))
199 (defun battery-update ()
200 "Update battery status information in the mode line."
201 (let ((data (and battery-status-function (funcall battery-status-function))))
202 (setq battery-mode-line-string
203 (propertize (if (and battery-mode-line-format
204 (<= (car (read-from-string (cdr (assq ?p data))))
205 battery-mode-line-limit))
206 (battery-format
207 battery-mode-line-format
208 data)
210 'face
211 (and (<= (car (read-from-string (cdr (assq ?p data))))
212 battery-load-critical)
213 'error)
214 'help-echo "Battery status information")))
215 (force-mode-line-update))
217 ;;; `/proc/apm' interface for Linux.
219 (defconst battery-linux-proc-apm-regexp
220 (concat "^\\([^ ]+\\)" ; Driver version.
221 " \\([^ ]+\\)" ; APM BIOS version.
222 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
223 " 0x\\([0-9a-f]+\\)" ; AC line status.
224 " 0x\\([0-9a-f]+\\)" ; Battery status.
225 " 0x\\([0-9a-f]+\\)" ; Battery flags.
226 " \\(-?[0-9]+\\)%" ; Load percentage.
227 " \\(-?[0-9]+\\)" ; Remaining time.
228 " \\(.*\\)" ; Time unit.
229 "$")
230 "Regular expression matching contents of `/proc/apm'.")
232 (defun battery-linux-proc-apm ()
233 "Get APM status information from Linux (the kernel).
234 This function works only with the new `/proc/apm' format introduced
235 in Linux version 1.3.58.
237 The following %-sequences are provided:
238 %v Linux driver version
239 %V APM BIOS version
240 %I APM BIOS status (verbose)
241 %L AC line status (verbose)
242 %B Battery status (verbose)
243 %b Battery status, empty means high, `-' means low,
244 `!' means critical, and `+' means charging
245 %p Battery load percentage
246 %s Remaining time (to charge or discharge) in seconds
247 %m Remaining time (to charge or discharge) in minutes
248 %h Remaining time (to charge or discharge) in hours
249 %t Remaining time (to charge or discharge) in the form `h:min'"
250 (let (driver-version bios-version bios-interface line-status
251 battery-status battery-status-symbol load-percentage
252 seconds minutes hours remaining-time tem)
253 (with-temp-buffer
254 (ignore-errors (insert-file-contents "/proc/apm"))
255 (when (re-search-forward battery-linux-proc-apm-regexp)
256 (setq driver-version (match-string 1))
257 (setq bios-version (match-string 2))
258 (setq tem (string-to-number (match-string 3) 16))
259 (if (not (logand tem 2))
260 (setq bios-interface "not supported")
261 (setq bios-interface "enabled")
262 (cond ((logand tem 16) (setq bios-interface "disabled"))
263 ((logand tem 32) (setq bios-interface "disengaged")))
264 (setq tem (string-to-number (match-string 4) 16))
265 (cond ((= tem 0) (setq line-status "off-line"))
266 ((= tem 1) (setq line-status "on-line"))
267 ((= tem 2) (setq line-status "on backup")))
268 (setq tem (string-to-number (match-string 6) 16))
269 (if (= tem 255)
270 (setq battery-status "N/A")
271 (setq tem (string-to-number (match-string 5) 16))
272 (cond ((= tem 0) (setq battery-status "high"
273 battery-status-symbol ""))
274 ((= tem 1) (setq battery-status "low"
275 battery-status-symbol "-"))
276 ((= tem 2) (setq battery-status "critical"
277 battery-status-symbol "!"))
278 ((= tem 3) (setq battery-status "charging"
279 battery-status-symbol "+")))
280 (setq load-percentage (match-string 7))
281 (setq seconds (string-to-number (match-string 8)))
282 (and (string-equal (match-string 9) "min")
283 (setq seconds (* 60 seconds)))
284 (setq minutes (/ seconds 60)
285 hours (/ seconds 3600))
286 (setq remaining-time
287 (format "%d:%02d" hours (- minutes (* 60 hours))))))))
288 (list (cons ?v (or driver-version "N/A"))
289 (cons ?V (or bios-version "N/A"))
290 (cons ?I (or bios-interface "N/A"))
291 (cons ?L (or line-status "N/A"))
292 (cons ?B (or battery-status "N/A"))
293 (cons ?b (or battery-status-symbol ""))
294 (cons ?p (or load-percentage "N/A"))
295 (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
296 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
297 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
298 (cons ?t (or remaining-time "N/A")))))
301 ;;; `/proc/acpi/' interface for Linux.
303 (defun battery-linux-proc-acpi ()
304 "Get ACPI status information from Linux (the kernel).
305 This function works only with the `/proc/acpi/' format introduced
306 in Linux version 2.4.20 and 2.6.0.
308 The following %-sequences are provided:
309 %c Current capacity (mAh)
310 %r Current rate
311 %B Battery status (verbose)
312 %b Battery status, empty means high, `-' means low,
313 `!' means critical, and `+' means charging
314 %d Temperature (in degrees Celsius)
315 %L AC line status (verbose)
316 %p Battery load percentage
317 %m Remaining time (to charge or discharge) in minutes
318 %h Remaining time (to charge or discharge) in hours
319 %t Remaining time (to charge or discharge) in the form `h:min'"
320 (let ((design-capacity 0)
321 (last-full-capacity 0)
322 full-capacity
323 (warn 0)
324 (low 0)
325 capacity rate rate-type charging-state minutes hours)
326 ;; ACPI provides information about each battery present in the system in
327 ;; a separate subdirectory. We are going to merge the available
328 ;; information together since displaying for a variable amount of
329 ;; batteries seems overkill for format-strings.
330 (with-temp-buffer
331 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
332 t "\\`[^.]")))
333 (erase-buffer)
334 (ignore-errors (insert-file-contents (expand-file-name "state" dir)))
335 (when (re-search-forward "present: +yes$" nil t)
336 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
337 (member charging-state '("unknown" "charged" nil))
338 ;; On most multi-battery systems, most of the time only one
339 ;; battery is "charging"/"discharging", the others are
340 ;; "unknown".
341 (setq charging-state (match-string 1)))
342 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
343 nil t)
344 (setq rate (+ (or rate 0) (string-to-number (match-string 1))))
345 (when (> rate 0)
346 (setq rate-type (or (and rate-type
347 (if (string= rate-type (match-string 2))
348 rate-type
349 (error
350 "Inconsistent rate types (%s vs. %s)"
351 rate-type (match-string 2))))
352 (match-string 2)))))
353 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
354 nil t)
355 (setq capacity
356 (+ (or capacity 0) (string-to-number (match-string 1))))))
357 (goto-char (point-max))
358 (ignore-errors (insert-file-contents (expand-file-name "info" dir)))
359 (when (re-search-forward "present: +yes$" nil t)
360 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
361 nil t)
362 (cl-incf design-capacity (string-to-number (match-string 1))))
363 (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
364 nil t)
365 (cl-incf last-full-capacity (string-to-number (match-string 1))))
366 (when (re-search-forward
367 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
368 (cl-incf warn (string-to-number (match-string 1))))
369 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
370 nil t)
371 (cl-incf low (string-to-number (match-string 1)))))))
372 (setq full-capacity (if (> last-full-capacity 0)
373 last-full-capacity design-capacity))
374 (and capacity rate
375 (setq minutes (if (zerop rate) 0
376 (floor (* (/ (float (if (string= charging-state
377 "charging")
378 (- full-capacity capacity)
379 capacity))
380 rate)
381 60)))
382 hours (/ minutes 60)))
383 (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
384 (cons ?L (or (battery-search-for-one-match-in-files
385 (mapcar (lambda (e) (concat e "/state"))
386 (ignore-errors
387 (directory-files "/proc/acpi/ac_adapter/"
388 t "\\`[^.]")))
389 "state: +\\(.*\\)$" 1)
391 "N/A"))
392 (cons ?d (or (battery-search-for-one-match-in-files
393 (mapcar (lambda (e) (concat e "/temperature"))
394 (ignore-errors
395 (directory-files "/proc/acpi/thermal_zone/"
396 t "\\`[^.]")))
397 "temperature: +\\([0-9]+\\) C$" 1)
399 "N/A"))
400 (cons ?r (or (and rate (concat (number-to-string rate) " "
401 rate-type)) "N/A"))
402 (cons ?B (or charging-state "N/A"))
403 (cons ?b (or (and (string= charging-state "charging") "+")
404 (and capacity (< capacity low) "!")
405 (and capacity (< capacity warn) "-")
406 ""))
407 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
408 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
409 (cons ?t (or (and minutes
410 (format "%d:%02d" hours (- minutes (* 60 hours))))
411 "N/A"))
412 (cons ?p (or (and full-capacity capacity
413 (> full-capacity 0)
414 (number-to-string
415 (floor (/ capacity
416 (/ (float full-capacity) 100)))))
417 "N/A")))))
420 ;;; `/sys/class/power_supply/BATN' interface for Linux.
422 (defun battery-linux-sysfs ()
423 "Get ACPI status information from Linux kernel.
424 This function works only with the new `/sys/class/power_supply/'
425 format introduced in Linux version 2.4.25.
427 The following %-sequences are provided:
428 %c Current capacity (mAh or mWh)
429 %r Current rate
430 %B Battery status (verbose)
431 %d Temperature (in degrees Celsius)
432 %p Battery load percentage
433 %L AC line status (verbose)
434 %m Remaining time (to charge or discharge) in minutes
435 %h Remaining time (to charge or discharge) in hours
436 %t Remaining time (to charge or discharge) in the form `h:min'"
437 (let (charging-state rate temperature hours
438 (charge-full 0.0)
439 (charge-now 0.0)
440 (energy-full 0.0)
441 (energy-now 0.0))
442 ;; SysFS provides information about each battery present in the
443 ;; system in a separate subdirectory. We are going to merge the
444 ;; available information together.
445 (with-temp-buffer
446 (dolist (dir (ignore-errors
447 (directory-files
448 "/sys/class/power_supply/" t
449 battery--linux-sysfs-regexp)))
450 (erase-buffer)
451 (ignore-errors (insert-file-contents
452 (expand-file-name "uevent" dir)))
453 (when (re-search-forward "POWER_SUPPLY_PRESENT=1$" nil t)
454 (goto-char (point-min))
455 (and (re-search-forward "POWER_SUPPLY_STATUS=\\(.*\\)$" nil t)
456 (member charging-state '("Unknown" "Full" nil))
457 (setq charging-state (match-string 1)))
458 (when (re-search-forward
459 "POWER_SUPPLY_\\(CURRENT\\|POWER\\)_NOW=\\([0-9]*\\)$"
460 nil t)
461 (setq rate (float (string-to-number (match-string 2)))))
462 (when (re-search-forward "POWER_SUPPLY_TEMP=\\([0-9]*\\)$" nil t)
463 (setq temperature (match-string 1)))
464 (let (full-string now-string)
465 ;; Sysfs may list either charge (mAh) or energy (mWh).
466 ;; Keep track of both, and choose which to report later.
467 (cond ((and (re-search-forward
468 "POWER_SUPPLY_CHARGE_FULL=\\([0-9]*\\)$" nil t)
469 (setq full-string (match-string 1))
470 (re-search-forward
471 "POWER_SUPPLY_CHARGE_NOW=\\([0-9]*\\)$" nil t)
472 (setq now-string (match-string 1)))
473 (setq charge-full (+ charge-full
474 (string-to-number full-string))
475 charge-now (+ charge-now
476 (string-to-number now-string))))
477 ((and (re-search-forward
478 "POWER_SUPPLY_ENERGY_FULL=\\([0-9]*\\)$" nil t)
479 (setq full-string (match-string 1))
480 (re-search-forward
481 "POWER_SUPPLY_ENERGY_NOW=\\([0-9]*\\)$" nil t)
482 (setq now-string (match-string 1)))
483 (setq energy-full (+ energy-full
484 (string-to-number full-string))
485 energy-now (+ energy-now
486 (string-to-number now-string))))))
487 (goto-char (point-min))
488 (when (and energy-now rate (not (zerop rate))
489 (re-search-forward
490 "POWER_SUPPLY_VOLTAGE_NOW=\\([0-9]*\\)$" nil t))
491 (let ((remaining (if (string= charging-state "Discharging")
492 energy-now
493 (- energy-full energy-now))))
494 (setq hours (/ (/ (* remaining (string-to-number
495 (match-string 1)))
496 rate)
497 10000000.0)))))))
498 (list (cons ?c (cond ((or (> charge-full 0) (> charge-now 0))
499 (number-to-string charge-now))
500 ((or (> energy-full 0) (> energy-now 0))
501 (number-to-string energy-now))
502 (t "N/A")))
503 (cons ?r (if rate (format "%.1f" (/ rate 1000000.0)) "N/A"))
504 (cons ?m (if hours (format "%d" (* hours 60)) "N/A"))
505 (cons ?h (if hours (format "%d" hours) "N/A"))
506 (cons ?t (if hours
507 (format "%d:%02d" hours (* (- hours (floor hours)) 60))
508 "N/A"))
509 (cons ?d (or temperature "N/A"))
510 (cons ?B (or charging-state "N/A"))
511 (cons ?p (cond ((and (> charge-full 0) (> charge-now 0))
512 (format "%.1f"
513 (/ (* 100 charge-now) charge-full)))
514 ((> energy-full 0)
515 (format "%.1f"
516 (/ (* 100 energy-now) energy-full)))
517 (t "N/A")))
518 (cons ?L (if (file-readable-p "/sys/class/power_supply/AC/online")
519 (if (battery-search-for-one-match-in-files
520 (list "/sys/class/power_supply/AC/online"
521 "/sys/class/power_supply/ACAD/online")
522 "1" 0)
523 "AC"
524 "BAT")
525 "N/A")))))
527 ;;; `pmset' interface for Darwin (OS X).
529 (defun battery-pmset ()
530 "Get battery status information using `pmset'.
532 The following %-sequences are provided:
533 %L Power source (verbose)
534 %B Battery status (verbose)
535 %b Battery status, empty means high, `-' means low,
536 `!' means critical, and `+' means charging
537 %p Battery load percentage
538 %h Remaining time in hours
539 %m Remaining time in minutes
540 %t Remaining time in the form `h:min'"
541 (let (power-source load-percentage battery-status battery-status-symbol
542 remaining-time hours minutes)
543 (with-temp-buffer
544 (ignore-errors (call-process "pmset" nil t nil "-g" "ps"))
545 (goto-char (point-min))
546 (when (re-search-forward "Currentl?y drawing from '\\(AC\\|Battery\\) Power'" nil t)
547 (setq power-source (match-string 1))
548 (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t)
549 (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
550 (setq load-percentage (match-string 1))
551 (goto-char (match-end 0))
552 (cond ((looking-at "; charging")
553 (setq battery-status "charging"
554 battery-status-symbol "+"))
555 ((< (string-to-number load-percentage) battery-load-low)
556 (setq battery-status "low"
557 battery-status-symbol "-"))
558 ((< (string-to-number load-percentage) battery-load-critical)
559 (setq battery-status "critical"
560 battery-status-symbol "!"))
562 (setq battery-status "high"
563 battery-status-symbol "")))
564 (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t)
565 (setq remaining-time (match-string 1))
566 (let ((h (string-to-number (match-string 2)))
567 (m (string-to-number (match-string 3))))
568 (setq hours (number-to-string (+ h (if (< m 30) 0 1)))
569 minutes (number-to-string (+ (* h 60) m)))))))))
570 (list (cons ?L (or power-source "N/A"))
571 (cons ?p (or load-percentage "N/A"))
572 (cons ?B (or battery-status "N/A"))
573 (cons ?b (or battery-status-symbol ""))
574 (cons ?h (or hours "N/A"))
575 (cons ?m (or minutes "N/A"))
576 (cons ?t (or remaining-time "N/A")))))
579 ;;; Private functions.
581 (defun battery-format (format alist)
582 "Substitute %-sequences in FORMAT."
583 (replace-regexp-in-string
584 "%."
585 (lambda (str)
586 (let ((char (aref str 1)))
587 (if (eq char ?%) "%"
588 (or (cdr (assoc char alist)) ""))))
589 format t t))
591 (defun battery-search-for-one-match-in-files (files regexp match-num)
592 "Search REGEXP in the content of the files listed in FILES.
593 If a match occurred, return the parenthesized expression numbered by
594 MATCH-NUM in the match. Otherwise, return nil."
595 (with-temp-buffer
596 (catch 'found
597 (dolist (file files)
598 (and (ignore-errors (insert-file-contents file nil nil nil 'replace))
599 (re-search-forward regexp nil t)
600 (throw 'found (match-string match-num)))))))
603 (provide 'battery)
605 ;;; battery.el ends here