unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* save-excursion doesn't save point?
@ 2002-06-08  5:56 Paul Stoeber
  2002-06-08  7:01 ` Miles Bader
  0 siblings, 1 reply; 6+ messages in thread
From: Paul Stoeber @ 2002-06-08  5:56 UTC (permalink / raw)


In GNU Emacs 21.2.1 (powerpc-unknown-linux-gnu)
 of 2002-05-26 on xyz
configured using `configure  --prefix=/e --without-x'
Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: nil
  value of $LC_CTYPE: nil
  value of $LC_MESSAGES: nil
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: nil
  value of $LANG: C
  locale-coding-system: nil
  default-enable-multibyte-characters: nil



;; This is my `*scratch*' buffer, and This is line 1.
;;
;;--- test line Alpha ---
;;--- test line Beta ---
;;
;; M-x eval-buffer.  Go to test line "Alpha".  C-a C-SPC C-n C-n.
;; ESC : (point).  Result is 109.  M-x shuffle-lines.  The two
;; test lines are transposed now.  The cursor rests on test line
;; "Beta".  ESC : (point).  Result is 58.  Point has changed, but
;; shuffle-lines has only one body form, which is a save-excursion
;; form.  Is that supposed to happen?

;; un-random used instead of random to make this test deterministic
(defun un-random (n)
  (1- n))

(defun shuffle-lines (beg end)
  "Randomly permute lines in region (all permutations equally likely)."
  (interactive "r")
  (save-excursion
    (let ((n (count-lines beg end)))
      (goto-char beg)
      (while (> n 1)
	(let ((r (un-random n)))
	  (if (zerop r);; special case for transpose-lines
	      nil
	    (set-mark (point))
	    (next-line r)
	    (transpose-lines 0)))
	(next-line 1)
	(setq n (1- n))))))

;; If you'd like to put this function into the main distribution,
;; don't forget to replace `un-random' with `random'.

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

* Re: save-excursion doesn't save point?
  2002-06-08  5:56 save-excursion doesn't save point? Paul Stoeber
@ 2002-06-08  7:01 ` Miles Bader
  2002-06-08  7:28   ` Paul Stoeber
  0 siblings, 1 reply; 6+ messages in thread
From: Miles Bader @ 2002-06-08  7:01 UTC (permalink / raw)


paul.stoeber@stud.tu-ilmenau.de (Paul Stoeber) writes:
> ;; Point has changed, but shuffle-lines has only one body form, which
> ;; is a save-excursion form.  Is that supposed to happen?

Yes.

save-excursion doesn't work by saving a character number, but setting a
marker at the old point-location, and restoring to the marker later.

If you insert or delete text adjacent to a marker, it will end up shoved
to one side or the other -- and transpose-lines works by deleting and
inserting.

-Miles
-- 
[|nurgle|]  ddt- demonic? so quake will have an evil kinda setting? one that 
            will  make every christian in the world foamm at the mouth? 
[iddt]      nurg, that's the goal 

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

* Re: save-excursion doesn't save point?
  2002-06-08  7:01 ` Miles Bader
@ 2002-06-08  7:28   ` Paul Stoeber
  2002-06-08  8:35     ` Miles Bader
  2002-06-11 17:11     ` Kevin Rodgers
  0 siblings, 2 replies; 6+ messages in thread
From: Paul Stoeber @ 2002-06-08  7:28 UTC (permalink / raw)
  Cc: bug-gnu-emacs

On Sat, Jun 08, 2002 at 07:01:30AM +0000, Miles Bader wrote:
> paul.stoeber@stud.tu-ilmenau.de (Paul Stoeber) writes:
> > ;; Point has changed, but shuffle-lines has only one body form, which
> > ;; is a save-excursion form.  Is that supposed to happen?
> 
> Yes.
> 
> save-excursion doesn't work by saving a character number, but setting a
> marker at the old point-location, and restoring to the marker later.
> 
> If you insert or delete text adjacent to a marker, it will end up shoved
> to one side or the other -- and transpose-lines works by deleting and
> inserting.
> 
> -Miles
> -- 
> [|nurgle|]  ddt- demonic? so quake will have an evil kinda setting? one that 
>             will  make every christian in the world foamm at the mouth? 
> [iddt]      nurg, that's the goal 
> 
> _______________________________________________
> Bug-gnu-emacs mailing list
> Bug-gnu-emacs@gnu.org
> http://mail.gnu.org/mailman/listinfo/bug-gnu-emacs

