From: Kevin Ballard Date: Fri, 13 Nov 2009 02:40:42 +0000 (-0800) Subject: Teach emacs M-x gofmt to save region/restrictions X-Git-Tag: weekly.2009-11-17~87 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=6c2ae1a6250187fe6460cad9fb21b774e9aca4bb;p=gostls13.git Teach emacs M-x gofmt to save region/restrictions R=agl, agl1 https://golang.org/cl/152078 --- diff --git a/misc/emacs/go-mode.el b/misc/emacs/go-mode.el index 47d790db41..42336a0fe7 100644 --- a/misc/emacs/go-mode.el +++ b/misc/emacs/go-mode.el @@ -475,4 +475,15 @@ Useful for development work." "Pipe the current buffer through the external tool `gofmt`." (interactive) - (shell-command-on-region 1 (+ (buffer-size) 1) "gofmt" t t shell-command-default-error-buffer)) + ;; for some reason save-excursion isn't working + ;; probably because shell-command-on-region deletes the contents of the + ;; region before filling in the new values + ;; so we will save the point/mark by hand + ;; similarly we can't use push-mark/pop-mark + (let ((old-mark (mark t)) (old-point (point))) + (save-restriction + (let (deactivate-mark) + (widen) + (shell-command-on-region (point-min) (point-max) "gofmt" t t shell-command-default-error-buffer))) + (goto-char (min old-point (point-max))) + (if old-mark (set-mark (min old-mark (point-max))))))