Skip to content

Commit dbcdc96

Browse files
authored
Merge pull request #96 from countvajhula/more-gardening
More gardening
2 parents dc5c523 + c3cff43 commit dbcdc96

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

symex-primitives-lisp.el

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@
4646
(looking-back "[,'`]" (line-beginning-position))
4747
(save-excursion (backward-char) ; just inside symex
4848
(or (symex-left-p)
49+
;; this is to exclude the case where
50+
;; we're inside a string, "|abc"
51+
;; which "inverts" the code structure
52+
;; and causes unexpected behavior when
53+
;; navigating using Emacs's built-in
54+
;; primitive symex motions. Unlike normal
55+
;; forms, opening and closing delimiters
56+
;; are not distinguished for strings and
57+
;; so we can't specifically check for
58+
;; "open quote," with the result that
59+
;; in the case "abc"|, we don't always
60+
;; select the right symex the way we
61+
;; would with (abc)|.
4962
(symex-string-p))))
5063
(condition-case nil
5164
(backward-sexp)
@@ -344,7 +357,6 @@ as special cases here."
344357
"Select the appropriate symex nearest to point."
345358
(cond ((and (not (eobp))
346359
(save-excursion (forward-char) (symex-right-p))) ; |)
347-
(forward-char)
348360
(symex-other))
349361
((thing-at-point 'sexp) ; som|ething
350362
(beginning-of-thing 'sexp))

symex-transformations-lisp.el

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
(cond ((symex--current-line-empty-p) ; ^<>$
9090
;; only join up to the next symex if the context suggests
9191
;; that a line break is not desired
92-
(if (or (save-excursion (next-line)
92+
(if (or (save-excursion (forward-line)
9393
(not (symex--current-line-empty-p)))
9494
(save-excursion (previous-line)
9595
(symex--current-line-empty-p)))
@@ -99,7 +99,13 @@
9999
((or (save-excursion (evil-last-non-blank) ; (<>$
100100
(symex-left-p)))
101101
(symex--join-to-next))
102-
((looking-at-p "\n") (symex--go-backward)) ; (abc <>
102+
((looking-at-p "\n") ; (abc <>
103+
(if (save-excursion (forward-line)
104+
(not (symex--current-line-empty-p)))
105+
;; only join up to the next symex if the context suggests
106+
;; that a line break is not desired
107+
(symex--join-to-next)
108+
(symex--go-backward)))
103109
((save-excursion (back-to-indentation) ; ^<>)
104110
(forward-char)
105111
(symex-right-p))
@@ -109,18 +115,18 @@
109115
;; on the same line, then don't attempt to join lines
110116
(let ((original-position (point)))
111117
(when (symex--go-backward)
112-
(let ((previous-symex-end-pos (symex--get-end-point 1)))
113-
(unless (symex--intervening-comment-line-p previous-symex-end-pos
114-
original-position)
115-
(goto-char previous-symex-end-pos)
116-
;; ensure that there isn't a comment on the
117-
;; preceding line before joining lines
118-
(unless (condition-case nil
119-
(progn (evil-find-char 1 ?\;)
120-
t)
121-
(error nil))
122-
(symex--join-to-match symex--re-right)
123-
(symex--adjust-point)))))))
118+
(save-excursion
119+
(let ((previous-symex-end-pos (symex--get-end-point 1)))
120+
(unless (symex--intervening-comment-line-p previous-symex-end-pos
121+
original-position)
122+
(goto-char previous-symex-end-pos)
123+
;; ensure that there isn't a comment on the
124+
;; preceding line before joining lines
125+
(unless (condition-case nil
126+
(progn (evil-find-char 1 ?\;)
127+
t)
128+
(error nil))
129+
(symex--join-to-match symex--re-right))))))))
124130
((save-excursion (forward-char) ; ... <>)
125131
(symex-right-p))
126132
(symex--go-backward))
@@ -209,17 +215,28 @@ text, on the respective side."
209215
(eolp))
210216
;; and if the side we want to paste on already
211217
;; contains an empty line,
212-
(save-excursion (if before
218+
(save-excursion (if (or before
219+
;; if we happen to be at the end
220+
;; of the buffer for pasting after,
221+
;; then check the opposite side instead
222+
;; for the clue on what's expected
223+
(save-excursion (forward-sexp)
224+
(eobp)))
213225
(previous-line)
214-
(progn (forward-sexp) (next-line)))
226+
(progn (forward-sexp)
227+
(forward-line)))
215228
(symex--current-line-empty-p))
216229
;; and if the text to be pasted contains newlines,
217230
;; then we typically want an extra newline separator
218231
(seq-contains-p (current-kill 0 t) ?\n))
219232
"\n\n")
220233
((or (symex--point-at-indentation-p)
221-
(save-excursion (forward-sexp)
222-
(eolp)))
234+
(let ((original-line (line-number-at-pos)))
235+
(save-excursion (forward-sexp)
236+
(or (eolp)
237+
;; for multi-line symex, add a newline
238+
(not (= original-line
239+
(line-number-at-pos)))))))
223240
"\n")
224241
(t " ")))
225242

symex-transformations.el

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,15 @@
166166

167167
(defun symex--capture-backward ()
168168
"Capture from behind."
169-
(when (symex-left-p)
169+
(when (and (symex-left-p)
170+
;; paredit captures 1 ((|2 3)) -> (1 (2 3)) but we don't
171+
;; want to in this case since point indicates the inner
172+
;; symex, which cannot capture, rather than the outer
173+
;; one. We avoid this by employing a guard condition here.
174+
(not (symex--point-at-first-symex-p)))
170175
(if (symex-empty-list-p)
171176
(forward-char)
172177
(symex--go-up)) ; need to be inside the symex to emit and capture
173-
;; paredit captures 1 ((|2 3)) -> (1 (2 3))
174-
;; but we don't want to in this case since point indicates the
175-
;; inner symex, which cannot capture, rather than the outer
176-
;; one. Just a note for the future.
177178
(paredit-backward-slurp-sexp 1)
178179
(fixup-whitespace)
179180
(symex--go-down)))
@@ -331,8 +332,9 @@ by default, joins next symex to current one."
331332
(concat (symex-lisp-paste-after)
332333
pasted-text)))
333334
(save-excursion
335+
(forward-sexp) ; go to beginning of pasted text
334336
(goto-char (+ (point)
335-
(length pasted-text)))
337+
(length pasted-text))) ; end of pasted text
336338
(symex--same-line-tidy-affected))
337339
;; move to indicate appropriate posterior selection
338340
(forward-sexp)
@@ -816,7 +818,7 @@ implementation."
816818
(symex-define-command symex-tidy-remaining ()
817819
"Tidy the remaining symexes."
818820
(interactive)
819-
(save-excursion
821+
(symex--save-point-excursion
820822
;; do it once first since it will be executed as a side-effect
821823
;; _after_ each step in the traversal
822824
(symex--tidy 1)

0 commit comments

Comments
 (0)