-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathar-sexp.el
259 lines (228 loc) · 10.1 KB
/
ar-sexp.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
;;; ar-sexp.el --- navigate balanced expressions and related stuff -*- lexical-binding: t; -*-
;; Copyright (C) 2023-2024 Andreas Röhler
;; Author: Andreas Röhler <[email protected]>
;; Version: 0.2
;; 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 2, or (at your option)
;; any later version.
;; This file 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;; Keywords: convenience
;; (defvar forward-sexp-function 'ar-sexp
;; "")
;;; Commentary:
;; Similar to ‘forward-sexp’ but, when called from inside a comment or string, match only inside.
;; Likewise, when called from outside a comment or string, ignore these for a match.
(require 'ar-thingatpt-utils)
(defun ar-forward-sexp-intern ()
(cond ((ignore-errors (ar-delimited-atpt))
(ar-end-of-delimited-atpt))
(t (skip-syntax-forward "^(")
(ar-end-of-delimited-atpt))))
(defun ar-backward-sexp-intern ()
"Internally used.
Argument CHAR the char before cursor position."
;; (let (;;(complement-char (ar--return-complement-char-maybe char))
;; (pps (parse-partial-sexp (point-min) (point)))
;; (char (ar--return-complement-char-maybe (char-before))))
(skip-chars-backward " \t\n\r\f")
(unless (bobp)
(let ((orig (point)))
(pcase (char-before)
(?\" (cond ((and (looking-back "\"\"\"" (line-beginning-position))
(save-excursion (forward-char -3) (nth 3 (parse-partial-sexp (point-min) (point)))))
(while (and (search-backward (match-string-no-properties 0))
(nth 8 (parse-partial-sexp (point-min) (point))))))
((save-excursion (forward-char -1) (nth 3 (parse-partial-sexp (point-min) (point))))
(forward-char -1)
(goto-char (nth 8 (parse-partial-sexp (point-min) (point)))))
(t (unless (bobp)
(forward-char -1)
(ar-backward-sexp-intern)))))
(?\} (unless
(progn
(forward-char -1)
(ar-backward-braced-atpt))
(goto-char orig)))
(?\) (unless
(progn
(forward-char -1)
(ar-backward-parentized-atpt))
(goto-char orig)))
(?\] (unless
(progn
(forward-char -1)
(ar-backward-bracketed-atpt))
(goto-char orig)))
(?\{ (forward-char -1))
(?\( (forward-char -1))
(?\[ (forward-char -1))
(_
(pcase (char-after)
(?\" (unless
(ar-backward-doublequoted-atpt)
(goto-char orig)
(unless (bobp)
(forward-char -1)
(ar-backward-sexp-intern))))
(?\} (unless
(progn
;; (forward-char -1)
(ar-backward-braced-atpt))
(goto-char orig)))
(?\) (unless
(progn
;; (forward-char -1)
(ar-backward-parentized-atpt))
(goto-char orig)))
(?\] (unless
(progn
;; (forward-char -1)
(ar-backward-bracketed-atpt))
(goto-char orig)))
(_ (if
(or (member (char-before) th-beg-delimiter-list)
(looking-back (concat "[" ar-delimiters-atpt "]") (line-beginning-position)))
(cond ((looking-back "\"\"\"\\|'''" (line-beginning-position))
(while (and (search-backward (match-string-no-properties 0))
(nth 8 (parse-partial-sexp (point-min) (point))))))
(t (save-restriction
(narrow-to-region (line-beginning-position) (point))
(ar-backward-delimited-atpt))))
(skip-syntax-backward "^|\s\"\\(\\)" (line-beginning-position))
;; (syntax-class-to-char 15)
)))))
(and (< (point) orig) (point)))))
(defun ar-forward-sexp (&optional arg)
"Move forward across one balanced expression (sexp).
Negative ARG -N means move backward across N balanced expressions.
If started from outside a string and comment, do not match inside
and vice versa.
Don't match an opening bracket with closing paren, but ], etc."
(interactive "P")
;; (if (eq 4 (prefix- numeric-value arg))
;; (with-temp-buffer
;; process code inside string or comment by temporarily removing that property
(let ((pps (parse-partial-sexp(point-min) (point)))
erg)
(if (< 0 (or arg 1))
(progn
;; (when(nth 8 pps)
;; (save-restriction
;; (narrow-to-region
;; (nth 8 pps)(point-max))))
(ar-forward-sexp-intern))
;; (when(nth 8 pps)
;; (save-restriction
;; (narrow-to-region
;; (nth 8 pps) (point))))
(ar-backward-sexp-intern))))
(defun ar-backward-sexp (&optional arg)
"Go backward over a balanced expression."
(interactive "P")
(ar-forward-sexp -1))
(defvar ar-align-default-re "\\(=>\\|->\\|<-\\|=\\)"
"Used by ar-align-symbol")
(defun ar-align-equal-sign()
(interactive "*")
(ar-align-symbol "=")
)
(defun ar-align-inline-comment()
""
(interactive "*")
(save-excursion
(let ((char (if (search-forward comment-start (line-end-position) t)
(progn
(goto-char (match-beginning 0))
(skip-chars-forward "^[[:blank:]]+")
(char-to-string (char-before)))
(when (search-backward comment-start (line-beginning-position) t)
(char-to-string (char-after))))))
(ar-align-symbol-intern char))))
(defun ar-align-mode-specific-stop()
(pcase major-mode
(`scala-mode (and (eq major-mode 'scala-mode) (looking-at " *case *$")))))
;; *** The two new functions 'looking-at-p' and 'string-match-p' can do
;; the same matching as 'looking-at' and 'string-match' without changing
;; the match data.
(defun ar-align-symbol-intern (regexp)
""
(save-excursion
(let ((end (copy-marker (line-end-position)))
(secondcolumn (and
(or (looking-at (concat "\\(" regexp "\\)[[:space:]]*"))
(looking-back (concat ".+\\(" regexp "\\)[[:space:]]*") (line-beginning-position)))
(not
(save-excursion
(goto-char (match-beginning 1))
(save-match-data
(looking-back (concat ".+" regexp ".*") (line-beginning-position)))))
;; (member (char-after) (list 32 ?\n ?\t ?\f))
;; haskell
;; (not (looking-back "main +\\(=+\\) *" (line-beginning-position)))
(goto-char (match-beginning 1))
;; better match-data then from looking-back
(looking-at regexp)
(current-column)))
(erg (match-string-no-properties 0))
(maxcolumn 0)
(indent (current-indentation))
(orig (copy-marker (point)))
col)
;; an equal-sign at current line
(when secondcolumn
;; (message "(match-string-no-properties 1): %s" (match-string-no-properties 1))
;; (setq erg (match-string-no-properties 1))
;; (message "erg: %s" erg)
(setq maxcolumn secondcolumn)
;; (setq orig (copy-marker (point)))
(while
(and (progn (beginning-of-line)
(not (bobp)))
(progn (forward-line -1)
(not (looking-at comment-start)))
(not (ar-empty-line-p))
(<= (current-indentation) indent)
(or
(ar-align-mode-specific-stop)
(setq col (string-match erg (buffer-substring-no-properties (line-beginning-position) (line-end-position))))))
(when col (move-to-column col))
(when (< maxcolumn (current-column))
(setq maxcolumn (current-column))))
;; before going downward, reach the last match
;; (when (match-beginning 1) (goto-char (match-beginning 1)))
(while (<= (line-end-position) end)
(setq col (string-match erg (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
(when col (move-to-column col)
(when (< (current-column) maxcolumn)
(insert (make-string (- maxcolumn (current-column)) 32))))
(forward-line 1))
(goto-char orig)
;; (when (concat negated-re "+ +\\(" regexp "\\)[[:blank:]]+.*")
;; (when (< (current-column) maxcolumn)
;; (insert (make-string (- maxcolumn (current-column)) 32))))
))))
(defun ar-align-symbol (&optional arg)
(interactive "*p")
(unless (nth 8 (parse-partial-sexp (point-min) (point)))
(let ((regexp
(cond ((stringp arg)
arg)
((not (looking-at "[[:alnum:]]"))
(char-to-string (char-after)))
(t ar-align-default-re))))
;; (when t
(when (or (eq this-command 'self-insert-command) (eq (prefix-numeric-value arg) 1))
(ar-align-symbol-intern regexp)))))
(defun ar-align-in-current-buffer ()
(interactive)
(add-hook 'post-command-hook #'ar-align-symbol nil t))
(provide 'ar-sexp)
;;; ar-sexp.el ends here