Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 205 lines (179 sloc) 8.346 kB
530d89f @thierryvolpiatto Enable lexical-binding in all files.
thierryvolpiatto authored
1 ;;; helm-eval.el --- eval expressions from helm. -*- lexical-binding: t -*-
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
2
7c31479 @thierryvolpiatto Update copyrights.
thierryvolpiatto authored
3 ;; Copyright (C) 2012 ~ 2015 Thierry Volpiatto <thierry.volpiatto@gmail.com>
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
4
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
9
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 ;;; Code:
b740a4b @thierryvolpiatto Finish replacing cl symbols and requiring cl-lib, no compile error.
thierryvolpiatto authored
19 (require 'cl-lib)
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
20 (require 'helm)
787e66f @thierryvolpiatto Require helm-help in several places.
thierryvolpiatto authored
21 (require 'helm-help)
320fe95 @thierryvolpiatto * helm-eval.el: require eldoc.
thierryvolpiatto authored
22 (require 'eldoc)
fa8bb07 @thierryvolpiatto Fix byte-compiler warnings in helm-eval.
thierryvolpiatto authored
23 (require 'edebug)
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
24
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
25
26 (defgroup helm-eval nil
27 "Eval related Applications and libraries for Helm."
28 :group 'helm)
29
4166d8a @thierryvolpiatto Replace "helm-c-" prefix by "helm-" in all files.
thierryvolpiatto authored
30 (defcustom helm-eldoc-in-minibuffer-show-fn
31 'helm-show-info-in-mode-line
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
32 "A function to display eldoc info.
33 Should take one arg: the string to display."
34 :group 'helm-eval
35 :type 'symbol)
36
4166d8a @thierryvolpiatto Replace "helm-c-" prefix by "helm-" in all files.
thierryvolpiatto authored
37 (defcustom helm-show-info-in-mode-line-delay 12
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
38 "Eldoc will show info in mode-line during this delay if user is idle."
39 :type 'integer
40 :group 'helm-eval)
41
42
94213c5 @thierryvolpiatto Fix eldoc compatibility with emacs-25 (#998).
thierryvolpiatto authored
43 ;;; Eldoc compatibility between emacs-24 and emacs-25
44 ;;
45 (if (require 'elisp-mode nil t) ; emacs-25
46 ;; Maybe the eldoc functions have been
47 ;; already aliased by eldoc-eval.
677a57f @thierryvolpiatto * helm-eval.el: Make the loop for eldoc compat simpler.
thierryvolpiatto authored
48 (cl-loop for (f . a) in '((eldoc-current-symbol .
49 elisp--current-symbol)
50 (eldoc-fnsym-in-current-sexp .
51 elisp--fnsym-in-current-sexp)
52 (eldoc-get-fnsym-args-string .
44e4cd4 @thierryvolpiatto * helm-eval.el: Fix eldoc compat with emacs-25.
thierryvolpiatto authored
53 elisp-get-fnsym-args-string)
677a57f @thierryvolpiatto * helm-eval.el: Make the loop for eldoc compat simpler.
thierryvolpiatto authored
54 (eldoc-get-var-docstring .
44e4cd4 @thierryvolpiatto * helm-eval.el: Fix eldoc compat with emacs-25.
thierryvolpiatto authored
55 elisp-get-var-docstring))
94213c5 @thierryvolpiatto Fix eldoc compatibility with emacs-25 (#998).
thierryvolpiatto authored
56 unless (fboundp f)
57 do (defalias f a))
58 ;; Emacs-24.
59 (declare-function eldoc-current-symbol "eldoc")
60 (declare-function eldoc-get-fnsym-args-string "eldoc" (sym &optional index))
61 (declare-function eldoc-get-var-docstring "eldoc" (sym))
62 (declare-function eldoc-fnsym-in-current-sexp "eldoc"))
7d8197a @thierryvolpiatto Fix warnings at compilation.
thierryvolpiatto authored
63
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
64 ;;; Evaluation Result
65 ;;
66 ;;
67 ;; Internal
68 (defvar helm-eldoc-active-minibuffers-list nil)
69
70 (defvar helm-eval-expression-map
b740a4b @thierryvolpiatto Finish replacing cl symbols and requiring cl-lib, no compile error.
thierryvolpiatto authored
71 (let ((map (make-sparse-keymap)))
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
72 (set-keymap-parent map helm-map)
73 (define-key map (kbd "<C-return>") 'helm-eval-new-line-and-indent)
e6daefc @thierryvolpiatto * helm-eval.el: Fix keymap conflict with tab.
thierryvolpiatto authored
74 (define-key map (kbd "<M-tab>") 'lisp-indent-line)
8e72c21 @thierryvolpiatto * helm-dabbrev.el: Allow usage in minibuffer, delay action and allow-…
thierryvolpiatto authored
75 (define-key map (kbd "<C-tab>") 'helm-lisp-completion-at-point)
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
76 (define-key map (kbd "C-p") 'previous-line)
77 (define-key map (kbd "C-n") 'next-line)
78 (define-key map (kbd "<up>") 'previous-line)
79 (define-key map (kbd "<down>") 'next-line)
80 (define-key map (kbd "<right>") 'forward-char)
81 (define-key map (kbd "<left>") 'backward-char)
82 map))
83
fa8bb07 @thierryvolpiatto Fix byte-compiler warnings in helm-eval.
thierryvolpiatto authored
84 (defun helm-build-evaluation-result-source ()
91ff90b @thierryvolpiatto Use helm-source to define sources in helm-eval.
thierryvolpiatto authored
85 (helm-build-dummy-source "Evaluation Result"
86 :multiline t
87 :mode-line "C-RET: nl-and-indent, M-tab: reindent, C-tab:complete, C-p/n: next/prec-line."
fa8bb07 @thierryvolpiatto Fix byte-compiler warnings in helm-eval.
thierryvolpiatto authored
88 :filtered-candidate-transformer (lambda (_candidates _source)
91ff90b @thierryvolpiatto Use helm-source to define sources in helm-eval.
thierryvolpiatto authored
89 (list
90 (condition-case nil
91 (with-helm-current-buffer
92 (pp-to-string
93 (if edebug-active
94 (edebug-eval-expression
95 (read helm-pattern))
bc4fb13 @thierryvolpiatto Finally reindent with the cl indentation behavior of 24.4.50.
thierryvolpiatto authored
96 (eval (read helm-pattern)))))
91ff90b @thierryvolpiatto Use helm-source to define sources in helm-eval.
thierryvolpiatto authored
97 (error "Error"))))
fa8bb07 @thierryvolpiatto Fix byte-compiler warnings in helm-eval.
thierryvolpiatto authored
98 :nohighlight t
91ff90b @thierryvolpiatto Use helm-source to define sources in helm-eval.
thierryvolpiatto authored
99 :action '(("Copy result to kill-ring" . (lambda (candidate)
100 (kill-new
101 (replace-regexp-in-string
c302c0a @thierryvolpiatto Add messages in sexp and calculation (#1110)
thierryvolpiatto authored
102 "\n" "" candidate))
103 (message "Result copied to kill-ring")))
fa8bb07 @thierryvolpiatto Fix byte-compiler warnings in helm-eval.
thierryvolpiatto authored
104 ("copy sexp to kill-ring" . (lambda (_candidate)
c302c0a @thierryvolpiatto Add messages in sexp and calculation (#1110)
thierryvolpiatto authored
105 (kill-new helm-input)
106 (message "Sexp copied to kill-ring"))))))
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
107
108 (defun helm-eval-new-line-and-indent ()
109 (interactive)
110 (newline) (lisp-indent-line))
111
112 (defun helm-eldoc-store-minibuffer ()
113 "Store minibuffer buffer name in `helm-eldoc-active-minibuffers-list'."
114 (with-selected-window (minibuffer-window)
86a50f9 @thierryvolpiatto * helm-eval.el: Simplify according to eldoc-eval.
thierryvolpiatto authored
115 (push (current-buffer) helm-eldoc-active-minibuffers-list)))
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
116
117 (defun helm-eldoc-show-in-eval ()
118 "Return eldoc in mode-line for current minibuffer input."
86a50f9 @thierryvolpiatto * helm-eval.el: Simplify according to eldoc-eval.
thierryvolpiatto authored
119 (let ((buf (window-buffer (active-minibuffer-window))))
c7913c1 @thierryvolpiatto * helm-eval.el (helm-eldoc-show-in-eval): Avoid error messages in ema…
thierryvolpiatto authored
120 (condition-case err
121 (when (member buf helm-eldoc-active-minibuffers-list)
9b3b7b4 @thierryvolpiatto * helm-eval.el (helm-eldoc-show-in-eval): Get rid of `minibuffer-comp…
thierryvolpiatto authored
122 (with-current-buffer buf
123 (let* ((sym (save-excursion
81e3b4f @thierryvolpiatto Fix limit of looking-back to 1 before point.
thierryvolpiatto authored
124 (unless (looking-back ")\\|\"" (1- (point)))
9b3b7b4 @thierryvolpiatto * helm-eval.el (helm-eldoc-show-in-eval): Get rid of `minibuffer-comp…
thierryvolpiatto authored
125 (forward-char -1))
126 (eldoc-current-symbol)))
127 (info-fn (eldoc-fnsym-in-current-sexp))
128 (doc (or (eldoc-get-var-docstring sym)
129 (eldoc-get-fnsym-args-string
130 (car info-fn) (cadr info-fn)))))
131 (when doc (funcall helm-eldoc-in-minibuffer-show-fn doc)))))
9b50655 @thierryvolpiatto Keep fixing unused args
thierryvolpiatto authored
132 (error (message "Eldoc in minibuffer error: %S" err) nil))))
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
133
4166d8a @thierryvolpiatto Replace "helm-c-" prefix by "helm-" in all files.
thierryvolpiatto authored
134 (defun helm-show-info-in-mode-line (str)
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
135 "Display string STR in mode-line."
136 (save-selected-window
137 (with-current-buffer helm-buffer
138 (let ((mode-line-format (concat " " str)))
139 (force-mode-line-update)
4166d8a @thierryvolpiatto Replace "helm-c-" prefix by "helm-" in all files.
thierryvolpiatto authored
140 (sit-for helm-show-info-in-mode-line-delay))
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
141 (force-mode-line-update))))
142
143 ;;; Calculation Result
144 ;;
145 ;;
4166d8a @thierryvolpiatto Replace "helm-c-" prefix by "helm-" in all files.
thierryvolpiatto authored
146 (defvar helm-source-calculation-result
91ff90b @thierryvolpiatto Use helm-source to define sources in helm-eval.
thierryvolpiatto authored
147 (helm-build-dummy-source "Calculation Result"
fa8bb07 @thierryvolpiatto Fix byte-compiler warnings in helm-eval.
thierryvolpiatto authored
148 :filtered-candidate-transformer (lambda (_candidates _source)
91ff90b @thierryvolpiatto Use helm-source to define sources in helm-eval.
thierryvolpiatto authored
149 (list
150 (condition-case nil
151 (calc-eval helm-pattern)
152 (error "error"))))
fa8bb07 @thierryvolpiatto Fix byte-compiler warnings in helm-eval.
thierryvolpiatto authored
153 :nohighlight t
c302c0a @thierryvolpiatto Add messages in sexp and calculation (#1110)
thierryvolpiatto authored
154 :action '(("Copy result to kill-ring" . (lambda (candidate)
155 (kill-new candidate)
e72e0b7 @michael-heerdegen make calculation message include result (#1110)
michael-heerdegen authored
156 (message "Result \"%s\" copied to kill-ring"
157 candidate)))
aeba45e @thierryvolpiatto Add also an action to copy calcul to kill-ring (#1110).
thierryvolpiatto authored
158 ("Copy operation to kill-ring" . (lambda (_candidate)
159 (kill-new helm-input)
160 (message "Calculation copied to kill-ring"))))))
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
161
162 ;;;###autoload
163 (defun helm-eval-expression (arg)
4166d8a @thierryvolpiatto Replace "helm-c-" prefix by "helm-" in all files.
thierryvolpiatto authored
164 "Preconfigured helm for `helm-source-evaluation-result'."
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
165 (interactive "P")
5025a84 @thierryvolpiatto Allow using helm-echo-input-in-header-line as local var (#1064).
thierryvolpiatto authored
166 (helm :sources (helm-build-evaluation-result-source)
167 :input (when arg (thing-at-point 'sexp))
168 :buffer "*helm eval*"
169 :echo-input-in-header-line nil
170 :history 'read-expression-history
171 :keymap helm-eval-expression-map))
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
172
4e6ddbb @thierryvolpiatto Make declarations with defvar.
thierryvolpiatto authored
173 (defvar eldoc-idle-delay)
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
174 ;;;###autoload
175 (defun helm-eval-expression-with-eldoc ()
4166d8a @thierryvolpiatto Replace "helm-c-" prefix by "helm-" in all files.
thierryvolpiatto authored
176 "Preconfigured helm for `helm-source-evaluation-result' with `eldoc' support. "
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
177 (interactive)
8e72c21 @thierryvolpiatto * helm-dabbrev.el: Allow usage in minibuffer, delay action and allow-…
thierryvolpiatto authored
178 (let ((timer (run-with-idle-timer
179 eldoc-idle-delay 'repeat
180 'helm-eldoc-show-in-eval)))
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
181 (unwind-protect
9bc2702 @thierryvolpiatto Reindent all again.
thierryvolpiatto authored
182 (minibuffer-with-setup-hook
183 'helm-eldoc-store-minibuffer
184 (call-interactively 'helm-eval-expression))
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
185 (and timer (cancel-timer timer))
186 (setq helm-eldoc-active-minibuffers-list
187 (cdr helm-eldoc-active-minibuffers-list)))))
188
189 ;;;###autoload
190 (defun helm-calcul-expression ()
4166d8a @thierryvolpiatto Replace "helm-c-" prefix by "helm-" in all files.
thierryvolpiatto authored
191 "Preconfigured helm for `helm-source-calculation-result'."
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
192 (interactive)
91ff90b @thierryvolpiatto Use helm-source to define sources in helm-eval.
thierryvolpiatto authored
193 (helm :sources 'helm-source-calculation-result
194 :buffer "*helm calcul*"))
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
195
196 (provide 'helm-eval)
197
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
198 ;; Local Variables:
a7ee65b @thierryvolpiatto Disable compile warnings and add new keyword to helm.
thierryvolpiatto authored
199 ;; byte-compile-warnings: (not cl-functions obsolete)
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
200 ;; coding: utf-8
201 ;; indent-tabs-mode: nil
202 ;; End:
203
ce61153 @thierryvolpiatto * helm-config.el: Move eval-expression to helm-eval.el
thierryvolpiatto authored
204 ;;; helm-eval.el ends here
Something went wrong with that request. Please try again.