[Top][All Lists]

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

Re: Run `fill-paragraph' on all the paragraphs in the currently opened L

From: Emanuel Berg
Subject: Re: Run `fill-paragraph' on all the paragraphs in the currently opened LaTeX document.
Date: Mon, 18 Oct 2021 18:28:10 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Hongyi Zhao wrote:

> So the idea suggested by Emanuel Berg and you now can be
> combined into the following:
> Iterate the whole buffer line-by-line with a semantic line
> as the unit block, and check each line if it starts with '\'
> or not, e.g. with (looking-at "\\\\"). When you find one
> "beg", start looking for where it ends, and when you find
> that push (beg end) onto a list.
> Then loop through the list and do (fill-region beg end) for
> each list pair item ...

Heh, interesting :)

But I think the advantage of semantic lines in terms of
filling is they will be so short that won't be necessary, if
one still does it maybe it will just be confusing (neither
fish nor fowl).

BTW one can optimize the algorithm proposed above by searching
for regexps instead of the line-by-line approach ... also it
is a good case for a DWIM interface, so, when used
interactively, the whole buffer will be filled that way only
if there is no region ...

;;; -*- lexical-binding: t -*-
;;; this file:

;; DWIM interface

(defun test-dwim (&optional beg end)
  (interactive (when (use-region-p)
                 (list (region-beginning) (region-end)) ))
  (let ((bg (or beg (point-min))) ; or just (point) here
        (ed (or end (point-max))) )
    (list bg ed) ; code here

;; test the interface

(when nil
    (set-mark   10)
    (goto-char 500)
    (call-interactively #'test-dwim) ) ; (10  500)
  (call-interactively #'test-dwim)     ; ( 1 1163)
  (test-dwim 30 90)                    ; (30   90)
  (test-dwim)                          ; ( 1 1163)

;; example function

(defun count-chars (&optional beg end)
  (interactive (when (use-region-p)
                 (list (region-beginning) (region-end)) ))
  (let*((bg   (or beg (point-min)))
        (ed   (or end (point-max)))
        (diff (- ed bg)) )
      (message "%d" diff) )))

;; test the example function

;; (call-interactively #'count-chars) ; 1162
;; (count-chars)                      ; 1162
;; (count-chars 10 40)                ; 30
;; (+ 1111 (count-chars))             ; 2273

underground experts united

reply via email to

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