diff --git a/README.org b/README.org index dabc7ae..efa8347 100644 --- a/README.org +++ b/README.org @@ -668,7 +668,7 @@ for technical information. display-buffer-pop-up-window (inhibit-same-window . t))) (add-to-list 'display-buffer-alist - `(,(rx (or "*Gofmt*" "*Occur*" "*grep*" "*xref*")) + `(,(rx (or "*Occur*" "*grep*" "*xref*")) display-buffer-reuse-window (inhibit-same-window . nil))) ;; BUG#70773: Eli tells `(setq delayed-warnings-hook nil)' is better. @@ -4503,14 +4503,20 @@ Links to learn [[https://go.dev/][Go]] are: 1. [[https://www.golangprograms.com/go-language.html][Go programming language]] contains a tutorial and a reference. 2. [[https://gobyexample.com/][Go by example]] is a showcase of short examples with explanations. 3. [[https://interpreterbook.com/][Writing An Interpreter In Go]] is book showing how to write a real program. +Listing [[lst:setup-go]] defines: +- =gotsfmt= which is a cheap small replacement for =gofmt= in [[https://github.com/dominikh/go-mode.el][go-mode]]. Adding + =gofmt= to =before-save-hook= is safe, but adding =gotsfmt= to it is unsafe + because =gotsfmt= calls =save-buffer=. +- =gots8tabs= which sets tab stops to the default 8 spaces locally in the + current buffer if the major mode is =go-ts-mode=. +- =gots2tabs= which sets tab stops to 2 spaces locally in the current buffer if + the major mode is =go-ts-mode=. #+caption[Setup Go programming]: #+caption: Setup Go programming. #+name: lst:setup-go #+begin_src emacs-lisp -n :results silent (when (featurep 'treesit) - (setopt go-ts-mode-indent-offset 8) ; for compatibility with Go tools. - (defun gots2tabs () "Set tab stops to 2 spaces in current `go-ts` buffer." (interactive) @@ -4523,32 +4529,37 @@ Links to learn [[https://go.dev/][Go]] are: (interactive) (when (eq major-mode 'go-ts-mode) (setq-local go-ts-mode-indent-offset 8) - (setq-local tab-width 9))) + (setq-local tab-width 8))) (defun gotsfmt () "Let `gofmt` format current `go-ts` buffer and file." (interactive) (when (eq major-mode 'go-ts-mode) - (save-buffer) - (let ((cb (current-buffer)) - (pb (get-buffer-create "*Gofmt*")) - (fn (buffer-file-name))) - (set-buffer pb) + (let ((source (current-buffer)) + (goal (get-buffer-create "*Gofmt Output*")) + (filename (buffer-file-name))) + (when (eq t (buffer-modified-p source)) ; autosaved is not OK. + (save-buffer)) + (set-buffer goal) (erase-buffer) - (if (zerop (call-process "gofmt" nil t nil "-d" fn)) + ;; Direct stdin and stderr to goal. + ;; Get goal with filename in patch by calling "gofmt -d filename". + ;; Provoke a gofmt error (delete package statement) to debug this. + (if (zerop (call-process "gofmt" nil t nil "-d" filename)) (if (> (point) (point-min)) (progn - (display-buffer pb) - (switch-to-buffer pb) - (diff-apply-hunk) - (set-buffer cb) + (diff-apply-buffer) + (set-buffer source) (save-buffer) (message "Gofmt tabbed your code!")) (message "Gofmt adores your code!")) - (display-buffer pb) - (switch-to-buffer pb) + (display-buffer goal) (message "Your code chokes Gofmt!"))))) + (add-to-list 'display-buffer-alist `("*Gofmt Output*" + display-buffer-reuse-window + (inhibit-same-window . nil))) + (add-to-list 'auto-mode-alist `(,(rx ".go" eos) . go-ts-mode))) #+end_src