all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stephen Berman <stephen.berman@gmx.net>
To: help-gnu-emacs@gnu.org
Subject: Re: Why save-excursion doesn't restore cursor position after 3 kill-line calls?
Date: Fri, 28 Nov 2008 23:49:45 +0100	[thread overview]
Message-ID: <873ahbtqie.fsf@escher.local.home> (raw)
In-Reply-To: 87oczzkfif.fsf@blackbart.sedgenet

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	other threads:[~2008-11-28 22:49 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
     [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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=873ahbtqie.fsf@escher.local.home \
    --to=stephen.berman@gmx.net \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.