aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2004-09-18 21:33:44 +0000
committerStefan Monnier2004-09-18 21:33:44 +0000
commit0f4a15f89c4363a259741ddaebd36db5d43fb76c (patch)
treee94fb9acd8cd7c57984fe43ec4629b26dcebd7f3
parent4bd65cb9acafc1f0e63ca213ffa974afea47057e (diff)
downloademacs-0f4a15f89c4363a259741ddaebd36db5d43fb76c.tar.gz
emacs-0f4a15f89c4363a259741ddaebd36db5d43fb76c.zip
(battery-linux-proc-apm): Use string-to-number.
(battery-linux-proc-acpi): Ignore all hidden files in the battery dir. Use dolist (and less indentation). Use erase-buffer. Pre-initialize `design-capacity', `warn', and `low'. (battery-format): Use replace-regexp-in-string.
-rw-r--r--lisp/ChangeLog18
-rw-r--r--lisp/battery.el142
2 files changed, 72 insertions, 88 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 6fd4579c785..8e86fdd3d19 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,11 @@
12004-09-18 Stefan Monnier <monnier@iro.umontreal.ca>
2
3 * battery.el (battery-linux-proc-apm): Use string-to-number.
4 (battery-linux-proc-acpi): Ignore all hidden files in the battery dir.
5 Use dolist (and less indentation). Use erase-buffer.
6 Pre-initialize `design-capacity', `warn', and `low'.
7 (battery-format): Use replace-regexp-in-string.
8
12004-09-18 Ralph Schleicher <rs@nunatak.allgaeu.org> 92004-09-18 Ralph Schleicher <rs@nunatak.allgaeu.org>
2 10
3 * battery.el: Delete superfluous empty lines. 11 * battery.el: Delete superfluous empty lines.
@@ -15,8 +23,8 @@
15 23
162004-09-17 Romain Francoise <romain@orebokech.com> 242004-09-17 Romain Francoise <romain@orebokech.com>
17 25
18 * ibuf-ext.el (define-ibuffer-filter filename): Expand 26 * ibuf-ext.el (define-ibuffer-filter filename):
19 dired-directory since buffer-file-name is absolute. 27 Expand dired-directory since buffer-file-name is absolute.
20 28
212004-09-17 Dan Nicolaescu <dann@ics.uci.edu> 292004-09-17 Dan Nicolaescu <dann@ics.uci.edu>
22 30
@@ -32,16 +40,14 @@
32 Use min-colors. 40 Use min-colors.
33 41
34 * term.el (term-down): Perform vertical motion if DOWN is negative. 42 * term.el (term-down): Perform vertical motion if DOWN is negative.
35 (term-exec-1): Set both TERMCAP and TERMINFO 43 (term-exec-1): Set both TERMCAP and TERMINFO unconditionally.
36 unconditionally.
37 (term-termcap-format): Synchronyze with the eterm terminfo entry 44 (term-termcap-format): Synchronyze with the eterm terminfo entry
38 in etc/e/eterm.ti. 45 in etc/e/eterm.ti.
39 (term-handle-colors-array): If the current foreground or 46 (term-handle-colors-array): If the current foreground or
40 background are zero (i.e. unspecified), use the foreground and 47 background are zero (i.e. unspecified), use the foreground and
41 background of the default face for reverse video. 48 background of the default face for reverse video.
42 (term-ansi-at-eval-string, term-ansi-default-fg) 49 (term-ansi-at-eval-string, term-ansi-default-fg)
43 (term-ansi-default-bg, term-ansi-current-temp): Delete unused 50 (term-ansi-default-bg, term-ansi-current-temp): Delete unused variable.
44 variable.
45 (term-mem): Delete ununsed function. 51 (term-mem): Delete ununsed function.
46 (term-protocol-version): Increment. 52 (term-protocol-version): Increment.
47 (term-current-face): Set to default. 53 (term-current-face): Set to default.
diff --git a/lisp/battery.el b/lisp/battery.el
index 2c7254be939..7b172321602 100644
--- a/lisp/battery.el
+++ b/lisp/battery.el
@@ -1,6 +1,7 @@
1;;; battery.el --- display battery status information 1;;; battery.el --- display battery status information
2 2
3;; Copyright (C) 1997, 1998, 2000, 2001, 2003 Free Software Foundation, Inc. 3;; Copyright (C) 1997, 1998, 2000, 2001, 2003, 2004
4;; Free Software Foundation, Inc.
4 5
5;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org> 6;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
6;; Keywords: hardware 7;; Keywords: hardware
@@ -31,6 +32,7 @@
31;;; Code: 32;;; Code:
32 33
33(require 'timer) 34(require 'timer)
35(eval-when-compile (require 'cl))
34 36
35 37
36(defgroup battery nil 38(defgroup battery nil
@@ -181,20 +183,20 @@ The following %-sequences are provided:
181 (re-search-forward battery-linux-proc-apm-regexp) 183 (re-search-forward battery-linux-proc-apm-regexp)
182 (setq driver-version (match-string 1)) 184 (setq driver-version (match-string 1))
183 (setq bios-version (match-string 2)) 185 (setq bios-version (match-string 2))
184 (setq tem (string-to-int (match-string 3) 16)) 186 (setq tem (string-to-number (match-string 3) 16))
185 (if (not (logand tem 2)) 187 (if (not (logand tem 2))
186 (setq bios-interface "not supported") 188 (setq bios-interface "not supported")
187 (setq bios-interface "enabled") 189 (setq bios-interface "enabled")
188 (cond ((logand tem 16) (setq bios-interface "disabled")) 190 (cond ((logand tem 16) (setq bios-interface "disabled"))
189 ((logand tem 32) (setq bios-interface "disengaged"))) 191 ((logand tem 32) (setq bios-interface "disengaged")))
190 (setq tem (string-to-int (match-string 4) 16)) 192 (setq tem (string-to-number (match-string 4) 16))
191 (cond ((= tem 0) (setq line-status "off-line")) 193 (cond ((= tem 0) (setq line-status "off-line"))
192 ((= tem 1) (setq line-status "on-line")) 194 ((= tem 1) (setq line-status "on-line"))
193 ((= tem 2) (setq line-status "on backup"))) 195 ((= tem 2) (setq line-status "on backup")))
194 (setq tem (string-to-int (match-string 6) 16)) 196 (setq tem (string-to-number (match-string 6) 16))
195 (if (= tem 255) 197 (if (= tem 255)
196 (setq battery-status "N/A") 198 (setq battery-status "N/A")
197 (setq tem (string-to-int (match-string 5) 16)) 199 (setq tem (string-to-number (match-string 5) 16))
198 (cond ((= tem 0) (setq battery-status "high" 200 (cond ((= tem 0) (setq battery-status "high"
199 battery-status-symbol "")) 201 battery-status-symbol ""))
200 ((= tem 1) (setq battery-status "low" 202 ((= tem 1) (setq battery-status "low"
@@ -242,64 +244,52 @@ The following %-sequences are provided:
242%m Remaining time in minutes 244%m Remaining time in minutes
243%h Remaining time in hours 245%h Remaining time in hours
244%t Remaining time in the form `h:min'" 246%t Remaining time in the form `h:min'"
245 (let (capacity design-capacity rate rate-type charging-state warn low 247 (let ((design-capacity 0)
246 minutes hours) 248 (warn 0)
247 ;; ACPI provides information about each battery present in the system 249 (low 0)
248 ;; in a separate subdirectory. We are going to merge the available 250 capacity rate rate-type charging-state minutes hours)
251 ;; ACPI provides information about each battery present in the system in
252 ;; a separate subdirectory. We are going to merge the available
249 ;; information together since displaying for a variable amount of 253 ;; information together since displaying for a variable amount of
250 ;; batteries seems overkill for format-strings. 254 ;; batteries seems overkill for format-strings.
251 (with-temp-buffer 255 (with-temp-buffer
252 (mapc (lambda (dir) 256 (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
253 (if (string-match "/\\.\\.?\\'" dir) 257 t "\\`[^.]")))
254 nil 258 (erase-buffer)
255 (delete-region (point-min) (point-max)) 259 (ignore-errors (insert-file-contents (expand-file-name "state" dir)))
256 (condition-case nil 260 (when (re-search-forward "present: +yes$" nil t)
257 (insert-file-contents (expand-file-name "state" dir)) 261 (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
258 (error nil)) 262 (member charging-state '("unknown" nil))
259 (when (re-search-forward "present: +yes$" nil t) 263 ;; On most multi-battery systems, most of the time only one
260 (and (re-search-forward 264 ;; battery is "charging"/"discharging", the others are
261 "charging state: +\\(.*\\)$" nil t) 265 ;; "unknown".
262 (or (null charging-state) 266 (setq charging-state (match-string 1)))
263 (string= charging-state "unknown")) 267 (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
264 ;; On most multi-battery systems, most of the time 268 nil t)
265 ;; only one battery is "charging/discharging", the 269 (setq rate (+ (or rate 0) (string-to-number (match-string 1)))
266 ;; others are "unknown". 270 rate-type (or (and rate-type
267 (setq charging-state (match-string 1))) 271 (if (string= rate-type (match-string 2))
268 (when (re-search-forward 272 rate-type
269 "present rate: +\\([0-9]+\\) \\(m[AW]\\)$" nil t) 273 (error
270 (setq rate (+ (or rate 0) 274 "Inconsistent rate types (%s vs. %s)"
271 (string-to-int (match-string 1))) 275 rate-type (match-string 2))))
272 rate-type (or (and rate-type 276 (match-string 2))))
273 (if (string= rate-type (match-string 2)) 277 (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
274 rate-type 278 nil t)
275 (error 279 (setq capacity
276 "Inconsistent rate types (%s vs. %s)" 280 (+ (or capacity 0) (string-to-number (match-string 1))))))
277 rate-type (match-string 2)))) 281 (goto-char (point-max))
278 (match-string 2)))) 282 (ignore-errors (insert-file-contents (expand-file-name "info" dir)))
279 (when (re-search-forward 283 (when (re-search-forward "present: +yes$" nil t)
280 "remaining capacity: +\\([0-9]+\\) m[AW]h$" nil t) 284 (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
281 (setq capacity (+ (or capacity 0) 285 nil t)
282 (string-to-int (match-string 1)))))) 286 (incf design-capacity (string-to-number (match-string 1))))
283 (goto-char (point-max)) 287 (when (re-search-forward
284 (condition-case nil 288 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
285 (insert-file-contents (expand-file-name "info" dir)) 289 (incf warn (string-to-number (match-string 1))))
286 (error nil)) 290 (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
287 (when (re-search-forward "present: +yes$" nil t) 291 nil t)
288 (when (re-search-forward 292 (incf low (string-to-number (match-string 1)))))))
289 "design capacity: +\\([0-9]+\\) m[AW]h$" nil t)
290 (setq design-capacity (+ (or design-capacity 0)
291 (string-to-int (match-string 1)))))
292 (when (re-search-forward
293 "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
294 (setq warn (+ (or warn 0)
295 (string-to-int (match-string 1)))))
296 (when (re-search-forward
297 "design capacity low: +\\([0-9]+\\) m[AW]h$" nil t)
298 (setq low (+ (or low 0)
299 (string-to-int (match-string 1))))))))
300 (condition-case nil
301 (directory-files "/proc/acpi/battery/" t)
302 (error nil))))
303 (and capacity rate 293 (and capacity rate
304 (setq minutes (if (zerop rate) 0 294 (setq minutes (if (zerop rate) 0
305 (floor (* (/ (float (if (string= charging-state 295 (floor (* (/ (float (if (string= charging-state
@@ -336,8 +326,8 @@ The following %-sequences are provided:
336 rate-type)) "N/A")) 326 rate-type)) "N/A"))
337 (cons ?B (or charging-state "N/A")) 327 (cons ?B (or charging-state "N/A"))
338 (cons ?b (or (and (string= charging-state "charging") "+") 328 (cons ?b (or (and (string= charging-state "charging") "+")
339 (and low (< capacity low) "!") 329 (and (< capacity low) "!")
340 (and warn (< capacity warn) "-") 330 (and (< capacity warn) "-")
341 "")) 331 ""))
342 (cons ?h (or (and hours (number-to-string hours)) "N/A")) 332 (cons ?h (or (and hours (number-to-string hours)) "N/A"))
343 (cons ?m (or (and minutes (number-to-string minutes)) "N/A")) 333 (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
@@ -355,28 +345,16 @@ The following %-sequences are provided:
355 345
356(defun battery-format (format alist) 346(defun battery-format (format alist)
357 "Substitute %-sequences in FORMAT." 347 "Substitute %-sequences in FORMAT."
358 (let ((index 0) 348 (replace-regexp-in-string
359 (length (length format)) 349 "%."
360 (result "") 350 (lambda (str)
361 char flag elem) 351 (let ((char (aref str 1)))
362 (while (< index length) 352 (if (eq char ?%) "%"
363 (setq char (aref format index)) 353 (or (cdr (assoc char alist)) ""))))
364 (if (not flag) 354 format t t))
365 (if (char-equal char ?%)
366 (setq flag t)
367 (setq result (concat result (char-to-string char))))
368 (cond ((char-equal char ?%)
369 (setq result (concat result "%")))
370 ((setq elem (assoc char alist))
371 (setq result (concat result (cdr elem)))))
372 (setq flag nil))
373 (setq index (1+ index)))
374 (or (null flag)
375 (setq result (concat result "%")))
376 result))
377 355
378 356
379(provide 'battery) 357(provide 'battery)
380 358
381;;; arch-tag: 65916f50-4754-4b6b-ac21-0b510f545a37 359;; arch-tag: 65916f50-4754-4b6b-ac21-0b510f545a37
382;;; battery.el ends here 360;;; battery.el ends here