From 350a5ce64fc17d229137f66e855a9d733dc76e2e Mon Sep 17 00:00:00 2001 From: Jan Newmarch Date: Mon, 31 Oct 2011 11:33:14 -0400 Subject: [PATCH] misc/emacs/go-lang.el: Fix restoration of multiple windows in a frame after gofmt If a frame has multiple windows then the windows must all be restored after gofmt has finished and the old windows must be restored. Before this fix, only the Go code edit window would be left. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5280050 --- misc/emacs/go-mode.el | 74 ++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/misc/emacs/go-mode.el b/misc/emacs/go-mode.el index e0992143b32..b35013d3979 100644 --- a/misc/emacs/go-mode.el +++ b/misc/emacs/go-mode.el @@ -7,6 +7,7 @@ ;;; To do: ;; * Indentation is *almost* identical to gofmt +;; ** We think struct literal keys are labels and outdent them ;; ** We disagree on the indentation of function literals in arguments ;; ** There are bugs with the close brace of struct literals ;; * Highlight identifiers according to their syntactic context: type, @@ -87,7 +88,7 @@ some syntax analysis.") (,(concat "\\\\s *\\w+\\s *" type-name) 1 font-lock-type-face) ;; Arrays/slices/map value type ;; XXX Wrong. Marks 0 in expression "foo[0] * x" -;; (,(concat "]" type-name) 1 font-lock-type-face) + ;; (,(concat "]" type-name) 1 font-lock-type-face) ;; Map key type (,(concat "\\\\|\\\\|\\w+\\s *:\\s *$") + (when (looking-at "\\\\|\\\\|\\w+\\s *:\\(\\S.\\|$\\)") (decf indent tab-width)) ;; Continuation lines are indented 1 level @@ -500,47 +500,49 @@ Useful for development work." ;;;###autoload (defun gofmt () - "Pipe the current buffer through the external tool `gofmt`. + "Pipe the current buffer through the external tool `gofmt`. Replace the current buffer on success; display errors on failure." - (interactive) - (let ((srcbuf (current-buffer))) - (with-temp-buffer - (let ((outbuf (current-buffer)) - (errbuf (get-buffer-create "*Gofmt Errors*")) - (coding-system-for-read 'utf-8) ;; use utf-8 with subprocesses - (coding-system-for-write 'utf-8)) - (with-current-buffer errbuf (erase-buffer)) - (with-current-buffer srcbuf - (save-restriction - (let (deactivate-mark) - (widen) - (if (= 0 (shell-command-on-region (point-min) (point-max) "gofmt" - outbuf nil errbuf)) - ;; gofmt succeeded: replace the current buffer with outbuf, - ;; restore the mark and point, and discard errbuf. - (let ((old-mark (mark t)) (old-point (point))) - (erase-buffer) - (insert-buffer-substring outbuf) - (goto-char (min old-point (point-max))) - (if old-mark (push-mark (min old-mark (point-max)) t)) - (kill-buffer errbuf)) + (interactive) + (let ((currconf (current-window-configuration))) + (let ((srcbuf (current-buffer))) + (with-temp-buffer + (let ((outbuf (current-buffer)) + (errbuf (get-buffer-create "*Gofmt Errors*")) + (coding-system-for-read 'utf-8) ;; use utf-8 with subprocesses + (coding-system-for-write 'utf-8)) + (with-current-buffer errbuf (erase-buffer)) + (with-current-buffer srcbuf + (save-restriction + (let (deactivate-mark) + (widen) + (if (= 0 (shell-command-on-region (point-min) (point-max) "gofmt" + outbuf nil errbuf)) + ;; restore window config + ;; gofmt succeeded: replace the current buffer with outbuf, + ;; restore the mark and point, and discard errbuf. + (let ((old-mark (mark t)) (old-point (point))) + (set-window-configuration currconf) + (erase-buffer) + (insert-buffer-substring outbuf) + (goto-char (min old-point (point-max))) + (if old-mark (push-mark (min old-mark (point-max)) t)) + (kill-buffer errbuf)) - ;; gofmt failed: display the errors - (display-buffer errbuf))))) + ;; gofmt failed: display the errors + (display-buffer errbuf))))) - ;; Collapse any window opened on outbuf if shell-command-on-region - ;; displayed it. - (delete-windows-on outbuf))))) + ;; Collapse any window opened on outbuf if shell-command-on-region + ;; displayed it. + (delete-windows-on outbuf)))))) ;;;###autoload (defun gofmt-before-save () - "Add this to .emacs to run gofmt on the current buffer when saving: + "Add this to .emacs to run gofmt on the current buffer when saving: (add-hook 'before-save-hook #'gofmt-before-save)" - (interactive) - (when (eq major-mode 'go-mode) (gofmt))) - + (interactive) + (when (eq major-mode 'go-mode) (gofmt))) (defun godoc-read-query () "Read a godoc query from the minibuffer."