Skip to content

Instantly share code, notes, and snippets.

@unjello
Last active June 8, 2016 08:15
Show Gist options
  • Save unjello/866f2702d024c6bdb5364c16c3435e47 to your computer and use it in GitHub Desktop.
Save unjello/866f2702d024c6bdb5364c16c3435e47 to your computer and use it in GitHub Desktop.
(defun unj-c-stringify (αbegin αend)
"Surround current line or each line in region with quotes and escape ones inside
based on Xah Lee http://ergoemacs.org/emacs/elisp_escape_quotes.html
v2016-06-08"
(interactive
(if (use-region-p)
(list (region-beginning) (region-end))
(list (line-beginning-position) (line-end-position))))
(save-excursion
(save-restriction
(narrow-to-region αbegin αend)
(goto-char (point-min))
(save-excursion
(while (search-forward "\"" nil t)
(replace-match "\\\"" 'FIXEDCASE 'LITERAL)))
(save-excursion (replace-regexp "^" "\""))
(save-excursion (replace-regexp "$" "\\\\n\"")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment