forked from russell/python-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python-mode.el
13726 lines (11891 loc) · 548 KB
/
python-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
;;; python-mode.el --- Towards an Python-IDE in Emacs
;; Maintainer: Andreas Roehler <[email protected]>
;; Keywords: languages, processes, python, oop
;; Copyright (C) 1992,1993,1994 Tim Peters
;; Author: 2003-2011 https://launchpad.net/python-mode
;; 1995-2002 Barry A. Warsaw
;; 1992-1994 Tim Peters
;; Maintainer: [email protected]
;; Created: Feb 1992
;; Keywords: python languages oop
;; 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:
;;; Code
(require 'comint)
(require 'custom)
(eval-when-compile (require 'cl))
(require 'compile)
(require 'ansi-color)
(require 'cc-cmds)
(require 'shell)
(defgroup python-mode nil
"Support for the Python programming language, <http://www.python.org/>"
:group 'languages
:prefix "py-")
(defconst py-version "6.0.6")
;;; User definable variables
(defcustom py-indent-offset 4
"*Amount of offset per level of indentation.
`\\[py-guess-indent-offset]' can usually guess a good value when
you're editing someone else's Python code."
:type 'integer
:group 'python-mode)
(make-variable-buffer-local 'py-indent-offset)
(defcustom pdb-path '/usr/lib/python2.7/pdb.py
"Where to find pdb.py. Edit this according to your system.
If you ignore the location `M-x py-guess-pdb-path' might display it.
"
:type 'variable
:group 'python-mode)
(defun py-guess-pdb-path ()
"If py-pdb-path isn't set, find location of pdb.py. "
(interactive)
(let ((ele (split-string (shell-command-to-string "whereis python")))
erg)
(while (or (not erg)(string= "" erg))
(when (and (string-match "^/" (car ele)) (not (string-match "/man" (car ele))))
(setq erg (shell-command-to-string (concat "find " (car ele) " -type f -name \"pdb.py\""))))
(setq ele (cdr ele)))
(if erg
(when (interactive-p) (message "%s" erg))
(when (interactive-p) (message "%s" "pdb.py not found, please customize `pdb-path'")))
(concat "'" erg)))
(defcustom py-install-directory ""
"Directory where python-mode.el and it's subdirectories should be installed. Needed for completion and other environment stuff only. "
:type 'string
:group 'python-mode)
(defcustom py-guess-py-install-directory-p t
"If in cases, `py-install-directory' isn't set, `py-set-load-path'should guess it from `buffer-file-name'. "
:type 'boolean
:group 'python-mode)
(defcustom py-load-pymacs-p nil
"If Pymacs as delivered with python-mode.el shall be loaded.
Default is nil.
Pymacs has been written by François Pinard and many others.
See original source: http://pymacs.progiciels-bpi.ca"
:type 'boolean
:group 'python-mode)
(defcustom py-verbose-p nil
"If indenting functions should report reached indent level.
Default is nil. "
:type 'boolean
:group 'python-mode)
(defcustom py-report-position-p nil
"If functions moving point like `py-forward-into-nomenclature' should report reached position.
Default is nil. "
:type 'boolean
:group 'python-mode)
(defcustom py-extensions "py-extensions.el"
"File where extensions to python-mode.el should be installed. Used by virtualenv support. "
:type 'string
:group 'python-mode)
(defcustom py-hide-show-minor-mode-p nil
"If hide-show minor-mode should be on, default is nil. "
:type 'boolean
:group 'python-mode)
(defcustom py-outline-minor-mode-p t
"If outline minor-mode should be on, default is `t'. "
:type 'boolean
:group 'python-mode)
(defcustom py-start-run-py-shell t
"If `python-mode' should start a python-shell, `py-shell'. Default is `t'.
A running python-shell presently is needed by complete-functions. "
:type 'boolean
:group 'python-mode)
(defcustom py-use-number-face-p nil
"If digits incl. hex-digits should get an own py-number-face.
Default is nil. With large files fontifying numbers may cause a
delay. Setting of `py-use-number-face-p' has visible effect only
when `py-number-face' was customized differently than inherited
default face. "
:type 'boolean
:group 'python-mode)
;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
(defcustom py-close-provides-newline t
"If a newline is inserted, when line after block isn't empty. Default is non-nil. "
:type 'boolean
:group 'python-mode)
(make-variable-buffer-local 'py-close-provides-newline)
(defcustom py-dedent-keep-relative-column t
"If point should follow dedent or kind of electric move to end of line. Default is t - keep relative position. "
:type 'boolean
:group 'python-mode)
(defcustom py-indent-honors-multiline-listing nil
"If `t', indents to 1+ column of opening delimiter. If `nil', indent adds one level to the beginning of statement. Default is `nil'. "
:type 'boolean
:group 'python-mode)
(defcustom py-indent-honors-inline-comment nil
"If non-nil, indents to column of inlined comment start.
Default is nil. "
:type 'boolean
:group 'python-mode)
(defcustom py-closing-list-dedents-bos nil
"If non-nil, closing parentesis dedents onto column of statement, otherwise keeps additional `py-indent-offset', default is nil "
:type 'boolean
:group 'python-mode)
(defcustom py-electric-colon-active-p nil
"`py-electric-colon' feature. Default is `nil'. See lp:837065 for discussions. "
:type 'boolean
:group 'python-mode)
(defcustom py-electric-comment-p t
"If \"#\" should call `py-electric-comment'. Default is `t'. "
:type 'boolean
:group 'python-mode)
(defcustom py-electric-comment-add-space-p nil
"If py-electric-comment should add a space. Default is `nil'. "
:type 'boolean
:group 'python-mode)
(defcustom py-mark-decorators nil
"If py-mark-def-or-class functions should mark decorators too. Default is `nil'. "
:type 'boolean
:group 'python-mode)
(defcustom py-tab-indent t
"*Non-nil means TAB in Python mode calls `py-indent-line'."
:type 'boolean
:group 'python-mode)
(defcustom py-complete-function 'py-shell-complete
"Function used for completion in buffers. "
:type '(choice (const :tag "py-completion-at-point" py-completion-at-point)
(const :tag "Pymacs based py-complete" py-complete)
(const :tag "py-shell-complete" py-shell-complete)
(const :tag "IPython's ipython-complete" ipython-complete))
:group 'python-mode)
(defcustom ipython-complete-function 'ipython-complete
"Function used for completion in IPython shell buffers.
Minor bug: `ipython-complete' raises the prompt counter when completion done
Richard Everson commented:
I don't know how to stop IPython from incrementing the prompt
counter, but using py-completion-at-point just hangs emacs for
me. If I start with a new IPython shell, then
In [1]: import sys
In [2]: sys.pa
then M-x py-completion-at-point, hoping to complete to sys.path, Emacs
hangs. Escaping out of it shows that the \*Python\* buffer has the
contents:
>>> Traceback (most recent call last):
File \"<stdin>\", line 1, in <module>
NameError: name 'nil' is not defined
>>> =
[ ... ]
On the other hand, IPython's interaction and completion itself is pretty
impressive (for versions greater than 0.10 at least): it inserts the
correct indentation for for, if, etc and it will show completions even
within a loop. Here's an example from a terminal shell:
In [1]:
In [1]: for i in range(3):
...: print i, sys.p<------------ Pressed tab here; indentation inser=
ted automatically
sys.path sys.path_importer_cache sys.prefix
sys.path_hooks sys.platform sys.py3kwarning
...: print i, sys.path<------------ Pressed tab again
sys.path sys.path_hooks sys.path_importer_cache
"
:type '(choice (const :tag "py-completion-at-point" py-completion-at-point)
(const :tag "py-shell-complete" py-shell-complete)
(const :tag "Pymacs based py-complete" py-complete)
(const :tag "IPython's ipython-complete" ipython-complete))
:group 'python-mode)
(make-variable-buffer-local 'ipython-complete-function)
(defcustom py-encoding-string " # -*- coding: utf-8 -*-"
"Default string specifying encoding of a Python file. "
:type 'string
:group 'python-mode)
(defvar py-encoding-string-re "^[ \t]*#[ \t]*-\\*-[ \t]*coding:.+-\\*-"
"Matches encoding string of a Python file. ")
(defvar symbol-definition-start-re)
(setq symbol-definition-start-re "^[ \t]*(\\(defun\\|defvar\\|defcustom\\)")
(defcustom py-shebang-startstring "#! /bin/env"
"Detecting the shell in head of file. "
:type 'string
:group 'python-mode)
(defvar py-shebang-regexp "#![ \t]?\\([^ \t\n]+\\)[ \t]?\\([iptj]+ython[^ \t\n]*\\)"
"Detecting the shell in head of file. ")
(defcustom py-python-command-args '("-i")
"*List of string arguments to be used when starting a Python shell."
:type '(repeat string)
:group 'python-mode)
(make-variable-buffer-local 'py-python-command-args)
(set-default 'py-python-command-args '("-i"))
(make-obsolete-variable 'py-jpython-command-args 'py-jython-command-args nil)
(defcustom py-jython-command-args '("-i")
"*List of string arguments to be used when starting a Jython shell."
:type '(repeat string)
:group 'python-mode
:tag "Jython Command Args")
(defcustom py-cleanup-temporary t
"If temporary buffers and files used by functions executing region should be deleted afterwards. "
:type 'boolean
:group 'python-mode)
(defcustom py-lhs-inbound-indent 1
"When line starts a multiline-assignment: How many colums indent should be more than opening bracket, brace or parenthesis. "
:type 'integer
:group 'python-mode)
(make-variable-buffer-local 'py-lhs-inbound-indent)
(defcustom py-rhs-inbound-indent 1
"When inside a multiline-assignment: How many colums indent should be more than opening bracket, brace or parenthesis. "
:type 'integer
:group 'python-mode)
(make-variable-buffer-local 'py-rhs-inbound-indent)
(defcustom py-continuation-offset 2
"*Additional amount of offset to give for some continuation lines.
Continuation lines are those that immediately follow a backslash
terminated line. "
:type 'integer
:group 'python-mode)
(defcustom py-smart-indentation t
"*Should `python-mode' try to automagically set some indentation variables?
When this variable is non-nil, two things happen when a buffer is set
to `python-mode':
1. `py-indent-offset' is guessed from existing code in the buffer.
Only guessed values between 2 and 8 are considered. If a valid
guess can't be made (perhaps because you are visiting a new
file), then the value in `py-indent-offset' is used.
2. `indent-tabs-mode' is turned off if `py-indent-offset' does not
equal `tab-width' (`indent-tabs-mode' is never turned on by
Python mode). This means that for newly written code, tabs are
only inserted in indentation if one tab is one indentation
level, otherwise only spaces are used.
Note that both these settings occur *after* `python-mode-hook' is run,
so if you want to defeat the automagic configuration, you must also
set `py-smart-indentation' to nil in your `python-mode-hook'."
:type 'boolean
:group 'python-mode)
(make-variable-buffer-local 'py-smart-indentation)
(defcustom py-align-multiline-strings-p t
"*Flag describing how multi-line triple quoted strings are aligned.
When this flag is non-nil, continuation lines are lined up under the
preceding line's indentation. When this flag is nil, continuation
lines are aligned to column zero."
:type '(choice (const :tag "Align under preceding line" t)
(const :tag "Align to column zero" nil))
:group 'python-mode)
(defcustom py-block-comment-prefix "##"
"*String used by \\[comment-region] to comment out a block of code.
This should follow the convention for non-indenting comment lines so
that the indentation commands won't get confused (i.e., the string
should be of the form `#x...' where `x' is not a blank or a tab, and
`...' is arbitrary). However, this string should not end in whitespace."
:type 'string
:group 'python-mode)
(defcustom py-indent-comments t
"When t, comment lines are indented. "
:type 'boolean
:group 'python-mode)
(defcustom py-temp-directory
(let ((ok '(lambda (x)
(and x
(setq x (expand-file-name x)) ; always true
(file-directory-p x)
(file-writable-p x)
x))))
(or (funcall ok (getenv "TMPDIR"))
(funcall ok "/usr/tmp")
(funcall ok "/tmp")
(funcall ok "/var/tmp")
(funcall ok ".")
(error
"Couldn't find a usable temp directory -- set `py-temp-directory'")))
"*Directory used for temporary files created by a *Python* process.
By default, the first directory from this list that exists and that you
can write into: the value (if any) of the environment variable TMPDIR,
/usr/tmp, /tmp, /var/tmp, or the current directory."
:type 'string
:group 'python-mode)
(defcustom py-beep-if-tab-change t
"*Ring the bell if `tab-width' is changed.
If a comment of the form
\t# vi:set tabsize=<number>:
is found before the first code line when the file is entered, and the
current value of (the general Emacs variable) `tab-width' does not
equal <number>, `tab-width' is set to <number>, a message saying so is
displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
the Emacs bell is also rung as a warning."
:type 'boolean
:group 'python-mode)
(defcustom py-jump-on-exception t
"*Jump to innermost exception frame in *Python Output* buffer.
When this variable is non-nil and an exception occurs when running
Python code synchronously in a subprocess, jump immediately to the
source code of the innermost traceback frame."
:type 'boolean
:group 'python-mode)
(defcustom py-ask-about-save t
"If not nil, ask about which buffers to save before executing some code.
Otherwise, all modified buffers are saved without asking."
:type 'boolean
:group 'python-mode)
(defcustom py-backspace-function 'backward-delete-char-untabify
"*Function called by `py-electric-backspace' when deleting backwards."
:type 'function
:group 'python-mode)
(defcustom py-delete-function 'delete-char
"*Function called by `py-electric-delete' when deleting forwards."
:type 'function
:group 'python-mode)
(defcustom py-pdbtrack-do-tracking-p t
"*Controls whether the pdbtrack feature is enabled or not.
When non-nil, pdbtrack is enabled in all comint-based buffers,
e.g. shell buffers and the *Python* buffer. When using pdb to debug a
Python program, pdbtrack notices the pdb prompt and displays the
source file and line that the program is stopped at, much the same way
as gud-mode does for debugging C programs with gdb."
:type 'boolean
:group 'python-mode)
(make-variable-buffer-local 'py-pdbtrack-do-tracking-p)
(defcustom py-pdbtrack-minor-mode-string " PDB"
"*String to use in the minor mode list when pdbtrack is enabled."
:type 'string
:group 'python-mode)
(defcustom py-import-check-point-max
20000
"Maximum number of characters to search for a Java-ish import statement.
When `python-mode' tries to calculate the shell to use (either a
CPython or a Jython shell), it looks at the so-called `shebang' line
-- i.e. #! line. If that's not available, it looks at some of the
file heading imports to see if they look Java-like."
:type 'integer
:group 'python-mode)
(defcustom py-jython-packages
'("java" "javax")
"Imported packages that imply `jython-mode'."
:type '(repeat string)
:group 'python-mode)
(make-obsolete-variable 'py-jpython-packages 'py-jython-packages nil)
(defcustom py-current-defun-show t
"If `py-current-defun' should jump to the definition, highlight it while waiting PY-WHICH-FUNC-DELAY seconds, before returning to previous position.
Default is `t'."
:type 'boolean
:group 'python-mode)
(defcustom py-current-defun-delay 2
"When called interactively, `py-current-defun' should wait PY-WHICH-FUNC-DELAY seconds at the definition name found, before returning to previous position. "
:type 'number
:group 'python-mode)
(defcustom py-send-receive-delay 5
"Seconds to wait for output, used by `python-send-receive'. "
:type 'number
:group 'python-mode)
(defvar py-exec-command nil
"Mode commands will set this. ")
(make-variable-buffer-local 'py-exec-command)
(defvar py-exec-string-command nil
"Mode commands will set this. ")
(make-variable-buffer-local 'py-exec-string-command)
(defvar py-which-bufname "Python")
(make-variable-buffer-local 'py-which-bufname)
(defcustom py-master-file nil
"If non-nil, \\[py-execute-buffer] executes the named
master file instead of the buffer's file. If the file name has a
relative path, the value of variable `default-directory' for the
buffer is prepended to come up with a file name.
Beside you may set this variable in the file's local
variable section, e.g.:
# Local Variables:
# py-master-file: \"master.py\"
# End:
"
:type 'string
:group 'python-mode)
(make-variable-buffer-local 'py-master-file)
(defvar py-pychecker-history nil)
(defcustom py-pychecker-command "pychecker"
"*Shell command used to run Pychecker."
:type 'string
:group 'python-mode
:tag "Pychecker Command")
(defcustom py-pychecker-command-args '("--stdlib")
"*List of string arguments to be passed to pychecker."
:type '(repeat string)
:group 'python-mode
:tag "Pychecker Command Args")
(defvar py-shell-alist
'(("jython" . 'jython)
("python" . 'cpython))
"*Alist of interpreters and python shells. Used by `py-choose-shell'
to select the appropriate python interpreter mode for a file.")
(defcustom py-shell-input-prompt-1-regexp "^>>> "
"*A regular expression to match the input prompt of the shell."
:type 'string
:group 'python-mode)
(defcustom py-shell-input-prompt-2-regexp "^[.][.][.] "
"*A regular expression to match the input prompt of the shell after the
first line of input."
:type 'string
:group 'python-mode)
(defcustom py-shell-switch-buffers-on-execute-p t
"When non-nil switch to the Python output buffer. "
:type 'boolean
:group 'python-mode)
(defcustom py-split-windows-on-execute-p t
"When non-nil split windows. "
:type 'boolean
:group 'python-mode)
(defcustom py-split-windows-on-execute-function 'split-window-vertically
"How window should get splitted to display results of py-execute-... functions. "
:type '(choice (const :tag "split-window-vertically" split-window-vertically)
(const :tag "split-window-horizontally" split-window-horizontally)
)
:group 'python-mode)
(make-variable-buffer-local 'py-split-windows-on-execute-function)
(defcustom py-hide-show-keywords
'("class" "def" "elif" "else" "except"
"for" "if" "while" "finally" "try"
"with")
"Keywords composing visible heads.
Also used by (minor-)outline-mode "
:type '(repeat string)
:group 'python-mode)
(defcustom py-hide-show-hide-docstrings t
"*Controls if doc strings can be hidden by hide-show"
:type 'boolean
:group 'python-mode)
(defcustom python-mode-hook nil
"Hook run when entering Python mode."
:group 'python-mode
:type 'hook)
(defcustom imenu-create-index-p t
"Non-nil means Python mode creates and displays an index menu of functions and global variables. "
:type 'boolean
:group 'python-mode)
(defcustom py-shell-name "python"
"A PATH/TO/EXECUTABLE or default value `py-shell' may look for, if no shell is specified by command. "
:type 'string
:group 'python-mode)
(make-variable-buffer-local 'py-shell-name)
(defcustom py-shell-toggle-1 py-shell-name
"A PATH/TO/EXECUTABLE or default value used by `py-toggle-shells'. "
:type 'string
:group 'python-mode)
(make-variable-buffer-local 'py-shell-toggle-1)
(defcustom py-shell-toggle-2 "python3"
"A PATH/TO/EXECUTABLE or default value used by `py-toggle-shells'. "
:type 'string
:group 'python-mode)
(make-variable-buffer-local 'py-shell-toggle-2)
(defcustom py-source-modes '(python-mode jython-mode)
"Used to determine if a buffer contains Python source code.
If a file is loaded into a buffer that is in one of these major modes, it is considered Python source by `py-load-file', which uses the
value to determine defaults."
:type '(repeat function)
:group 'python-mode)
(defcustom py-shell-prompt-alist
'(("ipython" . "^In \\[[0-9]+\\]: *")
(t . "^>>> "))
"Alist of Python input prompts.
Each element has the form (PROGRAM . REGEXP), where PROGRAM is
the value of `py-shell-name' for the python process and
REGEXP is a regular expression matching the Python prompt.
PROGRAM can also be t, which specifies the default when no other
element matches `py-shell-name'."
:type 'string
:group 'python-mode)
(defcustom py-shell-continuation-prompt-alist
'(("ipython" . "^ [.][.][.]+: *")
(t . "^[.][.][.] "))
"Alist of Python continued-line prompts.
Each element has the form (PROGRAM . REGEXP), where PROGRAM is
the value of `py-shell-name' for the python process and
REGEXP is a regular expression matching the Python prompt for
continued lines.
PROGRAM can also be t, which specifies the default when no other
element matches `py-shell-name'."
:type 'string
:group 'python-mode)
;; ipython.el
(defvar ipython-de-input-prompt-regexp "\\(?:
In \\[[0-9]+\\]: *.*
----+> \\(.*
\\)[\n]?\\)\\|\\(?:
In \\[[0-9]+\\]: *\\(.*
\\)\\)\\|^[ ]\\{3\\}[.]\\{3,\\}: *\\(.*
\\)"
"A regular expression to match the IPython input prompt and the python
command after it. The first match group is for a command that is rewritten,
the second for a 'normal' command, and the third for a multiline command.")
(defvar ipython-de-output-prompt-regexp "^Out\\[[0-9]+\\]: "
"A regular expression to match the output prompt of IPython.")
(defcustom py-match-paren-mode nil
"*Non-nil means, cursor will jump to beginning or end of a block.
This vice versa, to beginning first.
Sets `py-match-paren-key' in python-mode-map.
Customize `py-match-paren-key' which key to use. "
:type 'boolean
:group 'python-mode)
(defcustom py-match-paren-key "%"
"*String used by \\[comment-region] to comment out a block of code.
This should follow the convention for non-indenting comment lines so
that the indentation commands won't get confused (i.e., the string
should be of the form `#x...' where `x' is not a blank or a tab, and
`...' is arbitrary). However, this string should not end in whitespace."
:type 'string
:group 'python-mode)
(defcustom py-kill-empty-line t
"*If t, py-indent-forward-line kills empty lines. "
:type 'boolean
:group 'python-mode)
(defcustom py-remove-cwd-from-path t
"Whether to allow loading of Python modules from the current directory.
If this is non-nil, Emacs removes '' from sys.path when starting
an inferior Python process. This is the default, for security
reasons, as it is easy for the Python process to be started
without the user's realization (e.g. to perform completion)."
:type 'boolean
:group 'python-mode)
(defcustom py-imenu-show-method-args-p nil
"*Controls echoing of arguments of functions & methods in the Imenu buffer.
When non-nil, arguments are printed."
:type 'boolean
:group 'python-mode)
(defcustom python-default-interpreter 'cpython
"*Which Python interpreter is used by default.
The value for this variable can be either `cpython' or `jpython'.
When the value is `cpython', the variables `python-python-command' and
`python-python-command-args' are consulted to determine the interpreter
and arguments to use.
When the value is `jpython', the variables `python-jpython-command' and
`python-jpython-command-args' are consulted to determine the interpreter
and arguments to use.
Note that this variable is consulted only the first time that a Python
mode buffer is visited during an Emacs session. After that, use
\\[py-toggle-shells] to change the interpreter shell."
:type '(choice (const :tag "Python (a.k.a. CPython)" cpython)
(const :tag "JPython" jpython))
:group 'python-mode)
(defcustom python-python-command-args '("-i")
"*List of string arguments to be used when starting a Python shell."
:type '(repeat string)
:group 'python-mode)
(defcustom python-jython-command-args '("-i")
"*List of string arguments to be used when starting a Jython shell."
:type '(repeat string)
:group 'python-mode
:tag "JPython Command Args")
(defcustom python-pdbtrack-do-tracking-p t
"*Controls whether the pdbtrack feature is enabled or not.
When non-nil, pdbtrack is enabled in all comint-based buffers,
e.g. shell interaction buffers and the *Python* buffer.
When using pdb to debug a Python program, pdbtrack notices the
pdb prompt and presents the line in the source file where the
program is stopped in a pop-up buffer. It's similar to what
gud-mode does for debugging C programs with gdb, but without
having to restart the program."
:type 'boolean
:group 'python-mode)
(make-variable-buffer-local 'python-pdbtrack-do-tracking-p)
(defcustom python-pdbtrack-minor-mode-string " PDB"
"*Minor-mode sign to be displayed when pdbtrack is active."
:type 'string
:group 'python-mode)
(defcustom python-shell-prompt-alist
'(("ipython" . "^In \\[[0-9]+\\]: *")
(t . "^>>> "))
"Alist of Python input prompts.
Each element has the form (PROGRAM . REGEXP), where PROGRAM is
the value of `python-python-command' for the python process and
REGEXP is a regular expression matching the Python prompt.
PROGRAM can also be t, which specifies the default when no other
element matches `python-python-command'."
:type 'string
:group 'python-mode)
(defcustom python-shell-continuation-prompt-alist
'(("ipython" . "^ [.][.][.]+: *")
(t . "^[.][.][.] "))
"Alist of Python continued-line prompts.
Each element has the form (PROGRAM . REGEXP), where PROGRAM is
the value of `python-python-command' for the python process and
REGEXP is a regular expression matching the Python prompt for
continued lines.
PROGRAM can also be t, which specifies the default when no other
element matches `python-python-command'."
:type 'string
:group 'python-mode)
(defcustom python-python-command "python"
"Shell command to run Python interpreter.
Any arguments can't contain whitespace."
:group 'python-mode
:type 'string)
(defcustom python-jython-command "jython"
"Shell command to run Jython interpreter.
Any arguments can't contain whitespace."
:group 'python-mode
:type 'string)
(defcustom inferior-python-filter-regexp "\\`\\s-*\\S-?\\S-?\\s-*\\'"
"Input matching this regexp is not saved on the history list.
Default ignores all inputs of 0, 1, or 2 non-blank characters."
:type 'regexp
:group 'python-mode)
(defcustom python-remove-cwd-from-path t
"Whether to allow loading of Python modules from the current directory.
If this is non-nil, Emacs removes '' from sys.path when starting
an inferior Python process. This is the default, for security
reasons, as it is easy for the Python process to be started
without the user's realization (e.g. to perform completion)."
:type 'boolean
:group 'python-mode
:version "23.3")
(defcustom python-source-modes '(python-mode jython-mode)
"Used to determine if a buffer contains Python source code.
If a file is loaded into a buffer that is in one of these major modes, it is considered Python source by `py-load-file', which uses the
value to determine defaults."
:type '(repeat function)
:group 'python-mode)
(defcustom python-jython-packages '("java" "javax" "org" "com")
"Packages implying `jython-mode'.
If these are imported near the beginning of the buffer, `python-mode'
actually punts to `jython-mode'."
:type '(repeat string)
:group 'python-mode)
(defface py-number-face
'((t (:inherit default)))
;; '((t (:inherit 'font-lock-variable-name-face)))
"Highlight numbers. "
:group 'python-mode)
(defvar py-number-face 'py-number-face)
(defface py-XXX-tag-face
'((t (:inherit font-lock-string-face)))
"XXX\\|TODO\\|FIXME "
:group 'python-mode)
(defvar py-XXX-tag-face 'py-XXX-tag-face)
;; ;; Face for None, True, False, self, and Ellipsis
(defface py-pseudo-keyword-face
'((t (:inherit font-lock-keyword-face)))
"Face for pseudo keywords in Python mode, like self, True, False, Ellipsis."
:group 'python-mode)
(defvar py-pseudo-keyword-face 'py-pseudo-keyword-face)
(defface py-variable-name-face
'((t (:inherit default)))
;; '((t (:inherit 'font-lock-variable-name-face)))
"Face method decorators."
:group 'python-mode)
(defvar py-variable-name-face 'py-variable-name-face)
;; PEP 318 decorators
(defface py-decorators-face
'((t (:inherit font-lock-keyword-face)))
"Face method decorators."
:group 'python-mode)
(defvar py-decorators-face 'py-decorators-face)
;; Face for builtins
(defface py-builtins-face
'((t (:inherit font-lock-builtin-face)))
"Face for builtins like TypeError, object, open, and exec."
:group 'python-mode)
(defvar py-builtins-face 'py-builtins-face)
(defface py-class-name-face
'((t (:inherit font-lock-type-face)))
"Face for classes."
:group 'python-mode)
(defvar py-class-name-face 'py-class-name-face)
;; XXX, TODO, and FIXME comments and such
(defface py-exception-name-face
'((t (:inherit font-lock-builtin-face)))
"."
:group 'python-mode)
(defvar py-exception-name-face 'py-exception-name-face)
(defcustom py-use-local-default nil
"If `t', py-shell will use `py-shell-local-path' instead
of default Python.
Making switch between several virtualenv's easier,
Python-mode should deliver an installer, so named-shells pointing to virtualenv's will be available. "
:type 'boolean
:group 'python-mode)
;; (defcustom python-load-extended-executes-p t
;; "If commands from `python-extended-executes.el' should be loaded.
;;
;; Default is `t'.
;; Provides commands executing buffers code at different conditions, thus avoids customization of `py-shell-name', `py-shell-switch-buffers-on-execute-p'. "
;;
;; :type 'boolean
;; :group 'python-mode)
(defcustom py-shell-local-path ""
"If `py-use-local-default' is non-nil, `py-shell' will use EXECUTABLE indicated here incl. path. "
:type 'string
:group 'python-mode)
;;; highlight-indentation.el --- Function for highlighting indentation
;; Original Author: Anton Johansson <[email protected]> - http://antonj.se
;; Created: Dec 15 23:42:04 2010
;; URL: https://github.com/antonj/Highlight-Indentation-for-Emacs
(defcustom highlight-indentation nil
"If level of indentation should be displayed at start.
Toggle buffer local status via `M-x highlight-indentation' during session. "
:type 'boolean
:group 'python-mode)
(make-variable-buffer-local 'highlight-indentation)
(defvar highlight-indent-active nil)
(make-variable-buffer-local 'highlight-indent-active)
(defface highlight-indent-face
'((((class color) (background dark))
(:background "grey33"))
(((class color) (background light))
(:background "grey")))
"Basic face for highlighting indentation guides.")
(setq-default highlight-indent-offset 4)
(defvar ruby-indent-level nil)
(defvar nxml-child-indent nil)
(defun highlight-indentation-on ()
"Make sure `highlight-indentation' is on. "
(interactive)
(set (make-local-variable 'highlight-indent-active) nil)
(highlight-indentation)
(when (called-interactively-p 'any)
(message "highlight-indentation ON")))
(defun highlight-indentation-off ()
"Make sure `highlight-indentation' is off. "
(interactive)
(set (make-local-variable 'highlight-indent-active) t)
(highlight-indentation)
(when (called-interactively-p 'any)
(message "highlight-indentation OFF")))
(defun highlight-indentation (&optional indent-width)
"Toggle highlight indentation.
Optional argument INDENT-WIDTH specifies which indentation
level (spaces only) should be highlighted, if omitted
indent-width will be guessed from current major-mode"
(interactive "P")
(let ((re (format "\\( \\) \\{%s\\}" (- highlight-indent-offset 1))))
(if (not highlight-indent-active)
(progn ;; Toggle on
(set (make-local-variable 'highlight-indent-offset)
(if indent-width
indent-width
;; Set indentation offset according to major mode
(cond ((eq major-mode 'python-mode)
(if (boundp 'python-indent)
python-indent
py-indent-offset))
((eq major-mode 'ruby-mode)
ruby-indent-level)
((eq major-mode 'nxml-mode)
nxml-child-indent)
((local-variable-p 'c-basic-offset)
c-basic-offset)
(t
(default-value 'highlight-indent-offset)))))
(set (make-local-variable 'highlight-indent-active) t)
(if (featurep 'xemacs)
(font-lock-add-keywords nil `((,re (1 'paren-face-match))))
(font-lock-add-keywords nil `((,re (1 'highlight-indent-face)))))
(message (format "highlight-indentation with indent-width %s"
highlight-indent-offset)))
;; Toggle off
(set (make-local-variable 'highlight-indent-active) nil)
(if (featurep 'xemacs)
(font-lock-remove-keywords nil `((,re (1 'paren-face-match))))
(font-lock-remove-keywords nil `((,re (1 'highlight-indent-face)))))
(message "highlight-indentation OFF"))
(font-lock-fontify-buffer)))
(defcustom py-underscore-word-syntax-p t
"If underscore chars should be of syntax-class `word', not of `symbol'.
Underscores in word-class makes `forward-word' etc. travel the indentifiers. Default is `t'.
See bug report at launchpad, lp:940812 "
:type 'boolean
:group 'python-mode)
(defcustom py-edit-only-p nil
"When `t' Python-mode will not take resort nor check for installed Python executables. Default is nil.
See bug report at launchpad, lp:944093. "
:type 'boolean
:group 'python-mode)
;;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
(defvar py-execute-directory nil
"Stores the file's directory-name py-execute-... functions act upon. ")
(defvar py-prev-dir/file nil
"Caches (directory . file) pair used in the last `py-load-file' command.
Used for determining the default in the next one.")
(defvar py-exception-buffer nil)
(defvar py-output-buffer "*Python Output*")
(make-variable-buffer-local 'py-output-buffer)
(defvar py-execute-keep-temporary-file-p nil
"For tests only. Excute functions delete temporary files default. ")
(defvar py-expression-skip-regexp "^ =:#\t\r\n\f"
"py-expression assumes chars indicated possible composing a py-expression, skip it. ")
;; (setq py-expression-skip-regexp "^ =:#\t\r\n\f")
(defvar py-expression-looking-regexp "[^ =:#\t\r\n\f)]"
"py-expression assumes chars indicated possible composing a py-expression, when looking-at or -back. ")
;; (setq py-expression-looking-regexp "[^ =:#\t\r\n\f)]")