Commit 9d063816 authored by Jean-Marc Eurin's avatar Jean-Marc Eurin Committed by Sameer Ajmani

misc/emacs: Restore the window position after a successful gofmt.

This adds restoring the window position so that the buffer doesn't jump around after the erase/copy.

R=sameer
CC=golang-dev
https://golang.org/cl/5981055
parent a96a9836
...@@ -743,34 +743,37 @@ Replace the current buffer on success; display errors on failure." ...@@ -743,34 +743,37 @@ Replace the current buffer on success; display errors on failure."
(let ((currconf (current-window-configuration))) (let ((currconf (current-window-configuration)))
(let ((srcbuf (current-buffer))) (let ((srcbuf (current-buffer)))
(with-temp-buffer (with-temp-buffer
(let ((outbuf (current-buffer)) (let ((outbuf (current-buffer))
(errbuf (get-buffer-create "*Gofmt Errors*")) (errbuf (get-buffer-create "*Gofmt Errors*"))
(coding-system-for-read 'utf-8) ;; use utf-8 with subprocesses (coding-system-for-read 'utf-8) ;; use utf-8 with subprocesses
(coding-system-for-write 'utf-8)) (coding-system-for-write 'utf-8))
(with-current-buffer errbuf (erase-buffer)) (with-current-buffer errbuf (erase-buffer))
(with-current-buffer srcbuf (with-current-buffer srcbuf
(save-restriction (save-restriction
(let (deactivate-mark) (let (deactivate-mark)
(widen) (widen)
(if (= 0 (shell-command-on-region (point-min) (point-max) "gofmt" (if (= 0 (shell-command-on-region (point-min) (point-max) "gofmt"
outbuf nil errbuf)) outbuf nil errbuf))
;; restore window config ;; restore window config
;; gofmt succeeded: replace the current buffer with outbuf, ;; gofmt succeeded: replace the current buffer with outbuf,
;; restore the mark and point, and discard errbuf. ;; restore the mark and point, and discard errbuf.
(let ((old-mark (mark t)) (old-point (point))) (let ((old-mark (mark t))
(set-window-configuration currconf) (old-point (point))
(erase-buffer) (old-start (window-start)))
(insert-buffer-substring outbuf) (erase-buffer)
(goto-char (min old-point (point-max))) (insert-buffer-substring outbuf)
(if old-mark (push-mark (min old-mark (point-max)) t)) (set-window-configuration currconf)
(kill-buffer errbuf)) (set-window-start (selected-window) (min old-start (point-max)))
(goto-char (min old-point (point-max)))
;; gofmt failed: display the errors (if old-mark (push-mark (min old-mark (point-max)) t))
(display-buffer errbuf))))) (kill-buffer errbuf))
;; Collapse any window opened on outbuf if shell-command-on-region ;; gofmt failed: display the errors
;; displayed it. (display-buffer errbuf)))))
(delete-windows-on outbuf))))))
;; Collapse any window opened on outbuf if shell-command-on-region
;; displayed it.
(delete-windows-on outbuf))))))
;;;###autoload ;;;###autoload
(defun gofmt-before-save () (defun gofmt-before-save ()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment