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