org-eldoc.el 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. ;;; org-eldoc.el --- display org header and src block info using eldoc -*- lexical-binding: t; -*-
  2. ;; Copyright (c) 2014-2021 Free Software Foundation, Inc.
  3. ;; Author: Łukasz Gruner <lukasz@gruner.lu>
  4. ;; Maintainer: Łukasz Gruner <lukasz@gruner.lu>
  5. ;; Version: 6
  6. ;; Package-Requires: ((org "8"))
  7. ;; URL: https://bitbucket.org/ukaszg/org-eldoc
  8. ;; Created: 25/05/2014
  9. ;; Keywords: eldoc, outline, breadcrumb, org, babel, minibuffer
  10. ;; This file is not part of Emacs.
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;; Commentary:
  22. ;;; Changelog:
  23. ;; As of 01/11/14 switching license to GPL3 to allow submission to org-mode.
  24. ;; 08/11/14 switch code to automatically define eldoc-documentation-function, but don't autostart eldoc-mode.
  25. ;;; Code:
  26. (require 'org)
  27. (require 'ob-core)
  28. (require 'eldoc)
  29. (declare-function org-element-at-point "org-element" ())
  30. (declare-function org-element-property "org-element" (property element))
  31. (declare-function org-element-type "org-element" (element))
  32. (defgroup org-eldoc nil "" :group 'org)
  33. (defcustom org-eldoc-breadcrumb-separator "/"
  34. "Breadcrumb separator."
  35. :group 'org-eldoc
  36. :type 'string)
  37. (defcustom org-eldoc-test-buffer-name " *Org-eldoc test buffer*"
  38. "Name of the buffer used while testing for mode-local variable values."
  39. :group 'org-eldoc
  40. :type 'string)
  41. (defun org-eldoc-get-breadcrumb ()
  42. "Return breadcrumb if on a headline or nil."
  43. (let ((case-fold-search t) cur)
  44. (save-excursion
  45. (beginning-of-line)
  46. (save-match-data
  47. (when (looking-at org-complex-heading-regexp)
  48. (setq cur (match-string 4))
  49. (org-format-outline-path
  50. (append (org-get-outline-path) (list cur))
  51. (frame-width) "" org-eldoc-breadcrumb-separator))))))
  52. (defun org-eldoc-get-src-header ()
  53. "Returns lang and list of header properties if on src definition line and nil otherwise."
  54. (let ((case-fold-search t) info lang hdr-args)
  55. (save-excursion
  56. (beginning-of-line)
  57. (save-match-data
  58. (when (looking-at "^[ \t]*#\\+\\(begin\\|end\\)_src")
  59. (setq info (org-babel-get-src-block-info 'light)
  60. lang (propertize (or (nth 0 info) "no lang") 'face 'font-lock-string-face)
  61. hdr-args (nth 2 info))
  62. (concat
  63. lang
  64. ": "
  65. (mapconcat
  66. (lambda (elem)
  67. (when (and (cdr elem) (not (string= "" (cdr elem))))
  68. (concat
  69. (propertize (symbol-name (car elem)) 'face 'org-list-dt)
  70. " "
  71. (propertize (cdr elem) 'face 'org-verbatim)
  72. " ")))
  73. hdr-args " ")))))))
  74. (defun org-eldoc-get-src-lang ()
  75. "Return value of lang for the current block if in block body and nil otherwise."
  76. (let ((element (save-match-data (org-element-at-point))))
  77. (and (eq (org-element-type element) 'src-block)
  78. (>= (line-beginning-position)
  79. (org-element-property :post-affiliated element))
  80. (<=
  81. (line-end-position)
  82. (org-with-wide-buffer
  83. (goto-char (org-element-property :end element))
  84. (skip-chars-backward " \t\n")
  85. (line-end-position)))
  86. (org-element-property :language element))))
  87. (defvar org-eldoc-local-functions-cache (make-hash-table :size 40 :test 'equal)
  88. "Cache of major-mode's eldoc-documentation-functions,
  89. used by \\[org-eldoc-get-mode-local-documentation-function].")
  90. (defun org-eldoc-get-mode-local-documentation-function (lang)
  91. "Check if LANG-mode sets eldoc-documentation-function and return its value."
  92. (let ((cached-func (gethash lang org-eldoc-local-functions-cache 'empty))
  93. (mode-func (org-src-get-lang-mode lang))
  94. doc-func)
  95. (if (eq 'empty cached-func)
  96. (when (fboundp mode-func)
  97. (with-temp-buffer
  98. (funcall mode-func)
  99. (setq doc-func (if (boundp 'eldoc-documentation-functions)
  100. (let ((doc-funs eldoc-documentation-functions))
  101. (lambda (callback)
  102. (let ((eldoc-documentation-functions doc-funs))
  103. (run-hook-with-args-until-success
  104. 'eldoc-documentation-functions
  105. callback))))
  106. (and eldoc-documentation-function
  107. (symbol-value 'eldoc-documentation-function))))
  108. (puthash lang doc-func org-eldoc-local-functions-cache))
  109. doc-func)
  110. cached-func)))
  111. (declare-function c-eldoc-print-current-symbol-info "c-eldoc" ())
  112. (declare-function css-eldoc-function "css-eldoc" ())
  113. (declare-function php-eldoc-function "php-eldoc" ())
  114. (declare-function go-eldoc--documentation-function "go-eldoc" ())
  115. (defun org-eldoc-documentation-function (&rest args)
  116. "Return breadcrumbs when on a headline, args for src block header-line,
  117. calls other documentation functions depending on lang when inside src body."
  118. (or
  119. (org-eldoc-get-breadcrumb)
  120. (org-eldoc-get-src-header)
  121. (let ((lang (org-eldoc-get-src-lang)))
  122. (cond ((or
  123. (string= lang "emacs-lisp")
  124. (string= lang "elisp"))
  125. (cond ((boundp 'eldoc-documentation-functions) ; Emacs>=28
  126. (let ((eldoc-documentation-functions
  127. '(elisp-eldoc-var-docstring elisp-eldoc-funcall)))
  128. (eldoc-print-current-symbol-info)))
  129. ((fboundp 'elisp-eldoc-documentation-function)
  130. (elisp-eldoc-documentation-function))
  131. (t ; Emacs<25
  132. (let (eldoc-documentation-function)
  133. (eldoc-print-current-symbol-info)))))
  134. ((or
  135. (string= lang "c") ;; http://github.com/nflath/c-eldoc
  136. (string= lang "C")) (when (require 'c-eldoc nil t)
  137. (c-eldoc-print-current-symbol-info)))
  138. ;; https://github.com/zenozeng/css-eldoc
  139. ((string= lang "css") (when (require 'css-eldoc nil t)
  140. (css-eldoc-function)))
  141. ;; https://github.com/zenozeng/php-eldoc
  142. ((string= lang "php") (when (require 'php-eldoc nil t)
  143. (php-eldoc-function)))
  144. ((or
  145. (string= lang "go")
  146. (string= lang "golang")) (when (require 'go-eldoc nil t)
  147. (go-eldoc--documentation-function)))
  148. (t (let ((doc-fun (org-eldoc-get-mode-local-documentation-function lang))
  149. (callback (car args)))
  150. (when (functionp doc-fun)
  151. (if (functionp callback)
  152. (funcall doc-fun callback)
  153. (funcall doc-fun)))))))))
  154. ;;;###autoload
  155. (defun org-eldoc-load ()
  156. "Set up org-eldoc documentation function."
  157. (interactive)
  158. ;; This approach is taken from python.el.
  159. (with-no-warnings
  160. (cond
  161. ((null eldoc-documentation-function) ; Emacs<25
  162. (setq-local eldoc-documentation-function
  163. #'org-eldoc-documentation-function))
  164. ((boundp 'eldoc-documentation-functions) ; Emacs>=28
  165. (add-hook 'eldoc-documentation-functions
  166. #'org-eldoc-documentation-function nil t))
  167. (t
  168. (add-function :before-until (local 'eldoc-documentation-function)
  169. #'org-eldoc-documentation-function)))))
  170. ;;;###autoload
  171. (add-hook 'org-mode-hook #'org-eldoc-load)
  172. (provide 'org-eldoc)
  173. ;; -*- coding: utf-8-emacs; -*-
  174. ;;; org-eldoc.el ends here