all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Klaus Jantzen <k.d.jantzen@t-online.de>
To: emacs-list <help-gnu-emacs@gnu.org>
Subject: Re: Command in a function
Date: Sat, 27 Mar 2010 11:25:36 +0100	[thread overview]
Message-ID: <4BADDD20.6030008@t-online.de> (raw)
In-Reply-To: <52CFAD1180B1460090347833DE17B97D@us.oracle.com>

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.





  parent reply	other threads:[~2010-03-27 10:25 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 [this message]
2010-03-27 14:01             ` Drew Adams
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

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

  git send-email \
    --in-reply-to=4BADDD20.6030008@t-online.de \
    --to=k.d.jantzen@t-online.de \
    --cc=help-gnu-emacs@gnu.org \
    /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.
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.