Last active
December 15, 2015 15:39
-
-
Save sabof/5283582 to your computer and use it in GitHub Desktop.
es-patch
This file contains 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 es-patch (&optional patch-string custom-args) | |
(interactive | |
(list nil | |
current-prefix-arg | |
)) | |
(let (( patch-temp-file | |
(make-temp-file "patch")) | |
default-command | |
( \default-args | |
(concat "--no-backup-if-mismatch" | |
" " | |
"--ignore-whitespace" | |
" " | |
"--reject-file=-")) | |
command | |
command-result) | |
(with-temp-buffer | |
(if patch-string | |
(insert patch-string) | |
(yank)) | |
(unless (equal (char-before) ?\n ) | |
(insert ?\n )) | |
(write-region nil nil patch-temp-file)) | |
(save-buffer) | |
(setq default-command | |
(format "patch -i %s %s %s" | |
(shell-quote-argument patch-temp-file) | |
default-args | |
(shell-quote-argument (buffer-file-name)))) | |
(setq command | |
(cond ( (not custom-args) | |
default-command) | |
( (stringp custom-args) | |
(format "patch -i %s %s %s" | |
(shell-quote-argument patch-temp-file) | |
custom-args | |
(shell-quote-argument (buffer-file-name)) | |
)) | |
( t (read-string "Command: " | |
default-command)))) | |
(setq command-result | |
(shell-command-to-string | |
command)) | |
(setq command-result | |
(replace-regexp-in-string | |
(concat "^[[:blank:]\n]*" | |
"\\(?1:.+?\\)" | |
"[[:blank:]\n]*$") | |
"\\1" | |
command-result)) | |
(revert-buffer t t t) | |
(when (called-interactively-p 'any) | |
(message command-result)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment