Last active
August 29, 2015 14:21
-
-
Save vermiculus/8ed1ddfdbebc68804ff8 to your computer and use it in GitHub Desktop.
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 expl3-spacify-paragraph (begin end) | |
"Convert all spaces to tildes between BEGIN and END. | |
If the region is not active, the current paragraph is used." | |
(interactive (if (region-active-p) | |
(list (region-beginning) | |
(region-end)) | |
(save-excursion | |
(mark-paragraph) | |
(forward-word 1) | |
(forward-word -1) | |
(list (point) (mark))))) | |
(let ((text (buffer-substring-no-properties begin end)) | |
(from-region (region-active-p)) | |
indent) | |
(delete-region begin end) | |
(insert | |
(with-temp-buffer | |
(insert text) | |
(goto-char 1) | |
;; grab the indentation amount | |
(forward-line 1) | |
(forward-word 1) | |
(forward-word -1) | |
(setq indent (current-column)) | |
(goto-char 1) | |
;; remove all indentation | |
(while (not (eobp)) | |
(delete-horizontal-space) | |
(forward-line 1)) | |
(goto-char 1) | |
;; replace | |
(while (search-forward " " nil t) | |
(replace-match "~")) | |
(goto-char 1) | |
;; put spaces at the end of lines | |
(while (search-forward "\n" nil t) | |
(replace-match "~\n")) | |
(goto-char 1) | |
;; re-insert indentation | |
(forward-line 1) | |
(while (not (eobp)) | |
(insert (make-string indent ?\ )) | |
(forward-line 1)) | |
;; remove the trailing space if appropriate | |
(unless from-region | |
(when (search-backward "~" nil t) | |
(replace-match ""))) | |
(buffer-string))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment