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