unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* kill-emacs and returning values
@ 2009-02-14 13:51 Mathias Dahl
  2009-02-14 17:17 ` Mathias Megyei
  0 siblings, 1 reply; 5+ messages in thread
From: Mathias Dahl @ 2009-02-14 13:51 UTC (permalink / raw)
  To: emacs-devel

I am trying to use emacs interactively as part of a shell script and
want to return a value that I can use in the script.

How is the optional string parameter to `kill-emacs' supposed to be
used? The docstring says:

===
kill-emacs is an interactive built-in function in `C source code'.

(kill-emacs &optional arg)

Exit the Emacs job and kill it.
If arg is an integer, return arg as the exit program code.
If arg is a string, stuff it as keyboard input.
===

Okay, sounds promising. I did a small test:

  $ cat ke.el
  ;; Interactive stuff goes here...

  ;; In the end, kill emacs and return a value to the script running in
  ;; the shell
  (kill-emacs "This is the return value.")

Testing it:

  $ emacs -Q -nw -l ke.el
  This is the return value.

So far so good. Now I want to save this value in a variable.

  mathias@klibb:~$ emacs -Q -nw -l ke.el; read returnvariable
  This is the return value.

Let's see what we got:

  mathias@klibb:~$ echo $returnvariable

  $

Nothing? Okay... A pipe must be used, of course, Next try:

  mathias@klibb:~$ emacs -Q -nw -l ke.el | read returnvariable
  This is the return value.

  mathias@klibb:~$ echo $returnvariable

  $

I see now that "as keyboard input" does not mean what I thought; it is
not something returned to stdout.

How is this supposed to be used? It would seem more natural to return
things to stdout, but I am sure there is a good reason for it to work
the way it does, it's just that I don't understand it.

Please note that I do not want to use emacs in batch mode. Emacs must
be responsive because I am building a UI for file selection in it.

Thanks!

/Mathias

PS. Of course I can use write-region and write what I want to a file
and then read the result from there, but that seems like a less
elegant solution to me.




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

* Re: kill-emacs and returning values
  2009-02-14 13:51 kill-emacs and returning values Mathias Dahl
@ 2009-02-14 17:17 ` Mathias Megyei
  2009-02-14 20:23   ` Mathias Dahl
  0 siblings, 1 reply; 5+ messages in thread
From: Mathias Megyei @ 2009-02-14 17:17 UTC (permalink / raw)
  To: Mathias Dahl; +Cc: emacs-devel

On Sat, 2009-02-14 at 14:51 +0100, Mathias Dahl wrote:
> Okay, sounds promising. I did a small test:
> 
>   $ cat ke.el
>   ;; Interactive stuff goes here...
> 
>   ;; In the end, kill emacs and return a value to the script running in
>   ;; the shell
>   (kill-emacs "This is the return value.")
> 
Does it work for your purpose if you change the last line
to:

 (kill-emacs "returnvalue='This is the return value.'")

$ emacs -Q -nw -l ke.el    
returnvalue='This is the return value.'

$ echo $returnvalue
This is the return value.

Best Regards,

Mathias





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

* Re: kill-emacs and returning values
  2009-02-14 17:17 ` Mathias Megyei
@ 2009-02-14 20:23   ` Mathias Dahl
  2009-02-14 21:47     ` Mathias Megyei
  0 siblings, 1 reply; 5+ messages in thread
From: Mathias Dahl @ 2009-02-14 20:23 UTC (permalink / raw)
  To: mathias; +Cc: emacs-devel

> Does it work for your purpose if you change the last line
>  to:
>
>   (kill-emacs "returnvalue='This is the return value.'")
>
>
>  $ emacs -Q -nw -l ke.el
>
> returnvalue='This is the return value.'
>
>  $ echo $returnvalue

I tried that already and it would have - if it worked :( Could this
depend on which shell you use? I am using bash.




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

* Re: kill-emacs and returning values
  2009-02-14 20:23   ` Mathias Dahl
@ 2009-02-14 21:47     ` Mathias Megyei
  2009-02-14 22:01       ` Mathias Dahl
  0 siblings, 1 reply; 5+ messages in thread
From: Mathias Megyei @ 2009-02-14 21:47 UTC (permalink / raw)
  To: Mathias Dahl; +Cc: emacs-devel

On Sat, 2009-02-14 at 21:23 +0100, Mathias Dahl wrote:
> > Does it work for your purpose if you change the last line
> >  to:
> >
> >   (kill-emacs "returnvalue='This is the return value.'")
> >
> >
> >  $ emacs -Q -nw -l ke.el
> >
> > returnvalue='This is the return value.'
> >
> >  $ echo $returnvalue
> 
> I tried that already and it would have - if it worked :( Could this
> depend on which shell you use? I am using bash.
> 
Yes, it depends on the shell in question.
The above is for Bash. 
It works when Emacs is invoked "by hand" but not if it was 
started in  a shell script. It seems the command

  returnvalue='This is the return value.'

is executed after the script has finished (as the next
"keyboard input").

Within the script you can read in the "next keyboard input" 
into a variable. 

The following script seems to do what you want:

  #!/bin/bash

  /opt/emacs/23.0.60/bin/emacs -Q -nw -l ke.el
  read myval
  echo "myval is set to: '$myval'"

Mathias





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

* Re: kill-emacs and returning values
  2009-02-14 21:47     ` Mathias Megyei
@ 2009-02-14 22:01       ` Mathias Dahl
  0 siblings, 0 replies; 5+ messages in thread
From: Mathias Dahl @ 2009-02-14 22:01 UTC (permalink / raw)
  To: mathias; +Cc: emacs-devel

>  #!/bin/bash
>
>  /opt/emacs/23.0.60/bin/emacs -Q -nw -l ke.el
>  read myval
>  echo "myval is set to: '$myval'"

It does not work for me, read sits there, waiting for input
and I have to feed it something to get the prompt back,
and it is what I enter that is echoed back. The value emacs
"types" is just seen printed on the terminal.

I should probably forget about this and use write-region to
write the value to a file instead, something is clearly not
100 percent okay in my setup. I am using a pretest emacs
from some week ago, btw.

/Mathias




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

end of thread, other threads:[~2009-02-14 22:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-02-14 13:51 kill-emacs and returning values Mathias Dahl
2009-02-14 17:17 ` Mathias Megyei
2009-02-14 20:23   ` Mathias Dahl
2009-02-14 21:47     ` Mathias Megyei
2009-02-14 22:01       ` Mathias Dahl

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).