unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "Drew Adams" <drew.adams@oracle.com>
To: "'Klaus Jantzen'" <k.d.jantzen@t-online.de>,
	"'emacs-list'" <help-gnu-emacs@gnu.org>
Subject: RE: Command in a function
Date: Sat, 27 Mar 2010 07:01:29 -0700	[thread overview]
Message-ID: <3A65F8774A74436CB84A4FEE6CB9DAF3@us.oracle.com> (raw)
In-Reply-To: <4BADDD20.6030008@t-online.de>

> > 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	other threads:[~2010-03-27 14:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2010-03-26 16:04     ` Lennart Borgman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3A65F8774A74436CB84A4FEE6CB9DAF3@us.oracle.com \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=k.d.jantzen@t-online.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).