unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* defining functions in emacs
@ 2006-01-24 21:40 Matt Brown
  2006-01-24 23:13 ` Edward O'Connor
  2006-01-25 16:02 ` Kevin Rodgers
  0 siblings, 2 replies; 5+ messages in thread
From: Matt Brown @ 2006-01-24 21:40 UTC (permalink / raw)


Hi everyone

I wrote my first emacs function! Here's my function:


(defun read-environment ()
  (setq outbuf (generate-new-buffer "read-environment-outbuf"))
  (call-process "read-environment.sh" nil outbuf nil)
  (eval-buffer outbuf)
  (kill-buffer outbuf))

It uses read-environment.sh to generate a whole bunch of setenv lines, 
and then evaluates them.  Basically, it sources .bashrc and sets the new 
variables in emacs.  It works fine, but I want to be able to run it by
M-x read-environment

This doesn't work. I have to put
(read-environment)
into *scratch*, move to the ')' and do C-x C-e to evaluate it.  Pretty lame.

 From the digging around I've done, it seems like it should just work.  
Any ideas?  BTW, the defun is in my .emacs

Thanks
Matt

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

* Re: defining functions in emacs
  2006-01-24 21:40 Matt Brown
@ 2006-01-24 23:13 ` Edward O'Connor
  2006-01-25 16:02 ` Kevin Rodgers
  1 sibling, 0 replies; 5+ messages in thread
From: Edward O'Connor @ 2006-01-24 23:13 UTC (permalink / raw)


> (defun read-environment ()
> (setq outbuf (generate-new-buffer "read-environment-outbuf"))
> (call-process "read-environment.sh" nil outbuf nil)
> (eval-buffer outbuf)
> (kill-buffer outbuf))

How about this instead?

(defun read-environment ()
  (interactive)
  (with-temp-buffer
    (call-process "read-environment.sh" nil t nil)
    (eval-buffer)))


Ted

-- 
Edward O'Connor
hober0@gmail.com

Ense petit placidam sub libertate quietem.

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

* Re: defining functions in emacs
       [not found] <mailman.11.1138142928.2878.help-gnu-emacs@gnu.org>
@ 2006-01-25  8:09 ` Tim X
  2006-01-26  6:17 ` Barry Margolin
  1 sibling, 0 replies; 5+ messages in thread
From: Tim X @ 2006-01-25  8:09 UTC (permalink / raw)


Matt Brown <mbrown83@ucsc.edu> writes:

> Hi everyone
> 
> I wrote my first emacs function! Here's my function:
> 
> 
> (defun read-environment ()
>   (setq outbuf (generate-new-buffer "read-environment-outbuf"))
>   (call-process "read-environment.sh" nil outbuf nil)
>   (eval-buffer outbuf)
>   (kill-buffer outbuf))
> 
> It uses read-environment.sh to generate a whole bunch of setenv lines,
> and then evaluates them.  Basically, it sources .bashrc and sets the
> new variables in emacs.  It works fine, but I want to be able to run
> it by
> M-x read-environment
> 
> This doesn't work. I have to put
> (read-environment)
> into *scratch*, move to the ')' and do C-x C-e to evaluate it.  Pretty lame.
> 
>  From the digging around I've done, it seems like it should just work.
> Any ideas?  BTW, the defun is in my .emacs
> 
> Thanks
> Matt
> 
> 
You need to include (interactive) in your defun