Thank you very much.

I use shuffle-lines to shuffle my music playlist.
It could be generally useful.  Emacs already has shuffle-vector.


(defun shuffle-lines (beg end)
  "Randomly permute lines in region (all permutations equally likely)."
  (interactive "r")
  (let ((pt (point)) (mk (mark)) (n (count-lines beg end)))
    (goto-char beg)
    (while (> n 1)
      (let ((r (random n)))
	(if (zerop r);; special case for transpose-lines
	    nil
	  (set-mark (point))
	  (next-line r)
	  (transpose-lines 0)))
      (next-line 1)
      (setq n (1- n)))
    (goto-char pt)
    (set-mark mk)))


---

We beg your acceptance of this elegant thimble.
		-- Dodo

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

* Re: save-excursion doesn't save point?
  2002-06-08  7:28   ` Paul Stoeber
@ 2002-06-08  8:35     ` Miles Bader
  2002-06-11 17:11     ` Kevin Rodgers
  1 sibling, 0 replies; 6+ messages in thread
From: Miles Bader @ 2002-06-08  8:35 UTC (permalink / raw)
  Cc: bug-gnu-emacs

BTW, using the mark (e.g. the `mark' and `set-mark' functions) in a lisp
program is usually the wrong thing to do.

You might instead want to check out the function `transpose-regions'.

-Miles
-- 
`Cars give people wonderful freedom and increase their opportunities.
 But they also destroy the environment, to an extent so drastic that
 they kill all social life' (from _A Pattern Language_)

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

* Re: save-excursion doesn't save point?
  2002-06-08  7:28   ` Paul Stoeber
  2002-06-08  8:35     ` Miles Bader
@ 2002-06-11 17:11     ` Kevin Rodgers
  2002-06-11 19:06       ` Miles Bader
  1 sibling, 1 reply; 6+ messages in thread
From: Kevin Rodgers @ 2002-06-11 17:11 UTC (permalink / raw)


Paul Stoeber wrote:

> I use shuffle-lines to shuffle my music playlist.
> It could be generally useful.  Emacs already has shuffle-vector.


Then it seems the simplest implementation would be to read the region
into a vector, shuffle the vector, then overwrite the region with the
vector's contents.

-- 
Kevin Rodgers <kevinr@ihs.com>

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

* Re: save-excursion doesn't save point?
  2002-06-11 17:11     ` Kevin Rodgers
@ 2002-06-11 19:06       ` Miles Bader
  0 siblings, 0 replies; 6+ messages in thread
From: Miles Bader @ 2002-06-11 19:06 UTC (permalink / raw)


kevinr@ihs.com (Kevin Rodgers) writes:
> > It could be generally useful.  Emacs already has shuffle-vector.
> 
> Then it seems the simplest implementation would be to read the region
> into a vector, shuffle the vector, then overwrite the region with the
> vector's contents.

Or better yet, do it indirectly, just have two vectors, one holding the
position of the start of each line in the buffer, and the other holding
indices into the first vectors.  Use `shuffle-vector' to shuffle the
second vector and do a single pass over it inserting the shuffled lines
_after_ the original lines.  Then just delete the region holding the
original lines.

That would be _much_ more efficient....

-Miles
-- 
I'd rather be consing.

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

end of thread, other threads:[~2002-06-11 19:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-06-08  5:56 save-excursion doesn't save point? Paul Stoeber
2002-06-08  7:01 ` Miles Bader
2002-06-08  7:28   ` Paul Stoeber
2002-06-08  8:35     ` Miles Bader
2002-06-11 17:11     ` Kevin Rodgers
2002-06-11 19:06       ` Miles Bader

Code repositories for project(s) associated with this public inbox

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

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).