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


From: Stephen Berman
Subject: Re: Why save-excursion doesn't restore cursor position after 3 kill-line calls?
Date: Fri, 28 Nov 2008 23:49:45 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux)

On Fri, 28 Nov 2008 11:59:52 -0400 tyler <tyler.smith@mail.mcgill.ca> wrote:

> Barry Margolin <barmar@alum.mit.edu> writes:
>
>> In article 
>> <429c5cab-0015-4eb6-a794-ce990c6255d6@q26g2000prq.googlegroups.com>,
>>  "seberino@spawar.navy.mil" <seberino@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?
>>> 
>>> chris
>>
>> save-excursion restores the location in the buffer, not the row and 
>> column positions.  If the text around the location is deleted, it finds 
>> the closest remaining location.
>
> I don't understand. The documentation for save-excursion says:
>
> "Save point, mark, and current buffer; execute body; restore those things."
>
> 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.

If Barry Margolin's accurate but terse explanation is unclear, perhaps
this more detailed remark from the Emacs Lisp reference manual, node
(elisp)Excursions, helps:

"*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."

To see the difference, compare the first code above with the following:

(defun mykill ()
  (interactive)
  (save-excursion
    (forward-line 5)
    (kill-line)
    (kill-line 0)
    (kill-line)))

Steve Berman





reply via email to

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