refactor(default,lib): doom--org-headings

This commit is contained in:
Henrik Lissner 2022-02-09 22:26:08 +01:00
parent 39886536c3
commit 009613a470
2 changed files with 30 additions and 17 deletions

View file

@ -107,12 +107,13 @@ selection of all minor-modes, active or not."
;;; Documentation commands ;;; Documentation commands
(defvar org-agenda-files) (defvar org-agenda-files)
(defun doom--org-headings (files &optional depth include-files) (cl-defun doom--org-headings (files &key depth mindepth include-files &allow-other-keys)
"TODO" "TODO"
(require 'org) (require 'org)
(let* ((default-directory doom-docs-dir) (let* ((default-directory doom-docs-dir)
(org-agenda-files (mapcar #'expand-file-name (doom-enlist files))) (org-agenda-files (mapcar #'expand-file-name (doom-enlist files)))
(depth (if (integerp depth) depth)) (depth (if (integerp depth) depth))
(mindepth (if (integerp mindepth) mindepth))
(org-inhibit-startup t)) (org-inhibit-startup t))
(message "Loading search results...") (message "Loading search results...")
(unwind-protect (unwind-protect
@ -124,6 +125,8 @@ selection of all minor-modes, active or not."
(org-heading-components) (org-heading-components)
(when (and (or (null depth) (when (and (or (null depth)
(<= level depth)) (<= level depth))
(or (null mindepth)
(>= level mindepth))
(or (null tags) (or (null tags)
(not (string-match-p ":TOC" tags)))) (not (string-match-p ":TOC" tags))))
(let ((path (org-get-outline-path)) (let ((path (org-get-outline-path))
@ -147,15 +150,18 @@ selection of all minor-modes, active or not."
(defvar ivy-sort-functions-alist) (defvar ivy-sort-functions-alist)
;;;###autoload ;;;###autoload
(defun doom-completing-read-org-headings (prompt files &optional depth include-files initial-input extra-candidates) (cl-defun doom-completing-read-org-headings
(prompt files &rest plist &key depth mindepth include-files initial-input extra-candidates action)
"TODO" "TODO"
(let ((alist (let ((alist
(append (doom--org-headings files depth include-files) (append (apply #'doom--org-headings files plist)
extra-candidates)) extra-candidates))
ivy-sort-functions-alist) ivy-sort-functions-alist)
(if-let (result (completing-read prompt alist nil nil initial-input)) (if-let (result (completing-read prompt alist nil nil initial-input))
(cl-destructuring-bind (file &optional location) (cl-destructuring-bind (file &optional location)
(cdr (assoc result alist)) (cdr (assoc result alist))
(if action
(funcall action file location)
(find-file file) (find-file file)
(cond ((functionp location) (cond ((functionp location)
(funcall location)) (funcall location))
@ -165,7 +171,7 @@ selection of all minor-modes, active or not."
(when (outline-invisible-p) (when (outline-invisible-p)
(save-excursion (save-excursion
(outline-previous-visible-heading 1) (outline-previous-visible-heading 1)
(org-show-subtree))))) (org-show-subtree))))))
(user-error "Aborted")))) (user-error "Aborted"))))
;;;###autoload ;;;###autoload
@ -213,7 +219,10 @@ will be automatically appended to the result."
"troubleshooting.org" "troubleshooting.org"
"tutorials.org" "tutorials.org"
"faq.org") "faq.org")
3 t initial-input :depth 3
:include-files t
:initial-input initial-input
:extra-candidates
(mapcar (lambda (x) (mapcar (lambda (x)
(setcar x (concat "Doom Modules > " (car x))) (setcar x (concat "Doom Modules > " (car x)))
x) x)
@ -249,7 +258,8 @@ will be automatically appended to the result."
(nreverse (doom-files-in (expand-file-name "news" doom-docs-dir) (nreverse (doom-files-in (expand-file-name "news" doom-docs-dir)
:match "/[0-9]" :match "/[0-9]"
:relative-to doom-docs-dir)) :relative-to doom-docs-dir))
nil t initial-input)) :include-files t
:initial-input initial-input))
;;;###autoload ;;;###autoload
(defun doom/help-faq (&optional initial-input) (defun doom/help-faq (&optional initial-input)
@ -257,7 +267,8 @@ will be automatically appended to the result."
(interactive) (interactive)
(doom-completing-read-org-headings (doom-completing-read-org-headings
"Find in FAQ: " (list "faq.org") "Find in FAQ: " (list "faq.org")
2 nil initial-input)) :depth 2
:initial-input initial-input))
;;;###autoload ;;;###autoload
(defun doom/help-news () (defun doom/help-news ()

View file

@ -141,4 +141,6 @@ ARG is set, prompt for a known project to search from."
"Jump to an Org headline in `org-agenda-files'." "Jump to an Org headline in `org-agenda-files'."
(interactive) (interactive)
(doom-completing-read-org-headings (doom-completing-read-org-headings
"Jump to org headline: " org-agenda-files 3 t)) "Jump to org headline: " org-agenda-files
:depth 3
:include-files t))