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: Wed, 7 Sep 2022 09:03:28 +0300
User-agent: Mutt/+ () (2022-06-11)

* Drew Adams <drew.adams@oracle.com> [2022-09-05 21:07]:
> > However, there is some problem as it blocks, never ends, when I
> > interrupt it with Ctrl-G then I see that lines have been joined in all
> > of the buffer, but `while' loop was I guess still running.
> > 
> > Do you maybe know why?
> 
> You're repeatedly refilling/joining the same paragraphs.
> 
> `M-x debug-on-entry' shows you exactly what any of
> your functions is doing, step by step.

You have made this function:

(defun paragraphs-in-region (&optional start end msgp)
  "Return list of paragraphs in region.

By Drew Adams, Date: Mon, 5 Sep 2022 16:02:32 +0000, GNU Emacs Help."
  (interactive
   (if (use-region-p)
       (list (region-beginning) (region-end) t)
     (list (point-min) (point-max) t)))
  (let ((paras  ()))
    (save-excursion
      (goto-char start)
      (while (< (point) end)
        (push (buffer-substring-no-properties
               (point)
               (progn (forward-paragraph) (point)))
              paras))
      (setq paras  (nreverse paras))
      (when msgp (message "Paras: %S" paras))
      paras)))

Then I wanted to make general function to iterate over paragraphs this
way:

(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))))
    (save-excursion
      (goto-char start)
      (while (< (point) end)
        (funcall function)
        (forward-paragraph)))))

Then I use following functions to apply on the paragraph.

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

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

That means `join-lines-buffer-or-region' applies on paragraphs in
buffer or region.

Problem is in:

      (while (< (point) end)
        (funcall function)
        (forward-paragraph)))))

I still cannot understand WHEN is that happening, I just assume that
it happens when there are some empty lines on the end. Sometimes it
works, sometimes not.

-- 
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]