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

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

Re: [External] : How to get all paragraphs in list?


From: Jean Louis
Subject: Re: [External] : How to get all paragraphs in list?
Date: Fri, 16 Sep 2022 22:04:36 +0300
User-agent: Mutt/+ () (2022-06-11)

* Yuri Khan <yuri.v.khan@gmail.com> [2022-09-16 19:26]:
> On Fri, 16 Sept 2022 at 20:29, Emanuel Berg <incal@dataswamp.org> wrote:
> 
> > > More to the point[sic], you calculate and cache the END
> > > position as an integer. So if your FUNCTION reduces the
> > > length of the buffer text, you may never reach END.
> > > Should probably place a marker there
> >
> > Marker, do we have that? :O
> >
> > How does that look/work?
> 
> Will you forgive me if I say “(elisp) Markers”? Basically it’s a way
> to refer to a position in the buffer in a way that remains valid and
> somewhat sane under buffer text modification.

That was alright, and I have changed (point-max) to (point-max-marker)
now and it seem to work. I have to test it many times until I become
sure.

This is important because I have many documents which I often quote,
and copy into other programs, where broken lines are making incorrect
representation.

(defun rcd-paragraphs-iterate (function)
  "Iterate FUNCTION over paragraphs.

FUNCTION must accept string as single argument."
  (let ((start (if (use-region-p) (region-beginning) (point-min)))
        (end (if (use-region-p) (region-end) (point-max-marker))))
    (save-excursion
      (goto-char start)
      (while (<= (point) end)
        (funcall function)
        (forward-paragraph)))))

(defun join-lines ()
  "Joins lines of a paragraph."
  (interactive)
  (let ((fill-column (point-max)))
    (fill-paragraph)))

(defun join-lines-buffer-or-region ()
  "Join lines on all buffer."
  (interactive)
  (rcd-paragraphs-iterate 'join-lines))

(defun join-lines-string (text)
  "Join lines within TEXT."
  (with-temp-buffer
    (insert text)
    (rcd-paragraphs-iterate 'join-lines)
    (buffer-string)))

(defun join-lines-1 (text)
  (with-temp-buffer 
    (let ((fill-column 100000))
      (insert text)
      (goto-char 0)
      (fill-paragraph)
      (buffer-string))))

(defun kill-region-join-lines (start end)
  "Kill region with lines joined"
  (interactive "r")
  (when (region-active-p)
    (let* ((text (buffer-substring-no-properties start end))
           (joined (join-lines-1 text)))
      (deactivate-mark)
      (kill-new joined))))


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



reply via email to

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