forked from abo-abo/lispy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlispy-inline.el
261 lines (222 loc) · 8.92 KB
/
lispy-inline.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
;;; lispy-inline.el --- inline arglist and documentation. -*- lexical-binding: t -*-
;; Copyright (C) 2014-2015 Oleh Krehel
;; This file is not part of GNU Emacs
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Display current function arguments or docstring in an in-place
;; overlay.
;;; Code:
(require 'thingatpt)
(require 'subr-x)
(defgroup lispy-faces nil
"Font-lock faces for `lispy'."
:group 'lispy
:prefix "lispy-face-")
(defface lispy-face-hint
'((((class color) (background light))
:background "#fff3bc" :foreground "black")
(((class color) (background dark))
:background "black" :foreground "#fff3bc"))
"Basic hint face."
:group 'lispy-faces)
(defface lispy-face-req-nosel
'((t (:inherit lispy-face-hint)))
"Face for required unselected args."
:group 'lispy-faces)
(defface lispy-face-req-sel
'((t (:inherit lispy-face-req-nosel :bold t)))
"Face for required selected args."
:group 'lispy-faces)
(defface lispy-face-opt-nosel
'((t (:inherit lispy-face-hint :slant italic)))
"Face for optional unselected args."
:group 'lispy-faces)
(defface lispy-face-key-nosel
'((t (:inherit lispy-face-hint :slant italic)))
"Face for keyword unselected args."
:group 'lispy-faces)
(defface lispy-face-opt-sel
'((t (:inherit lispy-face-opt-nosel :bold t)))
"Face for optional selected args."
:group 'lispy-faces)
(defface lispy-face-key-sel
'((t (:inherit lispy-face-opt-nosel :bold t)))
"Face for keyword selected args."
:group 'lispy-faces)
(defface lispy-face-rst-nosel
'((t (:inherit lispy-face-hint)))
"Face for rest unselected args."
:group 'lispy-faces)
(defface lispy-face-rst-sel
'((t (:inherit lispy-face-rst-nosel :bold t)))
"Face for rest selected args."
:group 'lispy-faces)
(defcustom lispy-window-height-ratio 0.65
"`lispy--show' will fail with string taller than window height times this.
The caller of `lispy--show' might use a substitute e.g. `describe-function'."
:type 'float
:group 'lispy)
(defvar lispy-elisp-modes
'(emacs-lisp-mode lisp-interaction-mode eltex-mode minibuffer-inactive-mode
suggest-mode)
"Modes for which `lispy--eval-elisp' and related functions are appropriate.")
(defvar lispy-clojure-modes
'(clojure-mode clojurescript-mode clojurex-mode clojurec-mode)
"Modes for which clojure related functions are appropriate.")
(defvar lispy-overlay nil
"Hint overlay instance.")
(defvar lispy-hint-pos nil
"Point position where the hint should be (re-) displayed.")
(declare-function lispy--eval-clojure-cider "le-clojure")
(declare-function lispy--clojure-args "le-clojure")
(declare-function lispy--clojure-resolve "le-clojure")
(declare-function lispy--describe-clojure-java "le-clojure")
(declare-function lispy--eval-scheme "le-scheme")
(declare-function lispy--eval-lisp "le-lisp")
(declare-function lispy--lisp-args "le-lisp")
(declare-function lispy--lisp-describe "le-lisp")
(declare-function lispy--back-to-paren "lispy")
(declare-function lispy--current-function "lispy")
(declare-function lispy--in-comment-p "lispy")
(declare-function lispy--bounds-string "lispy")
;; ——— Commands ————————————————————————————————————————————————————————————————
(defun lispy--cleanup-overlay ()
"Delete `lispy-overlay' if it's valid and return t."
(when (overlayp lispy-overlay)
(delete-overlay lispy-overlay)
(setq lispy-overlay nil)
t))
(declare-function geiser-doc-symbol-at-point "geiser-doc")
(defun lispy--describe-inline (str pos)
"Toggle the overlay hint."
(condition-case nil
(save-excursion
(when (= 0 (count-lines (window-start) (point)))
(recenter 1))
(setq lispy-hint-pos pos)
(goto-char lispy-hint-pos)
(lispy--show (propertize str 'face 'lispy-face-hint)))
(error
(lispy--cleanup-overlay))))
(declare-function cider-nrepl-op-supported-p "ext:cider-client")
(declare-function cider-sync-request:info "ext:cider-client")
(declare-function nrepl-dict-get "ext:nrepl-dict")
(declare-function semantic-ctxt-current-symbol "ctxt")
;; ——— Utilities ———————————————————————————————————————————————————————————————
(defun lispy--arglist (symbol)
"Get arglist for SYMBOL."
(let (doc)
(if (setq doc (help-split-fundoc (documentation symbol t) symbol))
(car doc)
(prin1-to-string
(cons symbol (help-function-arglist symbol t))))))
(defun lispy--join-pad (strs width)
"Join STRS padding each line with WIDTH spaces."
(let* ((maxw (apply #'max (mapcar #'length strs)))
(padding (make-string width ?\ ))
(fstring (format "%%- %ds" maxw)))
(mapconcat
(lambda (x)
(concat
padding
(let ((str (format fstring x)))
(font-lock-append-text-property
0 (length str) 'face 'lispy-face-hint str)
str)))
strs
"\n")))
(defun lispy--show-fits-p (str)
"Return nil if window isn't large enough to display STR whole."
(let ((strs (split-string str "\n")))
(when (or (< (length strs) (* lispy-window-height-ratio (window-height)))
(window-minibuffer-p))
strs)))
(defun lispy--show (str)
"Show STR hint when `lispy--show-fits-p' is t."
(let ((last-point (point))
(strs (lispy--show-fits-p str)))
(if strs
(progn
(setq str (lispy--join-pad
strs
(+ (if (window-minibuffer-p)
(- (minibuffer-prompt-end) (point-min))
0)
(string-width (buffer-substring
(line-beginning-position)
(point))))))
(save-excursion
(goto-char lispy-hint-pos)
(if (= -1 (forward-line -1))
(setq str (concat str "\n"))
(end-of-line)
(setq str (concat "\n" str)))
(setq str (concat str
(buffer-substring (point) (1+ (point)))))
(if lispy-overlay
(progn
(move-overlay lispy-overlay (point) (+ (point) 1))
(overlay-put lispy-overlay 'invisible nil))
(setq lispy-overlay (make-overlay (point) (+ (point) 1)))
(overlay-put lispy-overlay 'priority 9999))
(overlay-put lispy-overlay 'display str)
(overlay-put lispy-overlay 'after-string "")
(put 'lispy-overlay 'last-point last-point)))
(save-selected-window
(pop-to-buffer (get-buffer-create "*lispy-help*"))
(let ((inhibit-read-only t))
(delete-region (point-min) (point-max))
(insert str)
(goto-char (point-min))
(help-mode))))))
(defun lispy--pretty-args (symbol)
"Return a vector of fontified strings for function SYMBOL."
(let* ((args (cdr (read (lispy--arglist symbol))))
(p-opt (cl-position '&optional args :test 'equal))
(p-rst (or (cl-position '&rest args :test 'equal)
(cl-position-if (lambda (x)
(and (symbolp x)
(string-match
"\\.\\.\\.\\'"
(symbol-name x))))
args)))
(a-req (cl-subseq args 0 (or p-opt p-rst (length args))))
(a-opt (and p-opt
(cl-subseq args (1+ p-opt) (or p-rst (length args)))))
(a-rst (and p-rst (last args))))
(format
"(%s)"
(mapconcat
#'identity
(append
(list (propertize (symbol-name symbol) 'face 'lispy-face-hint))
(mapcar
(lambda (x)
(propertize (downcase (prin1-to-string x)) 'face 'lispy-face-req-nosel))
a-req)
(mapcar
(lambda (x)
(propertize (downcase (prin1-to-string x)) 'face 'lispy-face-opt-nosel))
a-opt)
(mapcar
(lambda (x)
(setq x (downcase (symbol-name x)))
(unless (string-match "\\.\\.\\.$" x)
(setq x (concat x "...")))
(propertize x 'face 'lispy-face-rst-nosel))
a-rst))
" "))))
(provide 'lispy-inline)
;;; Local Variables:
;;; End:
;;; lispy-inline.el ends here