refactor(org): move doom-docs link defs to :lang org

Where they will be further generalized, later.

This also prevents an issue where org was loaded while the profile init
files are generated, which caused a warning about org-loaddefs which
introduces a noticable delay.
This commit is contained in:
Henrik Lissner 2022-09-16 03:10:36 +02:00
parent 511f0ca38f
commit ab9896c526
No known key found for this signature in database
GPG key ID: B60957CA074D39A3
3 changed files with 198 additions and 235 deletions

View file

@ -467,208 +467,4 @@ Keeps track of its own IDs in `doom-docs-dir' and toggles `doom-docs-mode' when
(add-hook 'doom-docs-org-mode-hook #'doom-docs-read-only-h)
;;
;;; Custom links
;;;###autoload
(defun doom-docs-read-link-desc-at-point (&optional default context)
"TODO"
(if (and (stringp default) (not (string-empty-p default)))
(string-trim default)
(if-let* ((context (or context (org-element-context)))
(context (org-element-lineage context '(link) t))
(beg (org-element-property :contents-begin context))
(end (org-element-property :contents-end context)))
(unless (= beg end)
(replace-regexp-in-string
"[ \n]+" " " (string-trim (buffer-substring-no-properties beg end)))))))
;;;###autoload
(defun doom-docs-doom-module-link-follow-fn (link)
(cl-destructuring-bind (&key category module flag)
(doom-docs--read-module-link link)
(when category
(let ((doom-modules-dirs (list doom-modules-dir)))
(if-let* ((path (doom-module-locate-path category module))
(path (or (car (doom-glob path "README.org"))
path)))
(find-file path)
(user-error "Can't find Doom module '%s'" link))))
(when flag
(goto-char (point-min))
(when (and (re-search-forward "^\\*+ \\(?:TODO \\)?Module flags")
(re-search-forward (format "^\\s-*- \\+%s ::[ \n]"
(substring (symbol-name flag) 1))
(save-excursion (org-get-next-sibling)
(point))))
(org-show-entry)
(recenter)))))
;;;###autoload
(defun doom-docs-doom-module-link-face-fn (link)
(cl-destructuring-bind (&key category module flag)
(doom-docs--read-module-link link)
(if (doom-module-locate-path category module)
`(:inherit org-priority
:weight bold)
'error)))
;;;###autoload
(defun doom-docs-doom-package-link-follow-fn (link)
"TODO"
(doom/describe-package
(intern-soft
(doom-docs-read-link-desc-at-point link))))
;;;###autoload
(defun doom-docs-make-symbol-link (fn)
"TODO"
(lambda (link)
(let ((desc (doom-docs-read-link-desc-at-point link)))
(funcall
fn (or (intern-soft desc)
(user-error "Can't find documentation for %S" desc))))))
(defun doom-docs--describe-kbd (keystr)
(dolist (key `(("<leader>" . ,doom-leader-key)
("<localleader>" . ,doom-localleader-key)
("<prefix>" . ,(if (bound-and-true-p evil-mode)
(concat doom-leader-key " u")
"C-u"))
("<help>" . ,(if (bound-and-true-p evil-mode)
(concat doom-leader-key " h")
"C-h"))
("\\<M-" . "alt-")
("\\<S-" . "shift-")
("\\<s-" . "super-")
("\\<C-" . "ctrl-")))
(setq keystr
(replace-regexp-in-string (car key) (cdr key)
keystr t t)))
keystr)
;;;###autoload
(defun doom-docs-read-kbd-at-point (&optional default context)
"TODO"
(doom-docs--describe-kbd
(doom-docs-read-link-desc-at-point default context)))
;;;###autoload
(after! org
;; Add "lookup" links for packages and keystrings; useful for Emacs
;; documentation -- especially Doom's!
(org-link-set-parameters
"kbd"
:follow (lambda (_) (minibuffer-message "%s" (doom--display-docs-link-in-eldoc-a)))
:help-echo #'doom-docs-read-kbd-at-point
:face 'help-key-binding)
(org-link-set-parameters
"var"
:follow (doom-docs-make-symbol-link #'helpful-variable)
:face '(font-lock-variable-name-face underline))
(org-link-set-parameters
"fn"
:follow (doom-docs-make-symbol-link #'helpful-callable)
:face '(font-lock-function-name-face underline))
(org-link-set-parameters
"face"
:follow (doom-docs-make-symbol-link #'describe-face)
:face '(font-lock-type-face underline))
(org-link-set-parameters
"doom-ref"
:follow (lambda (link)
(let ((link (doom-docs-read-link-desc-at-point link))
(url "https://github.com")
(doom-repo "doomemacs/doomemacs"))
(save-match-data
(browse-url
(cond ((string-match "^\\([^/]+\\(?:/[^/]+\\)?\\)?#\\([0-9]+\\(?:#.*\\)?\\)" link)
(format "%s/%s/issues/%s" url
(or (match-string 1 link)
doom-repo)
(match-string 2 link)))
((string-match "^\\([^/]+\\(?:/[^/]+\\)?@\\)?\\([a-z0-9]\\{7,\\}\\(?:#.*\\)?\\)" link)
(format "%s/%s/commit/%s" url
(or (match-string 1 link)
doom-repo)
(match-string 2 link)))
((user-error "Invalid doom-ref link: %S" link)))))))
:face (lambda (link)
(let ((link (doom-docs-read-link-desc-at-point link)))
(if (or (string-match "^\\([^/]+\\(?:/[^/]+\\)?\\)?#\\([0-9]+\\(?:#.*\\)?\\)" link)
(string-match "^\\([^/]+\\(?:/[^/]+\\)?@\\)?\\([a-z0-9]\\{7,\\}\\(?:#.*\\)?\\)" link))
'org-link
'error))))
(org-link-set-parameters
"doom-user"
:follow (lambda (link)
(browse-url
(format "https://github.com/%s"
(string-remove-prefix
"@" (doom-docs-read-link-desc-at-point link)))))
:face (lambda (_) 'org-priority))
(org-link-set-parameters
"doom-package"
:follow #'doom-docs-doom-package-link-follow-fn
:face (lambda (_) '(:inherit org-priority :slant italic)))
(org-link-set-parameters
"doom-source"
:follow (lambda (link)
(user-error "-- %S %S %S" source url link)
(cl-destructuring-bind (source . url)
(save-match-data
(and (string-match "^\\([^:]+\\):\\(.+\\)$" link)
(cons (match-string 1) (match-string 2))))
(pcase source
("doom"
(org-link-open (expand-file-name url doom-modules-dir)))
("contrib"
(browse-url (format "https://docs.doomemacs.org/modules/"
(replace-regexp-in-string "::\\(.+\\)$" "#\\1" url))))
(_ (user-error "%s is not a valid module source" source))))))
(org-link-set-parameters
"doom-module"
:follow #'doom-docs-doom-module-link-follow-fn
:face #'doom-docs-doom-module-link-face-fn)
(org-link-set-parameters
"doom-changelog"
:follow (lambda (link)
(find-file (doom-path doom-docs-dir "changelog.org"))
(org-match-sparse-tree nil link)))
(add-to-list 'org-link-abbrev-alist '("doom-repo" . "https://github.com/doomemacs/doomemacs/%s"))
(defadvice! doom--display-docs-link-in-eldoc-a (&rest _)
"Display full doom-*: links in minibuffer when cursor/mouse is over it."
:before-until #'org-eldoc-documentation-function
(when-let* ((context (org-element-context))
(path (org-element-property :path context)))
(pcase (org-element-property :type context)
("kbd"
(format "%s %s"
(propertize "Key sequence:" 'face 'bold)
(propertize (doom-docs-read-kbd-at-point path context)
'face 'help-key-binding)))
("doom-module"
(format "%s %s"
(propertize "Doom module:" 'face 'bold)
(propertize (doom-docs-read-link-desc-at-point path)
'face 'org-priority)))
("doom-package"
(format "%s %s"
(propertize "Doom package:" 'face 'bold)
(propertize (doom-docs-read-link-desc-at-point path)
'face 'org-priority)))))))
(defun doom-docs--read-module-link (link)
(cl-destructuring-bind (category &optional module flag)
(let ((desc (doom-docs-read-link-desc-at-point link)))
(if (string-prefix-p "+" (string-trim-left desc))
(list nil nil (intern desc))
(mapcar #'intern (split-string desc " " nil))))
(list :category category
:module module
:flag flag)))
;;; docs.el ends here

View file

@ -40,6 +40,90 @@ exist, and `org-link' otherwise."
'error)))
(plist-put plist :requires nil))))
;;;###autoload
(defun +org-link-read-desc-at-point (&optional default context)
"TODO"
(if (and (stringp default) (not (string-empty-p default)))
(string-trim default)
(if-let* ((context (or context (org-element-context)))
(context (org-element-lineage context '(link) t))
(beg (org-element-property :contents-begin context))
(end (org-element-property :contents-end context)))
(unless (= beg end)
(replace-regexp-in-string
"[ \n]+" " " (string-trim (buffer-substring-no-properties beg end)))))))
;;;###autoload
(defun +org-link-read-kbd-at-point (&optional default context)
"TODO"
(+org-link--describe-kbd
(+org-link-read-desc-at-point default context)))
(defun +org-link--describe-kbd (keystr)
(dolist (key `(("<leader>" . ,doom-leader-key)
("<localleader>" . ,doom-localleader-key)
("<prefix>" . ,(if (bound-and-true-p evil-mode)
(concat doom-leader-key " u")
"C-u"))
("<help>" . ,(if (bound-and-true-p evil-mode)
(concat doom-leader-key " h")
"C-h"))
("\\<M-" . "alt-")
("\\<S-" . "shift-")
("\\<s-" . "super-")
("\\<C-" . "ctrl-")))
(setq keystr
(replace-regexp-in-string (car key) (cdr key)
keystr t t)))
keystr)
(defun +org-link--read-module-link (link)
(cl-destructuring-bind (category &optional module flag)
(let ((desc (+org-link-read-desc-at-point link)))
(if (string-prefix-p "+" (string-trim-left desc))
(list nil nil (intern desc))
(mapcar #'intern (split-string desc " " nil))))
(list :category category
:module module
:flag flag)))
;;;###autoload
(defun +org-link--doom-module-link-face-fn (link)
(cl-destructuring-bind (&key category module flag)
(+org-link--read-module-link link)
(if (doom-module-locate-path category module)
`(:inherit org-priority
:weight bold)
'error)))
;;;###autoload
(defun +org-link-follow-doom-module-fn (link)
"TODO"
(cl-destructuring-bind (&key category module flag)
(+org-link--read-module-link link)
(when category
(let ((doom-modules-dirs (list doom-modules-dir)))
(if-let* ((path (doom-module-locate-path category module))
(path (or (car (doom-glob path "README.org"))
path)))
(find-file path)
(user-error "Can't find Doom module '%s'" link))))
(when flag
(goto-char (point-min))
(when (and (re-search-forward "^\\*+ \\(?:TODO \\)?Module flags")
(re-search-forward (format "^\\s-*- \\+%s ::[ \n]"
(substring (symbol-name flag) 1))
(save-excursion (org-get-next-sibling)
(point))))
(org-show-entry)
(recenter)))))
;;;###autoload
(defun +org-link-follow-doom-package-fn (link)
"TODO"
(doom/describe-package
(intern-soft
(+org-link-read-desc-at-point link))))
;;
;;; Image data functions (for custom inline images)

View file

@ -182,14 +182,6 @@ Is relative to `org-directory', unless it is absolute. Is used in Doom's default
("NO" . +org-todo-cancel)
("KILL" . +org-todo-cancel)))
(defadvice! +org-display-link-in-eldoc-a (&rest _)
"Display full link in minibuffer when cursor/mouse is over it."
:before-until #'org-eldoc-documentation-function
(when-let* ((context (org-element-context))
(path (org-element-property :path context)))
(pcase (org-element-property :type context)
(type (format "Link: %s" (org-element-property :raw-link context))))))
;; Automatic indent detection in org files is meaningless
(add-to-list 'doom-detect-indentation-excluded-modes 'org-mode)
@ -510,12 +502,10 @@ relative to `org-directory', unless it is an absolute path."
(defun +org-init-custom-links-h ()
;; Modify default file: links to colorize broken file links red
(org-link-set-parameters
"file"
:face (lambda (path)
"file" :face (lambda (path)
(if (or (file-remote-p path)
;; filter out network shares on windows (slow)
(and IS-WINDOWS
(string-prefix-p "\\\\" path))
(if IS-WINDOWS (string-prefix-p "\\\\" path))
(file-exists-p path))
'org-link
'(warning org-link))))
@ -530,7 +520,7 @@ relative to `org-directory', unless it is an absolute path."
'("duckduckgo" . "https://duckduckgo.com/?q=%s")
'("wikipedia" . "https://en.wikipedia.org/wiki/%s")
'("wolfram" . "https://wolframalpha.com/input/?i=%s")
'("doom" . "https://github.com/hlissner/doom-emacs/%s")
'("doom-repo" . "https://github.com/doomemacs/doomemacs/%s")
`("emacsdir" . ,(doom-path doom-emacs-dir "%s"))
`("doomdir" . ,(doom-path doom-user-dir "%s")))
@ -539,6 +529,112 @@ relative to `org-directory', unless it is an absolute path."
(+org-define-basic-link "doom-docs" 'doom-docs-dir)
(+org-define-basic-link "doom-modules" 'doom-modules-dir)
(defadvice! +org-display-link-in-eldoc-a (&rest _)
"Display full link in minibuffer when cursor/mouse is over it."
:before-until #'org-eldoc-documentation-function
(when-let (context (org-element-context))
(if-let ((type (org-element-property :type context))
(eldocfn (org-link-get-parameter type :eldoc)))
(funcall eldocfn context)
(format "Link: %s" (org-element-property :raw-link context)))))
;; Add "lookup" links for packages and keystrings; useful for Emacs
;; documentation -- especially Doom's!
(letf! ((defun -call-interactively (fn)
(lambda (link)
(let ((desc (+org-link-read-desc-at-point link)))
(funcall
fn (or (intern-soft desc)
(user-error "Can't find documentation for %S" desc))))))
(defun -eldoc-fn (label face)
(lambda (context)
(format "%s %s"
(propertize (format "%s:" label) 'face 'bold)
(propertize (+org-link-read-desc-at-point
(org-element-property :path context) context)
'face face)))))
(org-link-set-parameters
"kbd"
:follow (lambda (_) (minibuffer-message "%s" (+org-display-link-in-eldoc-a)))
:help-echo #'+org-link-read-kbd-at-point
:face 'help-key-binding
:eldoc (-eldoc-fn "Key sequence" 'help-key-binding))
(org-link-set-parameters
"var"
:follow (-call-interactively #'helpful-variable)
:face '(font-lock-variable-name-face underline))
(org-link-set-parameters
"fn"
:follow (-call-interactively #'helpful-callable)
:face '(font-lock-function-name-face underline))
(org-link-set-parameters
"face"
:follow (-call-interactively #'describe-face)
:face '(font-lock-type-face underline))
(org-link-set-parameters
"doom-package"
:follow #'+org-link-follow-doom-package-fn
:face (lambda (_) '(:inherit org-priority :slant italic))
:eldoc (-eldoc-fn "Doom package" 'org-priority))
(org-link-set-parameters
"doom-module"
:follow #'+org-link-follow-doom-module-fn
:face #'+org-link--doom-module-link-face-fn
:eldoc (-eldoc-fn "Doom module" 'org-priority))
(org-link-set-parameters
"doom-ref"
:follow (lambda (link)
(let ((link (+org-link-read-desc-at-point link))
(url "https://github.com")
(doom-repo "doomemacs/doomemacs"))
(save-match-data
(browse-url
(cond ((string-match "^\\([^/]+\\(?:/[^/]+\\)?\\)?#\\([0-9]+\\(?:#.*\\)?\\)" link)
(format "%s/%s/issues/%s" url
(or (match-string 1 link)
doom-repo)
(match-string 2 link)))
((string-match "^\\([^/]+\\(?:/[^/]+\\)?@\\)?\\([a-z0-9]\\{7,\\}\\(?:#.*\\)?\\)" link)
(format "%s/%s/commit/%s" url
(or (match-string 1 link)
doom-repo)
(match-string 2 link)))
((user-error "Invalid doom-ref link: %S" link)))))))
:face (lambda (link)
(let ((link (+org-link-read-desc-at-point link)))
(if (or (string-match "^\\([^/]+\\(?:/[^/]+\\)?\\)?#\\([0-9]+\\(?:#.*\\)?\\)" link)
(string-match "^\\([^/]+\\(?:/[^/]+\\)?@\\)?\\([a-z0-9]\\{7,\\}\\(?:#.*\\)?\\)" link))
'org-link
'error))))
(org-link-set-parameters
"doom-user"
:follow (lambda (link)
(browse-url
(format "https://github.com/%s"
(string-remove-prefix
"@" (+org-link-read-desc-at-point link)))))
:face (lambda (_) 'org-priority))
(org-link-set-parameters
"doom-source"
:follow (lambda (link)
(user-error "-- %S %S %S" source url link)
(cl-destructuring-bind (source . url)
(save-match-data
(and (string-match "^\\([^:]+\\):\\(.+\\)$" link)
(cons (match-string 1) (match-string 2))))
(pcase source
("doom"
(org-link-open (expand-file-name url doom-modules-dir)))
("contrib"
(browse-url (format "https://docs.doomemacs.org/modules/"
(replace-regexp-in-string "::\\(.+\\)$" "#\\1" url))))
(_ (user-error "%s is not a valid module source" source))))))
(org-link-set-parameters
"doom-changelog"
:follow (lambda (link)
(find-file (doom-path doom-docs-dir "changelog.org"))
(org-match-sparse-tree nil link))))
;; TODO PR this upstream
(defadvice! +org--follow-search-string-a (fn link &optional arg)
"Support ::SEARCH syntax for id: links."
@ -564,19 +660,6 @@ relative to `org-directory', unless it is an absolute path."
;; Add "lookup" links for packages and keystrings; useful for Emacs
;; documentation -- especially Doom's!
(org-link-set-parameters
"kbd"
:follow (lambda (_) (minibuffer-message "%s" (+org-display-link-in-eldoc-a)))
:help-echo #'+org-read-kbd-at-point
:face 'help-key-binding)
(org-link-set-parameters
"doom-package"
:follow #'+org-link--doom-package-follow-fn
:face (lambda (_) '(:inherit org-priority :slant italic)))
(org-link-set-parameters
"doom-module"
:follow #'+org-link--doom-module-follow-fn
:face #'+org-link--doom-module-face-fn)
;; Allow inline image previews of http(s)? urls or data uris.
;; `+org-http-image-data-fn' will respect `org-display-remote-inline-images'.