unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Writing a Lisp to show the capacity of the battery of the laptop
@ 2011-02-21 14:46 Thomas Dean
  2011-02-22 15:13 ` Le Wang
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Thomas Dean @ 2011-02-21 14:46 UTC (permalink / raw)
  To: help-gnu-emacs


Hi,

I'm using Archlinux. I want to write a lisp to show the remaining
capacity of the battery of the laptop. It is expected to work like
this:

1. Read from /proc/acpi/battery/BAT0/state and obtain the remaining
capacity of the battery.
2. Read from /proc/acpi/battery/BAT0/info and obtain the full capacity
of the battery.
3. By calculating remaining / full, get the percentage and print it.

I writing the following lisp but it seems that it doesn't work.

;--------------------------------LISP---------------------------------

; get remaining capacity
(shell-command
 "cat /proc/acpi/battery/BAT0/state | grep \"remaining capacity\" | sed \"s/^[a-z][a-z: ]*\([0-9][0-9]*\).*$/\1/g\""
 "td-battery-temp-buffer")
(set-buffer "td-battery-temp-buffer")
(setq td-battery-full (string-to-number (buffer-string)))
(erase-buffer)

; get full capacity
(shell-command
 "cat /proc/acpi/battery/BAT0/info | grep \"last full capacity\" | sed \"s/^[a-z][a-z: ]*\([0-9][0-9]*\).*$/\1/g\""
 "td-battery-temp-buffer")
(setq td-battery-full (string-to-number (buffer-string)))

; calculate percentage
(message (concat "Remaining Capacity: " (number-to-string td-battery-remaining)
		 "\nFull Capacity: " (number-to-string td-battery-full)))

; kill temporary buffer
(set-buffer-modified-p nil)
(kill-buffer)

;----------------------LISP END-------------------------------

I'm new to lisp :-) So it maybe looks ugly.

Could you please show me a good lisp that can finish this task?

Thomas

-- 



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Writing a Lisp to show the capacity of the battery of the laptop
  2011-02-21 14:46 Writing a Lisp to show the capacity of the battery of the laptop Thomas Dean
@ 2011-02-22 15:13 ` Le Wang
  2011-02-22 15:33 ` Deniz Dogan
  2011-02-22 17:25 ` Eli Zaretskii
  2 siblings, 0 replies; 9+ messages in thread
From: Le Wang @ 2011-02-22 15:13 UTC (permalink / raw)
  To: Thomas Dean; +Cc: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1758 bytes --]

Your Emacs probably came with "Emacs Lisp Intro" C-hi.  That's a good place
to start.

On Mon, Feb 21, 2011 at 10:46 PM, Thomas Dean <tdean9db@gmail.com> wrote:

>
> Hi,
>
> I'm using Archlinux. I want to write a lisp to show the remaining
> capacity of the battery of the laptop. It is expected to work like
> this:
>
> 1. Read from /proc/acpi/battery/BAT0/state and obtain the remaining
> capacity of the battery.
> 2. Read from /proc/acpi/battery/BAT0/info and obtain the full capacity
> of the battery.
> 3. By calculating remaining / full, get the percentage and print it.
>
> I writing the following lisp but it seems that it doesn't work.
>
> ;--------------------------------LISP---------------------------------
>
> ; get remaining capacity
> (shell-command
>  "cat /proc/acpi/battery/BAT0/state | grep \"remaining capacity\" | sed
> \"s/^[a-z][a-z: ]*\([0-9][0-9]*\).*$/\1/g\""
>  "td-battery-temp-buffer")
> (set-buffer "td-battery-temp-buffer")
> (setq td-battery-full (string-to-number (buffer-string)))
> (erase-buffer)
>
> ; get full capacity
> (shell-command
>  "cat /proc/acpi/battery/BAT0/info | grep \"last full capacity\" | sed
> \"s/^[a-z][a-z: ]*\([0-9][0-9]*\).*$/\1/g\""
>  "td-battery-temp-buffer")
> (setq td-battery-full (string-to-number (buffer-string)))
>
> ; calculate percentage
> (message (concat "Remaining Capacity: " (number-to-string
> td-battery-remaining)
>                 "\nFull Capacity: " (number-to-string td-battery-full)))
>
> ; kill temporary buffer
> (set-buffer-modified-p nil)
> (kill-buffer)
>
> ;----------------------LISP END-------------------------------
>
> I'm new to lisp :-) So it maybe looks ugly.
>
> Could you please show me a good lisp that can finish this task?
>
> Thomas
>
> --
>
>


-- 
Le

