-
Notifications
You must be signed in to change notification settings - Fork 27
/
emr-scheme.el
103 lines (79 loc) · 3.18 KB
/
emr-scheme.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
;;; emr-scheme.el --- Refactoring commands for Scheme. -*- lexical-binding: t; -*-
;; Copyright (C) 2013 Chris Barrett
;; Author: Chris Barrett <[email protected]>
;; This file is not part of GNU Emacs.
;; 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:
;; Refactoring commands for Scheme.
;;; Code:
(require 'emr)
(require 'emr-lisp)
(require 'emr-elisp)
(require 'dash)
(defun emr-scm:looking-at-def? ()
"Non-nil if point is looking at a definition form."
(emr-line-matches? (rx bol (* space) "(define" (+ space))))
(defun emr-scm:inside-def? ()
"Non-nil if point is inside a definition form."
(and
(emr-lisp-find-upwards 'define)
(not (emr-scm:looking-at-def?))))
;;;###autoload
(defun emr-scm-extract-function (name arglist)
"Extract a function, using the current region or form at point as the body.
NAME is the name of the new function.
ARGLIST is its argument list."
(interactive
(list
;; Read a name for the function, ensuring it is not blank.
(let ((x (read-string "Name: ")))
(if (s-blank? x) (user-error "Name must not be blank") x))
;; Prompt user with default arglist.
(read-string "Arglist: ")))
(cl-assert (not (s-blank? name)) () "Name must not be blank")
(emr-lisp-extraction-refactor (body) "Extracted to"
(let ((hdr (cons (intern name)
(emr-el:safe-read (format "(%s)" arglist)))))
;; Insert usage at point.
(insert (emr-el:print hdr))
;; Insert definition.
(->> (format "(define %s\n %s)" hdr body)
(emr-el:format-defun)
(emr-lisp-insert-above-defun)))))
;;;###autoload
(defun emr-scm-extract-variable (name)
"Extract the current region or form at point to a special variable.
The variable will be called NAME."
(interactive "*sName: ")
(cl-assert (not (s-blank? name)) () "Name must not be blank")
(emr-lisp-extraction-refactor (sexp) "Extracted to"
;; Insert usage.
(insert (s-trim name))
;; Insert definition.
(emr-lisp-insert-above-defun (format "(define %s %s)" name sexp))))
; ------------------
(emr-declare-command 'emr-scm-extract-function
:title "function"
:description "define"
:modes 'scheme-mode
:predicate (lambda ()
(not (or (emr-scm:looking-at-def?)
(emr-el:looking-at-let-binding-symbol?)))))
(emr-declare-command 'emr-scm-extract-variable
:title "variable"
:description "define"
:modes 'scheme-mode
:predicate (lambda ()
(not (or (emr-scm:looking-at-def?)
(emr-el:looking-at-let-binding-symbol?)))))
(provide 'emr-scheme)
;;; emr-scheme.el ends here