Update org-format function
This commit is contained in:
@@ -111,18 +111,35 @@
|
|||||||
;; Based off https://github.com/alphapapa/unpackaged.el#ensure-blank-lines-between-headings-and-before-contents
|
;; Based off https://github.com/alphapapa/unpackaged.el#ensure-blank-lines-between-headings-and-before-contents
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun org-format (&optional prefix)
|
(defun org-format (&optional prefix)
|
||||||
"Ensure blank lines exist after each org entry (before each heading).
|
"Ensure that blank lines exist between headings and between headings and their contents.
|
||||||
With PREFIX, operate on whole buffer"
|
With prefix, operate on whole buffer. Ensures that blank lines
|
||||||
|
exist after each headings's drawers."
|
||||||
(interactive "P")
|
(interactive "P")
|
||||||
(org-map-entries
|
(org-map-entries (lambda ()
|
||||||
(lambda ()
|
(org-with-wide-buffer
|
||||||
|
;; `org-map-entries' narrows the buffer, which prevents us from seeing
|
||||||
|
;; newlines before the current heading, so we do this part widened.
|
||||||
|
(while (not (looking-back "\n\n" nil))
|
||||||
|
;; Insert blank lines before heading.
|
||||||
|
(insert "\n")))
|
||||||
(let ((end (org-entry-end-position)))
|
(let ((end (org-entry-end-position)))
|
||||||
(goto-char end)
|
;; Insert blank lines before entry content
|
||||||
(unless (string-equal (string (char-before)) "\n")
|
(forward-line)
|
||||||
;; Insert blank line after entry end
|
(while (and (org-at-planning-p)
|
||||||
|
(< (point) (point-max)))
|
||||||
|
;; Skip planning lines
|
||||||
|
(forward-line))
|
||||||
|
(while (re-search-forward org-drawer-regexp end t)
|
||||||
|
;; Skip drawers. You might think that `org-at-drawer-p' would suffice, but
|
||||||
|
;; for some reason it doesn't work correctly when operating on hidden text.
|
||||||
|
;; This works, taken from `org-agenda-get-some-entry-text'.
|
||||||
|
(re-search-forward "^[ \t]*:END:.*\n?" end t)
|
||||||
|
(goto-char (match-end 0)))
|
||||||
|
(unless (or (= (point) (point-max))
|
||||||
|
(org-at-heading-p)
|
||||||
|
(looking-at-p "\n"))
|
||||||
(insert "\n"))))
|
(insert "\n"))))
|
||||||
t
|
t (if prefix
|
||||||
(if prefix
|
|
||||||
nil
|
nil
|
||||||
'tree)))
|
'tree)))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user