Skip to content

Commit

Permalink
Protect against symbols with unsafe chars
Browse files Browse the repository at this point in the history
Fixes abo-abo#648.

* lispy.el lispy--symbol-safe-chars-regexp: define safe chars.
(lispy--read): protect against unsafe chars in a symbol.
lispy-left lispy-right: also match for beginning and end of symbol.
  • Loading branch information
RuijieYu committed Apr 5, 2023
1 parent fe44efd commit c41d26a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lispy.el
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@
:group 'bindings
:prefix "lispy-")

(defvar lispy-left "[([{]"
(defvar lispy-left (rx (or (any "([{") symbol-start))
"Opening delimiter.")

(defvar lispy-right "[])}]"
(defvar lispy-right (rx (or (any "])}") symbol-end))
"Closing delimiter.")

(defvar lispy-outline "^;;\\(?:;[^#]\\|\\*+\\)"
Expand Down Expand Up @@ -7286,6 +7286,12 @@ See https://clojure.org/guides/weird_characters#_character_literal.")
(match-string subexp)))
t t nil subexp)))))

(defconst lispy--symbol-safe-chars-regexp
(rx (any alnum ?+ ?- ?* ?/ ?: ?=))
"Regexp for \"safe\" characters.
Safe characters are those which are suitable for a symbol and
have no special reader syntax.")

;; TODO: Make the read test pass on string with semi-colon
(defun lispy--read (str)
"Read STR including comments and newlines."
Expand Down Expand Up @@ -7524,11 +7530,13 @@ See https://clojure.org/guides/weird_characters#_character_literal.")
(unless (lispy--in-string-or-comment-p)
(replace-match (format "(ly-raw racket-option %s)"
(match-string 1)))))
;; Clojure # in a symbol
;; Protect symbols containing unsafe characters
(goto-char (point-min))
(while (re-search-forward "\\_<\\(?:\\sw\\|\\s_\\)+\\_>" nil t)
(unless (lispy--in-string-p)
(when (cl-position ?# (match-string 0))
(when (not (string-match
lispy--symbol-safe-chars-regexp
(match-string 0)))
(let* ((bnd (lispy--bounds-dwim))
(str (lispy--string-dwim bnd)))
(delete-region (car bnd) (cdr bnd))
Expand Down

0 comments on commit c41d26a

Please sign in to comment.