Skip to content

Instantly share code, notes, and snippets.

@sabof
Last active December 15, 2015 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sabof/5283582 to your computer and use it in GitHub Desktop.
Save sabof/5283582 to your computer and use it in GitHub Desktop.
es-patch
(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