;;; lang/org/autoload/org-link.el -*- lexical-binding: t; -*- (defun +org--relative-path (path root) (if (and buffer-file-name (file-in-directory-p buffer-file-name root)) (file-relative-name path) path)) (defun +org--read-link-path (key dir &optional fn) (let ((file (funcall (or fn #'read-file-name) (format "%s: " (capitalize key)) dir))) (format "%s:%s" key (file-relative-name file dir)))) ;;;###autoload (defun +org-define-basic-link (key dir-var &rest plist) "Define a link with some basic completion & fontification. KEY is the name of the link type. DIR-VAR is the directory variable to resolve links relative to. PLIST is passed to `org-link-set-parameters' verbatim. Links defined with this will be rendered in the `error' face if the file doesn't exist, and `org-link' otherwise." (declare (indent 2)) (let ((requires (plist-get plist :requires)) (dir-fn (if (functionp dir-var) dir-var (lambda () (symbol-value dir-var))))) (apply #'org-link-set-parameters key :complete (lambda () (if requires (mapc #'require (ensure-list requires))) (+org--relative-path (+org--read-link-path key (funcall dir-fn)) (funcall dir-fn))) :follow (lambda (link) (org-link-open-as-file (expand-file-name link (funcall dir-fn)) nil)) :face (lambda (link) (let* ((path (expand-file-name link (funcall dir-fn))) (option-index (string-match-p "::\\(.*\\)\\'" path)) (file-name (substring path 0 option-index))) (if (file-exists-p file-name) 'org-link '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 `(("" . ,doom-leader-key) ("" . ,doom-localleader-key) ("" . ,(if (bound-and-true-p evil-mode) (concat doom-leader-key " u") "C-u")) ("" . ,(if (bound-and-true-p evil-mode) (concat doom-leader-key " h") "C-h")) ("\\