I have a function that uses smartparens
to replace specified pairs of parentheses.
(defun replace-paren-at-point (paren)
(interactive "sReplace with: ")
(if (looking-at "[「『《〈(〔【]")
(progn
(set-mark-command nil)
(forward-sexp)
(sp-wrap-with-pair paren)
(sp-unwrap-sexp))
(if (save-excursion
(backward-char)
(looking-at "[」』》〉)〕】]"))
(progn
(set-mark-command nil)
(backward-sexp)
(sp-wrap-with-pair paren)
(sp-unwrap-sexp)))
(message "No parenthesis at point.")))
One issue with the function above is that the user would be prompted to enter the replace string before he/she is reminded that there is "No parenthesis at point."
(in cases which the cursor is at the wrong position.)
How do I change this so that an error message appears before the user is prompted for the input?
(defun replace-paren-at-point (paren)
(interactive)
(if (looking-at "[「『《〈(〔【]")
(progn
(read-string (format "Replace %s with: " (string (char-after))))
(set-mark-command nil)
(forward-sexp)
(sp-wrap-with-pair paren)
(sp-unwrap-sexp))
(if (save-excursion
(backward-char)
(looking-at "[」』》〉)〕】]"))
(progn
(read-string (format "Replace %s with: " (string (char-before))))
(set-mark-command nil)
(backward-sexp)
(sp-wrap-with-pair paren)
(sp-unwrap-sexp)))
(message "No parenthesis at point.")))
I try to use read-string
for the input prompt but it doesn't work this way.
Seems like read-string
has to be defined without the paren
argument, but I need the paren
variable set to store the value for the replace string. So I am kind of stuck.
Settings:
In case some of you might like to test the code above, here are the settings that should be evaluated in init.el
before running the tests.
(package-initialize)
(smartparens-global-mode t)
(sp-pair "「" "」")
(sp-pair "『" "』")
(sp-pair "【" "】")
(sp-pair "《" "》")
(sp-pair "〈" "〉")
(sp-pair "(" ")")
(sp-pair "〔" "〕")