all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Command in a function
@ 2010-03-24 21:50 Klaus Jantzen
  2010-03-24 22:36 ` Drew Adams
  0 siblings, 1 reply; 7+ messages in thread
From: Klaus Jantzen @ 2010-03-24 21:50 UTC (permalink / raw)
  To: emacs-list

Hello,

I am writing a function that inserts some text into a buffer.
At some point the cursor (point?) should go to the end of a line.

For that I found the command "end-of-line". How do I code the execution 
of this command
in a function?
I tried it with (command-execute end-of-line) but that resulted in an 
error message.

Thanks for any help.
-- 

K.D.J.





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

* RE: Command in a function
  2010-03-24 21:50 Command in a function Klaus Jantzen
@ 2010-03-24 22:36 ` Drew Adams
  2010-03-25  7:23   ` Klaus Jantzen
  0 siblings, 1 reply; 7+ messages in thread
From: Drew Adams @ 2010-03-24 22:36 UTC (permalink / raw)
  To: 'Klaus Jantzen', 'emacs-list'

> I am writing a function that inserts some text into a buffer.
> At some point the cursor (point?) should go to the end of a line.
> 
> For that I found the command "end-of-line". How do I code the 
> execution of this command in a function?
> I tried it with (command-execute end-of-line) but that resulted in an 
> error message.

Just use (end-of-line).

Or (end-of-line N), where N is the number of lines to first move forward or
backward.

In Emacs, a command is a function.

A function that has an `interactive' spec is a command. See the Elisp manual,
page `Using Interactive', if you want to make a function interactive (i.e. make
it a command, so you can bind it to a key or invoke it by name using `M-x').





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

* Re: Command in a function
  2010-03-24 22:36 ` Drew Adams
@ 2010-03-25  7:23   ` Klaus Jantzen
  2010-03-26 15:29     ` Drew Adams
  2010-03-26 16:04     ` Lennart Borgman
  0 siblings, 2 replies; 7+ messages in thread
From: Klaus Jantzen @ 2010-03-25  7:23 UTC (permalink / raw)
  Cc: 'emacs-list'

Drew Adams wrote:
>> I am writing a function that inserts some text into a buffer.
>> At some point the cursor (point?) should go to the end of a line.
>>
>> For that I found the command "end-of-line". How do I code the 
>> execution of this command in a function?
>> I tried it with (command-execute end-of-line) but that resulted in an 
>> error message.
>>     
>
> Just use (end-of-line).
>   

That is what I had in the beginning . I tried it again and receive the 
friendly message
"Invalid function (end-of-line)"


> Or (end-of-line N), where N is the number of lines to first move forward or
> backward.
>
> In Emacs, a command is a function.
>
> A function that has an `interactive' spec is a command. See the Elisp manual,
> page `Using Interactive', if you want to make a function interactive (i.e. make
> it a command, so you can bind it to a key or invoke it by name using `M-x').
>
>
>   

-- 

K.D.J.





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

* RE: Command in a function
  2010-03-25  7:23   ` Klaus Jantzen
@ 2010-03-26 15:29     ` Drew Adams
       [not found]       ` <4BACD9DE.6080802@t-online.de>
  2010-03-26 16:04     ` Lennart Borgman
  1 sibling, 1 reply; 7+ messages in thread
From: Drew Adams @ 2010-03-26 15:29 UTC (permalink / raw)
  To: 'Klaus Jantzen'; +Cc: 'emacs-list'

> >> I am writing a function that inserts some text into a buffer.
> >> At some point the cursor (point?) should go to the end of a line.
> >>
> >> For that I found the command "end-of-line". How do I code the 
> >> execution of this command in a function?
> >> I tried it with (command-execute end-of-line) but that 
> >> resulted in an error message.
> >
> > Just use (end-of-line).
> 
> That is what I had in the beginning . I tried it again and 
> receive the friendly message "Invalid function (end-of-line)"

You need to show more of the code you are trying to use.

(end-of-line) is not a function. It is an expression (a sexp) that represents a
function call. When it is evaluated, the function `end-of-line' is called (with
an empty argument list).

Apparently, you tried to use (end-of-line) in a context that expected a function
- e.g. as an functional argument to some higher-order function such as
`funcall', `apply', or `mapcar'.

Without seeing what your code is, it's difficult to help make things clearer for
you.

