0

Q: how can I detect, programmatically, if a paragraph has been filled?

fill-paragraph (synonyms fail me) fills a paragraph, and related fill-* functions do analogous things. Is there a way to detect, programmatically, if a paragraph has already been filled?

Dan
  • 32,584
  • 6
  • 98
  • 168
  • What are you trying to do? I use a function that fills a paragraph on first invocation, and then alternates between unfilling/filling on subsequent key presses. Maybe that would be useful? – Tyler Jul 28 '21 at 15:50
  • @Tyler: I am, in fact, trying to toggle between filled and unfilled paragraphs. I have functions for both, and just need a way to detect which should be deployed. – Dan Jul 28 '21 at 16:11
  • That's what I thought. See compact-uncompact-block here: https://emacs.stackexchange.com/a/1057/262 – Tyler Jul 28 '21 at 17:38
  • Here's @phils version: https://stackoverflow.com/a/14992483/523044 – Tyler Jul 28 '21 at 17:39

1 Answers1

0

Here is an ugly hack that works for me:

(defun fill-paragraph-p ()
  (let ((hash (buffer-hash))
        result)
    (save-excursion
      (fill-paragraph nil t)
      (setq result (equal hash (buffer-hash)))
      (prog1 result
        (unless result 
          (undo-start)
          (undo-more 1))))))

It's simple: hash the current buffer, fill the paragraph, then compare the hash of the buffer and the old hash. If it's the same, the paragraph is filled; otherwise, is not, and we undo fill-paragraph.

Firmin Martin
  • 1,265
  • 7
  • 23