unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Why save-excursion doesn't restore cursor position after 3 kill-line calls?
@ 2008-11-28  4:40 seberino
  2008-11-28 10:00 ` Barry Margolin
  0 siblings, 1 reply; 10+ messages in thread
From: seberino @ 2008-11-28  4:40 UTC (permalink / raw)
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why save-excursion doesn't restore cursor position after 3 kill-line calls?
  2008-11-28  4:40 Why save-excursion doesn't restore cursor position after 3 kill-line calls? seberino
@ 2008-11-28 10:00 ` Barry Margolin
  2008-11-28 15:59   ` tyler
       [not found]   ` <mailman.1440.1227888009.26697.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 10+ messages in thread
From: Barry Margolin @ 2008-11-28 10:00 UTC (permalink / raw)
  To: help-gnu-emacs

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.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why save-excursion doesn't restore cursor position after 3 kill-line calls?
  2008-11-28 10:00 ` Barry Margolin
@ 2008-11-28 15:59   ` tyler
  2008-11-28 22:49     ` Stephen Berman
       [not found]   ` <mailman.1440.1227888009.26697.help-gnu-emacs@gnu.org>
  1 sibling, 1 reply; 10+ messages in thread
From: tyler @ 2008-11-28 15:59 UTC (permalink / raw)
  To: help-gnu-emacs

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.

Cheers,

Tyler

-- 
Friends don't let friends send Word documents

http://www.nothingisreal.com/dfki/no-word





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why save-excursion doesn't restore cursor position after 3  kill-line calls?
       [not found]   ` <mailman.1440.1227888009.26697.help-gnu-emacs@gnu.org>
@ 2008-11-28 16:43     ` Xah Lee
  2008-11-28 20:18       ` tyler
       [not found]       ` <mailman.1449.1227903504.26697.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 10+ messages in thread
From: Xah Lee @ 2008-11-28 16:43 UTC (permalink / raw)
  To: help-gnu-emacs

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/^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why save-excursion doesn't restore cursor position after 3 kill-line calls?
  2008-11-28 16:43     ` Xah Lee
@ 2008-11-28 20:18       ` tyler
       [not found]       ` <mailman.1449.1227903504.26697.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 10+ messages in thread
From: tyler @ 2008-11-28 20:18 UTC (permalink / raw)
  To: help-gnu-emacs

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


-- 
Philosophy of science is about as useful to scientists as ornithology is to
birds.                                 --Richard Feynman





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why save-excursion doesn't restore cursor position after 3   kill-line calls?
       [not found]       ` <mailman.1449.1227903504.26697.help-gnu-emacs@gnu.org>
@ 2008-11-28 22:35         ` Andreas Politz
  2008-11-28 23:18           ` tyler
       [not found]           ` <mailman.1463.1227914342.26697.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 10+ messages in thread
From: Andreas Politz @ 2008-11-28 22:35 UTC (permalink / raw)
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why save-excursion doesn't restore cursor position after 3 kill-line calls?
  2008-11-28 15:59   ` tyler
@ 2008-11-28 22:49     ` Stephen Berman
  0 siblings, 0 replies; 10+ messages in thread
From: Stephen Berman @ 2008-11-28 22:49 UTC (permalink / raw)
  To: help-gnu-emacs

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





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why save-excursion doesn't restore cursor position after 3 kill-line calls?
  2008-11-28 22:35         ` Andreas Politz
@ 2008-11-28 23:18           ` tyler
       [not found]           ` <mailman.1463.1227914342.26697.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 10+ messages in thread
From: tyler @ 2008-11-28 23:18 UTC (permalink / raw)
  To: help-gnu-emacs

Andreas Politz <politza@fh-trier.de> writes:

> tyler wrote:
>>
>> 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.
>>
>
> 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.
> `----
>

Thanks. I think I understand, at least generally, what's going on now.
Markers are a new concept for me, so I'll have to do some more reading
to sort out the details.

Tyler

-- 






^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why save-excursion doesn't restore cursor position after 3 kill-line calls?
       [not found]           ` <mailman.1463.1227914342.26697.help-gnu-emacs@gnu.org>
@ 2008-11-29  8:21             ` Barry Margolin
  2008-11-29 17:28               ` tyler
  0 siblings, 1 reply; 10+ messages in thread
From: Barry Margolin @ 2008-11-29  8:21 UTC (permalink / raw)
  To: help-gnu-emacs

In article <mailman.1463.1227914342.26697.help-gnu-emacs@gnu.org>,
 tyler <tyler.smith@mail.mcgill.ca> wrote:

> Thanks. I think I understand, at least generally, what's going on now.
> Markers are a new concept for me, so I'll have to do some more reading
> to sort out the details.

Here's a simple example that may help.  Suppose you have a buffer 
containing:

1 abcdef
2 123456
3 wxyz

and point is on line 2 between 3 and 4.  You write a function that uses 
save-excursion while it deletes line 1.  When the save-excursion ends, 
point will still be between 3 and 4, although this will now be line 1.

The intent is to continue pointing to the same text that it originally 
pointed to.  But if that text itself is deleted, this is obviously not 
possible.  Any markers that were within the deleted text will end up 
pointing to the place where the text used to be.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Why save-excursion doesn't restore cursor position after 3 kill-line calls?
  2008-11-29  8:21             ` Barry Margolin
@ 2008-11-29 17:28               ` tyler
  0 siblings, 0 replies; 10+ messages in thread
From: tyler @ 2008-11-29 17:28 UTC (permalink / raw)
  To: help-gnu-emacs

Barry Margolin <barmar@alum.mit.edu> writes:

> In article <mailman.1463.1227914342.26697.help-gnu-emacs@gnu.org>,
>  tyler <tyler.smith@mail.mcgill.ca> wrote:
>
>> Thanks. I think I understand, at least generally, what's going on now.
>> Markers are a new concept for me, so I'll have to do some more reading
>> to sort out the details.
>
> Here's a simple example that may help.  Suppose you have a buffer 
> containing:
>
> 1 abcdef
> 2 123456
> 3 wxyz
>
> and point is on line 2 between 3 and 4.  You write a function that uses 
> save-excursion while it deletes line 1.  When the save-excursion ends, 
> point will still be between 3 and 4, although this will now be line 1.
>
> The intent is to continue pointing to the same text that it originally 
> pointed to.  But if that text itself is deleted, this is obviously not 
> possible.  Any markers that were within the deleted text will end up 
> pointing to the place where the text used to be.

Thanks, that does help!

Tyler

-- 
I never loan my books, for people never return them. The only books
remaining in my library are those I’ve borrowed from others. 





^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2008-11-29 17:28 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-28  4:40 Why save-excursion doesn't restore cursor position after 3 kill-line calls? seberino
2008-11-28 10:00 ` Barry Margolin
2008-11-28 15:59   ` tyler
2008-11-28 22:49     ` Stephen Berman
     [not found]   ` <mailman.1440.1227888009.26697.help-gnu-emacs@gnu.org>
2008-11-28 16:43     ` Xah Lee
2008-11-28 20:18       ` tyler
     [not found]       ` <mailman.1449.1227903504.26697.help-gnu-emacs@gnu.org>
2008-11-28 22:35         ` Andreas Politz
2008-11-28 23:18           ` tyler
     [not found]           ` <mailman.1463.1227914342.26697.help-gnu-emacs@gnu.org>
2008-11-29  8:21             ` Barry Margolin
2008-11-29 17:28               ` tyler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).