org-eldoc.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. ;;; org-eldoc.el --- display org header and src block info using eldoc
  2. ;; Copyright (c) 2014 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. (defgroup org-eldoc nil "" :group 'org)
  30. (defcustom org-eldoc-breadcrumb-separator "/"
  31. "Breadcrumb separator."
  32. :group 'org-eldoc
  33. :type 'string)
  34. (defcustom org-eldoc-test-buffer-name " *Org-eldoc test buffer*"
  35. "Name of the buffer used while testing for mode-local variable values."
  36. :group 'org-eldoc
  37. :type 'string)
  38. (defun org-eldoc-get-breadcrumb ()
  39. "Return breadcrumb if on a headline or nil."
  40. (let ((case-fold-search t) cur)
  41. (save-excursion
  42. (beginning-of-line)
  43. (save-match-data
  44. (when (looking-at org-complex-heading-regexp)
  45. (setq cur (match-string 4))
  46. (org-format-outline-path
  47. (append (org-get-outline-path) (list cur))
  48. (frame-width) "" org-eldoc-breadcrumb-separator))))))
  49. (defun org-eldoc-get-src-header ()
  50. "Returns lang and list of header properties if on src definition line and nil otherwise."
  51. (let ((case-fold-search t) info lang hdr-args)
  52. (save-excursion
  53. (beginning-of-line)
  54. (save-match-data
  55. (when (looking-at "^[ \t]*#\\+\\(begin\\|end\\)_src")
  56. (setq info (org-babel-get-src-block-info 'light)
  57. lang (propertize (nth 0 info) 'face 'font-lock-string-face)
  58. hdr-args (nth 2 info))
  59. (concat
  60. lang
  61. ": "
  62. (mapconcat
  63. (lambda (elem)
  64. (when (and (cdr elem) (not (string= "" (cdr elem))))
  65. (concat
  66. (propertize (symbol-name (car elem)) 'face 'org-list-dt)
  67. " "
  68. (propertize (cdr elem) 'face 'org-verbatim)
  69. " ")))
  70. hdr-args " ")))))))
  71. (defun org-eldoc-get-src-lang ()
  72. "Return value of lang for the current block if in block body and nil otherwise."
  73. (let ((case-fold-search t))
  74. (save-match-data
  75. (when (org-between-regexps-p ".*#\\+begin_src"
  76. ".*#\\+end_src")
  77. (save-excursion
  78. (goto-char (org-babel-where-is-src-block-head))
  79. (car (org-babel-parse-src-block-match)))))))
  80. (defvar org-eldoc-local-functions-cache (make-hash-table :size 40 :test 'equal)
  81. "Cache of major-mode's eldoc-documentation-functions,
  82. used by \\[org-eldoc-get-mode-local-documentation-function].")
  83. (defun org-eldoc-get-mode-local-documentation-function (lang)
  84. "Check if LANG-mode sets eldoc-documentation-function and return its value."
  85. (let ((cached-func (gethash lang org-eldoc-local-functions-cache 'empty))
  86. (mode-func (intern-soft (format "%s-mode" lang)))
  87. doc-func)
  88. (if (eq 'empty cached-func)
  89. (when (fboundp mode-func)
  90. (with-temp-buffer
  91. (funcall mode-func)
  92. (setq doc-func (and eldoc-documentation-function
  93. (symbol-value 'eldoc-documentation-function)))
  94. (puthash lang doc-func org-eldoc-local-functions-cache))
  95. doc-func)
  96. cached-func)))
  97. (declare-function c-eldoc-print-current-symbol-info "c-eldoc" ())
  98. (declare-function css-eldoc-function "css-eldoc" ())
  99. (declare-function php-eldoc-function "php-eldoc" ())
  100. (declare-function go-eldoc--documentation-function "go-eldoc" ())
  101. (defun org-eldoc-documentation-function ()
  102. "Return breadcrumbs when on a headline, args for src block header-line,
  103. calls other documentation functions depending on lang when inside src body."
  104. (or
  105. (org-eldoc-get-breadcrumb)
  106. (org-eldoc-get-src-header)
  107. (let ((lang (org-eldoc-get-src-lang)))
  108. (cond ((or
  109. (string= lang "emacs-lisp")
  110. (string= lang "elisp")) (if (fboundp 'elisp-eldoc-documentation-function)
  111. (elisp-eldoc-documentation-function)
  112. (let (eldoc-documentation-function)
  113. (eldoc-print-current-symbol-info))))
  114. ((or
  115. (string= lang "c") ;; http://github.com/nflath/c-eldoc
  116. (string= lang "C")) (when (require 'c-eldoc nil t)
  117. (c-eldoc-print-current-symbol-info)))
  118. ;; https://github.com/zenozeng/css-eldoc
  119. ((string= lang "css") (when (require 'css-eldoc nil t)
  120. (css-eldoc-function)))
  121. ;; https://github.com/zenozeng/php-eldoc
  122. ((string= lang "php") (when (require 'php-eldoc nil t)
  123. (php-eldoc-function)))
  124. ((or
  125. (string= lang "go")
  126. (string= lang "golang")) (when (require 'go-eldoc nil t)
  127. (go-eldoc--documentation-function)))
  128. (t (let ((doc-fun (org-eldoc-get-mode-local-documentation-function lang)))
  129. (when (fboundp doc-fun) (funcall doc-fun))))))))
  130. ;;;###autoload
  131. (defun org-eldoc-load ()
  132. "Set up org-eldoc documentation function."
  133. (interactive)
  134. (setq-local eldoc-documentation-function #'org-eldoc-documentation-function))
  135. ;;;###autoload
  136. (add-hook 'org-mode-hook #'org-eldoc-load)
  137. (provide 'org-eldoc)
  138. ;; -*- coding: utf-8-emacs; -*-
  139. ;;; org-eldoc.el ends here