Skip to content

Commit

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

* lispy.el lispy--symbol-safe-chars: define list of safe chars.
(lispy--read): protect against unsafe chars in a symbol.
(lispy--replace-regexp-in-code): add predicate to only replace on match data.
  • Loading branch information
RuijieYu committed Apr 7, 2023
1 parent 8175e9a commit 311cfd0
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions lispy.el
Original file line number Diff line number Diff line change
Expand Up @@ -7200,13 +7200,21 @@ For example, a `setq' statement is amended with variable name that it uses."
(insert char)
(lispy--indent-region (point) pt))))

(defun lispy--replace-regexp-in-code (regexp to-string)
(defun lispy--replace-regexp-in-code
(regexp to-string &optional pred)
"Replace text matching REGEXP with TO-STRING in whole buffer.
Ignore the matches in strings and comments."
(goto-char (point-min))
(while (re-search-forward regexp nil t)
(unless (lispy--in-string-or-comment-p)
(replace-match to-string))))
Ignore the matches in strings and comments.

PRED should be a 0-arg predicate with access to the regexp match
data. PRED defaults to `always', and should return non-nil when
a specific match data should be replaced."
(save-excursion
(goto-char (point-min))
(save-match-data
(while (re-search-forward regexp nil t)
(unless (lispy--in-string-or-comment-p)
(when (funcall (or pred #'always))
(replace-match to-string)))))))

;;* Utilities: source transformation
(defvar lispy--braces-table
Expand Down Expand Up @@ -7286,6 +7294,18 @@ See https://clojure.org/guides/weird_characters#_character_literal.")
(match-string subexp)))
t t nil subexp)))))

(defconst lispy--symbol-safe-chars
(seq-concatenate 'list "+-*/:="
(number-sequence ?a ?z)
(number-sequence ?A ?Z))
"List of known \"safe\" characters.
Safe characters are those which are suitable for a symbol and
which have no special reader syntax.

Missing a few safe characters would not be a serious problem. It
would only produce a slightly larger internal representation when
lispy tries to parse a given sexp.")

;; 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,15 +7544,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
(goto-char (point-min))
(while (re-search-forward "\\_<\\(?:\\sw\\|\\s_\\)+\\_>" nil t)
(unless (lispy--in-string-p)
(when (cl-position ?# (match-string 0))
(let* ((bnd (lispy--bounds-dwim))
(str (lispy--string-dwim bnd)))
(delete-region (car bnd) (cdr bnd))
(insert (format "(ly-raw symbol %S)" str))))))
;; Protect symbols containing unsafe characters
(lispy--replace-regexp-in-code
"\\_<\\(?:\\sw\\|\\s_\\)+\\_>"
"(ly-raw symbol \"\\&\")"
(lambda ()
(seq-difference
(match-string 0) lispy--symbol-safe-chars)))
;; Clojure (. object method)
(goto-char (point-min))
(while (re-search-forward "(\\.[\t\n ]" nil t)
Expand Down

0 comments on commit 311cfd0

Please sign in to comment.