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

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

Re: Why save-excursion doesn't restore cursor position after 3 kill-lin


From: Xah Lee
Subject: Re: Why save-excursion doesn't restore cursor position after 3 kill-line calls?
Date: Fri, 28 Nov 2008 08:43:19 -0800 (PST)
User-agent: G2/1.0

On Nov 28, 7:59 am, tyler <tyler.sm...@mail.mcgill.ca> wrote:
> Barry Margolin <bar...@alum.mit.edu> writes:
> > In article
> > <429c5cab-0015-4eb6-a794-ce990c625...@q26g2000prq.googlegroups.com>,
> >  "seber...@spawar.navy.mil" <seber...@spawar.navy.mil> wrote:
>
> >> I'm trying to map C-d to a function that deletes an entire line and
> >> stops on the *SAME* column number of the following line....
>
> >> (global-set-key "\^d" (lambda () (interactive) (save-excursion (kill-
> >> line)
> >> (kill-line 0)
> >> (kill-line))))
>
> >> Why doesn't save-excursion preserve the column number after these kill-
> >> line invocations?

> I tried Chris' code, first calling (point), then running the command,
> then running (point) again, and the value has definitely not been
> restored. It is possible, for example:
>
> (defun mykill ()
>   (interactive)
>   (let ((p (point)))
>     (kill-line)
>     (kill-line 0)
>     (kill-line)
>     (goto-char p)))
>
> I've been confused by save-excursion in other contexts though, so I'm
> still unclear on why the original version of this function doesn't work.

Like Barry Margolin has said, save-excursion restore the cursor
position but not when the text the cursor is on is deleted in your
code. In other words, it is theoretically senseless to preserve
something that's not there anymore.

in your code, your several kill-line code deleted the line the cursor
is on.

what exactly do you want to do?

if you want to return your cursor to the same column it was on, you
can get the column position then move your cursor to that afterwards.
However, that presumes that you are on a line that has enough chars to
cover the column. Overall, i think you need to clarify the behavior
you wanted.

  Xah
∑ http://xahlee.org/

reply via email to

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