aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el180
1 files changed, 11 insertions, 169 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 4246177495c..3d243f14f07 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -99,7 +99,9 @@
99 "import" "in" "is" "lambda" "not" "or" "pass" "print" 99 "import" "in" "is" "lambda" "not" "or" "pass" "print"
100 "raise" "return" "try" "while" "with" "yield" 100 "raise" "return" "try" "while" "with" "yield"
101 ;; Not real keywords, but close enough to be fontified as such 101 ;; Not real keywords, but close enough to be fontified as such
102 "self" "True" "False") 102 "self" "True" "False"
103 ;; Python 3
104 "nonlocal")
103 symbol-end) 105 symbol-end)
104 (,(rx symbol-start "None" symbol-end) ; see § Keywords in 2.7 manual 106 (,(rx symbol-start "None" symbol-end) ; see § Keywords in 2.7 manual
105 . font-lock-constant-face) 107 . font-lock-constant-face)
@@ -499,44 +501,6 @@ statement."
499 :type 'integer) 501 :type 'integer)
500 502
501 503
502(defcustom python-default-interpreter 'cpython
503 "*Which Python interpreter is used by default.
504The value for this variable can be either `cpython' or `jpython'.
505
506When the value is `cpython', the variables `python-python-command' and
507`python-python-command-args' are consulted to determine the interpreter
508and arguments to use.
509
510When the value is `jpython', the variables `python-jpython-command' and
511`python-jpython-command-args' are consulted to determine the interpreter
512and arguments to use.
513
514Note that this variable is consulted only the first time that a Python
515mode buffer is visited during an Emacs session. After that, use
516\\[python-toggle-shells] to change the interpreter shell."
517 :type '(choice (const :tag "Python (a.k.a. CPython)" cpython)
518 (const :tag "JPython" jpython))
519 :group 'python)
520
521(defcustom python-python-command-args '("-i")
522 "*List of string arguments to be used when starting a Python shell."
523 :type '(repeat string)
524 :group 'python)
525
526(defcustom python-jython-command-args '("-i")
527 "*List of string arguments to be used when starting a Jython shell."
528 :type '(repeat string)
529 :group 'python
530 :tag "JPython Command Args")
531
532;; for toggling between CPython and JPython
533(defvar python-which-shell nil)
534(defvar python-which-args python-python-command-args)
535(defvar python-which-bufname "Python")
536(make-variable-buffer-local 'python-which-shell)
537(make-variable-buffer-local 'python-which-args)
538(make-variable-buffer-local 'python-which-bufname)
539
540(defcustom python-pdbtrack-do-tracking-p t 504(defcustom python-pdbtrack-do-tracking-p t
541 "*Controls whether the pdbtrack feature is enabled or not. 505 "*Controls whether the pdbtrack feature is enabled or not.
542 506
@@ -562,11 +526,6 @@ having to restart the program."
562 (push '(python-pdbtrack-is-tracking-p python-pdbtrack-minor-mode-string) 526 (push '(python-pdbtrack-is-tracking-p python-pdbtrack-minor-mode-string)
563 minor-mode-alist)) 527 minor-mode-alist))
564 528
565;; Bind python-file-queue before installing the kill-emacs-hook.
566(defvar python-file-queue nil
567 "Queue of Python temp files awaiting execution.
568Currently-active file is at the head of the list.")
569
570(defcustom python-shell-prompt-alist 529(defcustom python-shell-prompt-alist
571 '(("ipython" . "^In \\[[0-9]+\\]: *") 530 '(("ipython" . "^In \\[[0-9]+\\]: *")
572 (t . "^>>> ")) 531 (t . "^>>> "))
@@ -2402,6 +2361,7 @@ Interactively, prompt for the name with completion."
2402 2361
2403(autoload 'pymacs-load "pymacs" nil t) 2362(autoload 'pymacs-load "pymacs" nil t)
2404(autoload 'brm-init "bikemacs") 2363(autoload 'brm-init "bikemacs")
2364(defvar brm-menu)
2405 2365
2406;; I'm not sure how useful BRM really is, and it's certainly dangerous 2366;; I'm not sure how useful BRM really is, and it's certainly dangerous
2407;; the way it modifies files outside Emacs... Also note that the 2367;; the way it modifies files outside Emacs... Also note that the
@@ -2421,7 +2381,7 @@ without confirmation."
2421 (features (cons 'python-mode features))) ; and requires this 2381 (features (cons 'python-mode features))) ; and requires this
2422 (brm-init) ; second line of normal recipe 2382 (brm-init) ; second line of normal recipe
2423 (remove-hook 'python-mode-hook ; undo this from `brm-init' 2383 (remove-hook 'python-mode-hook ; undo this from `brm-init'
2424 '(lambda () (easy-menu-add brm-menu))) 2384 (lambda () (easy-menu-add brm-menu)))
2425 (easy-menu-define 2385 (easy-menu-define
2426 python-brm-menu python-mode-map 2386 python-brm-menu python-mode-map
2427 "Bicycle Repair Man" 2387 "Bicycle Repair Man"
@@ -2460,7 +2420,7 @@ without confirmation."
2460(defvar python-mode-running) ;Dynamically scoped var. 2420(defvar python-mode-running) ;Dynamically scoped var.
2461 2421
2462;;;###autoload 2422;;;###autoload
2463(define-derived-mode python-mode fundamental-mode "Python" 2423(define-derived-mode python-mode prog-mode "Python"
2464 "Major mode for editing Python files. 2424 "Major mode for editing Python files.
2465Turns on Font Lock mode unconditionally since it is currently required 2425Turns on Font Lock mode unconditionally since it is currently required
2466for correct parsing of the source. 2426for correct parsing of the source.
@@ -2538,7 +2498,7 @@ with skeleton expansions for compound statement templates.
2538 ;; doesn't seem to work properly. 2498 ;; doesn't seem to work properly.
2539 (add-to-list 'hs-special-modes-alist 2499 (add-to-list 'hs-special-modes-alist
2540 `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#" 2500 `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#"
2541 ,(lambda (arg) 2501 ,(lambda (_arg)
2542 (python-end-of-defun) 2502 (python-end-of-defun)
2543 (skip-chars-backward " \t\n")) 2503 (skip-chars-backward " \t\n"))
2544 nil)) 2504 nil))
@@ -2584,20 +2544,6 @@ Runs `jython-mode-hook' after `python-mode-hook'."
2584 2544
2585;; pdbtrack features 2545;; pdbtrack features
2586 2546
2587(defun python-comint-output-filter-function (string)
2588 "Watch output for Python prompt and exec next file waiting in queue.
2589This function is appropriate for `comint-output-filter-functions'."
2590 ;; TBD: this should probably use split-string
2591 (when (and (string-match python--prompt-regexp string)
2592 python-file-queue)
2593 (condition-case nil
2594 (delete-file (car python-file-queue))
2595 (error nil))
2596 (setq python-file-queue (cdr python-file-queue))
2597 (if python-file-queue
2598 (let ((pyproc (get-buffer-process (current-buffer))))
2599 (python-execute-file pyproc (car python-file-queue))))))
2600
2601(defun python-pdbtrack-overlay-arrow (activation) 2547(defun python-pdbtrack-overlay-arrow (activation)
2602 "Activate or deactivate arrow at beginning-of-line in current buffer." 2548 "Activate or deactivate arrow at beginning-of-line in current buffer."
2603 (if activation 2549 (if activation
@@ -2611,7 +2557,7 @@ This function is appropriate for `comint-output-filter-functions'."
2611 (setq overlay-arrow-position nil 2557 (setq overlay-arrow-position nil
2612 python-pdbtrack-is-tracking-p nil))) 2558 python-pdbtrack-is-tracking-p nil)))
2613 2559
2614(defun python-pdbtrack-track-stack-file (text) 2560(defun python-pdbtrack-track-stack-file (_text)
2615 "Show the file indicated by the pdb stack entry line, in a separate window. 2561 "Show the file indicated by the pdb stack entry line, in a separate window.
2616 2562
2617Activity is disabled if the buffer-local variable 2563Activity is disabled if the buffer-local variable
@@ -2723,8 +2669,8 @@ problem."
2723 ) 2669 )
2724 ) 2670 )
2725 2671
2726(defun python-pdbtrack-grub-for-buffer (funcname lineno) 2672(defun python-pdbtrack-grub-for-buffer (funcname _lineno)
2727 "Find recent python-mode buffer named, or having function named funcname." 2673 "Find recent Python mode buffer named, or having function named FUNCNAME."
2728 (let ((buffers (buffer-list)) 2674 (let ((buffers (buffer-list))
2729 buf 2675 buf
2730 got) 2676 got)
@@ -2742,45 +2688,6 @@ problem."
2742 (setq got buf))) 2688 (setq got buf)))
2743 got)) 2689 got))
2744 2690
2745(defun python-toggle-shells (arg)
2746 "Toggles between the CPython and JPython shells.
2747
2748With positive argument ARG (interactively \\[universal-argument]),
2749uses the CPython shell, with negative ARG uses the JPython shell, and
2750with a zero argument, toggles the shell.
2751
2752Programmatically, ARG can also be one of the symbols `cpython' or
2753`jpython', equivalent to positive arg and negative arg respectively."
2754 (interactive "P")
2755 ;; default is to toggle
2756 (if (null arg)
2757 (setq arg 0))
2758 ;; preprocess arg
2759 (cond
2760 ((equal arg 0)
2761 ;; toggle
2762 (if (string-equal python-which-bufname "Python")
2763 (setq arg -1)
2764 (setq arg 1)))
2765 ((equal arg 'cpython) (setq arg 1))
2766 ((equal arg 'jpython) (setq arg -1)))
2767 (let (msg)
2768 (cond
2769 ((< 0 arg)
2770 ;; set to CPython
2771 (setq python-which-shell python-python-command
2772 python-which-args python-python-command-args
2773 python-which-bufname "Python"
2774 msg "CPython"
2775 mode-name "Python"))
2776 ((> 0 arg)
2777 (setq python-which-shell python-jython-command
2778 python-which-args python-jython-command-args
2779 python-which-bufname "JPython"
2780 msg "JPython"
2781 mode-name "JPython")))
2782 (message "Using the %s shell" msg)))
2783
2784;; Python subprocess utilities and filters 2691;; Python subprocess utilities and filters
2785(defun python-execute-file (proc filename) 2692(defun python-execute-file (proc filename)
2786 "Send to Python interpreter process PROC \"execfile('FILENAME')\". 2693 "Send to Python interpreter process PROC \"execfile('FILENAME')\".
@@ -2801,71 +2708,6 @@ comint believe the user typed this string so that
2801 (set-buffer curbuf)) 2708 (set-buffer curbuf))
2802 (process-send-string proc cmd))) 2709 (process-send-string proc cmd)))
2803 2710
2804;;;###autoload
2805(defun python-shell (&optional argprompt)
2806 "Start an interactive Python interpreter in another window.
2807This is like Shell mode, except that Python is running in the window
2808instead of a shell. See the `Interactive Shell' and `Shell Mode'
2809sections of the Emacs manual for details, especially for the key
2810bindings active in the `*Python*' buffer.
2811
2812With optional \\[universal-argument], the user is prompted for the
2813flags to pass to the Python interpreter. This has no effect when this
2814command is used to switch to an existing process, only when a new
2815process is started. If you use this, you will probably want to ensure
2816that the current arguments are retained (they will be included in the
2817prompt). This argument is ignored when this function is called
2818programmatically.
2819
2820Note: You can toggle between using the CPython interpreter and the
2821JPython interpreter by hitting \\[python-toggle-shells]. This toggles
2822buffer local variables which control whether all your subshell
2823interactions happen to the `*JPython*' or `*Python*' buffers (the
2824latter is the name used for the CPython buffer).
2825
2826Warning: Don't use an interactive Python if you change sys.ps1 or
2827sys.ps2 from their default values, or if you're running code that
2828prints `>>> ' or `... ' at the start of a line. `python-mode' can't
2829distinguish your output from Python's output, and assumes that `>>> '
2830at the start of a line is a prompt from Python. Similarly, the Emacs
2831Shell mode code assumes that both `>>> ' and `... ' at the start of a
2832line are Python prompts. Bad things can happen if you fool either
2833mode.
2834
2835Warning: If you do any editing *in* the process buffer *while* the
2836buffer is accepting output from Python, do NOT attempt to `undo' the
2837changes. Some of the output (nowhere near the parts you changed!) may
2838be lost if you do. This appears to be an Emacs bug, an unfortunate
2839interaction between undo and process filters; the same problem exists in
2840non-Python process buffers using the default (Emacs-supplied) process
2841filter."
2842 (interactive "P")
2843 (require 'ansi-color) ; For ipython
2844 ;; Set the default shell if not already set
2845 (when (null python-which-shell)
2846 (python-toggle-shells python-default-interpreter))
2847 (let ((args python-which-args))
2848 (when (and argprompt
2849 (called-interactively-p 'interactive)
2850 (fboundp 'split-string))
2851 ;; TBD: Perhaps force "-i" in the final list?
2852 (setq args (split-string
2853 (read-string (concat python-which-bufname
2854 " arguments: ")
2855 (concat
2856 (mapconcat 'identity python-which-args " ") " ")
2857 ))))
2858 (switch-to-buffer-other-window
2859 (apply 'make-comint python-which-bufname python-which-shell nil args))
2860 (set-process-sentinel (get-buffer-process (current-buffer))
2861 'python-sentinel)
2862 (python--set-prompt-regexp)
2863 (add-hook 'comint-output-filter-functions
2864 'python-comint-output-filter-function nil t)
2865 ;; pdbtrack
2866 (set-syntax-table python-mode-syntax-table)
2867 (use-local-map python-shell-map)))
2868
2869(defun python-pdbtrack-toggle-stack-tracking (arg) 2711(defun python-pdbtrack-toggle-stack-tracking (arg)
2870 (interactive "P") 2712 (interactive "P")
2871 (if (not (get-buffer-process (current-buffer))) 2713 (if (not (get-buffer-process (current-buffer)))
@@ -2886,7 +2728,7 @@ filter."
2886 (interactive) 2728 (interactive)
2887 (python-pdbtrack-toggle-stack-tracking 0)) 2729 (python-pdbtrack-toggle-stack-tracking 0))
2888 2730
2889(defun python-sentinel (proc msg) 2731(defun python-sentinel (_proc _msg)
2890 (setq overlay-arrow-position nil)) 2732 (setq overlay-arrow-position nil))
2891 2733
2892(provide 'python) 2734(provide 'python)