Created
January 29, 2026 02:04
-
-
Save mmarshall540/d6866a3d348e5898409552d493623e71 to your computer and use it in GitHub Desktop.
DWIM command for inserting pairs of quotes or escaped quotes inside of a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defun my/insert-quotes-or-break-string (&optional arg) | |
| "This enhances the behavior of `insert-pair' for double-quotes. | |
| It acts differently from `insert-pair', because when splitting an | |
| existing string, the strings will be outside of the new pair, not | |
| inside. So in that case, we want to add spaces inside of the new pair, | |
| not outside. If the mark is active, escaped double-quotes are added | |
| around the region. This command makes it easier to edit tempo templates | |
| and skeletons. To add escaped double-quotes within a template string, | |
| just activate the mark before calling this command. This works even if | |
| the region is empty." | |
| (interactive "P") | |
| (cond | |
| ((not (nth 3 (syntax-ppss))) ; not in a string | |
| (insert-pair arg ?\" ?\")) | |
| ((region-active-p) ; in a string with active mark | |
| (when (eq (point) (region-end)) (exchange-point-and-mark)) | |
| (when (memq (char-syntax (preceding-char)) (list ?w ?_)) | |
| (insert " ")) | |
| (insert "\\\"") | |
| (save-excursion | |
| (goto-char (region-end)) | |
| (insert "\\\"") | |
| (when (memq (char-syntax (following-char)) (list ?w ?_)) | |
| (insert " ")))) | |
| (t ; in a string with inactive mark | |
| (insert "\" ") ; so just split the string | |
| (save-excursion (insert " \""))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment