all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Jean Louis <bugs@gnu.support>
To: Yuri Khan <yuri.v.khan@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: [External] : How to get all paragraphs in list?
Date: Fri, 16 Sep 2022 22:04:36 +0300	[thread overview]
Message-ID: <YyTIxGfWatBT4QS+@protected.localdomain> (raw)
In-Reply-To: <CAP_d_8VQq+NYPjOnm=MY9LuAe0fF_A45X0BAVBODp03B1=DmTg@mail.gmail.com>

* 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	other threads:[~2022-09-16 19:04 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-05  4:15 How to get all paragraphs in list? Jean Louis
2022-09-05  4:40 ` Teemu Likonen
2022-09-05  5:12 ` Emanuel Berg
2022-09-05  9:13 ` Alessandro Bertulli
2022-09-05 16:40   ` Jean Louis
2022-09-06  1:31     ` Emanuel Berg
2022-09-16 14:18       ` Jean Louis
2022-09-16 14:21       ` Jean Louis
2022-09-05 16:02 ` [External] : " Drew Adams
2022-09-05 17:04   ` Jean Louis
2022-09-05 18:05     ` Drew Adams
2022-09-06 11:17       ` Jean Louis
2022-09-07  6:03       ` Jean Louis
2022-09-07  6:13         ` Emanuel Berg
2022-09-07 13:05         ` Yuri Khan
2022-09-07 15:04           ` Drew Adams
2022-09-07 23:51           ` Emanuel Berg
2022-09-16 16:17             ` Yuri Khan
2022-09-16 19:04               ` Jean Louis [this message]
2022-09-16 18:55             ` Jean Louis

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=YyTIxGfWatBT4QS+@protected.localdomain \
    --to=bugs@gnu.support \
    --cc=help-gnu-emacs@gnu.org \
    --cc=yuri.v.khan@gmail.com \
    /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.