リンクの内容をeldocで表示

eldoc の機能を用いてカーソル下のリンクをエコーエリアに出す設定です.意外と簡単にできました.

(with-eval-after-load "org"
  (add-hook 'org-mode-hook #'my-load-echo-org-link)

  (defun my-echo-org-link ()
    (when (org-in-regexp org-link-bracket-re 1)
      (let ((link "Link:")
            (msg (org-link-unescape (match-string-no-properties 1))))
        (put-text-property 0 (length link) 'face 'minibuffer-prompt link)
        (eldoc-message (format "%s %s" link msg)))))

  ;; これはダメな例
  ;; (defun my-load-echo-org-link ()
  ;;   (setq-local eldoc-documentation-function #'my-echo-org-link))
  ;; こちらが正しい実装
  (defun my-load-echo-org-link ()
    (add-function :before-until (local 'eldoc-documentation-function)
                  #'my-echo-org-link)))

なお, org-eldoc と併用する場合, eldoc-documentation-function が専有されてしまうので,次のように advice して回避します.

(when (require 'org-eldoc nil t)
  (defun my-org-eldoc-load ()
    "Set up org-eldoc documentation function."
    (interactive)
    (add-function :before-until (local 'eldoc-documentation-function)
                  #'org-eldoc-documentation-function))
  (advice-add 'org-eldoc-load :override #'my-org-eldoc-load))

Updates

  • [2019-11-19 Tue] my-load-echo-org-link の実装を修正
amazon_banner_large
amazon_banner_small