* lisp/files.el (locate-dominating-file): Doc fix.
[emacs.git] / lisp / battery.el
blob586be9e8938ca9529c34b689fba220073813bfed
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))
37 (defgroup battery nil
38 "Display battery status information."
39 :prefix "battery-"
40 :group 'hardware)
42 (defcustom battery-status-function
43 (cond ((and (eq system-type 'gnu/linux)
44 (file-readable-p "/proc/apm"))
45 'battery-linux-proc-apm)
46 ((and (eq system-type 'gnu/linux)
47 (file-directory-p "/proc/acpi/battery"))
48 'battery-linux-proc-acpi)
49 ((and (eq system-type 'gnu/linux)
50 (file-directory-p "/sys/class/power_supply/")
51 (directory-files "/sys/class/power_supply/" nil "BAT[0-9]$"))
52 'battery-linux-sysfs)
53 ((and (eq system-type 'gnu/linux)
54 (file-directory-p "/sys/class/power_supply/yeeloong-bat/")
55 (directory-files "/sys/class/power_supply/yeeloong-bat/" nil "charge_"))
56 'battery-yeeloong-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
70 (CONVERSION . REPLACEMENT-TEXT)
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)
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)")
82 ((eq battery-status-function 'battery-pmset)
83 "%L power, battery %B (%p%% load, remaining time %t)")
84 ((eq battery-status-function 'battery-yeeloong-sysfs)
85 "%L power, battery %B (%p%% load, remaining time %t)")
86 (battery-status-function
87 "Power %L, battery %B (%p%% load, remaining time %t)"))
88 "Control string formatting the string to display in the echo area.
89 Ordinary characters in the control string are printed as-is, while
90 conversion specifications introduced by a `%' character in the control
91 string are substituted as defined by the current value of the variable
92 `battery-status-function'. Here are the ones generally available:
93 %c Current capacity (mAh or mWh)
94 %r Current rate of charge or discharge
95 %B Battery status (verbose)
96 %b Battery status: empty means high, `-' means low,
97 `!' means critical, and `+' means charging
98 %d Temperature (in degrees Celsius)
99 %L AC line status (verbose)
100 %p Battery load percentage
101 %m Remaining time (to charge or discharge) in minutes
102 %h Remaining time (to charge or discharge) in hours
103 %t Remaining time (to charge or discharge) in the form `h:min'"
104 :type '(choice string (const nil))
105 :group 'battery)
107 (defvar battery-mode-line-string nil
108 "String to display in the mode line.")
109 ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
111 (defcustom battery-mode-line-limit 100
112 "Percentage of full battery load below which display battery status"
113 :version "24.1"
114 :type 'integer
115 :group 'battery)
117 (defcustom battery-mode-line-format
118 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
119 "[%b%p%%,%d°C]")
120 (battery-status-function
121 "[%b%p%%]"))
122 "Control string formatting the string to display in the mode line.
123 Ordinary characters in the control string are printed as-is, while
124 conversion specifications introduced by a `%' character in the control
125 string are substituted as defined by the current value of the variable
126 `battery-status-function'. Here are the ones generally available:
127 %c Current capacity (mAh or mWh)
128 %r Current rate of charge or discharge
129 %B Battery status (verbose)
130 %b Battery status: empty means high, `-' means low,
131 `!' means critical, and `+' means charging
132 %d Temperature (in degrees Celsius)
133 %L AC line status (verbose)
134 %p Battery load percentage
135 %m Remaining time (to charge or discharge) in minutes
136 %h Remaining time (to charge or discharge) in hours
137 %t Remaining time (to charge or discharge) in the form `h:min'"
138 :type '(choice string (const nil))
139 :group 'battery)
141 (defcustom battery-update-interval 60
142 "Seconds after which the battery status will be updated."
143 :type 'integer
144 :group 'battery)
146 (defcustom battery-load-low 25
147 "Upper bound of low battery load percentage.
148 A battery load percentage below this number is considered low."
149 :type 'integer
150 :group 'battery)
152 (defcustom battery-load-critical 10
153 "Upper bound of critical battery load percentage.
154 A battery load percentage below this number is considered critical."
155 :type 'integer
156 :group 'battery)
158 (defvar battery-update-timer nil
159 "Interval timer object.")
161 ;;;###autoload
162 (defun battery ()
163 "Display battery status information in the echo area.
164 The text being displayed in the echo area is controlled by the variables
165 `battery-echo-area-format' and `battery-status-function'."
166 (interactive)
167 (message "%s" (if (and battery-echo-area-format battery-status-function)
168 (battery-format battery-echo-area-format
169 (funcall battery-status-function))
170 "Battery status not available")))
172 ;;;###autoload
173 (define-minor-mode display-battery-mode
174 "Toggle battery status display in mode line (Display Battery mode).
175 With a prefix argument ARG, enable Display Battery mode if ARG is
176 positive, and disable it otherwise. If called from Lisp, enable
177 the mode if ARG is omitted or nil.
179 The text displayed in the mode line is controlled by
180 `battery-mode-line-format' and `battery-status-function'.
181 The mode line is be updated every `battery-update-interval'
182 seconds."
183 :global t :group 'battery
184 (setq battery-mode-line-string "")
185 (or global-mode-string (setq global-mode-string '("")))
186 (and battery-update-timer (cancel-timer battery-update-timer))
187 (if (and battery-status-function battery-mode-line-format)
188 (if (not display-battery-mode)
189 (setq global-mode-string
190 (delq 'battery-mode-line-string global-mode-string))
191 (add-to-list 'global-mode-string 'battery-mode-line-string t)
192 (setq battery-update-timer (run-at-time nil battery-update-interval
193 'battery-update-handler))
194 (battery-update))
195 (message "Battery status not available")
196 (setq display-battery-mode nil)))
198 (defun battery-update-handler ()
199 (battery-update)
200 (sit-for 0))
202 (defun battery-update ()
203 "Update battery status information in the mode line."
204 (let ((data (and battery-status-function (funcall battery-status-function))))
205 (setq battery-mode-line-string
206 (propertize (if (and battery-mode-line-format
207 (<= (car (read-from-string (cdr (assq ?p data))))
208 battery-mode-line-limit))
209 (battery-format
210 battery-mode-line-format
211 data)
213 'face
214 (and (<= (car (read-from-string (cdr (assq ?p data))))
215 battery-load-critical)
216 'error)
217 'help-echo "Battery status information")))
218 (force-mode-line-update))
220 ;;; `/proc/apm' interface for Linux.
222 (defconst battery-linux-proc-apm-regexp
223 (concat "^\\([^ ]+\\)" ; Driver version.
224 " \\([^ ]+\\)" ; APM BIOS version.
225 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
226 " 0x\\([0-9a-f]+\\)" ; AC line status.
227 " 0x\\([0-9a-f]+\\)" ; Battery status.
228 " 0x\\([0-9a-f]+\\)" ; Battery flags.
229 " \\(-?[0-9]+\\)%" ; Load percentage.
230 " \\(-?[0-9]+\\)" ; Remaining time.
231 " \\(.*\\)" ; Time unit.
232 "$")
233 "Regular expression matching contents of `/proc/apm'.")
235 (defun battery-linux-proc-apm ()
236 "Get APM status information from Linux (the kernel).
237 This function works only with the new `/proc/apm' format introduced
238 in Linux version 1.3.58.
240 The following %-sequences are provided:
241 %v Linux driver version
242 %V APM BIOS version
243 %I APM BIOS status (verbose)
244 %L AC line status (verbose)
245 %B Battery status (verbose)
246 %b Battery status, empty means high, `-' means low,
247 `!' means critical, and `+' means charging
248 %p Battery load percentage
249 %s Remaining time (to charge or discharge) in seconds
250 %m Remaining time (to charge or discharge) in minutes
251 %h Remaining time (to charge or discharge) in hours
252 %t Remaining time (to charge or discharge) in the form `h:min'"
253 (let (driver-version bios-version bios-interface line-status
254 battery-status battery-status-symbol load-percentage
255 seconds minutes hours remaining-time tem)
256 (with-temp-buffer
257 (ignore-errors (insert-file-contents "/proc/apm"))
258 (when (re-search-forward battery-linux-proc-apm-regexp)
259 (setq driver-version (match-string 1))
260 (setq bios-version (match-string 2))
261 (setq tem (string-to-number (match-string 3) 16))
262 (if (not (logand tem 2))
263 (setq bios-interface "not supported")
264 (setq bios-interface "enabled")
265 (cond ((logand tem 16) (setq bios-interface "disabled"))
266 ((logand tem 32) (setq bios-interface "disengaged")))
267 (setq tem (string-to-number (match-string 4) 16))
268 (cond ((= tem 0) (setq line-status "off-line"))
269 ((= tem 1) (setq line-status "on-line"))
270 ((= tem 2) (setq line-status "on backup")))
271 (setq tem (string-to-number (match-string 6) 16))
272 (if (= tem 255)
273 (setq battery-status "N/A")
274 (setq tem (string-to-number (match-string 5) 16))
275 (cond ((= tem 0) (setq battery-status "high"
276 battery-status-symbol ""))
277 ((= tem 1) (setq battery-status "low"
278 battery-status-symbol "-"))
279 ((= tem 2) (setq battery-status "critical"
280 battery-status-symbol "!"))
281 ((= tem 3) (setq battery-status "charging"
282 battery-status-symbol "+")))
283 (setq load-percentage (match-string 7))
284 (setq seconds (string-to-number (match-string 8)))
285 (and (string-equal (match-string 9) "min")
286 (setq seconds (* 60 seconds)))
287 (setq minutes (/ seconds 60)
288 hours (/ seconds 3600))
289 (setq remaining-time
290 (format "%d:%02d" hours (- minutes (* 60 hours))))))))
291 (list (cons ?v (or driver-version "N/A"))
292 (cons ?V (or bios-version "N/A"))
293 (cons ?I (or bios-interface "N/A"))
294 (cons ?L (or line-status "N/A"))
295 (cons ?B (or battery-status "N/A"))
296 (cons ?b (or battery-status-symbol ""))
297 (cons ?p (or load-percentage "N/A"))
298 (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
299 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
300 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
301 (cons ?t (or remaining-time "N/A")))))
304 ;;; `/proc/acpi/' interface for Linux.
306 (defun battery-linux-proc-acpi ()
307 "Get ACPI status information from Linux (the kernel).
308 This function works only with the `/proc/acpi/' format introduced
309 in Linux version 2.4.20 and 2.6.0.
311 The following %-sequences are provided:
312 %c Current capacity (mAh)
313 %r Current rate
314 %B Battery status (verbose)
315 %b Battery status, empty means high, `-' means low,
316 `!' means critical, and `+' means charging
317 %d Temperature (in degrees Celsius)
318 %L AC line status (verbose)
319 %p Battery load percentage
320 %m Remaining time (to charge or discharge) in minutes
321 %h Remaining time (to charge or discharge) in hours
322 %t Remaining time (to charge or discharge) in the form `h:min'"
323 (let ((design-capacity 0)
324 (last-full-capacity 0)
325 full-capacity
326 (warn 0)
327 (low 0)
328 capacity rate rate-type charging-state minutes hours)
329 ;; ACPI provides information about each battery present in the system in
330 ;; a separate subdirectory. We are going to merge the available
331 ;; information together since displaying for a variable amount of
332 ;; batteries seems overkill for format-strings.
333 (with-temp-buffer
334 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
335 t "\\`[^.]")))
336 (erase-buffer)
337 (ignore-errors (insert-file-contents (expand-file-name "state" dir)))
338 (when (re-search-forward "present: +yes$" nil t)
339 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
340 (member charging-state '("unknown" "charged" nil))
341 ;; On most multi-battery systems, most of the time only one
342 ;; battery is "charging"/"discharging", the others are
343 ;; "unknown".
344 (setq charging-state (match-string 1)))
345 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
346 nil t)
347 (setq rate (+ (or rate 0) (string-to-number (match-string 1)))
348 rate-type (or (and rate-type
349 (if (string= rate-type (match-string 2))
350 rate-type
351 (error
352 "Inconsistent rate types (%s vs. %s)"
353 rate-type (match-string 2))))
354 (match-string 2))))
355 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
356 nil t)
357 (setq capacity
358 (+ (or capacity 0) (string-to-number (match-string 1))))))
359 (goto-char (point-max))
360 (ignore-errors (insert-file-contents (expand-file-name "info" dir)))
361 (when (re-search-forward "present: +yes$" nil t)
362 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
363 nil t)
364 (incf design-capacity (string-to-number (match-string 1))))
365 (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
366 nil t)
367 (incf last-full-capacity (string-to-number (match-string 1))))
368 (when (re-search-forward
369 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
370 (incf warn (string-to-number (match-string 1))))
371 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
372 nil t)
373 (incf low (string-to-number (match-string 1)))))))
374 (setq full-capacity (if (> last-full-capacity 0)
375 last-full-capacity design-capacity))
376 (and capacity rate
377 (setq minutes (if (zerop rate) 0
378 (floor (* (/ (float (if (string= charging-state
379 "charging")
380 (- full-capacity capacity)
381 capacity))
382 rate)
383 60)))
384 hours (/ minutes 60)))
385 (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
386 (cons ?L (or (battery-search-for-one-match-in-files
387 (mapcar (lambda (e) (concat e "/state"))
388 (ignore-errors
389 (directory-files "/proc/acpi/ac_adapter/"
390 t "\\`[^.]")))
391 "state: +\\(.*\\)$" 1)
393 "N/A"))
394 (cons ?d (or (battery-search-for-one-match-in-files
395 (mapcar (lambda (e) (concat e "/temperature"))
396 (ignore-errors
397 (directory-files "/proc/acpi/thermal_zone/"
398 t "\\`[^.]")))
399 "temperature: +\\([0-9]+\\) C$" 1)
401 "N/A"))
402 (cons ?r (or (and rate (concat (number-to-string rate) " "
403 rate-type)) "N/A"))
404 (cons ?B (or charging-state "N/A"))
405 (cons ?b (or (and (string= charging-state "charging") "+")
406 (and capacity (< capacity low) "!")
407 (and capacity (< capacity warn) "-")
408 ""))
409 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
410 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
411 (cons ?t (or (and minutes
412 (format "%d:%02d" hours (- minutes (* 60 hours))))
413 "N/A"))
414 (cons ?p (or (and full-capacity capacity
415 (> full-capacity 0)
416 (number-to-string
417 (floor (/ capacity
418 (/ (float full-capacity) 100)))))
419 "N/A")))))
422 ;;; `/sys/class/power_supply/BATN' interface for Linux.
424 (defun battery-linux-sysfs ()
425 "Get ACPI status information from Linux kernel.
426 This function works only with the new `/sys/class/power_supply/'
427 format introduced in Linux version 2.4.25.
429 The following %-sequences are provided:
430 %c Current capacity (mAh or mWh)
431 %r Current rate
432 %B Battery status (verbose)
433 %d Temperature (in degrees Celsius)
434 %p Battery load percentage
435 %L AC line status (verbose)
436 %m Remaining time (to charge or discharge) in minutes
437 %h Remaining time (to charge or discharge) in hours
438 %t Remaining time (to charge or discharge) in the form `h:min'"
439 (let (charging-state rate temperature hours
440 (charge-full 0.0)
441 (charge-now 0.0)
442 (energy-full 0.0)
443 (energy-now 0.0))
444 ;; SysFS provides information about each battery present in the
445 ;; system in a separate subdirectory. We are going to merge the
446 ;; available information together.
447 (with-temp-buffer
448 (dolist (dir (ignore-errors
449 (directory-files
450 "/sys/class/power_supply/" t "BAT[0-9]$")))
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 ((> charge-full 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")))))
528 (defun battery-yeeloong-sysfs ()
529 "Get ACPI status information from Linux (the kernel).
530 This function works only on the Lemote Yeeloong.
532 The following %-sequences are provided:
533 %c Current capacity (mAh)
534 %r Current rate
535 %B Battery status (verbose)
536 %b Battery status, empty means high, `-' means low,
537 `!' means critical, and `+' means charging
538 %L AC line status (verbose)
539 %p Battery load percentage
540 %m Remaining time (to charge or discharge) in minutes
541 %h Remaining time (to charge or discharge) in hours
542 %t Remaining time (to charge or discharge) in the form `h:min'"
544 (let (capacity
545 capacity-level
546 status
547 ac-online
548 hours
549 current-now
550 charge-full
551 charge-now)
553 (with-temp-buffer
554 (ignore-errors
555 (insert-file-contents "/sys/class/power_supply/yeeloong-bat/uevent")
556 (goto-char 1)
557 (search-forward "POWER_SUPPLY_CHARGE_NOW=")
558 (setq charge-now (read (current-buffer)))
559 (goto-char 1)
560 (search-forward "POWER_SUPPLY_CHARGE_FULL=")
561 (setq charge-full (read (current-buffer)))
562 (goto-char 1)
563 (search-forward "POWER_SUPPLY_CURRENT_NOW=")
564 (setq current-now (read (current-buffer)))
565 (goto-char 1)
566 (search-forward "POWER_SUPPLY_CAPACITY_LEVEL=")
567 (setq capacity-level (buffer-substring (point) (line-end-position)))
568 (goto-char 1)
569 (search-forward "POWER_SUPPLY_STATUS=")
570 (setq status (buffer-substring (point) (line-end-position))))
572 (erase-buffer)
573 (ignore-errors
574 (insert-file-contents
575 "/sys/class/power_supply/yeeloong-ac/online")
576 (goto-char 1)
577 (setq ac-online (read (current-buffer)))
578 (erase-buffer)))
581 (setq capacity (round (/ (* charge-now 100.0) charge-full)))
582 (when (and current-now (not (= current-now 0)))
583 (if (< current-now 0)
584 ;; Charging
585 (setq hours (/ (- charge-now charge-full) (+ 0.0 current-now)))
586 ;; Discharging
587 (setq hours (/ charge-now (+ 0.0 current-now)))))
589 (list (cons ?c (if charge-now
590 (number-to-string charge-now)
591 "N/A"))
592 (cons ?r current-now)
593 (cons ?B (cond ((equal capacity-level "Full") "full")
594 ((equal status "Charging") "charging")
595 ((equal capacity-level "Low") "low")
596 ((equal capacity-level "Critical") "critical")
597 (t "high")))
598 (cons ?b (cond ((equal capacity-level "Full") " ")
599 ((equal status "Charging") "+")
600 ((equal capacity-level "Low") "-")
601 ((equal capacity-level "Critical") "!")
602 (t " ")))
603 (cons ?h (if hours (number-to-string hours) "N/A"))
604 (cons ?m (if hours (number-to-string (* 60 hours)) "N/A"))
605 (cons ?t (if hours
606 (format "%d:%d"
607 (/ (round (* 60 hours)) 60)
608 (% (round (* 60 hours)) 60))
609 "N/A"))
610 (cons ?p (if capacity (number-to-string capacity) "N/A"))
611 (cons ?L (if (eq ac-online 1) "AC" "BAT")))))
613 ;;; `pmset' interface for Darwin (OS X).
615 (defun battery-pmset ()
616 "Get battery status information using `pmset'.
618 The following %-sequences are provided:
619 %L Power source (verbose)
620 %B Battery status (verbose)
621 %b Battery status, empty means high, `-' means low,
622 `!' means critical, and `+' means charging
623 %p Battery load percentage
624 %h Remaining time in hours
625 %m Remaining time in minutes
626 %t Remaining time in the form `h:min'"
627 (let (power-source load-percentage battery-status battery-status-symbol
628 remaining-time hours minutes)
629 (with-temp-buffer
630 (ignore-errors (call-process "pmset" nil t nil "-g" "ps"))
631 (goto-char (point-min))
632 (when (re-search-forward "Currentl?y drawing from '\\(AC\\|Battery\\) Power'" nil t)
633 (setq power-source (match-string 1))
634 (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t)
635 (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
636 (setq load-percentage (match-string 1))
637 (goto-char (match-end 0))
638 (cond ((looking-at "; charging")
639 (setq battery-status "charging"
640 battery-status-symbol "+"))
641 ((< (string-to-number load-percentage) battery-load-low)
642 (setq battery-status "low"
643 battery-status-symbol "-"))
644 ((< (string-to-number load-percentage) battery-load-critical)
645 (setq battery-status "critical"
646 battery-status-symbol "!"))
648 (setq battery-status "high"
649 battery-status-symbol "")))
650 (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t)
651 (setq remaining-time (match-string 1))
652 (let ((h (string-to-number (match-string 2)))
653 (m (string-to-number (match-string 3))))
654 (setq hours (number-to-string (+ h (if (< m 30) 0 1)))
655 minutes (number-to-string (+ (* h 60) m)))))))))
656 (list (cons ?L (or power-source "N/A"))
657 (cons ?p (or load-percentage "N/A"))
658 (cons ?B (or battery-status "N/A"))
659 (cons ?b (or battery-status-symbol ""))
660 (cons ?h (or hours "N/A"))
661 (cons ?m (or minutes "N/A"))
662 (cons ?t (or remaining-time "N/A")))))
665 ;;; Private functions.
667 (defun battery-format (format alist)
668 "Substitute %-sequences in FORMAT."
669 (replace-regexp-in-string
670 "%."
671 (lambda (str)
672 (let ((char (aref str 1)))
673 (if (eq char ?%) "%"
674 (or (cdr (assoc char alist)) ""))))
675 format t t))
677 (defun battery-search-for-one-match-in-files (files regexp match-num)
678 "Search REGEXP in the content of the files listed in FILES.
679 If a match occurred, return the parenthesized expression numbered by
680 MATCH-NUM in the match. Otherwise, return nil."
681 (with-temp-buffer
682 (catch 'found
683 (dolist (file files)
684 (and (ignore-errors (insert-file-contents file nil nil nil 'replace))
685 (re-search-forward regexp nil t)
686 (throw 'found (match-string match-num)))))))
689 (provide 'battery)
691 ;;; battery.el ends here