mirror of
https://github.com/golang/go
synced 2024-11-15 11:10:22 -07:00
[release-branch.go1] misc/emacs: Fix the automatic gofmt when creating a new file.
««« backport 5b4920fe3605 misc/emacs: Fix the automatic gofmt when creating a new file. Patching the buffer with the output from gofmt -d only works if the file already exists. If it doesn't, replace the content with the output of gofmt. R=sameer CC=golang-dev https://golang.org/cl/6302063 »»»
This commit is contained in:
parent
687e047b14
commit
95c8f44f3a
@ -777,43 +777,59 @@ Replace the current buffer on success; display errors on failure."
|
|||||||
(save-restriction
|
(save-restriction
|
||||||
(let (deactivate-mark)
|
(let (deactivate-mark)
|
||||||
(widen)
|
(widen)
|
||||||
(if (= 0 (shell-command-on-region (point-min) (point-max) "gofmt -d"
|
; If this is a new file, diff-mode can't apply a
|
||||||
patchbuf nil errbuf))
|
; patch to a non-exisiting file, so replace the buffer
|
||||||
; gofmt succeeded: apply patch hunks.
|
; completely with the output of 'gofmt'.
|
||||||
(progn
|
; If the file exists, patch it to keep the 'undo' list happy.
|
||||||
(kill-buffer errbuf)
|
(let* ((newfile (not (file-exists-p filename)))
|
||||||
(gofmt-apply-patch filename srcbuf patchbuf)
|
(flag (if newfile "" " -d")))
|
||||||
(set-window-configuration currconf))
|
(if (= 0 (shell-command-on-region (point-min) (point-max)
|
||||||
|
(concat "gofmt" flag)
|
||||||
|
patchbuf nil errbuf))
|
||||||
|
; gofmt succeeded: replace buffer or apply patch hunks.
|
||||||
|
(let ((old-point (point))
|
||||||
|
(old-mark (mark t)))
|
||||||
|
(kill-buffer errbuf)
|
||||||
|
(if newfile
|
||||||
|
; New file, replace it (diff-mode won't work)
|
||||||
|
(gofmt-replace-buffer srcbuf patchbuf)
|
||||||
|
; Existing file, patch it
|
||||||
|
(gofmt-apply-patch filename srcbuf patchbuf))
|
||||||
|
(goto-char (min old-point (point-max)))
|
||||||
|
;; Restore the mark and point
|
||||||
|
(if old-mark (push-mark (min old-mark (point-max)) t))
|
||||||
|
(set-window-configuration currconf))
|
||||||
|
|
||||||
;; gofmt failed: display the errors
|
;; gofmt failed: display the errors
|
||||||
(gofmt-process-errors filename errbuf)))))
|
(gofmt-process-errors filename errbuf))))))
|
||||||
|
|
||||||
;; Collapse any window opened on outbuf if shell-command-on-region
|
;; Collapse any window opened on outbuf if shell-command-on-region
|
||||||
;; displayed it.
|
;; displayed it.
|
||||||
(delete-windows-on patchbuf)))
|
(delete-windows-on patchbuf)))
|
||||||
(kill-buffer patchbuf))))
|
(kill-buffer patchbuf))))
|
||||||
|
|
||||||
|
(defun gofmt-replace-buffer (srcbuf patchbuf)
|
||||||
|
(with-current-buffer srcbuf
|
||||||
|
(erase-buffer)
|
||||||
|
(insert-buffer-substring patchbuf)))
|
||||||
|
|
||||||
(defconst gofmt-stdin-tag "<standard input>")
|
(defconst gofmt-stdin-tag "<standard input>")
|
||||||
|
|
||||||
(defun gofmt-apply-patch (filename srcbuf patchbuf)
|
(defun gofmt-apply-patch (filename srcbuf patchbuf)
|
||||||
(require 'diff-mode)
|
(require 'diff-mode)
|
||||||
;; apply all the patch hunks and restore the mark and point
|
;; apply all the patch hunks and restore the mark and point
|
||||||
(let ((old-point (point))
|
(with-current-buffer patchbuf
|
||||||
(old-mark (mark t)))
|
(let ((filename (file-name-nondirectory filename))
|
||||||
(with-current-buffer patchbuf
|
(min (point-min)))
|
||||||
(let ((filename (file-name-nondirectory filename))
|
(replace-string gofmt-stdin-tag filename nil min (point-max))
|
||||||
(min (point-min)))
|
(replace-regexp "^--- /tmp/gofmt[0-9]*" (concat "--- /tmp/" filename)
|
||||||
(replace-string gofmt-stdin-tag filename nil min (point-max))
|
nil min (point-max)))
|
||||||
(replace-regexp "^--- /tmp/gofmt[0-9]*" (concat "--- /tmp/" filename)
|
(condition-case nil
|
||||||
nil min (point-max)))
|
(while t
|
||||||
(condition-case nil
|
(diff-hunk-next)
|
||||||
(while t
|
(diff-apply-hunk))
|
||||||
(diff-hunk-next)
|
;; When there's no more hunks, diff-hunk-next signals an error, ignore it
|
||||||
(diff-apply-hunk))
|
(error nil))))
|
||||||
;; When there's no more hunks, diff-hunk-next signals an error, ignore it
|
|
||||||
(error nil)))
|
|
||||||
(goto-char (min old-point (point-max)))
|
|
||||||
(if old-mark (push-mark (min old-mark (point-max)) t))))
|
|
||||||
|
|
||||||
(defun gofmt-process-errors (filename errbuf)
|
(defun gofmt-process-errors (filename errbuf)
|
||||||
;; Convert the gofmt stderr to something understood by the compilation mode.
|
;; Convert the gofmt stderr to something understood by the compilation mode.
|
||||||
|
Loading…
Reference in New Issue
Block a user