-
Notifications
You must be signed in to change notification settings - Fork 261
/
elpy.el
4073 lines (3642 loc) · 155 KB
/
elpy.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
;;; elpy.el --- Emacs Python Development Environment -*- lexical-binding: t -*-
;; Copyright (C) 2012-2019 Jorgen Schaefer
;; Author: Jorgen Schaefer <[email protected]>, Gaby Launay <[email protected]>
;; URL: https://github.com/jorgenschaefer/elpy
;; Version: 1.35.0
;; Keywords: Python, IDE, Languages, Tools
;; Package-Requires: ((company "0.9.10") (emacs "24.4") (highlight-indentation "0.7.0") (pyvenv "1.20") (yasnippet "0.13.0") (s "1.12.0"))
;; 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/>.
;;; Commentary:
;; The Emacs Lisp Python Environment in Emacs
;; Elpy is an Emacs package to bring powerful Python editing to Emacs.
;; It combines a number of existing Emacs packages, both written in
;; Emacs Lisp as well as Python.
;; For more information, read the Elpy manual:
;; https://elpy.readthedocs.io/en/latest/index.html
;;; Code:
(require 'cus-edit)
(require 'etags)
(require 'files-x)
(require 'grep)
(require 'hideshow)
(require 'ido)
(require 'json)
(require 'python)
(require 'subr-x)
(require 'xref nil t)
(require 'cl-lib) ; for `cl-every', `cl-copy-list', `cl-delete-if-not'
(require 'elpy-refactor)
(require 'elpy-django)
(require 'elpy-profile)
(require 'elpy-shell)
(require 'elpy-rpc)
(require 'pyvenv)
(defconst elpy-version "1.35.0"
"The version of the Elpy Lisp code.")
;;;;;;;;;;;;;;;;;;;;;;
;;; User customization
(defgroup elpy nil
"The Emacs Lisp Python Environment."
:prefix "elpy-"
:group 'languages)
(defcustom elpy-mode-hook nil
"Hook run when `elpy-mode' is enabled.
This can be used to enable minor modes for Python development."
:type 'hook
:options '(subword-mode hl-line-mode)
:group 'elpy)
(defcustom elpy-modules '(elpy-module-sane-defaults
elpy-module-company
elpy-module-eldoc
elpy-module-flymake
elpy-module-highlight-indentation
elpy-module-pyvenv
elpy-module-yasnippet
elpy-module-django)
"Which Elpy modules to use.
Elpy can use a number of modules for additional features, which
can be individually enabled or disabled."
:type '(set (const :tag "Inline code completion (company-mode)"
elpy-module-company)
(const :tag "Show function signatures (ElDoc)"
elpy-module-eldoc)
(const :tag "Highlight syntax errors (Flymake)"
elpy-module-flymake)
(const :tag "Code folding"
elpy-module-folding)
(const :tag "Show the virtualenv in the mode line (pyvenv)"
elpy-module-pyvenv)
(const :tag "Display indentation markers (highlight-indentation)"
elpy-module-highlight-indentation)
(const :tag "Expand code snippets (YASnippet)"
elpy-module-yasnippet)
(const :tag "Django configurations (Elpy-Django)"
elpy-module-django)
(const :tag "Automatically update documentation (Autodoc)."
elpy-module-autodoc)
(const :tag "Configure some sane defaults for Emacs"
elpy-module-sane-defaults))
:group 'elpy)
(defcustom elpy-project-ignored-directories
'(".tox" "build" "dist" ".cask" ".ipynb_checkpoints")
"Directories ignored by functions working on the whole project.
This is in addition to `vc-directory-exclusion-list'
and `grep-find-ignored-directories', as appropriate."
:type '(repeat string)
:safe (lambda (val)
(cl-every #'stringp val))
:group 'elpy)
(defun elpy-project-ignored-directories ()
"Compute the list of ignored directories for project.
Combines
`elpy-project-ignored-directories'
`vc-directory-exclusion-list'
`grep-find-ignored-directories'"
(delete-dups
(append elpy-project-ignored-directories
vc-directory-exclusion-list
(if (fboundp 'rgrep-find-ignored-directories)
(rgrep-find-ignored-directories (elpy-project-root))
(cl-delete-if-not #'stringp (cl-copy-list
grep-find-ignored-directories))))))
(defcustom elpy-project-root nil
"The root of the project the current buffer is in.
There is normally no use in setting this variable directly, as
Elpy tries to detect the project root automatically. See
`elpy-project-root-finder-functions' for a way of influencing
this.
Setting this variable globally will override Elpy's automatic
project detection facilities entirely.
Alternatively, you can set this in file- or directory-local
variables using \\[add-file-local-variable] or
\\[add-dir-local-variable].
Do not use this variable in Emacs Lisp programs. Instead, call
the `elpy-project-root' function. It will do the right thing."
:type 'directory
:safe 'file-directory-p
:group 'elpy)
(make-variable-buffer-local 'elpy-project-root)
(defcustom elpy-project-root-finder-functions
'(elpy-project-find-projectile-root
elpy-project-find-python-root
elpy-project-find-git-root
elpy-project-find-hg-root
elpy-project-find-svn-root)
"List of functions to ask for the current project root.
These will be checked in turn. The first directory found is used."
:type '(set (const :tag "Projectile project root"
elpy-project-find-projectile-root)
(const :tag "Python project (setup.py, setup.cfg)"
elpy-project-find-python-root)
(const :tag "Git repository root (.git)"
elpy-project-find-git-root)
(const :tag "Mercurial project root (.hg)"
elpy-project-find-hg-root)
(const :tag "Subversion project root (.svn)"
elpy-project-find-svn-root)
(const :tag "Django project root (manage.py, django-admin.py)"
elpy-project-find-django-root))
:group 'elpy)
(make-obsolete-variable 'elpy-company-hide-modeline
'elpy-remove-modeline-lighter
"1.10.0")
(defcustom elpy-remove-modeline-lighter t
"Non-nil if Elpy should remove most mode line display.
Modeline shows many minor modes currently active. For Elpy, this is mostly
uninteresting information, but if you rely on your modeline in other modes,
you might want to keep it."
:type 'boolean
:group 'elpy)
(defcustom elpy-company-post-completion-function 'ignore
"Your preferred Company post completion function.
Elpy can automatically insert parentheses after completing
callable objects.
The heuristic on when to insert these parentheses can easily be
wrong, though, so this is disabled by default. Set this variable
to the function `elpy-company-post-complete-parens' to enable
this feature."
:type '(choice (const :tag "Ignore post complete" ignore)
(const :tag "Complete callables with parens"
elpy-company-post-complete-parens)
(function :tag "Other function"))
:group 'elpy)
(defcustom elpy-get-info-from-shell nil
"If t, use the shell to gather docstrings and completions.
Normally elpy provides completion and documentation using static code analysis (from jedi). With this option set to t, elpy will add the completion candidates and the docstrings from the associated python shell; This activates fallback completion candidates for cases when static code analysis fails."
:type 'boolean
:group 'elpy)
(defcustom elpy-get-info-from-shell-timeout 1
"Timeout (in seconds) for gathering information from the shell."
:type 'number
:group 'elpy)
(defcustom elpy-eldoc-show-current-function t
"If true, show the current function if no calltip is available.
When Elpy can not find the calltip of the function call at point,
it can show the name of the function or class and method being
edited instead. Setting this variable to nil disables this feature."
:type 'boolean
:group 'elpy)
(defcustom elpy-test-runner 'elpy-test-discover-runner
"The test runner to use to run tests."
:type '(choice (const :tag "Unittest Discover" elpy-test-discover-runner)
(const :tag "Green" elpy-test-green-runner)
(const :tag "Django Discover" elpy-test-django-runner)
(const :tag "Nose" elpy-test-nose-runner)
(const :tag "py.test" elpy-test-pytest-runner)
(const :tag "Twisted Trial" elpy-test-trial-runner))
:safe 'elpy-test-runner-p
:group 'elpy)
(defcustom elpy-test-discover-runner-command '("python-shell-interpreter" "-m" "unittest")
"The command to use for `elpy-test-discover-runner'.
If the string \"python-shell-interpreter\" is present, it will be replaced with
the value of `python-shell-interpreter'."
:type '(repeat string)
:group 'elpy)
(defcustom elpy-test-green-runner-command '("green")
"The command to use for `elpy-test-green-runner'."
:type '(repeat string)
:group 'elpy)
(defcustom elpy-test-nose-runner-command '("nosetests")
"The command to use for `elpy-test-nose-runner'."
:type '(repeat string)
:group 'elpy)
(defcustom elpy-test-trial-runner-command '("trial")
"The command to use for `elpy-test-trial-runner'."
:type '(repeat string)
:group 'elpy)
(defcustom elpy-test-pytest-runner-command '("py.test")
"The command to use for `elpy-test-pytest-runner'."
:type '(repeat string)
:group 'elpy)
(defcustom elpy-test-compilation-function 'compile
"Function used by `elpy-test-run' to run a test command.
The function should behave similarly to `compile'. Another good
option is `pdb'."
:type 'string
:group 'elpy)
(defcustom elpy-rgrep-file-pattern "*.py"
"FILES to use for `elpy-rgrep-symbol'."
:type 'string
:group 'elpy)
(defcustom elpy-disable-backend-error-display t
"Non-nil if Elpy should disable backend error display."
:type 'boolean
:group 'elpy)
(defcustom elpy-formatter nil
"Auto formatter used by `elpy-format-code'.
if nil, use the first formatter found amongst
`yapf' , `autopep8' and `black'."
:type '(choice (const :tag "First one found" nil)
(const :tag "Yapf" yapf)
(const :tag "autopep8" autopep8)
(const :tag "Black" black))
:group 'elpy)
(defcustom elpy-syntax-check-command "flake8"
"The command to use for `elpy-check'."
:type 'string
:group 'elpy)
;;;;;;;;;;;;;
;;; Elpy Mode
(defvar elpy-refactor-map
(let ((map (make-sparse-keymap "Refactor")))
(define-key map (kbd "i") (cons (format "%snline"
(propertize "i" 'face 'bold))
'elpy-refactor-inline))
(define-key map (kbd "f") (cons (format "%sunction extraction"
(propertize "f" 'face 'bold))
'elpy-refactor-extract-function))
(define-key map (kbd "v") (cons (format "%sariable extraction"
(propertize "v" 'face 'bold))
'elpy-refactor-extract-variable))
(define-key map (kbd "r") (cons (format "%sename"
(propertize "r" 'face 'bold))
'elpy-refactor-rename))
map)
"Key map for the refactor command.")
(defvar elpy-mode-map
(let ((map (make-sparse-keymap)))
;; Alphabetical order to make it easier to find free C-c C-X
;; bindings in the future. Heh.
;; (define-key map (kbd "<backspace>") 'python-indent-dedent-line-backspace)
;; (define-key map (kbd "<backtab>") 'python-indent-dedent-line)
;; (define-key map (kbd "C-M-x") 'python-shell-send-defun)
;; (define-key map (kbd "C-c <") 'python-indent-shift-left)
;; (define-key map (kbd "C-c >") 'python-indent-shift-right)
(define-key map (kbd "C-c RET") 'elpy-importmagic-add-import)
(define-key map (kbd "C-c C-b") 'elpy-nav-expand-to-indentation)
(define-key map (kbd "C-c C-c") 'elpy-shell-send-region-or-buffer)
(define-key map (kbd "C-c C-d") 'elpy-doc)
(define-key map (kbd "C-c C-e") 'elpy-multiedit-python-symbol-at-point)
(define-key map (kbd "C-c C-f") 'elpy-find-file)
(define-key map (kbd "C-c C-n") 'elpy-flymake-next-error)
(define-key map (kbd "C-c C-o") 'elpy-occur-definitions)
(define-key map (kbd "C-c C-p") 'elpy-flymake-previous-error)
(define-key map (kbd "C-c @ C-c") 'elpy-folding-toggle-at-point)
(define-key map (kbd "C-c @ C-b") 'elpy-folding-toggle-docstrings)
(define-key map (kbd "C-c @ C-m") 'elpy-folding-toggle-comments)
(define-key map (kbd "C-c @ C-f") 'elpy-folding-hide-leafs)
(define-key map (kbd "C-c C-s") 'elpy-rgrep-symbol)
(define-key map (kbd "C-c C-t") 'elpy-test)
(define-key map (kbd "C-c C-v") 'elpy-check)
(define-key map (kbd "C-c C-z") 'elpy-shell-switch-to-shell)
(define-key map (kbd "C-c C-k") 'elpy-shell-kill)
(define-key map (kbd "C-c C-K") 'elpy-shell-kill-all)
(define-key map (kbd "C-c C-r") elpy-refactor-map)
(define-key map (kbd "C-c C-x") elpy-django-mode-map)
(define-key map (kbd "<S-return>") 'elpy-open-and-indent-line-below)
(define-key map (kbd "<C-S-return>") 'elpy-open-and-indent-line-above)
(define-key map (kbd "<C-return>") 'elpy-shell-send-statement-and-step)
(define-key map (kbd "<C-down>") 'elpy-nav-forward-block)
(define-key map (kbd "<C-up>") 'elpy-nav-backward-block)
(define-key map (kbd "<C-left>") 'elpy-nav-backward-indent)
(define-key map (kbd "<C-right>") 'elpy-nav-forward-indent)
(define-key map (kbd "<M-down>") 'elpy-nav-move-line-or-region-down)
(define-key map (kbd "<M-up>") 'elpy-nav-move-line-or-region-up)
(define-key map (kbd "<M-left>") 'elpy-nav-indent-shift-left)
(define-key map (kbd "<M-right>") 'elpy-nav-indent-shift-right)
(unless (fboundp 'xref-find-definitions)
(define-key map (kbd "M-.") 'elpy-goto-definition))
(if (not (fboundp 'xref-find-definitions-other-window))
(define-key map (kbd "C-x 4 M-.") 'elpy-goto-definition-other-window)
(define-key map (kbd "C-x 4 M-.") 'xref-find-definitions-other-window))
(when (fboundp 'xref-pop-marker-stack)
(define-key map (kbd "M-*") 'xref-pop-marker-stack))
(define-key map (kbd "M-TAB") 'elpy-company-backend)
map)
"Key map for the Emacs Lisp Python Environment.")
(defvar elpy-shell-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "e") 'elpy-shell-send-statement)
(define-key map (kbd "E") 'elpy-shell-send-statement-and-go)
(define-key map (kbd "s") 'elpy-shell-send-top-statement)
(define-key map (kbd "S") 'elpy-shell-send-top-statement-and-go)
(define-key map (kbd "f") 'elpy-shell-send-defun)
(define-key map (kbd "F") 'elpy-shell-send-defun-and-go)
(define-key map (kbd "c") 'elpy-shell-send-defclass)
(define-key map (kbd "C") 'elpy-shell-send-defclass-and-go)
(define-key map (kbd "o") 'elpy-shell-send-group)
(define-key map (kbd "O") 'elpy-shell-send-group-and-go)
(define-key map (kbd "w") 'elpy-shell-send-codecell)
(define-key map (kbd "W") 'elpy-shell-send-codecell-and-go)
(define-key map (kbd "r") 'elpy-shell-send-region-or-buffer)
(define-key map (kbd "R") 'elpy-shell-send-region-or-buffer-and-go)
(define-key map (kbd "b") 'elpy-shell-send-buffer)
(define-key map (kbd "B") 'elpy-shell-send-buffer-and-go)
(define-key map (kbd "C-e") 'elpy-shell-send-statement-and-step)
(define-key map (kbd "C-S-E") 'elpy-shell-send-statement-and-step-and-go)
(define-key map (kbd "C-s") 'elpy-shell-send-top-statement-and-step)
(define-key map (kbd "C-S-S") 'elpy-shell-send-top-statement-and-step-and-go)
(define-key map (kbd "C-f") 'elpy-shell-send-defun-and-step)
(define-key map (kbd "C-S-F") 'elpy-shell-send-defun-and-step-and-go)
(define-key map (kbd "C-c") 'elpy-shell-send-defclass-and-step)
(define-key map (kbd "C-S-C") 'elpy-shell-send-defclass-and-step-and-go)
(define-key map (kbd "C-o") 'elpy-shell-send-group-and-step)
(define-key map (kbd "C-S-O") 'elpy-shell-send-group-and-step-and-go)
(define-key map (kbd "C-w") 'elpy-shell-send-codecell-and-step)
(define-key map (kbd "C-S-W") 'elpy-shell-send-codecell-and-step-and-go)
(define-key map (kbd "C-r") 'elpy-shell-send-region-or-buffer-and-step)
(define-key map (kbd "C-S-R") 'elpy-shell-send-region-or-buffer-and-step-and-go)
(define-key map (kbd "C-b") 'elpy-shell-send-buffer-and-step)
(define-key map (kbd "C-S-B") 'elpy-shell-send-buffer-and-step-and-go)
map)
"Key map for the shell related commands.")
(fset 'elpy-shell-map elpy-shell-map)
(defcustom elpy-shell-command-prefix-key "C-c C-y"
"Prefix key used to call elpy shell related commands.
This option need to bet set through `customize' or `customize-set-variable' to be taken into account."
:type 'string
:group 'elpy
:set
(lambda (var key)
(when (and (boundp var) (symbol-value var))
(define-key elpy-mode-map (kbd (symbol-value var)) nil))
(when key
(define-key elpy-mode-map (kbd key) 'elpy-shell-map)
(set var key))))
(defvar elpy-pdb-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "d") 'elpy-pdb-debug-buffer)
(define-key map (kbd "p") 'elpy-pdb-break-at-point)
(define-key map (kbd "e") 'elpy-pdb-debug-last-exception)
(define-key map (kbd "b") 'elpy-pdb-toggle-breakpoint-at-point)
map)
"Key map for the shell related commands.")
(fset 'elpy-pdb-map elpy-pdb-map)
(define-key elpy-mode-map (kbd "C-c C-u") 'elpy-pdb-map)
(easy-menu-define elpy-menu elpy-mode-map
"Elpy Mode Menu"
'("Elpy"
["Documentation" elpy-doc
:help "Get documentation for symbol at point"]
["Run Tests" elpy-test
:help "Run test at point, or all tests in the project"]
["Go to Definition" elpy-goto-definition
:help "Go to the definition of the symbol at point"]
["Go to previous definition" pop-tag-mark
:active (if (version< emacs-version "25.1")
(not (ring-empty-p find-tag-marker-ring))
(> xref-marker-ring-length 0))
:help "Return to the position"]
["Complete" elpy-company-backend
:keys "M-TAB"
:help "Complete at point"]
["Refactor" elpy-refactor
:help "Refactor options"]
"---"
("Interactive Python"
["Switch to Python Shell" elpy-shell-switch-to-shell
:help "Start and switch to the interactive Python"]
["Send Region or Buffer" elpy-shell-send-region-or-buffer
:label (if (use-region-p)
"Send Region to Python"
"Send Buffer to Python")
:help "Send the current region or the whole buffer to Python"]
["Send Definition" elpy-shell-send-defun
:help "Send current definition to Python"]
["Kill Python shell" elpy-shell-kill
:help "Kill the current Python shell"]
["Kill all Python shells" elpy-shell-kill-all
:help "Kill all Python shells"])
("Debugging"
["Debug buffer" elpy-pdb-debug-buffer
:help "Debug the current buffer using pdb"]
["Debug at point" elpy-pdb-break-at-point
:help "Debug the current buffer and stop at the current position"]
["Debug last exception" elpy-pdb-debug-last-exception
:help "Run post-mortem pdb on the last exception"]
["Add/remove breakpoint" elpy-pdb-toggle-breakpoint-at-point
:help "Add or remove a breakpoint at the current position"])
("Project"
["Find File" elpy-find-file
:help "Interactively find a file in the current project"]
["Find Symbol" elpy-rgrep-symbol
:help "Find occurrences of a symbol in the current project"]
["Set Project Root" elpy-set-project-root
:help "Change the current project root"]
["Set Project Variable" elpy-set-project-variable
:help "Configure a project-specific option"])
("Syntax Check"
["Check Syntax" elpy-check
:help "Check the syntax of the current file"]
["Next Error" elpy-flymake-next-error
:help "Go to the next inline error, if any"]
["Previous Error" elpy-flymake-previous-error
:help "Go to the previous inline error, if any"])
("Code folding"
["Hide/show at point" elpy-folding-toggle-at-point
:help "Hide or show the block or docstring at point"]
["Hide/show all docstrings" elpy-folding-toggle-docstrings
:help "Hide or show all the docstrings"]
["Hide/show all comments" elpy-folding-toggle-comments
:help "Hide or show all the comments"]
["Hide leafs" elpy-folding-hide-leafs
:help "Hide all leaf blocks (blocks not containing other blocks)"])
("Indentation Blocks"
["Dedent" python-indent-shift-left
:help "Dedent current block or region"
:suffix (if (use-region-p) "Region" "Block")]
["Indent" python-indent-shift-right
:help "Indent current block or region"
:suffix (if (use-region-p) "Region" "Block")]
["Up" elpy-nav-move-line-or-region-up
:help "Move current block or region up"
:suffix (if (use-region-p) "Region" "Block")]
["Down" elpy-nav-move-line-or-region-down
:help "Move current block or region down"
:suffix (if (use-region-p) "Region" "Block")])
"---"
["Configure" elpy-config t]))
(defvar elpy-enabled-p nil
"Is Elpy enabled or not.")
;;;###autoload
(defun elpy-enable (&optional _ignored)
"Enable Elpy in all future Python buffers."
(interactive)
(unless elpy-enabled-p
(when (< emacs-major-version 24)
(error "Elpy requires Emacs 24 or newer"))
(when _ignored
(warn "The argument to `elpy-enable' is deprecated, customize `elpy-modules' instead"))
(let ((filename (find-lisp-object-file-name 'python-mode
'symbol-function)))
(when (and filename
(string-match "/python-mode\\.el\\'"
filename))
(error (concat "You are using python-mode.el. "
"Elpy only works with python.el from "
"Emacs 24 and above"))))
(elpy-modules-global-init)
(define-key inferior-python-mode-map (kbd "C-c C-z") 'elpy-shell-switch-to-buffer)
(add-hook 'python-mode-hook 'elpy-mode)
(add-hook 'pyvenv-post-activate-hooks 'elpy-rpc--disconnect)
(add-hook 'pyvenv-post-deactivate-hooks 'elpy-rpc--disconnect)
(add-hook 'inferior-python-mode-hook 'elpy-shell--enable-output-filter)
(add-hook 'python-shell-first-prompt-hook 'elpy-shell--send-setup-code t)
;; Add codecell boundaries highligting
(font-lock-add-keywords
'python-mode
`((,(replace-regexp-in-string "\\\\" "\\\\"
elpy-shell-cell-boundary-regexp)
0 'elpy-codecell-boundary prepend)))
;; Enable Elpy-mode in the opened python buffer
(setq elpy-enabled-p t)
(dolist (buffer (buffer-list))
(and (not (string-match "^ ?\\*" (buffer-name buffer)))
(with-current-buffer buffer
(when (string= major-mode 'python-mode)
(python-mode) ;; update codecell fontification
(elpy-mode t)))))
))
(defun elpy-disable ()
"Disable Elpy in all future Python buffers."
(interactive)
(elpy-modules-global-stop)
(define-key inferior-python-mode-map (kbd "C-c C-z") nil)
(remove-hook 'python-mode-hook 'elpy-mode)
(remove-hook 'pyvenv-post-activate-hooks 'elpy-rpc--disconnect)
(remove-hook 'pyvenv-post-deactivate-hooks 'elpy-rpc--disconnect)
(remove-hook 'inferior-python-mode-hook 'elpy-shell--enable-output-filter)
(remove-hook 'python-shell-first-prompt-hook 'elpy-shell--send-setup-code)
;; Remove codecell boundaries highligting
(font-lock-remove-keywords
'python-mode
`((,(replace-regexp-in-string "\\\\" "\\\\"
elpy-shell-cell-boundary-regexp)
0 'elpy-codecell-boundary prepend)))
(setq elpy-enabled-p nil))
;;;###autoload
(define-minor-mode elpy-mode
"Minor mode in Python buffers for the Emacs Lisp Python Environment.
This mode fully supports virtualenvs. Once you switch a
virtualenv using \\[pyvenv-workon], you can use
\\[elpy-rpc-restart] to make the elpy Python process use your
virtualenv.
\\{elpy-mode-map}"
:lighter " Elpy"
(unless (derived-mode-p 'python-mode 'python-ts-mode)
(error "Elpy only works with `python-mode'"))
(unless elpy-enabled-p
(error "Please enable Elpy with `(elpy-enable)` before using it"))
(when (boundp 'xref-backend-functions)
(add-hook 'xref-backend-functions #'elpy--xref-backend nil t))
;; Set this for `elpy-check' command
(setq-local python-check-command elpy-syntax-check-command)
(cond
(elpy-mode
(elpy-modules-buffer-init))
((not elpy-mode)
(elpy-modules-buffer-stop))))
;;;;;;;;;;;;;;;
;;; Elpy Config
(defvar elpy-config--related-custom-groups
'(("Elpy" elpy "elpy-")
("Python" python "python-")
("Virtual Environments (Pyvenv)" pyvenv "pyvenv-")
("Completion (Company)" company "company-")
("Call Signatures (ElDoc)" eldoc "eldoc-")
("Inline Errors (Flymake)" flymake "flymake-")
("Code folding (hideshow)" hideshow "hs-")
("Snippets (YASnippet)" yasnippet "yas-")
("Directory Grep (rgrep)" grep "grep-")
("Search as You Type (ido)" ido "ido-")
("Django extension" elpy-django "elpy-django-")
("Autodoc extension" elpy-autodoc "elpy-autodoc-")
;; ffip does not use defcustom
;; highlight-indent does not use defcustom, either. Its sole face
;; is defined in basic-faces.
))
(defvar elpy-config--get-config "import json
import sys
import warnings
warnings.filterwarnings('ignore', category=FutureWarning)
warnings.filterwarnings('ignore', category=DeprecationWarning)
warnings.filterwarnings('ignore', category=PendingDeprecationWarning)
from distutils.version import LooseVersion
try:
import urllib2 as urllib
except ImportError:
import urllib.request as urllib
# Check if we can connect to pypi quickly enough
try:
response = urllib.urlopen('https://pypi.org/pypi', timeout=1)
CAN_CONNECT_TO_PYPI = True
except:
CAN_CONNECT_TO_PYPI = False
def latest(package, version=None):
if not CAN_CONNECT_TO_PYPI:
return None
try:
response = urllib.urlopen('https://pypi.org/pypi/{package}/json'.format(package=package),
timeout=2).read()
latest = json.loads(response)['info']['version']
if version is None or LooseVersion(version) < LooseVersion(latest):
return latest
else:
return None
except:
return None
config = {}
config['can_connect_to_pypi'] = CAN_CONNECT_TO_PYPI
config['rpc_python_version'] = ('{major}.{minor}.{micro}'
.format(major=sys.version_info[0],
minor=sys.version_info[1],
micro=sys.version_info[2]))
try:
import elpy
config['elpy_version'] = elpy.__version__
except:
config['elpy_version'] = None
try:
import jedi
if isinstance(jedi.__version__, tuple):
config['jedi_version'] = '.'.join(str(x) for x in jedi.__version__)
else:
config['jedi_version'] = jedi.__version__
config['jedi_latest'] = latest('jedi', config['jedi_version'])
except:
config['jedi_version'] = None
config['jedi_latest'] = latest('jedi')
try:
import autopep8
config['autopep8_version'] = autopep8.__version__
config['autopep8_latest'] = latest('autopep8', config['autopep8_version'])
except:
config['autopep8_version'] = None
config['autopep8_latest'] = latest('autopep8')
try:
import yapf
config['yapf_version'] = yapf.__version__
config['yapf_latest'] = latest('yapf', config['yapf_version'])
except:
config['yapf_version'] = None
config['yapf_latest'] = latest('yapf')
try:
import black
config['black_version'] = black.__version__
config['black_latest'] = latest('black', config['black_version'])
except:
config['black_version'] = None
config['black_latest'] = latest('black')
json.dump(config, sys.stdout)
")
(defun elpy-config-error (&optional fmt &rest args)
"Note a configuration problem.
FMT is the formating string.
This will show a message in the minibuffer that tells the user to
use \\[elpy-config]."
(let ((msg (if fmt
(apply #'format fmt args)
"Elpy is not properly configured")))
(error "%s; use M-x elpy-config to configure it" msg)))
;;;###autoload
(defun elpy-config ()
"Configure Elpy.
This function will pop up a configuration buffer, which is mostly
a customize buffer, but has some more options."
(interactive)
(let ((buf (custom-get-fresh-buffer "*Elpy Config*"))
(config (elpy-config--get-config))
(custom-search-field nil))
(with-current-buffer buf
(elpy-insert--header "Elpy Configuration")
(elpy-config--insert-configuration-table config)
(insert "\n")
(elpy-insert--header "Warnings")
(elpy-config--insert-configuration-problems config)
(elpy-insert--header "Options")
(let ((custom-buffer-style 'tree))
(Custom-mode)
(elpy-config--insert-help)
(dolist (cust elpy-config--related-custom-groups)
(widget-create 'custom-group
:custom-last t
:custom-state 'hidden
:tag (car cust)
:value (cadr cust)))
(widget-setup)
(goto-char (point-min))))
(pop-to-buffer-same-window buf)))
;;;###autoload
(defun elpy-version ()
"Display the version of Elpy."
(interactive)
(message "Elpy %s (use M-x elpy-config for details)" elpy-version))
(defun elpy-config--insert-help ()
"Insert the customization help."
(let ((start (point)))
;; Help display from `customize-browse'
(widget-insert (format "\
%s buttons; type RET or click mouse-1
on a button to invoke its action.
Invoke [+] to expand a group, and [-] to collapse an expanded group.\n"
(if custom-raised-buttons
"`Raised' text indicates"
"Square brackets indicate")))
(if custom-browse-only-groups
(widget-insert "\
Invoke the [Group] button below to edit that item in another window.\n\n")
(widget-insert "Invoke the ")
(widget-create 'item
:format "%t"
:tag "[Group]"
:tag-glyph "folder")
(widget-insert ", ")
(widget-create 'item
:format "%t"
:tag "[Face]"
:tag-glyph "face")
(widget-insert ", and ")
(widget-create 'item
:format "%t"
:tag "[Option]"
:tag-glyph "option")
(widget-insert " buttons below to edit that
item in another window.\n\n")
(fill-region start (point)))))
(defun elpy-config--insert-configuration-problems (&optional config)
"Insert help text and widgets for configuration problems."
(unless config
(setq config (elpy-config--get-config)))
(let* ((rpc-python-version (gethash "rpc_python_version" config)))
;; Python not found
(unless (gethash "rpc_python_executable" config)
(elpy-insert--para
"Elpy can not find the configured Python interpreter. Please make "
"sure that the variable `elpy-rpc-python-command' points to a "
"command in your PATH. You can change the variable below.\n\n"))
;; No virtual env
(when (and (gethash "rpc_python_executable" config)
(not (gethash "virtual_env" config)))
(elpy-insert--para
"You have not activated a virtual env. It is not mandatory but"
" often a good idea to work inside a virtual env. You can use "
"`M-x pyvenv-activate` or `M-x pyvenv-workon` to activate one.\n\n"))
;; No virtual env, but ~/.local/bin not in PATH
(when (and (not (memq system-type '(ms-dos windows-nt)))
(gethash "rpc_python_executable" config)
(not pyvenv-virtual-env)
(not (or (member (expand-file-name "~/.local/bin")
exec-path)
(member (expand-file-name "~/.local/bin/")
exec-path))))
(elpy-insert--para
"The directory ~/.local/bin/ is not in your PATH. As there is "
"no active virtualenv, installing Python packages locally will "
"place executables in that directory, so Emacs won't find them. "
"If you are missing some commands, do add this directory to your "
"PATH -- and then do `elpy-rpc-restart'.\n\n"))
;; Python found, but can't find the elpy module
(when (and (gethash "rpc_python_executable" config)
(not (gethash "elpy_version" config)))
(elpy-insert--para
"The Python interpreter could not find the elpy module. "
"Please report to: "
"https://github.com/jorgenschaefer/elpy/issues/new."
"\n")
(insert "\n"))
;; Bad backend version
(when (and (gethash "elpy_version" config)
(not (equal (gethash "elpy_version" config)
elpy-version)))
(let ((elpy-python-version (gethash "elpy_version" config)))
(elpy-insert--para
"The Elpy backend is version " elpy-python-version " while "
"the Emacs package is " elpy-version ". This is incompatible. "
"Please report to: https://github.com/jorgenschaefer/elpy/issues/new."
"\n")))
;; Otherwise unparseable output.
(when (gethash "error_output" config)
(elpy-insert--para
"There was an unexpected problem starting the RPC process. Please "
"check the following output to see if this makes sense to you. "
"To me, it doesn't.\n")
(insert "\n"
(gethash "error_output" config) "\n"
"\n"))
;; Interactive python interpreter not in the current virtual env
(when (and pyvenv-virtual-env
(not (string-prefix-p (expand-file-name pyvenv-virtual-env)
(executable-find
python-shell-interpreter))))
(elpy-insert--para
"The python interactive interpreter (" python-shell-interpreter
") is not installed on the current virtualenv ("
pyvenv-virtual-env "). The system binary ("
(executable-find python-shell-interpreter)
") will be used instead."
"\n")
(insert "\n")
(widget-create 'elpy-insert--pip-button
:package python-shell-interpreter :norpc t)
(insert "\n\n"))
;; Couldn't connect to pypi to check package versions
(when (not (gethash "can_connect_to_pypi" config))
(elpy-insert--para
"Elpy could not connect to Pypi (or at least not quickly enough) "
"and check if the python packages were up-to-date. "
"You can still try to update all of them:"
"\n")
(insert "\n")
(widget-create 'elpy-insert--generic-button
:button-name "[Update python packages]"
:function (lambda () (with-elpy-rpc-virtualenv-activated
(elpy-rpc--install-dependencies))))
(insert "\n\n"))
;; Pip not available in the rpc virtualenv
(when (and
(equal elpy-rpc-virtualenv-path 'default)
(elpy-rpc--pip-missing))
(elpy-insert--para
"Pip doesn't seem to be installed in the dedicated virtualenv "
"created by Elpy (" (elpy-rpc-get-virtualenv-path) "). "
"This may prevent some features from working properly"
" (completion, documentation, reformatting, ...). "
"You can try reinstalling the virtualenv. "
"If the problem persists, please report on Elpy's github page."
"\n\n")
(widget-create 'elpy-insert--generic-button
:button-name "[Reinstall RPC virtualenv]"
:function (lambda () (elpy-rpc-reinstall-virtualenv)))
(insert "\n\n"))
;; Requested backend unavailable
(when (and (gethash "rpc_python_executable" config)
(not (gethash "jedi_version" config)))
(elpy-insert--para
"The Jedi package is not currently installed. "
"This package is needed for code completion, code navigation "
"and access to documentation.\n")
(insert "\n")
(widget-create 'elpy-insert--pip-button
:package "jedi")
(insert "\n\n"))
;; Newer version of Jedi available
(when (and (gethash "jedi_version" config)
(gethash "jedi_latest" config))
(elpy-insert--para
"There is a newer version of Jedi available.\n")
(insert "\n")
(widget-create 'elpy-insert--pip-button
:package "jedi" :upgrade t)
(insert "\n\n"))
;; No auto formatting tool available
(unless (or
(gethash "autopep8_version" config)
(gethash "yapf_version" config)
(gethash "black_version" config))
(elpy-insert--para
"No autoformatting package is currently installed. "
"At least one is needed (Autopep8, Yapf or Black) "
"to perform autoformatting (`C-c C-r f` in a python buffer).\n")
(insert "\n")
(widget-create 'elpy-insert--pip-button
:package "autopep8")
(insert "\n")
(widget-create 'elpy-insert--pip-button
:package "yapf")
(insert "\n")
(widget-create 'elpy-insert--pip-button
:package "black")
(insert "\n\n"))
;; Newer version of autopep8 available
(when (and (gethash "autopep8_version" config)
(gethash "autopep8_latest" config))
(elpy-insert--para
"There is a newer version of the autopep8 package available.\n")
(insert "\n")
(widget-create 'elpy-insert--pip-button
:package "autopep8" :upgrade t)
(insert "\n\n"))
;; Newer version of yapf available
(when (and (gethash "yapf_version" config)
(gethash "yapf_latest" config))
(elpy-insert--para
"There is a newer version of the yapf package available.\n")
(insert "\n")
(widget-create 'elpy-insert--pip-button
:package "yapf" :upgrade t)
(insert "\n\n"))
;; Newer version of black available
(when (and (gethash "black_version" config)
(gethash "black_latest" config))
(elpy-insert--para
"There is a newer version of the black package available.\n")
(insert "\n")
(widget-create 'elpy-insert--pip-button
:package "black" :upgrade t)
(insert "\n\n"))
;; Syntax checker not available