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?

  • 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 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 at 16:11
  • That's what I thought. See compact-uncompact-block here: emacs.stackexchange.com/a/1057/262
    – Tyler
    Jul 28 at 17:38
  • Here's @phils version: stackoverflow.com/a/14992483/523044
    – Tyler
    Jul 28 at 17:39

Here is an ugly hack that works for me:

(defun fill-paragraph-p ()
  (let ((hash (buffer-hash))
      (fill-paragraph nil t)
      (setq result (equal hash (buffer-hash)))
      (prog1 result
        (unless result 
          (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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.