The point is: In a context that expects an expression to evaluate, you use an
expression, such as (end-of-line) or (funcall 'end-of-line), that represents a
function call. In a context that expects a function, you use an expression, such
as `end-of-line', that represents a function.

You asked how to use the command `end-of-line' in a function you are defining.
Here is an example:

(defun foo ()
  ... ; do something
  (end-of-line) ; go to eol
  ... ; do some more stuff
)






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

* Re: Command in a function
  2010-03-25  7:23   ` Klaus Jantzen
  2010-03-26 15:29     ` Drew Adams
@ 2010-03-26 16:04     ` Lennart Borgman
  1 sibling, 0 replies; 7+ messages in thread
From: Lennart Borgman @ 2010-03-26 16:04 UTC (permalink / raw)
  To: Klaus Jantzen; +Cc: emacs-list

On Thu, Mar 25, 2010 at 8:23 AM, Klaus Jantzen <k.d.jantzen@t-online.de> wrote:
>
> That is what I had in the beginning . I tried it again and receive the
> friendly message
> "Invalid function (end-of-line)"

It looks like you have something like ((end-of-line)) since the
compiler complaints about the function "(end-of-line)".

You may have gotten that from a bad defmacro call (I have not followed
this thread).




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

* Re: Command in a function
       [not found]         ` <52CFAD1180B1460090347833DE17B97D@us.oracle.com>
@ 2010-03-27 10:25           ` Klaus Jantzen
  2010-03-27 14:01             ` Drew Adams
  0 siblings, 1 reply; 7+ messages in thread
From: Klaus Jantzen @ 2010-03-27 10:25 UTC (permalink / raw)
  To: emacs-list

Drew Adams wrote:
>> Here is what I want to do: I want to find out, where the cursor is 
>> before I insert some text: if it is at the end of a line I ensure
>> that it is here (end-of-line), write  a new line (new-line) go to
>> the beginning of that line. This function is supposed to be called
>> by those functions that must insert text at the beginning
>> line.
>>
>> (defun g-where ()
>>   "Determines where we are"
>>   (interactive)
>>   (if (bolp)   ; at the end of a line ?
>>     ()
>>     ((end-of-line)
>>      (new-line)
>>      (beginning-of-line))))
>>
>> The code is  certainly somewhat crude as it is one of my first 
>> emacs-functions I wrote.
>>     
>
> 1. C-h f if
>
> ,----
> | if is a special form in `C source code'.
> | 
> | (if COND THEN ELSE...)
> | 
> | If COND yields non-nil, do THEN, else do ELSE...
> | Returns the value of THEN or the value of the last of the ELSE's.
> | THEN must be one expression, but ELSE... can be zero or more expressions.
> | If COND yields nil, and there are no ELSE's, the value is nil.
> | 
> `----
>
> You are using (if COND THEN (ELSE...)), not (if COND THEN ELSE...).
>
> 2. No such function: `new-line'.
>
> If you just want to insert a newline unless point is at the beginning of the
> line (and do nothing otherwise), then just do that:
>
> (defun foo ()
>   (interactive)
>   (unless (bolp) (insert "\n")))
>
> Or if you want the return value to let you know whether you were at bolp to
> begin with, then:
>
> (defun foo ()
>   (interactive)
>   (if (bolp)
>       nil ; We were at bolp
>    (insert "\n")
>    t)) ; We weren't at bolp
>
>   
Drew,

thanks very much for your help.
Your last message put me on the right track. Following your suggestions 
I wrote the function
in question as follows:

(defun g-where ()
  "Determines where we are"
  (interactive)
  (if (not (eolp))
    (end-of-line))
  (insert "\n"))

This moves the cursor to the end of a line if the cursor  is
at the beginning of line that is not empty or if the cursor is somewhere 
in a line.

Thanks a lot.

-- 

K.D.J.





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

* RE: Command in a function
  2010-03-27 10:25           ` Klaus Jantzen
@ 2010-03-27 14:01             ` Drew Adams
  0 siblings, 0 replies; 7+ messages in thread
From: Drew Adams @ 2010-03-27 14:01 UTC (permalink / raw)
  To: 'Klaus Jantzen', 'emacs-list'

> > If you just want to insert a newline unless point is at the 
> > beginning of the line (and do nothing otherwise), then just do that:
> >
> > (defun foo ()
> >   (interactive)
> >   (unless (bolp) (insert "\n")))
> >
> > Or if you want the return value to let you know whether you 
> > were at bolp to begin with, then:
> >
> > (defun foo ()
> >   (interactive)
> >   (if (bolp)
> >       nil ; We were at bolp
> >    (insert "\n")
> >    t)) ; We weren't at bolp
>
> Drew,
> 
> thanks very much for your help.
> Your last message put me on the right track. Following your 
> suggestions I wrote the function in question as follows:
> 
> (defun g-where ()
>   "Determines where we are"
>   (interactive)
>   (if (not (eolp)) (end-of-line))
>   (insert "\n"))
> 
> This moves the cursor to the end of a line if the cursor  is
> at the beginning of line that is not empty or if the cursor 
> is somewhere in a line.
> 
> Thanks a lot.

You always insert the newline (`insert' is not inside the `if'). `insert' always
returns nil, so your command does too - its return value determines/shows
nothing.

So your side-effect-only command inserts a newline, moving first to eol if not
already there. IOW, it inserts a newline after the current non-empty line. That
is not what your doc string says.

Your `if' has only one branch (THEN). And you do not use the `if' return value -
you use the `if' only for its side effect. To make this
single-branch-and-side-effect-only behavior clearer to human readers, it is
somewhat conventional to use `when' or `unless':

(defun g-where ()
  "Insert a newline after the current line, unless empty."
  (interactive)
  (unless (eolp) (end-of-line))
  (insert "\n"))








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

end of thread, other threads:[~2010-03-27 14:01 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-03-24 21:50 Command in a function Klaus Jantzen
2010-03-24 22:36 ` Drew Adams
2010-03-25  7:23   ` Klaus Jantzen
2010-03-26 15:29     ` Drew Adams
     [not found]       ` <4BACD9DE.6080802@t-online.de>
     [not found]         ` <52CFAD1180B1460090347833DE17B97D@us.oracle.com>
2010-03-27 10:25           ` Klaus Jantzen
2010-03-27 14:01             ` Drew Adams
2010-03-26 16:04     ` Lennart Borgman

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.