;; (C) Muli Ben-Yehuda , GPL'd ;; ;; M-x display-battery to add the battery status to your mode line, ;; M-x display-battery-stop to remove it ;; ;; Code mostly copy and pasted from time.el, some bits copied from ;; Alex Shinn's battery.el. ;; ;; $Id: battery.el,v 1.1 2003/09/28 21:12:41 muli Exp $ (defvar display-battery-string nil) (defvar display-battery-interval 120 "Number of seconds to wait before updating battery info") (defun battery-shell-command(command output-buffer) "Execute string COMMAND, capture output in output-buffer." "we need this because shell-command pipes its output to the " "echo area, if it has exactly one line of output?!" (let ((buffer (get-buffer-create (or output-buffer "*Shell Command Output*"))) (exit-status nil) (directory default-directory)) (save-excursion (set-buffer buffer) (setq buffer-read-only nil) ;; XEmacs change (setq default-directory directory) (erase-buffer)) (setq exit-status (call-process-region (point) (point) shell-file-name nil buffer nil shell-command-switch command)))) (defun display-battery() (interactive) (let ((old (get-itimer "display-battery"))) (if old (delete-itimer old))) (if (memq 'display-battery-string global-mode-string) (setq global-mode-string (remove 'display-battery-string global-mode-string))) (or global-mode-string (setq global-mode-string '(""))) (setq global-mode-string (append global-mode-string '(display-battery-string))) (display-battery-function) (start-itimer "display-battery" 'display-battery-function display-battery-interval display-battery-interval)) (defun display-battery-function() "Generate representing battery info" (save-excursion ;; Because we can't read /proc/apm directly, ;; and shell-command is b0rked (see battery-shell-command help string) ;; TODO: this can barf, check exit status (battery-shell-command "cat /proc/apm" "*battery*") (set-buffer "*battery*") (beginning-of-buffer) (setq display-battery-string (if (looking-at ".*[ \t]\\([0-9]+%\\)") (concat "Bat: " (buffer-substring (match-beginning 1) (match-end 1)) " ") "Bat: APM off ")))) (defun display-battery-stop() (interactive) (delete-itimer "display-battery") (setq display-battery-string nil)) (provide 'battery)