2013-12-26 13:45:19 -03:00
|
|
|
|
;;; python.el --- Python's flying circus support for Emacs -*- lexical-binding: t -*-
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2014-01-01 07:43:34 +00:00
|
|
|
|
;; Copyright (C) 2003-2014 Free Software Foundation, Inc.
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
;; Author: Fabián E. Gallina <fabian@anue.biz>
|
2012-05-17 00:03:18 -03:00
|
|
|
|
;; URL: https://github.com/fgallina/python.el
|
2014-07-19 10:13:07 -03:00
|
|
|
|
;; Version: 0.24.4
|
2014-02-09 17:34:22 -08:00
|
|
|
|
;; Maintainer: emacs-devel@gnu.org
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; Created: Jul 2010
|
|
|
|
|
;; Keywords: languages
|
|
|
|
|
|
2012-05-17 00:03:45 -03:00
|
|
|
|
;; This file is part of GNU Emacs.
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:45 -03:00
|
|
|
|
;; GNU Emacs 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.
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:45 -03:00
|
|
|
|
;; GNU Emacs 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.
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2012-05-17 00:03:45 -03:00
|
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;; Major mode for editing Python files with some fontification and
|
|
|
|
|
;; indentation bits extracted from original Dave Love's python.el
|
|
|
|
|
;; found in GNU/Emacs.
|
|
|
|
|
|
|
|
|
|
;; Implements Syntax highlighting, Indentation, Movement, Shell
|
2014-07-26 20:43:51 -03:00
|
|
|
|
;; interaction, Shell completion, Shell virtualenv support, Shell
|
|
|
|
|
;; syntax highlighting, Pdb tracking, Symbol completion, Skeletons,
|
|
|
|
|
;; FFAP, Code Check, Eldoc, Imenu.
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
;; Syntax highlighting: Fontification of code is provided and supports
|
|
|
|
|
;; python's triple quoted strings properly.
|
|
|
|
|
|
|
|
|
|
;; Indentation: Automatic indentation with indentation cycling is
|
|
|
|
|
;; provided, it allows you to navigate different available levels of
|
2013-11-28 21:03:39 -05:00
|
|
|
|
;; indentation by hitting <tab> several times. Also electric-indent-mode
|
|
|
|
|
;; is supported such that when inserting a colon the current line is
|
|
|
|
|
;; dedented automatically if needed.
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
;; Movement: `beginning-of-defun' and `end-of-defun' functions are
|
2012-05-17 00:03:25 -03:00
|
|
|
|
;; properly implemented. There are also specialized
|
2012-07-16 10:13:01 -03:00
|
|
|
|
;; `forward-sentence' and `backward-sentence' replacements called
|
|
|
|
|
;; `python-nav-forward-block', `python-nav-backward-block'
|
|
|
|
|
;; respectively which navigate between beginning of blocks of code.
|
|
|
|
|
;; Extra functions `python-nav-forward-statement',
|
2012-07-17 15:02:53 -03:00
|
|
|
|
;; `python-nav-backward-statement',
|
|
|
|
|
;; `python-nav-beginning-of-statement', `python-nav-end-of-statement',
|
2013-09-02 00:37:18 -03:00
|
|
|
|
;; `python-nav-beginning-of-block', `python-nav-end-of-block' and
|
|
|
|
|
;; `python-nav-if-name-main' are included but no bound to any key. At
|
|
|
|
|
;; last but not least the specialized `python-nav-forward-sexp' allows
|
|
|
|
|
;; easy navigation between code blocks. If you prefer `cc-mode'-like
|
|
|
|
|
;; `forward-sexp' movement, setting `forward-sexp-function' to nil is
|
|
|
|
|
;; enough, You can do that using the `python-mode-hook':
|
2013-02-13 21:42:11 -03:00
|
|
|
|
|
|
|
|
|
;; (add-hook 'python-mode-hook
|
|
|
|
|
;; (lambda () (setq forward-sexp-function nil)))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2014-07-20 15:12:30 -03:00
|
|
|
|
;; Shell interaction: is provided and allows opening Python shells
|
|
|
|
|
;; inside Emacs and executing any block of code of your current buffer
|
|
|
|
|
;; in that inferior Python process.
|
|
|
|
|
|
|
|
|
|
;; Besides that only the standard CPython (2.x and 3.x) shell and
|
|
|
|
|
;; IPython are officially supported out of the box, the interaction
|
|
|
|
|
;; should support any other readline based Python shells as well
|
|
|
|
|
;; (e.g. Jython and Pypy have been reported to work). You can change
|
|
|
|
|
;; your default interpreter and commandline arguments by setting the
|
|
|
|
|
;; `python-shell-interpreter' and `python-shell-interpreter-args'
|
|
|
|
|
;; variables. This example enables IPython globally:
|
|
|
|
|
|
|
|
|
|
;; (setq python-shell-interpreter "ipython"
|
|
|
|
|
;; python-shell-interpreter-args "-i")
|
|
|
|
|
|
|
|
|
|
;; Using the "console" subcommand to start IPython in server-client
|
|
|
|
|
;; mode is known to fail intermittently due a bug on IPython itself
|
|
|
|
|
;; (see URL `http://debbugs.gnu.org/cgi/bugreport.cgi?bug=18052#27').
|
|
|
|
|
;; There seems to be a race condition in the IPython server (A.K.A
|
|
|
|
|
;; kernel) when code is sent while it is still initializing, sometimes
|
|
|
|
|
;; causing the shell to get stalled. With that said, if an IPython
|
|
|
|
|
;; kernel is already running, "console --existing" seems to work fine.
|
|
|
|
|
|
|
|
|
|
;; Running IPython on Windows needs more tweaking. The way you should
|
|
|
|
|
;; set `python-shell-interpreter' and `python-shell-interpreter-args'
|
|
|
|
|
;; is as follows (of course you need to modify the paths according to
|
|
|
|
|
;; your system):
|
|
|
|
|
|
|
|
|
|
;; (setq python-shell-interpreter "C:\\Python27\\python.exe"
|
|
|
|
|
;; python-shell-interpreter-args
|
|
|
|
|
;; "-i C:\\Python27\\Scripts\\ipython-script.py")
|
|
|
|
|
|
2014-07-20 16:51:16 -03:00
|
|
|
|
;; If you are experiencing missing or delayed output in your shells,
|
|
|
|
|
;; that's likely caused by your Operating System's pipe buffering
|
|
|
|
|
;; (e.g. this is known to happen running CPython 3.3.4 in Windows 7.
|
|
|
|
|
;; See URL `http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17304'). To
|
|
|
|
|
;; fix this, using CPython's "-u" commandline argument or setting the
|
|
|
|
|
;; "PYTHONUNBUFFERED" environment variable should help: See URL
|
|
|
|
|
;; `https://docs.python.org/3/using/cmdline.html#cmdoption-u'.
|
|
|
|
|
|
2014-07-20 15:12:30 -03:00
|
|
|
|
;; The interaction relies upon having prompts for input (e.g. ">>> "
|
|
|
|
|
;; and "... " in standard Python shell) and output (e.g. "Out[1]: " in
|
|
|
|
|
;; IPython) detected properly. Failing that Emacs may hang but, in
|
|
|
|
|
;; the case that happens, you can recover with \\[keyboard-quit]. To
|
|
|
|
|
;; avoid this issue, a two-step prompt autodetection mechanism is
|
|
|
|
|
;; provided: the first step is manual and consists of a collection of
|
|
|
|
|
;; regular expressions matching common prompts for Python shells
|
|
|
|
|
;; stored in `python-shell-prompt-input-regexps' and
|
2014-07-19 10:13:07 -03:00
|
|
|
|
;; `python-shell-prompt-output-regexps', and dir-local friendly vars
|
|
|
|
|
;; `python-shell-prompt-regexp', `python-shell-prompt-block-regexp',
|
|
|
|
|
;; `python-shell-prompt-output-regexp' which are appended to the
|
|
|
|
|
;; former automatically when a shell spawns; the second step is
|
|
|
|
|
;; automatic and depends on the `python-shell-prompt-detect' helper
|
|
|
|
|
;; function. See its docstring for details on global variables that
|
|
|
|
|
;; modify its behavior.
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
;; Shell completion: hitting tab will try to complete the current
|
2014-07-20 15:12:30 -03:00
|
|
|
|
;; word. Shell completion is implemented in such way that if you
|
|
|
|
|
;; change the `python-shell-interpreter' it should be possible to
|
|
|
|
|
;; integrate custom logic to calculate completions. To achieve this
|
|
|
|
|
;; you just need to set `python-shell-completion-setup-code' and
|
|
|
|
|
;; `python-shell-completion-string-code'. The default provided code,
|
|
|
|
|
;; enables autocompletion for both CPython and IPython (and ideally
|
|
|
|
|
;; any readline based Python shell). This code depends on the
|
2012-05-17 00:03:07 -03:00
|
|
|
|
;; readline module, so if you are using some Operating System that
|
2014-07-20 15:12:30 -03:00
|
|
|
|
;; bundles Python without it (like Windows), installing pyreadline
|
|
|
|
|
;; from URL `http://ipython.scipy.org/moin/PyReadline/Intro' should
|
|
|
|
|
;; suffice. To troubleshoot why you are not getting any completions
|
|
|
|
|
;; you can try the following in your Python shell:
|
|
|
|
|
|
|
|
|
|
;; >>> import readline, rlcompleter
|
|
|
|
|
|
|
|
|
|
;; If you see an error, then you need to either install pyreadline or
|
|
|
|
|
;; setup custom code that avoids that dependency.
|
2012-05-17 00:03:07 -03:00
|
|
|
|
|
2012-05-17 00:03:21 -03:00
|
|
|
|
;; Shell virtualenv support: The shell also contains support for
|
|
|
|
|
;; virtualenvs and other special environment modifications thanks to
|
2012-05-17 00:03:10 -03:00
|
|
|
|
;; `python-shell-process-environment' and `python-shell-exec-path'.
|
|
|
|
|
;; These two variables allows you to modify execution paths and
|
2012-05-17 00:03:24 -03:00
|
|
|
|
;; environment variables to make easy for you to setup virtualenv rules
|
2012-05-17 00:03:21 -03:00
|
|
|
|
;; or behavior modifications when running shells. Here is an example
|
2012-05-17 00:03:10 -03:00
|
|
|
|
;; of how to make shell processes to be run using the /path/to/env/
|
|
|
|
|
;; virtualenv:
|
|
|
|
|
|
|
|
|
|
;; (setq python-shell-process-environment
|
|
|
|
|
;; (list
|
|
|
|
|
;; (format "PATH=%s" (mapconcat
|
|
|
|
|
;; 'identity
|
|
|
|
|
;; (reverse
|
|
|
|
|
;; (cons (getenv "PATH")
|
|
|
|
|
;; '("/path/to/env/bin/")))
|
|
|
|
|
;; ":"))
|
|
|
|
|
;; "VIRTUAL_ENV=/path/to/env/"))
|
|
|
|
|
;; (python-shell-exec-path . ("/path/to/env/bin/"))
|
|
|
|
|
|
2012-06-17 01:53:31 -07:00
|
|
|
|
;; Since the above is cumbersome and can be programmatically
|
2012-05-17 00:03:21 -03:00
|
|
|
|
;; calculated, the variable `python-shell-virtualenv-path' is
|
|
|
|
|
;; provided. When this variable is set with the path of the
|
|
|
|
|
;; virtualenv to use, `process-environment' and `exec-path' get proper
|
|
|
|
|
;; values in order to run shells inside the specified virtualenv. So
|
|
|
|
|
;; the following will achieve the same as the previous example:
|
|
|
|
|
|
|
|
|
|
;; (setq python-shell-virtualenv-path "/path/to/env/")
|
|
|
|
|
|
2012-05-17 00:03:27 -03:00
|
|
|
|
;; Also the `python-shell-extra-pythonpaths' variable have been
|
|
|
|
|
;; introduced as simple way of adding paths to the PYTHONPATH without
|
|
|
|
|
;; affecting existing values.
|
|
|
|
|
|
2014-07-26 20:43:51 -03:00
|
|
|
|
;; Shell syntax highlighting: when enabled current input in shell is
|
|
|
|
|
;; highlighted. The variable `python-shell-font-lock-enable' controls
|
|
|
|
|
;; activation of this feature globally when shells are started.
|
|
|
|
|
;; Activation/deactivation can be also controlled on the fly via the
|
|
|
|
|
;; `python-shell-font-lock-toggle' command.
|
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; Pdb tracking: when you execute a block of code that contains some
|
|
|
|
|
;; call to pdb (or ipdb) it will prompt the block of code and will
|
|
|
|
|
;; follow the execution of pdb marking the current line with an arrow.
|
|
|
|
|
|
2012-05-17 00:02:56 -03:00
|
|
|
|
;; Symbol completion: you can complete the symbol at point. It uses
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; the shell completion in background so you should run
|
|
|
|
|
;; `python-shell-send-buffer' from time to time to get better results.
|
|
|
|
|
|
2014-06-11 22:35:26 -04:00
|
|
|
|
;; Skeletons: skeletons are provided for simple inserting of things like class,
|
|
|
|
|
;; def, for, import, if, try, and while. These skeletons are
|
|
|
|
|
;; integrated with abbrev. If you have `abbrev-mode' activated and
|
2012-05-17 00:03:05 -03:00
|
|
|
|
;; `python-skeleton-autoinsert' is set to t, then whenever you type
|
|
|
|
|
;; the name of any of those defined and hit SPC, they will be
|
2013-01-23 22:24:09 -03:00
|
|
|
|
;; automatically expanded. As an alternative you can use the defined
|
2014-06-11 22:35:26 -04:00
|
|
|
|
;; skeleton commands: `python-skeleton-<foo>'.
|
2012-05-17 00:03:05 -03:00
|
|
|
|
|
2012-05-17 00:03:00 -03:00
|
|
|
|
;; FFAP: You can find the filename for a given module when using ffap
|
|
|
|
|
;; out of the box. This feature needs an inferior python shell
|
|
|
|
|
;; running.
|
|
|
|
|
|
2012-05-17 00:03:14 -03:00
|
|
|
|
;; Code check: Check the current file for errors with `python-check'
|
|
|
|
|
;; using the program defined in `python-check-command'.
|
2012-05-17 00:03:00 -03:00
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; Eldoc: returns documentation for object at point by using the
|
2012-05-17 00:02:56 -03:00
|
|
|
|
;; inferior python subprocess to inspect its documentation. As you
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; might guessed you should run `python-shell-send-buffer' from time
|
|
|
|
|
;; to time to get better results too.
|
|
|
|
|
|
2013-04-18 23:31:09 -03:00
|
|
|
|
;; Imenu: There are two index building functions to be used as
|
|
|
|
|
;; `imenu-create-index-function': `python-imenu-create-index' (the
|
|
|
|
|
;; default one, builds the alist in form of a tree) and
|
2013-12-26 13:45:19 -03:00
|
|
|
|
;; `python-imenu-create-flat-index'. See also
|
2013-04-18 23:31:09 -03:00
|
|
|
|
;; `python-imenu-format-item-label-function',
|
|
|
|
|
;; `python-imenu-format-parent-item-label-function',
|
|
|
|
|
;; `python-imenu-format-parent-item-jump-label-function' variables for
|
|
|
|
|
;; changing the way labels are formatted in the tree version.
|
2012-05-17 00:03:14 -03:00
|
|
|
|
|
2012-05-17 00:03:02 -03:00
|
|
|
|
;; If you used python-mode.el you probably will miss auto-indentation
|
|
|
|
|
;; when inserting newlines. To achieve the same behavior you have
|
|
|
|
|
;; two options:
|
|
|
|
|
|
|
|
|
|
;; 1) Use GNU/Emacs' standard binding for `newline-and-indent': C-j.
|
|
|
|
|
|
|
|
|
|
;; 2) Add the following hook in your .emacs:
|
|
|
|
|
|
|
|
|
|
;; (add-hook 'python-mode-hook
|
|
|
|
|
;; #'(lambda ()
|
|
|
|
|
;; (define-key python-mode-map "\C-m" 'newline-and-indent)))
|
|
|
|
|
|
|
|
|
|
;; I'd recommend the first one since you'll get the same behavior for
|
|
|
|
|
;; all modes out-of-the-box.
|
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;;; Installation:
|
|
|
|
|
|
|
|
|
|
;; Add this to your .emacs:
|
|
|
|
|
|
|
|
|
|
;; (add-to-list 'load-path "/folder/containing/file")
|
|
|
|
|
;; (require 'python)
|
|
|
|
|
|
|
|
|
|
;;; TODO:
|
|
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
|
|
(require 'ansi-color)
|
2014-07-19 10:13:07 -03:00
|
|
|
|
(require 'cl-lib)
|
2012-05-17 00:03:05 -03:00
|
|
|
|
(require 'comint)
|
2014-07-19 10:13:07 -03:00
|
|
|
|
(require 'json)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-11-19 16:30:55 -05:00
|
|
|
|
;; Avoid compiler warnings
|
|
|
|
|
(defvar view-return-to-alist)
|
|
|
|
|
(defvar compilation-error-regexp-alist)
|
|
|
|
|
(defvar outline-heading-end-regexp)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(autoload 'comint-mode "comint")
|
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(add-to-list 'auto-mode-alist (cons (purecopy "\\.py\\'") 'python-mode))
|
|
|
|
|
;;;###autoload
|
2013-09-10 23:44:35 -07:00
|
|
|
|
(add-to-list 'interpreter-mode-alist (cons (purecopy "python[0-9.]*") 'python-mode))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defgroup python nil
|
|
|
|
|
"Python Language's flying circus support for Emacs."
|
|
|
|
|
:group 'languages
|
2012-12-31 16:35:57 -03:00
|
|
|
|
:version "24.3"
|
2012-05-17 00:02:52 -03:00
|
|
|
|
:link '(emacs-commentary-link "python"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Bindings
|
|
|
|
|
|
|
|
|
|
(defvar python-mode-map
|
|
|
|
|
(let ((map (make-sparse-keymap)))
|
2012-05-17 00:03:15 -03:00
|
|
|
|
;; Movement
|
2012-10-11 21:07:25 -03:00
|
|
|
|
(define-key map [remap backward-sentence] 'python-nav-backward-block)
|
|
|
|
|
(define-key map [remap forward-sentence] 'python-nav-forward-block)
|
|
|
|
|
(define-key map [remap backward-up-list] 'python-nav-backward-up-list)
|
2012-07-16 14:18:39 -03:00
|
|
|
|
(define-key map "\C-c\C-j" 'imenu)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; Indent specific
|
|
|
|
|
(define-key map "\177" 'python-indent-dedent-line-backspace)
|
|
|
|
|
(define-key map (kbd "<backtab>") 'python-indent-dedent-line)
|
|
|
|
|
(define-key map "\C-c<" 'python-indent-shift-left)
|
|
|
|
|
(define-key map "\C-c>" 'python-indent-shift-right)
|
2012-05-17 00:03:05 -03:00
|
|
|
|
;; Skeletons
|
|
|
|
|
(define-key map "\C-c\C-tc" 'python-skeleton-class)
|
|
|
|
|
(define-key map "\C-c\C-td" 'python-skeleton-def)
|
|
|
|
|
(define-key map "\C-c\C-tf" 'python-skeleton-for)
|
|
|
|
|
(define-key map "\C-c\C-ti" 'python-skeleton-if)
|
2014-06-11 22:35:26 -04:00
|
|
|
|
(define-key map "\C-c\C-tm" 'python-skeleton-import)
|
2012-05-17 00:03:05 -03:00
|
|
|
|
(define-key map "\C-c\C-tt" 'python-skeleton-try)
|
|
|
|
|
(define-key map "\C-c\C-tw" 'python-skeleton-while)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; Shell interaction
|
2012-07-27 09:38:19 -03:00
|
|
|
|
(define-key map "\C-c\C-p" 'run-python)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(define-key map "\C-c\C-s" 'python-shell-send-string)
|
|
|
|
|
(define-key map "\C-c\C-r" 'python-shell-send-region)
|
|
|
|
|
(define-key map "\C-\M-x" 'python-shell-send-defun)
|
|
|
|
|
(define-key map "\C-c\C-c" 'python-shell-send-buffer)
|
|
|
|
|
(define-key map "\C-c\C-l" 'python-shell-send-file)
|
|
|
|
|
(define-key map "\C-c\C-z" 'python-shell-switch-to-shell)
|
2012-05-17 00:03:00 -03:00
|
|
|
|
;; Some util commands
|
|
|
|
|
(define-key map "\C-c\C-v" 'python-check)
|
2012-05-17 00:03:00 -03:00
|
|
|
|
(define-key map "\C-c\C-f" 'python-eldoc-at-point)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; Utilities
|
|
|
|
|
(substitute-key-definition 'complete-symbol 'completion-at-point
|
2012-05-17 00:03:35 -03:00
|
|
|
|
map global-map)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(easy-menu-define python-menu map "Python Mode menu"
|
|
|
|
|
`("Python"
|
2012-05-17 00:03:35 -03:00
|
|
|
|
:help "Python-specific Features"
|
|
|
|
|
["Shift region left" python-indent-shift-left :active mark-active
|
|
|
|
|
:help "Shift region left by a single indentation step"]
|
|
|
|
|
["Shift region right" python-indent-shift-right :active mark-active
|
|
|
|
|
:help "Shift region right by a single indentation step"]
|
|
|
|
|
"-"
|
|
|
|
|
["Start of def/class" beginning-of-defun
|
|
|
|
|
:help "Go to start of outermost definition around point"]
|
|
|
|
|
["End of def/class" end-of-defun
|
|
|
|
|
:help "Go to end of definition around point"]
|
|
|
|
|
["Mark def/class" mark-defun
|
|
|
|
|
:help "Mark outermost definition around point"]
|
2012-07-16 14:18:39 -03:00
|
|
|
|
["Jump to def/class" imenu
|
2012-05-17 00:03:35 -03:00
|
|
|
|
:help "Jump to a class or function definition"]
|
2012-05-17 00:03:25 -03:00
|
|
|
|
"--"
|
2012-05-17 00:03:35 -03:00
|
|
|
|
("Skeletons")
|
2012-05-17 00:03:25 -03:00
|
|
|
|
"---"
|
2012-05-17 00:03:35 -03:00
|
|
|
|
["Start interpreter" run-python
|
|
|
|
|
:help "Run inferior Python process in a separate buffer"]
|
|
|
|
|
["Switch to shell" python-shell-switch-to-shell
|
|
|
|
|
:help "Switch to running inferior Python process"]
|
|
|
|
|
["Eval string" python-shell-send-string
|
|
|
|
|
:help "Eval string in inferior Python session"]
|
|
|
|
|
["Eval buffer" python-shell-send-buffer
|
|
|
|
|
:help "Eval buffer in inferior Python session"]
|
|
|
|
|
["Eval region" python-shell-send-region
|
|
|
|
|
:help "Eval region in inferior Python session"]
|
|
|
|
|
["Eval defun" python-shell-send-defun
|
|
|
|
|
:help "Eval defun in inferior Python session"]
|
|
|
|
|
["Eval file" python-shell-send-file
|
|
|
|
|
:help "Eval file in inferior Python session"]
|
|
|
|
|
["Debugger" pdb :help "Run pdb under GUD"]
|
2012-05-17 00:03:25 -03:00
|
|
|
|
"----"
|
2012-05-17 00:03:35 -03:00
|
|
|
|
["Check file" python-check
|
|
|
|
|
:help "Check file for errors"]
|
|
|
|
|
["Help on symbol" python-eldoc-at-point
|
|
|
|
|
:help "Get help on symbol at point"]
|
|
|
|
|
["Complete symbol" completion-at-point
|
|
|
|
|
:help "Complete symbol before point"]))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
map)
|
|
|
|
|
"Keymap for `python-mode'.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Python specialized rx
|
|
|
|
|
|
2012-05-17 00:03:05 -03:00
|
|
|
|
(eval-when-compile
|
|
|
|
|
(defconst python-rx-constituents
|
2012-06-18 16:41:23 -04:00
|
|
|
|
`((block-start . ,(rx symbol-start
|
2012-05-17 00:03:05 -03:00
|
|
|
|
(or "def" "class" "if" "elif" "else" "try"
|
|
|
|
|
"except" "finally" "for" "while" "with")
|
|
|
|
|
symbol-end))
|
2014-07-09 00:55:53 -03:00
|
|
|
|
(dedenter . ,(rx symbol-start
|
|
|
|
|
(or "elif" "else" "except" "finally")
|
|
|
|
|
symbol-end))
|
|
|
|
|
(block-ender . ,(rx symbol-start
|
|
|
|
|
(or
|
|
|
|
|
"break" "continue" "pass" "raise" "return")
|
|
|
|
|
symbol-end))
|
2012-06-18 16:41:23 -04:00
|
|
|
|
(decorator . ,(rx line-start (* space) ?@ (any letter ?_)
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(* (any word ?_))))
|
2012-06-18 16:41:23 -04:00
|
|
|
|
(defun . ,(rx symbol-start (or "def" "class") symbol-end))
|
|
|
|
|
(if-name-main . ,(rx line-start "if" (+ space) "__name__"
|
2012-05-17 00:03:41 -03:00
|
|
|
|
(+ space) "==" (+ space)
|
|
|
|
|
(any ?' ?\") "__main__" (any ?' ?\")
|
|
|
|
|
(* space) ?:))
|
2012-06-18 16:41:23 -04:00
|
|
|
|
(symbol-name . ,(rx (any letter ?_) (* (any word ?_))))
|
|
|
|
|
(open-paren . ,(rx (or "{" "[" "(")))
|
|
|
|
|
(close-paren . ,(rx (or "}" "]" ")")))
|
|
|
|
|
(simple-operator . ,(rx (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%)))
|
|
|
|
|
;; FIXME: rx should support (not simple-operator).
|
|
|
|
|
(not-simple-operator . ,(rx
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(not
|
|
|
|
|
(any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%))))
|
2012-06-18 16:41:23 -04:00
|
|
|
|
;; FIXME: Use regexp-opt.
|
|
|
|
|
(operator . ,(rx (or "+" "-" "/" "&" "^" "~" "|" "*" "<" ">"
|
2012-05-17 00:03:05 -03:00
|
|
|
|
"=" "%" "**" "//" "<<" ">>" "<=" "!="
|
|
|
|
|
"==" ">=" "is" "not")))
|
2012-06-18 16:41:23 -04:00
|
|
|
|
;; FIXME: Use regexp-opt.
|
|
|
|
|
(assignment-operator . ,(rx (or "=" "+=" "-=" "*=" "/=" "//=" "%=" "**="
|
2012-10-08 18:30:36 -03:00
|
|
|
|
">>=" "<<=" "&=" "^=" "|=")))
|
|
|
|
|
(string-delimiter . ,(rx (and
|
|
|
|
|
;; Match even number of backslashes.
|
|
|
|
|
(or (not (any ?\\ ?\' ?\")) point
|
|
|
|
|
;; Quotes might be preceded by a escaped quote.
|
|
|
|
|
(and (or (not (any ?\\)) point) ?\\
|
|
|
|
|
(* ?\\ ?\\) (any ?\' ?\")))
|
|
|
|
|
(* ?\\ ?\\)
|
|
|
|
|
;; Match single or triple quotes of any kind.
|
|
|
|
|
(group (or "\"" "\"\"\"" "'" "'''"))))))
|
|
|
|
|
"Additional Python specific sexps for `python-rx'")
|
|
|
|
|
|
|
|
|
|
(defmacro python-rx (&rest regexps)
|
|
|
|
|
"Python mode specialized rx macro.
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
This variant of `rx' supports common Python named REGEXPS."
|
2012-10-08 18:30:36 -03:00
|
|
|
|
(let ((rx-constituents (append python-rx-constituents rx-constituents)))
|
|
|
|
|
(cond ((null regexps)
|
|
|
|
|
(error "No regexp"))
|
|
|
|
|
((cdr regexps)
|
|
|
|
|
(rx-to-string `(and ,@regexps) t))
|
|
|
|
|
(t
|
|
|
|
|
(rx-to-string (car regexps) t))))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Font-lock and syntax
|
2012-08-07 23:30:08 -03:00
|
|
|
|
|
2013-04-16 23:52:50 -03:00
|
|
|
|
(eval-when-compile
|
|
|
|
|
(defun python-syntax--context-compiler-macro (form type &optional syntax-ppss)
|
|
|
|
|
(pcase type
|
|
|
|
|
(`'comment
|
|
|
|
|
`(let ((ppss (or ,syntax-ppss (syntax-ppss))))
|
|
|
|
|
(and (nth 4 ppss) (nth 8 ppss))))
|
|
|
|
|
(`'string
|
|
|
|
|
`(let ((ppss (or ,syntax-ppss (syntax-ppss))))
|
|
|
|
|
(and (nth 3 ppss) (nth 8 ppss))))
|
|
|
|
|
(`'paren
|
|
|
|
|
`(nth 1 (or ,syntax-ppss (syntax-ppss))))
|
|
|
|
|
(_ form))))
|
|
|
|
|
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(defun python-syntax-context (type &optional syntax-ppss)
|
|
|
|
|
"Return non-nil if point is on TYPE using SYNTAX-PPSS.
|
|
|
|
|
TYPE can be `comment', `string' or `paren'. It returns the start
|
|
|
|
|
character address of the specified TYPE."
|
2013-04-16 23:52:50 -03:00
|
|
|
|
(declare (compiler-macro python-syntax--context-compiler-macro))
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(let ((ppss (or syntax-ppss (syntax-ppss))))
|
2012-11-19 16:30:55 -05:00
|
|
|
|
(pcase type
|
|
|
|
|
(`comment (and (nth 4 ppss) (nth 8 ppss)))
|
|
|
|
|
(`string (and (nth 3 ppss) (nth 8 ppss)))
|
|
|
|
|
(`paren (nth 1 ppss))
|
|
|
|
|
(_ nil))))
|
2012-08-07 23:30:08 -03:00
|
|
|
|
|
|
|
|
|
(defun python-syntax-context-type (&optional syntax-ppss)
|
|
|
|
|
"Return the context type using SYNTAX-PPSS.
|
|
|
|
|
The type returned can be `comment', `string' or `paren'."
|
|
|
|
|
(let ((ppss (or syntax-ppss (syntax-ppss))))
|
|
|
|
|
(cond
|
|
|
|
|
((nth 8 ppss) (if (nth 4 ppss) 'comment 'string))
|
|
|
|
|
((nth 1 ppss) 'paren))))
|
|
|
|
|
|
|
|
|
|
(defsubst python-syntax-comment-or-string-p ()
|
|
|
|
|
"Return non-nil if point is inside 'comment or 'string."
|
|
|
|
|
(nth 8 (syntax-ppss)))
|
|
|
|
|
|
|
|
|
|
(define-obsolete-function-alias
|
2012-08-15 09:29:11 -07:00
|
|
|
|
'python-info-ppss-context #'python-syntax-context "24.3")
|
2012-08-07 23:30:08 -03:00
|
|
|
|
|
|
|
|
|
(define-obsolete-function-alias
|
2012-08-15 09:29:11 -07:00
|
|
|
|
'python-info-ppss-context-type #'python-syntax-context-type "24.3")
|
2012-08-07 23:30:08 -03:00
|
|
|
|
|
|
|
|
|
(define-obsolete-function-alias
|
|
|
|
|
'python-info-ppss-comment-or-string-p
|
2012-08-15 09:29:11 -07:00
|
|
|
|
#'python-syntax-comment-or-string-p "24.3")
|
2012-08-07 23:30:08 -03:00
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(defvar python-font-lock-keywords
|
|
|
|
|
;; Keywords
|
|
|
|
|
`(,(rx symbol-start
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(or
|
|
|
|
|
"and" "del" "from" "not" "while" "as" "elif" "global" "or" "with"
|
|
|
|
|
"assert" "else" "if" "pass" "yield" "break" "except" "import" "class"
|
|
|
|
|
"in" "raise" "continue" "finally" "is" "return" "def" "for" "lambda"
|
|
|
|
|
"try"
|
|
|
|
|
;; Python 2:
|
|
|
|
|
"print" "exec"
|
|
|
|
|
;; Python 3:
|
|
|
|
|
;; False, None, and True are listed as keywords on the Python 3
|
|
|
|
|
;; documentation, but since they also qualify as constants they are
|
|
|
|
|
;; fontified like that in order to keep font-lock consistent between
|
|
|
|
|
;; Python versions.
|
2012-05-17 00:03:35 -03:00
|
|
|
|
"nonlocal"
|
|
|
|
|
;; Extra:
|
|
|
|
|
"self")
|
2012-05-17 00:02:52 -03:00
|
|
|
|
symbol-end)
|
|
|
|
|
;; functions
|
|
|
|
|
(,(rx symbol-start "def" (1+ space) (group (1+ (or word ?_))))
|
|
|
|
|
(1 font-lock-function-name-face))
|
|
|
|
|
;; classes
|
|
|
|
|
(,(rx symbol-start "class" (1+ space) (group (1+ (or word ?_))))
|
|
|
|
|
(1 font-lock-type-face))
|
|
|
|
|
;; Constants
|
2012-05-17 00:03:19 -03:00
|
|
|
|
(,(rx symbol-start
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(or
|
|
|
|
|
"Ellipsis" "False" "None" "NotImplemented" "True" "__debug__"
|
|
|
|
|
;; copyright, license, credits, quit and exit are added by the site
|
|
|
|
|
;; module and they are not intended to be used in programs
|
|
|
|
|
"copyright" "credits" "exit" "license" "quit")
|
2012-05-17 00:03:19 -03:00
|
|
|
|
symbol-end) . font-lock-constant-face)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; Decorators.
|
|
|
|
|
(,(rx line-start (* (any " \t")) (group "@" (1+ (or word ?_))
|
|
|
|
|
(0+ "." (1+ (or word ?_)))))
|
|
|
|
|
(1 font-lock-type-face))
|
|
|
|
|
;; Builtin Exceptions
|
|
|
|
|
(,(rx symbol-start
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(or
|
|
|
|
|
"ArithmeticError" "AssertionError" "AttributeError" "BaseException"
|
|
|
|
|
"DeprecationWarning" "EOFError" "EnvironmentError" "Exception"
|
|
|
|
|
"FloatingPointError" "FutureWarning" "GeneratorExit" "IOError"
|
|
|
|
|
"ImportError" "ImportWarning" "IndexError" "KeyError"
|
|
|
|
|
"KeyboardInterrupt" "LookupError" "MemoryError" "NameError"
|
|
|
|
|
"NotImplementedError" "OSError" "OverflowError"
|
|
|
|
|
"PendingDeprecationWarning" "ReferenceError" "RuntimeError"
|
|
|
|
|
"RuntimeWarning" "StopIteration" "SyntaxError" "SyntaxWarning"
|
|
|
|
|
"SystemError" "SystemExit" "TypeError" "UnboundLocalError"
|
|
|
|
|
"UnicodeDecodeError" "UnicodeEncodeError" "UnicodeError"
|
|
|
|
|
"UnicodeTranslateError" "UnicodeWarning" "UserWarning" "VMSError"
|
|
|
|
|
"ValueError" "Warning" "WindowsError" "ZeroDivisionError"
|
|
|
|
|
;; Python 2:
|
|
|
|
|
"StandardError"
|
|
|
|
|
;; Python 3:
|
|
|
|
|
"BufferError" "BytesWarning" "IndentationError" "ResourceWarning"
|
|
|
|
|
"TabError")
|
2012-05-17 00:02:52 -03:00
|
|
|
|
symbol-end) . font-lock-type-face)
|
|
|
|
|
;; Builtins
|
2012-05-17 00:03:20 -03:00
|
|
|
|
(,(rx symbol-start
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(or
|
|
|
|
|
"abs" "all" "any" "bin" "bool" "callable" "chr" "classmethod"
|
|
|
|
|
"compile" "complex" "delattr" "dict" "dir" "divmod" "enumerate"
|
|
|
|
|
"eval" "filter" "float" "format" "frozenset" "getattr" "globals"
|
|
|
|
|
"hasattr" "hash" "help" "hex" "id" "input" "int" "isinstance"
|
|
|
|
|
"issubclass" "iter" "len" "list" "locals" "map" "max" "memoryview"
|
|
|
|
|
"min" "next" "object" "oct" "open" "ord" "pow" "print" "property"
|
|
|
|
|
"range" "repr" "reversed" "round" "set" "setattr" "slice" "sorted"
|
|
|
|
|
"staticmethod" "str" "sum" "super" "tuple" "type" "vars" "zip"
|
|
|
|
|
"__import__"
|
|
|
|
|
;; Python 2:
|
|
|
|
|
"basestring" "cmp" "execfile" "file" "long" "raw_input" "reduce"
|
|
|
|
|
"reload" "unichr" "unicode" "xrange" "apply" "buffer" "coerce"
|
|
|
|
|
"intern"
|
|
|
|
|
;; Python 3:
|
|
|
|
|
"ascii" "bytearray" "bytes" "exec"
|
|
|
|
|
;; Extra:
|
|
|
|
|
"__all__" "__doc__" "__name__" "__package__")
|
2012-05-17 00:03:20 -03:00
|
|
|
|
symbol-end) . font-lock-builtin-face)
|
2012-06-17 01:53:31 -07:00
|
|
|
|
;; assignments
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; support for a = b = c = 5
|
|
|
|
|
(,(lambda (limit)
|
2012-05-17 00:03:02 -03:00
|
|
|
|
(let ((re (python-rx (group (+ (any word ?. ?_)))
|
|
|
|
|
(? ?\[ (+ (not (any ?\]))) ?\]) (* space)
|
2013-08-26 22:41:41 -04:00
|
|
|
|
assignment-operator))
|
|
|
|
|
(res nil))
|
|
|
|
|
(while (and (setq res (re-search-forward re limit t))
|
|
|
|
|
(or (python-syntax-context 'paren)
|
|
|
|
|
(equal (char-after (point-marker)) ?=))))
|
|
|
|
|
res))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(1 font-lock-variable-name-face nil nil))
|
|
|
|
|
;; support for a, b, c = (1, 2, 3)
|
|
|
|
|
(,(lambda (limit)
|
|
|
|
|
(let ((re (python-rx (group (+ (any word ?. ?_))) (* space)
|
|
|
|
|
(* ?, (* space) (+ (any word ?. ?_)) (* space))
|
|
|
|
|
?, (* space) (+ (any word ?. ?_)) (* space)
|
2013-08-26 22:41:41 -04:00
|
|
|
|
assignment-operator))
|
|
|
|
|
(res nil))
|
|
|
|
|
(while (and (setq res (re-search-forward re limit t))
|
|
|
|
|
(goto-char (match-end 1))
|
|
|
|
|
(python-syntax-context 'paren)))
|
|
|
|
|
res))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(1 font-lock-variable-name-face nil nil))))
|
|
|
|
|
|
2012-05-17 00:03:46 -03:00
|
|
|
|
(defconst python-syntax-propertize-function
|
|
|
|
|
(syntax-propertize-rules
|
2012-10-08 18:30:36 -03:00
|
|
|
|
((python-rx string-delimiter)
|
2012-09-30 17:14:02 -03:00
|
|
|
|
(0 (ignore (python-syntax-stringify))))))
|
2012-09-24 14:54:46 -03:00
|
|
|
|
|
|
|
|
|
(defsubst python-syntax-count-quotes (quote-char &optional point limit)
|
|
|
|
|
"Count number of quotes around point (max is 3).
|
|
|
|
|
QUOTE-CHAR is the quote char to count. Optional argument POINT is
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
the point where scan starts (defaults to current point), and LIMIT
|
2012-09-24 14:54:46 -03:00
|
|
|
|
is used to limit the scan."
|
|
|
|
|
(let ((i 0))
|
|
|
|
|
(while (and (< i 3)
|
|
|
|
|
(or (not limit) (< (+ point i) limit))
|
|
|
|
|
(eq (char-after (+ point i)) quote-char))
|
2012-12-29 08:04:55 -03:00
|
|
|
|
(setq i (1+ i)))
|
2012-09-24 14:54:46 -03:00
|
|
|
|
i))
|
|
|
|
|
|
|
|
|
|
(defun python-syntax-stringify ()
|
|
|
|
|
"Put `syntax-table' property correctly on single/triple quotes."
|
2012-10-03 18:53:09 -03:00
|
|
|
|
(let* ((num-quotes (length (match-string-no-properties 1)))
|
2012-09-24 14:54:46 -03:00
|
|
|
|
(ppss (prog2
|
|
|
|
|
(backward-char num-quotes)
|
|
|
|
|
(syntax-ppss)
|
|
|
|
|
(forward-char num-quotes)))
|
|
|
|
|
(string-start (and (not (nth 4 ppss)) (nth 8 ppss)))
|
|
|
|
|
(quote-starting-pos (- (point) num-quotes))
|
|
|
|
|
(quote-ending-pos (point))
|
|
|
|
|
(num-closing-quotes
|
|
|
|
|
(and string-start
|
|
|
|
|
(python-syntax-count-quotes
|
|
|
|
|
(char-before) string-start quote-starting-pos))))
|
|
|
|
|
(cond ((and string-start (= num-closing-quotes 0))
|
|
|
|
|
;; This set of quotes doesn't match the string starting
|
|
|
|
|
;; kind. Do nothing.
|
|
|
|
|
nil)
|
|
|
|
|
((not string-start)
|
|
|
|
|
;; This set of quotes delimit the start of a string.
|
|
|
|
|
(put-text-property quote-starting-pos (1+ quote-starting-pos)
|
|
|
|
|
'syntax-table (string-to-syntax "|")))
|
|
|
|
|
((= num-quotes num-closing-quotes)
|
|
|
|
|
;; This set of quotes delimit the end of a string.
|
|
|
|
|
(put-text-property (1- quote-ending-pos) quote-ending-pos
|
|
|
|
|
'syntax-table (string-to-syntax "|")))
|
|
|
|
|
((> num-quotes num-closing-quotes)
|
|
|
|
|
;; This may only happen whenever a triple quote is closing
|
|
|
|
|
;; a single quoted string. Add string delimiter syntax to
|
|
|
|
|
;; all three quotes.
|
|
|
|
|
(put-text-property quote-starting-pos quote-ending-pos
|
|
|
|
|
'syntax-table (string-to-syntax "|"))))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defvar python-mode-syntax-table
|
|
|
|
|
(let ((table (make-syntax-table)))
|
|
|
|
|
;; Give punctuation syntax to ASCII that normally has symbol
|
|
|
|
|
;; syntax or has word syntax and isn't a letter.
|
|
|
|
|
(let ((symbol (string-to-syntax "_"))
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(sst (standard-syntax-table)))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(dotimes (i 128)
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(unless (= i ?_)
|
|
|
|
|
(if (equal symbol (aref sst i))
|
|
|
|
|
(modify-syntax-entry i "." table)))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(modify-syntax-entry ?$ "." table)
|
|
|
|
|
(modify-syntax-entry ?% "." table)
|
|
|
|
|
;; exceptions
|
|
|
|
|
(modify-syntax-entry ?# "<" table)
|
|
|
|
|
(modify-syntax-entry ?\n ">" table)
|
|
|
|
|
(modify-syntax-entry ?' "\"" table)
|
|
|
|
|
(modify-syntax-entry ?` "$" table)
|
|
|
|
|
table)
|
|
|
|
|
"Syntax table for Python files.")
|
|
|
|
|
|
|
|
|
|
(defvar python-dotty-syntax-table
|
|
|
|
|
(let ((table (make-syntax-table python-mode-syntax-table)))
|
|
|
|
|
(modify-syntax-entry ?. "w" table)
|
|
|
|
|
(modify-syntax-entry ?_ "w" table)
|
|
|
|
|
table)
|
|
|
|
|
"Dotty syntax table for Python files.
|
|
|
|
|
It makes underscores and dots word constituent chars.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Indentation
|
|
|
|
|
|
|
|
|
|
(defcustom python-indent-offset 4
|
|
|
|
|
"Default indentation offset for Python."
|
|
|
|
|
:group 'python
|
|
|
|
|
:type 'integer
|
|
|
|
|
:safe 'integerp)
|
|
|
|
|
|
|
|
|
|
(defcustom python-indent-guess-indent-offset t
|
|
|
|
|
"Non-nil tells Python mode to guess `python-indent-offset' value."
|
|
|
|
|
:type 'boolean
|
2012-05-17 00:03:23 -03:00
|
|
|
|
:group 'python
|
|
|
|
|
:safe 'booleanp)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-12-29 09:57:49 -03:00
|
|
|
|
(defcustom python-indent-trigger-commands
|
|
|
|
|
'(indent-for-tab-command yas-expand yas/expand)
|
|
|
|
|
"Commands that might trigger a `python-indent-line' call."
|
|
|
|
|
:type '(repeat symbol)
|
|
|
|
|
:group 'python)
|
|
|
|
|
|
2012-06-14 22:33:57 -03:00
|
|
|
|
(define-obsolete-variable-alias
|
2012-08-15 09:29:11 -07:00
|
|
|
|
'python-indent 'python-indent-offset "24.3")
|
2012-06-14 22:33:57 -03:00
|
|
|
|
|
|
|
|
|
(define-obsolete-variable-alias
|
2012-08-15 09:29:11 -07:00
|
|
|
|
'python-guess-indent 'python-indent-guess-indent-offset "24.3")
|
2012-06-14 22:33:57 -03:00
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(defvar python-indent-current-level 0
|
|
|
|
|
"Current indentation level `python-indent-line-function' is using.")
|
|
|
|
|
|
|
|
|
|
(defvar python-indent-levels '(0)
|
|
|
|
|
"Levels of indentation available for `python-indent-line-function'.")
|
|
|
|
|
|
|
|
|
|
(defun python-indent-guess-indent-offset ()
|
2012-05-17 00:02:53 -03:00
|
|
|
|
"Guess and set `python-indent-offset' for the current buffer."
|
2012-06-14 22:33:57 -03:00
|
|
|
|
(interactive)
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(save-excursion
|
|
|
|
|
(save-restriction
|
|
|
|
|
(widen)
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(let ((block-end))
|
|
|
|
|
(while (and (not block-end)
|
|
|
|
|
(re-search-forward
|
|
|
|
|
(python-rx line-start block-start) nil t))
|
|
|
|
|
(when (and
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(not (python-syntax-context-type))
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(progn
|
|
|
|
|
(goto-char (line-end-position))
|
|
|
|
|
(python-util-forward-comment -1)
|
|
|
|
|
(if (equal (char-before) ?:)
|
|
|
|
|
t
|
|
|
|
|
(forward-line 1)
|
|
|
|
|
(when (python-info-block-continuation-line-p)
|
|
|
|
|
(while (and (python-info-continuation-line-p)
|
|
|
|
|
(not (eobp)))
|
|
|
|
|
(forward-line 1))
|
|
|
|
|
(python-util-forward-comment -1)
|
|
|
|
|
(when (equal (char-before) ?:)
|
|
|
|
|
t)))))
|
|
|
|
|
(setq block-end (point-marker))))
|
|
|
|
|
(let ((indentation
|
|
|
|
|
(when block-end
|
|
|
|
|
(goto-char block-end)
|
|
|
|
|
(python-util-forward-comment)
|
|
|
|
|
(current-indentation))))
|
2013-11-25 22:15:49 -05:00
|
|
|
|
(if (and indentation (not (zerop indentation)))
|
2012-11-26 20:31:06 -03:00
|
|
|
|
(set (make-local-variable 'python-indent-offset) indentation)
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(message "Can't guess python-indent-offset, using defaults: %s"
|
|
|
|
|
python-indent-offset)))))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:09 -03:00
|
|
|
|
(defun python-indent-context ()
|
|
|
|
|
"Get information on indentation context.
|
|
|
|
|
Context information is returned with a cons with the form:
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
(STATUS . START)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
Where status can be any of the following symbols:
|
2013-12-12 02:37:09 -03:00
|
|
|
|
|
|
|
|
|
* after-comment: When current line might continue a comment block
|
2012-05-17 00:02:52 -03:00
|
|
|
|
* inside-paren: If point in between (), {} or []
|
|
|
|
|
* inside-string: If point is inside a string
|
|
|
|
|
* after-backslash: Previous line ends in a backslash
|
|
|
|
|
* after-beginning-of-block: Point is after beginning of block
|
|
|
|
|
* after-line: Point is after normal line
|
2014-07-09 00:55:53 -03:00
|
|
|
|
* dedenter-statement: Point is on a dedenter statement.
|
2012-05-17 00:02:52 -03:00
|
|
|
|
* no-indent: Point is at beginning of buffer or other special case
|
|
|
|
|
START is the buffer position where the sexp starts."
|
|
|
|
|
(save-restriction
|
|
|
|
|
(widen)
|
|
|
|
|
(let ((ppss (save-excursion (beginning-of-line) (syntax-ppss)))
|
|
|
|
|
(start))
|
|
|
|
|
(cons
|
|
|
|
|
(cond
|
2012-05-17 00:02:55 -03:00
|
|
|
|
;; Beginning of buffer
|
2012-05-17 00:02:56 -03:00
|
|
|
|
((save-excursion
|
|
|
|
|
(goto-char (line-beginning-position))
|
|
|
|
|
(bobp))
|
2012-05-17 00:02:55 -03:00
|
|
|
|
'no-indent)
|
2013-12-12 02:37:09 -03:00
|
|
|
|
;; Comment continuation
|
|
|
|
|
((save-excursion
|
|
|
|
|
(when (and
|
|
|
|
|
(or
|
|
|
|
|
(python-info-current-line-comment-p)
|
|
|
|
|
(python-info-current-line-empty-p))
|
|
|
|
|
(progn
|
|
|
|
|
(forward-comment -1)
|
|
|
|
|
(python-info-current-line-comment-p)))
|
|
|
|
|
(setq start (point))
|
|
|
|
|
'after-comment)))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; Inside string
|
2012-08-07 23:30:08 -03:00
|
|
|
|
((setq start (python-syntax-context 'string ppss))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
'inside-string)
|
2012-10-07 13:13:52 -03:00
|
|
|
|
;; Inside a paren
|
|
|
|
|
((setq start (python-syntax-context 'paren ppss))
|
|
|
|
|
'inside-paren)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; After backslash
|
2012-08-07 23:30:08 -03:00
|
|
|
|
((setq start (when (not (or (python-syntax-context 'string ppss)
|
|
|
|
|
(python-syntax-context 'comment ppss)))
|
2013-02-19 00:18:32 -03:00
|
|
|
|
(let ((line-beg-pos (line-number-at-pos)))
|
|
|
|
|
(python-info-line-ends-backslash-p
|
|
|
|
|
(1- line-beg-pos)))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
'after-backslash)
|
|
|
|
|
;; After beginning of block
|
|
|
|
|
((setq start (save-excursion
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(when (progn
|
|
|
|
|
(back-to-indentation)
|
|
|
|
|
(python-util-forward-comment -1)
|
|
|
|
|
(equal (char-before) ?:))
|
|
|
|
|
;; Move to the first block start that's not in within
|
|
|
|
|
;; a string, comment or paren and that's not a
|
|
|
|
|
;; continuation line.
|
|
|
|
|
(while (and (re-search-backward
|
|
|
|
|
(python-rx block-start) nil t)
|
|
|
|
|
(or
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(python-syntax-context-type)
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(python-info-continuation-line-p))))
|
|
|
|
|
(when (looking-at (python-rx block-start))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(point-marker)))))
|
|
|
|
|
'after-beginning-of-block)
|
2014-07-09 00:55:53 -03:00
|
|
|
|
((when (setq start (python-info-dedenter-statement-p))
|
|
|
|
|
'dedenter-statement))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; After normal line
|
|
|
|
|
((setq start (save-excursion
|
2012-05-17 00:03:22 -03:00
|
|
|
|
(back-to-indentation)
|
2012-10-07 13:13:52 -03:00
|
|
|
|
(skip-chars-backward (rx (or whitespace ?\n)))
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(python-nav-beginning-of-statement)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(point-marker)))
|
|
|
|
|
'after-line)
|
|
|
|
|
;; Do not indent
|
|
|
|
|
(t 'no-indent))
|
|
|
|
|
start))))
|
|
|
|
|
|
|
|
|
|
(defun python-indent-calculate-indentation ()
|
|
|
|
|
"Calculate correct indentation offset for the current line."
|
|
|
|
|
(let* ((indentation-context (python-indent-context))
|
|
|
|
|
(context-status (car indentation-context))
|
|
|
|
|
(context-start (cdr indentation-context)))
|
|
|
|
|
(save-restriction
|
|
|
|
|
(widen)
|
|
|
|
|
(save-excursion
|
2012-11-19 16:30:55 -05:00
|
|
|
|
(pcase context-status
|
|
|
|
|
(`no-indent 0)
|
2013-12-12 02:37:09 -03:00
|
|
|
|
(`after-comment
|
|
|
|
|
(goto-char context-start)
|
|
|
|
|
(current-indentation))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
;; When point is after beginning of block just add one level
|
|
|
|
|
;; of indentation relative to the context-start
|
2012-11-19 16:30:55 -05:00
|
|
|
|
(`after-beginning-of-block
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(goto-char context-start)
|
|
|
|
|
(+ (current-indentation) python-indent-offset))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
;; When after a simple line just use previous line
|
2014-07-09 00:55:53 -03:00
|
|
|
|
;; indentation.
|
2012-11-19 16:30:55 -05:00
|
|
|
|
(`after-line
|
2013-12-12 20:32:05 -03:00
|
|
|
|
(let* ((pair (save-excursion
|
|
|
|
|
(goto-char context-start)
|
|
|
|
|
(cons
|
|
|
|
|
(current-indentation)
|
|
|
|
|
(python-info-beginning-of-block-p))))
|
|
|
|
|
(context-indentation (car pair))
|
2014-07-09 00:55:53 -03:00
|
|
|
|
;; TODO: Separate block enders into its own case.
|
2013-12-12 20:32:05 -03:00
|
|
|
|
(adjustment
|
2014-07-09 00:55:53 -03:00
|
|
|
|
(if (save-excursion
|
|
|
|
|
(python-util-forward-comment -1)
|
|
|
|
|
(python-nav-beginning-of-statement)
|
|
|
|
|
(looking-at (python-rx block-ender)))
|
2013-12-12 20:32:05 -03:00
|
|
|
|
python-indent-offset
|
|
|
|
|
0)))
|
|
|
|
|
(- context-indentation adjustment)))
|
2014-07-09 00:55:53 -03:00
|
|
|
|
;; When point is on a dedenter statement, search for the
|
|
|
|
|
;; opening block that corresponds to it and use its
|
|
|
|
|
;; indentation. If no opening block is found just remove
|
|
|
|
|
;; indentation as this is an invalid python file.
|
|
|
|
|
(`dedenter-statement
|
|
|
|
|
(let ((block-start-point
|
|
|
|
|
(python-info-dedenter-opening-block-position)))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(if (not block-start-point)
|
|
|
|
|
0
|
|
|
|
|
(goto-char block-start-point)
|
|
|
|
|
(current-indentation)))))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
;; When inside of a string, do nothing. just use the current
|
|
|
|
|
;; indentation. XXX: perhaps it would be a good idea to
|
|
|
|
|
;; invoke standard text indentation here
|
2012-11-19 16:30:55 -05:00
|
|
|
|
(`inside-string
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(goto-char context-start)
|
|
|
|
|
(current-indentation))
|
2012-06-17 01:53:31 -07:00
|
|
|
|
;; After backslash we have several possibilities.
|
2012-11-19 16:30:55 -05:00
|
|
|
|
(`after-backslash
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(cond
|
|
|
|
|
;; Check if current line is a dot continuation. For this
|
|
|
|
|
;; the current line must start with a dot and previous
|
|
|
|
|
;; line must contain a dot too.
|
|
|
|
|
((save-excursion
|
|
|
|
|
(back-to-indentation)
|
|
|
|
|
(when (looking-at "\\.")
|
2012-05-17 00:03:36 -03:00
|
|
|
|
;; If after moving one line back point is inside a paren it
|
|
|
|
|
;; needs to move back until it's not anymore
|
|
|
|
|
(while (prog2
|
|
|
|
|
(forward-line -1)
|
|
|
|
|
(and (not (bobp))
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(python-syntax-context 'paren))))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(goto-char (line-end-position))
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(while (and (re-search-backward
|
|
|
|
|
"\\." (line-beginning-position) t)
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(python-syntax-context-type)))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(if (and (looking-at "\\.")
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(not (python-syntax-context-type)))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
;; The indentation is the same column of the
|
|
|
|
|
;; first matching dot that's not inside a
|
|
|
|
|
;; comment, a string or a paren
|
|
|
|
|
(current-column)
|
|
|
|
|
;; No dot found on previous line, just add another
|
|
|
|
|
;; indentation level.
|
|
|
|
|
(+ (current-indentation) python-indent-offset)))))
|
|
|
|
|
;; Check if prev line is a block continuation
|
|
|
|
|
((let ((block-continuation-start
|
|
|
|
|
(python-info-block-continuation-line-p)))
|
|
|
|
|
(when block-continuation-start
|
|
|
|
|
;; If block-continuation-start is set jump to that
|
|
|
|
|
;; marker and use first column after the block start
|
|
|
|
|
;; as indentation value.
|
|
|
|
|
(goto-char block-continuation-start)
|
|
|
|
|
(re-search-forward
|
|
|
|
|
(python-rx block-start (* space))
|
|
|
|
|
(line-end-position) t)
|
|
|
|
|
(current-column))))
|
|
|
|
|
;; Check if current line is an assignment continuation
|
|
|
|
|
((let ((assignment-continuation-start
|
|
|
|
|
(python-info-assignment-continuation-line-p)))
|
|
|
|
|
(when assignment-continuation-start
|
|
|
|
|
;; If assignment-continuation is set jump to that
|
|
|
|
|
;; marker and use first column after the assignment
|
|
|
|
|
;; operator as indentation value.
|
|
|
|
|
(goto-char assignment-continuation-start)
|
|
|
|
|
(current-column))))
|
|
|
|
|
(t
|
|
|
|
|
(forward-line -1)
|
2012-06-17 01:53:31 -07:00
|
|
|
|
(goto-char (python-info-beginning-of-backslash))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(if (save-excursion
|
|
|
|
|
(and
|
|
|
|
|
(forward-line -1)
|
2012-05-17 00:03:36 -03:00
|
|
|
|
(goto-char
|
2012-06-17 01:53:31 -07:00
|
|
|
|
(or (python-info-beginning-of-backslash) (point)))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(python-info-line-ends-backslash-p)))
|
|
|
|
|
;; The two previous lines ended in a backslash so we must
|
|
|
|
|
;; respect previous line indentation.
|
|
|
|
|
(current-indentation)
|
|
|
|
|
;; What happens here is that we are dealing with the second
|
|
|
|
|
;; line of a backslash continuation, in that case we just going
|
|
|
|
|
;; to add one indentation level.
|
|
|
|
|
(+ (current-indentation) python-indent-offset)))))
|
|
|
|
|
;; When inside a paren there's a need to handle nesting
|
|
|
|
|
;; correctly
|
2012-11-19 16:30:55 -05:00
|
|
|
|
(`inside-paren
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(cond
|
2012-06-17 01:53:31 -07:00
|
|
|
|
;; If current line closes the outermost open paren use the
|
2012-05-17 00:03:34 -03:00
|
|
|
|
;; current indentation of the context-start line.
|
|
|
|
|
((save-excursion
|
|
|
|
|
(skip-syntax-forward "\s" (line-end-position))
|
|
|
|
|
(when (and (looking-at (regexp-opt '(")" "]" "}")))
|
|
|
|
|
(progn
|
|
|
|
|
(forward-char 1)
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(not (python-syntax-context 'paren))))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(goto-char context-start)
|
|
|
|
|
(current-indentation))))
|
|
|
|
|
;; If open paren is contained on a line by itself add another
|
|
|
|
|
;; indentation level, else look for the first word after the
|
|
|
|
|
;; opening paren and use it's column position as indentation
|
|
|
|
|
;; level.
|
|
|
|
|
((let* ((content-starts-in-newline)
|
|
|
|
|
(indent
|
|
|
|
|
(save-excursion
|
|
|
|
|
(if (setq content-starts-in-newline
|
|
|
|
|
(progn
|
|
|
|
|
(goto-char context-start)
|
|
|
|
|
(forward-char)
|
|
|
|
|
(save-restriction
|
|
|
|
|
(narrow-to-region
|
|
|
|
|
(line-beginning-position)
|
|
|
|
|
(line-end-position))
|
|
|
|
|
(python-util-forward-comment))
|
|
|
|
|
(looking-at "$")))
|
|
|
|
|
(+ (current-indentation) python-indent-offset)
|
|
|
|
|
(current-column)))))
|
|
|
|
|
;; Adjustments
|
|
|
|
|
(cond
|
|
|
|
|
;; If current line closes a nested open paren de-indent one
|
|
|
|
|
;; level.
|
|
|
|
|
((progn
|
2012-05-17 00:03:03 -03:00
|
|
|
|
(back-to-indentation)
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(looking-at (regexp-opt '(")" "]" "}"))))
|
|
|
|
|
(- indent python-indent-offset))
|
|
|
|
|
;; If the line of the opening paren that wraps the current
|
|
|
|
|
;; line starts a block add another level of indentation to
|
|
|
|
|
;; follow new pep8 recommendation. See: http://ur1.ca/5rojx
|
|
|
|
|
((save-excursion
|
|
|
|
|
(when (and content-starts-in-newline
|
|
|
|
|
(progn
|
|
|
|
|
(goto-char context-start)
|
|
|
|
|
(back-to-indentation)
|
|
|
|
|
(looking-at (python-rx block-start))))
|
|
|
|
|
(+ indent python-indent-offset))))
|
|
|
|
|
(t indent)))))))))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defun python-indent-calculate-levels ()
|
|
|
|
|
"Calculate `python-indent-levels' and reset `python-indent-current-level'."
|
2014-07-09 00:55:53 -03:00
|
|
|
|
(if (not (python-info-dedenter-statement-p))
|
|
|
|
|
(let* ((indentation (python-indent-calculate-indentation))
|
|
|
|
|
(remainder (% indentation python-indent-offset))
|
|
|
|
|
(steps (/ (- indentation remainder) python-indent-offset)))
|
|
|
|
|
(setq python-indent-levels (list 0))
|
|
|
|
|
(dotimes (step steps)
|
|
|
|
|
(push (* python-indent-offset (1+ step)) python-indent-levels))
|
|
|
|
|
(when (not (eq 0 remainder))
|
|
|
|
|
(push (+ (* python-indent-offset steps) remainder) python-indent-levels)))
|
|
|
|
|
(setq python-indent-levels
|
|
|
|
|
(or
|
|
|
|
|
(mapcar (lambda (pos)
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char pos)
|
|
|
|
|
(current-indentation)))
|
|
|
|
|
(python-info-dedenter-opening-block-positions))
|
|
|
|
|
(list 0))))
|
|
|
|
|
(setq python-indent-current-level (1- (length python-indent-levels))
|
|
|
|
|
python-indent-levels (nreverse python-indent-levels)))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defun python-indent-toggle-levels ()
|
|
|
|
|
"Toggle `python-indent-current-level' over `python-indent-levels'."
|
|
|
|
|
(setq python-indent-current-level (1- python-indent-current-level))
|
|
|
|
|
(when (< python-indent-current-level 0)
|
|
|
|
|
(setq python-indent-current-level (1- (length python-indent-levels)))))
|
|
|
|
|
|
|
|
|
|
(defun python-indent-line (&optional force-toggle)
|
|
|
|
|
"Internal implementation of `python-indent-line-function'.
|
|
|
|
|
Uses the offset calculated in
|
|
|
|
|
`python-indent-calculate-indentation' and available levels
|
2012-05-17 00:03:09 -03:00
|
|
|
|
indicated by the variable `python-indent-levels' to set the
|
|
|
|
|
current indentation.
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-12-29 09:57:49 -03:00
|
|
|
|
When the variable `last-command' is equal to one of the symbols
|
|
|
|
|
inside `python-indent-trigger-commands' or FORCE-TOGGLE is
|
|
|
|
|
non-nil it cycles levels indicated in the variable
|
|
|
|
|
`python-indent-levels' by setting the current level in the
|
|
|
|
|
variable `python-indent-current-level'.
|
|
|
|
|
|
|
|
|
|
When the variable `last-command' is not equal to one of the
|
|
|
|
|
symbols inside `python-indent-trigger-commands' and FORCE-TOGGLE
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
is nil it calculates possible indentation levels and saves them
|
|
|
|
|
in the variable `python-indent-levels'. Afterwards it sets the
|
2012-12-29 09:57:49 -03:00
|
|
|
|
variable `python-indent-current-level' correctly so offset is
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
equal to
|
|
|
|
|
(nth python-indent-current-level python-indent-levels)"
|
2012-09-23 15:21:33 -03:00
|
|
|
|
(or
|
2012-12-29 09:57:49 -03:00
|
|
|
|
(and (or (and (memq this-command python-indent-trigger-commands)
|
2012-09-23 15:21:33 -03:00
|
|
|
|
(eq last-command this-command))
|
|
|
|
|
force-toggle)
|
|
|
|
|
(not (equal python-indent-levels '(0)))
|
|
|
|
|
(or (python-indent-toggle-levels) t))
|
|
|
|
|
(python-indent-calculate-levels))
|
|
|
|
|
(let* ((starting-pos (point-marker))
|
|
|
|
|
(indent-ending-position
|
|
|
|
|
(+ (line-beginning-position) (current-indentation)))
|
|
|
|
|
(follow-indentation-p
|
|
|
|
|
(or (bolp)
|
|
|
|
|
(and (<= (line-beginning-position) starting-pos)
|
|
|
|
|
(>= indent-ending-position starting-pos))))
|
|
|
|
|
(next-indent (nth python-indent-current-level python-indent-levels)))
|
|
|
|
|
(unless (= next-indent (current-indentation))
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(delete-horizontal-space)
|
|
|
|
|
(indent-to next-indent)
|
|
|
|
|
(goto-char starting-pos))
|
|
|
|
|
(and follow-indentation-p (back-to-indentation)))
|
2014-07-09 00:55:53 -03:00
|
|
|
|
(python-info-dedenter-opening-block-message))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defun python-indent-line-function ()
|
|
|
|
|
"`indent-line-function' for Python mode.
|
2012-05-17 00:03:09 -03:00
|
|
|
|
See `python-indent-line' for details."
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(python-indent-line))
|
|
|
|
|
|
|
|
|
|
(defun python-indent-dedent-line ()
|
2012-05-17 00:03:09 -03:00
|
|
|
|
"De-indent current line."
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(interactive "*")
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(when (and (not (python-syntax-comment-or-string-p))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(<= (point-marker) (save-excursion
|
|
|
|
|
(back-to-indentation)
|
|
|
|
|
(point-marker)))
|
|
|
|
|
(> (current-column) 0))
|
|
|
|
|
(python-indent-line t)
|
|
|
|
|
t))
|
|
|
|
|
|
|
|
|
|
(defun python-indent-dedent-line-backspace (arg)
|
2012-05-17 00:03:09 -03:00
|
|
|
|
"De-indent current line.
|
2012-05-17 00:02:52 -03:00
|
|
|
|
Argument ARG is passed to `backward-delete-char-untabify' when
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
point is not in between the indentation."
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(interactive "*p")
|
|
|
|
|
(when (not (python-indent-dedent-line))
|
|
|
|
|
(backward-delete-char-untabify arg)))
|
2012-05-17 00:02:53 -03:00
|
|
|
|
(put 'python-indent-dedent-line-backspace 'delete-selection 'supersede)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defun python-indent-region (start end)
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Indent a Python region automagically.
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
Called from a program, START and END specify the region to indent."
|
2012-05-17 00:03:04 -03:00
|
|
|
|
(let ((deactivate-mark nil))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char end)
|
|
|
|
|
(setq end (point-marker))
|
|
|
|
|
(goto-char start)
|
|
|
|
|
(or (bolp) (forward-line 1))
|
|
|
|
|
(while (< (point) end)
|
|
|
|
|
(or (and (bolp) (eolp))
|
|
|
|
|
(let (word)
|
|
|
|
|
(forward-line -1)
|
|
|
|
|
(back-to-indentation)
|
|
|
|
|
(setq word (current-word))
|
|
|
|
|
(forward-line 1)
|
2012-10-07 13:13:52 -03:00
|
|
|
|
(when (and word
|
|
|
|
|
;; Don't mess with strings, unless it's the
|
|
|
|
|
;; enclosing set of quotes.
|
|
|
|
|
(or (not (python-syntax-context 'string))
|
|
|
|
|
(eq
|
|
|
|
|
(syntax-after
|
|
|
|
|
(+ (1- (point))
|
|
|
|
|
(current-indentation)
|
|
|
|
|
(python-syntax-count-quotes (char-after) (point))))
|
|
|
|
|
(string-to-syntax "|"))))
|
2012-05-17 00:03:04 -03:00
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(delete-horizontal-space)
|
|
|
|
|
(indent-to (python-indent-calculate-indentation)))))
|
|
|
|
|
(forward-line 1))
|
|
|
|
|
(move-marker end nil))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defun python-indent-shift-left (start end &optional count)
|
|
|
|
|
"Shift lines contained in region START END by COUNT columns to the left.
|
2012-05-17 00:03:09 -03:00
|
|
|
|
COUNT defaults to `python-indent-offset'. If region isn't
|
|
|
|
|
active, the current line is shifted. The shifted region includes
|
|
|
|
|
the lines in which START and END lie. An error is signaled if
|
|
|
|
|
any lines in the region are indented less than COUNT columns."
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(interactive
|
|
|
|
|
(if mark-active
|
|
|
|
|
(list (region-beginning) (region-end) current-prefix-arg)
|
|
|
|
|
(list (line-beginning-position) (line-end-position) current-prefix-arg)))
|
|
|
|
|
(if count
|
|
|
|
|
(setq count (prefix-numeric-value count))
|
|
|
|
|
(setq count python-indent-offset))
|
|
|
|
|
(when (> count 0)
|
2012-05-17 00:03:04 -03:00
|
|
|
|
(let ((deactivate-mark nil))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char start)
|
|
|
|
|
(while (< (point) end)
|
|
|
|
|
(if (and (< (current-indentation) count)
|
|
|
|
|
(not (looking-at "[ \t]*$")))
|
|
|
|
|
(error "Can't shift all lines enough"))
|
|
|
|
|
(forward-line))
|
|
|
|
|
(indent-rigidly start end (- count))))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(add-to-list 'debug-ignored-errors "^Can't shift all lines enough")
|
|
|
|
|
|
|
|
|
|
(defun python-indent-shift-right (start end &optional count)
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Shift lines contained in region START END by COUNT columns to the right.
|
2012-05-17 00:03:09 -03:00
|
|
|
|
COUNT defaults to `python-indent-offset'. If region isn't
|
|
|
|
|
active, the current line is shifted. The shifted region includes
|
|
|
|
|
the lines in which START and END lie."
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(interactive
|
|
|
|
|
(if mark-active
|
|
|
|
|
(list (region-beginning) (region-end) current-prefix-arg)
|
|
|
|
|
(list (line-beginning-position) (line-end-position) current-prefix-arg)))
|
2012-05-17 00:03:04 -03:00
|
|
|
|
(let ((deactivate-mark nil))
|
2013-11-28 21:03:39 -05:00
|
|
|
|
(setq count (if count (prefix-numeric-value count)
|
|
|
|
|
python-indent-offset))
|
2012-05-17 00:03:04 -03:00
|
|
|
|
(indent-rigidly start end count)))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:42 -03:00
|
|
|
|
(defun python-indent-post-self-insert-function ()
|
2013-11-28 21:03:39 -05:00
|
|
|
|
"Adjust indentation after insertion of some characters.
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
This function is intended to be added to `post-self-insert-hook.'
|
|
|
|
|
If a line renders a paren alone, after adding a char before it,
|
|
|
|
|
the line will be re-indented automatically if needed."
|
2013-11-28 21:03:39 -05:00
|
|
|
|
(when (and electric-indent-mode
|
|
|
|
|
(eq (char-before) last-command-event))
|
|
|
|
|
(cond
|
2014-07-01 00:54:11 -03:00
|
|
|
|
;; Electric indent inside parens
|
|
|
|
|
((and
|
|
|
|
|
(not (bolp))
|
|
|
|
|
(let ((paren-start (python-syntax-context 'paren)))
|
|
|
|
|
;; Check that point is inside parens.
|
|
|
|
|
(when paren-start
|
|
|
|
|
(not
|
|
|
|
|
;; Filter the case where input is happening in the same
|
|
|
|
|
;; line where the open paren is.
|
|
|
|
|
(= (line-number-at-pos)
|
|
|
|
|
(line-number-at-pos paren-start)))))
|
|
|
|
|
;; When content has been added before the closing paren or a
|
|
|
|
|
;; comma has been inserted, it's ok to do the trick.
|
|
|
|
|
(or
|
|
|
|
|
(memq (char-after) '(?\) ?\] ?\}))
|
|
|
|
|
(eq (char-before) ?,)))
|
2013-11-28 21:03:39 -05:00
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char (line-beginning-position))
|
2014-07-01 00:54:11 -03:00
|
|
|
|
(let ((indentation (python-indent-calculate-indentation)))
|
|
|
|
|
(when (< (current-indentation) indentation)
|
|
|
|
|
(indent-line-to indentation)))))
|
|
|
|
|
;; Electric colon
|
2013-11-28 21:03:39 -05:00
|
|
|
|
((and (eq ?: last-command-event)
|
|
|
|
|
(memq ?: electric-indent-chars)
|
|
|
|
|
(not current-prefix-arg)
|
|
|
|
|
(eolp)
|
|
|
|
|
(not (equal ?: (char-before (1- (point)))))
|
|
|
|
|
(not (python-syntax-comment-or-string-p)))
|
2014-07-09 00:55:53 -03:00
|
|
|
|
(python-indent-line)))))
|
2012-05-17 00:03:42 -03:00
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
;;; Navigation
|
|
|
|
|
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(defvar python-nav-beginning-of-defun-regexp
|
2012-05-17 00:03:13 -03:00
|
|
|
|
(python-rx line-start (* space) defun (+ space) (group symbol-name))
|
2012-05-17 00:03:25 -03:00
|
|
|
|
"Regexp matching class or function definition.
|
2012-05-17 00:03:14 -03:00
|
|
|
|
The name of the defun should be grouped so it can be retrieved
|
|
|
|
|
via `match-string'.")
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-11-12 10:26:50 -03:00
|
|
|
|
(defun python-nav--beginning-of-defun (&optional arg)
|
|
|
|
|
"Internal implementation of `python-nav-beginning-of-defun'.
|
|
|
|
|
With positive ARG search backwards, else search forwards."
|
2012-05-17 00:03:44 -03:00
|
|
|
|
(when (or (null arg) (= arg 0)) (setq arg 1))
|
|
|
|
|
(let* ((re-search-fn (if (> arg 0)
|
|
|
|
|
#'re-search-backward
|
|
|
|
|
#'re-search-forward))
|
|
|
|
|
(line-beg-pos (line-beginning-position))
|
|
|
|
|
(line-content-start (+ line-beg-pos (current-indentation)))
|
|
|
|
|
(pos (point-marker))
|
2012-11-12 10:26:50 -03:00
|
|
|
|
(beg-indentation
|
|
|
|
|
(and (> arg 0)
|
|
|
|
|
(save-excursion
|
2012-11-26 18:45:58 -03:00
|
|
|
|
(while (and
|
|
|
|
|
(not (python-info-looking-at-beginning-of-defun))
|
|
|
|
|
(python-nav-backward-block)))
|
|
|
|
|
(or (and (python-info-looking-at-beginning-of-defun)
|
|
|
|
|
(+ (current-indentation) python-indent-offset))
|
|
|
|
|
0))))
|
2012-05-17 00:03:44 -03:00
|
|
|
|
(found
|
|
|
|
|
(progn
|
|
|
|
|
(when (and (< arg 0)
|
|
|
|
|
(python-info-looking-at-beginning-of-defun))
|
|
|
|
|
(end-of-line 1))
|
|
|
|
|
(while (and (funcall re-search-fn
|
|
|
|
|
python-nav-beginning-of-defun-regexp nil t)
|
2012-11-12 10:26:50 -03:00
|
|
|
|
(or (python-syntax-context-type)
|
|
|
|
|
;; Handle nested defuns when moving
|
|
|
|
|
;; backwards by checking indentation.
|
|
|
|
|
(and (> arg 0)
|
|
|
|
|
(not (= (current-indentation) 0))
|
|
|
|
|
(>= (current-indentation) beg-indentation)))))
|
2012-05-17 00:03:44 -03:00
|
|
|
|
(and (python-info-looking-at-beginning-of-defun)
|
|
|
|
|
(or (not (= (line-number-at-pos pos)
|
|
|
|
|
(line-number-at-pos)))
|
|
|
|
|
(and (>= (point) line-beg-pos)
|
|
|
|
|
(<= (point) line-content-start)
|
|
|
|
|
(> pos line-content-start)))))))
|
|
|
|
|
(if found
|
|
|
|
|
(or (beginning-of-line 1) t)
|
|
|
|
|
(and (goto-char pos) nil))))
|
|
|
|
|
|
2012-11-12 10:26:50 -03:00
|
|
|
|
(defun python-nav-beginning-of-defun (&optional arg)
|
|
|
|
|
"Move point to `beginning-of-defun'.
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
With positive ARG search backwards else search forward.
|
|
|
|
|
ARG nil or 0 defaults to 1. When searching backwards,
|
|
|
|
|
nested defuns are handled with care depending on current
|
|
|
|
|
point position. Return non-nil if point is moved to
|
2012-11-12 10:26:50 -03:00
|
|
|
|
`beginning-of-defun'."
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(when (or (null arg) (= arg 0)) (setq arg 1))
|
2012-05-17 00:03:44 -03:00
|
|
|
|
(let ((found))
|
2013-12-26 13:45:19 -03:00
|
|
|
|
(while (and (not (= arg 0))
|
|
|
|
|
(let ((keep-searching-p
|
|
|
|
|
(python-nav--beginning-of-defun arg)))
|
|
|
|
|
(when (and keep-searching-p (null found))
|
|
|
|
|
(setq found t))
|
|
|
|
|
keep-searching-p))
|
|
|
|
|
(setq arg (if (> arg 0) (1- arg) (1+ arg))))
|
2012-05-17 00:03:44 -03:00
|
|
|
|
found))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-11-12 10:26:50 -03:00
|
|
|
|
(defun python-nav-end-of-defun ()
|
2012-05-17 00:02:52 -03:00
|
|
|
|
"Move point to the end of def or class.
|
|
|
|
|
Returns nil if point is not in a def or class."
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(interactive)
|
2012-11-12 10:26:50 -03:00
|
|
|
|
(let ((beg-defun-indent)
|
|
|
|
|
(beg-pos (point)))
|
2012-05-17 00:03:44 -03:00
|
|
|
|
(when (or (python-info-looking-at-beginning-of-defun)
|
2012-11-12 10:26:50 -03:00
|
|
|
|
(python-nav-beginning-of-defun 1)
|
|
|
|
|
(python-nav-beginning-of-defun -1))
|
2012-05-17 00:03:44 -03:00
|
|
|
|
(setq beg-defun-indent (current-indentation))
|
2012-11-12 10:26:50 -03:00
|
|
|
|
(while (progn
|
|
|
|
|
(python-nav-end-of-statement)
|
|
|
|
|
(python-util-forward-comment 1)
|
|
|
|
|
(and (> (current-indentation) beg-defun-indent)
|
|
|
|
|
(not (eobp)))))
|
|
|
|
|
(python-util-forward-comment -1)
|
2012-05-17 00:03:44 -03:00
|
|
|
|
(forward-line 1)
|
2012-11-12 10:26:50 -03:00
|
|
|
|
;; Ensure point moves forward.
|
|
|
|
|
(and (> beg-pos (point)) (goto-char beg-pos)))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2013-04-17 19:23:13 -03:00
|
|
|
|
(defun python-nav--syntactically (fn poscompfn &optional contextfn)
|
|
|
|
|
"Move point using FN avoiding places with specific context.
|
2013-04-18 23:31:09 -03:00
|
|
|
|
FN must take no arguments. POSCOMPFN is a two arguments function
|
2013-04-17 19:23:13 -03:00
|
|
|
|
used to compare current and previous point after it is moved
|
|
|
|
|
using FN, this is normally a less-than or greater-than
|
|
|
|
|
comparison. Optional argument CONTEXTFN defaults to
|
|
|
|
|
`python-syntax-context-type' and is used for checking current
|
|
|
|
|
point context, it must return a non-nil value if this point must
|
|
|
|
|
be skipped."
|
|
|
|
|
(let ((contextfn (or contextfn 'python-syntax-context-type))
|
|
|
|
|
(start-pos (point-marker))
|
|
|
|
|
(prev-pos))
|
|
|
|
|
(catch 'found
|
|
|
|
|
(while t
|
|
|
|
|
(let* ((newpos
|
|
|
|
|
(and (funcall fn) (point-marker)))
|
|
|
|
|
(context (funcall contextfn)))
|
|
|
|
|
(cond ((and (not context) newpos
|
|
|
|
|
(or (and (not prev-pos) newpos)
|
|
|
|
|
(and prev-pos newpos
|
|
|
|
|
(funcall poscompfn newpos prev-pos))))
|
|
|
|
|
(throw 'found (point-marker)))
|
|
|
|
|
((and newpos context)
|
|
|
|
|
(setq prev-pos (point)))
|
|
|
|
|
(t (when (not newpos) (goto-char start-pos))
|
|
|
|
|
(throw 'found nil))))))))
|
2013-04-17 02:08:20 -03:00
|
|
|
|
|
|
|
|
|
(defun python-nav--forward-defun (arg)
|
|
|
|
|
"Internal implementation of python-nav-{backward,forward}-defun.
|
|
|
|
|
Uses ARG to define which function to call, and how many times
|
|
|
|
|
repeat it."
|
|
|
|
|
(let ((found))
|
|
|
|
|
(while (and (> arg 0)
|
|
|
|
|
(setq found
|
|
|
|
|
(python-nav--syntactically
|
|
|
|
|
(lambda ()
|
|
|
|
|
(re-search-forward
|
|
|
|
|
python-nav-beginning-of-defun-regexp nil t))
|
|
|
|
|
'>)))
|
|
|
|
|
(setq arg (1- arg)))
|
|
|
|
|
(while (and (< arg 0)
|
|
|
|
|
(setq found
|
|
|
|
|
(python-nav--syntactically
|
|
|
|
|
(lambda ()
|
|
|
|
|
(re-search-backward
|
|
|
|
|
python-nav-beginning-of-defun-regexp nil t))
|
|
|
|
|
'<)))
|
|
|
|
|
(setq arg (1+ arg)))
|
|
|
|
|
found))
|
|
|
|
|
|
|
|
|
|
(defun python-nav-backward-defun (&optional arg)
|
|
|
|
|
"Navigate to closer defun backward ARG times.
|
|
|
|
|
Unlikely `python-nav-beginning-of-defun' this doesn't care about
|
|
|
|
|
nested definitions."
|
|
|
|
|
(interactive "^p")
|
|
|
|
|
(python-nav--forward-defun (- (or arg 1))))
|
|
|
|
|
|
|
|
|
|
(defun python-nav-forward-defun (&optional arg)
|
|
|
|
|
"Navigate to closer defun forward ARG times.
|
|
|
|
|
Unlikely `python-nav-beginning-of-defun' this doesn't care about
|
|
|
|
|
nested definitions."
|
|
|
|
|
(interactive "^p")
|
|
|
|
|
(python-nav--forward-defun (or arg 1)))
|
|
|
|
|
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(defun python-nav-beginning-of-statement ()
|
2012-07-16 10:13:01 -03:00
|
|
|
|
"Move to start of current statement."
|
2012-05-17 00:03:15 -03:00
|
|
|
|
(interactive "^")
|
2013-12-24 16:48:40 -03:00
|
|
|
|
(back-to-indentation)
|
|
|
|
|
(let* ((ppss (syntax-ppss))
|
|
|
|
|
(context-point
|
|
|
|
|
(or
|
|
|
|
|
(python-syntax-context 'paren ppss)
|
|
|
|
|
(python-syntax-context 'string ppss))))
|
|
|
|
|
(cond ((bobp))
|
|
|
|
|
(context-point
|
|
|
|
|
(goto-char context-point)
|
|
|
|
|
(python-nav-beginning-of-statement))
|
|
|
|
|
((save-excursion
|
|
|
|
|
(forward-line -1)
|
|
|
|
|
(python-info-line-ends-backslash-p))
|
|
|
|
|
(forward-line -1)
|
|
|
|
|
(python-nav-beginning-of-statement))))
|
2012-10-08 02:19:15 -03:00
|
|
|
|
(point-marker))
|
2012-05-17 00:03:15 -03:00
|
|
|
|
|
2012-12-31 17:58:57 -03:00
|
|
|
|
(defun python-nav-end-of-statement (&optional noend)
|
|
|
|
|
"Move to end of current statement.
|
|
|
|
|
Optional argument NOEND is internal and makes the logic to not
|
|
|
|
|
jump to the end of line when moving forward searching for the end
|
|
|
|
|
of the statement."
|
2012-05-17 00:03:15 -03:00
|
|
|
|
(interactive "^")
|
2012-12-31 17:58:57 -03:00
|
|
|
|
(let (string-start bs-pos)
|
|
|
|
|
(while (and (or noend (goto-char (line-end-position)))
|
|
|
|
|
(not (eobp))
|
|
|
|
|
(cond ((setq string-start (python-syntax-context 'string))
|
|
|
|
|
(goto-char string-start)
|
2013-01-10 00:44:12 -03:00
|
|
|
|
(if (python-syntax-context 'paren)
|
|
|
|
|
;; Ended up inside a paren, roll again.
|
|
|
|
|
(python-nav-end-of-statement t)
|
|
|
|
|
;; This is not inside a paren, move to the
|
|
|
|
|
;; end of this string.
|
|
|
|
|
(goto-char (+ (point)
|
|
|
|
|
(python-syntax-count-quotes
|
|
|
|
|
(char-after (point)) (point))))
|
|
|
|
|
(or (re-search-forward (rx (syntax string-delimiter)) nil t)
|
|
|
|
|
(goto-char (point-max)))))
|
2012-12-31 17:58:57 -03:00
|
|
|
|
((python-syntax-context 'paren)
|
|
|
|
|
;; The statement won't end before we've escaped
|
|
|
|
|
;; at least one level of parenthesis.
|
|
|
|
|
(condition-case err
|
|
|
|
|
(goto-char (scan-lists (point) 1 -1))
|
|
|
|
|
(scan-error (goto-char (nth 3 err)))))
|
|
|
|
|
((setq bs-pos (python-info-line-ends-backslash-p))
|
|
|
|
|
(goto-char bs-pos)
|
|
|
|
|
(forward-line 1))))))
|
2012-10-08 02:19:15 -03:00
|
|
|
|
(point-marker))
|
2012-05-17 00:03:15 -03:00
|
|
|
|
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(defun python-nav-backward-statement (&optional arg)
|
|
|
|
|
"Move backward to previous statement.
|
|
|
|
|
With ARG, repeat. See `python-nav-forward-statement'."
|
2012-05-17 00:03:15 -03:00
|
|
|
|
(interactive "^p")
|
|
|
|
|
(or arg (setq arg 1))
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(python-nav-forward-statement (- arg)))
|
2012-05-17 00:03:15 -03:00
|
|
|
|
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(defun python-nav-forward-statement (&optional arg)
|
|
|
|
|
"Move forward to next statement.
|
|
|
|
|
With ARG, repeat. With negative argument, move ARG times
|
|
|
|
|
backward to previous statement."
|
2012-05-17 00:03:15 -03:00
|
|
|
|
(interactive "^p")
|
|
|
|
|
(or arg (setq arg 1))
|
|
|
|
|
(while (> arg 0)
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(python-nav-end-of-statement)
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(python-util-forward-comment)
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(python-nav-beginning-of-statement)
|
2012-05-17 00:03:15 -03:00
|
|
|
|
(setq arg (1- arg)))
|
|
|
|
|
(while (< arg 0)
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(python-nav-beginning-of-statement)
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(python-util-forward-comment -1)
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(python-nav-beginning-of-statement)
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(setq arg (1+ arg))))
|
|
|
|
|
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(defun python-nav-beginning-of-block ()
|
2012-07-16 10:13:01 -03:00
|
|
|
|
"Move to start of current block."
|
|
|
|
|
(interactive "^")
|
2013-09-04 23:46:34 -04:00
|
|
|
|
(let ((starting-pos (point)))
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(if (progn
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(python-nav-beginning-of-statement)
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(looking-at (python-rx block-start)))
|
|
|
|
|
(point-marker)
|
|
|
|
|
;; Go to first line beginning a statement
|
|
|
|
|
(while (and (not (bobp))
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(or (and (python-nav-beginning-of-statement) nil)
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(python-info-current-line-comment-p)
|
|
|
|
|
(python-info-current-line-empty-p)))
|
|
|
|
|
(forward-line -1))
|
|
|
|
|
(let ((block-matching-indent
|
|
|
|
|
(- (current-indentation) python-indent-offset)))
|
|
|
|
|
(while
|
|
|
|
|
(and (python-nav-backward-block)
|
|
|
|
|
(> (current-indentation) block-matching-indent)))
|
|
|
|
|
(if (and (looking-at (python-rx block-start))
|
|
|
|
|
(= (current-indentation) block-matching-indent))
|
|
|
|
|
(point-marker)
|
|
|
|
|
(and (goto-char starting-pos) nil))))))
|
|
|
|
|
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(defun python-nav-end-of-block ()
|
2012-07-16 10:13:01 -03:00
|
|
|
|
"Move to end of current block."
|
|
|
|
|
(interactive "^")
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(when (python-nav-beginning-of-block)
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(let ((block-indentation (current-indentation)))
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(python-nav-end-of-statement)
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(while (and (forward-line 1)
|
|
|
|
|
(not (eobp))
|
|
|
|
|
(or (and (> (current-indentation) block-indentation)
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(or (python-nav-end-of-statement) t))
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(python-info-current-line-comment-p)
|
|
|
|
|
(python-info-current-line-empty-p))))
|
|
|
|
|
(python-util-forward-comment -1)
|
|
|
|
|
(point-marker))))
|
|
|
|
|
|
|
|
|
|
(defun python-nav-backward-block (&optional arg)
|
|
|
|
|
"Move backward to previous block of code.
|
|
|
|
|
With ARG, repeat. See `python-nav-forward-block'."
|
|
|
|
|
(interactive "^p")
|
|
|
|
|
(or arg (setq arg 1))
|
|
|
|
|
(python-nav-forward-block (- arg)))
|
|
|
|
|
|
|
|
|
|
(defun python-nav-forward-block (&optional arg)
|
|
|
|
|
"Move forward to next block of code.
|
|
|
|
|
With ARG, repeat. With negative argument, move ARG times
|
|
|
|
|
backward to previous block."
|
|
|
|
|
(interactive "^p")
|
|
|
|
|
(or arg (setq arg 1))
|
|
|
|
|
(let ((block-start-regexp
|
|
|
|
|
(python-rx line-start (* whitespace) block-start))
|
|
|
|
|
(starting-pos (point)))
|
|
|
|
|
(while (> arg 0)
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(python-nav-end-of-statement)
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(while (and
|
|
|
|
|
(re-search-forward block-start-regexp nil t)
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(python-syntax-context-type)))
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(setq arg (1- arg)))
|
|
|
|
|
(while (< arg 0)
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(python-nav-beginning-of-statement)
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(while (and
|
|
|
|
|
(re-search-backward block-start-regexp nil t)
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(python-syntax-context-type)))
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(setq arg (1+ arg)))
|
2012-07-17 15:02:53 -03:00
|
|
|
|
(python-nav-beginning-of-statement)
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(if (not (looking-at (python-rx block-start)))
|
|
|
|
|
(and (goto-char starting-pos) nil)
|
|
|
|
|
(and (not (= (point) starting-pos)) (point-marker)))))
|
|
|
|
|
|
2013-12-25 15:07:31 -03:00
|
|
|
|
(defun python-nav--lisp-forward-sexp (&optional arg)
|
|
|
|
|
"Standard version `forward-sexp'.
|
|
|
|
|
It ignores completely the value of `forward-sexp-function' by
|
|
|
|
|
setting it to nil before calling `forward-sexp'. With positive
|
|
|
|
|
ARG move forward only one sexp, else move backwards."
|
2013-01-10 00:44:12 -03:00
|
|
|
|
(let ((forward-sexp-function)
|
2013-12-25 15:07:31 -03:00
|
|
|
|
(arg (if (or (not arg) (> arg 0)) 1 -1)))
|
|
|
|
|
(forward-sexp arg)))
|
|
|
|
|
|
|
|
|
|
(defun python-nav--lisp-forward-sexp-safe (&optional arg)
|
|
|
|
|
"Safe version of standard `forward-sexp'.
|
|
|
|
|
When at end of sexp (i.e. looking at a opening/closing paren)
|
|
|
|
|
skips it instead of throwing an error. With positive ARG move
|
|
|
|
|
forward only one sexp, else move backwards."
|
|
|
|
|
(let* ((arg (if (or (not arg) (> arg 0)) 1 -1))
|
|
|
|
|
(paren-regexp
|
|
|
|
|
(if (> arg 0) (python-rx close-paren) (python-rx open-paren)))
|
|
|
|
|
(search-fn
|
|
|
|
|
(if (> arg 0) #'re-search-forward #'re-search-backward)))
|
2012-08-09 00:30:37 -03:00
|
|
|
|
(condition-case nil
|
2013-12-25 15:07:31 -03:00
|
|
|
|
(python-nav--lisp-forward-sexp arg)
|
2012-08-09 00:30:37 -03:00
|
|
|
|
(error
|
|
|
|
|
(while (and (funcall search-fn paren-regexp nil t)
|
|
|
|
|
(python-syntax-context 'paren)))))))
|
|
|
|
|
|
2013-12-25 15:07:31 -03:00
|
|
|
|
(defun python-nav--forward-sexp (&optional dir safe)
|
2012-10-08 02:19:15 -03:00
|
|
|
|
"Move to forward sexp.
|
2013-12-25 15:07:31 -03:00
|
|
|
|
With positive optional argument DIR direction move forward, else
|
|
|
|
|
backwards. When optional argument SAFE is non-nil do not throw
|
|
|
|
|
errors when at end of sexp, skip it instead."
|
2012-10-08 02:19:15 -03:00
|
|
|
|
(setq dir (or dir 1))
|
|
|
|
|
(unless (= dir 0)
|
|
|
|
|
(let* ((forward-p (if (> dir 0)
|
|
|
|
|
(and (setq dir 1) t)
|
|
|
|
|
(and (setq dir -1) nil)))
|
|
|
|
|
(context-type (python-syntax-context-type)))
|
|
|
|
|
(cond
|
2013-02-13 21:42:11 -03:00
|
|
|
|
((memq context-type '(string comment))
|
2012-10-08 02:19:15 -03:00
|
|
|
|
;; Inside of a string, get out of it.
|
2013-02-13 21:42:11 -03:00
|
|
|
|
(let ((forward-sexp-function))
|
|
|
|
|
(forward-sexp dir)))
|
2012-10-08 02:19:15 -03:00
|
|
|
|
((or (eq context-type 'paren)
|
|
|
|
|
(and forward-p (looking-at (python-rx open-paren)))
|
|
|
|
|
(and (not forward-p)
|
|
|
|
|
(eq (syntax-class (syntax-after (1- (point))))
|
|
|
|
|
(car (string-to-syntax ")")))))
|
|
|
|
|
;; Inside a paren or looking at it, lisp knows what to do.
|
2013-12-25 15:07:31 -03:00
|
|
|
|
(if safe
|
|
|
|
|
(python-nav--lisp-forward-sexp-safe dir)
|
|
|
|
|
(python-nav--lisp-forward-sexp dir)))
|
2012-10-08 02:19:15 -03:00
|
|
|
|
(t
|
|
|
|
|
;; This part handles the lispy feel of
|
|
|
|
|
;; `python-nav-forward-sexp'. Knowing everything about the
|
|
|
|
|
;; current context and the context of the next sexp tries to
|
|
|
|
|
;; follow the lisp sexp motion commands in a symmetric manner.
|
|
|
|
|
(let* ((context
|
|
|
|
|
(cond
|
|
|
|
|
((python-info-beginning-of-block-p) 'block-start)
|
|
|
|
|
((python-info-end-of-block-p) 'block-end)
|
|
|
|
|
((python-info-beginning-of-statement-p) 'statement-start)
|
|
|
|
|
((python-info-end-of-statement-p) 'statement-end)))
|
|
|
|
|
(next-sexp-pos
|
|
|
|
|
(save-excursion
|
2013-12-25 15:07:31 -03:00
|
|
|
|
(if safe
|
|
|
|
|
(python-nav--lisp-forward-sexp-safe dir)
|
|
|
|
|
(python-nav--lisp-forward-sexp dir))
|
2012-10-08 02:19:15 -03:00
|
|
|
|
(point)))
|
2013-02-13 21:42:11 -03:00
|
|
|
|
(next-sexp-context
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char next-sexp-pos)
|
|
|
|
|
(cond
|
|
|
|
|
((python-info-beginning-of-block-p) 'block-start)
|
|
|
|
|
((python-info-end-of-block-p) 'block-end)
|
|
|
|
|
((python-info-beginning-of-statement-p) 'statement-start)
|
|
|
|
|
((python-info-end-of-statement-p) 'statement-end)
|
|
|
|
|
((python-info-statement-starts-block-p) 'starts-block)
|
|
|
|
|
((python-info-statement-ends-block-p) 'ends-block)))))
|
2012-10-08 02:19:15 -03:00
|
|
|
|
(if forward-p
|
|
|
|
|
(cond ((and (not (eobp))
|
|
|
|
|
(python-info-current-line-empty-p))
|
|
|
|
|
(python-util-forward-comment dir)
|
|
|
|
|
(python-nav--forward-sexp dir))
|
|
|
|
|
((eq context 'block-start)
|
|
|
|
|
(python-nav-end-of-block))
|
|
|
|
|
((eq context 'statement-start)
|
|
|
|
|
(python-nav-end-of-statement))
|
|
|
|
|
((and (memq context '(statement-end block-end))
|
|
|
|
|
(eq next-sexp-context 'ends-block))
|
|
|
|
|
(goto-char next-sexp-pos)
|
|
|
|
|
(python-nav-end-of-block))
|
|
|
|
|
((and (memq context '(statement-end block-end))
|
|
|
|
|
(eq next-sexp-context 'starts-block))
|
|
|
|
|
(goto-char next-sexp-pos)
|
|
|
|
|
(python-nav-end-of-block))
|
|
|
|
|
((memq context '(statement-end block-end))
|
|
|
|
|
(goto-char next-sexp-pos)
|
|
|
|
|
(python-nav-end-of-statement))
|
|
|
|
|
(t (goto-char next-sexp-pos)))
|
|
|
|
|
(cond ((and (not (bobp))
|
|
|
|
|
(python-info-current-line-empty-p))
|
2013-02-13 21:42:11 -03:00
|
|
|
|
(python-util-forward-comment dir)
|
|
|
|
|
(python-nav--forward-sexp dir))
|
2012-10-08 02:19:15 -03:00
|
|
|
|
((eq context 'block-end)
|
|
|
|
|
(python-nav-beginning-of-block))
|
|
|
|
|
((eq context 'statement-end)
|
|
|
|
|
(python-nav-beginning-of-statement))
|
|
|
|
|
((and (memq context '(statement-start block-start))
|
|
|
|
|
(eq next-sexp-context 'starts-block))
|
|
|
|
|
(goto-char next-sexp-pos)
|
|
|
|
|
(python-nav-beginning-of-block))
|
|
|
|
|
((and (memq context '(statement-start block-start))
|
|
|
|
|
(eq next-sexp-context 'ends-block))
|
|
|
|
|
(goto-char next-sexp-pos)
|
|
|
|
|
(python-nav-beginning-of-block))
|
|
|
|
|
((memq context '(statement-start block-start))
|
|
|
|
|
(goto-char next-sexp-pos)
|
|
|
|
|
(python-nav-beginning-of-statement))
|
|
|
|
|
(t (goto-char next-sexp-pos))))))))))
|
2012-08-09 00:30:37 -03:00
|
|
|
|
|
|
|
|
|
(defun python-nav-forward-sexp (&optional arg)
|
2013-12-25 15:07:31 -03:00
|
|
|
|
"Move forward across expressions.
|
|
|
|
|
With ARG, do it that many times. Negative arg -N means move
|
|
|
|
|
backward N times."
|
|
|
|
|
(interactive "^p")
|
|
|
|
|
(or arg (setq arg 1))
|
|
|
|
|
(while (> arg 0)
|
|
|
|
|
(python-nav--forward-sexp 1)
|
|
|
|
|
(setq arg (1- arg)))
|
|
|
|
|
(while (< arg 0)
|
|
|
|
|
(python-nav--forward-sexp -1)
|
|
|
|
|
(setq arg (1+ arg))))
|
|
|
|
|
|
|
|
|
|
(defun python-nav-backward-sexp (&optional arg)
|
|
|
|
|
"Move backward across expressions.
|
|
|
|
|
With ARG, do it that many times. Negative arg -N means move
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
forward N times."
|
2013-12-25 15:07:31 -03:00
|
|
|
|
(interactive "^p")
|
|
|
|
|
(or arg (setq arg 1))
|
|
|
|
|
(python-nav-forward-sexp (- arg)))
|
|
|
|
|
|
|
|
|
|
(defun python-nav-forward-sexp-safe (&optional arg)
|
|
|
|
|
"Move forward safely across expressions.
|
|
|
|
|
With ARG, do it that many times. Negative arg -N means move
|
|
|
|
|
backward N times."
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(interactive "^p")
|
|
|
|
|
(or arg (setq arg 1))
|
|
|
|
|
(while (> arg 0)
|
2013-12-25 15:07:31 -03:00
|
|
|
|
(python-nav--forward-sexp 1 t)
|
2012-08-09 00:30:37 -03:00
|
|
|
|
(setq arg (1- arg)))
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(while (< arg 0)
|
2013-12-25 15:07:31 -03:00
|
|
|
|
(python-nav--forward-sexp -1 t)
|
2012-05-17 00:03:15 -03:00
|
|
|
|
(setq arg (1+ arg))))
|
|
|
|
|
|
2013-12-25 15:07:31 -03:00
|
|
|
|
(defun python-nav-backward-sexp-safe (&optional arg)
|
|
|
|
|
"Move backward safely across expressions.
|
|
|
|
|
With ARG, do it that many times. Negative arg -N means move
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
forward N times."
|
2013-12-25 15:07:31 -03:00
|
|
|
|
(interactive "^p")
|
|
|
|
|
(or arg (setq arg 1))
|
|
|
|
|
(python-nav-forward-sexp-safe (- arg)))
|
|
|
|
|
|
2012-10-08 23:07:26 -03:00
|
|
|
|
(defun python-nav--up-list (&optional dir)
|
|
|
|
|
"Internal implementation of `python-nav-up-list'.
|
|
|
|
|
DIR is always 1 or -1 and comes sanitized from
|
|
|
|
|
`python-nav-up-list' calls."
|
|
|
|
|
(let ((context (python-syntax-context-type))
|
|
|
|
|
(forward-p (> dir 0)))
|
|
|
|
|
(cond
|
|
|
|
|
((memq context '(string comment)))
|
|
|
|
|
((eq context 'paren)
|
|
|
|
|
(let ((forward-sexp-function))
|
|
|
|
|
(up-list dir)))
|
|
|
|
|
((and forward-p (python-info-end-of-block-p))
|
|
|
|
|
(let ((parent-end-pos
|
|
|
|
|
(save-excursion
|
|
|
|
|
(let ((indentation (and
|
|
|
|
|
(python-nav-beginning-of-block)
|
|
|
|
|
(current-indentation))))
|
|
|
|
|
(while (and indentation
|
|
|
|
|
(> indentation 0)
|
|
|
|
|
(>= (current-indentation) indentation)
|
|
|
|
|
(python-nav-backward-block)))
|
|
|
|
|
(python-nav-end-of-block)))))
|
|
|
|
|
(and (> (or parent-end-pos (point)) (point))
|
|
|
|
|
(goto-char parent-end-pos))))
|
|
|
|
|
(forward-p (python-nav-end-of-block))
|
|
|
|
|
((and (not forward-p)
|
|
|
|
|
(> (current-indentation) 0)
|
|
|
|
|
(python-info-beginning-of-block-p))
|
|
|
|
|
(let ((prev-block-pos
|
|
|
|
|
(save-excursion
|
|
|
|
|
(let ((indentation (current-indentation)))
|
|
|
|
|
(while (and (python-nav-backward-block)
|
2012-10-11 21:07:25 -03:00
|
|
|
|
(>= (current-indentation) indentation))))
|
2012-10-08 23:07:26 -03:00
|
|
|
|
(point))))
|
|
|
|
|
(and (> (point) prev-block-pos)
|
|
|
|
|
(goto-char prev-block-pos))))
|
|
|
|
|
((not forward-p) (python-nav-beginning-of-block)))))
|
|
|
|
|
|
|
|
|
|
(defun python-nav-up-list (&optional arg)
|
|
|
|
|
"Move forward out of one level of parentheses (or blocks).
|
|
|
|
|
With ARG, do this that many times.
|
|
|
|
|
A negative argument means move backward but still to a less deep spot.
|
|
|
|
|
This command assumes point is not in a string or comment."
|
|
|
|
|
(interactive "^p")
|
|
|
|
|
(or arg (setq arg 1))
|
|
|
|
|
(while (> arg 0)
|
|
|
|
|
(python-nav--up-list 1)
|
|
|
|
|
(setq arg (1- arg)))
|
|
|
|
|
(while (< arg 0)
|
|
|
|
|
(python-nav--up-list -1)
|
|
|
|
|
(setq arg (1+ arg))))
|
|
|
|
|
|
|
|
|
|
(defun python-nav-backward-up-list (&optional arg)
|
|
|
|
|
"Move backward out of one level of parentheses (or blocks).
|
|
|
|
|
With ARG, do this that many times.
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
A negative argument means move forward but still to a less deep spot.
|
2012-10-08 23:07:26 -03:00
|
|
|
|
This command assumes point is not in a string or comment."
|
|
|
|
|
(interactive "^p")
|
|
|
|
|
(or arg (setq arg 1))
|
|
|
|
|
(python-nav-up-list (- arg)))
|
|
|
|
|
|
2013-09-02 00:37:18 -03:00
|
|
|
|
(defun python-nav-if-name-main ()
|
|
|
|
|
"Move point at the beginning the __main__ block.
|
|
|
|
|
When \"if __name__ == '__main__':\" is found returns its
|
|
|
|
|
position, else returns nil."
|
|
|
|
|
(interactive)
|
|
|
|
|
(let ((point (point))
|
|
|
|
|
(found (catch 'found
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(while (re-search-forward
|
|
|
|
|
(python-rx line-start
|
|
|
|
|
"if" (+ space)
|
|
|
|
|
"__name__" (+ space)
|
|
|
|
|
"==" (+ space)
|
|
|
|
|
(group-n 1 (or ?\" ?\'))
|
|
|
|
|
"__main__" (backref 1) (* space) ":")
|
|
|
|
|
nil t)
|
|
|
|
|
(when (not (python-syntax-context-type))
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(throw 'found t))))))
|
|
|
|
|
(if found
|
|
|
|
|
(point)
|
|
|
|
|
(ignore (goto-char point)))))
|
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
;;; Shell integration
|
|
|
|
|
|
2012-05-17 00:03:23 -03:00
|
|
|
|
(defcustom python-shell-buffer-name "Python"
|
|
|
|
|
"Default buffer name for Python interpreter."
|
|
|
|
|
:type 'string
|
|
|
|
|
:group 'python
|
|
|
|
|
:safe 'stringp)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defcustom python-shell-interpreter "python"
|
|
|
|
|
"Default Python interpreter for shell."
|
|
|
|
|
:type 'string
|
2012-05-17 00:03:33 -03:00
|
|
|
|
:group 'python)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:23 -03:00
|
|
|
|
(defcustom python-shell-internal-buffer-name "Python Internal"
|
|
|
|
|
"Default buffer name for the Internal Python interpreter."
|
|
|
|
|
:type 'string
|
|
|
|
|
:group 'python
|
|
|
|
|
:safe 'stringp)
|
2012-05-17 00:03:21 -03:00
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(defcustom python-shell-interpreter-args "-i"
|
|
|
|
|
"Default arguments for the Python interpreter."
|
|
|
|
|
:type 'string
|
2012-05-17 00:03:33 -03:00
|
|
|
|
:group 'python)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2014-07-19 10:13:07 -03:00
|
|
|
|
(defcustom python-shell-interpreter-interactive-arg "-i"
|
|
|
|
|
"Interpreter argument to force it to run interactively."
|
|
|
|
|
:type 'string
|
|
|
|
|
:version "24.4")
|
|
|
|
|
|
|
|
|
|
(defcustom python-shell-prompt-detect-enabled t
|
|
|
|
|
"Non-nil enables autodetection of interpreter prompts."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:safe 'booleanp
|
|
|
|
|
:version "24.4")
|
|
|
|
|
|
|
|
|
|
(defcustom python-shell-prompt-detect-failure-warning t
|
|
|
|
|
"Non-nil enables warnings when detection of prompts fail."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:safe 'booleanp
|
|
|
|
|
:version "24.4")
|
|
|
|
|
|
|
|
|
|
(defcustom python-shell-prompt-input-regexps
|
|
|
|
|
'(">>> " "\\.\\.\\. " ; Python
|
2014-07-20 15:12:30 -03:00
|
|
|
|
"In \\[[0-9]+\\]: " ; IPython
|
2014-07-26 20:43:51 -03:00
|
|
|
|
" \\.\\.\\.: " ; IPython
|
2014-07-20 15:12:30 -03:00
|
|
|
|
;; Using ipdb outside IPython may fail to cleanup and leave static
|
|
|
|
|
;; IPython prompts activated, this adds some safeguard for that.
|
|
|
|
|
"In : " "\\.\\.\\.: ")
|
2014-07-19 10:13:07 -03:00
|
|
|
|
"List of regular expressions matching input prompts."
|
|
|
|
|
:type '(repeat string)
|
|
|
|
|
:version "24.4")
|
|
|
|
|
|
|
|
|
|
(defcustom python-shell-prompt-output-regexps
|
|
|
|
|
'("" ; Python
|
2014-07-20 15:12:30 -03:00
|
|
|
|
"Out\\[[0-9]+\\]: " ; IPython
|
|
|
|
|
"Out :") ; ipdb safeguard
|
2014-07-19 10:13:07 -03:00
|
|
|
|
"List of regular expressions matching output prompts."
|
|
|
|
|
:type '(repeat string)
|
|
|
|
|
:version "24.4")
|
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(defcustom python-shell-prompt-regexp ">>> "
|
2014-07-19 10:13:07 -03:00
|
|
|
|
"Regular expression matching top level input prompt of Python shell.
|
2012-05-17 00:03:09 -03:00
|
|
|
|
It should not contain a caret (^) at the beginning."
|
2014-07-19 10:13:07 -03:00
|
|
|
|
:type 'string)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2014-07-19 10:13:07 -03:00
|
|
|
|
(defcustom python-shell-prompt-block-regexp "\\.\\.\\. "
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Regular expression matching block input prompt of Python shell.
|
2012-05-17 00:03:09 -03:00
|
|
|
|
It should not contain a caret (^) at the beginning."
|
2014-07-19 10:13:07 -03:00
|
|
|
|
:type 'string)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:23 -03:00
|
|
|
|
(defcustom python-shell-prompt-output-regexp ""
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Regular expression matching output prompt of Python shell.
|
2012-05-17 00:03:09 -03:00
|
|
|
|
It should not contain a caret (^) at the beginning."
|
2014-07-19 10:13:07 -03:00
|
|
|
|
:type 'string)
|
2012-05-17 00:03:06 -03:00
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(defcustom python-shell-prompt-pdb-regexp "[(<]*[Ii]?[Pp]db[>)]+ "
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Regular expression matching pdb input prompt of Python shell.
|
2012-05-17 00:03:09 -03:00
|
|
|
|
It should not contain a caret (^) at the beginning."
|
2014-07-19 10:13:07 -03:00
|
|
|
|
:type 'string)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2014-07-26 20:43:51 -03:00
|
|
|
|
(defcustom python-shell-font-lock-enable t
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Should syntax highlighting be enabled in the Python shell buffer?
|
|
|
|
|
Restart the Python shell after changing this variable for it to take effect."
|
2012-05-17 00:03:40 -03:00
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'python
|
|
|
|
|
:safe 'booleanp)
|
|
|
|
|
|
2014-07-26 20:43:51 -03:00
|
|
|
|
(define-obsolete-variable-alias
|
|
|
|
|
'python-shell-enable-font-lock python-shell-font-lock-enable "24.4")
|
|
|
|
|
|
2012-05-17 00:03:10 -03:00
|
|
|
|
(defcustom python-shell-process-environment nil
|
2012-05-17 00:03:24 -03:00
|
|
|
|
"List of environment variables for Python shell.
|
|
|
|
|
This variable follows the same rules as `process-environment'
|
2012-05-17 00:03:10 -03:00
|
|
|
|
since it merges with it before the process creation routines are
|
|
|
|
|
called. When this variable is nil, the Python shell is run with
|
2012-05-17 00:03:24 -03:00
|
|
|
|
the default `process-environment'."
|
2012-05-17 00:03:10 -03:00
|
|
|
|
:type '(repeat string)
|
|
|
|
|
:group 'python
|
|
|
|
|
:safe 'listp)
|
|
|
|
|
|
2012-05-17 00:03:27 -03:00
|
|
|
|
(defcustom python-shell-extra-pythonpaths nil
|
|
|
|
|
"List of extra pythonpaths for Python shell.
|
|
|
|
|
The values of this variable are added to the existing value of
|
|
|
|
|
PYTHONPATH in the `process-environment' variable."
|
|
|
|
|
:type '(repeat string)
|
|
|
|
|
:group 'python
|
|
|
|
|
:safe 'listp)
|
|
|
|
|
|
2012-05-17 00:03:10 -03:00
|
|
|
|
(defcustom python-shell-exec-path nil
|
|
|
|
|
"List of path to search for binaries.
|
|
|
|
|
This variable follows the same rules as `exec-path' since it
|
|
|
|
|
merges with it before the process creation routines are called.
|
|
|
|
|
When this variable is nil, the Python shell is run with the
|
|
|
|
|
default `exec-path'."
|
|
|
|
|
:type '(repeat string)
|
|
|
|
|
:group 'python
|
|
|
|
|
:safe 'listp)
|
|
|
|
|
|
2012-05-17 00:03:21 -03:00
|
|
|
|
(defcustom python-shell-virtualenv-path nil
|
|
|
|
|
"Path to virtualenv root.
|
|
|
|
|
This variable, when set to a string, makes the values stored in
|
|
|
|
|
`python-shell-process-environment' and `python-shell-exec-path'
|
|
|
|
|
to be modified properly so shells are started with the specified
|
|
|
|
|
virtualenv."
|
2013-05-08 21:40:20 -04:00
|
|
|
|
:type '(choice (const nil) string)
|
2012-05-17 00:03:21 -03:00
|
|
|
|
:group 'python
|
|
|
|
|
:safe 'stringp)
|
|
|
|
|
|
2012-05-17 00:03:09 -03:00
|
|
|
|
(defcustom python-shell-setup-codes '(python-shell-completion-setup-code
|
|
|
|
|
python-ffap-setup-code
|
|
|
|
|
python-eldoc-setup-code)
|
2012-05-17 00:03:29 -03:00
|
|
|
|
"List of code run by `python-shell-send-setup-codes'."
|
2012-05-17 00:03:09 -03:00
|
|
|
|
:type '(repeat symbol)
|
|
|
|
|
:group 'python
|
|
|
|
|
:safe 'listp)
|
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(defcustom python-shell-compilation-regexp-alist
|
|
|
|
|
`((,(rx line-start (1+ (any " \t")) "File \""
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c
|
|
|
|
|
"\", line " (group (1+ digit)))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
1 2)
|
|
|
|
|
(,(rx " in file " (group (1+ not-newline)) " on line "
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(group (1+ digit)))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
1 2)
|
|
|
|
|
(,(rx line-start "> " (group (1+ (not (any "(\"<"))))
|
2012-05-17 00:03:35 -03:00
|
|
|
|
"(" (group (1+ digit)) ")" (1+ (not (any "("))) "()")
|
2012-05-17 00:02:52 -03:00
|
|
|
|
1 2))
|
|
|
|
|
"`compilation-error-regexp-alist' for inferior Python."
|
|
|
|
|
:type '(alist string)
|
|
|
|
|
:group 'python)
|
|
|
|
|
|
2014-07-19 10:13:07 -03:00
|
|
|
|
(defvar python-shell--prompt-calculated-input-regexp nil
|
|
|
|
|
"Calculated input prompt regexp for inferior python shell.
|
|
|
|
|
Do not set this variable directly, instead use
|
|
|
|
|
`python-shell-prompt-set-calculated-regexps'.")
|
|
|
|
|
|
|
|
|
|
(defvar python-shell--prompt-calculated-output-regexp nil
|
|
|
|
|
"Calculated output prompt regexp for inferior python shell.
|
|
|
|
|
Do not set this variable directly, instead use
|
|
|
|
|
`python-shell-set-prompt-regexp'.")
|
|
|
|
|
|
|
|
|
|
(defun python-shell-prompt-detect ()
|
|
|
|
|
"Detect prompts for the current `python-shell-interpreter'.
|
|
|
|
|
When prompts can be retrieved successfully from the
|
|
|
|
|
`python-shell-interpreter' run with
|
|
|
|
|
`python-shell-interpreter-interactive-arg', returns a list of
|
|
|
|
|
three elements, where the first two are input prompts and the
|
|
|
|
|
last one is an output prompt. When no prompts can be detected
|
|
|
|
|
and `python-shell-prompt-detect-failure-warning' is non-nil,
|
|
|
|
|
shows a warning with instructions to avoid hangs and returns nil.
|
|
|
|
|
When `python-shell-prompt-detect-enabled' is nil avoids any
|
|
|
|
|
detection and just returns nil."
|
|
|
|
|
(when python-shell-prompt-detect-enabled
|
|
|
|
|
(let* ((process-environment (python-shell-calculate-process-environment))
|
|
|
|
|
(exec-path (python-shell-calculate-exec-path))
|
2014-07-19 16:19:47 -03:00
|
|
|
|
(code (concat
|
|
|
|
|
"import sys\n"
|
|
|
|
|
"ps = [getattr(sys, 'ps%s' % i, '') for i in range(1,4)]\n"
|
|
|
|
|
;; JSON is built manually for compatibility
|
|
|
|
|
"ps_json = '\\n[\"%s\", \"%s\", \"%s\"]\\n' % tuple(ps)\n"
|
|
|
|
|
"print (ps_json)\n"
|
|
|
|
|
"sys.exit(0)\n"))
|
2014-07-19 10:13:07 -03:00
|
|
|
|
(output
|
|
|
|
|
(with-temp-buffer
|
2014-07-19 16:19:47 -03:00
|
|
|
|
;; TODO: improve error handling by using
|
|
|
|
|
;; `condition-case' and displaying the error message to
|
|
|
|
|
;; the user in the no-prompts warning.
|
|
|
|
|
(ignore-errors
|
|
|
|
|
(let ((code-file (python-shell--save-temp-file code)))
|
|
|
|
|
;; Use `process-file' as it is remote-host friendly.
|
|
|
|
|
(process-file
|
|
|
|
|
(executable-find python-shell-interpreter)
|
|
|
|
|
code-file
|
|
|
|
|
'(t nil)
|
|
|
|
|
nil
|
|
|
|
|
python-shell-interpreter-interactive-arg)
|
|
|
|
|
;; Try to cleanup
|
|
|
|
|
(delete-file code-file)))
|
2014-07-19 10:13:07 -03:00
|
|
|
|
(buffer-string)))
|
|
|
|
|
(prompts
|
|
|
|
|
(catch 'prompts
|
|
|
|
|
(dolist (line (split-string output "\n" t))
|
|
|
|
|
(let ((res
|
|
|
|
|
;; Check if current line is a valid JSON array
|
|
|
|
|
(and (string= (substring line 0 2) "[\"")
|
|
|
|
|
(ignore-errors
|
|
|
|
|
;; Return prompts as a list, not vector
|
|
|
|
|
(append (json-read-from-string line) nil)))))
|
|
|
|
|
;; The list must contain 3 strings, where the first
|
|
|
|
|
;; is the input prompt, the second is the block
|
|
|
|
|
;; prompt and the last one is the output prompt. The
|
|
|
|
|
;; input prompt is the only one that can't be empty.
|
|
|
|
|
(when (and (= (length res) 3)
|
|
|
|
|
(cl-every #'stringp res)
|
|
|
|
|
(not (string= (car res) "")))
|
|
|
|
|
(throw 'prompts res))))
|
|
|
|
|
nil)))
|
|
|
|
|
(when (and (not prompts)
|
|
|
|
|
python-shell-prompt-detect-failure-warning)
|
|
|
|
|
(warn
|
|
|
|
|
(concat
|
|
|
|
|
"Python shell prompts cannot be detected.\n"
|
|
|
|
|
"If your emacs session hangs when starting python shells\n"
|
|
|
|
|
"recover with `keyboard-quit' and then try fixing the\n"
|
|
|
|
|
"interactive flag for your interpreter by adjusting the\n"
|
|
|
|
|
"`python-shell-interpreter-interactive-arg' or add regexps\n"
|
|
|
|
|
"matching shell prompts in the directory-local friendly vars:\n"
|
|
|
|
|
" + `python-shell-prompt-regexp'\n"
|
|
|
|
|
" + `python-shell-prompt-block-regexp'\n"
|
|
|
|
|
" + `python-shell-prompt-output-regexp'\n"
|
|
|
|
|
"Or alternatively in:\n"
|
|
|
|
|
" + `python-shell-prompt-input-regexps'\n"
|
|
|
|
|
" + `python-shell-prompt-output-regexps'")))
|
|
|
|
|
prompts)))
|
|
|
|
|
|
|
|
|
|
(defun python-shell-prompt-validate-regexps ()
|
|
|
|
|
"Validate all user provided regexps for prompts.
|
|
|
|
|
Signals `user-error' if any of these vars contain invalid
|
|
|
|
|
regexps: `python-shell-prompt-regexp',
|
|
|
|
|
`python-shell-prompt-block-regexp',
|
|
|
|
|
`python-shell-prompt-pdb-regexp',
|
|
|
|
|
`python-shell-prompt-output-regexp',
|
|
|
|
|
`python-shell-prompt-input-regexps',
|
|
|
|
|
`python-shell-prompt-output-regexps'."
|
|
|
|
|
(dolist (symbol (list 'python-shell-prompt-input-regexps
|
|
|
|
|
'python-shell-prompt-output-regexps
|
|
|
|
|
'python-shell-prompt-regexp
|
|
|
|
|
'python-shell-prompt-block-regexp
|
|
|
|
|
'python-shell-prompt-pdb-regexp
|
|
|
|
|
'python-shell-prompt-output-regexp))
|
|
|
|
|
(dolist (regexp (let ((regexps (symbol-value symbol)))
|
|
|
|
|
(if (listp regexps)
|
|
|
|
|
regexps
|
|
|
|
|
(list regexps))))
|
|
|
|
|
(when (not (python-util-valid-regexp-p regexp))
|
|
|
|
|
(user-error "Invalid regexp %s in `%s'"
|
|
|
|
|
regexp symbol)))))
|
|
|
|
|
|
|
|
|
|
(defun python-shell-prompt-set-calculated-regexps ()
|
|
|
|
|
"Detect and set input and output prompt regexps.
|
|
|
|
|
Build and set the values for `python-shell-input-prompt-regexp'
|
|
|
|
|
and `python-shell-output-prompt-regexp' using the values from
|
|
|
|
|
`python-shell-prompt-regexp', `python-shell-prompt-block-regexp',
|
|
|
|
|
`python-shell-prompt-pdb-regexp',
|
|
|
|
|
`python-shell-prompt-output-regexp',
|
|
|
|
|
`python-shell-prompt-input-regexps',
|
|
|
|
|
`python-shell-prompt-output-regexps' and detected prompts from
|
|
|
|
|
`python-shell-prompt-detect'."
|
|
|
|
|
(when (not (and python-shell--prompt-calculated-input-regexp
|
|
|
|
|
python-shell--prompt-calculated-output-regexp))
|
|
|
|
|
(let* ((detected-prompts (python-shell-prompt-detect))
|
|
|
|
|
(input-prompts nil)
|
|
|
|
|
(output-prompts nil)
|
|
|
|
|
(build-regexp
|
|
|
|
|
(lambda (prompts)
|
|
|
|
|
(concat "^\\("
|
|
|
|
|
(mapconcat #'identity
|
|
|
|
|
(sort prompts
|
|
|
|
|
(lambda (a b)
|
|
|
|
|
(let ((length-a (length a))
|
|
|
|
|
(length-b (length b)))
|
|
|
|
|
(if (= length-a length-b)
|
|
|
|
|
(string< a b)
|
|
|
|
|
(> (length a) (length b))))))
|
|
|
|
|
"\\|")
|
|
|
|
|
"\\)"))))
|
|
|
|
|
;; Validate ALL regexps
|
|
|
|
|
(python-shell-prompt-validate-regexps)
|
|
|
|
|
;; Collect all user defined input prompts
|
|
|
|
|
(dolist (prompt (append python-shell-prompt-input-regexps
|
|
|
|
|
(list python-shell-prompt-regexp
|
|
|
|
|
python-shell-prompt-block-regexp
|
|
|
|
|
python-shell-prompt-pdb-regexp)))
|
|
|
|
|
(cl-pushnew prompt input-prompts :test #'string=))
|
|
|
|
|
;; Collect all user defined output prompts
|
|
|
|
|
(dolist (prompt (cons python-shell-prompt-output-regexp
|
|
|
|
|
python-shell-prompt-output-regexps))
|
|
|
|
|
(cl-pushnew prompt output-prompts :test #'string=))
|
|
|
|
|
;; Collect detected prompts if any
|
|
|
|
|
(when detected-prompts
|
|
|
|
|
(dolist (prompt (butlast detected-prompts))
|
|
|
|
|
(setq prompt (regexp-quote prompt))
|
|
|
|
|
(cl-pushnew prompt input-prompts :test #'string=))
|
|
|
|
|
(cl-pushnew (regexp-quote
|
|
|
|
|
(car (last detected-prompts)))
|
|
|
|
|
output-prompts :test #'string=))
|
|
|
|
|
;; Set input and output prompt regexps from collected prompts
|
|
|
|
|
(setq python-shell--prompt-calculated-input-regexp
|
|
|
|
|
(funcall build-regexp input-prompts)
|
|
|
|
|
python-shell--prompt-calculated-output-regexp
|
|
|
|
|
(funcall build-regexp output-prompts)))))
|
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(defun python-shell-get-process-name (dedicated)
|
2012-05-17 00:03:33 -03:00
|
|
|
|
"Calculate the appropriate process name for inferior Python process.
|
2012-05-17 00:02:52 -03:00
|
|
|
|
If DEDICATED is t and the variable `buffer-file-name' is non-nil
|
|
|
|
|
returns a string with the form
|
|
|
|
|
`python-shell-buffer-name'[variable `buffer-file-name'] else
|
2012-08-11 00:46:07 +08:00
|
|
|
|
returns the value of `python-shell-buffer-name'."
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(let ((process-name
|
|
|
|
|
(if (and dedicated
|
|
|
|
|
buffer-file-name)
|
|
|
|
|
(format "%s[%s]" python-shell-buffer-name buffer-file-name)
|
|
|
|
|
(format "%s" python-shell-buffer-name))))
|
|
|
|
|
process-name))
|
|
|
|
|
|
2012-05-17 00:03:21 -03:00
|
|
|
|
(defun python-shell-internal-get-process-name ()
|
2012-05-17 00:03:33 -03:00
|
|
|
|
"Calculate the appropriate process name for Internal Python process.
|
2012-05-17 00:03:21 -03:00
|
|
|
|
The name is calculated from `python-shell-global-buffer-name' and
|
|
|
|
|
a hash of all relevant global shell settings in order to ensure
|
|
|
|
|
uniqueness for different types of configurations."
|
|
|
|
|
(format "%s [%s]"
|
|
|
|
|
python-shell-internal-buffer-name
|
|
|
|
|
(md5
|
|
|
|
|
(concat
|
2014-07-19 10:13:07 -03:00
|
|
|
|
python-shell-interpreter
|
|
|
|
|
python-shell-interpreter-args
|
|
|
|
|
python-shell--prompt-calculated-input-regexp
|
|
|
|
|
python-shell--prompt-calculated-output-regexp
|
2012-05-17 00:03:21 -03:00
|
|
|
|
(mapconcat #'symbol-value python-shell-setup-codes "")
|
2012-05-17 00:03:29 -03:00
|
|
|
|
(mapconcat #'identity python-shell-process-environment "")
|
|
|
|
|
(mapconcat #'identity python-shell-extra-pythonpaths "")
|
|
|
|
|
(mapconcat #'identity python-shell-exec-path "")
|
2012-05-17 00:03:21 -03:00
|
|
|
|
(or python-shell-virtualenv-path "")
|
2012-05-17 00:03:29 -03:00
|
|
|
|
(mapconcat #'identity python-shell-exec-path "")))))
|
2012-05-17 00:03:21 -03:00
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(defun python-shell-parse-command ()
|
2012-05-17 00:03:09 -03:00
|
|
|
|
"Calculate the string used to execute the inferior Python process."
|
2013-01-28 18:59:42 -03:00
|
|
|
|
(let ((process-environment (python-shell-calculate-process-environment))
|
|
|
|
|
(exec-path (python-shell-calculate-exec-path)))
|
|
|
|
|
(format "%s %s"
|
|
|
|
|
(executable-find python-shell-interpreter)
|
|
|
|
|
python-shell-interpreter-args)))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:24 -03:00
|
|
|
|
(defun python-shell-calculate-process-environment ()
|
|
|
|
|
"Calculate process environment given `python-shell-virtualenv-path'."
|
2012-05-17 00:03:26 -03:00
|
|
|
|
(let ((process-environment (append
|
|
|
|
|
python-shell-process-environment
|
|
|
|
|
process-environment nil))
|
2012-05-17 00:03:21 -03:00
|
|
|
|
(virtualenv (if python-shell-virtualenv-path
|
|
|
|
|
(directory-file-name python-shell-virtualenv-path)
|
|
|
|
|
nil)))
|
2012-05-17 00:03:27 -03:00
|
|
|
|
(when python-shell-extra-pythonpaths
|
|
|
|
|
(setenv "PYTHONPATH"
|
|
|
|
|
(format "%s%s%s"
|
|
|
|
|
(mapconcat 'identity
|
|
|
|
|
python-shell-extra-pythonpaths
|
|
|
|
|
path-separator)
|
|
|
|
|
path-separator
|
|
|
|
|
(or (getenv "PYTHONPATH") ""))))
|
2012-05-17 00:03:21 -03:00
|
|
|
|
(if (not virtualenv)
|
2012-05-17 00:03:26 -03:00
|
|
|
|
process-environment
|
|
|
|
|
(setenv "PYTHONHOME" nil)
|
|
|
|
|
(setenv "PATH" (format "%s/bin%s%s"
|
2012-05-17 00:03:27 -03:00
|
|
|
|
virtualenv path-separator
|
|
|
|
|
(or (getenv "PATH") "")))
|
2012-05-17 00:03:26 -03:00
|
|
|
|
(setenv "VIRTUAL_ENV" virtualenv))
|
|
|
|
|
process-environment))
|
2012-05-17 00:03:21 -03:00
|
|
|
|
|
|
|
|
|
(defun python-shell-calculate-exec-path ()
|
|
|
|
|
"Calculate exec path given `python-shell-virtualenv-path'."
|
2012-05-17 00:03:26 -03:00
|
|
|
|
(let ((path (append python-shell-exec-path
|
|
|
|
|
exec-path nil)))
|
2012-05-17 00:03:21 -03:00
|
|
|
|
(if (not python-shell-virtualenv-path)
|
|
|
|
|
path
|
|
|
|
|
(cons (format "%s/bin"
|
|
|
|
|
(directory-file-name python-shell-virtualenv-path))
|
|
|
|
|
path))))
|
|
|
|
|
|
2014-07-26 20:43:51 -03:00
|
|
|
|
(defun python-shell-comint-end-of-output-p (output)
|
|
|
|
|
"Return non-nil if OUTPUT is ends with input prompt."
|
|
|
|
|
(string-match
|
|
|
|
|
;; XXX: It seems on OSX an extra carriage return is attached
|
|
|
|
|
;; at the end of output, this handles that too.
|
|
|
|
|
(concat
|
|
|
|
|
"\r?\n?"
|
|
|
|
|
;; Remove initial caret from calculated regexp
|
|
|
|
|
(replace-regexp-in-string
|
|
|
|
|
(rx string-start ?^) ""
|
|
|
|
|
python-shell--prompt-calculated-input-regexp)
|
|
|
|
|
(rx eos))
|
|
|
|
|
output))
|
|
|
|
|
|
2014-07-26 23:02:38 -03:00
|
|
|
|
(define-obsolete-function-alias
|
|
|
|
|
'python-comint-output-filter-function
|
|
|
|
|
'ansi-color-filter-apply
|
|
|
|
|
"24.5")
|
|
|
|
|
|
|
|
|
|
(defun python-comint-postoutput-scroll-to-bottom (output)
|
|
|
|
|
"Faster version of `comint-postoutput-scroll-to-bottom'.
|
|
|
|
|
Avoids `recenter' calls until OUTPUT is completely sent."
|
|
|
|
|
(when (and (not (string= "" output))
|
|
|
|
|
(python-shell-comint-end-of-output-p
|
|
|
|
|
(ansi-color-filter-apply output)))
|
|
|
|
|
(comint-postoutput-scroll-to-bottom output))
|
|
|
|
|
output)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-10-07 16:37:37 -03:00
|
|
|
|
(defvar python-shell--parent-buffer nil)
|
|
|
|
|
|
2014-07-26 20:43:51 -03:00
|
|
|
|
(defvar python-shell--font-lock-buffer nil)
|
|
|
|
|
|
|
|
|
|
(defun python-shell-font-lock-get-or-create-buffer ()
|
|
|
|
|
"Get or create a font-lock buffer for current inferior process."
|
|
|
|
|
(if python-shell--font-lock-buffer
|
|
|
|
|
python-shell--font-lock-buffer
|
|
|
|
|
(let ((process-name
|
|
|
|
|
(process-name (get-buffer-process (current-buffer)))))
|
|
|
|
|
(generate-new-buffer
|
|
|
|
|
(format "*%s-font-lock*" process-name)))))
|
|
|
|
|
|
|
|
|
|
(defun python-shell-font-lock-kill-buffer ()
|
|
|
|
|
"Kill the font-lock buffer safely."
|
|
|
|
|
(when (and python-shell--font-lock-buffer
|
|
|
|
|
(buffer-live-p python-shell--font-lock-buffer))
|
|
|
|
|
(kill-buffer python-shell--font-lock-buffer)
|
|
|
|
|
(when (eq major-mode 'inferior-python-mode)
|
|
|
|
|
(setq python-shell--font-lock-buffer nil))))
|
|
|
|
|
|
|
|
|
|
(defmacro python-shell-font-lock-with-font-lock-buffer (&rest body)
|
|
|
|
|
"Execute the forms in BODY in the font-lock buffer.
|
|
|
|
|
The value returned is the value of the last form in BODY. See
|
|
|
|
|
also `with-current-buffer'."
|
|
|
|
|
(declare (indent 0) (debug t))
|
|
|
|
|
`(save-current-buffer
|
|
|
|
|
(when (not (eq major-mode 'inferior-python-mode))
|
|
|
|
|
(error "Current buffer is not in `inferior-python-mode'."))
|
|
|
|
|
(when (not (and python-shell--font-lock-buffer
|
|
|
|
|
(get-buffer python-shell--font-lock-buffer)))
|
|
|
|
|
(setq python-shell--font-lock-buffer
|
|
|
|
|
(python-shell-font-lock-get-or-create-buffer)))
|
|
|
|
|
(set-buffer python-shell--font-lock-buffer)
|
|
|
|
|
(set (make-local-variable 'delay-mode-hooks) t)
|
|
|
|
|
(let ((python-indent-guess-indent-offset nil))
|
|
|
|
|
(when (not (eq major-mode 'python-mode))
|
|
|
|
|
(python-mode))
|
|
|
|
|
,@body)))
|
|
|
|
|
|
|
|
|
|
(defun python-shell-font-lock-cleanup-buffer ()
|
|
|
|
|
"Cleanup the font-lock buffer.
|
|
|
|
|
Provided as a command because this might be handy if something
|
|
|
|
|
goes wrong and syntax highlighting in the shell gets messed up."
|
|
|
|
|
(interactive)
|
|
|
|
|
(python-shell-font-lock-with-font-lock-buffer
|
|
|
|
|
(delete-region (point-min) (point-max))))
|
|
|
|
|
|
|
|
|
|
(defun python-shell-font-lock-comint-output-filter-function (output)
|
|
|
|
|
"Clean up the font-lock buffer after any OUTPUT."
|
|
|
|
|
(when (and (not (string= "" output))
|
|
|
|
|
;; Is end of output and is not just a prompt.
|
|
|
|
|
(not (member
|
|
|
|
|
(python-shell-comint-end-of-output-p
|
|
|
|
|
(ansi-color-filter-apply output))
|
|
|
|
|
'(nil 0))))
|
|
|
|
|
;; If output is other than an input prompt then "real" output has
|
|
|
|
|
;; been received and the font-lock buffer must be cleaned up.
|
|
|
|
|
(python-shell-font-lock-cleanup-buffer))
|
|
|
|
|
output)
|
|
|
|
|
|
|
|
|
|
(defun python-shell-font-lock-post-command-hook ()
|
|
|
|
|
"Fontifies current line in shell buffer."
|
|
|
|
|
(if (eq this-command 'comint-send-input)
|
|
|
|
|
;; Add a newline when user sends input as this may be a block.
|
|
|
|
|
(python-shell-font-lock-with-font-lock-buffer
|
|
|
|
|
(goto-char (line-end-position))
|
|
|
|
|
(newline))
|
|
|
|
|
(when (and (python-util-comint-last-prompt)
|
|
|
|
|
(> (point) (cdr (python-util-comint-last-prompt))))
|
|
|
|
|
(let ((input (buffer-substring-no-properties
|
2014-07-26 22:00:26 -03:00
|
|
|
|
(cdr (python-util-comint-last-prompt)) (point-max)))
|
|
|
|
|
(old-input (python-shell-font-lock-with-font-lock-buffer
|
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
|
(line-beginning-position) (point-max))))
|
|
|
|
|
(current-point (point))
|
|
|
|
|
(buffer-undo-list t))
|
|
|
|
|
;; When input hasn't changed, do nothing.
|
|
|
|
|
(when (not (string= input old-input))
|
|
|
|
|
(delete-region (cdr (python-util-comint-last-prompt)) (point-max))
|
|
|
|
|
(insert
|
|
|
|
|
(python-shell-font-lock-with-font-lock-buffer
|
|
|
|
|
(delete-region (line-beginning-position)
|
|
|
|
|
(line-end-position))
|
|
|
|
|
(insert input)
|
|
|
|
|
;; Ensure buffer is fontified, keeping it
|
|
|
|
|
;; compatible with Emacs < 24.4.
|
|
|
|
|
(if (fboundp 'font-lock-ensure)
|
|
|
|
|
(funcall 'font-lock-ensure)
|
|
|
|
|
(font-lock-default-fontify-buffer))
|
|
|
|
|
;; Replace FACE text properties with FONT-LOCK-FACE so
|
|
|
|
|
;; they are not overwritten by comint buffer's font lock.
|
|
|
|
|
(python-util-text-properties-replace-name
|
|
|
|
|
'face 'font-lock-face)
|
|
|
|
|
(buffer-substring (line-beginning-position)
|
|
|
|
|
(line-end-position))))
|
|
|
|
|
(goto-char current-point))))))
|
2014-07-26 20:43:51 -03:00
|
|
|
|
|
|
|
|
|
(defun python-shell-font-lock-turn-on (&optional msg)
|
|
|
|
|
"Turn on shell font-lock.
|
|
|
|
|
With argument MSG show activation message."
|
|
|
|
|
(python-shell-font-lock-kill-buffer)
|
|
|
|
|
(set (make-local-variable 'python-shell--font-lock-buffer) nil)
|
|
|
|
|
(add-hook 'post-command-hook
|
|
|
|
|
#'python-shell-font-lock-post-command-hook nil 'local)
|
|
|
|
|
(add-hook 'kill-buffer-hook
|
|
|
|
|
#'python-shell-font-lock-kill-buffer nil 'local)
|
|
|
|
|
(add-hook 'comint-output-filter-functions
|
|
|
|
|
#'python-shell-font-lock-comint-output-filter-function
|
|
|
|
|
'append 'local)
|
|
|
|
|
(when msg
|
|
|
|
|
(message "Shell font-lock is enabled")))
|
|
|
|
|
|
|
|
|
|
(defun python-shell-font-lock-turn-off (&optional msg)
|
|
|
|
|
"Turn off shell font-lock.
|
|
|
|
|
With argument MSG show deactivation message."
|
|
|
|
|
(python-shell-font-lock-kill-buffer)
|
|
|
|
|
(when (python-util-comint-last-prompt)
|
|
|
|
|
;; Cleanup current fontification
|
|
|
|
|
(remove-text-properties
|
|
|
|
|
(cdr (python-util-comint-last-prompt))
|
|
|
|
|
(line-end-position)
|
|
|
|
|
'(face nil font-lock-face nil)))
|
|
|
|
|
(set (make-local-variable 'python-shell--font-lock-buffer) nil)
|
|
|
|
|
(remove-hook 'post-command-hook
|
|
|
|
|
#'python-shell-font-lock-post-command-hook'local)
|
|
|
|
|
(remove-hook 'kill-buffer-hook
|
|
|
|
|
#'python-shell-font-lock-kill-buffer 'local)
|
|
|
|
|
(remove-hook 'comint-output-filter-functions
|
|
|
|
|
#'python-shell-font-lock-comint-output-filter-function
|
|
|
|
|
'local)
|
|
|
|
|
(when msg
|
|
|
|
|
(message "Shell font-lock is disabled")))
|
|
|
|
|
|
|
|
|
|
(defun python-shell-font-lock-toggle (&optional msg)
|
|
|
|
|
"Toggle font-lock for shell.
|
|
|
|
|
With argument MSG show activation/deactivation message."
|
|
|
|
|
(interactive "p")
|
|
|
|
|
(set (make-local-variable 'python-shell-font-lock-enable)
|
|
|
|
|
(not python-shell-font-lock-enable))
|
|
|
|
|
(if python-shell-font-lock-enable
|
|
|
|
|
(python-shell-font-lock-turn-on msg)
|
|
|
|
|
(python-shell-font-lock-turn-off msg))
|
|
|
|
|
python-shell-font-lock-enable)
|
2012-10-08 18:30:36 -03:00
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(define-derived-mode inferior-python-mode comint-mode "Inferior Python"
|
2012-05-17 00:03:06 -03:00
|
|
|
|
"Major mode for Python inferior process.
|
2012-05-17 00:03:09 -03:00
|
|
|
|
Runs a Python interpreter as a subprocess of Emacs, with Python
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
I/O through an Emacs buffer. Variables `python-shell-interpreter'
|
|
|
|
|
and `python-shell-interpreter-args' control which Python
|
|
|
|
|
interpreter is run. Variables
|
2012-05-17 00:03:09 -03:00
|
|
|
|
`python-shell-prompt-regexp',
|
|
|
|
|
`python-shell-prompt-output-regexp',
|
|
|
|
|
`python-shell-prompt-block-regexp',
|
2014-07-26 20:43:51 -03:00
|
|
|
|
`python-shell-font-lock-enable',
|
2012-05-17 00:03:09 -03:00
|
|
|
|
`python-shell-completion-setup-code',
|
2012-05-17 00:03:29 -03:00
|
|
|
|
`python-shell-completion-string-code',
|
2012-05-17 00:03:31 -03:00
|
|
|
|
`python-shell-completion-module-string-code',
|
2012-05-17 00:03:29 -03:00
|
|
|
|
`python-eldoc-setup-code', `python-eldoc-string-code',
|
|
|
|
|
`python-ffap-setup-code' and `python-ffap-string-code' can
|
|
|
|
|
customize this mode for different Python interpreters.
|
2012-05-17 00:03:09 -03:00
|
|
|
|
|
2014-07-26 23:02:38 -03:00
|
|
|
|
This mode resets `comint-output-filter-functions' locally, so you
|
|
|
|
|
may want to re-add custom functions to it using the
|
|
|
|
|
`inferior-python-mode-hook'.
|
|
|
|
|
|
2012-05-17 00:03:09 -03:00
|
|
|
|
You can also add additional setup code to be run at
|
|
|
|
|
initialization of the interpreter via `python-shell-setup-codes'
|
|
|
|
|
variable.
|
|
|
|
|
|
|
|
|
|
\(Type \\[describe-mode] in the process buffer for a list of commands.)"
|
2014-07-19 10:13:07 -03:00
|
|
|
|
(let ((interpreter python-shell-interpreter)
|
|
|
|
|
(args python-shell-interpreter-args))
|
|
|
|
|
(when python-shell--parent-buffer
|
|
|
|
|
(python-util-clone-local-variables python-shell--parent-buffer))
|
|
|
|
|
;; Users can override default values for these vars when calling
|
|
|
|
|
;; `run-python'. This ensures new values let-bound in
|
|
|
|
|
;; `python-shell-make-comint' are locally set.
|
|
|
|
|
(set (make-local-variable 'python-shell-interpreter) interpreter)
|
|
|
|
|
(set (make-local-variable 'python-shell-interpreter-args) args))
|
|
|
|
|
(set (make-local-variable 'python-shell--prompt-calculated-input-regexp) nil)
|
|
|
|
|
(set (make-local-variable 'python-shell--prompt-calculated-output-regexp) nil)
|
|
|
|
|
(python-shell-prompt-set-calculated-regexps)
|
|
|
|
|
(setq comint-prompt-regexp python-shell--prompt-calculated-input-regexp)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(setq mode-line-process '(":%s"))
|
2014-07-26 23:02:38 -03:00
|
|
|
|
(set (make-local-variable 'comint-output-filter-functions)
|
|
|
|
|
'(ansi-color-process-output
|
|
|
|
|
python-pdbtrack-comint-output-filter-function
|
|
|
|
|
python-comint-postoutput-scroll-to-bottom))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(set (make-local-variable 'compilation-error-regexp-alist)
|
|
|
|
|
python-shell-compilation-regexp-alist)
|
2012-05-17 00:02:58 -03:00
|
|
|
|
(define-key inferior-python-mode-map [remap complete-symbol]
|
|
|
|
|
'completion-at-point)
|
|
|
|
|
(add-hook 'completion-at-point-functions
|
|
|
|
|
'python-shell-completion-complete-at-point nil 'local)
|
2012-05-17 00:03:06 -03:00
|
|
|
|
(add-to-list (make-local-variable 'comint-dynamic-complete-functions)
|
|
|
|
|
'python-shell-completion-complete-at-point)
|
2012-07-27 13:42:19 -03:00
|
|
|
|
(define-key inferior-python-mode-map "\t"
|
2012-05-17 00:03:06 -03:00
|
|
|
|
'python-shell-completion-complete-or-indent)
|
2012-08-09 01:08:29 -03:00
|
|
|
|
(make-local-variable 'python-pdbtrack-buffers-to-kill)
|
|
|
|
|
(make-local-variable 'python-pdbtrack-tracked-buffer)
|
|
|
|
|
(make-local-variable 'python-shell-internal-last-output)
|
2014-07-26 20:43:51 -03:00
|
|
|
|
(when python-shell-font-lock-enable
|
|
|
|
|
(python-shell-font-lock-turn-on))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(compilation-shell-minor-mode 1))
|
|
|
|
|
|
2012-08-06 00:27:43 -03:00
|
|
|
|
(defun python-shell-make-comint (cmd proc-name &optional pop internal)
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Create a Python shell comint buffer.
|
|
|
|
|
CMD is the Python command to be executed and PROC-NAME is the
|
2012-05-17 00:03:22 -03:00
|
|
|
|
process name the comint buffer will get. After the comint buffer
|
2012-08-06 00:27:43 -03:00
|
|
|
|
is created the `inferior-python-mode' is activated. When
|
|
|
|
|
optional argument POP is non-nil the buffer is shown. When
|
|
|
|
|
optional argument INTERNAL is non-nil this process is run on a
|
|
|
|
|
buffer with a name that starts with a space, following the Emacs
|
|
|
|
|
convention for temporary/internal buffers, and also makes sure
|
|
|
|
|
the user is not queried for confirmation when the process is
|
|
|
|
|
killed."
|
2012-05-17 00:03:22 -03:00
|
|
|
|
(save-excursion
|
2012-08-06 00:27:43 -03:00
|
|
|
|
(let* ((proc-buffer-name
|
|
|
|
|
(format (if (not internal) "*%s*" " *%s*") proc-name))
|
2012-05-17 00:03:24 -03:00
|
|
|
|
(process-environment (python-shell-calculate-process-environment))
|
2012-05-17 00:03:22 -03:00
|
|
|
|
(exec-path (python-shell-calculate-exec-path)))
|
|
|
|
|
(when (not (comint-check-proc proc-buffer-name))
|
2012-05-17 00:03:23 -03:00
|
|
|
|
(let* ((cmdlist (split-string-and-unquote cmd))
|
2014-07-19 10:13:07 -03:00
|
|
|
|
(interpreter (car cmdlist))
|
|
|
|
|
(args (cdr cmdlist))
|
2012-08-06 00:27:43 -03:00
|
|
|
|
(buffer (apply #'make-comint-in-buffer proc-name proc-buffer-name
|
2014-07-19 10:13:07 -03:00
|
|
|
|
interpreter nil args))
|
2012-10-07 16:37:37 -03:00
|
|
|
|
(python-shell--parent-buffer (current-buffer))
|
2014-07-19 10:13:07 -03:00
|
|
|
|
(process (get-buffer-process buffer))
|
|
|
|
|
;; As the user may have overriden default values for
|
|
|
|
|
;; these vars on `run-python', let-binding them allows
|
|
|
|
|
;; to have the new right values in all setup code
|
|
|
|
|
;; that's is done in `inferior-python-mode', which is
|
|
|
|
|
;; important, especially for prompt detection.
|
|
|
|
|
(python-shell-interpreter interpreter)
|
|
|
|
|
(python-shell-interpreter-args
|
|
|
|
|
(mapconcat #'identity args " ")))
|
2012-05-17 00:03:23 -03:00
|
|
|
|
(with-current-buffer buffer
|
2012-10-07 16:37:37 -03:00
|
|
|
|
(inferior-python-mode))
|
2012-08-06 00:27:43 -03:00
|
|
|
|
(accept-process-output process)
|
|
|
|
|
(and pop (pop-to-buffer buffer t))
|
|
|
|
|
(and internal (set-process-query-on-exit-flag process nil))))
|
2012-05-17 00:03:25 -03:00
|
|
|
|
proc-buffer-name)))
|
2012-05-17 00:03:22 -03:00
|
|
|
|
|
2012-07-27 09:38:19 -03:00
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun run-python (cmd &optional dedicated show)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
"Run an inferior Python process.
|
2012-05-17 00:03:09 -03:00
|
|
|
|
Input and output via buffer named after
|
|
|
|
|
`python-shell-buffer-name'. If there is a process already
|
|
|
|
|
running in that buffer, just switch to it.
|
2012-07-27 09:38:19 -03:00
|
|
|
|
|
|
|
|
|
With argument, allows you to define CMD so you can edit the
|
|
|
|
|
command used to call the interpreter and define DEDICATED, so a
|
|
|
|
|
dedicated process for the current buffer is open. When numeric
|
|
|
|
|
prefix arg is other than 0 or 4 do not SHOW.
|
|
|
|
|
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
Runs the hook `inferior-python-mode-hook' after
|
|
|
|
|
`comint-mode-hook' is run. (Type \\[describe-mode] in the
|
2012-07-27 09:38:19 -03:00
|
|
|
|
process buffer for a list of commands.)"
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(interactive
|
|
|
|
|
(if current-prefix-arg
|
|
|
|
|
(list
|
2014-06-11 21:45:33 -04:00
|
|
|
|
(read-shell-command "Run Python: " (python-shell-parse-command))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(y-or-n-p "Make dedicated process? ")
|
2012-07-27 09:38:19 -03:00
|
|
|
|
(= (prefix-numeric-value current-prefix-arg) 4))
|
|
|
|
|
(list (python-shell-parse-command) nil t)))
|
|
|
|
|
(python-shell-make-comint
|
|
|
|
|
cmd (python-shell-get-process-name dedicated) show)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
dedicated)
|
|
|
|
|
|
2012-05-17 00:03:21 -03:00
|
|
|
|
(defun run-python-internal ()
|
|
|
|
|
"Run an inferior Internal Python process.
|
|
|
|
|
Input and output via buffer named after
|
|
|
|
|
`python-shell-internal-buffer-name' and what
|
2012-07-31 20:43:31 -03:00
|
|
|
|
`python-shell-internal-get-process-name' returns.
|
|
|
|
|
|
|
|
|
|
This new kind of shell is intended to be used for generic
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
communication related to defined configurations; the main
|
2012-07-31 20:43:31 -03:00
|
|
|
|
difference with global or dedicated shells is that these ones are
|
|
|
|
|
attached to a configuration, not a buffer. This means that can
|
|
|
|
|
be used for example to retrieve the sys.path and other stuff,
|
|
|
|
|
without messing with user shells. Note that
|
2014-07-26 20:43:51 -03:00
|
|
|
|
`python-shell-font-lock-enable' and `inferior-python-mode-hook'
|
2012-07-31 20:43:31 -03:00
|
|
|
|
are set to nil for these shells, so setup codes are not sent at
|
|
|
|
|
startup."
|
2014-07-26 20:43:51 -03:00
|
|
|
|
(let ((python-shell-font-lock-enable nil)
|
2012-07-31 20:43:31 -03:00
|
|
|
|
(inferior-python-mode-hook nil))
|
2012-08-06 00:27:43 -03:00
|
|
|
|
(get-buffer-process
|
|
|
|
|
(python-shell-make-comint
|
|
|
|
|
(python-shell-parse-command)
|
|
|
|
|
(python-shell-internal-get-process-name) nil t))))
|
2012-05-17 00:03:21 -03:00
|
|
|
|
|
2013-10-29 21:28:36 -04:00
|
|
|
|
(defun python-shell-get-buffer ()
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Return inferior Python buffer for current buffer."
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(let* ((dedicated-proc-name (python-shell-get-process-name t))
|
|
|
|
|
(dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name))
|
|
|
|
|
(global-proc-name (python-shell-get-process-name nil))
|
|
|
|
|
(global-proc-buffer-name (format "*%s*" global-proc-name))
|
|
|
|
|
(dedicated-running (comint-check-proc dedicated-proc-buffer-name))
|
|
|
|
|
(global-running (comint-check-proc global-proc-buffer-name)))
|
|
|
|
|
;; Always prefer dedicated
|
2013-10-29 21:28:36 -04:00
|
|
|
|
(or (and dedicated-running dedicated-proc-buffer-name)
|
|
|
|
|
(and global-running global-proc-buffer-name))))
|
|
|
|
|
|
|
|
|
|
(defun python-shell-get-process ()
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Return inferior Python process for current buffer."
|
2013-10-29 21:28:36 -04:00
|
|
|
|
(get-buffer-process (python-shell-get-buffer)))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2014-07-19 10:13:07 -03:00
|
|
|
|
(defun python-shell-get-or-create-process (&optional cmd dedicated show)
|
|
|
|
|
"Get or create an inferior Python process for current buffer and return it.
|
|
|
|
|
Arguments CMD, DEDICATED and SHOW are those of `run-python' and
|
|
|
|
|
are used to start the shell. If those arguments are not
|
|
|
|
|
provided, `run-python' is called interactively and the user will
|
|
|
|
|
be asked for their values."
|
2012-05-17 00:03:41 -03:00
|
|
|
|
(let* ((dedicated-proc-name (python-shell-get-process-name t))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name))
|
|
|
|
|
(global-proc-name (python-shell-get-process-name nil))
|
|
|
|
|
(global-proc-buffer-name (format "*%s*" global-proc-name))
|
|
|
|
|
(dedicated-running (comint-check-proc dedicated-proc-buffer-name))
|
2014-05-14 14:17:05 -04:00
|
|
|
|
(global-running (comint-check-proc global-proc-buffer-name)))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(when (and (not dedicated-running) (not global-running))
|
2014-07-19 10:13:07 -03:00
|
|
|
|
(if (if (not cmd)
|
|
|
|
|
;; XXX: Refactor code such that calling `run-python'
|
|
|
|
|
;; interactively is not needed anymore.
|
|
|
|
|
(call-interactively 'run-python)
|
|
|
|
|
(run-python cmd dedicated show))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(setq dedicated-running t)
|
|
|
|
|
(setq global-running t)))
|
|
|
|
|
;; Always prefer dedicated
|
|
|
|
|
(get-buffer-process (if dedicated-running
|
|
|
|
|
dedicated-proc-buffer-name
|
|
|
|
|
global-proc-buffer-name))))
|
|
|
|
|
|
2012-05-17 00:03:32 -03:00
|
|
|
|
(defvar python-shell-internal-buffer nil
|
|
|
|
|
"Current internal shell buffer for the current buffer.
|
|
|
|
|
This is really not necessary at all for the code to work but it's
|
|
|
|
|
there for compatibility with CEDET.")
|
|
|
|
|
|
2012-07-31 20:43:31 -03:00
|
|
|
|
(defvar python-shell-internal-last-output nil
|
|
|
|
|
"Last output captured by the internal shell.
|
|
|
|
|
This is really not necessary at all for the code to work but it's
|
|
|
|
|
there for compatibility with CEDET.")
|
|
|
|
|
|
2012-05-17 00:03:21 -03:00
|
|
|
|
(defun python-shell-internal-get-or-create-process ()
|
|
|
|
|
"Get or create an inferior Internal Python process."
|
|
|
|
|
(let* ((proc-name (python-shell-internal-get-process-name))
|
2012-08-06 00:27:43 -03:00
|
|
|
|
(proc-buffer-name (format " *%s*" proc-name)))
|
2012-07-31 20:43:31 -03:00
|
|
|
|
(when (not (process-live-p proc-name))
|
|
|
|
|
(run-python-internal)
|
|
|
|
|
(setq python-shell-internal-buffer proc-buffer-name)
|
|
|
|
|
;; XXX: Why is this `sit-for' needed?
|
|
|
|
|
;; `python-shell-make-comint' calls `accept-process-output'
|
|
|
|
|
;; already but it is not helping to get proper output on
|
|
|
|
|
;; 'gnu/linux when the internal shell process is not running and
|
|
|
|
|
;; a call to `python-shell-internal-send-string' is issued.
|
|
|
|
|
(sit-for 0.1 t))
|
2012-05-17 00:03:21 -03:00
|
|
|
|
(get-buffer-process proc-buffer-name)))
|
|
|
|
|
|
2012-05-17 00:03:32 -03:00
|
|
|
|
(define-obsolete-function-alias
|
2012-08-15 09:29:11 -07:00
|
|
|
|
'python-proc 'python-shell-internal-get-or-create-process "24.3")
|
2012-05-17 00:03:32 -03:00
|
|
|
|
|
|
|
|
|
(define-obsolete-variable-alias
|
2012-08-15 09:29:11 -07:00
|
|
|
|
'python-buffer 'python-shell-internal-buffer "24.3")
|
2012-05-17 00:03:32 -03:00
|
|
|
|
|
2012-07-31 20:43:31 -03:00
|
|
|
|
(define-obsolete-variable-alias
|
2012-08-15 09:29:11 -07:00
|
|
|
|
'python-preoutput-result 'python-shell-internal-last-output "24.3")
|
2012-07-31 20:43:31 -03:00
|
|
|
|
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(defun python-shell--save-temp-file (string)
|
|
|
|
|
(let* ((temporary-file-directory
|
|
|
|
|
(if (file-remote-p default-directory)
|
|
|
|
|
(concat (file-remote-p default-directory) "/tmp")
|
|
|
|
|
temporary-file-directory))
|
|
|
|
|
(temp-file-name (make-temp-file "py"))
|
|
|
|
|
(coding-system-for-write 'utf-8))
|
|
|
|
|
(with-temp-file temp-file-name
|
|
|
|
|
(insert "# -*- coding: utf-8 -*-\n") ;Not needed for Python-3.
|
|
|
|
|
(insert string)
|
|
|
|
|
(delete-trailing-whitespace))
|
|
|
|
|
temp-file-name))
|
|
|
|
|
|
|
|
|
|
(defun python-shell-send-string (string &optional process)
|
2014-02-04 16:35:52 -03:00
|
|
|
|
"Send STRING to inferior Python PROCESS."
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(interactive "sPython command: ")
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(let ((process (or process (python-shell-get-or-create-process))))
|
|
|
|
|
(if (string-match ".\n+." string) ;Multiline.
|
|
|
|
|
(let* ((temp-file-name (python-shell--save-temp-file string)))
|
2013-11-23 11:39:50 -08:00
|
|
|
|
(python-shell-send-file temp-file-name process temp-file-name t))
|
2012-05-17 00:03:06 -03:00
|
|
|
|
(comint-send-string process string)
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(when (or (not (string-match "\n\\'" string))
|
|
|
|
|
(string-match "\n[ \t].*\n?\\'" string))
|
|
|
|
|
(comint-send-string process "\n")))))
|
2012-05-17 00:03:06 -03:00
|
|
|
|
|
2012-10-07 01:33:16 -03:00
|
|
|
|
(defvar python-shell-output-filter-in-progress nil)
|
|
|
|
|
(defvar python-shell-output-filter-buffer nil)
|
|
|
|
|
|
|
|
|
|
(defun python-shell-output-filter (string)
|
|
|
|
|
"Filter used in `python-shell-send-string-no-output' to grab output.
|
|
|
|
|
STRING is the output received to this point from the process.
|
|
|
|
|
This filter saves received output from the process in
|
|
|
|
|
`python-shell-output-filter-buffer' and stops receiving it after
|
|
|
|
|
detecting a prompt at the end of the buffer."
|
|
|
|
|
(setq
|
|
|
|
|
string (ansi-color-filter-apply string)
|
|
|
|
|
python-shell-output-filter-buffer
|
|
|
|
|
(concat python-shell-output-filter-buffer string))
|
2014-07-26 20:43:51 -03:00
|
|
|
|
(when (python-shell-comint-end-of-output-p
|
2012-10-07 01:33:16 -03:00
|
|
|
|
python-shell-output-filter-buffer)
|
|
|
|
|
;; Output ends when `python-shell-output-filter-buffer' contains
|
|
|
|
|
;; the prompt attached at the end of it.
|
|
|
|
|
(setq python-shell-output-filter-in-progress nil
|
|
|
|
|
python-shell-output-filter-buffer
|
|
|
|
|
(substring python-shell-output-filter-buffer
|
|
|
|
|
0 (match-beginning 0)))
|
2014-07-19 10:13:07 -03:00
|
|
|
|
(when (string-match
|
|
|
|
|
python-shell--prompt-calculated-output-regexp
|
|
|
|
|
python-shell-output-filter-buffer)
|
2014-07-20 15:12:30 -03:00
|
|
|
|
;; Some shells, like IPython might append a prompt before the
|
2012-10-07 01:33:16 -03:00
|
|
|
|
;; output, clean that.
|
|
|
|
|
(setq python-shell-output-filter-buffer
|
|
|
|
|
(substring python-shell-output-filter-buffer (match-end 0)))))
|
|
|
|
|
"")
|
2012-09-30 21:53:44 -03:00
|
|
|
|
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(defun python-shell-send-string-no-output (string &optional process)
|
2012-05-17 00:03:06 -03:00
|
|
|
|
"Send STRING to PROCESS and inhibit output.
|
2014-02-04 16:35:52 -03:00
|
|
|
|
Return the output."
|
2012-09-30 21:53:44 -03:00
|
|
|
|
(let ((process (or process (python-shell-get-or-create-process)))
|
|
|
|
|
(comint-preoutput-filter-functions
|
2012-10-07 01:33:16 -03:00
|
|
|
|
'(python-shell-output-filter))
|
|
|
|
|
(python-shell-output-filter-in-progress t)
|
2012-09-30 21:53:44 -03:00
|
|
|
|
(inhibit-quit t))
|
2012-07-17 13:47:58 -03:00
|
|
|
|
(or
|
|
|
|
|
(with-local-quit
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(python-shell-send-string string process)
|
2012-10-07 01:33:16 -03:00
|
|
|
|
(while python-shell-output-filter-in-progress
|
|
|
|
|
;; `python-shell-output-filter' takes care of setting
|
|
|
|
|
;; `python-shell-output-filter-in-progress' to NIL after it
|
|
|
|
|
;; detects end of output.
|
2012-09-30 21:53:44 -03:00
|
|
|
|
(accept-process-output process))
|
|
|
|
|
(prog1
|
2012-10-07 01:33:16 -03:00
|
|
|
|
python-shell-output-filter-buffer
|
|
|
|
|
(setq python-shell-output-filter-buffer nil)))
|
2012-07-17 13:47:58 -03:00
|
|
|
|
(with-current-buffer (process-buffer process)
|
|
|
|
|
(comint-interrupt-subjob)))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:21 -03:00
|
|
|
|
(defun python-shell-internal-send-string (string)
|
|
|
|
|
"Send STRING to the Internal Python interpreter.
|
|
|
|
|
Returns the output. See `python-shell-send-string-no-output'."
|
2012-07-31 20:43:31 -03:00
|
|
|
|
;; XXX Remove `python-shell-internal-last-output' once CEDET is
|
|
|
|
|
;; updated to support this new mode.
|
|
|
|
|
(setq python-shell-internal-last-output
|
|
|
|
|
(python-shell-send-string-no-output
|
|
|
|
|
;; Makes this function compatible with the old
|
|
|
|
|
;; python-send-receive. (At least for CEDET).
|
|
|
|
|
(replace-regexp-in-string "_emacs_out +" "" string)
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(python-shell-internal-get-or-create-process))))
|
2012-05-17 00:03:21 -03:00
|
|
|
|
|
|
|
|
|
(define-obsolete-function-alias
|
2012-08-15 09:29:11 -07:00
|
|
|
|
'python-send-receive 'python-shell-internal-send-string "24.3")
|
2012-05-17 00:03:32 -03:00
|
|
|
|
|
|
|
|
|
(define-obsolete-function-alias
|
2012-08-15 09:29:11 -07:00
|
|
|
|
'python-send-string 'python-shell-internal-send-string "24.3")
|
2012-05-17 00:03:21 -03:00
|
|
|
|
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(defvar python--use-fake-loc nil
|
|
|
|
|
"If non-nil, use `compilation-fake-loc' to trace errors back to the buffer.
|
|
|
|
|
If nil, regions of text are prepended by the corresponding number of empty
|
|
|
|
|
lines and Python is told to output error messages referring to the whole
|
|
|
|
|
source file.")
|
|
|
|
|
|
2013-09-02 10:56:03 -03:00
|
|
|
|
(defun python-shell-buffer-substring (start end &optional nomain)
|
|
|
|
|
"Send buffer substring from START to END formatted for shell.
|
|
|
|
|
This is a wrapper over `buffer-substring' that takes care of
|
|
|
|
|
different transformations for the code sent to be evaluated in
|
|
|
|
|
the python shell:
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
1. When optional argument NOMAIN is non-nil everything under an
|
2013-09-02 10:56:03 -03:00
|
|
|
|
\"if __name__ == '__main__'\" block will be removed.
|
|
|
|
|
2. When a subregion of the buffer is sent, it takes care of
|
2013-09-09 09:26:38 -07:00
|
|
|
|
appending extra empty lines so tracebacks are correct.
|
2013-09-02 10:56:03 -03:00
|
|
|
|
3. Wraps indented regions under an \"if True:\" block so the
|
|
|
|
|
interpreter evaluates them correctly."
|
|
|
|
|
(let ((substring (buffer-substring-no-properties start end))
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(fillstr (unless python--use-fake-loc
|
|
|
|
|
(make-string (1- (line-number-at-pos start)) ?\n)))
|
2013-09-02 10:56:03 -03:00
|
|
|
|
(toplevel-block-p (save-excursion
|
|
|
|
|
(goto-char start)
|
|
|
|
|
(or (zerop (line-number-at-pos start))
|
|
|
|
|
(progn
|
|
|
|
|
(python-util-forward-comment 1)
|
|
|
|
|
(zerop (current-indentation)))))))
|
|
|
|
|
(with-temp-buffer
|
|
|
|
|
(python-mode)
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(if fillstr (insert fillstr))
|
2013-09-02 10:56:03 -03:00
|
|
|
|
(insert substring)
|
|
|
|
|
(goto-char (point-min))
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(unless python--use-fake-loc
|
|
|
|
|
;; python-shell--save-temp-file adds an extra coding line, which would
|
|
|
|
|
;; throw off the line-counts, so let's try to compensate here.
|
|
|
|
|
(if (looking-at "[ \t]*[#\n]")
|
|
|
|
|
(delete-region (point) (line-beginning-position 2))))
|
2013-09-02 10:56:03 -03:00
|
|
|
|
(when (not toplevel-block-p)
|
|
|
|
|
(insert "if True:")
|
|
|
|
|
(delete-region (point) (line-end-position)))
|
|
|
|
|
(when nomain
|
|
|
|
|
(let* ((if-name-main-start-end
|
|
|
|
|
(and nomain
|
|
|
|
|
(save-excursion
|
|
|
|
|
(when (python-nav-if-name-main)
|
|
|
|
|
(cons (point)
|
2013-12-25 15:07:31 -03:00
|
|
|
|
(progn (python-nav-forward-sexp-safe)
|
2013-09-02 10:56:03 -03:00
|
|
|
|
(point)))))))
|
|
|
|
|
;; Oh destructuring bind, how I miss you.
|
|
|
|
|
(if-name-main-start (car if-name-main-start-end))
|
|
|
|
|
(if-name-main-end (cdr if-name-main-start-end)))
|
|
|
|
|
(when if-name-main-start-end
|
|
|
|
|
(goto-char if-name-main-start)
|
|
|
|
|
(delete-region if-name-main-start if-name-main-end)
|
|
|
|
|
(insert
|
|
|
|
|
(make-string
|
|
|
|
|
(- (line-number-at-pos if-name-main-end)
|
|
|
|
|
(line-number-at-pos if-name-main-start)) ?\n)))))
|
|
|
|
|
(buffer-substring-no-properties (point-min) (point-max)))))
|
|
|
|
|
|
2013-10-29 21:28:36 -04:00
|
|
|
|
(declare-function compilation-fake-loc "compile"
|
|
|
|
|
(marker file &optional line col))
|
|
|
|
|
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(defun python-shell-send-region (start end &optional nomain)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
"Send the region delimited by START and END to inferior Python process."
|
|
|
|
|
(interactive "r")
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(let* ((python--use-fake-loc
|
|
|
|
|
(or python--use-fake-loc (not buffer-file-name)))
|
|
|
|
|
(string (python-shell-buffer-substring start end nomain))
|
|
|
|
|
(process (python-shell-get-or-create-process))
|
|
|
|
|
(_ (string-match "\\`\n*\\(.*\\)" string)))
|
|
|
|
|
(message "Sent: %s..." (match-string 1 string))
|
|
|
|
|
(let* ((temp-file-name (python-shell--save-temp-file string))
|
|
|
|
|
(file-name (or (buffer-file-name) temp-file-name)))
|
2013-11-23 11:39:50 -08:00
|
|
|
|
(python-shell-send-file file-name process temp-file-name t)
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(unless python--use-fake-loc
|
|
|
|
|
(with-current-buffer (process-buffer process)
|
|
|
|
|
(compilation-fake-loc (copy-marker start) temp-file-name
|
|
|
|
|
2)) ;; Not 1, because of the added coding line.
|
|
|
|
|
))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:39 -03:00
|
|
|
|
(defun python-shell-send-buffer (&optional arg)
|
|
|
|
|
"Send the entire buffer to inferior Python process.
|
2012-09-24 20:15:40 -03:00
|
|
|
|
With prefix ARG allow execution of code inside blocks delimited
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
by \"if __name__== '__main__':\"."
|
2012-05-17 00:03:39 -03:00
|
|
|
|
(interactive "P")
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(save-restriction
|
|
|
|
|
(widen)
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(python-shell-send-region (point-min) (point-max) (not arg))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defun python-shell-send-defun (arg)
|
2012-05-17 00:03:12 -03:00
|
|
|
|
"Send the current defun to inferior Python process.
|
2012-05-17 00:03:44 -03:00
|
|
|
|
When argument ARG is non-nil do not include decorators."
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(interactive "P")
|
|
|
|
|
(save-excursion
|
2012-05-17 00:03:12 -03:00
|
|
|
|
(python-shell-send-region
|
|
|
|
|
(progn
|
2012-05-17 00:03:44 -03:00
|
|
|
|
(end-of-line 1)
|
2012-11-12 10:26:50 -03:00
|
|
|
|
(while (and (or (python-nav-beginning-of-defun)
|
2012-05-17 00:03:44 -03:00
|
|
|
|
(beginning-of-line 1))
|
|
|
|
|
(> (current-indentation) 0)))
|
|
|
|
|
(when (not arg)
|
|
|
|
|
(while (and (forward-line -1)
|
|
|
|
|
(looking-at (python-rx decorator))))
|
|
|
|
|
(forward-line 1))
|
2012-05-17 00:03:40 -03:00
|
|
|
|
(point-marker))
|
2012-05-17 00:03:12 -03:00
|
|
|
|
(progn
|
2012-11-12 10:26:50 -03:00
|
|
|
|
(or (python-nav-end-of-defun)
|
2012-05-17 00:03:44 -03:00
|
|
|
|
(end-of-line 1))
|
2012-05-17 00:03:40 -03:00
|
|
|
|
(point-marker)))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2013-11-23 11:39:50 -08:00
|
|
|
|
(defun python-shell-send-file (file-name &optional process temp-file-name
|
|
|
|
|
delete)
|
2012-05-17 00:03:03 -03:00
|
|
|
|
"Send FILE-NAME to inferior Python PROCESS.
|
|
|
|
|
If TEMP-FILE-NAME is passed then that file is used for processing
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
instead, while internally the shell will continue to use FILE-NAME.
|
|
|
|
|
If DELETE is non-nil, delete the file afterwards."
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(interactive "fFile to send: ")
|
2012-05-17 00:03:06 -03:00
|
|
|
|
(let* ((process (or process (python-shell-get-or-create-process)))
|
|
|
|
|
(temp-file-name (when temp-file-name
|
2012-09-07 16:53:15 +02:00
|
|
|
|
(expand-file-name
|
|
|
|
|
(or (file-remote-p temp-file-name 'localname)
|
|
|
|
|
temp-file-name))))
|
|
|
|
|
(file-name (or (when file-name
|
|
|
|
|
(expand-file-name
|
|
|
|
|
(or (file-remote-p file-name 'localname)
|
|
|
|
|
file-name)))
|
|
|
|
|
temp-file-name)))
|
2012-05-17 00:03:06 -03:00
|
|
|
|
(when (not file-name)
|
|
|
|
|
(error "If FILE-NAME is nil then TEMP-FILE-NAME must be non-nil"))
|
2012-05-17 00:02:56 -03:00
|
|
|
|
(python-shell-send-string
|
2012-05-17 00:02:54 -03:00
|
|
|
|
(format
|
2012-05-17 00:03:03 -03:00
|
|
|
|
(concat "__pyfile = open('''%s''');"
|
|
|
|
|
"exec(compile(__pyfile.read(), '''%s''', 'exec'));"
|
2013-11-23 11:39:50 -08:00
|
|
|
|
"__pyfile.close()%s")
|
|
|
|
|
(or temp-file-name file-name) file-name
|
|
|
|
|
(if delete (format "; import os; os.remove('''%s''')"
|
|
|
|
|
(or temp-file-name file-name))
|
|
|
|
|
""))
|
2012-05-17 00:02:56 -03:00
|
|
|
|
process)))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defun python-shell-switch-to-shell ()
|
|
|
|
|
"Switch to inferior Python process buffer."
|
|
|
|
|
(interactive)
|
|
|
|
|
(pop-to-buffer (process-buffer (python-shell-get-or-create-process)) t))
|
|
|
|
|
|
2012-05-17 00:03:09 -03:00
|
|
|
|
(defun python-shell-send-setup-code ()
|
|
|
|
|
"Send all setup code for shell.
|
|
|
|
|
This function takes the list of setup code to send from the
|
|
|
|
|
`python-shell-setup-codes' list."
|
2012-08-14 10:39:27 -03:00
|
|
|
|
(let ((process (get-buffer-process (current-buffer))))
|
2012-05-17 00:03:09 -03:00
|
|
|
|
(dolist (code python-shell-setup-codes)
|
|
|
|
|
(when code
|
2012-08-14 10:39:27 -03:00
|
|
|
|
(message "Sent %s" code)
|
2012-05-17 00:03:32 -03:00
|
|
|
|
(python-shell-send-string
|
2012-05-17 00:03:09 -03:00
|
|
|
|
(symbol-value code) process)))))
|
|
|
|
|
|
|
|
|
|
(add-hook 'inferior-python-mode-hook
|
|
|
|
|
#'python-shell-send-setup-code)
|
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
;;; Shell completion
|
|
|
|
|
|
2012-05-17 00:03:23 -03:00
|
|
|
|
(defcustom python-shell-completion-setup-code
|
2012-05-17 00:02:52 -03:00
|
|
|
|
"try:
|
2014-07-20 15:12:30 -03:00
|
|
|
|
import readline, rlcompleter
|
2012-05-17 00:02:52 -03:00
|
|
|
|
except ImportError:
|
2014-07-20 15:12:30 -03:00
|
|
|
|
def __PYTHON_EL_get_completions(text):
|
|
|
|
|
return []
|
2012-05-17 00:02:52 -03:00
|
|
|
|
else:
|
2014-07-20 15:12:30 -03:00
|
|
|
|
def __PYTHON_EL_get_completions(text):
|
2012-05-17 00:02:52 -03:00
|
|
|
|
completions = []
|
|
|
|
|
try:
|
2014-07-20 15:12:30 -03:00
|
|
|
|
splits = text.split()
|
|
|
|
|
is_module = splits and splits[0] in ('from', 'import')
|
|
|
|
|
is_ipython = getattr(
|
|
|
|
|
__builtins__, '__IPYTHON__',
|
|
|
|
|
getattr(__builtins__, '__IPYTHON__active', False))
|
|
|
|
|
if is_module:
|
|
|
|
|
from IPython.core.completerlib import module_completion
|
|
|
|
|
completions = module_completion(text.strip())
|
|
|
|
|
elif is_ipython and getattr(__builtins__, '__IP', None):
|
|
|
|
|
completions = __IP.complete(text)
|
|
|
|
|
elif is_ipython and getattr(__builtins__, 'get_ipython', None):
|
|
|
|
|
completions = get_ipython().Completer.all_completions(text)
|
|
|
|
|
else:
|
|
|
|
|
i = 0
|
|
|
|
|
while True:
|
|
|
|
|
res = readline.get_completer()(text, i)
|
|
|
|
|
if not res:
|
|
|
|
|
break
|
|
|
|
|
i += 1
|
|
|
|
|
completions.append(res)
|
|
|
|
|
except:
|
2012-05-17 00:02:52 -03:00
|
|
|
|
pass
|
|
|
|
|
return completions"
|
2012-05-17 00:03:23 -03:00
|
|
|
|
"Code used to setup completion in inferior Python processes."
|
|
|
|
|
:type 'string
|
2012-05-17 00:03:33 -03:00
|
|
|
|
:group 'python)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:23 -03:00
|
|
|
|
(defcustom python-shell-completion-string-code
|
2014-07-20 15:12:30 -03:00
|
|
|
|
"';'.join(__PYTHON_EL_get_completions('''%s'''))\n"
|
|
|
|
|
"Python code used to get a string of completions separated by semicolons.
|
|
|
|
|
The string passed to the function is the current python name or
|
|
|
|
|
the full statement in the case of imports."
|
2012-05-17 00:03:23 -03:00
|
|
|
|
:type 'string
|
2012-05-17 00:03:33 -03:00
|
|
|
|
:group 'python)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2014-07-20 15:12:30 -03:00
|
|
|
|
(define-obsolete-variable-alias
|
|
|
|
|
'python-shell-completion-module-string-code
|
|
|
|
|
'python-shell-completion-string-code
|
|
|
|
|
"24.4"
|
|
|
|
|
"Completion string code must also autocomplete modules.")
|
2012-05-17 00:03:27 -03:00
|
|
|
|
|
2012-05-17 00:03:31 -03:00
|
|
|
|
(defcustom python-shell-completion-pdb-string-code
|
|
|
|
|
"';'.join(globals().keys() + locals().keys())"
|
|
|
|
|
"Python code used to get completions separated by semicolons for [i]pdb."
|
|
|
|
|
:type 'string
|
2012-05-17 00:03:33 -03:00
|
|
|
|
:group 'python)
|
2012-05-17 00:03:31 -03:00
|
|
|
|
|
2012-08-14 01:18:41 -03:00
|
|
|
|
(defun python-shell-completion-get-completions (process line input)
|
|
|
|
|
"Do completion at point for PROCESS.
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
LINE is used to detect the context on how to complete given INPUT."
|
2012-08-14 01:18:41 -03:00
|
|
|
|
(let* ((prompt
|
2013-09-02 00:21:13 -03:00
|
|
|
|
;; Get last prompt of the inferior process buffer (this
|
|
|
|
|
;; intentionally avoids using `comint-last-prompt' because
|
|
|
|
|
;; of incompatibilities with Emacs 24.x).
|
2012-08-14 01:18:41 -03:00
|
|
|
|
(with-current-buffer (process-buffer process)
|
2013-09-02 00:21:13 -03:00
|
|
|
|
(save-excursion
|
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
|
(- (point) (length line))
|
|
|
|
|
(progn
|
|
|
|
|
(re-search-backward "^")
|
|
|
|
|
(python-util-forward-comment)
|
|
|
|
|
(point))))))
|
2014-07-20 15:12:30 -03:00
|
|
|
|
(completion-code
|
2012-08-14 01:18:41 -03:00
|
|
|
|
;; Check whether a prompt matches a pdb string, an import
|
|
|
|
|
;; statement or just the standard prompt and use the
|
|
|
|
|
;; correct python-shell-completion-*-code string
|
|
|
|
|
(cond ((and (> (length python-shell-completion-pdb-string-code) 0)
|
|
|
|
|
(string-match
|
|
|
|
|
(concat "^" python-shell-prompt-pdb-regexp) prompt))
|
2014-07-20 15:12:30 -03:00
|
|
|
|
python-shell-completion-pdb-string-code)
|
2012-08-14 01:18:41 -03:00
|
|
|
|
((string-match
|
2014-07-19 10:13:07 -03:00
|
|
|
|
python-shell--prompt-calculated-input-regexp prompt)
|
2014-07-20 15:12:30 -03:00
|
|
|
|
python-shell-completion-string-code)
|
2012-08-14 01:18:41 -03:00
|
|
|
|
(t nil)))
|
|
|
|
|
(input
|
2014-07-20 15:12:30 -03:00
|
|
|
|
(if (string-match
|
|
|
|
|
(python-rx (+ space) (or "from" "import") space)
|
|
|
|
|
line)
|
|
|
|
|
line
|
2012-08-14 01:18:41 -03:00
|
|
|
|
input)))
|
|
|
|
|
(and completion-code
|
|
|
|
|
(> (length input) 0)
|
|
|
|
|
(with-current-buffer (process-buffer process)
|
2014-06-21 11:14:54 -03:00
|
|
|
|
(let ((completions
|
|
|
|
|
(python-util-strip-string
|
|
|
|
|
(python-shell-send-string-no-output
|
|
|
|
|
(format completion-code input) process))))
|
2012-08-14 01:18:41 -03:00
|
|
|
|
(and (> (length completions) 2)
|
|
|
|
|
(split-string completions
|
|
|
|
|
"^'\\|^\"\\|;\\|'$\\|\"$" t)))))))
|
|
|
|
|
|
|
|
|
|
(defun python-shell-completion-complete-at-point (&optional process)
|
|
|
|
|
"Perform completion at point in inferior Python.
|
|
|
|
|
Optional argument PROCESS forces completions to be retrieved
|
|
|
|
|
using that one instead of current buffer's process."
|
|
|
|
|
(setq process (or process (get-buffer-process (current-buffer))))
|
|
|
|
|
(let* ((start
|
|
|
|
|
(save-excursion
|
|
|
|
|
(with-syntax-table python-dotty-syntax-table
|
2012-05-17 00:03:37 -03:00
|
|
|
|
(let* ((paren-depth (car (syntax-ppss)))
|
|
|
|
|
(syntax-string "w_")
|
|
|
|
|
(syntax-list (string-to-syntax syntax-string)))
|
2012-08-14 01:18:41 -03:00
|
|
|
|
;; Stop scanning for the beginning of the completion
|
|
|
|
|
;; subject after the char before point matches a
|
|
|
|
|
;; delimiter
|
|
|
|
|
(while (member
|
|
|
|
|
(car (syntax-after (1- (point)))) syntax-list)
|
2012-05-17 00:03:37 -03:00
|
|
|
|
(skip-syntax-backward syntax-string)
|
|
|
|
|
(when (or (equal (char-before) ?\))
|
|
|
|
|
(equal (char-before) ?\"))
|
|
|
|
|
(forward-char -1))
|
|
|
|
|
(while (or
|
|
|
|
|
;; honor initial paren depth
|
|
|
|
|
(> (car (syntax-ppss)) paren-depth)
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(python-syntax-context 'string))
|
2012-08-14 01:18:41 -03:00
|
|
|
|
(forward-char -1)))
|
|
|
|
|
(point)))))
|
|
|
|
|
(end (point)))
|
|
|
|
|
(list start end
|
|
|
|
|
(completion-table-dynamic
|
|
|
|
|
(apply-partially
|
|
|
|
|
#'python-shell-completion-get-completions
|
|
|
|
|
process (buffer-substring-no-properties
|
|
|
|
|
(line-beginning-position) end))))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defun python-shell-completion-complete-or-indent ()
|
|
|
|
|
"Complete or indent depending on the context.
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
If content before pointer is all whitespace, indent.
|
|
|
|
|
If not try to complete."
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(interactive)
|
|
|
|
|
(if (string-match "^[[:space:]]*$"
|
|
|
|
|
(buffer-substring (comint-line-beginning-position)
|
|
|
|
|
(point-marker)))
|
|
|
|
|
(indent-for-tab-command)
|
2012-05-17 00:03:38 -03:00
|
|
|
|
(completion-at-point)))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; PDB Track integration
|
|
|
|
|
|
2012-05-17 00:03:31 -03:00
|
|
|
|
(defcustom python-pdbtrack-activate t
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Non-nil makes Python shell enable pdbtracking."
|
2012-05-17 00:03:31 -03:00
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'python
|
|
|
|
|
:safe 'booleanp)
|
|
|
|
|
|
2012-05-17 00:03:23 -03:00
|
|
|
|
(defcustom python-pdbtrack-stacktrace-info-regexp
|
2013-12-12 21:56:12 -03:00
|
|
|
|
"> \\([^\"(<]+\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_<>]+\\)()"
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Regular expression matching stacktrace information.
|
2012-05-17 00:03:28 -03:00
|
|
|
|
Used to extract the current line and module being inspected."
|
2012-05-17 00:03:23 -03:00
|
|
|
|
:type 'string
|
|
|
|
|
:group 'python
|
|
|
|
|
:safe 'stringp)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:28 -03:00
|
|
|
|
(defvar python-pdbtrack-tracked-buffer nil
|
|
|
|
|
"Variable containing the value of the current tracked buffer.
|
|
|
|
|
Never set this variable directly, use
|
|
|
|
|
`python-pdbtrack-set-tracked-buffer' instead.")
|
|
|
|
|
|
|
|
|
|
(defvar python-pdbtrack-buffers-to-kill nil
|
|
|
|
|
"List of buffers to be deleted after tracking finishes.")
|
|
|
|
|
|
|
|
|
|
(defun python-pdbtrack-set-tracked-buffer (file-name)
|
|
|
|
|
"Set the buffer for FILE-NAME as the tracked buffer.
|
|
|
|
|
Internally it uses the `python-pdbtrack-tracked-buffer' variable.
|
|
|
|
|
Returns the tracked buffer."
|
2013-12-12 21:56:12 -03:00
|
|
|
|
(let ((file-buffer (get-file-buffer
|
|
|
|
|
(concat (file-remote-p default-directory)
|
|
|
|
|
file-name))))
|
2012-05-17 00:03:28 -03:00
|
|
|
|
(if file-buffer
|
|
|
|
|
(setq python-pdbtrack-tracked-buffer file-buffer)
|
|
|
|
|
(setq file-buffer (find-file-noselect file-name))
|
|
|
|
|
(when (not (member file-buffer python-pdbtrack-buffers-to-kill))
|
|
|
|
|
(add-to-list 'python-pdbtrack-buffers-to-kill file-buffer)))
|
|
|
|
|
file-buffer))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defun python-pdbtrack-comint-output-filter-function (output)
|
|
|
|
|
"Move overlay arrow to current pdb line in tracked buffer.
|
|
|
|
|
Argument OUTPUT is a string with the output from the comint process."
|
2012-05-17 00:03:31 -03:00
|
|
|
|
(when (and python-pdbtrack-activate (not (string= output "")))
|
2012-05-17 00:03:28 -03:00
|
|
|
|
(let* ((full-output (ansi-color-filter-apply
|
|
|
|
|
(buffer-substring comint-last-input-end (point-max))))
|
|
|
|
|
(line-number)
|
|
|
|
|
(file-name
|
|
|
|
|
(with-temp-buffer
|
|
|
|
|
(insert full-output)
|
2013-01-30 12:02:58 -03:00
|
|
|
|
;; When the debugger encounters a pdb.set_trace()
|
|
|
|
|
;; command, it prints a single stack frame. Sometimes
|
|
|
|
|
;; it prints a bit of extra information about the
|
|
|
|
|
;; arguments of the present function. When ipdb
|
|
|
|
|
;; encounters an exception, it prints the _entire_ stack
|
|
|
|
|
;; trace. To handle all of these cases, we want to find
|
|
|
|
|
;; the _last_ stack frame printed in the most recent
|
2013-02-01 22:04:06 -08:00
|
|
|
|
;; batch of output, then jump to the corresponding
|
2013-01-30 12:02:58 -03:00
|
|
|
|
;; file/line number.
|
|
|
|
|
(goto-char (point-max))
|
|
|
|
|
(when (re-search-backward python-pdbtrack-stacktrace-info-regexp nil t)
|
2012-05-17 00:03:28 -03:00
|
|
|
|
(setq line-number (string-to-number
|
|
|
|
|
(match-string-no-properties 2)))
|
|
|
|
|
(match-string-no-properties 1)))))
|
|
|
|
|
(if (and file-name line-number)
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(let* ((tracked-buffer
|
|
|
|
|
(python-pdbtrack-set-tracked-buffer file-name))
|
2012-05-17 00:03:28 -03:00
|
|
|
|
(shell-buffer (current-buffer))
|
|
|
|
|
(tracked-buffer-window (get-buffer-window tracked-buffer))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(tracked-buffer-line-pos))
|
2012-05-17 00:03:28 -03:00
|
|
|
|
(with-current-buffer tracked-buffer
|
2012-05-17 00:03:28 -03:00
|
|
|
|
(set (make-local-variable 'overlay-arrow-string) "=>")
|
|
|
|
|
(set (make-local-variable 'overlay-arrow-position) (make-marker))
|
|
|
|
|
(setq tracked-buffer-line-pos (progn
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(forward-line (1- line-number))
|
|
|
|
|
(point-marker)))
|
|
|
|
|
(when tracked-buffer-window
|
|
|
|
|
(set-window-point
|
|
|
|
|
tracked-buffer-window tracked-buffer-line-pos))
|
|
|
|
|
(set-marker overlay-arrow-position tracked-buffer-line-pos))
|
2012-05-17 00:03:28 -03:00
|
|
|
|
(pop-to-buffer tracked-buffer)
|
|
|
|
|
(switch-to-buffer-other-window shell-buffer))
|
|
|
|
|
(when python-pdbtrack-tracked-buffer
|
|
|
|
|
(with-current-buffer python-pdbtrack-tracked-buffer
|
|
|
|
|
(set-marker overlay-arrow-position nil))
|
|
|
|
|
(mapc #'(lambda (buffer)
|
|
|
|
|
(ignore-errors (kill-buffer buffer)))
|
|
|
|
|
python-pdbtrack-buffers-to-kill)
|
|
|
|
|
(setq python-pdbtrack-tracked-buffer nil
|
|
|
|
|
python-pdbtrack-buffers-to-kill nil)))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Symbol completion
|
|
|
|
|
|
|
|
|
|
(defun python-completion-complete-at-point ()
|
|
|
|
|
"Complete current symbol at point.
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
For this to work as best as possible you should call
|
2012-05-17 00:02:52 -03:00
|
|
|
|
`python-shell-send-buffer' from time to time so context in
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
inferior Python process is updated properly."
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(let ((process (python-shell-get-process)))
|
|
|
|
|
(if (not process)
|
2012-05-17 00:03:44 -03:00
|
|
|
|
(error "Completion needs an inferior Python process running")
|
2012-08-14 01:18:41 -03:00
|
|
|
|
(python-shell-completion-complete-at-point process))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(add-to-list 'debug-ignored-errors
|
|
|
|
|
"^Completion needs an inferior Python process running.")
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Fill paragraph
|
|
|
|
|
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(defcustom python-fill-comment-function 'python-fill-comment
|
|
|
|
|
"Function to fill comments.
|
2012-10-08 19:18:05 -03:00
|
|
|
|
This is the function used by `python-fill-paragraph' to
|
2012-05-17 00:03:11 -03:00
|
|
|
|
fill comments."
|
|
|
|
|
:type 'symbol
|
2012-10-04 13:39:37 -03:00
|
|
|
|
:group 'python)
|
2012-05-17 00:03:11 -03:00
|
|
|
|
|
|
|
|
|
(defcustom python-fill-string-function 'python-fill-string
|
|
|
|
|
"Function to fill strings.
|
2012-10-08 19:18:05 -03:00
|
|
|
|
This is the function used by `python-fill-paragraph' to
|
2012-05-17 00:03:11 -03:00
|
|
|
|
fill strings."
|
|
|
|
|
:type 'symbol
|
2012-10-04 13:39:37 -03:00
|
|
|
|
:group 'python)
|
2012-05-17 00:03:11 -03:00
|
|
|
|
|
|
|
|
|
(defcustom python-fill-decorator-function 'python-fill-decorator
|
|
|
|
|
"Function to fill decorators.
|
2012-10-08 19:18:05 -03:00
|
|
|
|
This is the function used by `python-fill-paragraph' to
|
2012-05-17 00:03:11 -03:00
|
|
|
|
fill decorators."
|
|
|
|
|
:type 'symbol
|
2012-10-04 13:39:37 -03:00
|
|
|
|
:group 'python)
|
2012-05-17 00:03:11 -03:00
|
|
|
|
|
|
|
|
|
(defcustom python-fill-paren-function 'python-fill-paren
|
|
|
|
|
"Function to fill parens.
|
2012-10-08 19:18:05 -03:00
|
|
|
|
This is the function used by `python-fill-paragraph' to
|
2012-05-17 00:03:11 -03:00
|
|
|
|
fill parens."
|
|
|
|
|
:type 'symbol
|
2012-10-04 13:39:37 -03:00
|
|
|
|
:group 'python)
|
|
|
|
|
|
2012-10-05 10:42:08 -03:00
|
|
|
|
(defcustom python-fill-docstring-style 'pep-257
|
2012-10-04 13:39:37 -03:00
|
|
|
|
"Style used to fill docstrings.
|
|
|
|
|
This affects `python-fill-string' behavior with regards to
|
|
|
|
|
triple quotes positioning.
|
|
|
|
|
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
Possible values are `django', `onetwo', `pep-257', `pep-257-nn',
|
|
|
|
|
`symmetric', and nil. A value of nil won't care about quotes
|
2012-10-05 10:42:08 -03:00
|
|
|
|
position and will treat docstrings a normal string, any other
|
|
|
|
|
value may result in one of the following docstring styles:
|
2012-10-04 13:39:37 -03:00
|
|
|
|
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
`django':
|
2012-10-04 13:39:37 -03:00
|
|
|
|
|
|
|
|
|
\"\"\"
|
|
|
|
|
Process foo, return bar.
|
|
|
|
|
\"\"\"
|
|
|
|
|
|
|
|
|
|
\"\"\"
|
|
|
|
|
Process foo, return bar.
|
|
|
|
|
|
|
|
|
|
If processing fails throw ProcessingError.
|
|
|
|
|
\"\"\"
|
|
|
|
|
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
`onetwo':
|
2012-10-05 10:42:08 -03:00
|
|
|
|
|
|
|
|
|
\"\"\"Process foo, return bar.\"\"\"
|
|
|
|
|
|
|
|
|
|
\"\"\"
|
|
|
|
|
Process foo, return bar.
|
|
|
|
|
|
|
|
|
|
If processing fails throw ProcessingError.
|
|
|
|
|
|
|
|
|
|
\"\"\"
|
|
|
|
|
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
`pep-257':
|
2012-10-04 13:39:37 -03:00
|
|
|
|
|
|
|
|
|
\"\"\"Process foo, return bar.\"\"\"
|
|
|
|
|
|
|
|
|
|
\"\"\"Process foo, return bar.
|
|
|
|
|
|
|
|
|
|
If processing fails throw ProcessingError.
|
|
|
|
|
|
|
|
|
|
\"\"\"
|
|
|
|
|
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
`pep-257-nn':
|
2012-10-04 13:39:37 -03:00
|
|
|
|
|
|
|
|
|
\"\"\"Process foo, return bar.\"\"\"
|
|
|
|
|
|
|
|
|
|
\"\"\"Process foo, return bar.
|
|
|
|
|
|
|
|
|
|
If processing fails throw ProcessingError.
|
|
|
|
|
\"\"\"
|
|
|
|
|
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
`symmetric':
|
2012-10-04 13:39:37 -03:00
|
|
|
|
|
|
|
|
|
\"\"\"Process foo, return bar.\"\"\"
|
|
|
|
|
|
|
|
|
|
\"\"\"
|
|
|
|
|
Process foo, return bar.
|
|
|
|
|
|
|
|
|
|
If processing fails throw ProcessingError.
|
|
|
|
|
\"\"\""
|
2012-10-05 10:42:08 -03:00
|
|
|
|
:type '(choice
|
|
|
|
|
(const :tag "Don't format docstrings" nil)
|
|
|
|
|
(const :tag "Django's coding standards style." django)
|
|
|
|
|
(const :tag "One newline and start and Two at end style." onetwo)
|
|
|
|
|
(const :tag "PEP-257 with 2 newlines at end of string." pep-257)
|
|
|
|
|
(const :tag "PEP-257 with 1 newline at end of string." pep-257-nn)
|
|
|
|
|
(const :tag "Symmetric style." symmetric))
|
2012-05-17 00:03:11 -03:00
|
|
|
|
:group 'python
|
2012-10-05 10:42:08 -03:00
|
|
|
|
:safe (lambda (val)
|
|
|
|
|
(memq val '(django onetwo pep-257 pep-257-nn symmetric nil))))
|
2012-05-17 00:03:11 -03:00
|
|
|
|
|
2012-10-08 19:18:05 -03:00
|
|
|
|
(defun python-fill-paragraph (&optional justify)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
"`fill-paragraph-function' handling multi-line strings and possibly comments.
|
|
|
|
|
If any of the current line is in or at the end of a multi-line string,
|
|
|
|
|
fill the string or the paragraph of it that point is in, preserving
|
2012-05-17 00:02:56 -03:00
|
|
|
|
the string's indentation.
|
|
|
|
|
Optional argument JUSTIFY defines if the paragraph should be justified."
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(interactive "P")
|
|
|
|
|
(save-excursion
|
|
|
|
|
(cond
|
|
|
|
|
;; Comments
|
2012-10-04 13:39:37 -03:00
|
|
|
|
((python-syntax-context 'comment)
|
|
|
|
|
(funcall python-fill-comment-function justify))
|
2012-05-17 00:03:11 -03:00
|
|
|
|
;; Strings/Docstrings
|
2012-10-04 13:39:37 -03:00
|
|
|
|
((save-excursion (or (python-syntax-context 'string)
|
|
|
|
|
(equal (string-to-syntax "|")
|
|
|
|
|
(syntax-after (point)))))
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(funcall python-fill-string-function justify))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; Decorators
|
|
|
|
|
((equal (char-after (save-excursion
|
2012-10-08 19:18:05 -03:00
|
|
|
|
(python-nav-beginning-of-statement))) ?@)
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(funcall python-fill-decorator-function justify))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;; Parens
|
2012-08-07 23:30:08 -03:00
|
|
|
|
((or (python-syntax-context 'paren)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(looking-at (python-rx open-paren))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(skip-syntax-forward "^(" (line-end-position))
|
|
|
|
|
(looking-at (python-rx open-paren))))
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(funcall python-fill-paren-function justify))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(t t))))
|
|
|
|
|
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(defun python-fill-comment (&optional justify)
|
2012-10-08 19:18:05 -03:00
|
|
|
|
"Comment fill function for `python-fill-paragraph'.
|
2012-05-17 00:03:13 -03:00
|
|
|
|
JUSTIFY should be used (if applicable) as in `fill-paragraph'."
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(fill-comment-paragraph justify))
|
|
|
|
|
|
|
|
|
|
(defun python-fill-string (&optional justify)
|
2012-10-08 19:18:05 -03:00
|
|
|
|
"String fill function for `python-fill-paragraph'.
|
2012-05-17 00:03:13 -03:00
|
|
|
|
JUSTIFY should be used (if applicable) as in `fill-paragraph'."
|
2013-09-04 23:46:34 -04:00
|
|
|
|
(let* ((str-start-pos
|
2012-12-29 08:04:55 -03:00
|
|
|
|
(set-marker
|
|
|
|
|
(make-marker)
|
|
|
|
|
(or (python-syntax-context 'string)
|
|
|
|
|
(and (equal (string-to-syntax "|")
|
|
|
|
|
(syntax-after (point)))
|
|
|
|
|
(point)))))
|
2012-10-04 13:39:37 -03:00
|
|
|
|
(num-quotes (python-syntax-count-quotes
|
|
|
|
|
(char-after str-start-pos) str-start-pos))
|
|
|
|
|
(str-end-pos
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char (+ str-start-pos num-quotes))
|
|
|
|
|
(or (re-search-forward (rx (syntax string-delimiter)) nil t)
|
|
|
|
|
(goto-char (point-max)))
|
|
|
|
|
(point-marker)))
|
|
|
|
|
(multi-line-p
|
|
|
|
|
;; Docstring styles may vary for oneliners and multi-liners.
|
|
|
|
|
(> (count-matches "\n" str-start-pos str-end-pos) 0))
|
|
|
|
|
(delimiters-style
|
2012-11-19 16:30:55 -05:00
|
|
|
|
(pcase python-fill-docstring-style
|
2012-10-04 13:39:37 -03:00
|
|
|
|
;; delimiters-style is a cons cell with the form
|
|
|
|
|
;; (START-NEWLINES . END-NEWLINES). When any of the sexps
|
|
|
|
|
;; is NIL means to not add any newlines for start or end
|
2012-10-05 10:42:08 -03:00
|
|
|
|
;; of docstring. See `python-fill-docstring-style' for a
|
2012-10-04 13:39:37 -03:00
|
|
|
|
;; graphic idea of each style.
|
2012-11-19 16:30:55 -05:00
|
|
|
|
(`django (cons 1 1))
|
|
|
|
|
(`onetwo (and multi-line-p (cons 1 2)))
|
|
|
|
|
(`pep-257 (and multi-line-p (cons nil 2)))
|
|
|
|
|
(`pep-257-nn (and multi-line-p (cons nil 1)))
|
|
|
|
|
(`symmetric (and multi-line-p (cons 1 1)))))
|
2012-10-04 13:39:37 -03:00
|
|
|
|
(docstring-p (save-excursion
|
|
|
|
|
;; Consider docstrings those strings which
|
|
|
|
|
;; start on a line by themselves.
|
2012-10-05 10:42:08 -03:00
|
|
|
|
(python-nav-beginning-of-statement)
|
|
|
|
|
(and (= (point) str-start-pos))))
|
2012-10-04 13:39:37 -03:00
|
|
|
|
(fill-paragraph-function))
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(save-restriction
|
2012-10-04 13:39:37 -03:00
|
|
|
|
(narrow-to-region str-start-pos str-end-pos)
|
|
|
|
|
(fill-paragraph justify))
|
|
|
|
|
(save-excursion
|
2012-10-05 10:42:08 -03:00
|
|
|
|
(when (and docstring-p python-fill-docstring-style)
|
2012-10-04 13:39:37 -03:00
|
|
|
|
;; Add the number of newlines indicated by the selected style
|
|
|
|
|
;; at the start of the docstring.
|
|
|
|
|
(goto-char (+ str-start-pos num-quotes))
|
|
|
|
|
(delete-region (point) (progn
|
|
|
|
|
(skip-syntax-forward "> ")
|
|
|
|
|
(point)))
|
|
|
|
|
(and (car delimiters-style)
|
|
|
|
|
(or (newline (car delimiters-style)) t)
|
|
|
|
|
;; Indent only if a newline is added.
|
|
|
|
|
(indent-according-to-mode))
|
|
|
|
|
;; Add the number of newlines indicated by the selected style
|
|
|
|
|
;; at the end of the docstring.
|
|
|
|
|
(goto-char (if (not (= str-end-pos (point-max)))
|
|
|
|
|
(- str-end-pos num-quotes)
|
|
|
|
|
str-end-pos))
|
|
|
|
|
(delete-region (point) (progn
|
|
|
|
|
(skip-syntax-backward "> ")
|
|
|
|
|
(point)))
|
|
|
|
|
(and (cdr delimiters-style)
|
|
|
|
|
;; Add newlines only if string ends.
|
|
|
|
|
(not (= str-end-pos (point-max)))
|
|
|
|
|
(or (newline (cdr delimiters-style)) t)
|
|
|
|
|
;; Again indent only if a newline is added.
|
|
|
|
|
(indent-according-to-mode))))) t)
|
2012-05-17 00:03:11 -03:00
|
|
|
|
|
2013-09-04 23:46:34 -04:00
|
|
|
|
(defun python-fill-decorator (&optional _justify)
|
2012-10-08 19:18:05 -03:00
|
|
|
|
"Decorator fill function for `python-fill-paragraph'.
|
2012-05-17 00:03:13 -03:00
|
|
|
|
JUSTIFY should be used (if applicable) as in `fill-paragraph'."
|
2012-05-17 00:03:11 -03:00
|
|
|
|
t)
|
|
|
|
|
|
|
|
|
|
(defun python-fill-paren (&optional justify)
|
2012-10-08 19:18:05 -03:00
|
|
|
|
"Paren fill function for `python-fill-paragraph'.
|
2012-05-17 00:03:13 -03:00
|
|
|
|
JUSTIFY should be used (if applicable) as in `fill-paragraph'."
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(save-restriction
|
|
|
|
|
(narrow-to-region (progn
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(while (python-syntax-context 'paren)
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(goto-char (1- (point-marker))))
|
|
|
|
|
(point-marker)
|
|
|
|
|
(line-beginning-position))
|
|
|
|
|
(progn
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(when (not (python-syntax-context 'paren))
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(end-of-line)
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(when (not (python-syntax-context 'paren))
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(skip-syntax-backward "^)")))
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(while (python-syntax-context 'paren)
|
2012-05-17 00:03:11 -03:00
|
|
|
|
(goto-char (1+ (point-marker))))
|
|
|
|
|
(point-marker)))
|
|
|
|
|
(let ((paragraph-start "\f\\|[ \t]*$")
|
|
|
|
|
(paragraph-separate ",")
|
|
|
|
|
(fill-paragraph-function))
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(fill-paragraph justify))
|
|
|
|
|
(while (not (eobp))
|
|
|
|
|
(forward-line 1)
|
|
|
|
|
(python-indent-line)
|
|
|
|
|
(goto-char (line-end-position)))) t)
|
|
|
|
|
|
2012-05-17 00:02:59 -03:00
|
|
|
|
|
2012-05-17 00:03:05 -03:00
|
|
|
|
;;; Skeletons
|
|
|
|
|
|
|
|
|
|
(defcustom python-skeleton-autoinsert nil
|
|
|
|
|
"Non-nil means template skeletons will be automagically inserted.
|
|
|
|
|
This happens when pressing \"if<SPACE>\", for example, to prompt for
|
|
|
|
|
the if condition."
|
|
|
|
|
:type 'boolean
|
2012-05-17 00:03:23 -03:00
|
|
|
|
:group 'python
|
|
|
|
|
:safe 'booleanp)
|
2012-05-17 00:03:05 -03:00
|
|
|
|
|
2012-06-14 22:33:57 -03:00
|
|
|
|
(define-obsolete-variable-alias
|
2012-08-15 09:29:11 -07:00
|
|
|
|
'python-use-skeletons 'python-skeleton-autoinsert "24.3")
|
2012-06-14 22:33:57 -03:00
|
|
|
|
|
2012-05-17 00:03:05 -03:00
|
|
|
|
(defvar python-skeleton-available '()
|
|
|
|
|
"Internal list of available skeletons.")
|
|
|
|
|
|
2013-04-16 13:28:11 -04:00
|
|
|
|
(define-abbrev-table 'python-mode-skeleton-abbrev-table ()
|
|
|
|
|
"Abbrev table for Python mode skeletons."
|
2012-05-17 00:03:05 -03:00
|
|
|
|
:case-fixed t
|
|
|
|
|
;; Allow / inside abbrevs.
|
|
|
|
|
:regexp "\\(?:^\\|[^/]\\)\\<\\([[:word:]/]+\\)\\W*"
|
|
|
|
|
;; Only expand in code.
|
|
|
|
|
:enable-function (lambda ()
|
|
|
|
|
(and
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(not (python-syntax-comment-or-string-p))
|
2012-05-17 00:03:05 -03:00
|
|
|
|
python-skeleton-autoinsert)))
|
|
|
|
|
|
|
|
|
|
(defmacro python-skeleton-define (name doc &rest skel)
|
|
|
|
|
"Define a `python-mode' skeleton using NAME DOC and SKEL.
|
|
|
|
|
The skeleton will be bound to python-skeleton-NAME and will
|
2013-04-16 13:28:11 -04:00
|
|
|
|
be added to `python-mode-skeleton-abbrev-table'."
|
2012-06-18 16:41:23 -04:00
|
|
|
|
(declare (indent 2))
|
2012-05-17 00:03:05 -03:00
|
|
|
|
(let* ((name (symbol-name name))
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(function-name (intern (concat "python-skeleton-" name))))
|
2012-05-17 00:03:05 -03:00
|
|
|
|
`(progn
|
2013-04-16 13:28:11 -04:00
|
|
|
|
(define-abbrev python-mode-skeleton-abbrev-table
|
|
|
|
|
,name "" ',function-name :system t)
|
2012-05-17 00:03:05 -03:00
|
|
|
|
(setq python-skeleton-available
|
|
|
|
|
(cons ',function-name python-skeleton-available))
|
|
|
|
|
(define-skeleton ,function-name
|
|
|
|
|
,(or doc
|
|
|
|
|
(format "Insert %s statement." name))
|
|
|
|
|
,@skel))))
|
2012-05-17 00:03:05 -03:00
|
|
|
|
|
2013-04-16 13:28:11 -04:00
|
|
|
|
(define-abbrev-table 'python-mode-abbrev-table ()
|
|
|
|
|
"Abbrev table for Python mode."
|
|
|
|
|
:parents (list python-mode-skeleton-abbrev-table))
|
|
|
|
|
|
2012-05-17 00:03:05 -03:00
|
|
|
|
(defmacro python-define-auxiliary-skeleton (name doc &optional &rest skel)
|
|
|
|
|
"Define a `python-mode' auxiliary skeleton using NAME DOC and SKEL.
|
|
|
|
|
The skeleton will be bound to python-skeleton-NAME."
|
2012-06-18 16:41:23 -04:00
|
|
|
|
(declare (indent 2))
|
2012-05-17 00:03:05 -03:00
|
|
|
|
(let* ((name (symbol-name name))
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(function-name (intern (concat "python-skeleton--" name)))
|
2012-05-17 00:03:05 -03:00
|
|
|
|
(msg (format
|
|
|
|
|
"Add '%s' clause? " name)))
|
|
|
|
|
(when (not skel)
|
|
|
|
|
(setq skel
|
|
|
|
|
`(< ,(format "%s:" name) \n \n
|
|
|
|
|
> _ \n)))
|
|
|
|
|
`(define-skeleton ,function-name
|
|
|
|
|
,(or doc
|
|
|
|
|
(format "Auxiliary skeleton for %s statement." name))
|
|
|
|
|
nil
|
|
|
|
|
(unless (y-or-n-p ,msg)
|
|
|
|
|
(signal 'quit t))
|
|
|
|
|
,@skel)))
|
|
|
|
|
|
|
|
|
|
(python-define-auxiliary-skeleton else nil)
|
|
|
|
|
|
|
|
|
|
(python-define-auxiliary-skeleton except nil)
|
|
|
|
|
|
|
|
|
|
(python-define-auxiliary-skeleton finally nil)
|
|
|
|
|
|
|
|
|
|
(python-skeleton-define if nil
|
|
|
|
|
"Condition: "
|
|
|
|
|
"if " str ":" \n
|
|
|
|
|
_ \n
|
|
|
|
|
("other condition, %s: "
|
|
|
|
|
<
|
|
|
|
|
"elif " str ":" \n
|
|
|
|
|
> _ \n nil)
|
|
|
|
|
'(python-skeleton--else) | ^)
|
|
|
|
|
|
|
|
|
|
(python-skeleton-define while nil
|
|
|
|
|
"Condition: "
|
|
|
|
|
"while " str ":" \n
|
|
|
|
|
> _ \n
|
|
|
|
|
'(python-skeleton--else) | ^)
|
|
|
|
|
|
|
|
|
|
(python-skeleton-define for nil
|
|
|
|
|
"Iteration spec: "
|
|
|
|
|
"for " str ":" \n
|
|
|
|
|
> _ \n
|
|
|
|
|
'(python-skeleton--else) | ^)
|
|
|
|
|
|
2014-06-11 22:35:26 -04:00
|
|
|
|
(python-skeleton-define import nil
|
|
|
|
|
"Import from module: "
|
|
|
|
|
"from " str & " " | -5
|
|
|
|
|
"import "
|
|
|
|
|
("Identifier: " str ", ") -2 \n _)
|
|
|
|
|
|
2012-05-17 00:03:05 -03:00
|
|
|
|
(python-skeleton-define try nil
|
|
|
|
|
nil
|
|
|
|
|
"try:" \n
|
|
|
|
|
> _ \n
|
|
|
|
|
("Exception, %s: "
|
|
|
|
|
<
|
|
|
|
|
"except " str ":" \n
|
|
|
|
|
> _ \n nil)
|
|
|
|
|
resume:
|
|
|
|
|
'(python-skeleton--except)
|
|
|
|
|
'(python-skeleton--else)
|
|
|
|
|
'(python-skeleton--finally) | ^)
|
|
|
|
|
|
|
|
|
|
(python-skeleton-define def nil
|
|
|
|
|
"Function name: "
|
2012-12-11 04:22:55 -03:00
|
|
|
|
"def " str "(" ("Parameter, %s: "
|
|
|
|
|
(unless (equal ?\( (char-before)) ", ")
|
|
|
|
|
str) "):" \n
|
|
|
|
|
"\"\"\"" - "\"\"\"" \n
|
|
|
|
|
> _ \n)
|
2012-05-17 00:03:05 -03:00
|
|
|
|
|
|
|
|
|
(python-skeleton-define class nil
|
|
|
|
|
"Class name: "
|
2012-12-11 04:22:55 -03:00
|
|
|
|
"class " str "(" ("Inheritance, %s: "
|
|
|
|
|
(unless (equal ?\( (char-before)) ", ")
|
|
|
|
|
str)
|
2014-06-11 22:24:24 -04:00
|
|
|
|
& ")" | -1
|
2012-05-17 00:03:05 -03:00
|
|
|
|
":" \n
|
|
|
|
|
"\"\"\"" - "\"\"\"" \n
|
|
|
|
|
> _ \n)
|
|
|
|
|
|
|
|
|
|
(defun python-skeleton-add-menu-items ()
|
|
|
|
|
"Add menu items to Python->Skeletons menu."
|
2013-09-04 23:46:34 -04:00
|
|
|
|
(let ((skeletons (sort python-skeleton-available 'string<)))
|
2012-05-17 00:03:05 -03:00
|
|
|
|
(dolist (skeleton skeletons)
|
|
|
|
|
(easy-menu-add-item
|
|
|
|
|
nil '("Python" "Skeletons")
|
|
|
|
|
`[,(format
|
2012-11-19 16:30:55 -05:00
|
|
|
|
"Insert %s" (nth 2 (split-string (symbol-name skeleton) "-")))
|
2012-05-17 00:03:05 -03:00
|
|
|
|
,skeleton t]))))
|
|
|
|
|
|
2012-05-17 00:02:59 -03:00
|
|
|
|
;;; FFAP
|
|
|
|
|
|
2012-05-17 00:03:23 -03:00
|
|
|
|
(defcustom python-ffap-setup-code
|
2012-05-17 00:02:59 -03:00
|
|
|
|
"def __FFAP_get_module_path(module):
|
|
|
|
|
try:
|
|
|
|
|
import os
|
|
|
|
|
path = __import__(module).__file__
|
|
|
|
|
if path[-4:] == '.pyc' and os.path.exists(path[0:-1]):
|
|
|
|
|
path = path[:-1]
|
|
|
|
|
return path
|
|
|
|
|
except:
|
|
|
|
|
return ''"
|
2012-05-17 00:03:23 -03:00
|
|
|
|
"Python code to get a module path."
|
|
|
|
|
:type 'string
|
2012-05-17 00:03:33 -03:00
|
|
|
|
:group 'python)
|
2012-05-17 00:02:59 -03:00
|
|
|
|
|
2012-05-17 00:03:23 -03:00
|
|
|
|
(defcustom python-ffap-string-code
|
2012-05-17 00:02:59 -03:00
|
|
|
|
"__FFAP_get_module_path('''%s''')\n"
|
2012-05-17 00:03:23 -03:00
|
|
|
|
"Python code used to get a string with the path of a module."
|
|
|
|
|
:type 'string
|
2012-05-17 00:03:33 -03:00
|
|
|
|
:group 'python)
|
2012-05-17 00:02:59 -03:00
|
|
|
|
|
|
|
|
|
(defun python-ffap-module-path (module)
|
|
|
|
|
"Function for `ffap-alist' to return path for MODULE."
|
|
|
|
|
(let ((process (or
|
|
|
|
|
(and (eq major-mode 'inferior-python-mode)
|
|
|
|
|
(get-buffer-process (current-buffer)))
|
|
|
|
|
(python-shell-get-process))))
|
|
|
|
|
(if (not process)
|
|
|
|
|
nil
|
|
|
|
|
(let ((module-file
|
2012-05-17 00:03:06 -03:00
|
|
|
|
(python-shell-send-string-no-output
|
2012-05-17 00:02:59 -03:00
|
|
|
|
(format python-ffap-string-code module) process)))
|
|
|
|
|
(when module-file
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(substring-no-properties module-file 1 -1))))))
|
2012-05-17 00:02:59 -03:00
|
|
|
|
|
2013-06-18 00:17:58 -07:00
|
|
|
|
(defvar ffap-alist)
|
|
|
|
|
|
2012-05-17 00:02:59 -03:00
|
|
|
|
(eval-after-load "ffap"
|
|
|
|
|
'(progn
|
|
|
|
|
(push '(python-mode . python-ffap-module-path) ffap-alist)
|
|
|
|
|
(push '(inferior-python-mode . python-ffap-module-path) ffap-alist)))
|
|
|
|
|
|
2012-05-17 00:03:00 -03:00
|
|
|
|
|
|
|
|
|
;;; Code check
|
|
|
|
|
|
2012-05-17 00:03:23 -03:00
|
|
|
|
(defcustom python-check-command
|
2012-05-17 00:03:34 -03:00
|
|
|
|
"pyflakes"
|
2012-05-17 00:03:23 -03:00
|
|
|
|
"Command used to check a Python file."
|
|
|
|
|
:type 'string
|
2012-05-17 00:03:33 -03:00
|
|
|
|
:group 'python)
|
2012-05-17 00:03:00 -03:00
|
|
|
|
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(defcustom python-check-buffer-name
|
|
|
|
|
"*Python check: %s*"
|
|
|
|
|
"Buffer name used for check commands."
|
|
|
|
|
:type 'string
|
|
|
|
|
:group 'python)
|
|
|
|
|
|
2012-05-17 00:03:00 -03:00
|
|
|
|
(defvar python-check-custom-command nil
|
|
|
|
|
"Internal use.")
|
|
|
|
|
|
|
|
|
|
(defun python-check (command)
|
|
|
|
|
"Check a Python file (default current buffer's file).
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
Runs COMMAND, a shell command, as if by `compile'.
|
|
|
|
|
See `python-check-command' for the default."
|
2012-05-17 00:03:00 -03:00
|
|
|
|
(interactive
|
|
|
|
|
(list (read-string "Check command: "
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(or python-check-custom-command
|
|
|
|
|
(concat python-check-command " "
|
2012-05-17 00:03:00 -03:00
|
|
|
|
(shell-quote-argument
|
|
|
|
|
(or
|
|
|
|
|
(let ((name (buffer-file-name)))
|
|
|
|
|
(and name
|
|
|
|
|
(file-name-nondirectory name)))
|
|
|
|
|
"")))))))
|
|
|
|
|
(setq python-check-custom-command command)
|
|
|
|
|
(save-some-buffers (not compilation-ask-about-save) nil)
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(let ((process-environment (python-shell-calculate-process-environment))
|
|
|
|
|
(exec-path (python-shell-calculate-exec-path)))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(compilation-start command nil
|
2013-09-04 23:46:34 -04:00
|
|
|
|
(lambda (_modename)
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(format python-check-buffer-name command)))))
|
2012-05-17 00:03:00 -03:00
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
;;; Eldoc
|
|
|
|
|
|
2012-05-17 00:03:23 -03:00
|
|
|
|
(defcustom python-eldoc-setup-code
|
2012-05-17 00:02:52 -03:00
|
|
|
|
"def __PYDOC_get_help(obj):
|
|
|
|
|
try:
|
2012-05-17 00:03:18 -03:00
|
|
|
|
import inspect
|
2012-05-17 00:02:58 -03:00
|
|
|
|
if hasattr(obj, 'startswith'):
|
|
|
|
|
obj = eval(obj, globals())
|
2012-05-17 00:03:18 -03:00
|
|
|
|
doc = inspect.getdoc(obj)
|
|
|
|
|
if not doc and callable(obj):
|
|
|
|
|
target = None
|
|
|
|
|
if inspect.isclass(obj) and hasattr(obj, '__init__'):
|
|
|
|
|
target = obj.__init__
|
|
|
|
|
objtype = 'class'
|
|
|
|
|
else:
|
|
|
|
|
target = obj
|
|
|
|
|
objtype = 'def'
|
|
|
|
|
if target:
|
|
|
|
|
args = inspect.formatargspec(
|
|
|
|
|
*inspect.getargspec(target)
|
|
|
|
|
)
|
|
|
|
|
name = obj.__name__
|
|
|
|
|
doc = '{objtype} {name}{args}'.format(
|
|
|
|
|
objtype=objtype, name=name, args=args
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
doc = doc.splitlines()[0]
|
2012-05-17 00:02:52 -03:00
|
|
|
|
except:
|
2012-05-17 00:02:58 -03:00
|
|
|
|
doc = ''
|
|
|
|
|
try:
|
|
|
|
|
exec('print doc')
|
|
|
|
|
except SyntaxError:
|
|
|
|
|
print(doc)"
|
2012-05-17 00:03:23 -03:00
|
|
|
|
"Python code to setup documentation retrieval."
|
|
|
|
|
:type 'string
|
2012-05-17 00:03:33 -03:00
|
|
|
|
:group 'python)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:23 -03:00
|
|
|
|
(defcustom python-eldoc-string-code
|
2012-05-17 00:02:58 -03:00
|
|
|
|
"__PYDOC_get_help('''%s''')\n"
|
2012-05-17 00:03:23 -03:00
|
|
|
|
"Python code used to get a string with the documentation of an object."
|
|
|
|
|
:type 'string
|
2012-05-17 00:03:33 -03:00
|
|
|
|
:group 'python)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:00 -03:00
|
|
|
|
(defun python-eldoc--get-doc-at-point (&optional force-input force-process)
|
2012-05-17 00:03:03 -03:00
|
|
|
|
"Internal implementation to get documentation at point.
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
If not FORCE-INPUT is passed then what `python-info-current-symbol'
|
|
|
|
|
returns will be used. If not FORCE-PROCESS is passed what
|
|
|
|
|
`python-shell-get-process' returns is used."
|
2012-05-17 00:03:00 -03:00
|
|
|
|
(let ((process (or force-process (python-shell-get-process))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(if (not process)
|
2012-07-18 15:40:03 -03:00
|
|
|
|
(error "Eldoc needs an inferior Python process running")
|
|
|
|
|
(let ((input (or force-input
|
|
|
|
|
(python-info-current-symbol t))))
|
|
|
|
|
(and input
|
|
|
|
|
(python-shell-send-string-no-output
|
|
|
|
|
(format python-eldoc-string-code input)
|
|
|
|
|
process))))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:00 -03:00
|
|
|
|
(defun python-eldoc-function ()
|
|
|
|
|
"`eldoc-documentation-function' for Python.
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
For this to work as best as possible you should call
|
2012-05-17 00:03:00 -03:00
|
|
|
|
`python-shell-send-buffer' from time to time so context in
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
inferior Python process is updated properly."
|
2012-05-17 00:03:00 -03:00
|
|
|
|
(python-eldoc--get-doc-at-point))
|
|
|
|
|
|
|
|
|
|
(defun python-eldoc-at-point (symbol)
|
|
|
|
|
"Get help on SYMBOL using `help'.
|
|
|
|
|
Interactively, prompt for symbol."
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(interactive
|
2012-07-18 15:40:03 -03:00
|
|
|
|
(let ((symbol (python-info-current-symbol t))
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(enable-recursive-minibuffers t))
|
|
|
|
|
(list (read-string (if symbol
|
|
|
|
|
(format "Describe symbol (default %s): " symbol)
|
|
|
|
|
"Describe symbol: ")
|
|
|
|
|
nil nil symbol))))
|
2012-07-18 15:40:03 -03:00
|
|
|
|
(message (python-eldoc--get-doc-at-point symbol)))
|
|
|
|
|
|
|
|
|
|
(add-to-list 'debug-ignored-errors
|
|
|
|
|
"^Eldoc needs an inferior Python process running.")
|
2012-05-17 00:03:00 -03:00
|
|
|
|
|
2012-11-26 18:45:58 -03:00
|
|
|
|
|
|
|
|
|
;;; Imenu
|
|
|
|
|
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(defvar python-imenu-format-item-label-function
|
|
|
|
|
'python-imenu-format-item-label
|
|
|
|
|
"Imenu function used to format an item label.
|
|
|
|
|
It must be a function with two arguments: TYPE and NAME.")
|
|
|
|
|
|
|
|
|
|
(defvar python-imenu-format-parent-item-label-function
|
|
|
|
|
'python-imenu-format-parent-item-label
|
|
|
|
|
"Imenu function used to format a parent item label.
|
|
|
|
|
It must be a function with two arguments: TYPE and NAME.")
|
|
|
|
|
|
|
|
|
|
(defvar python-imenu-format-parent-item-jump-label-function
|
|
|
|
|
'python-imenu-format-parent-item-jump-label
|
|
|
|
|
"Imenu function used to format a parent jump item label.
|
|
|
|
|
It must be a function with two arguments: TYPE and NAME.")
|
|
|
|
|
|
|
|
|
|
(defun python-imenu-format-item-label (type name)
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Return Imenu label for single node using TYPE and NAME."
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(format "%s (%s)" name type))
|
|
|
|
|
|
|
|
|
|
(defun python-imenu-format-parent-item-label (type name)
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Return Imenu label for parent node using TYPE and NAME."
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(format "%s..." (python-imenu-format-item-label type name)))
|
|
|
|
|
|
2013-09-04 23:46:34 -04:00
|
|
|
|
(defun python-imenu-format-parent-item-jump-label (type _name)
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Return Imenu label for parent node jump using TYPE and NAME."
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(if (string= type "class")
|
|
|
|
|
"*class definition*"
|
|
|
|
|
"*function definition*"))
|
|
|
|
|
|
2013-08-13 13:36:32 -03:00
|
|
|
|
(defun python-imenu--put-parent (type name pos tree)
|
|
|
|
|
"Add the parent with TYPE, NAME and POS to TREE."
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(let ((label
|
|
|
|
|
(funcall python-imenu-format-item-label-function type name))
|
|
|
|
|
(jump-label
|
|
|
|
|
(funcall python-imenu-format-parent-item-jump-label-function type name)))
|
2013-08-13 13:36:32 -03:00
|
|
|
|
(if (not tree)
|
|
|
|
|
(cons label pos)
|
|
|
|
|
(cons label (cons (cons jump-label pos) tree)))))
|
2013-04-18 23:31:09 -03:00
|
|
|
|
|
2013-08-13 13:36:32 -03:00
|
|
|
|
(defun python-imenu--build-tree (&optional min-indent prev-indent tree)
|
2013-04-18 23:31:09 -03:00
|
|
|
|
"Recursively build the tree of nested definitions of a node.
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
Arguments MIN-INDENT, PREV-INDENT and TREE are internal and should
|
2013-08-13 13:36:32 -03:00
|
|
|
|
not be passed explicitly unless you know what you are doing."
|
|
|
|
|
(setq min-indent (or min-indent 0)
|
|
|
|
|
prev-indent (or prev-indent python-indent-offset))
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(let* ((pos (python-nav-backward-defun))
|
|
|
|
|
(type)
|
|
|
|
|
(name (when (and pos (looking-at python-nav-beginning-of-defun-regexp))
|
|
|
|
|
(let ((split (split-string (match-string-no-properties 0))))
|
|
|
|
|
(setq type (car split))
|
|
|
|
|
(cadr split))))
|
|
|
|
|
(label (when name
|
|
|
|
|
(funcall python-imenu-format-item-label-function type name)))
|
2013-08-13 13:36:32 -03:00
|
|
|
|
(indent (current-indentation))
|
|
|
|
|
(children-indent-limit (+ python-indent-offset min-indent)))
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(cond ((not pos)
|
2013-08-13 13:36:32 -03:00
|
|
|
|
;; Nothing found, probably near to bobp.
|
|
|
|
|
nil)
|
|
|
|
|
((<= indent min-indent)
|
|
|
|
|
;; The current indentation points that this is a parent
|
|
|
|
|
;; node, add it to the tree and stop recursing.
|
|
|
|
|
(python-imenu--put-parent type name pos tree))
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(t
|
|
|
|
|
(python-imenu--build-tree
|
|
|
|
|
min-indent
|
|
|
|
|
indent
|
2013-08-13 13:36:32 -03:00
|
|
|
|
(if (<= indent children-indent-limit)
|
2013-08-15 22:15:51 -07:00
|
|
|
|
;; This lies within the children indent offset range,
|
|
|
|
|
;; so it's a normal child of its parent (i.e., not
|
|
|
|
|
;; a child of a child).
|
2013-08-13 13:36:32 -03:00
|
|
|
|
(cons (cons label pos) tree)
|
2013-08-15 22:15:51 -07:00
|
|
|
|
;; Oh no, a child of a child?! Fear not, we
|
|
|
|
|
;; know how to roll. We recursively parse these by
|
2013-08-13 13:36:32 -03:00
|
|
|
|
;; swapping prev-indent and min-indent plus adding this
|
2013-08-15 22:15:51 -07:00
|
|
|
|
;; newly found item to a fresh subtree. This works, I
|
2013-08-13 13:36:32 -03:00
|
|
|
|
;; promise.
|
|
|
|
|
(cons
|
|
|
|
|
(python-imenu--build-tree
|
|
|
|
|
prev-indent indent (list (cons label pos)))
|
|
|
|
|
tree)))))))
|
2013-04-18 23:31:09 -03:00
|
|
|
|
|
|
|
|
|
(defun python-imenu-create-index ()
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Return tree Imenu alist for the current Python buffer.
|
2013-04-18 23:31:09 -03:00
|
|
|
|
Change `python-imenu-format-item-label-function',
|
|
|
|
|
`python-imenu-format-parent-item-label-function',
|
|
|
|
|
`python-imenu-format-parent-item-jump-label-function' to
|
|
|
|
|
customize how labels are formatted."
|
|
|
|
|
(goto-char (point-max))
|
|
|
|
|
(let ((index)
|
|
|
|
|
(tree))
|
|
|
|
|
(while (setq tree (python-imenu--build-tree))
|
|
|
|
|
(setq index (cons tree index)))
|
|
|
|
|
index))
|
|
|
|
|
|
|
|
|
|
(defun python-imenu-create-flat-index (&optional alist prefix)
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Return flat outline of the current Python buffer for Imenu.
|
|
|
|
|
Optional argument ALIST is the tree to be flattened; when nil
|
2013-04-18 23:31:09 -03:00
|
|
|
|
`python-imenu-build-index' is used with
|
|
|
|
|
`python-imenu-format-parent-item-jump-label-function'
|
|
|
|
|
`python-imenu-format-parent-item-label-function'
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
`python-imenu-format-item-label-function' set to
|
|
|
|
|
(lambda (type name) name)
|
|
|
|
|
Optional argument PREFIX is used in recursive calls and should
|
|
|
|
|
not be passed explicitly.
|
2013-04-18 23:31:09 -03:00
|
|
|
|
|
|
|
|
|
Converts this:
|
|
|
|
|
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
((\"Foo\" . 103)
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(\"Bar\" . 138)
|
|
|
|
|
(\"decorator\"
|
|
|
|
|
(\"decorator\" . 173)
|
|
|
|
|
(\"wrap\"
|
|
|
|
|
(\"wrap\" . 353)
|
|
|
|
|
(\"wrapped_f\" . 393))))
|
|
|
|
|
|
|
|
|
|
To this:
|
|
|
|
|
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
((\"Foo\" . 103)
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(\"Bar\" . 138)
|
|
|
|
|
(\"decorator\" . 173)
|
|
|
|
|
(\"decorator.wrap\" . 353)
|
|
|
|
|
(\"decorator.wrapped_f\" . 393))"
|
2013-04-19 10:29:41 -03:00
|
|
|
|
;; Inspired by imenu--flatten-index-alist removed in revno 21853.
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(apply
|
|
|
|
|
'nconc
|
|
|
|
|
(mapcar
|
|
|
|
|
(lambda (item)
|
|
|
|
|
(let ((name (if prefix
|
|
|
|
|
(concat prefix "." (car item))
|
|
|
|
|
(car item)))
|
|
|
|
|
(pos (cdr item)))
|
|
|
|
|
(cond ((or (numberp pos) (markerp pos))
|
|
|
|
|
(list (cons name pos)))
|
|
|
|
|
((listp pos)
|
|
|
|
|
(cons
|
|
|
|
|
(cons name (cdar pos))
|
|
|
|
|
(python-imenu-create-flat-index (cddr item) name))))))
|
|
|
|
|
(or alist
|
2013-09-04 23:46:34 -04:00
|
|
|
|
(let* ((fn (lambda (_type name) name))
|
2013-04-19 10:29:41 -03:00
|
|
|
|
(python-imenu-format-item-label-function fn)
|
|
|
|
|
(python-imenu-format-parent-item-label-function fn)
|
|
|
|
|
(python-imenu-format-parent-item-jump-label-function fn))
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(python-imenu-create-index))))))
|
2012-11-26 18:45:58 -03:00
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
;;; Misc helpers
|
|
|
|
|
|
2012-05-17 00:03:14 -03:00
|
|
|
|
(defun python-info-current-defun (&optional include-type)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
"Return name of surrounding function with Python compatible dotty syntax.
|
2012-05-17 00:03:14 -03:00
|
|
|
|
Optional argument INCLUDE-TYPE indicates to include the type of the defun.
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
This function can be used as the value of `add-log-current-defun-function'
|
|
|
|
|
since it returns nil if point is not inside a defun."
|
2013-02-13 20:07:59 -03:00
|
|
|
|
(save-restriction
|
|
|
|
|
(widen)
|
|
|
|
|
(save-excursion
|
|
|
|
|
(end-of-line 1)
|
|
|
|
|
(let ((names)
|
|
|
|
|
(starting-indentation (current-indentation))
|
|
|
|
|
(starting-pos (point))
|
|
|
|
|
(first-run t)
|
|
|
|
|
(last-indent)
|
|
|
|
|
(type))
|
|
|
|
|
(catch 'exit
|
|
|
|
|
(while (python-nav-beginning-of-defun 1)
|
2013-02-20 17:41:46 -03:00
|
|
|
|
(when (save-match-data
|
|
|
|
|
(and
|
|
|
|
|
(or (not last-indent)
|
|
|
|
|
(< (current-indentation) last-indent))
|
|
|
|
|
(or
|
|
|
|
|
(and first-run
|
|
|
|
|
(save-excursion
|
|
|
|
|
;; If this is the first run, we may add
|
|
|
|
|
;; the current defun at point.
|
|
|
|
|
(setq first-run nil)
|
|
|
|
|
(goto-char starting-pos)
|
|
|
|
|
(python-nav-beginning-of-statement)
|
|
|
|
|
(beginning-of-line 1)
|
|
|
|
|
(looking-at-p
|
|
|
|
|
python-nav-beginning-of-defun-regexp)))
|
|
|
|
|
(< starting-pos
|
2012-11-12 10:26:50 -03:00
|
|
|
|
(save-excursion
|
2013-02-20 17:41:46 -03:00
|
|
|
|
(let ((min-indent
|
|
|
|
|
(+ (current-indentation)
|
|
|
|
|
python-indent-offset)))
|
|
|
|
|
(if (< starting-indentation min-indent)
|
|
|
|
|
;; If the starting indentation is not
|
|
|
|
|
;; within the min defun indent make the
|
|
|
|
|
;; check fail.
|
|
|
|
|
starting-pos
|
|
|
|
|
;; Else go to the end of defun and add
|
|
|
|
|
;; up the current indentation to the
|
|
|
|
|
;; ending position.
|
|
|
|
|
(python-nav-end-of-defun)
|
|
|
|
|
(+ (point)
|
|
|
|
|
(if (>= (current-indentation) min-indent)
|
|
|
|
|
(1+ (current-indentation))
|
|
|
|
|
0)))))))))
|
|
|
|
|
(save-match-data (setq last-indent (current-indentation)))
|
2013-02-13 20:07:59 -03:00
|
|
|
|
(if (or (not include-type) type)
|
|
|
|
|
(setq names (cons (match-string-no-properties 1) names))
|
|
|
|
|
(let ((match (split-string (match-string-no-properties 0))))
|
|
|
|
|
(setq type (car match))
|
|
|
|
|
(setq names (cons (cadr match) names)))))
|
|
|
|
|
;; Stop searching ASAP.
|
|
|
|
|
(and (= (current-indentation) 0) (throw 'exit t))))
|
|
|
|
|
(and names
|
|
|
|
|
(concat (and type (format "%s " type))
|
|
|
|
|
(mapconcat 'identity names ".")))))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-07-18 15:40:03 -03:00
|
|
|
|
(defun python-info-current-symbol (&optional replace-self)
|
|
|
|
|
"Return current symbol using dotty syntax.
|
|
|
|
|
With optional argument REPLACE-SELF convert \"self\" to current
|
|
|
|
|
parent defun name."
|
|
|
|
|
(let ((name
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(and (not (python-syntax-comment-or-string-p))
|
2012-07-18 15:40:03 -03:00
|
|
|
|
(with-syntax-table python-dotty-syntax-table
|
|
|
|
|
(let ((sym (symbol-at-point)))
|
|
|
|
|
(and sym
|
|
|
|
|
(substring-no-properties (symbol-name sym))))))))
|
|
|
|
|
(when name
|
|
|
|
|
(if (not replace-self)
|
|
|
|
|
name
|
|
|
|
|
(let ((current-defun (python-info-current-defun)))
|
|
|
|
|
(if (not current-defun)
|
|
|
|
|
name
|
|
|
|
|
(replace-regexp-in-string
|
|
|
|
|
(python-rx line-start word-start "self" word-end ?.)
|
|
|
|
|
(concat
|
|
|
|
|
(mapconcat 'identity
|
|
|
|
|
(butlast (split-string current-defun "\\."))
|
|
|
|
|
".") ".")
|
|
|
|
|
name)))))))
|
|
|
|
|
|
2012-10-08 02:19:15 -03:00
|
|
|
|
(defun python-info-statement-starts-block-p ()
|
2012-07-17 17:27:49 -03:00
|
|
|
|
"Return non-nil if current statement opens a block."
|
|
|
|
|
(save-excursion
|
|
|
|
|
(python-nav-beginning-of-statement)
|
|
|
|
|
(looking-at (python-rx block-start))))
|
|
|
|
|
|
2012-10-08 02:19:15 -03:00
|
|
|
|
(defun python-info-statement-ends-block-p ()
|
|
|
|
|
"Return non-nil if point is at end of block."
|
|
|
|
|
(let ((end-of-block-pos (save-excursion
|
|
|
|
|
(python-nav-end-of-block)))
|
|
|
|
|
(end-of-statement-pos (save-excursion
|
|
|
|
|
(python-nav-end-of-statement))))
|
|
|
|
|
(and end-of-block-pos end-of-statement-pos
|
|
|
|
|
(= end-of-block-pos end-of-statement-pos))))
|
|
|
|
|
|
|
|
|
|
(defun python-info-beginning-of-statement-p ()
|
|
|
|
|
"Return non-nil if point is at beginning of statement."
|
|
|
|
|
(= (point) (save-excursion
|
|
|
|
|
(python-nav-beginning-of-statement)
|
|
|
|
|
(point))))
|
|
|
|
|
|
|
|
|
|
(defun python-info-end-of-statement-p ()
|
|
|
|
|
"Return non-nil if point is at end of statement."
|
|
|
|
|
(= (point) (save-excursion
|
|
|
|
|
(python-nav-end-of-statement)
|
|
|
|
|
(point))))
|
|
|
|
|
|
|
|
|
|
(defun python-info-beginning-of-block-p ()
|
|
|
|
|
"Return non-nil if point is at beginning of block."
|
|
|
|
|
(and (python-info-beginning-of-statement-p)
|
|
|
|
|
(python-info-statement-starts-block-p)))
|
|
|
|
|
|
|
|
|
|
(defun python-info-end-of-block-p ()
|
|
|
|
|
"Return non-nil if point is at end of block."
|
|
|
|
|
(and (python-info-end-of-statement-p)
|
|
|
|
|
(python-info-statement-ends-block-p)))
|
|
|
|
|
|
2014-07-09 00:55:53 -03:00
|
|
|
|
(define-obsolete-function-alias
|
|
|
|
|
'python-info-closing-block
|
|
|
|
|
'python-info-dedenter-opening-block-position "24.4")
|
|
|
|
|
|
|
|
|
|
(defun python-info-dedenter-opening-block-position ()
|
|
|
|
|
"Return the point of the closest block the current line closes.
|
|
|
|
|
Returns nil if point is not on a dedenter statement or no opening
|
|
|
|
|
block can be detected. The latter case meaning current file is
|
|
|
|
|
likely an invalid python file."
|
|
|
|
|
(let ((positions (python-info-dedenter-opening-block-positions))
|
|
|
|
|
(indentation (current-indentation))
|
|
|
|
|
(position))
|
|
|
|
|
(while (and (not position)
|
|
|
|
|
positions)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(save-excursion
|
2014-07-09 00:55:53 -03:00
|
|
|
|
(goto-char (car positions))
|
|
|
|
|
(if (<= (current-indentation) indentation)
|
|
|
|
|
(setq position (car positions))
|
|
|
|
|
(setq positions (cdr positions)))))
|
|
|
|
|
position))
|
|
|
|
|
|
|
|
|
|
(defun python-info-dedenter-opening-block-positions ()
|
|
|
|
|
"Return points of blocks the current line may close sorted by closer.
|
|
|
|
|
Returns nil if point is not on a dedenter statement or no opening
|
|
|
|
|
block can be detected. The latter case meaning current file is
|
|
|
|
|
likely an invalid python file."
|
|
|
|
|
(save-excursion
|
|
|
|
|
(let ((dedenter-pos (python-info-dedenter-statement-p)))
|
|
|
|
|
(when dedenter-pos
|
|
|
|
|
(goto-char dedenter-pos)
|
|
|
|
|
(let* ((pairs '(("elif" "elif" "if")
|
|
|
|
|
("else" "if" "elif" "except" "for" "while")
|
|
|
|
|
("except" "except" "try")
|
|
|
|
|
("finally" "else" "except" "try")))
|
|
|
|
|
(dedenter (match-string-no-properties 0))
|
|
|
|
|
(possible-opening-blocks (cdr (assoc-string dedenter pairs)))
|
|
|
|
|
(collected-indentations)
|
|
|
|
|
(opening-blocks))
|
|
|
|
|
(catch 'exit
|
|
|
|
|
(while (python-nav--syntactically
|
|
|
|
|
(lambda ()
|
|
|
|
|
(re-search-backward (python-rx block-start) nil t))
|
|
|
|
|
#'<)
|
|
|
|
|
(let ((indentation (current-indentation)))
|
|
|
|
|
(when (and (not (memq indentation collected-indentations))
|
|
|
|
|
(or (not collected-indentations)
|
|
|
|
|
(< indentation (apply #'min collected-indentations))))
|
|
|
|
|
(setq collected-indentations
|
|
|
|
|
(cons indentation collected-indentations))
|
|
|
|
|
(when (member (match-string-no-properties 0)
|
|
|
|
|
possible-opening-blocks)
|
|
|
|
|
(setq opening-blocks (cons (point) opening-blocks))))
|
|
|
|
|
(when (zerop indentation)
|
|
|
|
|
(throw 'exit nil)))))
|
|
|
|
|
;; sort by closer
|
|
|
|
|
(nreverse opening-blocks))))))
|
|
|
|
|
|
|
|
|
|
(define-obsolete-function-alias
|
|
|
|
|
'python-info-closing-block-message
|
|
|
|
|
'python-info-dedenter-opening-block-message "24.4")
|
|
|
|
|
|
|
|
|
|
(defun python-info-dedenter-opening-block-message ()
|
|
|
|
|
"Message the first line of the block the current statement closes."
|
|
|
|
|
(let ((point (python-info-dedenter-opening-block-position)))
|
2012-05-17 00:03:42 -03:00
|
|
|
|
(when point
|
|
|
|
|
(save-restriction
|
|
|
|
|
(widen)
|
|
|
|
|
(message "Closes %s" (save-excursion
|
|
|
|
|
(goto-char point)
|
|
|
|
|
(buffer-substring
|
|
|
|
|
(point) (line-end-position))))))))
|
|
|
|
|
|
2014-07-09 00:55:53 -03:00
|
|
|
|
(defun python-info-dedenter-statement-p ()
|
|
|
|
|
"Return point if current statement is a dedenter.
|
|
|
|
|
Sets `match-data' to the keyword that starts the dedenter
|
|
|
|
|
statement."
|
|
|
|
|
(save-excursion
|
|
|
|
|
(python-nav-beginning-of-statement)
|
|
|
|
|
(when (and (not (python-syntax-context-type))
|
|
|
|
|
(looking-at (python-rx dedenter)))
|
|
|
|
|
(point))))
|
|
|
|
|
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(defun python-info-line-ends-backslash-p (&optional line-number)
|
2012-05-17 00:03:35 -03:00
|
|
|
|
"Return non-nil if current line ends with backslash.
|
2012-05-17 00:03:34 -03:00
|
|
|
|
With optional argument LINE-NUMBER, check that line instead."
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(save-excursion
|
|
|
|
|
(save-restriction
|
2012-05-17 00:03:36 -03:00
|
|
|
|
(widen)
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(when line-number
|
2013-02-19 00:18:32 -03:00
|
|
|
|
(python-util-goto-line line-number))
|
2012-05-17 00:03:36 -03:00
|
|
|
|
(while (and (not (eobp))
|
|
|
|
|
(goto-char (line-end-position))
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(python-syntax-context 'paren)
|
2012-05-17 00:03:36 -03:00
|
|
|
|
(not (equal (char-before (point)) ?\\)))
|
|
|
|
|
(forward-line 1))
|
|
|
|
|
(when (equal (char-before) ?\\)
|
|
|
|
|
(point-marker)))))
|
|
|
|
|
|
2012-06-17 01:53:31 -07:00
|
|
|
|
(defun python-info-beginning-of-backslash (&optional line-number)
|
|
|
|
|
"Return the point where the backslashed line start.
|
2012-05-17 00:03:44 -03:00
|
|
|
|
Optional argument LINE-NUMBER forces the line number to check against."
|
2012-05-17 00:03:36 -03:00
|
|
|
|
(save-excursion
|
|
|
|
|
(save-restriction
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(widen)
|
2012-05-17 00:03:36 -03:00
|
|
|
|
(when line-number
|
2013-02-19 00:18:32 -03:00
|
|
|
|
(python-util-goto-line line-number))
|
2012-05-17 00:03:36 -03:00
|
|
|
|
(when (python-info-line-ends-backslash-p)
|
|
|
|
|
(while (save-excursion
|
|
|
|
|
(goto-char (line-beginning-position))
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(python-syntax-context 'paren))
|
2012-05-17 00:03:36 -03:00
|
|
|
|
(forward-line -1))
|
|
|
|
|
(back-to-indentation)
|
|
|
|
|
(point-marker)))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defun python-info-continuation-line-p ()
|
2012-05-17 00:03:34 -03:00
|
|
|
|
"Check if current line is continuation of another.
|
|
|
|
|
When current line is continuation of another return the point
|
|
|
|
|
where the continued line ends."
|
|
|
|
|
(save-excursion
|
|
|
|
|
(save-restriction
|
|
|
|
|
(widen)
|
|
|
|
|
(let* ((context-type (progn
|
|
|
|
|
(back-to-indentation)
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(python-syntax-context-type)))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(line-start (line-number-at-pos))
|
|
|
|
|
(context-start (when context-type
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(python-syntax-context context-type))))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(cond ((equal context-type 'paren)
|
|
|
|
|
;; Lines inside a paren are always a continuation line
|
|
|
|
|
;; (except the first one).
|
2012-07-18 21:55:55 -03:00
|
|
|
|
(python-util-forward-comment -1)
|
|
|
|
|
(point-marker))
|
|
|
|
|
((member context-type '(string comment))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
;; move forward an roll again
|
|
|
|
|
(goto-char context-start)
|
|
|
|
|
(python-util-forward-comment)
|
|
|
|
|
(python-info-continuation-line-p))
|
|
|
|
|
(t
|
2012-07-18 21:55:55 -03:00
|
|
|
|
;; Not within a paren, string or comment, the only way
|
|
|
|
|
;; we are dealing with a continuation line is that
|
|
|
|
|
;; previous line contains a backslash, and this can
|
|
|
|
|
;; only be the previous line from current
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(back-to-indentation)
|
|
|
|
|
(python-util-forward-comment -1)
|
|
|
|
|
(when (and (equal (1- line-start) (line-number-at-pos))
|
|
|
|
|
(python-info-line-ends-backslash-p))
|
|
|
|
|
(point-marker))))))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defun python-info-block-continuation-line-p ()
|
|
|
|
|
"Return non-nil if current line is a continuation of a block."
|
|
|
|
|
(save-excursion
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(when (python-info-continuation-line-p)
|
|
|
|
|
(forward-line -1)
|
|
|
|
|
(back-to-indentation)
|
|
|
|
|
(when (looking-at (python-rx block-start))
|
|
|
|
|
(point-marker)))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(defun python-info-assignment-continuation-line-p ()
|
2012-05-17 00:03:34 -03:00
|
|
|
|
"Check if current line is a continuation of an assignment.
|
|
|
|
|
When current line is continuation of another with an assignment
|
|
|
|
|
return the point of the first non-blank character after the
|
|
|
|
|
operator."
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(save-excursion
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(when (python-info-continuation-line-p)
|
|
|
|
|
(forward-line -1)
|
|
|
|
|
(back-to-indentation)
|
|
|
|
|
(when (and (not (looking-at (python-rx block-start)))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(and (re-search-forward (python-rx not-simple-operator
|
|
|
|
|
assignment-operator
|
|
|
|
|
not-simple-operator)
|
|
|
|
|
(line-end-position) t)
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(not (python-syntax-context-type))))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(skip-syntax-forward "\s")
|
|
|
|
|
(point-marker)))))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:44 -03:00
|
|
|
|
(defun python-info-looking-at-beginning-of-defun (&optional syntax-ppss)
|
2012-05-17 00:03:44 -03:00
|
|
|
|
"Check if point is at `beginning-of-defun' using SYNTAX-PPSS."
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(and (not (python-syntax-context-type (or syntax-ppss (syntax-ppss))))
|
2012-05-17 00:03:44 -03:00
|
|
|
|
(save-excursion
|
|
|
|
|
(beginning-of-line 1)
|
|
|
|
|
(looking-at python-nav-beginning-of-defun-regexp))))
|
|
|
|
|
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(defun python-info-current-line-comment-p ()
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Return non-nil if current line is a comment line."
|
2013-02-19 00:18:32 -03:00
|
|
|
|
(char-equal
|
|
|
|
|
(or (char-after (+ (line-beginning-position) (current-indentation))) ?_)
|
|
|
|
|
?#))
|
2012-07-16 10:13:01 -03:00
|
|
|
|
|
|
|
|
|
(defun python-info-current-line-empty-p ()
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
"Return non-nil if current line is empty, ignoring whitespace."
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(save-excursion
|
|
|
|
|
(beginning-of-line 1)
|
|
|
|
|
(looking-at
|
|
|
|
|
(python-rx line-start (* whitespace)
|
|
|
|
|
(group (* not-newline))
|
|
|
|
|
(* whitespace) line-end))
|
|
|
|
|
(string-equal "" (match-string-no-properties 1))))
|
|
|
|
|
|
2012-05-17 00:03:14 -03:00
|
|
|
|
|
|
|
|
|
;;; Utility functions
|
|
|
|
|
|
2013-02-19 00:18:32 -03:00
|
|
|
|
(defun python-util-goto-line (line-number)
|
|
|
|
|
"Move point to LINE-NUMBER."
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(forward-line (1- line-number)))
|
2012-05-17 00:03:14 -03:00
|
|
|
|
|
2012-05-17 00:03:24 -03:00
|
|
|
|
;; Stolen from org-mode
|
2012-05-17 00:03:24 -03:00
|
|
|
|
(defun python-util-clone-local-variables (from-buffer &optional regexp)
|
2012-05-17 00:03:24 -03:00
|
|
|
|
"Clone local variables from FROM-BUFFER.
|
|
|
|
|
Optional argument REGEXP selects variables to clone and defaults
|
|
|
|
|
to \"^python-\"."
|
|
|
|
|
(mapc
|
|
|
|
|
(lambda (pair)
|
|
|
|
|
(and (symbolp (car pair))
|
|
|
|
|
(string-match (or regexp "^python-")
|
|
|
|
|
(symbol-name (car pair)))
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(set (make-local-variable (car pair))
|
|
|
|
|
(cdr pair))))
|
2012-05-17 00:03:24 -03:00
|
|
|
|
(buffer-local-variables from-buffer)))
|
|
|
|
|
|
2014-07-26 20:43:51 -03:00
|
|
|
|
(defvar comint-last-prompt-overlay) ; Shut up, bytecompiler
|
|
|
|
|
|
|
|
|
|
(defun python-util-comint-last-prompt ()
|
|
|
|
|
"Return comint last prompt overlay start and end.
|
|
|
|
|
This is for compatibility with Emacs < 24.4."
|
|
|
|
|
(cond ((bound-and-true-p comint-last-prompt-overlay)
|
|
|
|
|
(cons (overlay-start comint-last-prompt-overlay)
|
|
|
|
|
(overlay-end comint-last-prompt-overlay)))
|
|
|
|
|
((bound-and-true-p comint-last-prompt)
|
|
|
|
|
comint-last-prompt)
|
|
|
|
|
(t nil)))
|
|
|
|
|
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(defun python-util-forward-comment (&optional direction)
|
2012-05-17 00:03:44 -03:00
|
|
|
|
"Python mode specific version of `forward-comment'.
|
|
|
|
|
Optional argument DIRECTION defines the direction to move to."
|
2012-08-07 23:30:08 -03:00
|
|
|
|
(let ((comment-start (python-syntax-context 'comment))
|
2012-05-17 00:03:34 -03:00
|
|
|
|
(factor (if (< (or direction 0) 0)
|
|
|
|
|
-99999
|
|
|
|
|
99999)))
|
|
|
|
|
(when comment-start
|
|
|
|
|
(goto-char comment-start))
|
|
|
|
|
(forward-comment factor)))
|
|
|
|
|
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(defun python-util-popn (lst n)
|
|
|
|
|
"Return LST first N elements.
|
lisp/progmodes/python.el: Fix docstring typos.
(defconst, python-syntax-count-quotes)
(python-indent-region, python-indent-shift-right)
(python-indent-dedent-line-backspace, python-nav-backward-sexp)
(python-nav-backward-sexp-safe, python-nav-backward-up-list)
(python-shell-prompt-block-regexp, python-shell-prompt-output-regexp)
(python-shell-prompt-pdb-regexp, python-shell-enable-font-lock)
(inferior-python-mode, python-shell-make-comint, run-python-internal)
(python-shell-buffer-substring, python-shell-send-buffer)
(python-pdbtrack-activate, python-pdbtrack-stacktrace-info-regexp)
(python-completion-complete-at-point, python-fill-docstring-style)
(python-eldoc-function, python-imenu-format-item-label)
(python-imenu-format-parent-item-label)
(python-imenu-format-parent-item-jump-label)
(python-imenu--build-tree, python-imenu-create-index)
(python-imenu-create-flat-index): Fix docstring typos.
(python-indent-context, python-shell-prompt-regexp, run-python):
Remove superfluous backslashes.
(python-indent-line, python-nav-beginning-of-defun)
(python-shell-get-buffer, python-shell-get-process)
(python-info-current-defun, python-info-current-line-comment-p)
(python-info-current-line-empty-p, python-util-popn): Doc fixes.
(python-indent-post-self-insert-function, python-shell-send-file)
(python-shell-completion-get-completions)
(python-shell-completion-complete-or-indent)
(python-eldoc--get-doc-at-point): Reflow docstrings.
2014-03-15 18:37:58 +01:00
|
|
|
|
N should be an integer, when negative its opposite is used.
|
|
|
|
|
When N is bigger than the length of LST, the list is
|
|
|
|
|
returned as is."
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(let* ((n (min (abs n)))
|
|
|
|
|
(len (length lst))
|
|
|
|
|
(acc))
|
|
|
|
|
(if (> n len)
|
|
|
|
|
lst
|
|
|
|
|
(while (< 0 n)
|
|
|
|
|
(setq acc (cons (car lst) acc)
|
|
|
|
|
lst (cdr lst)
|
|
|
|
|
n (1- n)))
|
|
|
|
|
(reverse acc))))
|
|
|
|
|
|
2014-07-26 20:43:51 -03:00
|
|
|
|
(defun python-util-text-properties-replace-name
|
|
|
|
|
(from to &optional start end)
|
|
|
|
|
"Replace properties named FROM to TO, keeping its value.
|
|
|
|
|
Arguments START and END narrow the buffer region to work on."
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char (or start (point-min)))
|
|
|
|
|
(while (not (eobp))
|
|
|
|
|
(let ((plist (text-properties-at (point)))
|
|
|
|
|
(next-change (or (next-property-change (point) (current-buffer))
|
|
|
|
|
(or end (point-max)))))
|
|
|
|
|
(when (plist-get plist from)
|
|
|
|
|
(let* ((face (plist-get plist from))
|
|
|
|
|
(plist (plist-put plist from nil))
|
|
|
|
|
(plist (plist-put plist to face)))
|
|
|
|
|
(set-text-properties (point) next-change plist (current-buffer))))
|
|
|
|
|
(goto-char next-change)))))
|
|
|
|
|
|
2014-06-21 11:14:54 -03:00
|
|
|
|
(defun python-util-strip-string (string)
|
|
|
|
|
"Strip STRING whitespace and newlines from end and beginning."
|
|
|
|
|
(replace-regexp-in-string
|
|
|
|
|
(rx (or (: string-start (* (any whitespace ?\r ?\n)))
|
|
|
|
|
(: (* (any whitespace ?\r ?\n)) string-end)))
|
|
|
|
|
""
|
|
|
|
|
string))
|
|
|
|
|
|
2014-07-19 10:13:07 -03:00
|
|
|
|
(defun python-util-valid-regexp-p (regexp)
|
|
|
|
|
"Return non-nil if REGEXP is valid."
|
|
|
|
|
(ignore-errors (string-match regexp "") t))
|
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2013-11-04 14:14:58 -05:00
|
|
|
|
(defun python-electric-pair-string-delimiter ()
|
|
|
|
|
(when (and electric-pair-mode
|
|
|
|
|
(memq last-command-event '(?\" ?\'))
|
|
|
|
|
(let ((count 0))
|
|
|
|
|
(while (eq (char-before (- (point) count)) last-command-event)
|
|
|
|
|
(cl-incf count))
|
2014-04-07 00:23:45 +01:00
|
|
|
|
(= count 3))
|
|
|
|
|
(eq (char-after) last-command-event))
|
|
|
|
|
(save-excursion (insert (make-string 2 last-command-event)))))
|
2013-11-04 14:14:58 -05:00
|
|
|
|
|
2013-10-07 14:51:26 -04:00
|
|
|
|
(defvar electric-indent-inhibit)
|
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;;;###autoload
|
2012-06-19 09:04:47 -07:00
|
|
|
|
(define-derived-mode python-mode prog-mode "Python"
|
2012-05-17 00:03:09 -03:00
|
|
|
|
"Major mode for editing Python files.
|
|
|
|
|
|
2013-11-05 11:47:47 +02:00
|
|
|
|
\\{python-mode-map}"
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(set (make-local-variable 'tab-width) 8)
|
|
|
|
|
(set (make-local-variable 'indent-tabs-mode) nil)
|
|
|
|
|
|
|
|
|
|
(set (make-local-variable 'comment-start) "# ")
|
|
|
|
|
(set (make-local-variable 'comment-start-skip) "#+\\s-*")
|
|
|
|
|
|
|
|
|
|
(set (make-local-variable 'parse-sexp-lookup-properties) t)
|
|
|
|
|
(set (make-local-variable 'parse-sexp-ignore-comments) t)
|
|
|
|
|
|
2012-07-16 10:13:01 -03:00
|
|
|
|
(set (make-local-variable 'forward-sexp-function)
|
2012-08-09 00:30:37 -03:00
|
|
|
|
'python-nav-forward-sexp)
|
2012-07-16 10:13:01 -03:00
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(set (make-local-variable 'font-lock-defaults)
|
2012-05-17 00:03:46 -03:00
|
|
|
|
'(python-font-lock-keywords nil nil nil nil))
|
|
|
|
|
|
|
|
|
|
(set (make-local-variable 'syntax-propertize-function)
|
|
|
|
|
python-syntax-propertize-function)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(set (make-local-variable 'indent-line-function)
|
|
|
|
|
#'python-indent-line-function)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(set (make-local-variable 'indent-region-function) #'python-indent-region)
|
2013-10-07 14:51:26 -04:00
|
|
|
|
;; Because indentation is not redundant, we cannot safely reindent code.
|
|
|
|
|
(setq-local electric-indent-inhibit t)
|
2013-11-28 21:03:39 -05:00
|
|
|
|
(setq-local electric-indent-chars (cons ?: electric-indent-chars))
|
2013-11-04 14:14:58 -05:00
|
|
|
|
|
|
|
|
|
;; Add """ ... """ pairing to electric-pair-mode.
|
|
|
|
|
(add-hook 'post-self-insert-hook
|
|
|
|
|
#'python-electric-pair-string-delimiter 'append t)
|
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(set (make-local-variable 'paragraph-start) "\\s-*$")
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(set (make-local-variable 'fill-paragraph-function)
|
2013-11-28 21:03:39 -05:00
|
|
|
|
#'python-fill-paragraph)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(set (make-local-variable 'beginning-of-defun-function)
|
2012-11-12 10:26:50 -03:00
|
|
|
|
#'python-nav-beginning-of-defun)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(set (make-local-variable 'end-of-defun-function)
|
2012-11-12 10:26:50 -03:00
|
|
|
|
#'python-nav-end-of-defun)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(add-hook 'completion-at-point-functions
|
2013-11-28 21:03:39 -05:00
|
|
|
|
#'python-completion-complete-at-point nil 'local)
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
2012-05-17 00:03:42 -03:00
|
|
|
|
(add-hook 'post-self-insert-hook
|
2013-11-28 21:03:39 -05:00
|
|
|
|
#'python-indent-post-self-insert-function 'append 'local)
|
2012-05-17 00:03:42 -03:00
|
|
|
|
|
2013-04-18 23:31:09 -03:00
|
|
|
|
(set (make-local-variable 'imenu-create-index-function)
|
|
|
|
|
#'python-imenu-create-index)
|
2012-11-26 18:45:58 -03:00
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(set (make-local-variable 'add-log-current-defun-function)
|
|
|
|
|
#'python-info-current-defun)
|
|
|
|
|
|
2012-05-17 00:03:45 -03:00
|
|
|
|
(add-hook 'which-func-functions #'python-info-current-defun nil t)
|
|
|
|
|
|
2012-05-17 00:03:05 -03:00
|
|
|
|
(set (make-local-variable 'skeleton-further-elements)
|
|
|
|
|
'((abbrev-mode nil)
|
|
|
|
|
(< '(backward-delete-char-untabify (min python-indent-offset
|
2012-05-17 00:03:35 -03:00
|
|
|
|
(current-column))))
|
|
|
|
|
(^ '(- (1+ (current-indentation))))))
|
2012-05-17 00:03:05 -03:00
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(set (make-local-variable 'eldoc-documentation-function)
|
|
|
|
|
#'python-eldoc-function)
|
|
|
|
|
|
|
|
|
|
(add-to-list 'hs-special-modes-alist
|
2012-05-17 00:03:35 -03:00
|
|
|
|
`(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#"
|
2013-09-04 23:46:34 -04:00
|
|
|
|
,(lambda (_arg)
|
2012-11-12 10:26:50 -03:00
|
|
|
|
(python-nav-end-of-defun)) nil))
|
2012-05-17 00:02:52 -03:00
|
|
|
|
|
|
|
|
|
(set (make-local-variable 'outline-regexp)
|
|
|
|
|
(python-rx (* space) block-start))
|
2014-06-21 08:26:43 -03:00
|
|
|
|
(set (make-local-variable 'outline-heading-end-regexp) ":[^\n]*\n")
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(set (make-local-variable 'outline-level)
|
|
|
|
|
#'(lambda ()
|
|
|
|
|
"`outline-level' function for Python mode."
|
|
|
|
|
(1+ (/ (current-indentation) python-indent-offset))))
|
|
|
|
|
|
2012-05-17 00:03:05 -03:00
|
|
|
|
(python-skeleton-add-menu-items)
|
|
|
|
|
|
2012-08-09 01:08:29 -03:00
|
|
|
|
(make-local-variable 'python-shell-internal-buffer)
|
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
(when python-indent-guess-indent-offset
|
|
|
|
|
(python-indent-guess-indent-offset)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(provide 'python)
|
2012-07-18 16:04:06 -03:00
|
|
|
|
|
|
|
|
|
;; Local Variables:
|
|
|
|
|
;; coding: utf-8
|
|
|
|
|
;; indent-tabs-mode: nil
|
|
|
|
|
;; End:
|
|
|
|
|
|
2012-05-17 00:02:52 -03:00
|
|
|
|
;;; python.el ends here
|