forked from clojure-emacs/cider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcider-interaction.el
2113 lines (1843 loc) · 85.7 KB
/
cider-interaction.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
;;; cider-interaction.el --- IDE for Clojure -*- lexical-binding: t -*-
;; Copyright © 2012-2015 Tim King, Phil Hagelberg
;; Copyright © 2013-2015 Bozhidar Batsov, Hugo Duncan, Steve Purcell
;;
;; Author: Tim King <[email protected]>
;; Phil Hagelberg <[email protected]>
;; Bozhidar Batsov <[email protected]>
;; Hugo Duncan <[email protected]>
;; Steve Purcell <[email protected]>
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Provides an Emacs Lisp client to connect to Clojure nREPL servers.
;;; Code:
(require 'cider-client)
(require 'cider-util)
(require 'cider-stacktrace)
(require 'cider-test)
(require 'cider-doc)
(require 'clojure-mode)
(require 'dash)
(require 'thingatpt)
(require 'etags)
(require 'arc-mode)
(require 'ansi-color)
(require 'cl-lib)
(require 'compile)
(require 'tramp)
(defconst cider-error-buffer "*cider-error*")
(defconst cider-read-eval-buffer "*cider-read-eval*")
(defconst cider-doc-buffer "*cider-doc*")
(defconst cider-result-buffer "*cider-result*")
(defconst cider-nrepl-session-buffer "*cider-nrepl-session*")
(define-obsolete-variable-alias 'cider-use-local-resources
'cider-prefer-local-resources "0.7.0")
(defcustom cider-prefer-local-resources nil
"Prefer local resources to remote (tramp) ones when both are available."
:type 'boolean
:group 'cider)
(defcustom cider-show-error-buffer t
"Control the popup behavior of cider stacktraces.
The following values are possible t or 'always, 'except-in-repl,
'only-in-repl. Any other value, including nil, will cause the stacktrace
not to be automatically shown.
Irespective of the value of this variable, the `cider-error-buffer' is
always generated in the background. Use `cider-visit-error-buffer' to
navigate to this buffer."
:type '(choice (const :tag "always" t)
(const except-in-repl)
(const only-in-repl)
(const :tag "never" nil))
:group 'cider)
(define-obsolete-variable-alias 'cider-popup-stacktraces
'cider-show-error-buffer "0.7.0")
(defcustom cider-auto-jump-to-error t
"When non-nil automatically jump to error location during interactive compilation.
When set to 'errors-only, don't jump to warnings."
:type '(choice (const :tag "always" t)
(const errors-only)
(const :tag "never" nil))
:group 'cider
:package-version '(cider . "0.7.0"))
(defcustom cider-auto-select-error-buffer t
"Controls whether to auto-select the error popup buffer."
:type 'boolean
:group 'cider)
(defcustom cider-interactive-eval-result-prefix "=> "
"The prefix displayed in the minibuffer before a result value."
:type 'string
:group 'cider
:package-version '(cider . "0.5.0"))
(defcustom cider-switch-to-repl-command 'cider-switch-to-relevant-repl-buffer
"Select the command to be invoked when switching-to-repl.
The default option is `cider-switch-to-relevant-repl-buffer'. If
you'd like to not use smart matching of repl buffer based on
project directory, you can assign it to `cider-switch-to-current-repl-buffer'
which will use the default REPL connection."
:type 'symbol
:group 'cider)
(defcustom cider-prompt-save-file-on-load t
"Controls whether to prompt to save the file when loading a buffer."
:type 'boolean
:group 'cider
:package-version '(cider . "0.6.0"))
(defcustom cider-prompt-for-symbol t
"Controls when to prompt for symbol when a command requires one.
When non-nil, always prompt, and use the symbol at point as the default
value at the prompt.
When nil, attempt to use the symbol at point for the command, and only
prompt if that throws an error."
:type '(choice (const :tag "always" t)
(const :tag "dwim" nil))
:group 'cider
:package-version '(cider . "0.9.0"))
(defcustom cider-completion-use-context t
"When true, uses context at point to improve completion suggestions."
:type 'boolean
:group 'cider
:package-version '(cider . "0.7.0"))
(defcustom cider-annotate-completion-candidates t
"When true, annotate completion candidates with some extra information."
:type 'boolean
:group 'cider
:package-version '(cider . "0.8.0"))
(defcustom cider-annotate-completion-function
#'cider-default-annotate-completion-function
"Controls how the annotations for completion candidates are formatted.
Must be a function that takes two arguments: the abbreviation of the
candidate type according to `cider-completion-annotations-alist' and the
candidate's namespace."
:type 'function
:group 'cider
:package-version '(cider . "0.9.0"))
(defcustom cider-completion-annotations-alist
'(("class" "c")
("field" "fi")
("function" "f")
("import" "i")
("keyword" "k")
("local" "l")
("macro" "m")
("method" "me")
("namespace" "n")
("protocol" "p")
("protocol-function" "pf")
("record" "r")
("special-form" "s")
("static-field" "sf")
("static-method" "sm")
("type" "t")
("var" "v"))
"Controls the abbreviations used when annotating completion candidates.
Must be a list of elements with the form (TYPE . ABBREVIATION), where TYPE
is a possible value of the candidate's type returned from the completion
backend, and ABBREVIATION is a short form of that type."
:type '(alist :key-type string :value-type string)
:group 'cider
:package-version '(cider . "0.9.0"))
(defcustom cider-completion-annotations-include-ns 'unqualified
"Controls passing of namespaces to `cider-annotate-completion-function.'
When set to 'always, the candidate's namespace will always be passed if it
is available. When set to 'unqualified, the namespace will only be passed
if the candidate is not namespace-qualified."
:type '(choice (const always)
(const unqualified)
(const :tag "never" nil))
:group 'cider
:package-version '(cider . "0.9.0"))
(defconst cider-output-buffer "*cider-out*")
(defcustom cider-interactive-eval-output-destination 'repl-buffer
"The destination for stdout and stderr produced from interactive evaluation."
:type '(choice (const output-buffer)
(const repl-buffer))
:group 'cider
:package-version '(cider . "0.7.0"))
(defface cider-error-highlight-face
'((((supports :underline (:style wave)))
(:underline (:style wave :color "red") :inherit unspecified))
(t (:inherit font-lock-warning-face :underline t)))
"Face used to highlight compilation errors in Clojure buffers."
:group 'cider)
(defface cider-warning-highlight-face
'((((supports :underline (:style wave)))
(:underline (:style wave :color "yellow") :inherit unspecified))
(t (:inherit font-lock-warning-face :underline (:color "yellow"))))
"Face used to highlight compilation warnings in Clojure buffers."
:group 'cider)
(defvar cider-required-nrepl-ops
'("apropos" "classpath" "complete" "eldoc" "format-code" "format-edn" "info"
"inspect-pop" "inspect-push" "inspect-refresh"
"macroexpand" "ns-list" "ns-vars"
"resource" "stacktrace" "toggle-trace-var" "toggle-trace-ns" "undef")
"A list of nREPL ops required by CIDER to function properly.
All of them are provided by CIDER's nREPL middleware (cider-nrepl).")
(defvar cider-required-nrepl-version "0.2.7"
"The minimum nREPL version that's known to work properly with CIDER.")
(defvar-local cider-buffer-ns nil
"Current Clojure namespace of some buffer.
Useful for special buffers (e.g. REPL, doc buffers) that have to
keep track of a namespace.
This should never be set in Clojure buffers, as there the namespace
should be extracted from the buffer's ns form.")
(defun cider-ensure-op-supported (op)
"Check for support of middleware op OP.
Signal an error if it is not supported."
(unless (nrepl-op-supported-p op)
(error "Can't find nREPL middleware providing op \"%s\". Please, install (or update) cider-nrepl %s and restart CIDER" op (upcase cider-version))))
(defun cider--check-required-nrepl-ops ()
"Check whether all required nREPL ops are present."
(let ((missing-ops (-remove 'nrepl-op-supported-p cider-required-nrepl-ops)))
(when missing-ops
(cider-repl-emit-interactive-err-output
(format "WARNING: The following required nREPL ops are not supported: \n%s\nPlease, install (or update) cider-nrepl %s and restart CIDER"
(cider-string-join missing-ops " ")
(upcase cider-version))))))
;;; Connection info
(defun cider--java-version ()
"Retrieve the underlying connection's Java version."
(with-current-buffer (nrepl-current-connection-buffer)
(when nrepl-versions
(-> nrepl-versions
(nrepl-dict-get "java")
(nrepl-dict-get "version-string")))))
(defun cider--clojure-version ()
"Retrieve the underlying connection's Clojure version."
(with-current-buffer (nrepl-current-connection-buffer)
(when nrepl-versions
(-> nrepl-versions
(nrepl-dict-get "clojure")
(nrepl-dict-get "version-string")))))
(defun cider--nrepl-version ()
"Retrieve the underlying connection's nREPL version."
(with-current-buffer (nrepl-current-connection-buffer)
(when nrepl-versions
(-> nrepl-versions
(nrepl-dict-get "nrepl")
(nrepl-dict-get "version-string")))))
(defun cider--check-required-nrepl-version ()
"Check whether we're using a compatible nREPL version."
(let ((nrepl-version (cider--nrepl-version)))
(if nrepl-version
(when (version< nrepl-version cider-required-nrepl-version)
(cider-repl-emit-interactive-err-output
(format "WARNING: CIDER requires nREPL %s (or newer) to work properly" cider-required-nrepl-version)))
(cider-repl-emit-interactive-err-output
(format "WARNING: Can't determine nREPL's version. Please, update nREPL to %s." cider-required-nrepl-version)))))
(defun cider--check-middleware-compatibility-callback (buffer)
"A callback to check if the middleware used is compatible with CIDER."
(nrepl-make-response-handler
buffer
(lambda (_buffer result)
(let ((middleware-version (read result)))
(unless (and middleware-version (equal cider-version middleware-version))
(cider-repl-emit-interactive-err-output (format "WARNING: CIDER's version (%s) does not match cider-nrepl's version (%s)" cider-version middleware-version)))))
'()
'()
'()))
(defun cider--check-middleware-compatibility ()
"Retrieve the underlying connection's CIDER nREPL version."
(nrepl-request:eval
"(try
(require 'cider.nrepl.version)
(:version-string @(resolve 'cider.nrepl.version/version))
(catch Throwable _ \"not installed\"))"
(cider--check-middleware-compatibility-callback (current-buffer))))
(defun cider--connection-info (connection-buffer)
"Return info about CONNECTION-BUFFER.
Info contains project name, current REPL namespace, host:port
endpoint and Clojure version."
(with-current-buffer (get-buffer connection-buffer)
(format "Active nREPL connection: %s:%s, %s:%s (Java %s, Clojure %s, nREPL %s)"
(or (nrepl--project-name nrepl-project-dir) "<no project>")
cider-buffer-ns
(car nrepl-endpoint)
(cadr nrepl-endpoint)
(cider--java-version)
(cider--clojure-version)
(cider--nrepl-version))))
(defun cider-display-current-connection-info ()
"Display information about the current connection."
(interactive)
(message (cider--connection-info (nrepl-current-connection-buffer))))
(defun cider-rotate-connection ()
"Rotate and display the current nREPL connection."
(interactive)
(cider-ensure-connected)
(setq nrepl-connection-list
(append (cdr nrepl-connection-list)
(list (car nrepl-connection-list))))
(message (cider--connection-info (car nrepl-connection-list))))
(defun cider-extract-designation-from-current-repl-buffer ()
"Extract the designation from the cider repl buffer name."
(let ((repl-buffer-name (cider-current-repl-buffer))
(template (split-string nrepl-repl-buffer-name-template "%s")))
(string-match (format "^%s\\(.*\\)%s"
(regexp-quote (concat (car template) nrepl-buffer-name-separator))
(regexp-quote (cadr template)))
repl-buffer-name)
(or (match-string 1 repl-buffer-name) "<no designation>")))
(defun cider-change-buffers-designation ()
"Change the designation in cider buffer names.
Buffer names changed are cider-repl and nrepl-server."
(interactive)
(cider-ensure-connected)
(let* ((designation (read-string (format "Change CIDER buffer designation from '%s': "
(cider-extract-designation-from-current-repl-buffer))))
(new-repl-buffer-name (nrepl-format-buffer-name-template
nrepl-repl-buffer-name-template designation)))
(with-current-buffer (cider-current-repl-buffer)
(rename-buffer new-repl-buffer-name)
(setq-local nrepl-repl-buffer new-repl-buffer-name)
(setq-local nrepl-connection-buffer new-repl-buffer-name)
(setq nrepl-connection-list
(cons new-repl-buffer-name (cdr nrepl-connection-list)))
(when nrepl-server-buffer
(let ((new-server-buffer-name (nrepl-format-buffer-name-template
nrepl-server-buffer-name-template designation)))
(with-current-buffer nrepl-server-buffer
(rename-buffer new-server-buffer-name))
(setq-local nrepl-server-buffer new-server-buffer-name))))
(message "CIDER buffer designation changed to: %s" designation)))
;;; Switching between REPL & source buffers
(defvar-local cider-last-clojure-buffer nil
"A buffer-local variable holding the last Clojure source buffer.
`cider-switch-to-last-clojure-buffer' uses this variable to jump
back to last Clojure source buffer.")
(defvar cider-current-clojure-buffer nil
"This variable holds current buffer temporarily when connecting to a REPL.
It is set to current buffer when `cider' or `cider-jack-in' is called.
After the REPL buffer is created, the value of this variable is used
to call `cider-remember-clojure-buffer'.")
(defun cider-remember-clojure-buffer (buffer)
"Try to remember the BUFFER from which the user jumps.
The BUFFER needs to be a Clojure buffer and current major mode needs
to be `cider-repl-mode'. The user can use `cider-switch-to-last-clojure-buffer'
to jump back to the last Clojure source buffer."
(when (and buffer
(with-current-buffer buffer
(derived-mode-p 'clojure-mode))
(derived-mode-p 'cider-repl-mode))
(setq cider-last-clojure-buffer buffer)))
(defun cider-switch-to-repl-buffer (&optional arg)
"Invoke `cider-switch-to-repl-command'."
(interactive "p")
(funcall cider-switch-to-repl-command arg))
(defun cider-switch-to-current-repl-buffer (&optional arg)
"Select the REPL buffer, when possible in an existing window.
Hint: You can use `display-buffer-reuse-frames' and
`special-display-buffer-names' to customize the frame in which
the buffer should appear.
With a prefix ARG sets the namespace in the REPL buffer to that
of the namespace in the Clojure source buffer."
(interactive "p")
(cider-ensure-connected)
(let ((buffer (current-buffer)))
(when (eq 4 arg)
(cider-repl-set-ns (cider-current-ns)))
(pop-to-buffer (cider-get-repl-buffer))
(cider-remember-clojure-buffer buffer)
(goto-char (point-max))))
(defun cider-find-connection-buffer-for-project-directory (project-directory)
"Find the relevant connection-buffer for the given PROJECT-DIRECTORY.
A check is made to ensure that all connection buffers have a project-directory
otherwise there is ambiguity as to which connection buffer should be selected.
If there are multiple connection buffers matching PROJECT-DIRECTORY there
is ambiguity, therefore nil is returned."
(unless (-filter
(lambda (conn)
(not
(with-current-buffer (get-buffer conn)
nrepl-project-dir)))
nrepl-connection-list)
(let ((matching-connections
(-filter
(lambda (conn)
(let ((conn-proj-dir (with-current-buffer (get-buffer conn)
nrepl-project-dir)))
(when conn-proj-dir
(equal (file-truename project-directory)
(file-truename conn-proj-dir)))))
nrepl-connection-list)))
(when (= 1 (length matching-connections))
(car matching-connections)))))
(defun cider-switch-to-relevant-repl-buffer (&optional arg)
"Select the REPL buffer, when possible in an existing window.
The buffer chosen is based on the file open in the current buffer.
If the REPL buffer cannot be unambiguously determined, the REPL
buffer is chosen based on the current connection buffer and a
message raised informing the user.
Hint: You can use `display-buffer-reuse-frames' and
`special-display-buffer-names' to customize the frame in which
the buffer should appear.
With a prefix ARG sets the namespace in the REPL buffer to that
of the namespace in the Clojure source buffer.
With a second prefix ARG the chosen REPL buffer is based on a
supplied project directory."
(interactive "p")
(cider-ensure-connected)
(let* ((project-directory
(or (when (eq 16 arg) (read-directory-name "Project: "))
(nrepl-project-directory-for (nrepl-current-dir))))
(connection-buffer
(or
(and (= 1 (length nrepl-connection-list)) (car nrepl-connection-list))
(and project-directory
(cider-find-connection-buffer-for-project-directory project-directory)))))
(when connection-buffer
(setq nrepl-connection-list
(cons connection-buffer (delq connection-buffer nrepl-connection-list))))
(cider-switch-to-current-repl-buffer arg)
(message
(format (if connection-buffer
"Switched to REPL: %s"
"Could not determine relevant nREPL connection, using: %s")
(with-current-buffer (nrepl-current-connection-buffer)
(format "%s:%s, %s:%s"
(or (nrepl--project-name nrepl-project-dir) "<no project>")
cider-buffer-ns
(car nrepl-endpoint)
(cadr nrepl-endpoint)))))))
(defun cider-switch-to-last-clojure-buffer ()
"Switch to the last Clojure buffer.
The default keybinding for this command is
the same as `cider-switch-to-repl-buffer',
so that it is very convenient to jump between a
Clojure buffer and the REPL buffer."
(interactive)
(if (and (derived-mode-p 'cider-repl-mode)
(buffer-live-p cider-last-clojure-buffer))
(pop-to-buffer cider-last-clojure-buffer)
(message "Don't know the original Clojure buffer")))
(defun cider-find-and-clear-repl-buffer ()
"Find the current REPL buffer and clear it.
Returns to the buffer in which the command was invoked."
(interactive)
(let ((origin-buffer (current-buffer)))
(switch-to-buffer (cider-current-repl-buffer))
(cider-repl-clear-buffer)
(switch-to-buffer origin-buffer)))
(defvar cider-minibuffer-history '()
"History list of expressions read from the minibuffer.")
(defvar cider-minibuffer-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map minibuffer-local-map)
(define-key map (kbd "TAB") 'complete-symbol)
(define-key map (kbd "M-TAB") 'complete-symbol)
map)
"Minibuffer keymap used for reading Clojure expressions.")
(defun cider-read-from-minibuffer (prompt &optional initial-value)
"Read a string from the minibuffer, prompting with PROMPT.
If INITIAL-VALUE is non-nil, it is inserted into the minibuffer before
reading input."
(minibuffer-with-setup-hook
(lambda ()
(set-syntax-table clojure-mode-syntax-table)
(add-hook 'completion-at-point-functions
#'cider-complete-at-point nil t)
(run-hooks 'eval-expression-minibuffer-setup-hook))
(read-from-minibuffer prompt initial-value
cider-minibuffer-map nil
'cider-minibuffer-history)))
;;; Utilities
(defun cider--clear-compilation-highlights ()
"Remove compilation highlights."
(remove-overlays (point-min) (point-max) 'cider-note-p t))
(defun cider-clear-compilation-highlights (&optional arg)
"Remove compilation highlights.
When invoked with a prefix ARG the command doesn't prompt for confirmation."
(interactive "P")
(when (or arg (y-or-n-p "Are you sure you want to clear the compilation highlights? "))
(cider--clear-compilation-highlights)))
(defun cider--quit-error-window ()
"Buries the `cider-error-buffer' and quits its containing window."
(-when-let (error-win (get-buffer-window cider-error-buffer))
(quit-window nil error-win)))
(defun cider-defun-at-point ()
"Return the text of the top-level sexp at point."
(apply #'buffer-substring-no-properties
(cider--region-for-defun-at-point)))
(defun cider--region-for-defun-at-point ()
"Return the start and end position of defun at point."
(save-excursion
(save-match-data
(end-of-defun)
(let ((end (point)))
(beginning-of-defun)
(list (point) end)))))
(defun cider-defun-at-point-start-pos ()
"Return the starting position of the current defun."
(car (cider--region-for-defun-at-point)))
(defun cider-ns-form ()
"Retrieve the ns form."
(when (clojure-find-ns)
(save-excursion
(goto-char (match-beginning 0))
(cider-defun-at-point))))
(defun cider-bounds-of-sexp-at-point ()
"Return the bounds sexp at point as a pair (or nil)."
(or (and (equal (char-after) ?\()
(member (char-before) '(?\' ?\, ?\@))
;; hide stuff before ( to avoid quirks with '( etc.
(save-restriction
(narrow-to-region (point) (point-max))
(bounds-of-thing-at-point 'sexp)))
(bounds-of-thing-at-point 'sexp)))
;; FIXME: This doesn't have properly at the beginning of the REPL prompt
(defun cider-symbol-at-point ()
"Return the name of the symbol at point, otherwise nil."
(let ((str (substring-no-properties (or (thing-at-point 'symbol) ""))))
(if (equal str (concat (cider-current-ns) "> "))
""
str)))
(defun cider-sexp-at-point ()
"Return the sexp at point as a string, otherwise nil."
(let ((bounds (cider-bounds-of-sexp-at-point)))
(if bounds
(buffer-substring-no-properties (car bounds)
(cdr bounds)))))
(defun cider-sexp-at-point-with-bounds ()
"Return a list containing the sexp at point and its bounds."
(let ((bounds (cider-bounds-of-sexp-at-point)))
(if bounds
(let ((start (car bounds))
(end (cdr bounds)))
(list (buffer-substring-no-properties start end)
(cons (set-marker (make-marker) start)
(set-marker (make-marker) end)))))))
(defun cider-last-sexp ()
"Return the sexp preceding the point."
(buffer-substring-no-properties
(save-excursion
(backward-sexp)
(point))
(point)))
(defun cider-last-sexp-start-pos ()
(save-excursion
(backward-sexp)
(point)))
;;;
(defun cider-tramp-prefix (&optional buffer)
"Use the filename for BUFFER to determine a tramp prefix.
Defaults to the current buffer.
Return the tramp prefix, or nil if BUFFER is local."
(let* ((buffer (or buffer (current-buffer)))
(name (or (buffer-file-name buffer)
(with-current-buffer buffer
default-directory))))
(when (tramp-tramp-file-p name)
(let ((vec (tramp-dissect-file-name name)))
(tramp-make-tramp-file-name (tramp-file-name-method vec)
(tramp-file-name-user vec)
(tramp-file-name-host vec)
nil)))))
(defun cider--client-tramp-filename (name &optional buffer)
"Return the tramp filename for path NAME relative to BUFFER.
If BUFFER has a tramp prefix, it will be added as a prefix to NAME.
If the resulting path is an existing tramp file, it returns the path,
otherwise, nil."
(let* ((buffer (or buffer (current-buffer)))
(name (concat (cider-tramp-prefix buffer) name)))
(if (tramp-handle-file-exists-p name)
name)))
(defun cider--server-filename (name)
"Return the nREPL server-relative filename for NAME."
(if (tramp-tramp-file-p name)
(with-parsed-tramp-file-name name nil
localname)
name))
(defvar cider-from-nrepl-filename-function
(if (eq system-type 'cygwin)
(lambda (resource) (let ((fixed-resource (replace-regexp-in-string "^/" "" resource)))
(replace-regexp-in-string
"\n" ""
(shell-command-to-string (format "cygpath --unix '%s'" fixed-resource)))))
#'identity)
"Function to translate nREPL namestrings to Emacs filenames.")
(defun cider--file-path (path)
"Return PATH's local or tramp path using `cider-prefer-local-resources'.
If no local or remote file exists, return nil."
(let* ((local-path (funcall cider-from-nrepl-filename-function path))
(tramp-path (and local-path (cider--client-tramp-filename local-path))))
(cond ((equal local-path "") "")
((and cider-prefer-local-resources (file-exists-p local-path))
local-path)
((and tramp-path (file-exists-p tramp-path))
tramp-path)
((and local-path (file-exists-p local-path))
local-path))))
(defun cider--url-to-file (url)
"Return the filename from the resource URL.
Uses `url-generic-parse-url' to parse the url. The filename is extracted and
then url decoded. If the decoded filename has a Windows device letter followed
by a colon immediately after the leading '/' then the leading '/' is dropped to
create a valid path."
(let ((filename (url-unhex-string (url-filename (url-generic-parse-url url)))))
(if (string-match "^/\\([a-zA-Z]:/.*\\)" filename)
(match-string 1 filename)
filename)))
(defun cider--tooling-file-p (file-name)
"Return t if FILE-NAME is not a 'real' source file.
Currently, only check if the relative file name starts with 'form-init'
which nREPL uses for temporary evaluation file names."
(let ((fname (file-name-nondirectory file-name)))
(string-match-p "^form-init" fname)))
(defun cider-find-file (url)
"Return a buffer visiting the file URL if it exists, or nil otherwise.
If URL has a scheme prefix, it must represent a fully-qualified file path
or an entry within a zip/jar archive. If URL doesn't contain a scheme
prefix and is an absolute path, it is treated as such. Finally, if URL is
relative, it is expanded within each of the open Clojure buffers till an
existing file ending with URL has been found."
(cond ((string-match "^file:\\(.+\\)" url)
(-when-let* ((file (cider--url-to-file (match-string 1 url)))
(path (cider--file-path file)))
(find-file-noselect path)))
((string-match "^\\(jar\\|zip\\):\\(file:.+\\)!/\\(.+\\)" url)
(-when-let* ((entry (match-string 3 url))
(file (cider--url-to-file (match-string 2 url)))
(path (cider--file-path file))
(name (format "%s:%s" path entry)))
(or (find-buffer-visiting name)
(if (tramp-tramp-file-p path)
(progn
;; Use emacs built in archiving
(find-file path)
(goto-char (point-min))
;; Make sure the file path is followed by a newline to
;; prevent eg. clj matching cljs.
(search-forward (concat entry "\n"))
;; moves up to matching line
(forward-line -1)
(archive-extract)
(current-buffer))
;; Use external zip program to just extract the single file
(with-current-buffer (generate-new-buffer
(file-name-nondirectory entry))
(archive-zip-extract path entry)
(set-visited-file-name name)
(setq-local default-directory (file-name-directory path))
(setq-local buffer-read-only t)
(set-buffer-modified-p nil)
(set-auto-mode)
(current-buffer))))))
(t (-if-let (path (cider--file-path url))
(find-file-noselect path)
(unless (file-name-absolute-p url)
(let ((cider-buffers (cider-util--clojure-buffers))
(url (file-name-nondirectory url)))
(or (cl-loop for bf in cider-buffers
for path = (with-current-buffer bf
(expand-file-name url))
if (and path (file-exists-p path))
return (find-file-noselect path))
(cl-loop for bf in cider-buffers
if (string= (buffer-name bf) url)
return bf))))))))
(defun cider-find-var-file (var)
"Return the buffer visiting the file in which VAR is defined, or nil if
not found."
(cider-ensure-op-supported "info")
(-when-let* ((info (cider-var-info var))
(file (nrepl-dict-get info "file")))
(cider-find-file file)))
(defun cider-jump-to (buffer &optional pos other-window)
"Push current point onto marker ring, and jump to BUFFER and POS.
POS can be either a numeric position in BUFFER or a cons (LINE . COLUMN)
where COLUMN can be nil. If OTHER-WINDOW is non-nil don't reuse current
window."
(ring-insert find-tag-marker-ring (point-marker))
(if other-window
(pop-to-buffer buffer)
;; like switch-to-buffer, but reuse existing window if BUFFER is visible
(pop-to-buffer buffer '((display-buffer-reuse-window display-buffer-same-window))))
(with-current-buffer buffer
(widen)
(goto-char (point-min))
(cider-mode +1)
(if (consp pos)
(progn
(forward-line (1- (or (car pos) 1)))
(if (cdr pos)
(move-to-column (cdr pos))
(back-to-indentation)))
(when pos
(goto-char pos)))))
(defun cider-jump-to-resource (path)
"Jump to the resource at the resource-relative PATH.
When called interactively, this operates on point."
(interactive (list (thing-at-point 'filename)))
(cider-ensure-op-supported "resource")
(-if-let* ((resource (cider-sync-request:resource path))
(buffer (cider-find-file resource)))
(cider-jump-to buffer)
(error "Cannot find resource %s" path)))
(defun cider--jump-to-loc-from-info (info &optional other-window)
"Jump to location give by INFO.
INFO object is returned by `cider-var-info' or `cider-member-info'.
OTHER-WINDOW is passed to `cider-jamp-to'."
(let* ((line (nrepl-dict-get info "line"))
(file (nrepl-dict-get info "file"))
(buffer (and file
(not (cider--tooling-file-p file))
(cider-find-file file))))
(if buffer
(cider-jump-to buffer (cons line nil) other-window)
(error "No source location"))))
(defun cider--jump-to-var (var &optional line)
"Jump to the definition of VAR, optionally at a specific LINE."
(-if-let (info (cider-var-info var))
(progn
(if line (setq info (nrepl-dict-put info "line" line)))
(cider--jump-to-loc-from-info info))
(error "Symbol %s not resolved" var)))
(defun cider-jump-to-var (&optional arg var line)
"Jump to the definition of VAR, optionally at a specific LINE.
Prompts for the symbol to use, or uses the symbol at point, depending on
the value of `cider-prompt-for-symbol'. With prefix arg ARG, does the
opposite of what that option dictates."
(interactive "P")
(cider-ensure-op-supported "info")
(if var
(cider--jump-to-var var line)
(funcall (cider-prompt-for-symbol-function arg)
"Symbol: "
#'cider--jump-to-var)))
(define-obsolete-function-alias 'cider-jump 'cider-jump-to-var "0.7.0")
(defalias 'cider-jump-back 'pop-tag-mark)
(defvar cider-completion-last-context nil)
(defun cider-completion-symbol-start-pos ()
"Find the starting position of the symbol at point, unless inside a string."
(let ((sap (symbol-at-point)))
(when (and sap (not (in-string-p)))
(car (bounds-of-thing-at-point 'symbol)))))
(defun cider-completion-get-context-at-point ()
"Extract the context at point.
If point is not inside the list, returns nil; otherwise return top-level
form, with symbol at point replaced by __prefix__."
(when (save-excursion
(condition-case _
(progn
(up-list)
(check-parens)
t)
(scan-error nil)
(user-error nil)))
(save-excursion
(let* ((pref-end (point))
(pref-start (cider-completion-symbol-start-pos))
(context (cider-defun-at-point))
(_ (beginning-of-defun))
(expr-start (point)))
(concat (substring context 0 (- pref-start expr-start))
"__prefix__"
(substring context (- pref-end expr-start)))))))
(defun cider-completion-get-context ()
"Extract context depending on `cider-completion-use-context' and major mode."
(let ((context (if (and cider-completion-use-context
;; Important because `beginning-of-defun' and
;; `ending-of-defun' work incorrectly in the REPL
;; buffer, so context extraction fails there.
(derived-mode-p 'clojure-mode))
(or (cider-completion-get-context-at-point)
"nil")
"nil")))
(if (string= cider-completion-last-context context)
":same"
(setq cider-completion-last-context context)
context)))
(defun cider-completion--parse-candidate-map (candidate-map)
(let ((candidate (nrepl-dict-get candidate-map "candidate"))
(type (nrepl-dict-get candidate-map "type"))
(ns (nrepl-dict-get candidate-map "ns")))
(put-text-property 0 1 'type type candidate)
(put-text-property 0 1 'ns ns candidate)
candidate))
(defun cider-complete (str)
"Complete STR with context at point."
(let* ((context (cider-completion-get-context))
(candidates (cider-sync-request:complete str context)))
(mapcar #'cider-completion--parse-candidate-map candidates)))
(defun cider-completion--get-candidate-type (symbol)
(let ((type (get-text-property 0 'type symbol)))
(or (cl-second (assoc type cider-completion-annotations-alist))
type)))
(defun cider-completion--get-candidate-ns (symbol)
(when (or (eq 'always cider-completion-annotations-include-ns)
(and (eq 'unqualified cider-completion-annotations-include-ns)
(not (cider-namespace-qualified-p symbol))))
(get-text-property 0 'ns symbol)))
(defun cider-default-annotate-completion-function (type ns)
(concat (when ns (format " (%s)" ns))
(when type (format " <%s>" type))))
(defun cider-annotate-symbol (symbol)
"Return a string suitable for annotating SYMBOL.
If SYMBOL has a text property `type` whose value is recognised, its
abbreviation according to `cider-completion-annotations-alist' will be
used. If `type` is present but not recognised, its value will be used
unaltered.
If SYMBOL has a text property `ns`, then its value will be used according
to `cider-completion-annotations-include-ns'.
The formatting is performed by `cider-annotate-completion-function'."
(when cider-annotate-completion-candidates
(let* ((type (cider-completion--get-candidate-type symbol))
(ns (cider-completion--get-candidate-ns symbol)))
(funcall cider-annotate-completion-function type ns))))
(defun cider-complete-at-point ()
"Complete the symbol at point."
(let ((sap (symbol-at-point)))
(when (and sap (not (in-string-p)) (cider-connected-p))
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(list (car bounds) (cdr bounds)
(completion-table-dynamic #'cider-complete)
:annotation-function #'cider-annotate-symbol
:company-doc-buffer #'cider-create-doc-buffer
:company-location #'cider-company-location
:company-docsig #'cider-company-docsig)))))
(defun cider-company-location (var)
"Open VAR's definition in a buffer.
Returns the cons of the buffer itself and the location of VAR's definition
in the buffer."
(-when-let* ((info (cider-var-info var))
(file (nrepl-dict-get info "file"))
(line (nrepl-dict-get info "line"))
(buffer (cider-find-file file)))
(with-current-buffer buffer
(save-excursion
(goto-char (point-min))
(forward-line (1- line))
(cons buffer (point))))))
(defun cider-company-docsig (thing)
"Return signature for THING."
(let ((arglist (cider-eldoc-arglist thing)))
(when arglist
(format "%s: %s"
(cider-eldoc-format-thing thing)
(cider-eldoc-format-arglist arglist 0)))))
(defun cider-javadoc-handler (symbol-name)
"Invoke the nREPL \"info\" op on SYMBOL-NAME if available."
(when symbol-name
(cider-ensure-op-supported "info")
(let* ((info (cider-var-info symbol-name))
(url (nrepl-dict-get info "javadoc")))
(if url
(browse-url url)
(error "No Javadoc available for %s" symbol-name)))))
(defun cider-javadoc (arg)
"Open Javadoc documentation in a popup buffer.
Prompts for the symbol to use, or uses the symbol at point, depending on
the value of `cider-prompt-for-symbol'. With prefix arg ARG, does the
opposite of what that option dictates."
(interactive "P")
(funcall (cider-prompt-for-symbol-function arg)
"Javadoc for: "
#'cider-javadoc-handler))
(defun cider-stdin-handler (&optional buffer)
"Make a stdin response handler for BUFFER."
(nrepl-make-response-handler (or buffer (current-buffer))
(lambda (buffer value)
(cider-repl-emit-result buffer value t))
(lambda (buffer out)
(cider-repl-emit-output buffer out))
(lambda (buffer err)
(cider-repl-emit-output buffer err))
nil))
(defun cider-insert-eval-handler (&optional buffer)
"Make a nREPL evaluation handler for the BUFFER.
The handler simply inserts the result value in BUFFER."
(let ((eval-buffer (current-buffer)))
(nrepl-make-response-handler (or buffer eval-buffer)
(lambda (_buffer value)
(with-current-buffer buffer
(insert value)))
(lambda (_buffer out)
(cider-repl-emit-interactive-output out))
(lambda (buffer err)
(cider-handle-compilation-errors err eval-buffer))
'())))
(defun cider--emit-interactive-eval-output (output repl-emit-function)
"Emit output resulting from interactive code evaluation.