diff lisp/battery.el @ 73700:294b1de9ac5f

2006-11-05 Micha?Cadilhac <michael.cadilhac@lrde.org> * battery.el (battery-linux-proc-acpi): Search an ac_adapter in `/proc/acpi/ac_adapter/*'. Ditto for the thermometers in `/proc/acpi/thermal_zone/*'. (battery-search-for-one-match-in-files): New. Search a regexp in the content of some files.
author Romain Francoise <romain@orebokech.com>
date Sun, 05 Nov 2006 13:22:30 +0000
parents 65d9fbabd719
children 685bae49b575 02cf29720f31
line wrap: on
line diff
--- a/lisp/battery.el	Sun Nov 05 12:21:01 2006 +0000
+++ b/lisp/battery.el	Sun Nov 05 13:22:30 2006 +0000
@@ -49,8 +49,8 @@
 	      (file-directory-p "/proc/acpi/battery"))
 	 'battery-linux-proc-acpi)
 	((and (eq system-type 'darwin)
-	      (condition-case nil  
-		  (with-temp-buffer 
+	      (condition-case nil
+		  (with-temp-buffer
 		    (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
 			 (> (buffer-size) 0)))
 		(error nil)))
@@ -355,45 +355,19 @@
 				   60)))
 	       hours (/ minutes 60)))
     (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
-	  (cons ?L (or (when (file-exists-p "/proc/acpi/ac_adapter/AC/state")
-			 (with-temp-buffer
-			   (insert-file-contents
-			    "/proc/acpi/ac_adapter/AC/state")
-			   (when (re-search-forward "state: +\\(.*\\)$" nil t)
-			     (match-string 1))))
+	  (cons ?L (or (battery-search-for-one-match-in-files
+			(mapcar (lambda (e) (concat e "/state"))
+				(directory-files "/proc/acpi/ac_adapter/"
+						 t "\\`[^.]"))
+			"state: +\\(.*\\)$" 1)
+
 		       "N/A"))
-	  (cons ?d (or (when (file-exists-p
-			      "/proc/acpi/thermal_zone/THRM/temperature")
-			 (with-temp-buffer
-			   (insert-file-contents
-			    "/proc/acpi/thermal_zone/THRM/temperature")
-			   (when (re-search-forward
-				  "temperature: +\\([0-9]+\\) C$" nil t)
-			     (match-string 1))))
-		       (when (file-exists-p
-			      "/proc/acpi/thermal_zone/THM/temperature")
-			 (with-temp-buffer
-			   (insert-file-contents
-			    "/proc/acpi/thermal_zone/THM/temperature")
-			   (when (re-search-forward
-				  "temperature: +\\([0-9]+\\) C$" nil t)
-			     (match-string 1))))
-		       (when (file-exists-p
-			      "/proc/acpi/thermal_zone/THM0/temperature")
-			 (with-temp-buffer
-			   (insert-file-contents
-			    "/proc/acpi/thermal_zone/THM0/temperature")
-			   (when (re-search-forward
-				  "temperature: +\\([0-9]+\\) C$" nil t)
-			     (match-string 1))))
-		       (when (file-exists-p
-			      "/proc/acpi/thermal_zone/THR2/temperature")
-			 (with-temp-buffer
-			   (insert-file-contents
-			    "/proc/acpi/thermal_zone/THR2/temperature")
-			   (when (re-search-forward
-				  "temperature: +\\([0-9]+\\) C$" nil t)
-			     (match-string 1))))
+	  (cons ?d (or (battery-search-for-one-match-in-files
+			(mapcar (lambda (e) (concat e "/temperature"))
+				(directory-files "/proc/acpi/thermal_zone/"
+						 t "\\`[^.]"))
+			"temperature: +\\([0-9]+\\) C$" 1)
+
 		       "N/A"))
 	  (cons ?r (or (and rate (concat (number-to-string rate) " "
 					 rate-type)) "N/A"))
@@ -479,6 +453,17 @@
 	 (or (cdr (assoc char alist)) ""))))
    format t t))
 
+(defun battery-search-for-one-match-in-files (files regexp match-num)
+  "Search REGEXP in the content of the files listed in FILES.
+If a match occured, return the parenthesized expression numbered by
+MATCH-NUM in the match.  Otherwise, return nil."
+  (with-temp-buffer
+    (catch 'found
+      (dolist (file files)
+	(and (ignore-errors (insert-file-contents file nil nil nil 'replace))
+	     (re-search-forward regexp nil t)
+	     (throw 'found (match-string match-num)))))))
+
 
 (provide 'battery)