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: Klaus Jantzen
Subject: Re: Command in a function
Date: Sat, 27 Mar 2010 11:25:36 +0100
User-agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090707)

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.





reply via email to

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