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-li


From: Andreas Politz
Subject: Re: Why save-excursion doesn't restore cursor position after 3 kill-line calls?
Date: Fri, 28 Nov 2008 23:35:01 +0100
User-agent: Mozilla-Thunderbird 2.0.0.17 (X11/20081018)

tyler wrote:
Xah Lee <xahlee@gmail.com> writes:

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.

Cursor-position is point in emacs jargon, no? The documentation clearly
states that point is saved and restored during save-excursion. Point
itself is just an integer, and does not disappear when text is deleted.

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

what exactly do you want to do?

I'm not sure what the OP wanted, but I want to understand why
save-excursion does not do the same thing as my function `mykill' does.

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.

The function provided above does exactly what I wanted (granted for only
simple cases). Try it with point on the u in 'presumes' in the paragraph
above. That line is killed, with point ending up at the same numeric
position where it started - i.e., the same column on the following line.
(This will only be the case when the following line is long enough, of
course).
The real question is why save-excursion *doesn't* do this. The
documentation states that point is restored. Either 'restoring' point
means something other than setting point to equal the previously saved
value, or there is something missing in the documentation.

Tyler



save-excursion stores a marker, not the 'byte' number. When you kill
the line where point is, this marker gets replaced to the beginning of
the line.

,----[ (info "(elisp)Excursions") ]
|    *Warning:* Ordinary insertion of text adjacent to the saved point
| value relocates the saved value, just as it relocates all markers.
| More precisely, the saved value is a marker with insertion type `nil'.
| *Note Marker Insertion Types::.  Therefore, when the saved point value
| is restored, it normally comes before the inserted text.
`----

-ap


reply via email to

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