annotate lisp/battery.el @ 107305:0b830883578a

Fix bug #5649: 23.1.92; Indentation problems in C mode.
author Alan Mackenzie <acm@muc.de>
date Mon, 01 Mar 2010 11:31:42 +0000
parents 1d1d5d9bd884
children 376148b31b5e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
93207
ba8704a0f1e5 Add file cookie coding:iso-8859-1 for the degree sign
Juri Linkov <juri@jurta.org>
parents: 79721
diff changeset
1 ;;; battery.el --- display battery status information -*- coding: iso-8859-1 -*-
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
64762
41bb365f41c4 Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 64094
diff changeset
3 ;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003, 2004,
106815
1d1d5d9bd884 Add 2010 to copyright years.
Glenn Morris <rgm@gnu.org>
parents: 100908
diff changeset
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5
30110
23cb074f9d88 Change author's mail address.
Gerd Moellmann <gerd@gnu.org>
parents: 26828
diff changeset
6 ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
20972
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
7 ;; Keywords: hardware
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8
18685
4f3350c88e6c Fix copyright and copying permissions.
Richard M. Stallman <rms@gnu.org>
parents: 18388
diff changeset
9 ;; This file is part of GNU Emacs.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 94449
diff changeset
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 94449
diff changeset
13 ;; the Free Software Foundation, either version 3 of the License, or
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 94449
diff changeset
14 ;; (at your option) any later version.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15
18685
4f3350c88e6c Fix copyright and copying permissions.
Richard M. Stallman <rms@gnu.org>
parents: 18388
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19 ;; GNU General Public License for more details.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 94449
diff changeset
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24 ;;; Commentary:
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25
94449
a96dda54aef3 (battery-status-function): Use w32-battery-status on Windows.
Jason Rumney <jasonr@gnu.org>
parents: 93207
diff changeset
26 ;; There is at present support for GNU/Linux, OS X and Windows. This
a96dda54aef3 (battery-status-function): Use w32-battery-status on Windows.
Jason Rumney <jasonr@gnu.org>
parents: 93207
diff changeset
27 ;; library supports both the `/proc/apm' file format of Linux version
a96dda54aef3 (battery-status-function): Use w32-battery-status on Windows.
Jason Rumney <jasonr@gnu.org>
parents: 93207
diff changeset
28 ;; 1.3.58 or newer and the `/proc/acpi/' directory structure of Linux
a96dda54aef3 (battery-status-function): Use w32-battery-status on Windows.
Jason Rumney <jasonr@gnu.org>
parents: 93207
diff changeset
29 ;; 2.4.20 and 2.6. Darwin (OS X) is supported by using the `pmset'
a96dda54aef3 (battery-status-function): Use w32-battery-status on Windows.
Jason Rumney <jasonr@gnu.org>
parents: 93207
diff changeset
30 ;; program. Windows is supported by the GetSystemPowerStatus API call.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32 ;;; Code:
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34 (require 'timer)
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
35 (eval-when-compile (require 'cl))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
38 (defgroup battery nil
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
39 "Display battery status information."
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
40 :prefix "battery-"
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
41 :group 'hardware)
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
42
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
43 (defcustom battery-status-function
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 (cond ((and (eq system-type 'gnu/linux)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 (file-readable-p "/proc/apm"))
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
46 'battery-linux-proc-apm)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
47 ((and (eq system-type 'gnu/linux)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
48 (file-directory-p "/proc/acpi/battery"))
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
49 'battery-linux-proc-acpi)
97362
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
50 ((and (eq system-type 'gnu/linux)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
51 (file-directory-p "/sys/class/power_supply/")
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
52 (directory-files "/sys/class/power_supply/" nil "BAT[0-9]$"))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
53 'battery-linux-sysfs)
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
54 ((and (eq system-type 'darwin)
73700
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
55 (condition-case nil
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
56 (with-temp-buffer
64907
08b55dc9bcd1 (battery-status-function): Don't use ignore-errors.
Lute Kamstra <lute@gnu.org>
parents: 64762
diff changeset
57 (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
08b55dc9bcd1 (battery-status-function): Don't use ignore-errors.
Lute Kamstra <lute@gnu.org>
parents: 64762
diff changeset
58 (> (buffer-size) 0)))
08b55dc9bcd1 (battery-status-function): Don't use ignore-errors.
Lute Kamstra <lute@gnu.org>
parents: 64762
diff changeset
59 (error nil)))
94449
a96dda54aef3 (battery-status-function): Use w32-battery-status on Windows.
Jason Rumney <jasonr@gnu.org>
parents: 93207
diff changeset
60 'battery-pmset)
a96dda54aef3 (battery-status-function): Use w32-battery-status on Windows.
Jason Rumney <jasonr@gnu.org>
parents: 93207
diff changeset
61 ((eq system-type 'windows-nt)
a96dda54aef3 (battery-status-function): Use w32-battery-status on Windows.
Jason Rumney <jasonr@gnu.org>
parents: 93207
diff changeset
62 'w32-battery-status))
100171
d42aff5ca541 * align.el:
Lute Kamstra <lute@gnu.org>
parents: 97362
diff changeset
63 "Function for getting battery status information.
36090
9ed7c3f74e35 (battery-status-function): Fix doc, :type.
Dave Love <fx@gnu.org>
parents: 32356
diff changeset
64 The function has to return an alist of conversion definitions.
9ed7c3f74e35 (battery-status-function): Fix doc, :type.
Dave Love <fx@gnu.org>
parents: 32356
diff changeset
65 Its cons cells are of the form
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 (CONVERSION . REPLACEMENT-TEXT)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 CONVERSION is the character code of a \"conversion specification\"
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
70 introduced by a `%' character in a control string."
36090
9ed7c3f74e35 (battery-status-function): Fix doc, :type.
Dave Love <fx@gnu.org>
parents: 32356
diff changeset
71 :type '(choice (const nil) function)
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
72 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
74 (defcustom battery-echo-area-format
94449
a96dda54aef3 (battery-status-function): Use w32-battery-status on Windows.
Jason Rumney <jasonr@gnu.org>
parents: 93207
diff changeset
75 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
76 "Power %L, battery %B at %r (%p%% load, remaining time %t)")
97362
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
77 ((eq battery-status-function 'battery-linux-sysfs)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
78 "Power %L, battery %B (%p%% load)")
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
79 ((eq battery-status-function 'battery-pmset)
94449
a96dda54aef3 (battery-status-function): Use w32-battery-status on Windows.
Jason Rumney <jasonr@gnu.org>
parents: 93207
diff changeset
80 "%L power, battery %B (%p%% load, remaining time %t)")
a96dda54aef3 (battery-status-function): Use w32-battery-status on Windows.
Jason Rumney <jasonr@gnu.org>
parents: 93207
diff changeset
81 (battery-status-function
a96dda54aef3 (battery-status-function): Use w32-battery-status on Windows.
Jason Rumney <jasonr@gnu.org>
parents: 93207
diff changeset
82 "Power %L, battery %B (%p%% load, remaining time %t)"))
100171
d42aff5ca541 * align.el:
Lute Kamstra <lute@gnu.org>
parents: 97362
diff changeset
83 "Control string formatting the string to display in the echo area.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84 Ordinary characters in the control string are printed as-is, while
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85 conversion specifications introduced by a `%' character in the control
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86 string are substituted as defined by the current value of the variable
69381
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
87 `battery-status-function'. Here are the ones generally available:
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
88 %c Current capacity (mAh or mWh)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
89 %r Current rate of charge or discharge
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
90 %B Battery status (verbose)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
91 %b Battery status: empty means high, `-' means low,
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
92 `!' means critical, and `+' means charging
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
93 %d Temperature (in degrees Celsius)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
94 %L AC line status (verbose)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
95 %p Battery load percentage
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
96 %m Remaining time (to charge or discharge) in minutes
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
97 %h Remaining time (to charge or discharge) in hours
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
98 %t Remaining time (to charge or discharge) in the form `h:min'"
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
99 :type '(choice string (const nil))
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
100 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 (defvar battery-mode-line-string nil
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103 "String to display in the mode line.")
59132
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
104 ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
106 (defcustom battery-mode-line-format
94449
a96dda54aef3 (battery-status-function): Use w32-battery-status on Windows.
Jason Rumney <jasonr@gnu.org>
parents: 93207
diff changeset
107 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
108 "[%b%p%%,%d°C]")
94449
a96dda54aef3 (battery-status-function): Use w32-battery-status on Windows.
Jason Rumney <jasonr@gnu.org>
parents: 93207
diff changeset
109 (battery-status-function
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
110 "[%b%p%%]"))
100171
d42aff5ca541 * align.el:
Lute Kamstra <lute@gnu.org>
parents: 97362
diff changeset
111 "Control string formatting the string to display in the mode line.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 Ordinary characters in the control string are printed as-is, while
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 conversion specifications introduced by a `%' character in the control
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 string are substituted as defined by the current value of the variable
69381
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
115 `battery-status-function'. Here are the ones generally available:
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
116 %c Current capacity (mAh or mWh)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
117 %r Current rate of charge or discharge
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
118 %B Battery status (verbose)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
119 %b Battery status: empty means high, `-' means low,
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
120 `!' means critical, and `+' means charging
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
121 %d Temperature (in degrees Celsius)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
122 %L AC line status (verbose)
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
123 %p Battery load percentage
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
124 %m Remaining time (to charge or discharge) in minutes
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
125 %h Remaining time (to charge or discharge) in hours
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
126 %t Remaining time (to charge or discharge) in the form `h:min'"
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
127 :type '(choice string (const nil))
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
128 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
130 (defcustom battery-update-interval 60
100171
d42aff5ca541 * align.el:
Lute Kamstra <lute@gnu.org>
parents: 97362
diff changeset
131 "Seconds after which the battery status will be updated."
21088
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
132 :type 'integer
ac1673121774 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 21076
diff changeset
133 :group 'battery)
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
134
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
135 (defcustom battery-load-low 25
100171
d42aff5ca541 * align.el:
Lute Kamstra <lute@gnu.org>
parents: 97362
diff changeset
136 "Upper bound of low battery load percentage.
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
137 A battery load percentage below this number is considered low."
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
138 :type 'integer
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
139 :group 'battery)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
140
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
141 (defcustom battery-load-critical 10
100171
d42aff5ca541 * align.el:
Lute Kamstra <lute@gnu.org>
parents: 97362
diff changeset
142 "Upper bound of critical battery load percentage.
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
143 A battery load percentage below this number is considered critical."
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
144 :type 'integer
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
145 :group 'battery)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
146
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 (defvar battery-update-timer nil
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 "Interval timer object.")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149
21076
dc82196eac58 Fixed spelling of `autoload' magic cookies.
Richard M. Stallman <rms@gnu.org>
parents: 20972
diff changeset
150 ;;;###autoload
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 (defun battery ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 "Display battery status information in the echo area.
26828
983f253505d0 (battery): Doc fix.
Gerd Moellmann <gerd@gnu.org>
parents: 21317
diff changeset
153 The text being displayed in the echo area is controlled by the variables
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 `battery-echo-area-format' and `battery-status-function'."
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 (interactive)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 (message "%s" (if (and battery-echo-area-format battery-status-function)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 (battery-format battery-echo-area-format
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 (funcall battery-status-function))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 "Battery status not available")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160
21076
dc82196eac58 Fixed spelling of `autoload' magic cookies.
Richard M. Stallman <rms@gnu.org>
parents: 20972
diff changeset
161 ;;;###autoload
59363
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
162 (define-minor-mode display-battery-mode
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163 "Display battery status information in the mode line.
32346
fa3f5e8f6201 (display-battery): Doc spelling fix.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 30110
diff changeset
164 The text being displayed in the mode line is controlled by the variables
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 `battery-mode-line-format' and `battery-status-function'.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 The mode line will be updated automatically every `battery-update-interval'
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 seconds."
61268
abd0d265b4ba (display-battery-mode): Specify :group.
Lute Kamstra <lute@gnu.org>
parents: 59363
diff changeset
168 :global t :group 'battery
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 (setq battery-mode-line-string "")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 (or global-mode-string (setq global-mode-string '("")))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 (and battery-update-timer (cancel-timer battery-update-timer))
59363
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
172 (if (not display-battery-mode)
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
173 (setq global-mode-string
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
174 (delq 'battery-mode-line-string global-mode-string))
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
175 (add-to-list 'global-mode-string 'battery-mode-line-string t)
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
176 (setq battery-update-timer (run-at-time nil battery-update-interval
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
177 'battery-update-handler))
9120644ff426 (display-battery-mode): Rename from display-battery.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59132
diff changeset
178 (battery-update)))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 (defun battery-update-handler ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 (battery-update)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 (sit-for 0))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 (defun battery-update ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 "Update battery status information in the mode line."
59132
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
186 (setq battery-mode-line-string
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
187 (propertize (if (and battery-mode-line-format
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
188 battery-status-function)
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
189 (battery-format
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
190 battery-mode-line-format
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
191 (funcall battery-status-function))
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
192 "")
38f1d5865861 (battery-mode-line-format): Remove initial spaces.
Richard M. Stallman <rms@gnu.org>
parents: 57143
diff changeset
193 'help-echo "Battery status information"))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 (force-mode-line-update))
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 ;;; `/proc/apm' interface for Linux.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 (defconst battery-linux-proc-apm-regexp
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200 (concat "^\\([^ ]+\\)" ; Driver version.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 " \\([^ ]+\\)" ; APM BIOS version.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 " 0x\\([0-9a-f]+\\)" ; AC line status.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 " 0x\\([0-9a-f]+\\)" ; Battery status.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 " 0x\\([0-9a-f]+\\)" ; Battery flags.
20972
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
206 " \\(-?[0-9]+\\)%" ; Load percentage.
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
207 " \\(-?[0-9]+\\)" ; Remaining time.
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 " \\(.*\\)" ; Time unit.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 "$")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 "Regular expression matching contents of `/proc/apm'.")
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 (defun battery-linux-proc-apm ()
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 "Get APM status information from Linux kernel.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214 This function works only with the new `/proc/apm' format introduced
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215 in Linux version 1.3.58.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 The following %-sequences are provided:
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 %v Linux driver version
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 %V APM BIOS version
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220 %I APM BIOS status (verbose)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221 %L AC line status (verbose)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
222 %B Battery status (verbose)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223 %b Battery status, empty means high, `-' means low,
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224 `!' means critical, and `+' means charging
63899
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
225 %p Battery load percentage
69381
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
226 %s Remaining time (to charge or discharge) in seconds
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
227 %m Remaining time (to charge or discharge) in minutes
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
228 %h Remaining time (to charge or discharge) in hours
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
229 %t Remaining time (to charge or discharge) in the form `h:min'"
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 (let (driver-version bios-version bios-interface line-status
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 battery-status battery-status-symbol load-percentage
63899
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
232 seconds minutes hours remaining-time tem)
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
233 (with-temp-buffer
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
234 (ignore-errors (insert-file-contents "/proc/apm"))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
235 (when (re-search-forward battery-linux-proc-apm-regexp)
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
236 (setq driver-version (match-string 1))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
237 (setq bios-version (match-string 2))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
238 (setq tem (string-to-number (match-string 3) 16))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
239 (if (not (logand tem 2))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
240 (setq bios-interface "not supported")
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
241 (setq bios-interface "enabled")
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
242 (cond ((logand tem 16) (setq bios-interface "disabled"))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
243 ((logand tem 32) (setq bios-interface "disengaged")))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
244 (setq tem (string-to-number (match-string 4) 16))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
245 (cond ((= tem 0) (setq line-status "off-line"))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
246 ((= tem 1) (setq line-status "on-line"))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
247 ((= tem 2) (setq line-status "on backup")))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
248 (setq tem (string-to-number (match-string 6) 16))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
249 (if (= tem 255)
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
250 (setq battery-status "N/A")
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
251 (setq tem (string-to-number (match-string 5) 16))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
252 (cond ((= tem 0) (setq battery-status "high"
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
253 battery-status-symbol ""))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
254 ((= tem 1) (setq battery-status "low"
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
255 battery-status-symbol "-"))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
256 ((= tem 2) (setq battery-status "critical"
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
257 battery-status-symbol "!"))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
258 ((= tem 3) (setq battery-status "charging"
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
259 battery-status-symbol "+")))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
260 (setq load-percentage (match-string 7))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
261 (setq seconds (string-to-number (match-string 8)))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
262 (and (string-equal (match-string 9) "min")
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
263 (setq seconds (* 60 seconds)))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
264 (setq minutes (/ seconds 60)
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
265 hours (/ seconds 3600))
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
266 (setq remaining-time
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
267 (format "%d:%02d" hours (- minutes (* 60 hours))))))))
20972
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
268 (list (cons ?v (or driver-version "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
269 (cons ?V (or bios-version "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
270 (cons ?I (or bios-interface "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
271 (cons ?L (or line-status "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
272 (cons ?B (or battery-status "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
273 (cons ?b (or battery-status-symbol ""))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
274 (cons ?p (or load-percentage "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
275 (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
276 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
277 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
9dff083ee7a9 (battery-linux-proc-apm-regexp): Load percentage
Karl Heuer <kwzh@gnu.org>
parents: 18685
diff changeset
278 (cons ?t (or remaining-time "N/A")))))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
281 ;;; `/proc/acpi/' interface for Linux.
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
282
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
283 (defun battery-linux-proc-acpi ()
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
284 "Get ACPI status information from Linux kernel.
97362
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
285 This function works only with the `/proc/acpi/' format introduced
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
286 in Linux version 2.4.20 and 2.6.0.
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
287
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
288 The following %-sequences are provided:
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
289 %c Current capacity (mAh)
63899
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
290 %r Current rate
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
291 %B Battery status (verbose)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
292 %b Battery status, empty means high, `-' means low,
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
293 `!' means critical, and `+' means charging
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
294 %d Temperature (in degrees Celsius)
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
295 %L AC line status (verbose)
63899
1f503fd2046d (battery-linux-proc-apm): Fix typo in docstring.
Lute Kamstra <lute@gnu.org>
parents: 61268
diff changeset
296 %p Battery load percentage
69381
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
297 %m Remaining time (to charge or discharge) in minutes
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
298 %h Remaining time (to charge or discharge) in hours
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
299 %t Remaining time (to charge or discharge) in the form `h:min'"
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
300 (let ((design-capacity 0)
68266
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
301 (last-full-capacity 0)
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
302 full-capacity
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
303 (warn 0)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
304 (low 0)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
305 capacity rate rate-type charging-state minutes hours)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
306 ;; ACPI provides information about each battery present in the system in
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
307 ;; a separate subdirectory. We are going to merge the available
57133
fd1a6df8927e Delete superfluous empty lines.
Eli Zaretskii <eliz@gnu.org>
parents: 56663
diff changeset
308 ;; information together since displaying for a variable amount of
fd1a6df8927e Delete superfluous empty lines.
Eli Zaretskii <eliz@gnu.org>
parents: 56663
diff changeset
309 ;; batteries seems overkill for format-strings.
fd1a6df8927e Delete superfluous empty lines.
Eli Zaretskii <eliz@gnu.org>
parents: 56663
diff changeset
310 (with-temp-buffer
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
311 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
312 t "\\`[^.]")))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
313 (erase-buffer)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
314 (ignore-errors (insert-file-contents (expand-file-name "state" dir)))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
315 (when (re-search-forward "present: +yes$" nil t)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
316 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
69381
780bbe0dfb17 (battery-echo-area-format): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 68651
diff changeset
317 (member charging-state '("unknown" "charged" nil))
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
318 ;; On most multi-battery systems, most of the time only one
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
319 ;; battery is "charging"/"discharging", the others are
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
320 ;; "unknown".
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
321 (setq charging-state (match-string 1)))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
322 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
323 nil t)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
324 (setq rate (+ (or rate 0) (string-to-number (match-string 1)))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
325 rate-type (or (and rate-type
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
326 (if (string= rate-type (match-string 2))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
327 rate-type
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
328 (error
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
329 "Inconsistent rate types (%s vs. %s)"
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
330 rate-type (match-string 2))))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
331 (match-string 2))))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
332 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
333 nil t)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
334 (setq capacity
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
335 (+ (or capacity 0) (string-to-number (match-string 1))))))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
336 (goto-char (point-max))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
337 (ignore-errors (insert-file-contents (expand-file-name "info" dir)))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
338 (when (re-search-forward "present: +yes$" nil t)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
339 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
340 nil t)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
341 (incf design-capacity (string-to-number (match-string 1))))
68266
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
342 (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
343 nil t)
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
344 (incf last-full-capacity (string-to-number (match-string 1))))
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
345 (when (re-search-forward
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
346 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
347 (incf warn (string-to-number (match-string 1))))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
348 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
349 nil t)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
350 (incf low (string-to-number (match-string 1)))))))
68266
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
351 (setq full-capacity (if (> last-full-capacity 0)
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
352 last-full-capacity design-capacity))
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
353 (and capacity rate
55904
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
354 (setq minutes (if (zerop rate) 0
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
355 (floor (* (/ (float (if (string= charging-state
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
356 "charging")
68266
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
357 (- full-capacity capacity)
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
358 capacity))
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
359 rate)
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
360 60)))
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
361 hours (/ minutes 60)))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
362 (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
73700
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
363 (cons ?L (or (battery-search-for-one-match-in-files
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
364 (mapcar (lambda (e) (concat e "/state"))
75178
b01847d9a51b (battery-linux-proc-acpi): Use ignore-errors around calls to directory-files.
Richard M. Stallman <rms@gnu.org>
parents: 75139
diff changeset
365 (ignore-errors
b01847d9a51b (battery-linux-proc-acpi): Use ignore-errors around calls to directory-files.
Richard M. Stallman <rms@gnu.org>
parents: 75139
diff changeset
366 (directory-files "/proc/acpi/ac_adapter/"
b01847d9a51b (battery-linux-proc-acpi): Use ignore-errors around calls to directory-files.
Richard M. Stallman <rms@gnu.org>
parents: 75139
diff changeset
367 t "\\`[^.]")))
73700
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
368 "state: +\\(.*\\)$" 1)
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
369
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
370 "N/A"))
73700
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
371 (cons ?d (or (battery-search-for-one-match-in-files
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
372 (mapcar (lambda (e) (concat e "/temperature"))
75178
b01847d9a51b (battery-linux-proc-acpi): Use ignore-errors around calls to directory-files.
Richard M. Stallman <rms@gnu.org>
parents: 75139
diff changeset
373 (ignore-errors
b01847d9a51b (battery-linux-proc-acpi): Use ignore-errors around calls to directory-files.
Richard M. Stallman <rms@gnu.org>
parents: 75139
diff changeset
374 (directory-files "/proc/acpi/thermal_zone/"
b01847d9a51b (battery-linux-proc-acpi): Use ignore-errors around calls to directory-files.
Richard M. Stallman <rms@gnu.org>
parents: 75139
diff changeset
375 t "\\`[^.]")))
73700
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
376 "temperature: +\\([0-9]+\\) C$" 1)
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
377
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
378 "N/A"))
55904
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
379 (cons ?r (or (and rate (concat (number-to-string rate) " "
f3be7c4dc1e6 (battery-linux-proc-acpi): mA was hardcored, but some
Eli Zaretskii <eliz@gnu.org>
parents: 52470
diff changeset
380 rate-type)) "N/A"))
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
381 (cons ?B (or charging-state "N/A"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
382 (cons ?b (or (and (string= charging-state "charging") "+")
69414
a159957988d5 (battery-linux-proc-acpi): Check `capacity' for non-nil
Juri Linkov <juri@jurta.org>
parents: 69381
diff changeset
383 (and capacity (< capacity low) "!")
a159957988d5 (battery-linux-proc-acpi): Check `capacity' for non-nil
Juri Linkov <juri@jurta.org>
parents: 69381
diff changeset
384 (and capacity (< capacity warn) "-")
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
385 ""))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
386 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
387 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
388 (cons ?t (or (and minutes
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
389 (format "%d:%02d" hours (- minutes (* 60 hours))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
390 "N/A"))
68266
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
391 (cons ?p (or (and full-capacity capacity
73599
65d9fbabd719 * battery.el (battery-linux-proc-acpi): Prevent range error when
Chong Yidong <cyd@stupidchicken.com>
parents: 70539
diff changeset
392 (> full-capacity 0)
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
393 (number-to-string
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
394 (floor (/ capacity
68266
0d67eef96242 (battery-linux-proc-acpi): Handle "last full capacity".
Richard M. Stallman <rms@gnu.org>
parents: 67802
diff changeset
395 (/ (float full-capacity) 100)))))
52450
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
396 "N/A")))))
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
397
d096f00804db (battery-linux-proc-acpi): New function.
Eli Zaretskii <eliz@gnu.org>
parents: 52401
diff changeset
398
97362
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
399 ;;; `/sys/class/power_supply/BATN' interface for Linux.
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
400
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
401 (defun battery-linux-sysfs ()
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
402 "Get ACPI status information from Linux kernel.
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
403 This function works only with the new `/sys/class/power_supply/'
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
404 format introduced in Linux version 2.4.25.
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
405
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
406 The following %-sequences are provided:
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
407 %c Current capacity (mAh or mWh)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
408 %B Battery status (verbose)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
409 %p Battery load percentage
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
410 %L AC line status (verbose)"
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
411 (let (charging-state
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
412 (charge-full 0.0)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
413 (charge-now 0.0)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
414 (energy-full 0.0)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
415 (energy-now 0.0))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
416 ;; SysFS provides information about each battery present in the
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
417 ;; system in a separate subdirectory. We are going to merge the
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
418 ;; available information together.
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
419 (with-temp-buffer
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
420 (dolist (dir (ignore-errors
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
421 (directory-files
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
422 "/sys/class/power_supply/" t "BAT[0-9]$")))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
423 (erase-buffer)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
424 (ignore-errors (insert-file-contents
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
425 (expand-file-name "uevent" dir)))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
426 (when (re-search-forward "POWER_SUPPLY_PRESENT=1$" nil t)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
427 (goto-char (point-min))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
428 (and (re-search-forward "POWER_SUPPLY_STATUS=\\(.*\\)$" nil t)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
429 (member charging-state '("Unknown" "Full" nil))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
430 (setq charging-state (match-string 1)))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
431 (let (full-string now-string)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
432 ;; Sysfs may list either charge (mAh) or energy (mWh).
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
433 ;; Keep track of both, and choose which to report later.
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
434 (cond ((and (re-search-forward
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
435 "POWER_SUPPLY_CHARGE_FULL=\\([0-9]*\\)$" nil t)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
436 (setq full-string (match-string 1))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
437 (re-search-forward
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
438 "POWER_SUPPLY_CHARGE_NOW=\\([0-9]*\\)$" nil t)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
439 (setq now-string (match-string 1)))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
440 (setq charge-full (+ charge-full
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
441 (string-to-number full-string))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
442 charge-now (+ charge-now
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
443 (string-to-number now-string))))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
444 ((and (re-search-forward
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
445 "POWER_SUPPLY_ENERGY_FULL=\\([0-9]*\\)$" nil t)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
446 (setq full-string (match-string 1))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
447 (re-search-forward
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
448 "POWER_SUPPLY_ENERGY_NOW=\\([0-9]*\\)$" nil t)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
449 (setq now-string (match-string 1)))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
450 (setq energy-full (+ energy-full
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
451 (string-to-number full-string))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
452 energy-now (+ energy-now
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
453 (string-to-number now-string)))))))))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
454 (list (cons ?c (cond ((or (> charge-full 0) (> charge-now 0))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
455 (number-to-string charge-now))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
456 ((or (> energy-full 0) (> energy-now 0))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
457 (number-to-string energy-now))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
458 (t "N/A")))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
459 (cons ?B (or charging-state "N/A"))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
460 (cons ?p (cond ((> charge-full 0)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
461 (format "%.1f"
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
462 (/ (* 100 charge-now) charge-full)))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
463 ((> energy-full 0)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
464 (format "%.1f"
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
465 (/ (* 100 energy-now) energy-full)))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
466 (t "N/A")))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
467 (cons ?L (if (file-readable-p "/sys/class/power_supply/AC/online")
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
468 (if (battery-search-for-one-match-in-files
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
469 (list "/sys/class/power_supply/AC/online"
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
470 "/sys/class/power_supply/ACAD/online")
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
471 "1" 0)
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
472 "AC"
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
473 "BAT")
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
474 "N/A")))))
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
475
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
476
a7ddf766669d (battery-echo-area-format, battery-status-function):
Chong Yidong <cyd@stupidchicken.com>
parents: 94678
diff changeset
477
64094
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
478 ;;; `pmset' interface for Darwin (OS X).
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
479
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
480 (defun battery-pmset ()
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
481 "Get battery status information using `pmset'.
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
482
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
483 The following %-sequences are provided:
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
484 %L Power source (verbose)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
485 %B Battery status (verbose)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
486 %b Battery status, empty means high, `-' means low,
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
487 `!' means critical, and `+' means charging
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
488 %p Battery load percentage
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
489 %h Remaining time in hours
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
490 %m Remaining time in minutes
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
491 %t Remaining time in the form `h:min'"
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
492 (let (power-source load-percentage battery-status battery-status-symbol
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
493 remaining-time hours minutes)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
494 (with-temp-buffer
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
495 (ignore-errors (call-process "pmset" nil t nil "-g" "ps"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
496 (goto-char (point-min))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
497 (when (re-search-forward "Currentl?y drawing from '\\(AC\\|Battery\\) Power'" nil t)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
498 (setq power-source (match-string 1))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
499 (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
500 (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
501 (setq load-percentage (match-string 1))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
502 (goto-char (match-end 0))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
503 (cond ((looking-at "; charging")
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
504 (setq battery-status "charging"
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
505 battery-status-symbol "+"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
506 ((< (string-to-number load-percentage) battery-load-low)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
507 (setq battery-status "low"
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
508 battery-status-symbol "-"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
509 ((< (string-to-number load-percentage) battery-load-critical)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
510 (setq battery-status "critical"
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
511 battery-status-symbol "!"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
512 (t
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
513 (setq battery-status "high"
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
514 battery-status-symbol "")))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
515 (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t)
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
516 (setq remaining-time (match-string 1))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
517 (let ((h (string-to-number (match-string 2)))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
518 (m (string-to-number (match-string 3))))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
519 (setq hours (number-to-string (+ h (if (< m 30) 0 1)))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
520 minutes (number-to-string (+ (* h 60) m)))))))))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
521 (list (cons ?L (or power-source "N/A"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
522 (cons ?p (or load-percentage "N/A"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
523 (cons ?B (or battery-status "N/A"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
524 (cons ?b (or battery-status-symbol ""))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
525 (cons ?h (or hours "N/A"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
526 (cons ?m (or minutes "N/A"))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
527 (cons ?t (or remaining-time "N/A")))))
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
528
e960506e0661 Add support for Darwin (with much debugging help from Samuel Lauber
Lute Kamstra <lute@gnu.org>
parents: 64091
diff changeset
529
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
530 ;;; Private functions.
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
531
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
532 (defun battery-format (format alist)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
533 "Substitute %-sequences in FORMAT."
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
534 (replace-regexp-in-string
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
535 "%."
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
536 (lambda (str)
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
537 (let ((char (aref str 1)))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
538 (if (eq char ?%) "%"
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
539 (or (cdr (assoc char alist)) ""))))
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
540 format t t))
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
541
73700
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
542 (defun battery-search-for-one-match-in-files (files regexp match-num)
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
543 "Search REGEXP in the content of the files listed in FILES.
75139
685bae49b575 (battery-search-for-one-match-in-files): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents: 73700
diff changeset
544 If a match occurred, return the parenthesized expression numbered by
73700
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
545 MATCH-NUM in the match. Otherwise, return nil."
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
546 (with-temp-buffer
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
547 (catch 'found
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
548 (dolist (file files)
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
549 (and (ignore-errors (insert-file-contents file nil nil nil 'replace))
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
550 (re-search-forward regexp nil t)
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
551 (throw 'found (match-string match-num)))))))
294b1de9ac5f 2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org>
Romain Francoise <romain@orebokech.com>
parents: 73599
diff changeset
552
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
553
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
554 (provide 'battery)
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
555
57143
b628e08b0230 (battery-linux-proc-apm): Use string-to-number.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 57133
diff changeset
556 ;; arch-tag: 65916f50-4754-4b6b-ac21-0b510f545a37
18388
7e14277c51f3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
557 ;;; battery.el ends here