Skip to content

Commit 5c1e5cc

Browse files
committed
Replace gofmt-before-save with gofmt minor mode
Using a minor mode has the advantage that disabling the hook is easier. To enable it (add-hook 'go-mode-hook 'gofmt-mode)
1 parent 8dce1e3 commit 5c1e5cc

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

go-mode.el

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,10 +1751,16 @@ The following extra functions are defined:
17511751
- `godef-describe' and `godef-jump'
17521752
- `go-coverage'
17531753
1754-
If you want to automatically run `gofmt' before saving a file,
1755-
add the following hook to your Emacs configuration:
1754+
If you want to automatically run `gofmt' before saving a file enable the
1755+
gofmt minor mode. To do so you can add the following to to your Emacs
1756+
configuration:
17561757
1757-
\(add-hook 'before-save-hook #'gofmt-before-save)
1758+
\(add-hook 'go-mode-hook #'gofmt-mode)
1759+
1760+
Or if you use `use-package':
1761+
1762+
\(use-package go-mode
1763+
:hook ((go-mode . gofmt-mode)))
17581764
17591765
If you want to use `godef-jump' instead of etags (or similar),
17601766
consider binding godef-jump to `M-.', which is the default key
@@ -1995,15 +2001,13 @@ arguments can be set as a list via ‘gofmt-args’."
19952001
(kill-buffer errbuf))))
19962002

19972003
;;;###autoload
1998-
(defun gofmt-before-save ()
1999-
"Add this to .emacs to run gofmt on the current buffer when saving:
2000-
\(add-hook 'before-save-hook 'gofmt-before-save).
2001-
2002-
Note that this will cause ‘go-mode’ to get loaded the first time
2003-
you save any file, kind of defeating the point of autoloading."
2004-
2005-
(interactive)
2006-
(when (eq major-mode 'go-mode) (gofmt)))
2004+
(define-minor-mode gofmt-mode
2005+
"Runs gofmt before saving the file."
2006+
:lighther "gofmt"
2007+
:global nil
2008+
(if gofmt-mode
2009+
(add-hook 'before-save-hook 'gofmt nil 'local)
2010+
(remove-hook 'before-save-hook 'gofmt 'local)))
20072011

20082012
(defun godoc--read-query ()
20092013
"Read a godoc query from the minibuffer."

0 commit comments

Comments
 (0)