May 21st 2026 - Things I Worked On
Removed empty heading and subheadings from Weekly Roundup
I use a template I created for my Weekly Roundup posts that has every possible category for the links I share (I'm always adding more). I wanted a way to clean up the file and remove empty categories, which are represented as heading and subheadings in the file after I'm done moving the links to the right spots. Here's the code I came up with to automate that:
(defun blog-delete-empty-weekly-roundup-elements () "Remove all empty Org headings and subheadings that have no content or children." (interactive) (save-excursion (goto-char (point-max)) ;; Move backward heading by heading (while (re-search-backward org-heading-regexp nil t) (let* ((element (org-element-at-point)) ;; Safely copy properties to avoid cache mutation side-effects (begin (org-element-property :begin element)) (end (org-element-property :end element)) (contents-begin (org-element-property :contents-begin element)) (contents-end (org-element-property :contents-end element))) ;; A heading is empty if it has no content block, or the content is just whitespace (when (or (not contents-begin) (string-match-p "\\`[ \t\n\r]*\\'" (buffer-substring-no-properties contents-begin contents-end))) (delete-region begin end))))))