diff options
| author | Tim Harper <timcharper@gmail.com> | 2011-08-23 11:19:56 -0600 |
|---|---|---|
| committer | Tim Harper <timcharper@gmail.com> | 2011-08-23 12:03:49 -0600 |
| commit | 205c650d7fbfdbe2b917e1d90e701633a55e89df (patch) | |
| tree | b9357cf79f112e43268f4055f81da99c7d53d985 | |
| parent | b2d9aebfe9654fa94c5b10fbd38b815cf1cc4976 (diff) | |
Refactor surround-pair for readability, flexibility. Subtle behavior change
Surround brackets when invoked with left-bracket pads with space
regardless of current major mode.
The surround-pairs-alist is considerably longer, but the resulting code
is easier to understand.
| -rw-r--r-- | surround.el | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/surround.el b/surround.el index c805c68..268fd7e 100644 --- a/surround.el +++ b/surround.el @@ -41,9 +41,14 @@ :group 'evil) (defcustom surround-pairs-alist - '((?\( . ?\)) - (?\[ . ?\]) - (?{ . ?}) + '((?\( . ("( " . " )")) + (?\[ . ("[ " . " ]")) + (?\{ . ("{ " . " }")) + + (?\) . ("(" . ")")) + (?\] . ("[" . "]")) + (?\} . ("{" . "}")) + (?# . ("#{" . "}")) (?b . ("(" . ")")) (?B . ("{" . "}")) @@ -74,20 +79,18 @@ This only affects inserting pairs, not deleting or changing them." (format "</%s>" (or tag ""))))) (defun surround-pair (char) - "Return the surround pair of CHAR. + "Return the surround pair of char. This is a cons cell (LEFT . RIGHT), both strings." - (let* ((open (or (car (rassoc char surround-pairs-alist)) char)) - (close (or (cdr (assoc char surround-pairs-alist)) char))) + (let ((pair (assoc-default char surround-pairs-alist))) (cond - ((functionp close) - (funcall close)) - ((consp close) - close) - ((eq (char-syntax char) ?\() - ;; add whitespace - (cons (format "%c " open) (format " %c" close))) + ((functionp pair) + (funcall pair)) + + ((consp pair) + pair) + (t - (cons (format "%c" open) (format "%c" close)))))) + (cons (format "%c" char) (format "%c" char)))))) (defun surround-outer-overlay (char) "Return outer overlay for the delimited range represented by CHAR. |
