help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Command in a function


From: Drew Adams
Subject: RE: Command in a function
Date: Sat, 27 Mar 2010 07:01:29 -0700

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








reply via email to

[Prev in Thread] Current Thread [Next in Thread]