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:
parent
839970c2de
commit
bfa90e82e9
1 changed files with 78 additions and 75 deletions
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
(defun doom--docs-hide-meta-h ()
|
(defun doom--docs-hide-meta-h ()
|
||||||
"Hide all meta or comment lines."
|
"Hide all meta or comment lines."
|
||||||
(save-excursion
|
(org-with-wide-buffer
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(let (case-fold-search)
|
(let (case-fold-search)
|
||||||
(while (re-search-forward "^[ \t]*\\#" nil t)
|
(while (re-search-forward "^[ \t]*\\#" nil t)
|
||||||
|
@ -26,7 +26,7 @@
|
||||||
|
|
||||||
(defun doom--docs-hide-drawers-h ()
|
(defun doom--docs-hide-drawers-h ()
|
||||||
"Hide all property drawers."
|
"Hide all property drawers."
|
||||||
(save-excursion
|
(org-with-wide-buffer
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(while (re-search-forward org-drawer-regexp nil t)
|
(while (re-search-forward org-drawer-regexp nil t)
|
||||||
(let ((beg (1- (match-beginning 0)))
|
(let ((beg (1- (match-beginning 0)))
|
||||||
|
@ -37,80 +37,82 @@
|
||||||
|
|
||||||
(defun doom--docs-hide-tags-h ()
|
(defun doom--docs-hide-tags-h ()
|
||||||
"Hide tags in org headings."
|
"Hide tags in org headings."
|
||||||
(goto-char (point-min))
|
(org-with-wide-buffer
|
||||||
(while (re-search-forward org-heading-regexp nil t)
|
(goto-char (point-min))
|
||||||
(when-let (tags (org-get-tags nil t))
|
(while (re-search-forward org-heading-regexp nil t)
|
||||||
(when (or (member "noorg" tags)
|
(when-let (tags (org-get-tags nil t))
|
||||||
(member "unfold" tags))
|
(when (or (member "noorg" tags)
|
||||||
;; prevent `org-ellipsis' around hidden regions
|
(member "unfold" tags))
|
||||||
(org-show-entry))
|
;; prevent `org-ellipsis' around hidden regions
|
||||||
(if (member "noorg" tags)
|
(org-show-entry))
|
||||||
(org-flag-region (line-end-position 0)
|
(if (member "noorg" tags)
|
||||||
(save-excursion
|
(org-flag-region (line-end-position 0)
|
||||||
(org-end-of-subtree t)
|
(save-excursion
|
||||||
(forward-line 1)
|
(org-end-of-subtree t)
|
||||||
(if (and (bolp) (eolp))
|
(forward-line 1)
|
||||||
(line-beginning-position)
|
(if (and (bolp) (eolp))
|
||||||
(line-end-position 0)))
|
(line-beginning-position)
|
||||||
doom-docs-mode t)
|
(line-end-position 0)))
|
||||||
(org-flag-region (save-excursion
|
doom-docs-mode t)
|
||||||
(goto-char (line-beginning-position))
|
(org-flag-region (save-excursion
|
||||||
(re-search-forward " +:[^ ]" (line-end-position))
|
(goto-char (line-beginning-position))
|
||||||
(match-beginning 0))
|
(re-search-forward " +:[^ ]" (line-end-position))
|
||||||
(line-end-position)
|
(match-beginning 0))
|
||||||
doom-docs-mode t)))))
|
(line-end-position)
|
||||||
|
doom-docs-mode t))))))
|
||||||
|
|
||||||
(defvar doom--docs-babel-cache nil)
|
(defvar doom--docs-babel-cache nil)
|
||||||
(defun doom--docs-hide-src-blocks-h ()
|
(defun doom--docs-hide-src-blocks-h ()
|
||||||
"Hide babel blocks (and/or their results) depending on their :exports arg."
|
"Hide babel blocks (and/or their results) depending on their :exports arg."
|
||||||
(let ((inhibit-read-only t))
|
(org-with-wide-buffer
|
||||||
(save-excursion
|
(let ((inhibit-read-only t))
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(make-local-variable 'doom--docs-babel-cache)
|
(make-local-variable 'doom--docs-babel-cache)
|
||||||
(while (re-search-forward org-babel-src-block-regexp nil t)
|
(while (re-search-forward org-babel-src-block-regexp nil t)
|
||||||
(let* ((beg (match-beginning 0))
|
(let* ((beg (match-beginning 0))
|
||||||
(end (save-excursion (goto-char (match-end 0))
|
(end (save-excursion (goto-char (match-end 0))
|
||||||
(skip-chars-forward "\n")
|
(skip-chars-forward "\n")
|
||||||
(point)))
|
(point)))
|
||||||
(exports
|
(exports
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(goto-char beg)
|
(goto-char beg)
|
||||||
(and (re-search-forward " :exports \\([^ \n]+\\)" (line-end-position) t)
|
(and (re-search-forward " :exports \\([^ \n]+\\)" (line-end-position) t)
|
||||||
(match-string-no-properties 1))))
|
(match-string-no-properties 1))))
|
||||||
(results (org-babel-where-is-src-block-result)))
|
(results (org-babel-where-is-src-block-result)))
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(when (and (if (stringp exports)
|
(when (and (if (stringp exports)
|
||||||
(member exports '("results" "both"))
|
(member exports '("results" "both"))
|
||||||
org-export-use-babel)
|
org-export-use-babel)
|
||||||
(not results)
|
(not results)
|
||||||
doom-docs-mode)
|
doom-docs-mode)
|
||||||
(cl-pushnew beg doom--docs-babel-cache)
|
(cl-pushnew beg doom--docs-babel-cache)
|
||||||
(quiet! (org-babel-execute-src-block))
|
(quiet! (org-babel-execute-src-block))
|
||||||
(setq results (org-babel-where-is-src-block-result))
|
(setq results (org-babel-where-is-src-block-result))
|
||||||
(org-element-cache-refresh beg)
|
(org-element-cache-refresh beg)
|
||||||
(restore-buffer-modified-p nil)))
|
(restore-buffer-modified-p nil)))
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(when results
|
(when results
|
||||||
(when (member exports '("code" "both" "t"))
|
(when (member exports '("code" "both" "t"))
|
||||||
(setq beg results))
|
(setq beg results))
|
||||||
(when (member exports '("none" "code"))
|
(when (member exports '("none" "code"))
|
||||||
(setq end (progn (goto-char results)
|
(setq end (progn (goto-char results)
|
||||||
(goto-char (org-babel-result-end))
|
(goto-char (org-babel-result-end))
|
||||||
(skip-chars-forward "\n")
|
(skip-chars-forward "\n")
|
||||||
(point))))))
|
(point))))))
|
||||||
(org-flag-region beg end doom-docs-mode t)))
|
(org-flag-region beg end doom-docs-mode t)))
|
||||||
(unless doom-docs-mode
|
(unless doom-docs-mode
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(dolist (pos doom--docs-babel-cache)
|
(dolist (pos doom--docs-babel-cache)
|
||||||
(goto-char pos)
|
(goto-char pos)
|
||||||
(org-babel-remove-result)
|
(org-babel-remove-result)
|
||||||
(org-element-cache-refresh pos))
|
(org-element-cache-refresh pos))
|
||||||
(restore-buffer-modified-p nil))))))
|
(kill-local-variable 'doom--docs-babel-cache)
|
||||||
|
(restore-buffer-modified-p nil))))))
|
||||||
|
|
||||||
(defvar doom--docs-macro-cache nil)
|
(defvar doom--docs-macro-cache nil)
|
||||||
(defun doom--docs-expand-macros-h ()
|
(defun doom--docs-expand-macros-h ()
|
||||||
"Expand {{{macros}}} with their value."
|
"Expand {{{macros}}} with their value."
|
||||||
(save-excursion
|
(org-with-wide-buffer
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(make-local-variable 'doom--docs-macro-cache)
|
(make-local-variable 'doom--docs-macro-cache)
|
||||||
(while (re-search-forward "{{{[^}]+}}}" nil t)
|
(while (re-search-forward "{{{[^}]+}}}" nil t)
|
||||||
|
@ -138,7 +140,7 @@
|
||||||
("<help>" . ,(if evilp "SPC h" "C-h")))))
|
("<help>" . ,(if evilp "SPC h" "C-h")))))
|
||||||
(defun doom--docs-expand-kbd-h ()
|
(defun doom--docs-expand-kbd-h ()
|
||||||
"Replace special keywords in [[kbd:][...]] links."
|
"Replace special keywords in [[kbd:][...]] links."
|
||||||
(save-excursion
|
(org-with-wide-buffer
|
||||||
(let ((inhibit-read-only t))
|
(let ((inhibit-read-only t))
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(while (re-search-forward "\\[\\[kbd:.*\\]\\[\\(.*<[^>]+>.*\\)\\]\\]" nil t)
|
(while (re-search-forward "\\[\\[kbd:.*\\]\\[\\(.*<[^>]+>.*\\)\\]\\]" nil t)
|
||||||
|
@ -156,7 +158,7 @@
|
||||||
|
|
||||||
(defun doom--docs-realign-tables-h ()
|
(defun doom--docs-realign-tables-h ()
|
||||||
"Realign tables, as they may have changed."
|
"Realign tables, as they may have changed."
|
||||||
(save-excursion
|
(org-with-wide-buffer
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(while (re-search-forward org-table-line-regexp nil t)
|
(while (re-search-forward org-table-line-regexp nil t)
|
||||||
(let ((inhibit-read-only 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."
|
"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
|
;; The rationale: if it's empty or non-existant, you want to write an org
|
||||||
;; file, not read it.
|
;; file, not read it.
|
||||||
(when (and buffer-file-name
|
(let ((file-name (buffer-file-name (buffer-base-buffer))))
|
||||||
(> (buffer-size) 0)
|
(when (and file-name
|
||||||
(not (string-prefix-p "." (file-name-base buffer-file-name)))
|
(> (buffer-size) 0)
|
||||||
(file-exists-p buffer-file-name))
|
(not (string-prefix-p "." (file-name-base file-name)))
|
||||||
(read-only-mode +1)))
|
(file-exists-p file-name))
|
||||||
|
(read-only-mode +1))))
|
||||||
|
|
||||||
;;;###autoload (add-hook 'doom-docs-org-mode-hook #'doom-docs-read-only-h)
|
;;;###autoload (add-hook 'doom-docs-org-mode-hook #'doom-docs-read-only-h)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue