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 15:14:13 +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))))))

Only the `save-excursion' seems to be insufficient? I don't
know when that works (most of the times) and when it doesn't
(sometimes) so this is as good a time as any to ask that...

OK, so now we have one hacker/hacky solution and one
scientist/scientific, so what is the next step? It is ...

the engineer solution!

Task: separate the material (i.e., the data type) from the
algorithm, so that the algorithm can be applied to any form of
data, be it a string, words on a line, lines as we have seen,
a list of arbitrary elements...

That way, we can do Jean's whole file scientific!

Only one problem ... who is the engineer around here?
Not Jean himself, is it??!!

Hahaha :)

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




reply via email to

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