,----[ C-h f interactive RET ]
| interactive is a special form.
| (interactive ARGS)
| 
| Specify a way of parsing arguments for interactive use of a function.
| For example, write
|   (defun foo (arg) "Doc string" (interactive "p") ...use arg...)
| to make ARG be the prefix argument when `foo' is called as a command.
| The "call" to `interactive' is actually a declaration rather than a function;
|  it tells `call-interactively' how to read arguments
|  to pass to the function.
| When actually called, `interactive' just returns nil.
| 
| The argument of `interactive' is usually a string containing a code letter
|  followed by a prompt.  (Some code letters do not use I/O to get
|  the argument and do not need prompts.)  To prompt for multiple arguments,
|  give a code letter, its prompt, a newline, and another code letter, etc.
|  Prompts are passed to format, and may use % escapes to print the
|  arguments that have already been read.
| If the argument is not a string, it is evaluated to get a list of
|  arguments to pass to the function.
| Just `(interactive)' means pass no args when calling interactively.
| 
| Code letters available are:
| a -- Function name: symbol with a function definition.
| b -- Name of existing buffer.
| B -- Name of buffer, possibly nonexistent.
| c -- Character (no input method is used).
| C -- Command name: symbol with interactive function definition.
| d -- Value of point as number.  Does not do I/O.
| D -- Directory name.
| e -- Parametrized event (i.e., one that's a list) that invoked this command.
|      If used more than once, the Nth `e' returns the Nth parameterized event.
|      This skips events that are integers or symbols.
| f -- Existing file name.
| F -- Possibly nonexistent file name.
| i -- Ignored, i.e. always nil.  Does not do I/O.
| k -- Key sequence (downcase the last event if needed to get a definition).
| K -- Key sequence to be redefined (do not downcase the last event).
| m -- Value of mark as number.  Does not do I/O.
| M -- Any string.  Inherits the current input method.
| n -- Number read using minibuffer.
| N -- Raw prefix arg, or if none, do like code `n'.
| p -- Prefix arg converted to number.  Does not do I/O.
| P -- Prefix arg in raw form.  Does not do I/O.
| r -- Region: point and mark as 2 numeric args, smallest first.  Does no I/O.
| s -- Any string.  Does not inherit the current input method.
| S -- Any symbol.
| v -- Variable name: symbol that is user-variable-p.
| x -- Lisp expression read but not evaluated.
| X -- Lisp expression read and evaluated.
| z -- Coding system.
| Z -- Coding system, nil if no prefix arg.
| In addition, if the string begins with `*'
|  then an error is signaled if the buffer is read-only.
|  This happens before reading any arguments.
| If the string begins with `@', then Emacs searches the key sequence
|  which invoked the command for its first mouse click (or any other
|  event which specifies a window), and selects that window before
|  reading any arguments.  You may use both `@' and `*'; they are
|  processed in the order that they appear.
`----

-- 
Tim Cross
The e-mail address on this message is FALSE (obviously!). My real e-mail is
to a company in Australia called rapttech and my login is tcross - if you 
really need to send mail, you should be able to work it out!

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

* Re: defining functions in emacs
  2006-01-24 21:40 Matt Brown
  2006-01-24 23:13 ` Edward O'Connor
@ 2006-01-25 16:02 ` Kevin Rodgers
  1 sibling, 0 replies; 5+ messages in thread
From: Kevin Rodgers @ 2006-01-25 16:02 UTC (permalink / raw)


Matt Brown wrote:
> I wrote my first emacs function! Here's my function:
> 
> 
> (defun read-environment ()
>  (setq outbuf (generate-new-buffer "read-environment-outbuf"))
>  (call-process "read-environment.sh" nil outbuf nil)
>  (eval-buffer outbuf)
>  (kill-buffer outbuf))
> 
> It uses read-environment.sh to generate a whole bunch of setenv lines, 
> and then evaluates them.  Basically, it sources .bashrc and sets the new 
> variables in emacs.

In Emacs there is always more than 1 way to skin a cat:

(let ((shell-file-name "/bin/bash"))
   (setq process-environment
         (split-string (shell-command-to-string ". $HOME/.bashrc; env") 
"\n")))

> It works fine, but I want to be able to run it by
> M-x read-environment
> 
> This doesn't work. I have to put
> (read-environment)
> into *scratch*, move to the ')' and do C-x C-e to evaluate it.  Pretty 
> lame.
> 
>  From the digging around I've done, it seems like it should just work.  
> Any ideas?  BTW, the defun is in my .emacs

Read the "Defining Commands" node of the Emacs Lisp manual, and its
subnodes.

-- 
Kevin Rodgers

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

* Re: defining functions in emacs
       [not found] <mailman.11.1138142928.2878.help-gnu-emacs@gnu.org>
  2006-01-25  8:09 ` defining functions in emacs Tim X
@ 2006-01-26  6:17 ` Barry Margolin
  1 sibling, 0 replies; 5+ messages in thread
From: Barry Margolin @ 2006-01-26  6:17 UTC (permalink / raw)


In article <mailman.11.1138142928.2878.help-gnu-emacs@gnu.org>,
 Matt Brown <mbrown83@ucsc.edu> wrote:

> Hi everyone
> 
> I wrote my first emacs function! Here's my function:
> 
> 
> (defun read-environment ()
>   (setq outbuf (generate-new-buffer "read-environment-outbuf"))
>   (call-process "read-environment.sh" nil outbuf nil)
>   (eval-buffer outbuf)
>   (kill-buffer outbuf))
> 
> It uses read-environment.sh to generate a whole bunch of setenv lines, 
> and then evaluates them.  Basically, it sources .bashrc and sets the new 
> variables in emacs.  It works fine, but I want to be able to run it by
> M-x read-environment

As others have pointed out, if you want to define a COMMAND, you have to 
use the (interactive) declaration.  You've just defined a function, and 
M-x executes commands.

> This doesn't work. I have to put
> (read-environment)
> into *scratch*, move to the ')' and do C-x C-e to evaluate it.  Pretty lame.

It's not quite that bad.  M-: can be used to execute arbitrary Lisp 
functions.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

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

end of thread, other threads:[~2006-01-26  6:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.11.1138142928.2878.help-gnu-emacs@gnu.org>
2006-01-25  8:09 ` defining functions in emacs Tim X
2006-01-26  6:17 ` Barry Margolin
2006-01-24 21:40 Matt Brown
2006-01-24 23:13 ` Edward O'Connor
2006-01-25 16:02 ` Kevin Rodgers

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