forked from drym-org/symex.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symex-primitives.el
216 lines (172 loc) · 6.99 KB
/
symex-primitives.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
;;; symex-primitives.el --- An evil way to edit Lisp symbolic expressions as trees -*- lexical-binding: t -*-
;; URL: https://github.com/countvajhula/symex.el
;; This program is "part of the world," in the sense described at
;; https://drym.org. From your perspective, this is no different than
;; MIT or BSD or other such "liberal" licenses that you may be
;; familiar with, that is to say, you are free to do whatever you like
;; with this program. It is much more than BSD or MIT, however, in
;; that it isn't a license at all but an idea about the world and how
;; economic systems could be set up so that everyone wins. Learn more
;; at drym.org.
;;
;; This work transcends traditional legal and economic systems, but
;; for the purposes of any such systems within which you may need to
;; operate:
;;
;; This is free and unencumbered software released into the public domain.
;; The authors relinquish any copyright claims on this work.
;;
;;; Commentary:
;; Primitive navigations using third party libraries. This layer of primitives
;; can be swapped out without changing the higher-level functionality provided
;; by the Symex DSL. In the future it may make sense to directly operate on
;; an annotated representation of the AST here containing a mapping to buffer
;; positions and other metadata relevant for editing purposes.
;;; Code:
(require 'symex-primitives-lisp)
(require 'symex-ts)
(defvar symex-clojure-modes)
(defun symex-tree-sitter-p ()
"Whether to use the tree sitter primitives."
(and tree-sitter-mode
;; We use the Lisp primitives for Clojure
;; even though Emacs 29 provides tree-sitter APIs
;; for it, since the Lisp primitives in Symex are
;; more mature than the Tree Sitter ones at the
;; present time.
(not (member major-mode symex-clojure-modes))))
;;; User Interface
(defun symex--adjust-point ()
"Helper to adjust point to indicate the correct symex."
(if (symex-tree-sitter-p)
(symex-ts--adjust-point)
(symex-lisp--adjust-point)))
;;; Predicates
(defun symex--point-at-root-symex-p ()
"Check if point is at a root symex."
(if (symex-tree-sitter-p)
;; note that tree-sitter has a global
;; root for the whole file -- that's
;; not the one we mean here, but
;; rather, top level definitions
(symex-ts--at-tree-root-p)
(symex-lisp--point-at-root-symex-p)))
(defun symex--point-at-first-symex-p ()
"Check if point is at the first symex at some level."
(if (symex-tree-sitter-p)
(symex-ts--at-first-p)
(symex-lisp--point-at-first-symex-p)))
(defun symex--point-at-last-symex-p ()
"Check if point is at the last symex at some level."
(if (symex-tree-sitter-p)
(symex-ts--at-last-p)
(symex-lisp--point-at-last-symex-p)))
(defun symex--point-at-final-symex-p ()
"Check if point is at the last symex in the buffer."
(if (symex-tree-sitter-p)
(symex-ts--at-final-p)
(symex-lisp--point-at-final-symex-p)))
(defun symex--point-at-initial-symex-p ()
"Check if point is at the first symex in the buffer."
(if (symex-tree-sitter-p)
(symex-ts--at-initial-p)
(symex-lisp--point-at-initial-symex-p)))
(defun symex--point-at-start-p ()
"Check if point is at the start of a symex."
(if (symex-tree-sitter-p)
(symex-ts--point-at-start-p)
(symex-lisp--point-at-start-p)))
;;; Navigation
(defun symex--go-forward (&optional count)
"Forward symex.
Go forward COUNT times, defaulting to one.
This is an internal utility that avoids any user-level concerns
such as symex selection via advice. This should be used in all
internal operations that are not primarily user-directed."
(interactive)
(if (symex-tree-sitter-p)
(symex-ts-move-next-sibling count)
(symex-lisp--forward count)))
(defun symex--go-backward (&optional count)
"Backward symex.
Go backward COUNT times, defaulting to one.
This is an internal utility that avoids any user-level concerns
such as symex selection via advice. This should be used in all
internal operations that are not primarily user-directed."
(interactive)
(if (symex-tree-sitter-p)
(symex-ts-move-prev-sibling count)
(symex-lisp--backward count)))
(defun symex--go-up (&optional count)
"Enter higher symex level.
Enter COUNT times, defaulting to one.
This is an internal utility that avoids any user-level concerns
such as symex selection via advice. This should be used in all
internal operations that are not primarily user-directed."
(interactive)
(if (symex-tree-sitter-p)
(symex-ts-move-child count)
(symex-lisp--go-up count)))
(defun symex--go-down (&optional count)
"Exit to lower symex level.
Exit COUNT times, defaulting to one.
This is an internal utility that avoids any user-level concerns
such as symex selection via advice. This should be used in all
internal operations that are not primarily user-directed."
(interactive)
(if (symex-tree-sitter-p)
(symex-ts-move-parent count)
(symex-lisp--go-down count)))
;;; Utilities
(defmacro symex-save-excursion (&rest body)
"Execute BODY while preserving position in the tree.
Like `save-excursion`, but in addition to preserving the point
position, this also preserves the structural position in the tree, for
languages where point position doesn't uniquely identify a tree
location (e.g. non-symex-based languages like Python)."
(let ((offset (gensym))
(result (gensym)))
`(let ((,offset (symex--point-height-offset)))
(let ((,result
(save-excursion
,@body)))
(symex-select-nearest)
(symex--go-up ,offset)
,result))))
(defun symex--point-height-offset ()
"Compute the height offset of the current symex.
This is measured from the lowest symex indicated by point.
This will always be zero for symex-oriented languages such as Lisp,
but in languages like Python where the same point position could
correspond to multiple hierarchy levels, this function computes the
difference from the lowest such level."
(if (symex-tree-sitter-p)
(symex-ts--point-height-offset)
(symex-lisp--point-height-offset)))
(defun symex--get-starting-point ()
"Get the point value at the start of the current symex."
(if (symex-tree-sitter-p)
(symex-ts--get-starting-point)
(symex-lisp--get-starting-point)))
(defun symex--get-end-point (count)
"Get the point value after COUNT symexes.
If the containing expression terminates earlier than COUNT
symexes, returns the end point of the last one found."
(if (symex-tree-sitter-p)
(symex-ts--get-end-point count)
(symex-lisp--get-end-point count)))
(defun symex-select-nearest ()
"Select symex nearest to point."
(interactive)
(if (symex-tree-sitter-p)
(symex-ts-set-current-node-from-point)
(symex-lisp--select-nearest))
(point))
(defun symex--primitive-exit ()
"Take necessary actions as part of exiting Symex mode, at a primitive level."
(symex--delete-overlay)
(if (symex-tree-sitter-p)
(symex-ts--exit)
(symex-lisp--exit)))
(provide 'symex-primitives)
;;; symex-primitives.el ends here