[-- Attachment #2: Type: text/html, Size: 2334 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Writing a Lisp to show the capacity of the battery of the laptop
  2011-02-21 14:46 Writing a Lisp to show the capacity of the battery of the laptop Thomas Dean
  2011-02-22 15:13 ` Le Wang
@ 2011-02-22 15:33 ` Deniz Dogan
  2011-02-22 16:51   ` Alberto Luaces
  2011-02-22 17:25 ` Eli Zaretskii
  2 siblings, 1 reply; 9+ messages in thread
From: Deniz Dogan @ 2011-02-22 15:33 UTC (permalink / raw)
  To: Thomas Dean; +Cc: help-gnu-emacs

2011/2/21 Thomas Dean <tdean9db@gmail.com>:
>
> Hi,
>
> I'm using Archlinux. I want to write a lisp to show the remaining
> capacity of the battery of the laptop. It is expected to work like
> this:
>
> 1. Read from /proc/acpi/battery/BAT0/state and obtain the remaining
> capacity of the battery.
> 2. Read from /proc/acpi/battery/BAT0/info and obtain the full capacity
> of the battery.
> 3. By calculating remaining / full, get the percentage and print it.
>
> I writing the following lisp but it seems that it doesn't work.
>
> ;--------------------------------LISP---------------------------------
>
> ; get remaining capacity
> (shell-command
>  "cat /proc/acpi/battery/BAT0/state | grep \"remaining capacity\" | sed \"s/^[a-z][a-z: ]*\([0-9][0-9]*\).*$/\1/g\""
>  "td-battery-temp-buffer")
> (set-buffer "td-battery-temp-buffer")
> (setq td-battery-full (string-to-number (buffer-string)))
> (erase-buffer)
>
> ; get full capacity
> (shell-command
>  "cat /proc/acpi/battery/BAT0/info | grep \"last full capacity\" | sed \"s/^[a-z][a-z: ]*\([0-9][0-9]*\).*$/\1/g\""
>  "td-battery-temp-buffer")
> (setq td-battery-full (string-to-number (buffer-string)))
>
> ; calculate percentage
> (message (concat "Remaining Capacity: " (number-to-string td-battery-remaining)
>                 "\nFull Capacity: " (number-to-string td-battery-full)))
>
> ; kill temporary buffer
> (set-buffer-modified-p nil)
> (kill-buffer)
>
> ;----------------------LISP END-------------------------------
>
> I'm new to lisp :-) So it maybe looks ugly.
>
> Could you please show me a good lisp that can finish this task?
>

Your code is unorganized and buffer-centric for no reason. Below is
some code I wrote, which I can't test at the moment, but it should
give you a few hints. By the way, try M-x battery in Emacs.

(defun bat-remaining ()
  "Return the battery's remaining capacity."
  (let ((result (shell-command "yada yada")))
    (string-to-number result)))

(defun bat-full ()
  "Return the battery's full capacity."
  (let ((result (shell-command "blah blah")))
    (string-to-number result)))

(defun bat-percent ()
  "Return the remaining amount of battery power as an integer."
  (let ((remaining (float (bat-remaining)))
        (full (float (bat-full))))
    (round (* (/ remaining full) 100))))

(defun bat-information ()
  "Echo information about the battery."
  (interactive)
  (message "Remaining capacity: %s\nFull capacity: %s"
           (bat-remaining)
           (bat-full)))


-- 
Deniz Dogan



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Writing a Lisp to show the capacity of the battery of the laptop
       [not found] <mailman.14.1298381790.4429.help-gnu-emacs@gnu.org>
@ 2011-02-22 16:48 ` Pascal J. Bourguignon
  0 siblings, 0 replies; 9+ messages in thread
From: Pascal J. Bourguignon @ 2011-02-22 16:48 UTC (permalink / raw)
  To: help-gnu-emacs

Thomas Dean <tdean9db@gmail.com> writes:

> Hi,
>
> I'm using Archlinux. I want to write a lisp to show the remaining
> capacity of the battery of the laptop. It is expected to work like
> this:
>
> 1. Read from /proc/acpi/battery/BAT0/state and obtain the remaining
> capacity of the battery.
> 2. Read from /proc/acpi/battery/BAT0/info and obtain the full capacity
> of the battery.
> 3. By calculating remaining / full, get the percentage and print it.
>
> I writing the following lisp but it seems that it doesn't work.
>
> ;--------------------------------LISP---------------------------------
>
> ; get remaining capacity
> (shell-command
>  "cat /proc/acpi/battery/BAT0/state | grep \"remaining capacity\" | sed \"s/^[a-z][a-z: ]*\([0-9][0-9]*\).*$/\1/g\""
>  "td-battery-temp-buffer")
> (set-buffer "td-battery-temp-buffer")
> (setq td-battery-full (string-to-number (buffer-string)))
> (erase-buffer)

(string-to-number (shell-command-to-string "..."))


> ; calculate percentage
> (message (concat "Remaining Capacity: " (number-to-string td-battery-remaining)
> 		 "\nFull Capacity: " (number-to-string td-battery-full)))

(message "Remaining Capacity: %d\nFull Capacity: %d"
         td-battery-remaining td-battery-full)


But of course, as mentionned, M-x battery.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Writing a Lisp to show the capacity of the battery of the laptop
  2011-02-22 15:33 ` Deniz Dogan
@ 2011-02-22 16:51   ` Alberto Luaces
  2011-02-22 17:25     ` Deniz Dogan
  0 siblings, 1 reply; 9+ messages in thread
From: Alberto Luaces @ 2011-02-22 16:51 UTC (permalink / raw)
  To: help-gnu-emacs

Deniz Dogan writes:

> Your code is unorganized and buffer-centric for no reason.

I'm also an elisp newbie and were doing the same buffer fiddling, since
I didn't know that `shell-command' returned a string with the output
until I saw your example.

Is there any existing convention about this? I can't find that
information in an explicit form in that function documentation.

-- 
Alberto




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Writing a Lisp to show the capacity of the battery of the laptop
  2011-02-21 14:46 Writing a Lisp to show the capacity of the battery of the laptop Thomas Dean
  2011-02-22 15:13 ` Le Wang
  2011-02-22 15:33 ` Deniz Dogan
@ 2011-02-22 17:25 ` Eli Zaretskii
  2 siblings, 0 replies; 9+ messages in thread
From: Eli Zaretskii @ 2011-02-22 17:25 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Mon, 21 Feb 2011 22:46:32 +0800
> From: Thomas Dean <tdean9db@gmail.com>
> 
> I'm using Archlinux. I want to write a lisp to show the remaining
> capacity of the battery of the laptop.

Are you aware that Emacs already has this?  Try "M-x battery RET", and
see if it already works on your laptop.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Writing a Lisp to show the capacity of the battery of the laptop
  2011-02-22 16:51   ` Alberto Luaces
@ 2011-02-22 17:25     ` Deniz Dogan
  2011-02-22 17:35       ` Alberto Luaces
  0 siblings, 1 reply; 9+ messages in thread
From: Deniz Dogan @ 2011-02-22 17:25 UTC (permalink / raw)
  To: Alberto Luaces; +Cc: help-gnu-emacs

2011/2/22 Alberto Luaces <aluaces@udc.es>:
> Deniz Dogan writes:
>
>> Your code is unorganized and buffer-centric for no reason.
>
> I'm also an elisp newbie and were doing the same buffer fiddling, since
> I didn't know that `shell-command' returned a string with the output
> until I saw your example.
>
> Is there any existing convention about this? I can't find that
> information in an explicit form in that function documentation.
>

Heh, my bad, it doesn't return a string. Ignore me.

-- 
Deniz Dogan



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Writing a Lisp to show the capacity of the battery of the laptop
  2011-02-22 17:25     ` Deniz Dogan
@ 2011-02-22 17:35       ` Alberto Luaces
  2011-02-22 17:41         ` Deniz Dogan
  0 siblings, 1 reply; 9+ messages in thread
From: Alberto Luaces @ 2011-02-22 17:35 UTC (permalink / raw)
  To: help-gnu-emacs

Deniz Dogan writes:

> 2011/2/22 Alberto Luaces:
>> Deniz Dogan writes:
>>
>>> Your code is unorganized and buffer-centric for no reason.
>>
>> I'm also an elisp newbie and were doing the same buffer fiddling, since
>> I didn't know that `shell-command' returned a string with the output
>> until I saw your example.
>>
>> Is there any existing convention about this? I can't find that
>> information in an explicit form in that function documentation.
>>
>
> Heh, my bad, it doesn't return a string. Ignore me.

What a pity! :(

-- 
Alberto




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Writing a Lisp to show the capacity of the battery of the laptop
  2011-02-22 17:35       ` Alberto Luaces
@ 2011-02-22 17:41         ` Deniz Dogan
  0 siblings, 0 replies; 9+ messages in thread
From: Deniz Dogan @ 2011-02-22 17:41 UTC (permalink / raw)
  To: Alberto Luaces; +Cc: help-gnu-emacs

2011/2/22 Alberto Luaces <aluaces@udc.es>:
> Deniz Dogan writes:
>
>> 2011/2/22 Alberto Luaces:
>>> Deniz Dogan writes:
>>>
>>>> Your code is unorganized and buffer-centric for no reason.
>>>
>>> I'm also an elisp newbie and were doing the same buffer fiddling, since
>>> I didn't know that `shell-command' returned a string with the output
>>> until I saw your example.
>>>
>>> Is there any existing convention about this? I can't find that
>>> information in an explicit form in that function documentation.
>>>
>>
>> Heh, my bad, it doesn't return a string. Ignore me.
>
> What a pity! :(
>

shell-command-to-string does though! I knew it existed somewhere...!

-- 
Deniz Dogan



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2011-02-22 17:41 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-02-21 14:46 Writing a Lisp to show the capacity of the battery of the laptop Thomas Dean
2011-02-22 15:13 ` Le Wang
2011-02-22 15:33 ` Deniz Dogan
2011-02-22 16:51   ` Alberto Luaces
2011-02-22 17:25     ` Deniz Dogan
2011-02-22 17:35       ` Alberto Luaces
2011-02-22 17:41         ` Deniz Dogan
2011-02-22 17:25 ` Eli Zaretskii
     [not found] <mailman.14.1298381790.4429.help-gnu-emacs@gnu.org>
2011-02-22 16:48 ` Pascal J. Bourguignon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).