-
Notifications
You must be signed in to change notification settings - Fork 4
/
matcha-elisp.el
199 lines (179 loc) · 6.86 KB
/
matcha-elisp.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
;;; matcha-elisp.el --- Integration with Transient. -*- lexical-binding: t -*-
;; Copyright (C) 2019 James Nguyen
;; Author: James Nguyen <[email protected]>
;; Maintainer: James Nguyen <[email protected]>
;; URL: https://github.com/jojojames/matcha
;; Version: 0.0.1
;; Package-Requires: ((emacs "25.1"))
;; Keywords: transient, emacs
;; HomePage: https://github.com/jojojames/matcha
;; This program 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 of the License, 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.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Integration with Transient.
;;; Code:
(require 'matcha-base)
(require 'matcha-macrostep)
;; (require 'elisp-refs)
(defun matcha-goto-scratch ()
"Move to *scratch* buffer."
(interactive)
(pop-to-buffer "*scratch*"))
(defun matcha-elisp-eval-current-form-sp (&optional arg)
"Call `eval-last-sexp' after moving out of one level of
parentheses. Will exit any strings and/or comments first.
An optional ARG can be used which is passed to `sp-up-sexp' to move out of more
than one sexp.
Requires smartparens because all movement is done using `sp-up-sexp'."
(interactive "p")
(require 'smartparens)
(let ((evil-move-beyond-eol t))
;; evil-move-beyond-eol disables the evil advices around eval-last-sexp
(ignore evil-move-beyond-eol)
(save-excursion
(let ((max 10))
(while (and (> max 0)
(sp-point-in-string-or-comment))
(decf max)
(sp-up-sexp)))
(sp-up-sexp arg)
(call-interactively 'eval-last-sexp))))
(defun matcha-elisp-eval-current-symbol-sp ()
"Call `eval-last-sexp' on the symbol around point.
Requires smartparens because all movement is done using `sp-forward-symbol'."
(interactive)
(require 'smartparens)
(let ((evil-move-beyond-eol t))
(ignore evil-move-beyond-eol)
;; evil-move-beyond-eol disables the evil advices around eval-last-sexp
(save-excursion
(sp-forward-symbol)
(call-interactively 'eval-last-sexp))))
;; https://github.com/jwiegley/use-package/issues/152
;; Edebug a defun or defmacro
(defvar matcha-fns-in-edebug nil
"List of functions for which `edebug' is instrumented.")
(defconst matcha-elisp-fns-regexp
(concat "(\\s-*"
"\\(defun\\|defmacro\\)\\s-+"
"\\(?1:\\(\\w\\|\\s_\\)+\\)\\_>")
"Regexp to find defun or defmacro definition.")
(defun matcha-elisp-toggle-edebug-defun ()
(interactive)
(let (fn)
(save-mark-and-excursion
(search-backward-regexp matcha-elisp-fns-regexp)
(setq fn (match-string 1))
(mark-sexp)
(narrow-to-region (point) (mark))
(if (member fn matcha-fns-in-edebug)
;; If the function is already being edebugged, uninstrument it
(progn
(setq matcha-fns-in-edebug (delete fn matcha-fns-in-edebug))
(eval-region (point) (mark))
(setq-default eval-expression-print-length 12)
(setq-default eval-expression-print-level 4)
(message "Edebug disabled: %s" fn))
;; If the function is not being edebugged, instrument it
(progn
(add-to-list 'matcha-fns-in-edebug fn)
(setq-default eval-expression-print-length nil)
(setq-default eval-expression-print-level nil)
(edebug-defun)
(message "Edebug: %s" fn)))
(widen))))
(when matcha-use-launcher-p
(matcha-set-format-command
:mode '(emacs-lisp-mode lisp-interaction-mode)
:command 'matcha-indent-region-or-buffer)
(matcha-set-debug-command
:mode '(emacs-lisp-mode lisp-interaction-mode)
:command 'matcha-emacs-lisp-debug)
(matcha-set-eval-command
:mode '(emacs-lisp-mode lisp-interaction-mode)
:command 'matcha-emacs-lisp-eval)
(matcha-set-mode-command
:mode '(emacs-lisp-mode lisp-interaction-mode)
:command 'matcha-emacs-lisp-mode)
(matcha-set-refactor-command
:mode '(emacs-lisp-mode lisp-interaction-mode)
:command 'emr-show-refactor-menu))
(transient-define-prefix matcha-emacs-lisp-debug ()
"Debug"
[["Debug"
("d" "Debug" matcha-elisp-toggle-edebug-defun)
("q" "Cancel Debug on Entry" cancel-debug-on-entry)
("f" "Debug on Entry" debug-on-entry)]
["Watch"
("w" "Watch" debug-watch)
("W" "Cancel Watch" cancel-debug-watch)]])
(transient-define-prefix matcha-emacs-lisp-eval ()
"Eval"
[["Eval"
("e" "Last" eval-last-sexp)
("r" "Region" eval-region)
("f" "Defun" eval-defun)
("b" "Buffer" eval-buffer)]
["Current"
("c" "Form" matcha-elisp-eval-current-form-sp)
("s" "Symbol" matcha-elisp-eval-current-symbol-sp)]
["Misc"
("j" "Eval and Print" eval-print-last-sexp)]])
(transient-define-prefix matcha-elisp-refs ()
"References"
[["References"
("f" "Function" elisp-refs-function)
("m" "Macro" elisp-refs-macro)
("c" "Special" elisp-refs-special)
("v" "Variable" elisp-refs-variable)
("s" "Symbol" elisp-refs-symbol)]])
(transient-define-prefix matcha-emacs-lisp-compile ()
"Compile"
[["Compile"
("c" "Compile" emacs-lisp-byte-compile)
("l" "Compile and Load" emacs-lisp-byte-compile-and-load)
("r" "Byte Recompile Directory" byte-recompile-directory)
("x" "Disassemble" disassemble)]])
(transient-define-prefix matcha-emacs-lisp-describe ()
"Describe"
[["Describe"
("f" "Function" describe-function)
("v" "Variable" describe-variable)
("s" "Symbol" describe-symbol)
("y" "Syntax" describe-syntax)
("c" "Categories" describe-categories)]])
(transient-define-prefix matcha-emacs-lisp-mode ()
"Emacs Lisp"
[["Actions"
("c" "Compile..." matcha-emacs-lisp-compile)
("d" "Debug..." matcha-emacs-lisp-debug)
("e" "Eval..." matcha-emacs-lisp-eval)
("m" "Macroexpand..." matcha-macrostep-expand-or-open-menu)
("s" "Describe..." matcha-emacs-lisp-describe)]
["Find"
("v" "Variable" find-variable)
("f" "Function" find-function)
("l" "Library" find-library)
("r" "References..." matcha-elisp-refs)]
["PP"
("pm" " Macroexpand Expression..." pp-macroexpand-expression)
("pl" " Macroexpand Last S-exp..." pp-macroexpand-last-sexp)
("pb" "Prettify Current Buffer" pp-buffer)
("pe" "Eval Last Sexp" pp-eval-last-sexp)
("ps" "Prettify Expression" pp-eval-expression)]
["Misc"
("x" "*Scratch*" matcha-goto-scratch)
("z" "IELM" ielm)]])
(provide 'matcha-elisp)
;;; matcha-elisp.el ends here
;; Local Variables:
;; byte-compile-warnings: (not free-vars unresolved noruntime cl-functions obsolete)
;; End: