refactor(docs): use org-with-wide-buffer

It uses save-excursion internally and is a little safer (in case of
narrowing).
This commit is contained in:
Henrik Lissner 2022-02-01 23:43:43 +01:00
parent 839970c2de
commit bfa90e82e9
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -5,7 +5,7 @@
(defun doom--docs-hide-meta-h ()
"Hide all meta or comment lines."
(save-excursion
(org-with-wide-buffer
(goto-char (point-min))
(let (case-fold-search)
(while (re-search-forward "^[ \t]*\\#" nil t)
@ -26,7 +26,7 @@
(defun doom--docs-hide-drawers-h ()
"Hide all property drawers."
(save-excursion
(org-with-wide-buffer
(goto-char (point-min))
(while (re-search-forward org-drawer-regexp nil t)
(let ((beg (1- (match-beginning 0)))
@ -37,6 +37,7 @@
(defun doom--docs-hide-tags-h ()
"Hide tags in org headings."
(org-with-wide-buffer
(goto-char (point-min))
(while (re-search-forward org-heading-regexp nil t)
(when-let (tags (org-get-tags nil t))
@ -58,13 +59,13 @@
(re-search-forward " +:[^ ]" (line-end-position))
(match-beginning 0))
(line-end-position)
doom-docs-mode t)))))
doom-docs-mode t))))))
(defvar doom--docs-babel-cache nil)
(defun doom--docs-hide-src-blocks-h ()
"Hide babel blocks (and/or their results) depending on their :exports arg."
(org-with-wide-buffer
(let ((inhibit-read-only t))
(save-excursion
(goto-char (point-min))
(make-local-variable 'doom--docs-babel-cache)
(while (re-search-forward org-babel-src-block-regexp nil t)
@ -105,12 +106,13 @@
(goto-char pos)
(org-babel-remove-result)
(org-element-cache-refresh pos))
(kill-local-variable 'doom--docs-babel-cache)
(restore-buffer-modified-p nil))))))
(defvar doom--docs-macro-cache nil)
(defun doom--docs-expand-macros-h ()
"Expand {{{macros}}} with their value."
(save-excursion
(org-with-wide-buffer
(goto-char (point-min))
(make-local-variable 'doom--docs-macro-cache)
(while (re-search-forward "{{{[^}]+}}}" nil t)
@ -138,7 +140,7 @@
("<help>" . ,(if evilp "SPC h" "C-h")))))
(defun doom--docs-expand-kbd-h ()
"Replace special keywords in [[kbd:][...]] links."
(save-excursion
(org-with-wide-buffer
(let ((inhibit-read-only t))
(goto-char (point-min))
(while (re-search-forward "\\[\\[kbd:.*\\]\\[\\(.*<[^>]+>.*\\)\\]\\]" nil t)
@ -156,7 +158,7 @@
(defun doom--docs-realign-tables-h ()
"Realign tables, as they may have changed."
(save-excursion
(org-with-wide-buffer
(goto-char (point-min))
(while (re-search-forward org-table-line-regexp nil t)
(let ((inhibit-read-only t))
@ -324,11 +326,12 @@ Keeps track of its own IDs in `doom-docs-dir' and toggles `doom-docs-mode' when
"Activate `read-only-mode' if the current file exists and is non-empty."
;; The rationale: if it's empty or non-existant, you want to write an org
;; file, not read it.
(when (and buffer-file-name
(let ((file-name (buffer-file-name (buffer-base-buffer))))
(when (and file-name
(> (buffer-size) 0)
(not (string-prefix-p "." (file-name-base buffer-file-name)))
(file-exists-p buffer-file-name))
(read-only-mode +1)))
(not (string-prefix-p "." (file-name-base file-name)))
(file-exists-p file-name))
(read-only-mode +1))))
;;;###autoload (add-hook 'doom-docs-org-mode-hook #'doom-docs-read-only-h)