Merge from trunk
[bpt/emacs.git] / lisp / battery.el
CommitLineData
d4919479 1;;; battery.el --- display battery status information -*- coding: iso-8859-1 -*-
6b279740 2
acaf905b 3;; Copyright (C) 1997-1998, 2000-2012 Free Software Foundation, Inc.
6b279740 4
3b361901 5;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
d5323ba0 6;; Keywords: hardware
6b279740 7
f12b0b96 8;; This file is part of GNU Emacs.
6b279740 9
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
6b279740 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
6b279740 14
f12b0b96 15;; GNU Emacs is distributed in the hope that it will be useful,
6b279740
RS
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
6b279740
RS
22
23;;; Commentary:
24
20d4381e
JR
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.
6b279740
RS
30
31;;; Code:
32
33(require 'timer)
0f4a15f8 34(eval-when-compile (require 'cl))
6b279740
RS
35
36\f
4bef9110
SE
37(defgroup battery nil
38 "Display battery status information."
39 :prefix "battery-"
40 :group 'hardware)
41
42(defcustom battery-status-function
6b279740
RS
43 (cond ((and (eq system-type 'gnu/linux)
44 (file-readable-p "/proc/apm"))
2b3cb60a
EZ
45 'battery-linux-proc-apm)
46 ((and (eq system-type 'gnu/linux)
47 (file-directory-p "/proc/acpi/battery"))
71d21198 48 'battery-linux-proc-acpi)
3660b4f5
CY
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)
30710442
RS
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)
71d21198 57 ((and (eq system-type 'darwin)
785428c7
RF
58 (condition-case nil
59 (with-temp-buffer
248634e9
LK
60 (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
61 (> (buffer-size) 0)))
62 (error nil)))
20d4381e
JR
63 'battery-pmset)
64 ((eq system-type 'windows-nt)
65 'w32-battery-status))
9201cc28 66 "Function for getting battery status information.
681e6b4b
DL
67The function has to return an alist of conversion definitions.
68Its cons cells are of the form
6b279740
RS
69
70 (CONVERSION . REPLACEMENT-TEXT)
71
72CONVERSION is the character code of a \"conversion specification\"
4bef9110 73introduced by a `%' character in a control string."
681e6b4b 74 :type '(choice (const nil) function)
4bef9110 75 :group 'battery)
6b279740 76
4bef9110 77(defcustom battery-echo-area-format
20d4381e 78 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
71d21198 79 "Power %L, battery %B at %r (%p%% load, remaining time %t)")
3660b4f5
CY
80 ((eq battery-status-function 'battery-linux-sysfs)
81 "Power %L, battery %B (%p%% load)")
71d21198 82 ((eq battery-status-function 'battery-pmset)
20d4381e 83 "%L power, battery %B (%p%% load, remaining time %t)")
30710442
RS
84 ((eq battery-status-function 'battery-yeeloong-sysfs)
85 "%L power, battery %B (%p%% load, remaining time %t)")
20d4381e
JR
86 (battery-status-function
87 "Power %L, battery %B (%p%% load, remaining time %t)"))
9201cc28 88 "Control string formatting the string to display in the echo area.
6b279740
RS
89Ordinary characters in the control string are printed as-is, while
90conversion specifications introduced by a `%' character in the control
91string are substituted as defined by the current value of the variable
76815b2a
RS
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'"
4bef9110
SE
104 :type '(choice string (const nil))
105 :group 'battery)
6b279740
RS
106
107(defvar battery-mode-line-string nil
108 "String to display in the mode line.")
f4c4fc74 109;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
6b279740 110
43d5bf84
RS
111(defcustom battery-mode-line-limit 100
112 "Percentage of full battery load below which display battery status"
2bed3f04 113 :version "24.1"
43d5bf84
RS
114 :type 'integer
115 :group 'battery)
116
4bef9110 117(defcustom battery-mode-line-format
20d4381e 118 (cond ((eq battery-status-function 'battery-linux-proc-acpi)
71d21198 119