-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-mode.el
1560 lines (1372 loc) · 59.6 KB
/
go-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; go-mode.el --- Major mode for the Go programming language
;; Copyright 2013 The go-mode Authors. All rights reserved.
;; Use of this source code is governed by a BSD-style
;; license that can be found in the LICENSE file.
;; Author: The go-mode Authors
;; Version: 1.2.0
;; Keywords: languages go
;; URL: https://github.com/dominikh/go-mode.el
;;
;; This file is not part of GNU Emacs.
;;; Code:
(require 'cl)
(require 'etags)
(require 'ffap)
(require 'find-file)
(require 'ring)
(require 'url)
;; XEmacs compatibility guidelines
;; - Minimum required version of XEmacs: 21.5.32
;; - Feature that cannot be backported: POSIX character classes in
;; regular expressions
;; - Functions that could be backported but won't because 21.5.32
;; covers them: plenty.
;; - Features that are still partly broken:
;; - godef will not work correctly if multibyte characters are
;; being used
;; - Fontification will not handle unicode correctly
;;
;; - Do not use \_< and \_> regexp delimiters directly; use
;; go--regexp-enclose-in-symbol
;;
;; - The character `_` must not be a symbol constituent but a
;; character constituent
;;
;; - Do not use process-lines
;;
;; - Use go--old-completion-list-style when using a plain list as the
;; collection for completing-read
;;
;; - Use go--position-bytes instead of position-bytes
(defmacro go--xemacs-p ()
`(featurep 'xemacs))
(defun go--delete-whole-line (&optional arg)
"Delete the current line without putting it in the `kill-ring'.
Derived from function `kill-whole-line'. ARG is defined as for that
function."
(setq arg (or arg 1))
(if (and (> arg 0)
(eobp)
(save-excursion (forward-visible-line 0) (eobp)))
(signal 'end-of-buffer nil))
(if (and (< arg 0)
(bobp)
(save-excursion (end-of-visible-line) (bobp)))
(signal 'beginning-of-buffer nil))
(cond ((zerop arg)
(delete-region (progn (forward-visible-line 0) (point))
(progn (end-of-visible-line) (point))))
((< arg 0)
(delete-region (progn (end-of-visible-line) (point))
(progn (forward-visible-line (1+ arg))
(unless (bobp)
(backward-char))
(point))))
(t
(delete-region (progn (forward-visible-line 0) (point))
(progn (forward-visible-line arg) (point))))))
;; declare-function is an empty macro that only byte-compile cares
;; about. Wrap in always false if to satisfy Emacsen without that
;; macro.
(if nil
(declare-function go--position-bytes "go-mode" (point)))
;; XEmacs unfortunately does not offer position-bytes. We can fall
;; back to just using (point), but it will be incorrect as soon as
;; multibyte characters are being used.
(if (fboundp 'position-bytes)
(defalias 'go--position-bytes #'position-bytes)
(defun go--position-bytes (point) point))
(defun go--old-completion-list-style (list)
(mapcar (lambda (x) (cons x nil)) list))
;; GNU Emacs 24 has prog-mode, older GNU Emacs and XEmacs do not, so
;; copy its definition for those.
(if (not (fboundp 'prog-mode))
(define-derived-mode prog-mode fundamental-mode "Prog"
"Major mode for editing source code."
(set (make-local-variable 'require-final-newline) mode-require-final-newline)
(set (make-local-variable 'parse-sexp-ignore-comments) t)
(setq bidi-paragraph-direction 'left-to-right)))
(defun go--regexp-enclose-in-symbol (s)
"Enclose S as regexp symbol.
XEmacs does not support \\_<, GNU Emacs does. In GNU Emacs we
make extensive use of \\_< to support unicode in identifiers.
Until we come up with a better solution for XEmacs, this solution
will break fontification in XEmacs for identifiers such as
\"typeµ\". XEmacs will consider \"type\" a keyword, GNU Emacs
won't."
(if (go--xemacs-p)
(concat "\\<" s "\\>")
(concat "\\_<" s "\\_>")))
(defun go-goto-opening-parenthesis (&optional _legacy-unused)
"Move up one level of parentheses."
;; The old implementation of go-goto-opening-parenthesis had an
;; optional argument to speed up the function. It didn't change the
;; function's outcome.
;; Silently fail if there's no matching opening parenthesis.
(condition-case nil
(backward-up-list)
(scan-error nil)))
(defconst go-dangling-operators-regexp "[^-]-\\|[^+]\\+\\|[/*&><.=|^]")
(defconst go-identifier-regexp "[[:word:][:multibyte:]]+")
(defconst go-type-name-no-prefix-regexp "\\(?:[[:word:][:multibyte:]]+\\.\\)?[[:word:][:multibyte:]]+")
(defconst go-qualified-identifier-regexp (concat go-identifier-regexp "\\." go-identifier-regexp))
(defconst go-label-regexp go-identifier-regexp)
(defconst go-type-regexp "[[:word:][:multibyte:]*]+")
(defconst go-func-regexp (concat (go--regexp-enclose-in-symbol "func") "\\s *\\(" go-identifier-regexp "\\)"))
(defconst go-func-meth-regexp (concat
(go--regexp-enclose-in-symbol "func") "\\s *\\(?:(\\s *"
"\\(" go-identifier-regexp "\\s +\\)?" go-type-regexp
"\\s *)\\s *\\)?\\("
go-identifier-regexp
"\\)("))
(defconst go-builtins
'("append" "cap" "close" "complex" "copy"
"delete" "imag" "len" "make" "new"
"panic" "print" "println" "real" "recover")
"All built-in functions in the Go language. Used for font locking.")
(defconst go-mode-keywords
'("break" "default" "func" "interface" "select"
"case" "defer" "go" "map" "struct"
"chan" "else" "goto" "package" "switch"
"const" "fallthrough" "if" "range" "type"
"continue" "for" "import" "return" "var")
"All keywords in the Go language. Used for font locking.")
(defconst go-constants '("nil" "true" "false" "iota"))
(defconst go-type-name-regexp (concat "\\(?:[*(]\\)*\\(\\(?:" go-identifier-regexp "\\.\\)?" go-identifier-regexp "\\)"))
;; Maximum number of identifiers that can be highlighted as type names
;; in one function type/declaration.
(defconst go--font-lock-func-param-num-groups 16)
(defvar go-dangling-cache)
(defvar go-godoc-history nil)
(defvar go--coverage-current-file-name)
(defgroup go nil
"Major mode for editing Go code."
:link '(url-link "https://github.com/dominikh/go-mode.el")
:group 'languages)
(defgroup go-cover nil
"Options specific to `cover`."
:group 'go)
(defcustom go-fontify-function-calls t
"Fontify function and method calls if this is non-nil."
:type 'boolean
:group 'go)
(defcustom go-mode-hook nil
"Hook called by `go-mode'."
:type 'hook
:group 'go)
(defcustom go-command "go"
"The 'go' command.
Some users have multiple Go development trees and invoke the 'go'
tool via a wrapper that sets GOROOT and GOPATH based on the
current directory. Such users should customize this variable to
point to the wrapper script."
:type 'string
:group 'go)
(defcustom gofmt-command "gofmt"
"The 'gofmt' command.
Some users may replace this with 'goimports'
from https://github.com/bradfitz/goimports."
:type 'string
:group 'go)
(defcustom gofmt-show-errors 'buffer
"Where to display gofmt error output.
It can either be displayed in its own buffer, in the echo area, or not at all.
Please note that Emacs outputs to the echo area when writing
files and will overwrite gofmt's echo output if used from inside
a `before-save-hook'."
:type '(choice
(const :tag "Own buffer" buffer)
(const :tag "Echo area" echo)
(const :tag "None" nil))
:group 'go)
(defcustom godef-command "godef"
"The 'godef' command."
:type 'string
:group 'go)
(defcustom go-other-file-alist
'(("_test\\.go\\'" (".go"))
("\\.go\\'" ("_test.go")))
"See the documentation of `ff-other-file-alist' for details."
:type '(repeat (list regexp (choice (repeat string) function)))
:group 'go)
(defun go--kill-new-message (url)
"Make URL the latest kill and print a message."
(kill-new url)
(message "%s" url))
(defcustom go-play-browse-function 'go--kill-new-message
"Function to call with the Playground URL.
See `go-play-region' for more details."
:type '(choice
(const :tag "Nothing" nil)
(const :tag "Kill + Message" go--kill-new-message)
(const :tag "Browse URL" browse-url)
(function :tag "Call function"))
:group 'go)
(defcustom go-coverage-display-buffer-func 'display-buffer-reuse-window
"How `go-coverage' should display the coverage buffer.
See `display-buffer' for a list of possible functions."
:type 'function
:group 'go-cover)
(defface go-coverage-untracked
'((t (:foreground "#505050")))
"Coverage color of untracked code."
:group 'go-cover)
(defface go-coverage-0
'((t (:foreground "#c00000")))
"Coverage color for uncovered code."
:group 'go-cover)
(defface go-coverage-1
'((t (:foreground "#808080")))
"Coverage color for covered code with weight 1."
:group 'go-cover)
(defface go-coverage-2
'((t (:foreground "#748c83")))
"Coverage color for covered code with weight 2."
:group 'go-cover)
(defface go-coverage-3
'((t (:foreground "#689886")))
"Coverage color for covered code with weight 3."
:group 'go-cover)
(defface go-coverage-4
'((t (:foreground "#5ca489")))
"Coverage color for covered code with weight 4."
:group 'go-cover)
(defface go-coverage-5
'((t (:foreground "#50b08c")))
"Coverage color for covered code with weight 5."
:group 'go-cover)
(defface go-coverage-6
'((t (:foreground "#44bc8f")))
"Coverage color for covered code with weight 6."
:group 'go-cover)
(defface go-coverage-7
'((t (:foreground "#38c892")))
"Coverage color for covered code with weight 7."
:group 'go-cover)
(defface go-coverage-8
'((t (:foreground "#2cd495")))
"Coverage color for covered code with weight 8.
For mode=set, all covered lines will have this weight."
:group 'go-cover)
(defface go-coverage-9
'((t (:foreground "#20e098")))
"Coverage color for covered code with weight 9."
:group 'go-cover)
(defface go-coverage-10
'((t (:foreground "#14ec9b")))
"Coverage color for covered code with weight 10."
:group 'go-cover)
(defface go-coverage-covered
'((t (:foreground "#2cd495")))
"Coverage color of covered code."
:group 'go-cover)
(defvar go-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?+ "." st)
(modify-syntax-entry ?- "." st)
(modify-syntax-entry ?% "." st)
(modify-syntax-entry ?& "." st)
(modify-syntax-entry ?| "." st)
(modify-syntax-entry ?^ "." st)
(modify-syntax-entry ?! "." st)
(modify-syntax-entry ?= "." st)
(modify-syntax-entry ?< "." st)
(modify-syntax-entry ?> "." st)
(modify-syntax-entry ?/ (if (go--xemacs-p) ". 1456" ". 124b") st)
(modify-syntax-entry ?* ". 23" st)
(modify-syntax-entry ?\n "> b" st)
(modify-syntax-entry ?\" "\"" st)
(modify-syntax-entry ?\' "\"" st)
(modify-syntax-entry ?` "\"" st)
(modify-syntax-entry ?\\ "\\" st)
;; It would be nicer to have _ as a symbol constituent, but that
;; would trip up XEmacs, which does not support the \_< anchor
(modify-syntax-entry ?_ "w" st)
st)
"Syntax table for Go mode.")
(defun go--build-font-lock-keywords ()
;; we cannot use 'symbols in regexp-opt because GNU Emacs <24
;; doesn't understand that
(append
`((go--match-func
,@(mapcar (lambda (x) `(,x font-lock-type-face))
(number-sequence 1 go--font-lock-func-param-num-groups)))
(,(go--regexp-enclose-in-symbol (regexp-opt go-mode-keywords t)) . font-lock-keyword-face)
(,(concat "\\(" (go--regexp-enclose-in-symbol (regexp-opt go-builtins t)) "\\)[[:space:]]*(") 1 font-lock-builtin-face)
(,(go--regexp-enclose-in-symbol (regexp-opt go-constants t)) . font-lock-constant-face)
(,go-func-regexp 1 font-lock-function-name-face)) ;; function (not method) name
(if go-fontify-function-calls
`((,(concat "\\(" go-identifier-regexp "\\)[[:space:]]*(") 1 font-lock-function-name-face) ;; function call/method name
(,(concat "[^[:word:][:multibyte:]](\\(" go-identifier-regexp "\\))[[:space:]]*(") 1 font-lock-function-name-face)) ;; bracketed function call
`((,go-func-meth-regexp 2 font-lock-function-name-face))) ;; method name
`(
("\\(`[^`]*`\\)" 1 font-lock-multiline) ;; raw string literal, needed for font-lock-syntactic-keywords
(,(concat (go--regexp-enclose-in-symbol "type") "[[:space:]]+\\([^[:space:]]+\\)") 1 font-lock-type-face) ;; types
(,(concat (go--regexp-enclose-in-symbol "type") "[[:space:]]+" go-identifier-regexp "[[:space:]]*" go-type-name-regexp) 1 font-lock-type-face) ;; types
(,(concat "[^[:word:][:multibyte:]]\\[\\([[:digit:]]+\\|\\.\\.\\.\\)?\\]" go-type-name-regexp) 2 font-lock-type-face) ;; Arrays/slices
(,(concat "\\(" go-identifier-regexp "\\)" "{") 1 font-lock-type-face)
(,(concat (go--regexp-enclose-in-symbol "map") "\\[[^]]+\\]" go-type-name-regexp) 1 font-lock-type-face) ;; map value type
(,(concat (go--regexp-enclose-in-symbol "map") "\\[" go-type-name-regexp) 1 font-lock-type-face) ;; map key type
(,(concat (go--regexp-enclose-in-symbol "chan") "[[:space:]]*\\(?:<-[[:space:]]*\\)?" go-type-name-regexp) 1 font-lock-type-face) ;; channel type
(,(concat (go--regexp-enclose-in-symbol "\\(?:new\\|make\\)") "\\(?:[[:space:]]\\|)\\)*(" go-type-name-regexp) 1 font-lock-type-face) ;; new/make type
;; TODO do we actually need this one or isn't it just a function call?
(,(concat "\\.\\s *(" go-type-name-regexp) 1 font-lock-type-face) ;; Type conversion
;; Like the original go-mode this also marks compound literal
;; fields. There, it was marked as to fix, but I grew quite
;; accustomed to it, so it'll stay for now.
(,(concat "^[[:space:]]*\\(" go-label-regexp "\\)[[:space:]]*:\\(\\S.\\|$\\)") 1 font-lock-constant-face) ;; Labels and compound literal fields
(,(concat (go--regexp-enclose-in-symbol "\\(goto\\|break\\|continue\\)") "[[:space:]]*\\(" go-label-regexp "\\)") 2 font-lock-constant-face)))) ;; labels in goto/break/continue
(defconst go--font-lock-syntactic-keywords
;; Override syntax property of raw string literal contents, so that
;; backslashes have no special meaning in ``. Used in Emacs 23 or older.
'((go--match-raw-string-literal
(1 (7 . ?`))
(2 (15 . nil)) ;; 15 = "generic string"
(3 (7 . ?`)))))
(defvar go-mode-map
(let ((m (make-sparse-keymap)))
(define-key m "}" #'go-mode-insert-and-indent)
(define-key m ")" #'go-mode-insert-and-indent)
(define-key m "," #'go-mode-insert-and-indent)
(define-key m ":" #'go-mode-insert-and-indent)
(define-key m "=" #'go-mode-insert-and-indent)
(define-key m (kbd "C-c C-a") #'go-import-add)
(define-key m (kbd "C-c C-j") #'godef-jump)
(define-key m (kbd "C-x 4 C-c C-j") #'godef-jump-other-window)
(define-key m (kbd "C-c C-d") #'godef-describe)
m)
"Keymap used by Go mode to implement electric keys.")
(easy-menu-define go-mode-menu go-mode-map
"Menu for Go mode."
'("Go"
["Describe Expression" godef-describe t]
["Jump to Definition" godef-jump t]
"---"
["Add Import" go-import-add t]
["Remove Unused Imports" go-remove-unused-imports t]
["Go to Imports" go-goto-imports t]
"---"
("Playground"
["Send Buffer" go-play-buffer t]
["Send Region" go-play-region t]
["Download" go-download-play t])
"---"
["Coverage" go-coverage t]
["Gofmt" gofmt t]
["Godoc" godoc t]
"---"
["Customize Mode" (customize-group 'go) t]))
(defun go-mode-insert-and-indent (key)
"Invoke the global binding of KEY, then reindent the line."
(interactive (list (this-command-keys)))
(call-interactively (lookup-key (current-global-map) key))
(indent-according-to-mode))
(defmacro go-paren-level ()
`(car (syntax-ppss)))
(defmacro go-in-string-or-comment-p ()
`(nth 8 (syntax-ppss)))
(defmacro go-in-string-p ()
`(nth 3 (syntax-ppss)))
(defmacro go-in-comment-p ()
`(nth 4 (syntax-ppss)))
(defmacro go-goto-beginning-of-string-or-comment ()
`(goto-char (nth 8 (syntax-ppss))))
(defun go--backward-irrelevant (&optional stop-at-string)
"Skip backwards over any characters that are irrelevant for
indentation and related tasks.
It skips over whitespace, comments, cases and labels and, if
STOP-AT-STRING is not true, over strings."
(let (pos (start-pos (point)))
(skip-chars-backward "\n\s\t")
(if (and (save-excursion (beginning-of-line) (go-in-string-p)) (looking-back "`") (not stop-at-string))
(backward-char))
(if (and (go-in-string-p) (not stop-at-string))
(go-goto-beginning-of-string-or-comment))
(if (looking-back "\\*/")
(backward-char))
(if (go-in-comment-p)
(go-goto-beginning-of-string-or-comment))
(setq pos (point))
(beginning-of-line)
(if (or (looking-at (concat "^" go-label-regexp ":")) (looking-at "^[[:space:]]*\\(case .+\\|default\\):"))
(end-of-line 0)
(goto-char pos))
(if (/= start-pos (point))
(go--backward-irrelevant stop-at-string))
(/= start-pos (point))))
(defun go--buffer-narrowed-p ()
"Return non-nil if the current buffer is narrowed."
(/= (buffer-size)
(- (point-max)
(point-min))))
(defun go--match-raw-string-literal (end)
"Search for a raw string literal.
Set point to the end of the occurence found on success. Return nil on failure."
(unless (go-in-string-or-comment-p)
(when (search-forward "`" end t)
(goto-char (match-beginning 0))
(if (go-in-string-or-comment-p)
(progn (goto-char (match-end 0))
(go--match-raw-string-literal end))
(when (looking-at "\\(`\\)\\([^`]*\\)\\(`\\)")
(goto-char (match-end 0))
t)))))
(defun go-previous-line-has-dangling-op-p ()
"Return non-nil if the current line is a continuation line."
(let* ((cur-line (line-number-at-pos))
(val (gethash cur-line go-dangling-cache 'nope)))
(if (or (go--buffer-narrowed-p) (equal val 'nope))
(save-excursion
(beginning-of-line)
(go--backward-irrelevant t)
(setq val (looking-back go-dangling-operators-regexp))
(if (not (go--buffer-narrowed-p))
(puthash cur-line val go-dangling-cache))))
val))
(defun go--at-function-definition ()
"Return non-nil if point is on the opening curly brace of a
function definition.
We do this by first calling (beginning-of-defun), which will take
us to the start of *some* function. We then look for the opening
curly brace of that function and compare its position against the
curly brace we are checking. If they match, we return non-nil."
(if (= (char-after) ?\{)
(save-excursion
(let ((old-point (point))
start-nesting)
(beginning-of-defun)
(when (looking-at "func ")
(setq start-nesting (go-paren-level))
(skip-chars-forward "^{")
(while (> (go-paren-level) start-nesting)
(forward-char)
(skip-chars-forward "^{") 0)
(if (and (= (go-paren-level) start-nesting) (= old-point (point)))
t))))))
(defun go--indentation-for-opening-parenthesis ()
"Return the semantic indentation for the current opening parenthesis.
If point is on an opening curly brace and said curly brace
belongs to a function declaration, the indentation of the func
keyword will be returned. Otherwise the indentation of the
current line will be returned."
(save-excursion
(if (go--at-function-definition)
(progn
(beginning-of-defun)
(current-indentation))
(current-indentation))))
(defun go-indentation-at-point ()
(save-excursion
(let (start-nesting)
(back-to-indentation)
(setq start-nesting (go-paren-level))
(cond
((go-in-string-p)
(current-indentation))
((looking-at "[])}]")
(go-goto-opening-parenthesis)
(if (go-previous-line-has-dangling-op-p)
(- (current-indentation) tab-width)
(go--indentation-for-opening-parenthesis)))
((progn (go--backward-irrelevant t) (looking-back go-dangling-operators-regexp))
;; only one nesting for all dangling operators in one operation
(if (go-previous-line-has-dangling-op-p)
(current-indentation)
(+ (current-indentation) tab-width)))
((zerop (go-paren-level))
0)
((progn (go-goto-opening-parenthesis) (< (go-paren-level) start-nesting))
(if (go-previous-line-has-dangling-op-p)
(current-indentation)
(+ (go--indentation-for-opening-parenthesis) tab-width)))
(t
(current-indentation))))))
(defun go-mode-indent-line ()
(interactive)
(let (indent
shift-amt
(pos (- (point-max) (point)))
(point (point))
(beg (line-beginning-position)))
(back-to-indentation)
(if (go-in-string-or-comment-p)
(goto-char point)
(setq indent (go-indentation-at-point))
(if (looking-at (concat go-label-regexp ":\\([[:space:]]*/.+\\)?$\\|case .+:\\|default:"))
(decf indent tab-width))
(setq shift-amt (- indent (current-column)))
(if (zerop shift-amt)
nil
(delete-region beg (point))
(indent-to indent))
;; If initial point was within line's indentation,
;; position after the indentation. Else stay at same point in text.
(if (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos))))))
(defun go-beginning-of-defun (&optional count)
(unless (bolp)
(end-of-line))
(setq count (or count 1))
(let (first failure)
(dotimes (i (abs count))
(setq first t)
(while (and (not failure)
(or first (go-in-string-or-comment-p)))
(if (>= count 0)
(progn
(go--backward-irrelevant)
(if (not (re-search-backward go-func-meth-regexp nil t))
(setq failure t)))
(if (looking-at go-func-meth-regexp)
(forward-char))
(if (not (re-search-forward go-func-meth-regexp nil t))
(setq failure t)))
(setq first nil)))
(if (< count 0)
(beginning-of-line))
(not failure)))
(defun go-end-of-defun ()
(let (orig-level)
;; It can happen that we're not placed before a function by emacs
(if (not (looking-at "func"))
(go-beginning-of-defun -1))
;; Find the { that starts the function, i.e., the next { that isn't
;; preceded by struct or interface, or a comment or struct tag. BUG:
;; breaks if there's a comment between the struct/interface keyword and
;; bracket, like this:
;;
;; struct /* why? */ {
(while (progn
(skip-chars-forward "^{")
(forward-char)
(or (go-in-string-or-comment-p)
(looking-back "\\(struct\\|interface\\)\\s-*{"))))
(setq orig-level (go-paren-level))
(while (>= (go-paren-level) orig-level)
(skip-chars-forward "^}")
(forward-char))))
(defun go--find-enclosing-parentheses (position)
"Return points of outermost '(' and ')' surrounding POSITION if
such parentheses exist.
If outermost '(' exists but ')' does not, it returns the next blank
line or end-of-buffer position instead of the position of the closing
parenthesis.
If the starting parenthesis is not found, it returns (POSITION
POSITION)."
(save-excursion
(let (beg end)
(goto-char position)
(while (> (go-paren-level) 0)
(re-search-backward "[(\\[{]" nil t)
(when (looking-at "(")
(setq beg (point))))
(if (null beg)
(list position position)
(goto-char position)
(while (and (> (go-paren-level) 0)
(search-forward ")" nil t)))
(when (> (go-paren-level) 0)
(unless (re-search-forward "^[[:space:]]*$" nil t)
(goto-char (point-max))))
(list beg (point))))))
(defun go--search-next-comma (end)
"Search forward from point for a comma whose nesting level is
the same as point. If it reaches the end of line or a closing
parenthesis before a comma, it stops at it."
(let ((orig-level (go-paren-level)))
(while (and (< (point) end)
(or (looking-at "[^,)\n]")
(> (go-paren-level) orig-level)))
(forward-char))
(when (and (looking-at ",")
(< (point) (1- end)))
(forward-char))))
(defun go--looking-at-keyword ()
(and (looking-at (concat "\\(" go-identifier-regexp "\\)"))
(member (match-string 1) go-mode-keywords)))
(defun go--match-func (end)
"Search for identifiers used as type names from a function
parameter list, and set the identifier positions as the results
of last search. Return t if search succeeded."
(when (re-search-forward (go--regexp-enclose-in-symbol "func") end t)
(let ((regions (go--match-func-type-names end)))
(if (null regions)
;; Nothing to highlight. This can happen if the current func
;; is "func()". Try next one.
(go--match-func end)
;; There are something to highlight. Set those positions as
;; last search results.
(setq regions (go--filter-match-data regions end))
(when regions
(set-match-data (go--make-match-data regions))
t)))))
(defun go--match-func-type-names (end)
(cond
;; Function declaration (e.g. "func foo(")
((looking-at (concat "[[:space:]\n]*" go-identifier-regexp "[[:space:]\n]*("))
(goto-char (match-end 0))
(nconc (go--match-parameter-list end)
(go--match-function-result end)))
;; Method declaration, function literal, or function type
((looking-at "[[:space:]]*(")
(goto-char (match-end 0))
(let ((regions (go--match-parameter-list end)))
;; Method declaration (e.g. "func (x y) foo(")
(when (looking-at (concat "[[:space:]]*" go-identifier-regexp "[[:space:]\n]*("))
(goto-char (match-end 0))
(setq regions (nconc regions (go--match-parameter-list end))))
(nconc regions (go--match-function-result end))))))
(defun go--parameter-list-type (end)
"Return `present' if the parameter list has names, or `absent' if
not, assuming point is at the beginning of a parameter list, just
after '('."
(save-excursion
(skip-chars-forward "[:space:]\n" end)
(cond ((> (point) end)
nil)
((looking-at (concat go-identifier-regexp "[[:space:]\n]*,"))
(goto-char (match-end 0))
(go--parameter-list-type end))
((or (looking-at go-qualified-identifier-regexp)
(looking-at (concat go-type-name-no-prefix-regexp "[[:space:]\n]*\\(?:)\\|\\'\\)"))
(go--looking-at-keyword)
(looking-at "[*\\[]\\|\\.\\.\\.\\|\\'"))
'absent)
(t 'present))))
(defconst go--opt-dotdotdot-regexp "\\(?:\\.\\.\\.\\)?")
(defconst go--parameter-type-regexp
(concat go--opt-dotdotdot-regexp "[[:space:]*\n]*\\(" go-type-name-no-prefix-regexp "\\)[[:space:]\n]*\\([,)]\\|\\'\\)"))
(defconst go--func-type-in-parameter-list-regexp
(concat go--opt-dotdotdot-regexp "[[:space:]*\n]*\\(" (go--regexp-enclose-in-symbol "func") "\\)"))
(defun go--match-parameters-common (identifier-regexp end)
(let ((acc ())
(start -1))
(while (progn (skip-chars-forward "[:space:]\n" end)
(and (not (looking-at "\\(?:)\\|\\'\\)"))
(< start (point))
(<= (point) end)))
(setq start (point))
(cond
((looking-at (concat identifier-regexp go--parameter-type-regexp))
(setq acc (nconc acc (list (match-beginning 1) (match-end 1))))
(goto-char (match-beginning 2)))
((looking-at (concat identifier-regexp go--func-type-in-parameter-list-regexp))
(goto-char (match-beginning 1))
(setq acc (nconc acc (go--match-func-type-names end)))
(go--search-next-comma end))
(t
(go--search-next-comma end))))
(when (and (looking-at ")")
(< (point) end))
(forward-char))
acc))
(defun go--match-parameters-with-identifier-list (end)
(go--match-parameters-common
(concat go-identifier-regexp "[[:space:]\n]+")
end))
(defun go--match-parameters-without-identifier-list (end)
(go--match-parameters-common "" end))
(defun go--filter-match-data (regions end)
"Remove points from REGIONS if they are beyond END.
REGIONS are a list whose size is multiple of 2. Element 2n is beginning of a
region and 2n+1 is end of it.
This function is used to make sure we don't override end point
that `font-lock-mode' gave to us."
(when regions
(let* ((vec (vconcat regions))
(i 0)
(len (length vec)))
(while (and (< i len)
(<= (nth i regions) end)
(<= (nth (1+ i) regions) end))
(setq i (+ i 2)))
(cond ((= i len)
regions)
((zerop i)
nil)
(t
(setcdr (nth i regions) nil)
regions)))))
(defun go--make-match-data (regions)
(let ((deficit (- (* 2 go--font-lock-func-param-num-groups)
(length regions))))
(when (> deficit 0)
(let ((last (car (last regions))))
(setq regions (nconc regions (make-list deficit last))))))
`(,(car regions) ,@(last regions) ,@regions))
(defun go--match-parameter-list (end)
"Return a list of identifier positions that are used as type
names in a function parameter list, assuming point is at the
beginning of a parameter list. Return nil if the text after
point does not look like a parameter list.
Set point to end of closing parenthesis on success.
In Go, the names must either all be present or all be absent
within a list of parameters.
Parsing a parameter list is a little bit complicated because we
have to scan through the parameter list to determine whether or
not the list has names. Until a type name is found or reaching
end of a parameter list, we are not sure which form the parameter
list is.
For example, X and Y are type names in a parameter list \"(X,
Y)\" but are parameter names in \"(X, Y int)\". We cannot say if
X is a type name until we see int after Y.
Note that even \"(int, float T)\" is a valid parameter
list. Builtin type names are not reserved words. In this example,
int and float are parameter names and only T is a type name.
In this function, we first scan the parameter list to see if the
list has names, and then handle it accordingly."
(let ((name (go--parameter-list-type end)))
(cond ((eq name 'present)
(go--match-parameters-with-identifier-list end))
((eq name 'absent)
(go--match-parameters-without-identifier-list end))
(t nil))))
(defun go--match-function-result (end)
"Return a list of identifier positions that are used as type
names in a function result, assuming point is at the beginning of
a result.
Function result is a unparenthesized type or a parameter list."
(cond ((and (looking-at (concat "[[:space:]*]*\\(" go-type-name-no-prefix-regexp "\\)"))
(not (member (match-string 1) go-mode-keywords)))
(list (match-beginning 1) (match-end 1)))
((looking-at "[[:space:]]*(")
(goto-char (match-end 0))
(go--match-parameter-list end))
(t nil)))
;;;###autoload
(define-derived-mode go-mode prog-mode "Go"
"Major mode for editing Go source text.
This mode provides (not just) basic editing capabilities for
working with Go code. It offers almost complete syntax
highlighting, indentation that is almost identical to gofmt and
proper parsing of the buffer content to allow features such as
navigation by function, manipulation of comments or detection of
strings.
In addition to these core features, it offers various features to
help with writing Go code. You can directly run buffer content
through gofmt, read godoc documentation from within Emacs, modify
and clean up the list of package imports or interact with the
Playground (uploading and downloading pastes).
The following extra functions are defined:
- `gofmt'
- `godoc'
- `go-import-add'
- `go-remove-unused-imports'
- `go-goto-imports'
- `go-play-buffer' and `go-play-region'
- `go-download-play'
- `godef-describe' and `godef-jump'
- `go-coverage'
If you want to automatically run `gofmt' before saving a file,
add the following hook to your emacs configuration:
\(add-hook 'before-save-hook #'gofmt-before-save)
If you want to use `godef-jump' instead of etags (or similar),
consider binding godef-jump to `M-.', which is the default key
for `find-tag':
\(add-hook 'go-mode-hook (lambda ()
(local-set-key (kbd \"M-.\") #'godef-jump)))
Please note that godef is an external dependency. You can install
it with
go get code.google.com/p/rog-go/exp/cmd/godef
If you're looking for even more integration with Go, namely
on-the-fly syntax checking, auto-completion and snippets, it is
recommended that you look at flycheck
\(see URL `https://github.com/flycheck/flycheck') or flymake in combination
with goflymake \(see URL `https://github.com/dougm/goflymake'), gocode
\(see URL `https://github.com/nsf/gocode'), go-eldoc
\(see URL `github.com/syohex/emacs-go-eldoc') and yasnippet-go
\(see URL `https://github.com/dominikh/yasnippet-go')"
;; Font lock
(set (make-local-variable 'font-lock-defaults)
'(go--build-font-lock-keywords))
;; Indentation
(set (make-local-variable 'indent-line-function) #'go-mode-indent-line)
;; Comments
(set (make-local-variable 'comment-start) "// ")
(set (make-local-variable 'comment-end) "")
(set (make-local-variable 'comment-use-syntax) t)
(set (make-local-variable 'comment-start-skip) "\\(//+\\|/\\*+\\)\\s *")
(set (make-local-variable 'beginning-of-defun-function) #'go-beginning-of-defun)
(set (make-local-variable 'end-of-defun-function) #'go-end-of-defun)
(set (make-local-variable 'parse-sexp-lookup-properties) t)
(if (boundp 'syntax-propertize-function)
(set (make-local-variable 'syntax-propertize-function) #'go-propertize-syntax)
(set (make-local-variable 'font-lock-syntactic-keywords)
go--font-lock-syntactic-keywords)
(set (make-local-variable 'font-lock-multiline) t))
(set (make-local-variable 'go-dangling-cache) (make-hash-table :test 'eql))
(add-hook 'before-change-functions (lambda (x y) (setq go-dangling-cache (make-hash-table :test 'eql))) t t)
;; ff-find-other-file
(setq ff-other-file-alist 'go-other-file-alist)
(setq imenu-generic-expression
'(("type" "^type *\\([^ \t\n\r\f]*\\)" 1)
("func" "^func *\\(.*\\) {" 1)))
(imenu-add-to-menubar "Index")
;; Go style
(setq indent-tabs-mode t)
;; Handle unit test failure output in compilation-mode
;;
;; Note the final t argument to add-to-list for append, ie put these at the
;; *ends* of compilation-error-regexp-alist[-alist]. We want go-test to be
;; handled first, otherwise other elements will match that don't work, and
;; those alists are traversed in *reverse* order:
;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2001-12/msg00674.html
(when (and (boundp 'compilation-error-regexp-alist)
(boundp 'compilation-error-regexp-alist-alist))
(add-to-list 'compilation-error-regexp-alist 'go-test t)
(add-to-list 'compilation-error-regexp-alist-alist
'(go-test . ("^\t+\\([^()\t\n]+\\):\\([0-9]+\\):? .*$" 1 2)) t)))
;;;###autoload
(add-to-list 'auto-mode-alist (cons "\\.go\\'" 'go-mode))
(defun go--apply-rcs-patch (patch-buffer)
"Apply an RCS-formatted diff from PATCH-BUFFER to the current buffer."
(let ((target-buffer (current-buffer))
;; Relative offset between buffer line numbers and line numbers
;; in patch.
;;
;; Line numbers in the patch are based on the source file, so
;; we have to keep an offset when making changes to the
;; buffer.
;;
;; Appending lines decrements the offset (possibly making it
;; negative), deleting lines increments it. This order
;; simplifies the forward-line invocations.
(line-offset 0))
(save-excursion
(with-current-buffer patch-buffer
(goto-char (point-min))
(while (not (eobp))
(unless (looking-at "^\\([ad]\\)\\([0-9]+\\) \\([0-9]+\\)")
(error "invalid rcs patch or internal error in go--apply-rcs-patch"))
(forward-line)
(let ((action (match-string 1))
(from (string-to-number (match-string 2)))
(len (string-to-number (match-string 3))))
(cond
((equal action "a")
(let ((start (point)))
(forward-line len)
(let ((text (buffer-substring start (point))))
(with-current-buffer target-buffer
(decf line-offset len)
(goto-char (point-min))
(forward-line (- from len line-offset))
(insert text)))))
((equal action "d")
(with-current-buffer target-buffer
(go--goto-line (- from line-offset))
(incf line-offset len)
(go--delete-whole-line len)))
(t
(error "invalid rcs patch or internal error in go--apply-rcs-patch")))))))))
(defun gofmt ()
"Format the current buffer according to the gofmt tool."
(interactive)
(let ((tmpfile (make-temp-file "gofmt" nil ".go"))
(patchbuf (get-buffer-create "*Gofmt patch*"))
(errbuf (if gofmt-show-errors (get-buffer-create "*Gofmt Errors*")))
(coding-system-for-read 'utf-8)
(coding-system-for-write 'utf-8))
(save-restriction
(widen)
(if errbuf
(with-current-buffer errbuf
(setq buffer-read-only nil)
(erase-buffer)))
(with-current-buffer patchbuf
(erase-buffer))
(write-region nil nil tmpfile)
;; We're using errbuf for the mixed stdout and stderr output. This
;; is not an issue because gofmt -w does not produce any stdout
;; output in case of success.
(if (zerop (call-process gofmt-command nil errbuf nil "-w" tmpfile))