help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: avoid narrow-to-region (was: Re: replace-regexp)


From: Emanuel Berg
Subject: Re: avoid narrow-to-region (was: Re: replace-regexp)
Date: Sun, 09 May 2021 14:43:40 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Yuri Khan wrote:

> (defun yk-shuffle-lines (begin end)
>   "Reorder lines between BEGIN and END randomly.
> If BEGIN or END is in the middle of a line, that line is included.
> Any markers inside the region may move
> to the beginning or end of their respective lines,
> and won’t follow the text they were associated with."
>   (interactive "*r")
>   (save-excursion
>     (let ((nlines (count-lines begin end)))
>       (goto-char begin)
>       (while (> nlines 1)
>         (let* ((begin1 (line-beginning-position))
>                (line1 (let ((end1 (line-end-position)))
>                         (prog1 (buffer-substring begin1 end1)
>                           (delete-region begin1 end1))))
>                (_ (forward-line (random nlines)))
>                (line2 (let* ((begin2 (line-beginning-position))
>                              (end2 (line-end-position)))
>                         (prog1 (buffer-substring begin2 end2)
>                           (delete-region begin2 end2)))))
>           (insert line1)
>           (goto-char begin1)
>           (insert line2))
>         (forward-line)
>         (setq nlines (1- nlines))))))
>
> (The structure of let*/let/let* bindings may seem
> excessively complex, but I’m taking care here to not expose
> bindings beyond their validity. E.g. as soon as
> (delete-region begin1 end1) is executed, end1 no longer
> refers to anything meaningful, so I drop that binding asap.)

Alright, that more like it. Scientific randomness at
you service!

Well, yes, `let' within a let body I think I've seen and even
done a couple of times, let inside a let varlist I believe is
a new experience but why not :)

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

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