Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1 | ;;; python-mode.el --- Major mode for editing Python programs |
| 2 | |
| 3 | ;; Copyright (C) 1992,1993,1994 Tim Peters |
| 4 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 5 | ;; Author: 2003-2007 http://sf.net/projects/python-mode |
| 6 | ;; 1995-2002 Barry A. Warsaw |
Barry Warsaw | fec75d6 | 1995-07-05 23:26:15 +0000 | [diff] [blame] | 7 | ;; 1992-1994 Tim Peters |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 8 | ;; Maintainer: python-mode@python.org |
| 9 | ;; Created: Feb 1992 |
| 10 | ;; Keywords: python languages oop |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 11 | |
Barry Warsaw | 19b1c61 | 2001-07-06 20:27:29 +0000 | [diff] [blame] | 12 | (defconst py-version "$Revision$" |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 13 | "`python-mode' version number.") |
| 14 | |
Barry Warsaw | cfec359 | 1995-03-10 15:58:16 +0000 | [diff] [blame] | 15 | ;; This software is provided as-is, without express or implied |
| 16 | ;; warranty. Permission to use, copy, modify, distribute or sell this |
| 17 | ;; software, without fee, for any purpose and by any individual or |
| 18 | ;; organization, is hereby granted, provided that the above copyright |
| 19 | ;; notice and this paragraph appear in all copies. |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 20 | |
| 21 | ;;; Commentary: |
Barry Warsaw | 755c671 | 1996-08-01 20:02:55 +0000 | [diff] [blame] | 22 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 23 | ;; This is a major mode for editing Python programs. It was developed by Tim |
| 24 | ;; Peters after an original idea by Michael A. Guravage. Tim subsequently |
| 25 | ;; left the net and in 1995, Barry Warsaw inherited the mode. Tim's now back |
| 26 | ;; but disavows all responsibility for the mode. In fact, we suspect he |
| 27 | ;; doesn't even use Emacs any more. In 2003, python-mode.el was moved to its |
| 28 | ;; own SourceForge project apart from the Python project, and now is |
| 29 | ;; maintained by the volunteers at the python-mode@python.org mailing list. |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 30 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 31 | ;; pdbtrack support contributed by Ken Manheimer, April 2001. Skip Montanaro |
| 32 | ;; has also contributed significantly to python-mode's development. |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 33 | |
Barry Warsaw | b2d5e62 | 2002-04-22 15:29:27 +0000 | [diff] [blame] | 34 | ;; Please use the SourceForge Python project to submit bugs or |
| 35 | ;; patches: |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 36 | ;; |
| 37 | ;; http://sourceforge.net/projects/python |
Barry Warsaw | 3723152 | 1997-12-11 17:23:13 +0000 | [diff] [blame] | 38 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 39 | ;; INSTALLATION: |
| 40 | |
| 41 | ;; To install, just drop this file into a directory on your load-path and |
| 42 | ;; byte-compile it. To set up Emacs to automatically edit files ending in |
| 43 | ;; ".py" using python-mode add the following to your ~/.emacs file (GNU |
| 44 | ;; Emacs) or ~/.xemacs/init.el file (XEmacs): |
| 45 | ;; (setq auto-mode-alist (cons '("\\.py$" . python-mode) auto-mode-alist)) |
| 46 | ;; (setq interpreter-mode-alist (cons '("python" . python-mode) |
| 47 | ;; interpreter-mode-alist)) |
| 48 | ;; (autoload 'python-mode "python-mode" "Python editing mode." t) |
| 49 | ;; |
| 50 | ;; In XEmacs syntax highlighting should be enabled automatically. In GNU |
| 51 | ;; Emacs you may have to add these lines to your ~/.emacs file: |
| 52 | ;; (global-font-lock-mode t) |
| 53 | ;; (setq font-lock-maximum-decoration t) |
| 54 | |
Barry Warsaw | 3723152 | 1997-12-11 17:23:13 +0000 | [diff] [blame] | 55 | ;; FOR MORE INFORMATION: |
| 56 | |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 57 | ;; There is some information on python-mode.el at |
| 58 | |
Barry Warsaw | f7039e2 | 1998-08-20 22:10:46 +0000 | [diff] [blame] | 59 | ;; http://www.python.org/emacs/python-mode/ |
| 60 | ;; |
Barry Warsaw | b2d5e62 | 2002-04-22 15:29:27 +0000 | [diff] [blame] | 61 | ;; It does contain links to other packages that you might find useful, |
| 62 | ;; such as pdb interfaces, OO-Browser links, etc. |
Barry Warsaw | 3723152 | 1997-12-11 17:23:13 +0000 | [diff] [blame] | 63 | |
| 64 | ;; BUG REPORTING: |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 65 | |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 66 | ;; As mentioned above, please use the SourceForge Python project for |
| 67 | ;; submitting bug reports or patches. The old recommendation, to use |
| 68 | ;; C-c C-b will still work, but those reports have a higher chance of |
| 69 | ;; getting buried in my mailbox. Please include a complete, but |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 70 | ;; concise code sample and a recipe for reproducing the bug. Send |
| 71 | ;; suggestions and other comments to python-mode@python.org. |
| 72 | |
| 73 | ;; When in a Python mode buffer, do a C-h m for more help. It's |
Barry Warsaw | 3723152 | 1997-12-11 17:23:13 +0000 | [diff] [blame] | 74 | ;; doubtful that a texinfo manual would be very useful, but if you |
| 75 | ;; want to contribute one, I'll certainly accept it! |
Barry Warsaw | b5e0ecb | 1995-03-14 18:32:54 +0000 | [diff] [blame] | 76 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 77 | ;;; Code: |
| 78 | |
Barry Warsaw | 6dfbe5d | 1998-08-20 21:51:27 +0000 | [diff] [blame] | 79 | (require 'comint) |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 80 | (require 'custom) |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 81 | (require 'cl) |
Barry Warsaw | 1f4fed6 | 2002-07-17 13:45:00 +0000 | [diff] [blame] | 82 | (require 'compile) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 83 | (require 'ansi-color) |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 84 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 85 | |
| 86 | ;; user definable variables |
| 87 | ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 88 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 89 | (defgroup python nil |
| 90 | "Support for the Python programming language, <http://www.python.org/>" |
Barry Warsaw | 5ed843f | 1999-07-28 22:06:06 +0000 | [diff] [blame] | 91 | :group 'languages |
| 92 | :prefix "py-") |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 93 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 94 | (defcustom py-tab-always-indent t |
| 95 | "*Non-nil means TAB in Python mode should always reindent the current line, |
| 96 | regardless of where in the line point is when the TAB command is used." |
| 97 | :type 'boolean |
| 98 | :group 'python) |
| 99 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 100 | (defcustom py-python-command "python" |
| 101 | "*Shell command used to start Python interpreter." |
| 102 | :type 'string |
| 103 | :group 'python) |
| 104 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 105 | (make-obsolete-variable 'py-jpython-command 'py-jython-command) |
| 106 | (defcustom py-jython-command "jython" |
| 107 | "*Shell command used to start the Jython interpreter." |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 108 | :type 'string |
Barry Warsaw | 5ed843f | 1999-07-28 22:06:06 +0000 | [diff] [blame] | 109 | :group 'python |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 110 | :tag "Jython Command") |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 111 | |
Barry Warsaw | aa384fd | 1999-02-16 23:36:16 +0000 | [diff] [blame] | 112 | (defcustom py-default-interpreter 'cpython |
| 113 | "*Which Python interpreter is used by default. |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 114 | The value for this variable can be either `cpython' or `jython'. |
Barry Warsaw | aa384fd | 1999-02-16 23:36:16 +0000 | [diff] [blame] | 115 | |
| 116 | When the value is `cpython', the variables `py-python-command' and |
| 117 | `py-python-command-args' are consulted to determine the interpreter |
| 118 | and arguments to use. |
| 119 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 120 | When the value is `jython', the variables `py-jython-command' and |
| 121 | `py-jython-command-args' are consulted to determine the interpreter |
Barry Warsaw | aa384fd | 1999-02-16 23:36:16 +0000 | [diff] [blame] | 122 | and arguments to use. |
| 123 | |
Barry Warsaw | 11f2156 | 1999-07-28 21:59:43 +0000 | [diff] [blame] | 124 | Note that this variable is consulted only the first time that a Python |
| 125 | mode buffer is visited during an Emacs session. After that, use |
Barry Warsaw | aa384fd | 1999-02-16 23:36:16 +0000 | [diff] [blame] | 126 | \\[py-toggle-shells] to change the interpreter shell." |
| 127 | :type '(choice (const :tag "Python (a.k.a. CPython)" cpython) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 128 | (const :tag "Jython" jython)) |
Barry Warsaw | aa384fd | 1999-02-16 23:36:16 +0000 | [diff] [blame] | 129 | :group 'python) |
| 130 | |
Barry Warsaw | 8f972b7 | 1998-02-05 20:45:49 +0000 | [diff] [blame] | 131 | (defcustom py-python-command-args '("-i") |
| 132 | "*List of string arguments to be used when starting a Python shell." |
| 133 | :type '(repeat string) |
| 134 | :group 'python) |
| 135 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 136 | (make-obsolete-variable 'py-jpython-command-args 'py-jython-command-args) |
| 137 | (defcustom py-jython-command-args '("-i") |
| 138 | "*List of string arguments to be used when starting a Jython shell." |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 139 | :type '(repeat string) |
Barry Warsaw | 5ed843f | 1999-07-28 22:06:06 +0000 | [diff] [blame] | 140 | :group 'python |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 141 | :tag "Jython Command Args") |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 142 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 143 | (defcustom py-indent-offset 4 |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 144 | "*Amount of offset per level of indentation. |
| 145 | `\\[py-guess-indent-offset]' can usually guess a good value when |
| 146 | you're editing someone else's Python code." |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 147 | :type 'integer |
| 148 | :group 'python) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 149 | |
Barry Warsaw | fd4c9e8 | 2001-06-18 23:40:35 +0000 | [diff] [blame] | 150 | (defcustom py-continuation-offset 4 |
Barry Warsaw | 40fb452 | 2001-07-06 20:07:13 +0000 | [diff] [blame] | 151 | "*Additional amount of offset to give for some continuation lines. |
Barry Warsaw | fd4c9e8 | 2001-06-18 23:40:35 +0000 | [diff] [blame] | 152 | Continuation lines are those that immediately follow a backslash |
Barry Warsaw | 40fb452 | 2001-07-06 20:07:13 +0000 | [diff] [blame] | 153 | terminated line. Only those continuation lines for a block opening |
| 154 | statement are given this extra offset." |
Barry Warsaw | fd4c9e8 | 2001-06-18 23:40:35 +0000 | [diff] [blame] | 155 | :type 'integer |
| 156 | :group 'python) |
| 157 | |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 158 | (defcustom py-smart-indentation t |
| 159 | "*Should `python-mode' try to automagically set some indentation variables? |
| 160 | When this variable is non-nil, two things happen when a buffer is set |
| 161 | to `python-mode': |
| 162 | |
Barry Warsaw | c0d2d51 | 1999-06-03 22:18:59 +0000 | [diff] [blame] | 163 | 1. `py-indent-offset' is guessed from existing code in the buffer. |
Barry Warsaw | 639eea6 | 1998-03-16 18:12:13 +0000 | [diff] [blame] | 164 | Only guessed values between 2 and 8 are considered. If a valid |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 165 | guess can't be made (perhaps because you are visiting a new |
Barry Warsaw | 639eea6 | 1998-03-16 18:12:13 +0000 | [diff] [blame] | 166 | file), then the value in `py-indent-offset' is used. |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 167 | |
Barry Warsaw | 639eea6 | 1998-03-16 18:12:13 +0000 | [diff] [blame] | 168 | 2. `indent-tabs-mode' is turned off if `py-indent-offset' does not |
| 169 | equal `tab-width' (`indent-tabs-mode' is never turned on by |
| 170 | Python mode). This means that for newly written code, tabs are |
| 171 | only inserted in indentation if one tab is one indentation |
| 172 | level, otherwise only spaces are used. |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 173 | |
Barry Warsaw | 8046bef | 1998-03-13 20:04:52 +0000 | [diff] [blame] | 174 | Note that both these settings occur *after* `python-mode-hook' is run, |
| 175 | so if you want to defeat the automagic configuration, you must also |
| 176 | set `py-smart-indentation' to nil in your `python-mode-hook'." |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 177 | :type 'boolean |
| 178 | :group 'python) |
| 179 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 180 | (defcustom py-align-multiline-strings-p t |
| 181 | "*Flag describing how multi-line triple quoted strings are aligned. |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 182 | When this flag is non-nil, continuation lines are lined up under the |
| 183 | preceding line's indentation. When this flag is nil, continuation |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 184 | lines are aligned to column zero." |
| 185 | :type '(choice (const :tag "Align under preceding line" t) |
| 186 | (const :tag "Align to column zero" nil)) |
| 187 | :group 'python) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 188 | |
Barry Warsaw | 218eb75 | 1998-09-22 19:51:47 +0000 | [diff] [blame] | 189 | (defcustom py-block-comment-prefix "##" |
Barry Warsaw | 867a32a | 1996-03-07 18:30:26 +0000 | [diff] [blame] | 190 | "*String used by \\[comment-region] to comment out a block of code. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 191 | This should follow the convention for non-indenting comment lines so |
| 192 | that the indentation commands won't get confused (i.e., the string |
| 193 | should be of the form `#x...' where `x' is not a blank or a tab, and |
Barry Warsaw | 218eb75 | 1998-09-22 19:51:47 +0000 | [diff] [blame] | 194 | `...' is arbitrary). However, this string should not end in whitespace." |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 195 | :type 'string |
| 196 | :group 'python) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 197 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 198 | (defcustom py-honor-comment-indentation t |
Barry Warsaw | 6d62775 | 1996-03-06 18:41:38 +0000 | [diff] [blame] | 199 | "*Controls how comment lines influence subsequent indentation. |
Barry Warsaw | 33d6ec0 | 1996-03-05 16:28:07 +0000 | [diff] [blame] | 200 | |
Barry Warsaw | 6d62775 | 1996-03-06 18:41:38 +0000 | [diff] [blame] | 201 | When nil, all comment lines are skipped for indentation purposes, and |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 202 | if possible, a faster algorithm is used (i.e. X/Emacs 19 and beyond). |
Barry Warsaw | 6d62775 | 1996-03-06 18:41:38 +0000 | [diff] [blame] | 203 | |
| 204 | When t, lines that begin with a single `#' are a hint to subsequent |
| 205 | line indentation. If the previous line is such a comment line (as |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 206 | opposed to one that starts with `py-block-comment-prefix'), then its |
Barry Warsaw | 6d62775 | 1996-03-06 18:41:38 +0000 | [diff] [blame] | 207 | indentation is used as a hint for this line's indentation. Lines that |
| 208 | begin with `py-block-comment-prefix' are ignored for indentation |
| 209 | purposes. |
| 210 | |
Barry Warsaw | d36cfe4 | 2002-03-15 16:46:46 +0000 | [diff] [blame] | 211 | When not nil or t, comment lines that begin with a single `#' are used |
| 212 | as indentation hints, unless the comment character is in column zero." |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 213 | :type '(choice |
| 214 | (const :tag "Skip all comment lines (fast)" nil) |
| 215 | (const :tag "Single # `sets' indentation for next line" t) |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 216 | (const :tag "Single # `sets' indentation except at column zero" |
| 217 | other) |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 218 | ) |
| 219 | :group 'python) |
Barry Warsaw | 33d6ec0 | 1996-03-05 16:28:07 +0000 | [diff] [blame] | 220 | |
Barry Warsaw | 516b620 | 1997-08-09 06:43:20 +0000 | [diff] [blame] | 221 | (defcustom py-temp-directory |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 222 | (let ((ok '(lambda (x) |
| 223 | (and x |
| 224 | (setq x (expand-file-name x)) ; always true |
| 225 | (file-directory-p x) |
| 226 | (file-writable-p x) |
| 227 | x)))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 228 | (or (funcall ok (getenv "TMPDIR")) |
| 229 | (funcall ok "/usr/tmp") |
| 230 | (funcall ok "/tmp") |
Barry Warsaw | d164837 | 2002-03-18 18:53:56 +0000 | [diff] [blame] | 231 | (funcall ok "/var/tmp") |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 232 | (funcall ok ".") |
| 233 | (error |
Barry Warsaw | 6c6db0a | 1998-02-25 16:33:56 +0000 | [diff] [blame] | 234 | "Couldn't find a usable temp directory -- set `py-temp-directory'"))) |
Barry Warsaw | d164837 | 2002-03-18 18:53:56 +0000 | [diff] [blame] | 235 | "*Directory used for temporary files created by a *Python* process. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 236 | By default, the first directory from this list that exists and that you |
Barry Warsaw | d164837 | 2002-03-18 18:53:56 +0000 | [diff] [blame] | 237 | can write into: the value (if any) of the environment variable TMPDIR, |
| 238 | /usr/tmp, /tmp, /var/tmp, or the current directory." |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 239 | :type 'string |
| 240 | :group 'python) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 241 | |
Barry Warsaw | 516b620 | 1997-08-09 06:43:20 +0000 | [diff] [blame] | 242 | (defcustom py-beep-if-tab-change t |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 243 | "*Ring the bell if `tab-width' is changed. |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 244 | If a comment of the form |
| 245 | |
| 246 | \t# vi:set tabsize=<number>: |
| 247 | |
| 248 | is found before the first code line when the file is entered, and the |
| 249 | current value of (the general Emacs variable) `tab-width' does not |
| 250 | equal <number>, `tab-width' is set to <number>, a message saying so is |
| 251 | displayed in the echo area, and if `py-beep-if-tab-change' is non-nil |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 252 | the Emacs bell is also rung as a warning." |
| 253 | :type 'boolean |
| 254 | :group 'python) |
| 255 | |
Barry Warsaw | 9981d22 | 1997-12-03 05:25:48 +0000 | [diff] [blame] | 256 | (defcustom py-jump-on-exception t |
| 257 | "*Jump to innermost exception frame in *Python Output* buffer. |
Barry Warsaw | 12c9294 | 1998-08-10 21:44:37 +0000 | [diff] [blame] | 258 | When this variable is non-nil and an exception occurs when running |
Barry Warsaw | 9981d22 | 1997-12-03 05:25:48 +0000 | [diff] [blame] | 259 | Python code synchronously in a subprocess, jump immediately to the |
Barry Warsaw | 12c9294 | 1998-08-10 21:44:37 +0000 | [diff] [blame] | 260 | source code of the innermost traceback frame." |
Barry Warsaw | 3bfed5b | 1998-05-19 16:25:04 +0000 | [diff] [blame] | 261 | :type 'boolean |
| 262 | :group 'python) |
| 263 | |
| 264 | (defcustom py-ask-about-save t |
| 265 | "If not nil, ask about which buffers to save before executing some code. |
| 266 | Otherwise, all modified buffers are saved without asking." |
| 267 | :type 'boolean |
| 268 | :group 'python) |
Barry Warsaw | 9981d22 | 1997-12-03 05:25:48 +0000 | [diff] [blame] | 269 | |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 270 | (defcustom py-backspace-function 'backward-delete-char-untabify |
| 271 | "*Function called by `py-electric-backspace' when deleting backwards." |
| 272 | :type 'function |
| 273 | :group 'python) |
| 274 | |
| 275 | (defcustom py-delete-function 'delete-char |
| 276 | "*Function called by `py-electric-delete' when deleting forwards." |
| 277 | :type 'function |
| 278 | :group 'python) |
| 279 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 280 | (defcustom py-imenu-show-method-args-p nil |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 281 | "*Controls echoing of arguments of functions & methods in the Imenu buffer. |
| 282 | When non-nil, arguments are printed." |
| 283 | :type 'boolean |
| 284 | :group 'python) |
| 285 | (make-variable-buffer-local 'py-indent-offset) |
| 286 | |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 287 | (defcustom py-pdbtrack-do-tracking-p t |
| 288 | "*Controls whether the pdbtrack feature is enabled or not. |
| 289 | When non-nil, pdbtrack is enabled in all comint-based buffers, |
| 290 | e.g. shell buffers and the *Python* buffer. When using pdb to debug a |
| 291 | Python program, pdbtrack notices the pdb prompt and displays the |
| 292 | source file and line that the program is stopped at, much the same way |
| 293 | as gud-mode does for debugging C programs with gdb." |
| 294 | :type 'boolean |
| 295 | :group 'python) |
| 296 | (make-variable-buffer-local 'py-pdbtrack-do-tracking-p) |
| 297 | |
| 298 | (defcustom py-pdbtrack-minor-mode-string " PDB" |
| 299 | "*String to use in the minor mode list when pdbtrack is enabled." |
| 300 | :type 'string |
| 301 | :group 'python) |
| 302 | |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 303 | (defcustom py-import-check-point-max |
| 304 | 20000 |
| 305 | "Maximum number of characters to search for a Java-ish import statement. |
| 306 | When `python-mode' tries to calculate the shell to use (either a |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 307 | CPython or a Jython shell), it looks at the so-called `shebang' line |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 308 | -- i.e. #! line. If that's not available, it looks at some of the |
| 309 | file heading imports to see if they look Java-like." |
| 310 | :type 'integer |
| 311 | :group 'python |
| 312 | ) |
| 313 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 314 | (make-obsolete-variable 'py-jpython-packages 'py-jython-packages) |
| 315 | (defcustom py-jython-packages |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 316 | '("java" "javax" "org" "com") |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 317 | "Imported packages that imply `jython-mode'." |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 318 | :type '(repeat string) |
| 319 | :group 'python) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 320 | |
Barry Warsaw | c0ecb53 | 1998-01-20 21:43:34 +0000 | [diff] [blame] | 321 | ;; Not customizable |
| 322 | (defvar py-master-file nil |
| 323 | "If non-nil, execute the named file instead of the buffer's file. |
Barry Warsaw | 50b3eb6 | 1998-02-25 15:57:47 +0000 | [diff] [blame] | 324 | The intent is to allow you to set this variable in the file's local |
Barry Warsaw | c0ecb53 | 1998-01-20 21:43:34 +0000 | [diff] [blame] | 325 | variable section, e.g.: |
| 326 | |
| 327 | # Local Variables: |
| 328 | # py-master-file: \"master.py\" |
| 329 | # End: |
| 330 | |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 331 | so that typing \\[py-execute-buffer] in that buffer executes the named |
Barry Warsaw | 1bbc031 | 1998-10-27 21:54:56 +0000 | [diff] [blame] | 332 | master file instead of the buffer's file. If the file name has a |
| 333 | relative path, the value of variable `default-directory' for the |
| 334 | buffer is prepended to come up with a file name.") |
Barry Warsaw | c0ecb53 | 1998-01-20 21:43:34 +0000 | [diff] [blame] | 335 | (make-variable-buffer-local 'py-master-file) |
| 336 | |
Barry Warsaw | 29a90f0 | 2002-04-22 21:48:20 +0000 | [diff] [blame] | 337 | (defcustom py-pychecker-command "pychecker" |
| 338 | "*Shell command used to run Pychecker." |
| 339 | :type 'string |
| 340 | :group 'python |
| 341 | :tag "Pychecker Command") |
| 342 | |
| 343 | (defcustom py-pychecker-command-args '("--stdlib") |
| 344 | "*List of string arguments to be passed to pychecker." |
| 345 | :type '(repeat string) |
| 346 | :group 'python |
| 347 | :tag "Pychecker Command Args") |
| 348 | |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 349 | (defvar py-shell-alist |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 350 | '(("jython" . 'jython) |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 351 | ("python" . 'cpython)) |
| 352 | "*Alist of interpreters and python shells. Used by `py-choose-shell' |
| 353 | to select the appropriate python interpreter mode for a file.") |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 354 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 355 | (defcustom py-shell-input-prompt-1-regexp "^>>> " |
| 356 | "*A regular expression to match the input prompt of the shell." |
| 357 | :type 'string |
| 358 | :group 'python) |
| 359 | |
| 360 | (defcustom py-shell-input-prompt-2-regexp "^[.][.][.] " |
| 361 | "*A regular expression to match the input prompt of the shell after the |
| 362 | first line of input." |
| 363 | :type 'string |
| 364 | :group 'python) |
| 365 | |
| 366 | (defcustom py-shell-switch-buffers-on-execute t |
| 367 | "*Controls switching to the Python buffer where commands are |
| 368 | executed. When non-nil the buffer switches to the Python buffer, if |
| 369 | not no switching occurs." |
| 370 | :type 'boolean |
| 371 | :group 'python) |
| 372 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 373 | |
| 374 | ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 375 | ;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT |
| 376 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 377 | (defvar py-line-number-offset 0 |
| 378 | "When an exception occurs as a result of py-execute-region, a |
| 379 | subsequent py-up-exception needs the line number where the region |
| 380 | started, in order to jump to the correct file line. This variable is |
| 381 | set in py-execute-region and used in py-jump-to-exception.") |
| 382 | |
Barry Warsaw | 5e21cb0 | 1997-11-05 18:41:11 +0000 | [diff] [blame] | 383 | (defconst py-emacs-features |
| 384 | (let (features) |
Barry Warsaw | 5e21cb0 | 1997-11-05 18:41:11 +0000 | [diff] [blame] | 385 | features) |
Barry Warsaw | c12c62e | 1997-09-04 04:18:07 +0000 | [diff] [blame] | 386 | "A list of features extant in the Emacs you are using. |
Barry Warsaw | 6ae21ad | 1997-11-06 14:36:49 +0000 | [diff] [blame] | 387 | There are many flavors of Emacs out there, with different levels of |
| 388 | support for features needed by `python-mode'.") |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 389 | |
Barry Warsaw | 29a90f0 | 2002-04-22 21:48:20 +0000 | [diff] [blame] | 390 | ;; Face for None, True, False, self, and Ellipsis |
| 391 | (defvar py-pseudo-keyword-face 'py-pseudo-keyword-face |
| 392 | "Face for pseudo keywords in Python mode, like self, True, False, Ellipsis.") |
| 393 | (make-face 'py-pseudo-keyword-face) |
| 394 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 395 | ;; PEP 318 decorators |
| 396 | (defvar py-decorators-face 'py-decorators-face |
| 397 | "Face method decorators.") |
| 398 | (make-face 'py-decorators-face) |
| 399 | |
| 400 | ;; Face for builtins |
| 401 | (defvar py-builtins-face 'py-builtins-face |
| 402 | "Face for builtins like TypeError, object, open, and exec.") |
| 403 | (make-face 'py-builtins-face) |
| 404 | |
| 405 | ;; XXX, TODO, and FIXME comments and such |
| 406 | (defvar py-XXX-tag-face 'py-XXX-tag-face |
| 407 | "Face for XXX, TODO, and FIXME tags") |
| 408 | (make-face 'py-XXX-tag-face) |
| 409 | |
Barry Warsaw | 29a90f0 | 2002-04-22 21:48:20 +0000 | [diff] [blame] | 410 | (defun py-font-lock-mode-hook () |
| 411 | (or (face-differs-from-default-p 'py-pseudo-keyword-face) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 412 | (copy-face 'font-lock-keyword-face 'py-pseudo-keyword-face)) |
| 413 | (or (face-differs-from-default-p 'py-builtins-face) |
| 414 | (copy-face 'font-lock-keyword-face 'py-builtins-face)) |
| 415 | (or (face-differs-from-default-p 'py-decorators-face) |
| 416 | (copy-face 'py-pseudo-keyword-face 'py-decorators-face)) |
| 417 | (or (face-differs-from-default-p 'py-XXX-tag-face) |
| 418 | (copy-face 'font-lock-comment-face 'py-XXX-tag-face)) |
| 419 | ) |
Barry Warsaw | 29a90f0 | 2002-04-22 21:48:20 +0000 | [diff] [blame] | 420 | (add-hook 'font-lock-mode-hook 'py-font-lock-mode-hook) |
| 421 | |
Barry Warsaw | fb07f40 | 1997-02-24 03:37:22 +0000 | [diff] [blame] | 422 | (defvar python-font-lock-keywords |
Barry Warsaw | b8f1166 | 1997-11-06 14:35:15 +0000 | [diff] [blame] | 423 | (let ((kw1 (mapconcat 'identity |
| 424 | '("and" "assert" "break" "class" |
| 425 | "continue" "def" "del" "elif" |
| 426 | "else" "except" "exec" "for" |
| 427 | "from" "global" "if" "import" |
| 428 | "in" "is" "lambda" "not" |
| 429 | "or" "pass" "print" "raise" |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 430 | "return" "while" "with" "yield" |
Barry Warsaw | b8f1166 | 1997-11-06 14:35:15 +0000 | [diff] [blame] | 431 | ) |
| 432 | "\\|")) |
| 433 | (kw2 (mapconcat 'identity |
| 434 | '("else:" "except:" "finally:" "try:") |
| 435 | "\\|")) |
Skip Montanaro | 302e550 | 2003-12-18 21:53:33 +0000 | [diff] [blame] | 436 | (kw3 (mapconcat 'identity |
| 437 | '("ArithmeticError" "AssertionError" |
| 438 | "AttributeError" "DeprecationWarning" "EOFError" |
| 439 | "Ellipsis" "EnvironmentError" "Exception" "False" |
| 440 | "FloatingPointError" "FutureWarning" "IOError" |
| 441 | "ImportError" "IndentationError" "IndexError" |
| 442 | "KeyError" "KeyboardInterrupt" "LookupError" |
| 443 | "MemoryError" "NameError" "None" "NotImplemented" |
| 444 | "NotImplementedError" "OSError" "OverflowError" |
| 445 | "OverflowWarning" "PendingDeprecationWarning" |
| 446 | "ReferenceError" "RuntimeError" "RuntimeWarning" |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 447 | "StopIteration" "SyntaxError" |
Skip Montanaro | 302e550 | 2003-12-18 21:53:33 +0000 | [diff] [blame] | 448 | "SyntaxWarning" "SystemError" "SystemExit" |
| 449 | "TabError" "True" "TypeError" "UnboundLocalError" |
| 450 | "UnicodeDecodeError" "UnicodeEncodeError" |
| 451 | "UnicodeError" "UnicodeTranslateError" |
| 452 | "UserWarning" "ValueError" "Warning" |
| 453 | "ZeroDivisionError" "__debug__" |
| 454 | "__import__" "__name__" "abs" "apply" "basestring" |
| 455 | "bool" "buffer" "callable" "chr" "classmethod" |
Neal Norwitz | ce96f69 | 2006-03-17 06:49:51 +0000 | [diff] [blame] | 456 | "cmp" "compile" "complex" "copyright" |
Skip Montanaro | 302e550 | 2003-12-18 21:53:33 +0000 | [diff] [blame] | 457 | "delattr" "dict" "dir" "divmod" |
Neal Norwitz | 0168802 | 2007-08-12 00:43:29 +0000 | [diff] [blame] | 458 | "enumerate" "eval" "exit" "file" |
Skip Montanaro | 302e550 | 2003-12-18 21:53:33 +0000 | [diff] [blame] | 459 | "filter" "float" "getattr" "globals" "hasattr" |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 460 | "hash" "hex" "id" "int" |
Skip Montanaro | 302e550 | 2003-12-18 21:53:33 +0000 | [diff] [blame] | 461 | "isinstance" "issubclass" "iter" "len" "license" |
| 462 | "list" "locals" "long" "map" "max" "min" "object" |
| 463 | "oct" "open" "ord" "pow" "property" "range" |
Guido van Rossum | 0919a1a | 2006-08-26 20:49:04 +0000 | [diff] [blame] | 464 | "reload" "repr" "round" |
Skip Montanaro | 302e550 | 2003-12-18 21:53:33 +0000 | [diff] [blame] | 465 | "setattr" "slice" "staticmethod" "str" "sum" |
| 466 | "super" "tuple" "type" "unichr" "unicode" "vars" |
Neal Norwitz | ce96f69 | 2006-03-17 06:49:51 +0000 | [diff] [blame] | 467 | "zip") |
Skip Montanaro | 302e550 | 2003-12-18 21:53:33 +0000 | [diff] [blame] | 468 | "\\|")) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 469 | (kw4 (mapconcat 'identity |
| 470 | ;; Exceptions and warnings |
| 471 | '("ArithmeticError" "AssertionError" |
| 472 | "AttributeError" "DeprecationWarning" "EOFError" |
| 473 | "EnvironmentError" "Exception" |
| 474 | "FloatingPointError" "FutureWarning" "IOError" |
| 475 | "ImportError" "IndentationError" "IndexError" |
| 476 | "KeyError" "KeyboardInterrupt" "LookupError" |
| 477 | "MemoryError" "NameError" "NotImplemented" |
| 478 | "NotImplementedError" "OSError" "OverflowError" |
| 479 | "OverflowWarning" "PendingDeprecationWarning" |
| 480 | "ReferenceError" "RuntimeError" "RuntimeWarning" |
| 481 | "StandardError" "StopIteration" "SyntaxError" |
| 482 | "SyntaxWarning" "SystemError" "SystemExit" |
| 483 | "TabError" "TypeError" "UnboundLocalError" |
| 484 | "UnicodeDecodeError" "UnicodeEncodeError" |
| 485 | "UnicodeError" "UnicodeTranslateError" |
| 486 | "UserWarning" "ValueError" "Warning" |
| 487 | "ZeroDivisionError") |
| 488 | "\\|")) |
Barry Warsaw | b8f1166 | 1997-11-06 14:35:15 +0000 | [diff] [blame] | 489 | ) |
Barry Warsaw | 33ab6e4 | 1996-03-05 00:44:31 +0000 | [diff] [blame] | 490 | (list |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 491 | '("^[ \t]*\\(@.+\\)" 1 'py-decorators-face) |
Barry Warsaw | ef3c891 | 1997-11-05 18:55:50 +0000 | [diff] [blame] | 492 | ;; keywords |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 493 | (cons (concat "\\<\\(" kw1 "\\)\\>[ \n\t(]") 1) |
Skip Montanaro | 302e550 | 2003-12-18 21:53:33 +0000 | [diff] [blame] | 494 | ;; builtins when they don't appear as object attributes |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 495 | (list (concat "\\([^. \t]\\|^\\)[ \t]*\\<\\(" kw3 "\\)\\>[ \n\t(]") 2 |
| 496 | 'py-builtins-face) |
Barry Warsaw | b8f1166 | 1997-11-06 14:35:15 +0000 | [diff] [blame] | 497 | ;; block introducing keywords with immediately following colons. |
| 498 | ;; Yes "except" is in both lists. |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 499 | (cons (concat "\\<\\(" kw2 "\\)[ \n\t(]") 1) |
| 500 | ;; Exceptions |
| 501 | (list (concat "\\<\\(" kw4 "\\)[ \n\t:,(]") 1 'py-builtins-face) |
| 502 | ;; `as' but only in "import foo as bar" or "with foo as bar" |
| 503 | '("[ \t]*\\(\\<from\\>.*\\)?\\<import\\>.*\\<\\(as\\)\\>" . 2) |
| 504 | '("[ \t]*\\<with\\>.*\\<\\(as\\)\\>" . 1) |
Barry Warsaw | 33ab6e4 | 1996-03-05 00:44:31 +0000 | [diff] [blame] | 505 | ;; classes |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 506 | '("\\<class[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)" 1 font-lock-type-face) |
Barry Warsaw | 33ab6e4 | 1996-03-05 00:44:31 +0000 | [diff] [blame] | 507 | ;; functions |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 508 | '("\\<def[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)" |
Barry Warsaw | 33ab6e4 | 1996-03-05 00:44:31 +0000 | [diff] [blame] | 509 | 1 font-lock-function-name-face) |
Barry Warsaw | 29a90f0 | 2002-04-22 21:48:20 +0000 | [diff] [blame] | 510 | ;; pseudo-keywords |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 511 | '("\\<\\(self\\|None\\|True\\|False\\|Ellipsis\\)\\>" |
Barry Warsaw | 29a90f0 | 2002-04-22 21:48:20 +0000 | [diff] [blame] | 512 | 1 py-pseudo-keyword-face) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 513 | ;; XXX, TODO, and FIXME tags |
| 514 | '("XXX\\|TODO\\|FIXME" 0 py-XXX-tag-face t) |
Barry Warsaw | 33ab6e4 | 1996-03-05 00:44:31 +0000 | [diff] [blame] | 515 | )) |
Barry Warsaw | 62d9d6e | 1996-03-06 20:32:27 +0000 | [diff] [blame] | 516 | "Additional expressions to highlight in Python mode.") |
Barry Warsaw | fb07f40 | 1997-02-24 03:37:22 +0000 | [diff] [blame] | 517 | (put 'python-mode 'font-lock-defaults '(python-font-lock-keywords)) |
| 518 | |
Barry Warsaw | e467bfb | 1997-11-26 05:40:58 +0000 | [diff] [blame] | 519 | ;; have to bind py-file-queue before installing the kill-emacs-hook |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 520 | (defvar py-file-queue nil |
| 521 | "Queue of Python temp files awaiting execution. |
| 522 | Currently-active file is at the head of the list.") |
| 523 | |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 524 | (defvar py-pdbtrack-is-tracking-p nil) |
Ken Manheimer | ed6000a | 2003-03-03 17:09:44 +0000 | [diff] [blame] | 525 | |
Barry Warsaw | 29a90f0 | 2002-04-22 21:48:20 +0000 | [diff] [blame] | 526 | (defvar py-pychecker-history nil) |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 527 | |
| 528 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 529 | |
| 530 | ;; Constants |
| 531 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 532 | (defconst py-stringlit-re |
| 533 | (concat |
Barry Warsaw | 751f493 | 1998-05-19 16:06:21 +0000 | [diff] [blame] | 534 | ;; These fail if backslash-quote ends the string (not worth |
| 535 | ;; fixing?). They precede the short versions so that the first two |
| 536 | ;; quotes don't look like an empty short string. |
| 537 | ;; |
| 538 | ;; (maybe raw), long single quoted triple quoted strings (SQTQ), |
| 539 | ;; with potential embedded single quotes |
| 540 | "[rR]?'''[^']*\\(\\('[^']\\|''[^']\\)[^']*\\)*'''" |
| 541 | "\\|" |
| 542 | ;; (maybe raw), long double quoted triple quoted strings (DQTQ), |
| 543 | ;; with potential embedded double quotes |
| 544 | "[rR]?\"\"\"[^\"]*\\(\\(\"[^\"]\\|\"\"[^\"]\\)[^\"]*\\)*\"\"\"" |
| 545 | "\\|" |
| 546 | "[rR]?'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 547 | "\\|" ; or |
Barry Warsaw | 751f493 | 1998-05-19 16:06:21 +0000 | [diff] [blame] | 548 | "[rR]?\"\\([^\"\n\\]\\|\\\\.\\)*\"" ; double-quoted |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 549 | ) |
| 550 | "Regular expression matching a Python string literal.") |
Barry Warsaw | 751f493 | 1998-05-19 16:06:21 +0000 | [diff] [blame] | 551 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 552 | (defconst py-continued-re |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 553 | ;; This is tricky because a trailing backslash does not mean |
| 554 | ;; continuation if it's in a comment |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 555 | (concat |
| 556 | "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*" |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 557 | "\\\\$") |
| 558 | "Regular expression matching Python backslash continuation lines.") |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 559 | |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 560 | (defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)" |
Barry Warsaw | 71c3adb | 1998-08-10 16:34:33 +0000 | [diff] [blame] | 561 | "Regular expression matching a blank or comment line.") |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 562 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 563 | (defconst py-outdent-re |
| 564 | (concat "\\(" (mapconcat 'identity |
| 565 | '("else:" |
| 566 | "except\\(\\s +.*\\)?:" |
| 567 | "finally:" |
| 568 | "elif\\s +.*:") |
| 569 | "\\|") |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 570 | "\\)") |
| 571 | "Regular expression matching statements to be dedented one level.") |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 572 | |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 573 | (defconst py-block-closing-keywords-re |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 574 | "\\(return\\|raise\\|break\\|continue\\|pass\\)" |
| 575 | "Regular expression matching keywords which typically close a block.") |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 576 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 577 | (defconst py-no-outdent-re |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 578 | (concat |
| 579 | "\\(" |
| 580 | (mapconcat 'identity |
| 581 | (list "try:" |
| 582 | "except\\(\\s +.*\\)?:" |
| 583 | "while\\s +.*:" |
| 584 | "for\\s +.*:" |
| 585 | "if\\s +.*:" |
| 586 | "elif\\s +.*:" |
| 587 | (concat py-block-closing-keywords-re "[ \t\n]") |
| 588 | ) |
| 589 | "\\|") |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 590 | "\\)") |
| 591 | "Regular expression matching lines not to dedent after.") |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 592 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 593 | (defvar py-traceback-line-re |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 594 | "[ \t]+File \"\\([^\"]+\\)\", line \\([0-9]+\\)" |
| 595 | "Regular expression that describes tracebacks.") |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 596 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 597 | ;; pdbtrack constants |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 598 | (defconst py-pdbtrack-stack-entry-regexp |
Ken Manheimer | e7aca52 | 2003-03-03 00:35:32 +0000 | [diff] [blame] | 599 | ; "^> \\([^(]+\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_]+\\)()" |
| 600 | "^> \\(.*\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_]+\\)()" |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 601 | "Regular expression pdbtrack uses to find a stack trace entry.") |
| 602 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 603 | (defconst py-pdbtrack-input-prompt "\n[(<]*[Pp]db[>)]+ " |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 604 | "Regular expression pdbtrack uses to recognize a pdb prompt.") |
| 605 | |
| 606 | (defconst py-pdbtrack-track-range 10000 |
| 607 | "Max number of characters from end of buffer to search for stack entry.") |
| 608 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 609 | |
| 610 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 611 | ;; Major mode boilerplate |
| 612 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 613 | ;; define a mode-specific abbrev table for those who use such things |
| 614 | (defvar python-mode-abbrev-table nil |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 615 | "Abbrev table in use in `python-mode' buffers.") |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 616 | (define-abbrev-table 'python-mode-abbrev-table nil) |
| 617 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 618 | (defvar python-mode-hook nil |
| 619 | "*Hook called by `python-mode'.") |
| 620 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 621 | (make-obsolete-variable 'jpython-mode-hook 'jython-mode-hook) |
| 622 | (defvar jython-mode-hook nil |
| 623 | "*Hook called by `jython-mode'. `jython-mode' also calls |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 624 | `python-mode-hook'.") |
| 625 | |
Barry Warsaw | 56bd2ed | 2002-04-25 15:44:17 +0000 | [diff] [blame] | 626 | (defvar py-shell-hook nil |
| 627 | "*Hook called by `py-shell'.") |
| 628 | |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 629 | ;; In previous version of python-mode.el, the hook was incorrectly |
| 630 | ;; called py-mode-hook, and was not defvar'd. Deprecate its use. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 631 | (and (fboundp 'make-obsolete-variable) |
| 632 | (make-obsolete-variable 'py-mode-hook 'python-mode-hook)) |
| 633 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 634 | (defvar py-mode-map () |
| 635 | "Keymap used in `python-mode' buffers.") |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 636 | (if py-mode-map |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 637 | nil |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 638 | (setq py-mode-map (make-sparse-keymap)) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 639 | ;; electric keys |
| 640 | (define-key py-mode-map ":" 'py-electric-colon) |
| 641 | ;; indentation level modifiers |
| 642 | (define-key py-mode-map "\C-c\C-l" 'py-shift-region-left) |
| 643 | (define-key py-mode-map "\C-c\C-r" 'py-shift-region-right) |
| 644 | (define-key py-mode-map "\C-c<" 'py-shift-region-left) |
| 645 | (define-key py-mode-map "\C-c>" 'py-shift-region-right) |
| 646 | ;; subprocess commands |
| 647 | (define-key py-mode-map "\C-c\C-c" 'py-execute-buffer) |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 648 | (define-key py-mode-map "\C-c\C-m" 'py-execute-import-or-reload) |
Barry Warsaw | 1d0364b | 1998-05-19 16:15:26 +0000 | [diff] [blame] | 649 | (define-key py-mode-map "\C-c\C-s" 'py-execute-string) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 650 | (define-key py-mode-map "\C-c|" 'py-execute-region) |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 651 | (define-key py-mode-map "\e\C-x" 'py-execute-def-or-class) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 652 | (define-key py-mode-map "\C-c!" 'py-shell) |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 653 | (define-key py-mode-map "\C-c\C-t" 'py-toggle-shells) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 654 | ;; Caution! Enter here at your own risk. We are trying to support |
| 655 | ;; several behaviors and it gets disgusting. :-( This logic ripped |
| 656 | ;; largely from CC Mode. |
| 657 | ;; |
| 658 | ;; In XEmacs 19, Emacs 19, and Emacs 20, we use this to bind |
| 659 | ;; backwards deletion behavior to DEL, which both Delete and |
| 660 | ;; Backspace get translated to. There's no way to separate this |
| 661 | ;; behavior in a clean way, so deal with it! Besides, it's been |
| 662 | ;; this way since the dawn of time. |
| 663 | (if (not (boundp 'delete-key-deletes-forward)) |
| 664 | (define-key py-mode-map "\177" 'py-electric-backspace) |
| 665 | ;; However, XEmacs 20 actually achieved enlightenment. It is |
| 666 | ;; possible to sanely define both backward and forward deletion |
| 667 | ;; behavior under X separately (TTYs are forever beyond hope, but |
| 668 | ;; who cares? XEmacs 20 does the right thing with these too). |
| 669 | (define-key py-mode-map [delete] 'py-electric-delete) |
| 670 | (define-key py-mode-map [backspace] 'py-electric-backspace)) |
Barry Warsaw | 8c4a8de | 1997-11-26 20:30:33 +0000 | [diff] [blame] | 671 | ;; Separate M-BS from C-M-h. The former should remain |
| 672 | ;; backward-kill-word. |
| 673 | (define-key py-mode-map [(control meta h)] 'py-mark-def-or-class) |
Barry Warsaw | 2518c67 | 1997-11-05 00:51:08 +0000 | [diff] [blame] | 674 | (define-key py-mode-map "\C-c\C-k" 'py-mark-block) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 675 | ;; Miscellaneous |
| 676 | (define-key py-mode-map "\C-c:" 'py-guess-indent-offset) |
| 677 | (define-key py-mode-map "\C-c\t" 'py-indent-region) |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 678 | (define-key py-mode-map "\C-c\C-d" 'py-pdbtrack-toggle-stack-tracking) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 679 | (define-key py-mode-map "\C-c\C-n" 'py-next-statement) |
| 680 | (define-key py-mode-map "\C-c\C-p" 'py-previous-statement) |
| 681 | (define-key py-mode-map "\C-c\C-u" 'py-goto-block-up) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 682 | (define-key py-mode-map "\C-c#" 'py-comment-region) |
| 683 | (define-key py-mode-map "\C-c?" 'py-describe-mode) |
Barry Warsaw | a7cc43b | 2002-04-22 17:15:19 +0000 | [diff] [blame] | 684 | (define-key py-mode-map "\C-c\C-h" 'py-help-at-point) |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 685 | (define-key py-mode-map "\e\C-a" 'py-beginning-of-def-or-class) |
| 686 | (define-key py-mode-map "\e\C-e" 'py-end-of-def-or-class) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 687 | (define-key py-mode-map "\C-c-" 'py-up-exception) |
| 688 | (define-key py-mode-map "\C-c=" 'py-down-exception) |
Barry Warsaw | f8ddb6a | 1999-01-18 21:49:39 +0000 | [diff] [blame] | 689 | ;; stuff that is `standard' but doesn't interface well with |
| 690 | ;; python-mode, which forces us to rebind to special commands |
| 691 | (define-key py-mode-map "\C-xnd" 'py-narrow-to-defun) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 692 | ;; information |
| 693 | (define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report) |
| 694 | (define-key py-mode-map "\C-c\C-v" 'py-version) |
Barry Warsaw | 29a90f0 | 2002-04-22 21:48:20 +0000 | [diff] [blame] | 695 | (define-key py-mode-map "\C-c\C-w" 'py-pychecker-run) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 696 | ;; shadow global bindings for newline-and-indent w/ the py- version. |
| 697 | ;; BAW - this is extremely bad form, but I'm not going to change it |
| 698 | ;; for now. |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 699 | (mapcar #'(lambda (key) |
| 700 | (define-key py-mode-map key 'py-newline-and-indent)) |
| 701 | (where-is-internal 'newline-and-indent)) |
Barry Warsaw | f19feb8 | 1999-01-21 17:06:11 +0000 | [diff] [blame] | 702 | ;; Force RET to be py-newline-and-indent even if it didn't get |
| 703 | ;; mapped by the above code. motivation: Emacs' default binding for |
| 704 | ;; RET is `newline' and C-j is `newline-and-indent'. Most Pythoneers |
| 705 | ;; expect RET to do a `py-newline-and-indent' and any Emacsers who |
| 706 | ;; dislike this are probably knowledgeable enough to do a rebind. |
| 707 | ;; However, we do *not* change C-j since many Emacsers have already |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 708 | ;; swapped RET and C-j and they don't want C-j bound to `newline' to |
Barry Warsaw | f19feb8 | 1999-01-21 17:06:11 +0000 | [diff] [blame] | 709 | ;; change. |
| 710 | (define-key py-mode-map "\C-m" 'py-newline-and-indent) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 711 | ) |
| 712 | |
| 713 | (defvar py-mode-output-map nil |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 714 | "Keymap used in *Python Output* buffers.") |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 715 | (if py-mode-output-map |
| 716 | nil |
| 717 | (setq py-mode-output-map (make-sparse-keymap)) |
| 718 | (define-key py-mode-output-map [button2] 'py-mouseto-exception) |
| 719 | (define-key py-mode-output-map "\C-c\C-c" 'py-goto-exception) |
| 720 | ;; TBD: Disable all self-inserting keys. This is bogus, we should |
| 721 | ;; really implement this as *Python Output* buffer being read-only |
| 722 | (mapcar #' (lambda (key) |
| 723 | (define-key py-mode-output-map key |
| 724 | #'(lambda () (interactive) (beep)))) |
| 725 | (where-is-internal 'self-insert-command)) |
Barry Warsaw | 850437a | 1995-03-08 21:50:28 +0000 | [diff] [blame] | 726 | ) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 727 | |
Barry Warsaw | 6dfbe5d | 1998-08-20 21:51:27 +0000 | [diff] [blame] | 728 | (defvar py-shell-map nil |
| 729 | "Keymap used in *Python* shell buffers.") |
| 730 | (if py-shell-map |
| 731 | nil |
| 732 | (setq py-shell-map (copy-keymap comint-mode-map)) |
| 733 | (define-key py-shell-map [tab] 'tab-to-tab-stop) |
| 734 | (define-key py-shell-map "\C-c-" 'py-up-exception) |
| 735 | (define-key py-shell-map "\C-c=" 'py-down-exception) |
| 736 | ) |
| 737 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 738 | (defvar py-mode-syntax-table nil |
| 739 | "Syntax table used in `python-mode' buffers.") |
Barry Warsaw | a7cc43b | 2002-04-22 17:15:19 +0000 | [diff] [blame] | 740 | (when (not py-mode-syntax-table) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 741 | (setq py-mode-syntax-table (make-syntax-table)) |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 742 | (modify-syntax-entry ?\( "()" py-mode-syntax-table) |
| 743 | (modify-syntax-entry ?\) ")(" py-mode-syntax-table) |
| 744 | (modify-syntax-entry ?\[ "(]" py-mode-syntax-table) |
| 745 | (modify-syntax-entry ?\] ")[" py-mode-syntax-table) |
| 746 | (modify-syntax-entry ?\{ "(}" py-mode-syntax-table) |
| 747 | (modify-syntax-entry ?\} "){" py-mode-syntax-table) |
| 748 | ;; Add operator symbols misassigned in the std table |
| 749 | (modify-syntax-entry ?\$ "." py-mode-syntax-table) |
| 750 | (modify-syntax-entry ?\% "." py-mode-syntax-table) |
| 751 | (modify-syntax-entry ?\& "." py-mode-syntax-table) |
| 752 | (modify-syntax-entry ?\* "." py-mode-syntax-table) |
| 753 | (modify-syntax-entry ?\+ "." py-mode-syntax-table) |
| 754 | (modify-syntax-entry ?\- "." py-mode-syntax-table) |
| 755 | (modify-syntax-entry ?\/ "." py-mode-syntax-table) |
| 756 | (modify-syntax-entry ?\< "." py-mode-syntax-table) |
| 757 | (modify-syntax-entry ?\= "." py-mode-syntax-table) |
| 758 | (modify-syntax-entry ?\> "." py-mode-syntax-table) |
| 759 | (modify-syntax-entry ?\| "." py-mode-syntax-table) |
| 760 | ;; For historical reasons, underscore is word class instead of |
| 761 | ;; symbol class. GNU conventions say it should be symbol class, but |
| 762 | ;; there's a natural conflict between what major mode authors want |
| 763 | ;; and what users expect from `forward-word' and `backward-word'. |
| 764 | ;; Guido and I have hashed this out and have decided to keep |
| 765 | ;; underscore in word class. If you're tempted to change it, try |
| 766 | ;; binding M-f and M-b to py-forward-into-nomenclature and |
Barry Warsaw | aa384fd | 1999-02-16 23:36:16 +0000 | [diff] [blame] | 767 | ;; py-backward-into-nomenclature instead. This doesn't help in all |
| 768 | ;; situations where you'd want the different behavior |
| 769 | ;; (e.g. backward-kill-word). |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 770 | (modify-syntax-entry ?\_ "w" py-mode-syntax-table) |
| 771 | ;; Both single quote and double quote are string delimiters |
| 772 | (modify-syntax-entry ?\' "\"" py-mode-syntax-table) |
| 773 | (modify-syntax-entry ?\" "\"" py-mode-syntax-table) |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 774 | ;; comment delimiters |
| 775 | (modify-syntax-entry ?\# "<" py-mode-syntax-table) |
| 776 | (modify-syntax-entry ?\n ">" py-mode-syntax-table) |
| 777 | ) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 778 | |
Barry Warsaw | a7cc43b | 2002-04-22 17:15:19 +0000 | [diff] [blame] | 779 | ;; An auxiliary syntax table which places underscore and dot in the |
| 780 | ;; symbol class for simplicity |
| 781 | (defvar py-dotted-expression-syntax-table nil |
| 782 | "Syntax table used to identify Python dotted expressions.") |
| 783 | (when (not py-dotted-expression-syntax-table) |
| 784 | (setq py-dotted-expression-syntax-table |
| 785 | (copy-syntax-table py-mode-syntax-table)) |
| 786 | (modify-syntax-entry ?_ "_" py-dotted-expression-syntax-table) |
| 787 | (modify-syntax-entry ?. "_" py-dotted-expression-syntax-table)) |
| 788 | |
Barry Warsaw | b3e81d5 | 1996-09-04 15:12:42 +0000 | [diff] [blame] | 789 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 790 | |
Barry Warsaw | bc3760b | 1998-09-14 16:16:18 +0000 | [diff] [blame] | 791 | ;; Utilities |
Barry Warsaw | bc3760b | 1998-09-14 16:16:18 +0000 | [diff] [blame] | 792 | (defmacro py-safe (&rest body) |
| 793 | "Safely execute BODY, return nil if an error occurred." |
| 794 | (` (condition-case nil |
| 795 | (progn (,@ body)) |
| 796 | (error nil)))) |
| 797 | |
| 798 | (defsubst py-keep-region-active () |
| 799 | "Keep the region active in XEmacs." |
| 800 | ;; Ignore byte-compiler warnings you might see. Also note that |
| 801 | ;; FSF's Emacs 19 does it differently; its policy doesn't require us |
| 802 | ;; to take explicit action. |
| 803 | (and (boundp 'zmacs-region-stays) |
| 804 | (setq zmacs-region-stays t))) |
| 805 | |
| 806 | (defsubst py-point (position) |
| 807 | "Returns the value of point at certain commonly referenced POSITIONs. |
| 808 | POSITION can be one of the following symbols: |
| 809 | |
| 810 | bol -- beginning of line |
| 811 | eol -- end of line |
| 812 | bod -- beginning of def or class |
| 813 | eod -- end of def or class |
| 814 | bob -- beginning of buffer |
| 815 | eob -- end of buffer |
| 816 | boi -- back to indentation |
| 817 | bos -- beginning of statement |
| 818 | |
| 819 | This function does not modify point or mark." |
| 820 | (let ((here (point))) |
| 821 | (cond |
| 822 | ((eq position 'bol) (beginning-of-line)) |
| 823 | ((eq position 'eol) (end-of-line)) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 824 | ((eq position 'bod) (py-beginning-of-def-or-class 'either)) |
| 825 | ((eq position 'eod) (py-end-of-def-or-class 'either)) |
Barry Warsaw | bc3760b | 1998-09-14 16:16:18 +0000 | [diff] [blame] | 826 | ;; Kind of funny, I know, but useful for py-up-exception. |
| 827 | ((eq position 'bob) (beginning-of-buffer)) |
| 828 | ((eq position 'eob) (end-of-buffer)) |
| 829 | ((eq position 'boi) (back-to-indentation)) |
| 830 | ((eq position 'bos) (py-goto-initial-line)) |
| 831 | (t (error "Unknown buffer position requested: %s" position)) |
| 832 | ) |
| 833 | (prog1 |
| 834 | (point) |
| 835 | (goto-char here)))) |
| 836 | |
| 837 | (defsubst py-highlight-line (from to file line) |
| 838 | (cond |
| 839 | ((fboundp 'make-extent) |
| 840 | ;; XEmacs |
| 841 | (let ((e (make-extent from to))) |
| 842 | (set-extent-property e 'mouse-face 'highlight) |
| 843 | (set-extent-property e 'py-exc-info (cons file line)) |
| 844 | (set-extent-property e 'keymap py-mode-output-map))) |
| 845 | (t |
| 846 | ;; Emacs -- Please port this! |
| 847 | ) |
| 848 | )) |
| 849 | |
| 850 | (defun py-in-literal (&optional lim) |
| 851 | "Return non-nil if point is in a Python literal (a comment or string). |
| 852 | Optional argument LIM indicates the beginning of the containing form, |
| 853 | i.e. the limit on how far back to scan." |
| 854 | ;; This is the version used for non-XEmacs, which has a nicer |
| 855 | ;; interface. |
| 856 | ;; |
| 857 | ;; WARNING: Watch out for infinite recursion. |
| 858 | (let* ((lim (or lim (py-point 'bod))) |
| 859 | (state (parse-partial-sexp lim (point)))) |
| 860 | (cond |
| 861 | ((nth 3 state) 'string) |
| 862 | ((nth 4 state) 'comment) |
| 863 | (t nil)))) |
| 864 | |
| 865 | ;; XEmacs has a built-in function that should make this much quicker. |
| 866 | ;; In this case, lim is ignored |
| 867 | (defun py-fast-in-literal (&optional lim) |
| 868 | "Fast version of `py-in-literal', used only by XEmacs. |
| 869 | Optional LIM is ignored." |
| 870 | ;; don't have to worry about context == 'block-comment |
| 871 | (buffer-syntactic-context)) |
| 872 | |
| 873 | (if (fboundp 'buffer-syntactic-context) |
| 874 | (defalias 'py-in-literal 'py-fast-in-literal)) |
| 875 | |
| 876 | |
| 877 | |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 878 | ;; Menu definitions, only relevent if you have the easymenu.el package |
| 879 | ;; (standard in the latest Emacs 19 and XEmacs 19 distributions). |
Barry Warsaw | 5490a06 | 1996-08-06 15:43:33 +0000 | [diff] [blame] | 880 | (defvar py-menu nil |
| 881 | "Menu for Python Mode. |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 882 | This menu will get created automatically if you have the `easymenu' |
| 883 | package. Note that the latest X/Emacs releases contain this package.") |
Barry Warsaw | 5490a06 | 1996-08-06 15:43:33 +0000 | [diff] [blame] | 884 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 885 | (and (py-safe (require 'easymenu) t) |
| 886 | (easy-menu-define |
| 887 | py-menu py-mode-map "Python Mode menu" |
| 888 | '("Python" |
| 889 | ["Comment Out Region" py-comment-region (mark)] |
| 890 | ["Uncomment Region" (py-comment-region (point) (mark) '(4)) (mark)] |
| 891 | "-" |
| 892 | ["Mark current block" py-mark-block t] |
Barry Warsaw | 2518c67 | 1997-11-05 00:51:08 +0000 | [diff] [blame] | 893 | ["Mark current def" py-mark-def-or-class t] |
| 894 | ["Mark current class" (py-mark-def-or-class t) t] |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 895 | "-" |
| 896 | ["Shift region left" py-shift-region-left (mark)] |
| 897 | ["Shift region right" py-shift-region-right (mark)] |
| 898 | "-" |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 899 | ["Import/reload file" py-execute-import-or-reload t] |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 900 | ["Execute buffer" py-execute-buffer t] |
| 901 | ["Execute region" py-execute-region (mark)] |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 902 | ["Execute def or class" py-execute-def-or-class (mark)] |
Barry Warsaw | 1d0364b | 1998-05-19 16:15:26 +0000 | [diff] [blame] | 903 | ["Execute string" py-execute-string t] |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 904 | ["Start interpreter..." py-shell t] |
| 905 | "-" |
| 906 | ["Go to start of block" py-goto-block-up t] |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 907 | ["Go to start of class" (py-beginning-of-def-or-class t) t] |
| 908 | ["Move to end of class" (py-end-of-def-or-class t) t] |
| 909 | ["Move to start of def" py-beginning-of-def-or-class t] |
| 910 | ["Move to end of def" py-end-of-def-or-class t] |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 911 | "-" |
| 912 | ["Describe mode" py-describe-mode t] |
| 913 | ))) |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 914 | |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 915 | |
| 916 | |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 917 | ;; Imenu definitions |
| 918 | (defvar py-imenu-class-regexp |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 919 | (concat ; <<classes>> |
| 920 | "\\(" ; |
| 921 | "^[ \t]*" ; newline and maybe whitespace |
| 922 | "\\(class[ \t]+[a-zA-Z0-9_]+\\)" ; class name |
| 923 | ; possibly multiple superclasses |
Barry Warsaw | 3179fe0 | 1998-04-04 21:36:53 +0000 | [diff] [blame] | 924 | "\\([ \t]*\\((\\([a-zA-Z0-9_,. \t\n]\\)*)\\)?\\)" |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 925 | "[ \t]*:" ; and the final : |
| 926 | "\\)" ; >>classes<< |
| 927 | ) |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 928 | "Regexp for Python classes for use with the Imenu package." |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 929 | ) |
| 930 | |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 931 | (defvar py-imenu-method-regexp |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 932 | (concat ; <<methods and functions>> |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 933 | "\\(" ; |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 934 | "^[ \t]*" ; new line and maybe whitespace |
| 935 | "\\(def[ \t]+" ; function definitions start with def |
| 936 | "\\([a-zA-Z0-9_]+\\)" ; name is here |
| 937 | ; function arguments... |
Barry Warsaw | 1d5f988 | 1998-10-28 04:08:13 +0000 | [diff] [blame] | 938 | ;; "[ \t]*(\\([-+/a-zA-Z0-9_=,\* \t\n.()\"'#]*\\))" |
| 939 | "[ \t]*(\\([^:#]*\\))" |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 940 | "\\)" ; end of def |
| 941 | "[ \t]*:" ; and then the : |
| 942 | "\\)" ; >>methods and functions<< |
| 943 | ) |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 944 | "Regexp for Python methods/functions for use with the Imenu package." |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 945 | ) |
| 946 | |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 947 | (defvar py-imenu-method-no-arg-parens '(2 8) |
| 948 | "Indices into groups of the Python regexp for use with Imenu. |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 949 | |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 950 | Using these values will result in smaller Imenu lists, as arguments to |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 951 | functions are not listed. |
| 952 | |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 953 | See the variable `py-imenu-show-method-args-p' for more |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 954 | information.") |
| 955 | |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 956 | (defvar py-imenu-method-arg-parens '(2 7) |
Barry Warsaw | 1bbc031 | 1998-10-27 21:54:56 +0000 | [diff] [blame] | 957 | "Indices into groups of the Python regexp for use with imenu. |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 958 | Using these values will result in large Imenu lists, as arguments to |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 959 | functions are listed. |
| 960 | |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 961 | See the variable `py-imenu-show-method-args-p' for more |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 962 | information.") |
| 963 | |
| 964 | ;; Note that in this format, this variable can still be used with the |
| 965 | ;; imenu--generic-function. Otherwise, there is no real reason to have |
| 966 | ;; it. |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 967 | (defvar py-imenu-generic-expression |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 968 | (cons |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 969 | (concat |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 970 | py-imenu-class-regexp |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 971 | "\\|" ; or... |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 972 | py-imenu-method-regexp |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 973 | ) |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 974 | py-imenu-method-no-arg-parens) |
| 975 | "Generic Python expression which may be used directly with Imenu. |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 976 | Used by setting the variable `imenu-generic-expression' to this value. |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 977 | Also, see the function \\[py-imenu-create-index] for a better |
| 978 | alternative for finding the index.") |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 979 | |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 980 | ;; These next two variables are used when searching for the Python |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 981 | ;; class/definitions. Just saving some time in accessing the |
| 982 | ;; generic-python-expression, really. |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 983 | (defvar py-imenu-generic-regexp nil) |
| 984 | (defvar py-imenu-generic-parens nil) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 985 | |
| 986 | |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 987 | (defun py-imenu-create-index-function () |
| 988 | "Python interface function for the Imenu package. |
| 989 | Finds all Python classes and functions/methods. Calls function |
| 990 | \\[py-imenu-create-index-engine]. See that function for the details |
| 991 | of how this works." |
| 992 | (setq py-imenu-generic-regexp (car py-imenu-generic-expression) |
| 993 | py-imenu-generic-parens (if py-imenu-show-method-args-p |
| 994 | py-imenu-method-arg-parens |
| 995 | py-imenu-method-no-arg-parens)) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 996 | (goto-char (point-min)) |
Barry Warsaw | 1d5f988 | 1998-10-28 04:08:13 +0000 | [diff] [blame] | 997 | ;; Warning: When the buffer has no classes or functions, this will |
| 998 | ;; return nil, which seems proper according to the Imenu API, but |
| 999 | ;; causes an error in the XEmacs port of Imenu. Sigh. |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1000 | (py-imenu-create-index-engine nil)) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1001 | |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1002 | (defun py-imenu-create-index-engine (&optional start-indent) |
| 1003 | "Function for finding Imenu definitions in Python. |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1004 | |
| 1005 | Finds all definitions (classes, methods, or functions) in a Python |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1006 | file for the Imenu package. |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1007 | |
| 1008 | Returns a possibly nested alist of the form |
| 1009 | |
| 1010 | (INDEX-NAME . INDEX-POSITION) |
| 1011 | |
| 1012 | The second element of the alist may be an alist, producing a nested |
| 1013 | list as in |
| 1014 | |
| 1015 | (INDEX-NAME . INDEX-ALIST) |
| 1016 | |
| 1017 | This function should not be called directly, as it calls itself |
| 1018 | recursively and requires some setup. Rather this is the engine for |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1019 | the function \\[py-imenu-create-index-function]. |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1020 | |
| 1021 | It works recursively by looking for all definitions at the current |
| 1022 | indention level. When it finds one, it adds it to the alist. If it |
| 1023 | finds a definition at a greater indentation level, it removes the |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 1024 | previous definition from the alist. In its place it adds all |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1025 | definitions found at the next indentation level. When it finds a |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1026 | definition that is less indented then the current level, it returns |
| 1027 | the alist it has created thus far. |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1028 | |
| 1029 | The optional argument START-INDENT indicates the starting indentation |
| 1030 | at which to continue looking for Python classes, methods, or |
| 1031 | functions. If this is not supplied, the function uses the indentation |
| 1032 | of the first definition found." |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1033 | (let (index-alist |
| 1034 | sub-method-alist |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1035 | looking-p |
| 1036 | def-name prev-name |
| 1037 | cur-indent def-pos |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1038 | (class-paren (first py-imenu-generic-parens)) |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1039 | (def-paren (second py-imenu-generic-parens))) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1040 | (setq looking-p |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1041 | (re-search-forward py-imenu-generic-regexp (point-max) t)) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1042 | (while looking-p |
| 1043 | (save-excursion |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1044 | ;; used to set def-name to this value but generic-extract-name |
| 1045 | ;; is new to imenu-1.14. this way it still works with |
| 1046 | ;; imenu-1.11 |
| 1047 | ;;(imenu--generic-extract-name py-imenu-generic-parens)) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1048 | (let ((cur-paren (if (match-beginning class-paren) |
| 1049 | class-paren def-paren))) |
| 1050 | (setq def-name |
Barry Warsaw | c852035 | 1997-11-26 06:14:40 +0000 | [diff] [blame] | 1051 | (buffer-substring-no-properties (match-beginning cur-paren) |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1052 | (match-end cur-paren)))) |
Barry Warsaw | 93c88cc | 1998-08-18 02:00:44 +0000 | [diff] [blame] | 1053 | (save-match-data |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1054 | (py-beginning-of-def-or-class 'either)) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1055 | (beginning-of-line) |
| 1056 | (setq cur-indent (current-indentation))) |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1057 | ;; HACK: want to go to the next correct definition location. We |
| 1058 | ;; explicitly list them here but it would be better to have them |
| 1059 | ;; in a list. |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1060 | (setq def-pos |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1061 | (or (match-beginning class-paren) |
| 1062 | (match-beginning def-paren))) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1063 | ;; if we don't have a starting indent level, take this one |
| 1064 | (or start-indent |
| 1065 | (setq start-indent cur-indent)) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1066 | ;; if we don't have class name yet, take this one |
| 1067 | (or prev-name |
| 1068 | (setq prev-name def-name)) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1069 | ;; what level is the next definition on? must be same, deeper |
| 1070 | ;; or shallower indentation |
| 1071 | (cond |
Barry Warsaw | 32a0396 | 2002-07-16 16:04:13 +0000 | [diff] [blame] | 1072 | ;; Skip code in comments and strings |
| 1073 | ((py-in-literal)) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1074 | ;; at the same indent level, add it to the list... |
| 1075 | ((= start-indent cur-indent) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1076 | (push (cons def-name def-pos) index-alist)) |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1077 | ;; deeper indented expression, recurse |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1078 | ((< start-indent cur-indent) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1079 | ;; the point is currently on the expression we're supposed to |
| 1080 | ;; start on, so go back to the last expression. The recursive |
| 1081 | ;; call will find this place again and add it to the correct |
| 1082 | ;; list |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1083 | (re-search-backward py-imenu-generic-regexp (point-min) 'move) |
| 1084 | (setq sub-method-alist (py-imenu-create-index-engine cur-indent)) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1085 | (if sub-method-alist |
| 1086 | ;; we put the last element on the index-alist on the start |
| 1087 | ;; of the submethod alist so the user can still get to it. |
| 1088 | (let ((save-elmt (pop index-alist))) |
Barry Warsaw | c852035 | 1997-11-26 06:14:40 +0000 | [diff] [blame] | 1089 | (push (cons prev-name |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1090 | (cons save-elmt sub-method-alist)) |
| 1091 | index-alist)))) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1092 | ;; found less indented expression, we're done. |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1093 | (t |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1094 | (setq looking-p nil) |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1095 | (re-search-backward py-imenu-generic-regexp (point-min) t))) |
| 1096 | ;; end-cond |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1097 | (setq prev-name def-name) |
| 1098 | (and looking-p |
| 1099 | (setq looking-p |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1100 | (re-search-forward py-imenu-generic-regexp |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 1101 | (point-max) 'move)))) |
| 1102 | (nreverse index-alist))) |
| 1103 | |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1104 | |
| 1105 | |
| 1106 | (defun py-choose-shell-by-shebang () |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1107 | "Choose CPython or Jython mode by looking at #! on the first line. |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1108 | Returns the appropriate mode function. |
| 1109 | Used by `py-choose-shell', and similar to but distinct from |
| 1110 | `set-auto-mode', though it uses `auto-mode-interpreter-regexp' (if available)." |
| 1111 | ;; look for an interpreter specified in the first line |
| 1112 | ;; similar to set-auto-mode (files.el) |
| 1113 | (let* ((re (if (boundp 'auto-mode-interpreter-regexp) |
| 1114 | auto-mode-interpreter-regexp |
| 1115 | ;; stolen from Emacs 21.2 |
| 1116 | "#![ \t]?\\([^ \t\n]*/bin/env[ \t]\\)?\\([^ \t\n]+\\)")) |
| 1117 | (interpreter (save-excursion |
| 1118 | (goto-char (point-min)) |
| 1119 | (if (looking-at re) |
| 1120 | (match-string 2) |
| 1121 | ""))) |
| 1122 | elt) |
| 1123 | ;; Map interpreter name to a mode. |
| 1124 | (setq elt (assoc (file-name-nondirectory interpreter) |
| 1125 | py-shell-alist)) |
| 1126 | (and elt (caddr elt)))) |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | (defun py-choose-shell-by-import () |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1131 | "Choose CPython or Jython mode based imports. |
| 1132 | If a file imports any packages in `py-jython-packages', within |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1133 | `py-import-check-point-max' characters from the start of the file, |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1134 | return `jython', otherwise return nil." |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1135 | (let (mode) |
| 1136 | (save-excursion |
| 1137 | (goto-char (point-min)) |
| 1138 | (while (and (not mode) |
| 1139 | (search-forward-regexp |
| 1140 | "^\\(\\(from\\)\\|\\(import\\)\\) \\([^ \t\n.]+\\)" |
| 1141 | py-import-check-point-max t)) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1142 | (setq mode (and (member (match-string 4) py-jython-packages) |
| 1143 | 'jython |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1144 | )))) |
| 1145 | mode)) |
| 1146 | |
| 1147 | |
| 1148 | (defun py-choose-shell () |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1149 | "Choose CPython or Jython mode. Returns the appropriate mode function. |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1150 | This does the following: |
| 1151 | - look for an interpreter with `py-choose-shell-by-shebang' |
| 1152 | - examine imports using `py-choose-shell-by-import' |
| 1153 | - default to the variable `py-default-interpreter'" |
| 1154 | (interactive) |
| 1155 | (or (py-choose-shell-by-shebang) |
| 1156 | (py-choose-shell-by-import) |
| 1157 | py-default-interpreter |
| 1158 | ; 'cpython ;; don't use to py-default-interpreter, because default |
| 1159 | ; ;; is only way to choose CPython |
| 1160 | )) |
| 1161 | |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 1162 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1163 | ;;;###autoload |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1164 | (defun python-mode () |
| 1165 | "Major mode for editing Python files. |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1166 | To submit a problem report, enter `\\[py-submit-bug-report]' from a |
| 1167 | `python-mode' buffer. Do `\\[py-describe-mode]' for detailed |
| 1168 | documentation. To see what version of `python-mode' you are running, |
| 1169 | enter `\\[py-version]'. |
| 1170 | |
| 1171 | This mode knows about Python indentation, tokens, comments and |
| 1172 | continuation lines. Paragraphs are separated by blank lines only. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1173 | |
| 1174 | COMMANDS |
| 1175 | \\{py-mode-map} |
| 1176 | VARIABLES |
| 1177 | |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 1178 | py-indent-offset\t\tindentation increment |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1179 | py-block-comment-prefix\t\tcomment string used by `comment-region' |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 1180 | py-python-command\t\tshell command to invoke Python interpreter |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 1181 | py-temp-directory\t\tdirectory used for temp files (if needed) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1182 | py-beep-if-tab-change\t\tring the bell if `tab-width' is changed" |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1183 | (interactive) |
Barry Warsaw | 615d4a4 | 1996-09-04 14:14:10 +0000 | [diff] [blame] | 1184 | ;; set up local variables |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1185 | (kill-all-local-variables) |
Barry Warsaw | 615d4a4 | 1996-09-04 14:14:10 +0000 | [diff] [blame] | 1186 | (make-local-variable 'font-lock-defaults) |
| 1187 | (make-local-variable 'paragraph-separate) |
| 1188 | (make-local-variable 'paragraph-start) |
| 1189 | (make-local-variable 'require-final-newline) |
| 1190 | (make-local-variable 'comment-start) |
Barry Warsaw | 5c8bef1 | 1996-12-17 21:56:10 +0000 | [diff] [blame] | 1191 | (make-local-variable 'comment-end) |
Barry Warsaw | 615d4a4 | 1996-09-04 14:14:10 +0000 | [diff] [blame] | 1192 | (make-local-variable 'comment-start-skip) |
| 1193 | (make-local-variable 'comment-column) |
Barry Warsaw | 003932a | 1998-07-07 15:11:24 +0000 | [diff] [blame] | 1194 | (make-local-variable 'comment-indent-function) |
Barry Warsaw | 615d4a4 | 1996-09-04 14:14:10 +0000 | [diff] [blame] | 1195 | (make-local-variable 'indent-region-function) |
| 1196 | (make-local-variable 'indent-line-function) |
Barry Warsaw | b3e81d5 | 1996-09-04 15:12:42 +0000 | [diff] [blame] | 1197 | (make-local-variable 'add-log-current-defun-function) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1198 | (make-local-variable 'fill-paragraph-function) |
Barry Warsaw | 615d4a4 | 1996-09-04 14:14:10 +0000 | [diff] [blame] | 1199 | ;; |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1200 | (set-syntax-table py-mode-syntax-table) |
Barry Warsaw | 003932a | 1998-07-07 15:11:24 +0000 | [diff] [blame] | 1201 | (setq major-mode 'python-mode |
| 1202 | mode-name "Python" |
| 1203 | local-abbrev-table python-mode-abbrev-table |
| 1204 | font-lock-defaults '(python-font-lock-keywords) |
| 1205 | paragraph-separate "^[ \t]*$" |
| 1206 | paragraph-start "^[ \t]*$" |
| 1207 | require-final-newline t |
| 1208 | comment-start "# " |
| 1209 | comment-end "" |
| 1210 | comment-start-skip "# *" |
| 1211 | comment-column 40 |
| 1212 | comment-indent-function 'py-comment-indent-function |
| 1213 | indent-region-function 'py-indent-region |
| 1214 | indent-line-function 'py-indent-line |
Barry Warsaw | b3e81d5 | 1996-09-04 15:12:42 +0000 | [diff] [blame] | 1215 | ;; tell add-log.el how to find the current function/method/variable |
| 1216 | add-log-current-defun-function 'py-current-defun |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1217 | |
| 1218 | fill-paragraph-function 'py-fill-paragraph |
Barry Warsaw | b1f8951 | 1996-09-03 16:38:30 +0000 | [diff] [blame] | 1219 | ) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1220 | (use-local-map py-mode-map) |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 1221 | ;; add the menu |
| 1222 | (if py-menu |
| 1223 | (easy-menu-add py-menu)) |
Barry Warsaw | 57697af | 1995-09-14 20:01:14 +0000 | [diff] [blame] | 1224 | ;; Emacs 19 requires this |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 1225 | (if (boundp 'comment-multi-line) |
Barry Warsaw | 57697af | 1995-09-14 20:01:14 +0000 | [diff] [blame] | 1226 | (setq comment-multi-line nil)) |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1227 | ;; Install Imenu if available |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 1228 | (when (py-safe (require 'imenu)) |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 1229 | (setq imenu-create-index-function #'py-imenu-create-index-function) |
| 1230 | (setq imenu-generic-expression py-imenu-generic-expression) |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 1231 | (if (fboundp 'imenu-add-to-menubar) |
| 1232 | (imenu-add-to-menubar (format "%s-%s" "IM" mode-name))) |
| 1233 | ) |
| 1234 | ;; Run the mode hook. Note that py-mode-hook is deprecated. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1235 | (if python-mode-hook |
| 1236 | (run-hooks 'python-mode-hook) |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 1237 | (run-hooks 'py-mode-hook)) |
| 1238 | ;; Now do the automagical guessing |
| 1239 | (if py-smart-indentation |
| 1240 | (let ((offset py-indent-offset)) |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 1241 | ;; It's okay if this fails to guess a good value |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 1242 | (if (and (py-safe (py-guess-indent-offset)) |
| 1243 | (<= py-indent-offset 8) |
| 1244 | (>= py-indent-offset 2)) |
| 1245 | (setq offset py-indent-offset)) |
| 1246 | (setq py-indent-offset offset) |
Barry Warsaw | 639eea6 | 1998-03-16 18:12:13 +0000 | [diff] [blame] | 1247 | ;; Only turn indent-tabs-mode off if tab-width != |
| 1248 | ;; py-indent-offset. Never turn it on, because the user must |
| 1249 | ;; have explicitly turned it off. |
| 1250 | (if (/= tab-width py-indent-offset) |
| 1251 | (setq indent-tabs-mode nil)) |
Barry Warsaw | 11f2156 | 1999-07-28 21:59:43 +0000 | [diff] [blame] | 1252 | )) |
| 1253 | ;; Set the default shell if not already set |
| 1254 | (when (null py-which-shell) |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1255 | (py-toggle-shells (py-choose-shell)))) |
| 1256 | |
| 1257 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1258 | (make-obsolete 'jpython-mode 'jython-mode) |
| 1259 | (defun jython-mode () |
| 1260 | "Major mode for editing Jython/Jython files. |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1261 | This is a simple wrapper around `python-mode'. |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1262 | It runs `jython-mode-hook' then calls `python-mode.' |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1263 | It is added to `interpreter-mode-alist' and `py-choose-shell'. |
| 1264 | " |
| 1265 | (interactive) |
| 1266 | (python-mode) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1267 | (py-toggle-shells 'jython) |
| 1268 | (when jython-mode-hook |
| 1269 | (run-hooks 'jython-mode-hook))) |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1270 | |
| 1271 | |
| 1272 | ;; It's handy to add recognition of Python files to the |
| 1273 | ;; interpreter-mode-alist and to auto-mode-alist. With the former, we |
| 1274 | ;; can specify different `derived-modes' based on the #! line, but |
| 1275 | ;; with the latter, we can't. So we just won't add them if they're |
| 1276 | ;; already added. |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1277 | ;;;###autoload |
| 1278 | (let ((modes '(("jython" . jython-mode) |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1279 | ("python" . python-mode)))) |
| 1280 | (while modes |
| 1281 | (when (not (assoc (car modes) interpreter-mode-alist)) |
Barry Warsaw | cf22c82 | 2002-04-25 21:46:33 +0000 | [diff] [blame] | 1282 | (push (car modes) interpreter-mode-alist)) |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1283 | (setq modes (cdr modes)))) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1284 | ;;;###autoload |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1285 | (when (not (or (rassq 'python-mode auto-mode-alist) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1286 | (rassq 'jython-mode auto-mode-alist))) |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1287 | (push '("\\.py$" . python-mode) auto-mode-alist)) |
| 1288 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1289 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1290 | |
Barry Warsaw | b91b743 | 1995-03-14 15:55:20 +0000 | [diff] [blame] | 1291 | ;; electric characters |
Barry Warsaw | 3874a3d | 1995-03-14 22:05:53 +0000 | [diff] [blame] | 1292 | (defun py-outdent-p () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1293 | "Returns non-nil if the current line should dedent one level." |
Barry Warsaw | 3874a3d | 1995-03-14 22:05:53 +0000 | [diff] [blame] | 1294 | (save-excursion |
| 1295 | (and (progn (back-to-indentation) |
| 1296 | (looking-at py-outdent-re)) |
Barry Warsaw | 1a1c6bb | 1999-01-09 17:22:38 +0000 | [diff] [blame] | 1297 | ;; short circuit infloop on illegal construct |
| 1298 | (not (bobp)) |
Barry Warsaw | f06777d | 1998-01-21 05:36:18 +0000 | [diff] [blame] | 1299 | (progn (forward-line -1) |
| 1300 | (py-goto-initial-line) |
| 1301 | (back-to-indentation) |
Barry Warsaw | 3874a3d | 1995-03-14 22:05:53 +0000 | [diff] [blame] | 1302 | (while (or (looking-at py-blank-or-comment-re) |
| 1303 | (bobp)) |
| 1304 | (backward-to-indentation 1)) |
| 1305 | (not (looking-at py-no-outdent-re))) |
| 1306 | ))) |
Skip Montanaro | 98a4fbe | 2003-08-01 04:00:32 +0000 | [diff] [blame] | 1307 | |
Barry Warsaw | b91b743 | 1995-03-14 15:55:20 +0000 | [diff] [blame] | 1308 | (defun py-electric-colon (arg) |
| 1309 | "Insert a colon. |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1310 | In certain cases the line is dedented appropriately. If a numeric |
| 1311 | argument ARG is provided, that many colons are inserted |
| 1312 | non-electrically. Electric behavior is inhibited inside a string or |
| 1313 | comment." |
Skip Montanaro | 98a4fbe | 2003-08-01 04:00:32 +0000 | [diff] [blame] | 1314 | (interactive "*P") |
Barry Warsaw | b91b743 | 1995-03-14 15:55:20 +0000 | [diff] [blame] | 1315 | (self-insert-command (prefix-numeric-value arg)) |
Barry Warsaw | 0c6563f | 1995-09-14 20:57:02 +0000 | [diff] [blame] | 1316 | ;; are we in a string or comment? |
| 1317 | (if (save-excursion |
| 1318 | (let ((pps (parse-partial-sexp (save-excursion |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 1319 | (py-beginning-of-def-or-class) |
Barry Warsaw | 0c6563f | 1995-09-14 20:57:02 +0000 | [diff] [blame] | 1320 | (point)) |
| 1321 | (point)))) |
| 1322 | (not (or (nth 3 pps) (nth 4 pps))))) |
| 1323 | (save-excursion |
| 1324 | (let ((here (point)) |
| 1325 | (outdent 0) |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 1326 | (indent (py-compute-indentation t))) |
Barry Warsaw | 0c6563f | 1995-09-14 20:57:02 +0000 | [diff] [blame] | 1327 | (if (and (not arg) |
| 1328 | (py-outdent-p) |
| 1329 | (= indent (save-excursion |
Barry Warsaw | a766182 | 1996-08-02 16:22:43 +0000 | [diff] [blame] | 1330 | (py-next-statement -1) |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 1331 | (py-compute-indentation t))) |
Barry Warsaw | 0c6563f | 1995-09-14 20:57:02 +0000 | [diff] [blame] | 1332 | ) |
| 1333 | (setq outdent py-indent-offset)) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1334 | ;; Don't indent, only dedent. This assumes that any lines |
| 1335 | ;; that are already dedented relative to |
| 1336 | ;; py-compute-indentation were put there on purpose. It's |
| 1337 | ;; highly annoying to have `:' indent for you. Use TAB, C-c |
| 1338 | ;; C-l or C-c C-r to adjust. TBD: Is there a better way to |
| 1339 | ;; determine this??? |
Barry Warsaw | 0c6563f | 1995-09-14 20:57:02 +0000 | [diff] [blame] | 1340 | (if (< (current-indentation) indent) nil |
| 1341 | (goto-char here) |
| 1342 | (beginning-of-line) |
| 1343 | (delete-horizontal-space) |
| 1344 | (indent-to (- indent outdent)) |
| 1345 | ))))) |
Barry Warsaw | b91b743 | 1995-03-14 15:55:20 +0000 | [diff] [blame] | 1346 | |
| 1347 | |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1348 | ;; Python subprocess utilities and filters |
| 1349 | (defun py-execute-file (proc filename) |
Neal Norwitz | 0168802 | 2007-08-12 00:43:29 +0000 | [diff] [blame] | 1350 | "Send to Python interpreter process PROC \"exec(open('FILENAME').read())\". |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1351 | Make that process's buffer visible and force display. Also make |
| 1352 | comint believe the user typed this string so that |
| 1353 | `kill-output-from-shell' does The Right Thing." |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1354 | (let ((curbuf (current-buffer)) |
| 1355 | (procbuf (process-buffer proc)) |
Barry Warsaw | 3c96f6f | 1998-08-20 19:44:51 +0000 | [diff] [blame] | 1356 | ; (comint-scroll-to-bottom-on-output t) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1357 | (msg (format "## working on region in file %s...\n" filename)) |
Neal Norwitz | 0168802 | 2007-08-12 00:43:29 +0000 | [diff] [blame] | 1358 | (cmd (format "exec(open(r'%s').read())\n" filename))) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1359 | (unwind-protect |
Barry Warsaw | 3c96f6f | 1998-08-20 19:44:51 +0000 | [diff] [blame] | 1360 | (save-excursion |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1361 | (set-buffer procbuf) |
| 1362 | (goto-char (point-max)) |
| 1363 | (move-marker (process-mark proc) (point)) |
| 1364 | (funcall (process-filter proc) proc msg)) |
| 1365 | (set-buffer curbuf)) |
| 1366 | (process-send-string proc cmd))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1367 | |
Barry Warsaw | 3c96f6f | 1998-08-20 19:44:51 +0000 | [diff] [blame] | 1368 | (defun py-comint-output-filter-function (string) |
| 1369 | "Watch output for Python prompt and exec next file waiting in queue. |
| 1370 | This function is appropriate for `comint-output-filter-functions'." |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1371 | ;;remove ansi terminal escape sequences from string, not sure why they are |
| 1372 | ;;still around... |
| 1373 | (setq string (ansi-color-filter-apply string)) |
| 1374 | (when (and (string-match py-shell-input-prompt-1-regexp string) |
| 1375 | py-file-queue) |
| 1376 | (if py-shell-switch-buffers-on-execute |
| 1377 | (pop-to-buffer (current-buffer))) |
Barry Warsaw | 3c96f6f | 1998-08-20 19:44:51 +0000 | [diff] [blame] | 1378 | (py-safe (delete-file (car py-file-queue))) |
| 1379 | (setq py-file-queue (cdr py-file-queue)) |
| 1380 | (if py-file-queue |
| 1381 | (let ((pyproc (get-buffer-process (current-buffer)))) |
| 1382 | (py-execute-file pyproc (car py-file-queue)))) |
| 1383 | )) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1384 | |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 1385 | (defun py-pdbtrack-overlay-arrow (activation) |
| 1386 | "Activate or de arrow at beginning-of-line in current buffer." |
| 1387 | ;; This was derived/simplified from edebug-overlay-arrow |
| 1388 | (cond (activation |
| 1389 | (setq overlay-arrow-position (make-marker)) |
| 1390 | (setq overlay-arrow-string "=>") |
| 1391 | (set-marker overlay-arrow-position (py-point 'bol) (current-buffer)) |
| 1392 | (setq py-pdbtrack-is-tracking-p t)) |
| 1393 | (overlay-arrow-position |
| 1394 | (setq overlay-arrow-position nil) |
| 1395 | (setq py-pdbtrack-is-tracking-p nil)) |
| 1396 | )) |
| 1397 | |
| 1398 | (defun py-pdbtrack-track-stack-file (text) |
| 1399 | "Show the file indicated by the pdb stack entry line, in a separate window. |
| 1400 | |
| 1401 | Activity is disabled if the buffer-local variable |
| 1402 | `py-pdbtrack-do-tracking-p' is nil. |
| 1403 | |
| 1404 | We depend on the pdb input prompt matching `py-pdbtrack-input-prompt' |
Ken Manheimer | e7aca52 | 2003-03-03 00:35:32 +0000 | [diff] [blame] | 1405 | at the beginning of the line. |
| 1406 | |
| 1407 | If the traceback target file path is invalid, we look for the most |
| 1408 | recently visited python-mode buffer which either has the name of the |
| 1409 | current function \(or class) or which defines the function \(or |
| 1410 | class). This is to provide for remote scripts, eg, Zope's 'Script |
Ken Manheimer | 595adce | 2003-05-01 20:46:14 +0000 | [diff] [blame] | 1411 | (Python)' - put a _copy_ of the script in a buffer named for the |
| 1412 | script, and set to python-mode, and pdbtrack will find it.)" |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 1413 | ;; Instead of trying to piece things together from partial text |
| 1414 | ;; (which can be almost useless depending on Emacs version), we |
| 1415 | ;; monitor to the point where we have the next pdb prompt, and then |
| 1416 | ;; check all text from comint-last-input-end to process-mark. |
| 1417 | ;; |
Ken Manheimer | e7aca52 | 2003-03-03 00:35:32 +0000 | [diff] [blame] | 1418 | ;; Also, we're very conservative about clearing the overlay arrow, |
| 1419 | ;; to minimize residue. This means, for instance, that executing |
| 1420 | ;; other pdb commands wipe out the highlight. You can always do a |
| 1421 | ;; 'where' (aka 'w') command to reveal the overlay arrow. |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 1422 | (let* ((origbuf (current-buffer)) |
| 1423 | (currproc (get-buffer-process origbuf))) |
Ken Manheimer | e7aca52 | 2003-03-03 00:35:32 +0000 | [diff] [blame] | 1424 | |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 1425 | (if (not (and currproc py-pdbtrack-do-tracking-p)) |
| 1426 | (py-pdbtrack-overlay-arrow nil) |
Ken Manheimer | e7aca52 | 2003-03-03 00:35:32 +0000 | [diff] [blame] | 1427 | |
| 1428 | (let* ((procmark (process-mark currproc)) |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 1429 | (block (buffer-substring (max comint-last-input-end |
| 1430 | (- procmark |
| 1431 | py-pdbtrack-track-range)) |
| 1432 | procmark)) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1433 | target target_fname target_lineno target_buffer) |
Ken Manheimer | e7aca52 | 2003-03-03 00:35:32 +0000 | [diff] [blame] | 1434 | |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 1435 | (if (not (string-match (concat py-pdbtrack-input-prompt "$") block)) |
| 1436 | (py-pdbtrack-overlay-arrow nil) |
Ken Manheimer | e7aca52 | 2003-03-03 00:35:32 +0000 | [diff] [blame] | 1437 | |
| 1438 | (setq target (py-pdbtrack-get-source-buffer block)) |
| 1439 | |
| 1440 | (if (stringp target) |
| 1441 | (message "pdbtrack: %s" target) |
| 1442 | |
| 1443 | (setq target_lineno (car target)) |
| 1444 | (setq target_buffer (cadr target)) |
| 1445 | (setq target_fname (buffer-file-name target_buffer)) |
| 1446 | (switch-to-buffer-other-window target_buffer) |
| 1447 | (goto-line target_lineno) |
| 1448 | (message "pdbtrack: line %s, file %s" target_lineno target_fname) |
| 1449 | (py-pdbtrack-overlay-arrow t) |
| 1450 | (pop-to-buffer origbuf t) |
| 1451 | |
| 1452 | ))))) |
| 1453 | ) |
| 1454 | |
| 1455 | (defun py-pdbtrack-get-source-buffer (block) |
| 1456 | "Return line number and buffer of code indicated by block's traceback text. |
| 1457 | |
| 1458 | We look first to visit the file indicated in the trace. |
| 1459 | |
| 1460 | Failing that, we look for the most recently visited python-mode buffer |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1461 | with the same name or having the named function. |
Ken Manheimer | e7aca52 | 2003-03-03 00:35:32 +0000 | [diff] [blame] | 1462 | |
| 1463 | If we're unable find the source code we return a string describing the |
| 1464 | problem as best as we can determine." |
| 1465 | |
| 1466 | (if (not (string-match py-pdbtrack-stack-entry-regexp block)) |
| 1467 | |
| 1468 | "Traceback cue not found" |
| 1469 | |
| 1470 | (let* ((filename (match-string 1 block)) |
| 1471 | (lineno (string-to-int (match-string 2 block))) |
| 1472 | (funcname (match-string 3 block)) |
| 1473 | funcbuffer) |
| 1474 | |
| 1475 | (cond ((file-exists-p filename) |
| 1476 | (list lineno (find-file-noselect filename))) |
| 1477 | |
| 1478 | ((setq funcbuffer (py-pdbtrack-grub-for-buffer funcname lineno)) |
| 1479 | (if (string-match "/Script (Python)$" filename) |
| 1480 | ;; Add in number of lines for leading '##' comments: |
| 1481 | (setq lineno |
| 1482 | (+ lineno |
| 1483 | (save-excursion |
| 1484 | (set-buffer funcbuffer) |
| 1485 | (count-lines |
| 1486 | (point-min) |
Ken Manheimer | ad428cd | 2003-03-03 04:05:03 +0000 | [diff] [blame] | 1487 | (max (point-min) |
| 1488 | (string-match "^\\([^#]\\|#[^#]\\|#$\\)" |
| 1489 | (buffer-substring (point-min) |
Ken Manheimer | 8e9b80f | 2003-06-17 19:18:57 +0000 | [diff] [blame] | 1490 | (point-max))) |
Ken Manheimer | ad428cd | 2003-03-03 04:05:03 +0000 | [diff] [blame] | 1491 | )))))) |
Ken Manheimer | e7aca52 | 2003-03-03 00:35:32 +0000 | [diff] [blame] | 1492 | (list lineno funcbuffer)) |
| 1493 | |
| 1494 | ((= (elt filename 0) ?\<) |
| 1495 | (format "(Non-file source: '%s')" filename)) |
| 1496 | |
Ken Manheimer | 595adce | 2003-05-01 20:46:14 +0000 | [diff] [blame] | 1497 | (t (format "Not found: %s(), %s" funcname filename))) |
Ken Manheimer | e7aca52 | 2003-03-03 00:35:32 +0000 | [diff] [blame] | 1498 | ) |
| 1499 | ) |
| 1500 | ) |
| 1501 | |
| 1502 | (defun py-pdbtrack-grub-for-buffer (funcname lineno) |
| 1503 | "Find most recent buffer itself named or having function funcname. |
| 1504 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1505 | We walk the buffer-list history for python-mode buffers that are |
Ken Manheimer | ed6000a | 2003-03-03 17:09:44 +0000 | [diff] [blame] | 1506 | named for funcname or define a function funcname." |
Ken Manheimer | e7aca52 | 2003-03-03 00:35:32 +0000 | [diff] [blame] | 1507 | (let ((buffers (buffer-list)) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1508 | buf |
Ken Manheimer | e7aca52 | 2003-03-03 00:35:32 +0000 | [diff] [blame] | 1509 | got) |
| 1510 | (while (and buffers (not got)) |
| 1511 | (setq buf (car buffers) |
| 1512 | buffers (cdr buffers)) |
Ken Manheimer | ed6000a | 2003-03-03 17:09:44 +0000 | [diff] [blame] | 1513 | (if (and (save-excursion (set-buffer buf) |
| 1514 | (string= major-mode "python-mode")) |
| 1515 | (or (string-match funcname (buffer-name buf)) |
Ken Manheimer | e7aca52 | 2003-03-03 00:35:32 +0000 | [diff] [blame] | 1516 | (string-match (concat "^\\s-*\\(def\\|class\\)\\s-+" |
| 1517 | funcname "\\s-*(") |
Ken Manheimer | 8e9b80f | 2003-06-17 19:18:57 +0000 | [diff] [blame] | 1518 | (save-excursion |
| 1519 | (set-buffer buf) |
| 1520 | (buffer-substring (point-min) |
| 1521 | (point-max)))))) |
Ken Manheimer | e7aca52 | 2003-03-03 00:35:32 +0000 | [diff] [blame] | 1522 | (setq got buf))) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1523 | got)) |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 1524 | |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1525 | (defun py-postprocess-output-buffer (buf) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1526 | "Highlight exceptions found in BUF. |
| 1527 | If an exception occurred return t, otherwise return nil. BUF must exist." |
Barry Warsaw | f9b99f4 | 1998-03-26 16:08:59 +0000 | [diff] [blame] | 1528 | (let (line file bol err-p) |
Barry Warsaw | 9981d22 | 1997-12-03 05:25:48 +0000 | [diff] [blame] | 1529 | (save-excursion |
| 1530 | (set-buffer buf) |
| 1531 | (beginning-of-buffer) |
| 1532 | (while (re-search-forward py-traceback-line-re nil t) |
| 1533 | (setq file (match-string 1) |
| 1534 | line (string-to-int (match-string 2)) |
| 1535 | bol (py-point 'bol)) |
Barry Warsaw | f9b99f4 | 1998-03-26 16:08:59 +0000 | [diff] [blame] | 1536 | (py-highlight-line bol (py-point 'eol) file line))) |
| 1537 | (when (and py-jump-on-exception line) |
| 1538 | (beep) |
| 1539 | (py-jump-to-exception file line) |
| 1540 | (setq err-p t)) |
| 1541 | err-p)) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1542 | |
Barry Warsaw | 9981d22 | 1997-12-03 05:25:48 +0000 | [diff] [blame] | 1543 | |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1544 | |
| 1545 | ;;; Subprocess commands |
| 1546 | |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1547 | ;; only used when (memq 'broken-temp-names py-emacs-features) |
| 1548 | (defvar py-serial-number 0) |
| 1549 | (defvar py-exception-buffer nil) |
| 1550 | (defconst py-output-buffer "*Python Output*") |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 1551 | (make-variable-buffer-local 'py-output-buffer) |
| 1552 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1553 | ;; for toggling between CPython and Jython |
Barry Warsaw | aa384fd | 1999-02-16 23:36:16 +0000 | [diff] [blame] | 1554 | (defvar py-which-shell nil) |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 1555 | (defvar py-which-args py-python-command-args) |
| 1556 | (defvar py-which-bufname "Python") |
| 1557 | (make-variable-buffer-local 'py-which-shell) |
| 1558 | (make-variable-buffer-local 'py-which-args) |
| 1559 | (make-variable-buffer-local 'py-which-bufname) |
| 1560 | |
| 1561 | (defun py-toggle-shells (arg) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1562 | "Toggles between the CPython and Jython shells. |
Barry Warsaw | 11f2156 | 1999-07-28 21:59:43 +0000 | [diff] [blame] | 1563 | |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1564 | With positive argument ARG (interactively \\[universal-argument]), |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1565 | uses the CPython shell, with negative ARG uses the Jython shell, and |
Barry Warsaw | 11f2156 | 1999-07-28 21:59:43 +0000 | [diff] [blame] | 1566 | with a zero argument, toggles the shell. |
| 1567 | |
| 1568 | Programmatically, ARG can also be one of the symbols `cpython' or |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1569 | `jython', equivalent to positive arg and negative arg respectively." |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 1570 | (interactive "P") |
| 1571 | ;; default is to toggle |
| 1572 | (if (null arg) |
| 1573 | (setq arg 0)) |
Barry Warsaw | 11f2156 | 1999-07-28 21:59:43 +0000 | [diff] [blame] | 1574 | ;; preprocess arg |
| 1575 | (cond |
| 1576 | ((equal arg 0) |
| 1577 | ;; toggle |
| 1578 | (if (string-equal py-which-bufname "Python") |
| 1579 | (setq arg -1) |
| 1580 | (setq arg 1))) |
| 1581 | ((equal arg 'cpython) (setq arg 1)) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1582 | ((equal arg 'jython) (setq arg -1))) |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 1583 | (let (msg) |
| 1584 | (cond |
| 1585 | ((< 0 arg) |
| 1586 | ;; set to CPython |
| 1587 | (setq py-which-shell py-python-command |
| 1588 | py-which-args py-python-command-args |
| 1589 | py-which-bufname "Python" |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1590 | msg "CPython") |
| 1591 | (if (string-equal py-which-bufname "Jython") |
| 1592 | (setq mode-name "Python"))) |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 1593 | ((> 0 arg) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1594 | (setq py-which-shell py-jython-command |
| 1595 | py-which-args py-jython-command-args |
| 1596 | py-which-bufname "Jython" |
| 1597 | msg "Jython") |
| 1598 | (if (string-equal py-which-bufname "Python") |
| 1599 | (setq mode-name "Jython"))) |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 1600 | ) |
Barry Warsaw | ea609c1 | 1998-04-10 16:08:26 +0000 | [diff] [blame] | 1601 | (message "Using the %s shell" msg) |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 1602 | (setq py-output-buffer (format "*%s Output*" py-which-bufname)))) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1603 | |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1604 | ;;;###autoload |
Barry Warsaw | 3b4e2f0 | 1999-02-16 23:52:46 +0000 | [diff] [blame] | 1605 | (defun py-shell (&optional argprompt) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1606 | "Start an interactive Python interpreter in another window. |
| 1607 | This is like Shell mode, except that Python is running in the window |
| 1608 | instead of a shell. See the `Interactive Shell' and `Shell Mode' |
| 1609 | sections of the Emacs manual for details, especially for the key |
| 1610 | bindings active in the `*Python*' buffer. |
| 1611 | |
Barry Warsaw | 3b4e2f0 | 1999-02-16 23:52:46 +0000 | [diff] [blame] | 1612 | With optional \\[universal-argument], the user is prompted for the |
| 1613 | flags to pass to the Python interpreter. This has no effect when this |
| 1614 | command is used to switch to an existing process, only when a new |
| 1615 | process is started. If you use this, you will probably want to ensure |
| 1616 | that the current arguments are retained (they will be included in the |
| 1617 | prompt). This argument is ignored when this function is called |
| 1618 | programmatically, or when running in Emacs 19.34 or older. |
| 1619 | |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 1620 | Note: You can toggle between using the CPython interpreter and the |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1621 | Jython interpreter by hitting \\[py-toggle-shells]. This toggles |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 1622 | buffer local variables which control whether all your subshell |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1623 | interactions happen to the `*Jython*' or `*Python*' buffers (the |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 1624 | latter is the name used for the CPython buffer). |
| 1625 | |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1626 | Warning: Don't use an interactive Python if you change sys.ps1 or |
| 1627 | sys.ps2 from their default values, or if you're running code that |
| 1628 | prints `>>> ' or `... ' at the start of a line. `python-mode' can't |
| 1629 | distinguish your output from Python's output, and assumes that `>>> ' |
| 1630 | at the start of a line is a prompt from Python. Similarly, the Emacs |
| 1631 | Shell mode code assumes that both `>>> ' and `... ' at the start of a |
| 1632 | line are Python prompts. Bad things can happen if you fool either |
| 1633 | mode. |
| 1634 | |
| 1635 | Warning: If you do any editing *in* the process buffer *while* the |
| 1636 | buffer is accepting output from Python, do NOT attempt to `undo' the |
| 1637 | changes. Some of the output (nowhere near the parts you changed!) may |
| 1638 | be lost if you do. This appears to be an Emacs bug, an unfortunate |
| 1639 | interaction between undo and process filters; the same problem exists in |
| 1640 | non-Python process buffers using the default (Emacs-supplied) process |
| 1641 | filter." |
Barry Warsaw | 3b4e2f0 | 1999-02-16 23:52:46 +0000 | [diff] [blame] | 1642 | (interactive "P") |
Barry Warsaw | 50765ab | 1999-08-10 21:49:00 +0000 | [diff] [blame] | 1643 | ;; Set the default shell if not already set |
| 1644 | (when (null py-which-shell) |
| 1645 | (py-toggle-shells py-default-interpreter)) |
Barry Warsaw | 3b4e2f0 | 1999-02-16 23:52:46 +0000 | [diff] [blame] | 1646 | (let ((args py-which-args)) |
| 1647 | (when (and argprompt |
| 1648 | (interactive-p) |
| 1649 | (fboundp 'split-string)) |
| 1650 | ;; TBD: Perhaps force "-i" in the final list? |
| 1651 | (setq args (split-string |
| 1652 | (read-string (concat py-which-bufname |
| 1653 | " arguments: ") |
| 1654 | (concat |
| 1655 | (mapconcat 'identity py-which-args " ") " ") |
| 1656 | )))) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1657 | (if (not (equal (buffer-name) "*Python*")) |
| 1658 | (switch-to-buffer-other-window |
| 1659 | (apply 'make-comint py-which-bufname py-which-shell nil args)) |
| 1660 | (apply 'make-comint py-which-bufname py-which-shell nil args)) |
Barry Warsaw | 3b4e2f0 | 1999-02-16 23:52:46 +0000 | [diff] [blame] | 1661 | (make-local-variable 'comint-prompt-regexp) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1662 | (setq comint-prompt-regexp (concat py-shell-input-prompt-1-regexp "\\|" |
| 1663 | py-shell-input-prompt-2-regexp "\\|" |
| 1664 | "^([Pp]db) ")) |
Barry Warsaw | 3b4e2f0 | 1999-02-16 23:52:46 +0000 | [diff] [blame] | 1665 | (add-hook 'comint-output-filter-functions |
| 1666 | 'py-comint-output-filter-function) |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 1667 | ;; pdbtrack |
| 1668 | (add-hook 'comint-output-filter-functions 'py-pdbtrack-track-stack-file) |
| 1669 | (setq py-pdbtrack-do-tracking-p t) |
Barry Warsaw | 3b4e2f0 | 1999-02-16 23:52:46 +0000 | [diff] [blame] | 1670 | (set-syntax-table py-mode-syntax-table) |
| 1671 | (use-local-map py-shell-map) |
Barry Warsaw | 56bd2ed | 2002-04-25 15:44:17 +0000 | [diff] [blame] | 1672 | (run-hooks 'py-shell-hook) |
Barry Warsaw | 3b4e2f0 | 1999-02-16 23:52:46 +0000 | [diff] [blame] | 1673 | )) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1674 | |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1675 | (defun py-clear-queue () |
| 1676 | "Clear the queue of temporary files waiting to execute." |
| 1677 | (interactive) |
| 1678 | (let ((n (length py-file-queue))) |
| 1679 | (mapcar 'delete-file py-file-queue) |
| 1680 | (setq py-file-queue nil) |
| 1681 | (message "%d pending files de-queued." n))) |
| 1682 | |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 1683 | |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1684 | (defun py-execute-region (start end &optional async) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1685 | "Execute the region in a Python interpreter. |
| 1686 | |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1687 | The region is first copied into a temporary file (in the directory |
| 1688 | `py-temp-directory'). If there is no Python interpreter shell |
| 1689 | running, this file is executed synchronously using |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1690 | `shell-command-on-region'. If the program is long running, use |
| 1691 | \\[universal-argument] to run the command asynchronously in its own |
| 1692 | buffer. |
| 1693 | |
| 1694 | When this function is used programmatically, arguments START and END |
| 1695 | specify the region to execute, and optional third argument ASYNC, if |
| 1696 | non-nil, specifies to run the command asynchronously in its own |
| 1697 | buffer. |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1698 | |
Neal Norwitz | 0168802 | 2007-08-12 00:43:29 +0000 | [diff] [blame] | 1699 | If the Python interpreter shell is running, the region is exec()'d |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1700 | in that shell. If you try to execute regions too quickly, |
| 1701 | `python-mode' will queue them up and execute them one at a time when |
| 1702 | it sees a `>>> ' prompt from Python. Each time this happens, the |
| 1703 | process buffer is popped into a window (if it's not already in some |
| 1704 | window) so you can see it, and a comment of the form |
| 1705 | |
| 1706 | \t## working on region in file <name>... |
| 1707 | |
| 1708 | is inserted at the end. See also the command `py-clear-queue'." |
| 1709 | (interactive "r\nP") |
Barry Warsaw | 4aab68e | 2002-04-25 19:17:42 +0000 | [diff] [blame] | 1710 | ;; Skip ahead to the first non-blank line |
Barry Warsaw | 014e0e2 | 1998-11-17 19:24:47 +0000 | [diff] [blame] | 1711 | (let* ((proc (get-process py-which-bufname)) |
Barry Warsaw | 5e21cb0 | 1997-11-05 18:41:11 +0000 | [diff] [blame] | 1712 | (temp (if (memq 'broken-temp-names py-emacs-features) |
Barry Warsaw | 1b34424 | 1998-08-07 22:24:16 +0000 | [diff] [blame] | 1713 | (let |
| 1714 | ((sn py-serial-number) |
| 1715 | (pid (and (fboundp 'emacs-pid) (emacs-pid)))) |
| 1716 | (setq py-serial-number (1+ py-serial-number)) |
| 1717 | (if pid |
| 1718 | (format "python-%d-%d" sn pid) |
| 1719 | (format "python-%d" sn))) |
Barry Warsaw | 2f32fbb | 1998-02-25 16:45:43 +0000 | [diff] [blame] | 1720 | (make-temp-name "python-"))) |
Barry Warsaw | b2d5e62 | 2002-04-22 15:29:27 +0000 | [diff] [blame] | 1721 | (file (concat (expand-file-name temp py-temp-directory) ".py")) |
Barry Warsaw | caee2fe | 2000-05-23 05:47:43 +0000 | [diff] [blame] | 1722 | (cur (current-buffer)) |
Barry Warsaw | b2d5e62 | 2002-04-22 15:29:27 +0000 | [diff] [blame] | 1723 | (buf (get-buffer-create file)) |
| 1724 | shell) |
Barry Warsaw | caee2fe | 2000-05-23 05:47:43 +0000 | [diff] [blame] | 1725 | ;; Write the contents of the buffer, watching out for indented regions. |
| 1726 | (save-excursion |
| 1727 | (goto-char start) |
Barry Warsaw | 69c9266 | 2002-05-12 17:37:46 +0000 | [diff] [blame] | 1728 | (beginning-of-line) |
| 1729 | (while (and (looking-at "\\s *$") |
| 1730 | (< (point) end)) |
| 1731 | (forward-line 1)) |
| 1732 | (setq start (point)) |
| 1733 | (or (< start end) |
| 1734 | (error "Region is empty")) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1735 | (setq py-line-number-offset (count-lines 1 start)) |
Barry Warsaw | 99eadf4 | 2000-06-23 20:24:25 +0000 | [diff] [blame] | 1736 | (let ((needs-if (/= (py-point 'bol) (py-point 'boi)))) |
Barry Warsaw | caee2fe | 2000-05-23 05:47:43 +0000 | [diff] [blame] | 1737 | (set-buffer buf) |
Barry Warsaw | b2d5e62 | 2002-04-22 15:29:27 +0000 | [diff] [blame] | 1738 | (python-mode) |
Barry Warsaw | 99eadf4 | 2000-06-23 20:24:25 +0000 | [diff] [blame] | 1739 | (when needs-if |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1740 | (insert "if 1:\n") |
| 1741 | (setq py-line-number-offset (- py-line-number-offset 1))) |
Barry Warsaw | a0113cd | 2002-04-22 16:23:29 +0000 | [diff] [blame] | 1742 | (insert-buffer-substring cur start end) |
| 1743 | ;; Set the shell either to the #! line command, or to the |
| 1744 | ;; py-which-shell buffer local variable. |
Barry Warsaw | 8849161 | 2002-04-25 21:31:47 +0000 | [diff] [blame] | 1745 | (setq shell (or (py-choose-shell-by-shebang) |
| 1746 | (py-choose-shell-by-import) |
| 1747 | py-which-shell)))) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1748 | (cond |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 1749 | ;; always run the code in its own asynchronous subprocess |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1750 | (async |
Barry Warsaw | caee2fe | 2000-05-23 05:47:43 +0000 | [diff] [blame] | 1751 | ;; User explicitly wants this to run in its own async subprocess |
| 1752 | (save-excursion |
| 1753 | (set-buffer buf) |
| 1754 | (write-region (point-min) (point-max) file nil 'nomsg)) |
Barry Warsaw | 34d8317 | 1998-11-20 03:04:07 +0000 | [diff] [blame] | 1755 | (let* ((buf (generate-new-buffer-name py-output-buffer)) |
| 1756 | ;; TBD: a horrible hack, but why create new Custom variables? |
| 1757 | (arg (if (string-equal py-which-bufname "Python") |
| 1758 | "-u" ""))) |
Barry Warsaw | b2d5e62 | 2002-04-22 15:29:27 +0000 | [diff] [blame] | 1759 | (start-process py-which-bufname buf shell arg file) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1760 | (pop-to-buffer buf) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1761 | (py-postprocess-output-buffer buf) |
Barry Warsaw | caee2fe | 2000-05-23 05:47:43 +0000 | [diff] [blame] | 1762 | ;; TBD: clean up the temporary file! |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1763 | )) |
| 1764 | ;; if the Python interpreter shell is running, queue it up for |
| 1765 | ;; execution there. |
| 1766 | (proc |
| 1767 | ;; use the existing python shell |
Barry Warsaw | caee2fe | 2000-05-23 05:47:43 +0000 | [diff] [blame] | 1768 | (save-excursion |
| 1769 | (set-buffer buf) |
| 1770 | (write-region (point-min) (point-max) file nil 'nomsg)) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1771 | (if (not py-file-queue) |
| 1772 | (py-execute-file proc file) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1773 | (message "File %s queued for execution" file)) |
Barry Warsaw | 3c96f6f | 1998-08-20 19:44:51 +0000 | [diff] [blame] | 1774 | (setq py-file-queue (append py-file-queue (list file))) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1775 | (setq py-exception-buffer (cons file (current-buffer)))) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1776 | (t |
Barry Warsaw | a0113cd | 2002-04-22 16:23:29 +0000 | [diff] [blame] | 1777 | ;; TBD: a horrible hack, but why create new Custom variables? |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1778 | (let ((cmd (concat py-which-shell (if (string-equal py-which-bufname |
| 1779 | "Jython") |
| 1780 | " -" "")))) |
Barry Warsaw | 34d8317 | 1998-11-20 03:04:07 +0000 | [diff] [blame] | 1781 | ;; otherwise either run it synchronously in a subprocess |
Barry Warsaw | caee2fe | 2000-05-23 05:47:43 +0000 | [diff] [blame] | 1782 | (save-excursion |
| 1783 | (set-buffer buf) |
| 1784 | (shell-command-on-region (point-min) (point-max) |
| 1785 | cmd py-output-buffer)) |
Barry Warsaw | 34d8317 | 1998-11-20 03:04:07 +0000 | [diff] [blame] | 1786 | ;; shell-command-on-region kills the output buffer if it never |
| 1787 | ;; existed and there's no output from the command |
| 1788 | (if (not (get-buffer py-output-buffer)) |
| 1789 | (message "No output.") |
| 1790 | (setq py-exception-buffer (current-buffer)) |
| 1791 | (let ((err-p (py-postprocess-output-buffer py-output-buffer))) |
| 1792 | (pop-to-buffer py-output-buffer) |
| 1793 | (if err-p |
| 1794 | (pop-to-buffer py-exception-buffer))) |
Barry Warsaw | caee2fe | 2000-05-23 05:47:43 +0000 | [diff] [blame] | 1795 | )) |
Barry Warsaw | 29a90f0 | 2002-04-22 21:48:20 +0000 | [diff] [blame] | 1796 | )) |
Barry Warsaw | 7153460 | 2001-02-20 23:07:56 +0000 | [diff] [blame] | 1797 | ;; Clean up after ourselves. |
| 1798 | (kill-buffer buf))) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1799 | |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 1800 | |
| 1801 | ;; Code execution commands |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1802 | (defun py-execute-buffer (&optional async) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1803 | "Send the contents of the buffer to a Python interpreter. |
Barry Warsaw | c0ecb53 | 1998-01-20 21:43:34 +0000 | [diff] [blame] | 1804 | If the file local variable `py-master-file' is non-nil, execute the |
| 1805 | named file instead of the buffer's file. |
| 1806 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1807 | If there is a *Python* process buffer it is used. If a clipping |
| 1808 | restriction is in effect, only the accessible portion of the buffer is |
| 1809 | sent. A trailing newline will be supplied if needed. |
| 1810 | |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1811 | See the `\\[py-execute-region]' docs for an account of some |
| 1812 | subtleties, including the use of the optional ASYNC argument." |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1813 | (interactive "P") |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1814 | (let ((old-buffer (current-buffer))) |
| 1815 | (if py-master-file |
| 1816 | (let* ((filename (expand-file-name py-master-file)) |
| 1817 | (buffer (or (get-file-buffer filename) |
| 1818 | (find-file-noselect filename)))) |
| 1819 | (set-buffer buffer))) |
| 1820 | (py-execute-region (point-min) (point-max) async) |
| 1821 | (pop-to-buffer old-buffer))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1822 | |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 1823 | (defun py-execute-import-or-reload (&optional async) |
| 1824 | "Import the current buffer's file in a Python interpreter. |
| 1825 | |
| 1826 | If the file has already been imported, then do reload instead to get |
Barry Warsaw | 7c29b23 | 1998-07-07 17:45:38 +0000 | [diff] [blame] | 1827 | the latest version. |
| 1828 | |
Neal Norwitz | 0168802 | 2007-08-12 00:43:29 +0000 | [diff] [blame] | 1829 | If the file's name does not end in \".py\", then do exec instead. |
Barry Warsaw | 7c29b23 | 1998-07-07 17:45:38 +0000 | [diff] [blame] | 1830 | |
| 1831 | If the current buffer is not visiting a file, do `py-execute-buffer' |
| 1832 | instead. |
| 1833 | |
| 1834 | If the file local variable `py-master-file' is non-nil, import or |
| 1835 | reload the named file instead of the buffer's file. The file may be |
| 1836 | saved based on the value of `py-execute-import-or-reload-save-p'. |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 1837 | |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1838 | See the `\\[py-execute-region]' docs for an account of some |
| 1839 | subtleties, including the use of the optional ASYNC argument. |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 1840 | |
Barry Warsaw | 7c29b23 | 1998-07-07 17:45:38 +0000 | [diff] [blame] | 1841 | This may be preferable to `\\[py-execute-buffer]' because: |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 1842 | |
| 1843 | - Definitions stay in their module rather than appearing at top |
| 1844 | level, where they would clutter the global namespace and not affect |
| 1845 | uses of qualified names (MODULE.NAME). |
| 1846 | |
| 1847 | - The Python debugger gets line number information about the functions." |
| 1848 | (interactive "P") |
| 1849 | ;; Check file local variable py-master-file |
| 1850 | (if py-master-file |
| 1851 | (let* ((filename (expand-file-name py-master-file)) |
| 1852 | (buffer (or (get-file-buffer filename) |
| 1853 | (find-file-noselect filename)))) |
| 1854 | (set-buffer buffer))) |
| 1855 | (let ((file (buffer-file-name (current-buffer)))) |
| 1856 | (if file |
| 1857 | (progn |
Barry Warsaw | 3bfed5b | 1998-05-19 16:25:04 +0000 | [diff] [blame] | 1858 | ;; Maybe save some buffers |
| 1859 | (save-some-buffers (not py-ask-about-save) nil) |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 1860 | (py-execute-string |
| 1861 | (if (string-match "\\.py$" file) |
| 1862 | (let ((f (file-name-sans-extension |
| 1863 | (file-name-nondirectory file)))) |
| 1864 | (format "if globals().has_key('%s'):\n reload(%s)\nelse:\n import %s\n" |
| 1865 | f f f)) |
Neal Norwitz | 0168802 | 2007-08-12 00:43:29 +0000 | [diff] [blame] | 1866 | (format "exec(open(r'%s'))\n" file)) |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 1867 | async)) |
| 1868 | ;; else |
| 1869 | (py-execute-buffer async)))) |
| 1870 | |
| 1871 | |
| 1872 | (defun py-execute-def-or-class (&optional async) |
| 1873 | "Send the current function or class definition to a Python interpreter. |
| 1874 | |
| 1875 | If there is a *Python* process buffer it is used. |
| 1876 | |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1877 | See the `\\[py-execute-region]' docs for an account of some |
| 1878 | subtleties, including the use of the optional ASYNC argument." |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 1879 | (interactive "P") |
| 1880 | (save-excursion |
| 1881 | (py-mark-def-or-class) |
| 1882 | ;; mark is before point |
| 1883 | (py-execute-region (mark) (point) async))) |
| 1884 | |
| 1885 | |
| 1886 | (defun py-execute-string (string &optional async) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1887 | "Send the argument STRING to a Python interpreter. |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 1888 | |
| 1889 | If there is a *Python* process buffer it is used. |
| 1890 | |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1891 | See the `\\[py-execute-region]' docs for an account of some |
| 1892 | subtleties, including the use of the optional ASYNC argument." |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 1893 | (interactive "sExecute Python command: ") |
| 1894 | (save-excursion |
| 1895 | (set-buffer (get-buffer-create |
| 1896 | (generate-new-buffer-name " *Python Command*"))) |
| 1897 | (insert string) |
| 1898 | (py-execute-region (point-min) (point-max) async))) |
| 1899 | |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1900 | |
| 1901 | |
| 1902 | (defun py-jump-to-exception (file line) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1903 | "Jump to the Python code in FILE at LINE." |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1904 | (let ((buffer (cond ((string-equal file "<stdin>") |
Barry Warsaw | ebc7b7a | 1998-05-19 15:01:06 +0000 | [diff] [blame] | 1905 | (if (consp py-exception-buffer) |
| 1906 | (cdr py-exception-buffer) |
| 1907 | py-exception-buffer)) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1908 | ((and (consp py-exception-buffer) |
| 1909 | (string-equal file (car py-exception-buffer))) |
| 1910 | (cdr py-exception-buffer)) |
| 1911 | ((py-safe (find-file-noselect file))) |
| 1912 | ;; could not figure out what file the exception |
| 1913 | ;; is pointing to, so prompt for it |
| 1914 | (t (find-file (read-file-name "Exception file: " |
| 1915 | nil |
| 1916 | file t)))))) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 1917 | ;; Fiddle about with line number |
| 1918 | (setq line (+ py-line-number-offset line)) |
| 1919 | |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1920 | (pop-to-buffer buffer) |
Barry Warsaw | ebc7b7a | 1998-05-19 15:01:06 +0000 | [diff] [blame] | 1921 | ;; Force Python mode |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 1922 | (if (not (eq major-mode 'python-mode)) |
Barry Warsaw | ebc7b7a | 1998-05-19 15:01:06 +0000 | [diff] [blame] | 1923 | (python-mode)) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1924 | (goto-line line) |
| 1925 | (message "Jumping to exception in file %s on line %d" file line))) |
| 1926 | |
| 1927 | (defun py-mouseto-exception (event) |
Barry Warsaw | 12c9294 | 1998-08-10 21:44:37 +0000 | [diff] [blame] | 1928 | "Jump to the code which caused the Python exception at EVENT. |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1929 | EVENT is usually a mouse click." |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1930 | (interactive "e") |
| 1931 | (cond |
| 1932 | ((fboundp 'event-point) |
| 1933 | ;; XEmacs |
| 1934 | (let* ((point (event-point event)) |
| 1935 | (buffer (event-buffer event)) |
| 1936 | (e (and point buffer (extent-at point buffer 'py-exc-info))) |
| 1937 | (info (and e (extent-property e 'py-exc-info)))) |
| 1938 | (message "Event point: %d, info: %s" point info) |
| 1939 | (and info |
| 1940 | (py-jump-to-exception (car info) (cdr info))) |
| 1941 | )) |
| 1942 | ;; Emacs -- Please port this! |
| 1943 | )) |
| 1944 | |
| 1945 | (defun py-goto-exception () |
| 1946 | "Go to the line indicated by the traceback." |
| 1947 | (interactive) |
| 1948 | (let (file line) |
| 1949 | (save-excursion |
| 1950 | (beginning-of-line) |
| 1951 | (if (looking-at py-traceback-line-re) |
| 1952 | (setq file (match-string 1) |
| 1953 | line (string-to-int (match-string 2))))) |
| 1954 | (if (not file) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1955 | (error "Not on a traceback line")) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1956 | (py-jump-to-exception file line))) |
| 1957 | |
| 1958 | (defun py-find-next-exception (start buffer searchdir errwhere) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1959 | "Find the next Python exception and jump to the code that caused it. |
| 1960 | START is the buffer position in BUFFER from which to begin searching |
| 1961 | for an exception. SEARCHDIR is a function, either |
| 1962 | `re-search-backward' or `re-search-forward' indicating the direction |
| 1963 | to search. ERRWHERE is used in an error message if the limit (top or |
| 1964 | bottom) of the trackback stack is encountered." |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1965 | (let (file line) |
| 1966 | (save-excursion |
| 1967 | (set-buffer buffer) |
| 1968 | (goto-char (py-point start)) |
| 1969 | (if (funcall searchdir py-traceback-line-re nil t) |
| 1970 | (setq file (match-string 1) |
| 1971 | line (string-to-int (match-string 2))))) |
| 1972 | (if (and file line) |
| 1973 | (py-jump-to-exception file line) |
| 1974 | (error "%s of traceback" errwhere)))) |
| 1975 | |
| 1976 | (defun py-down-exception (&optional bottom) |
| 1977 | "Go to the next line down in the traceback. |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1978 | With \\[univeral-argument] (programmatically, optional argument |
| 1979 | BOTTOM), jump to the bottom (innermost) exception in the exception |
| 1980 | stack." |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1981 | (interactive "P") |
| 1982 | (let* ((proc (get-process "Python")) |
| 1983 | (buffer (if proc "*Python*" py-output-buffer))) |
| 1984 | (if bottom |
| 1985 | (py-find-next-exception 'eob buffer 're-search-backward "Bottom") |
| 1986 | (py-find-next-exception 'eol buffer 're-search-forward "Bottom")))) |
| 1987 | |
| 1988 | (defun py-up-exception (&optional top) |
| 1989 | "Go to the previous line up in the traceback. |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 1990 | With \\[universal-argument] (programmatically, optional argument TOP) |
| 1991 | jump to the top (outermost) exception in the exception stack." |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1992 | (interactive "P") |
| 1993 | (let* ((proc (get-process "Python")) |
| 1994 | (buffer (if proc "*Python*" py-output-buffer))) |
| 1995 | (if top |
| 1996 | (py-find-next-exception 'bob buffer 're-search-forward "Top") |
Barry Warsaw | ffbc17d | 1997-11-27 20:08:14 +0000 | [diff] [blame] | 1997 | (py-find-next-exception 'bol buffer 're-search-backward "Top")))) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1998 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1999 | |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 2000 | ;; Electric deletion |
| 2001 | (defun py-electric-backspace (arg) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2002 | "Delete preceding character or levels of indentation. |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 2003 | Deletion is performed by calling the function in `py-backspace-function' |
Barry Warsaw | b053993 | 1996-12-17 22:05:07 +0000 | [diff] [blame] | 2004 | with a single argument (the number of characters to delete). |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2005 | |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2006 | If point is at the leftmost column, delete the preceding newline. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2007 | |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 2008 | Otherwise, if point is at the leftmost non-whitespace character of a |
| 2009 | line that is neither a continuation line nor a non-indenting comment |
| 2010 | line, or if point is at the end of a blank line, this command reduces |
| 2011 | the indentation to match that of the line that opened the current |
| 2012 | block of code. The line that opened the block is displayed in the |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2013 | echo area to help you keep track of where you are. With |
| 2014 | \\[universal-argument] dedents that many blocks (but not past column |
| 2015 | zero). |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 2016 | |
| 2017 | Otherwise the preceding character is deleted, converting a tab to |
| 2018 | spaces if needed so that only a single column position is deleted. |
Barry Warsaw | fa2def2 | 1999-05-24 21:43:37 +0000 | [diff] [blame] | 2019 | \\[universal-argument] specifies how many characters to delete; |
| 2020 | default is 1. |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2021 | |
| 2022 | When used programmatically, argument ARG specifies the number of |
| 2023 | blocks to dedent, or the number of characters to delete, as indicated |
| 2024 | above." |
Barry Warsaw | 0397073 | 1996-07-03 23:12:52 +0000 | [diff] [blame] | 2025 | (interactive "*p") |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2026 | (if (or (/= (current-indentation) (current-column)) |
| 2027 | (bolp) |
| 2028 | (py-continuation-line-p) |
Barry Warsaw | fa2def2 | 1999-05-24 21:43:37 +0000 | [diff] [blame] | 2029 | ; (not py-honor-comment-indentation) |
| 2030 | ; (looking-at "#[^ \t\n]") ; non-indenting # |
| 2031 | ) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 2032 | (funcall py-backspace-function arg) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2033 | ;; else indent the same as the colon line that opened the block |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2034 | ;; force non-blank so py-goto-block-up doesn't ignore it |
| 2035 | (insert-char ?* 1) |
| 2036 | (backward-char) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2037 | (let ((base-indent 0) ; indentation of base line |
| 2038 | (base-text "") ; and text of base line |
| 2039 | (base-found-p nil)) |
Barry Warsaw | 0397073 | 1996-07-03 23:12:52 +0000 | [diff] [blame] | 2040 | (save-excursion |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 2041 | (while (< 0 arg) |
Barry Warsaw | 0397073 | 1996-07-03 23:12:52 +0000 | [diff] [blame] | 2042 | (condition-case nil ; in case no enclosing block |
| 2043 | (progn |
| 2044 | (py-goto-block-up 'no-mark) |
| 2045 | (setq base-indent (current-indentation) |
| 2046 | base-text (py-suck-up-leading-text) |
| 2047 | base-found-p t)) |
| 2048 | (error nil)) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 2049 | (setq arg (1- arg)))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2050 | (delete-char 1) ; toss the dummy character |
| 2051 | (delete-horizontal-space) |
| 2052 | (indent-to base-indent) |
| 2053 | (if base-found-p |
| 2054 | (message "Closes block: %s" base-text))))) |
| 2055 | |
Barry Warsaw | fc8a01f | 1995-03-09 16:07:29 +0000 | [diff] [blame] | 2056 | |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 2057 | (defun py-electric-delete (arg) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2058 | "Delete preceding or following character or levels of whitespace. |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 2059 | |
| 2060 | The behavior of this function depends on the variable |
| 2061 | `delete-key-deletes-forward'. If this variable is nil (or does not |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2062 | exist, as in older Emacsen and non-XEmacs versions), then this |
| 2063 | function behaves identically to \\[c-electric-backspace]. |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 2064 | |
| 2065 | If `delete-key-deletes-forward' is non-nil and is supported in your |
| 2066 | Emacs, then deletion occurs in the forward direction, by calling the |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2067 | function in `py-delete-function'. |
| 2068 | |
| 2069 | \\[universal-argument] (programmatically, argument ARG) specifies the |
| 2070 | number of characters to delete (default is 1)." |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 2071 | (interactive "*p") |
Barry Warsaw | 1d7b0fa | 1999-01-15 02:12:31 +0000 | [diff] [blame] | 2072 | (if (or (and (fboundp 'delete-forward-p) ;XEmacs 21 |
| 2073 | (delete-forward-p)) |
| 2074 | (and (boundp 'delete-key-deletes-forward) ;XEmacs 20 |
| 2075 | delete-key-deletes-forward)) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 2076 | (funcall py-delete-function arg) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 2077 | (py-electric-backspace arg))) |
| 2078 | |
| 2079 | ;; required for pending-del and delsel modes |
Skip Montanaro | 98a4fbe | 2003-08-01 04:00:32 +0000 | [diff] [blame] | 2080 | (put 'py-electric-colon 'delete-selection t) ;delsel |
| 2081 | (put 'py-electric-colon 'pending-delete t) ;pending-del |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 2082 | (put 'py-electric-backspace 'delete-selection 'supersede) ;delsel |
| 2083 | (put 'py-electric-backspace 'pending-delete 'supersede) ;pending-del |
| 2084 | (put 'py-electric-delete 'delete-selection 'supersede) ;delsel |
| 2085 | (put 'py-electric-delete 'pending-delete 'supersede) ;pending-del |
| 2086 | |
| 2087 | |
| 2088 | |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 2089 | (defun py-indent-line (&optional arg) |
| 2090 | "Fix the indentation of the current line according to Python rules. |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2091 | With \\[universal-argument] (programmatically, the optional argument |
| 2092 | ARG non-nil), ignore dedenting rules for block closing statements |
| 2093 | (e.g. return, raise, break, continue, pass) |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 2094 | |
| 2095 | This function is normally bound to `indent-line-function' so |
| 2096 | \\[indent-for-tab-command] will call it." |
| 2097 | (interactive "P") |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2098 | (let* ((ci (current-indentation)) |
| 2099 | (move-to-indentation-p (<= (current-column) ci)) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 2100 | (need (py-compute-indentation (not arg))) |
| 2101 | (cc (current-column))) |
| 2102 | ;; dedent out a level if previous command was the same unless we're in |
| 2103 | ;; column 1 |
| 2104 | (if (and (equal last-command this-command) |
| 2105 | (/= cc 0)) |
| 2106 | (progn |
| 2107 | (beginning-of-line) |
| 2108 | (delete-horizontal-space) |
| 2109 | (indent-to (* (/ (- cc 1) py-indent-offset) py-indent-offset))) |
| 2110 | (progn |
| 2111 | ;; see if we need to dedent |
| 2112 | (if (py-outdent-p) |
| 2113 | (setq need (- need py-indent-offset))) |
| 2114 | (if (or py-tab-always-indent |
| 2115 | move-to-indentation-p) |
| 2116 | (progn (if (/= ci need) |
| 2117 | (save-excursion |
| 2118 | (beginning-of-line) |
| 2119 | (delete-horizontal-space) |
| 2120 | (indent-to need))) |
| 2121 | (if move-to-indentation-p (back-to-indentation))) |
| 2122 | (insert-tab)))))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2123 | |
| 2124 | (defun py-newline-and-indent () |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2125 | "Strives to act like the Emacs `newline-and-indent'. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2126 | This is just `strives to' because correct indentation can't be computed |
| 2127 | from scratch for Python code. In general, deletes the whitespace before |
| 2128 | point, inserts a newline, and takes an educated guess as to how you want |
| 2129 | the new line indented." |
| 2130 | (interactive) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2131 | (let ((ci (current-indentation))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2132 | (if (< ci (current-column)) ; if point beyond indentation |
| 2133 | (newline-and-indent) |
| 2134 | ;; else try to act like newline-and-indent "normally" acts |
| 2135 | (beginning-of-line) |
| 2136 | (insert-char ?\n 1) |
| 2137 | (move-to-column ci)))) |
| 2138 | |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 2139 | (defun py-compute-indentation (honor-block-close-p) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2140 | "Compute Python indentation. |
| 2141 | When HONOR-BLOCK-CLOSE-P is non-nil, statements such as `return', |
| 2142 | `raise', `break', `continue', and `pass' force one level of |
| 2143 | dedenting." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2144 | (save-excursion |
Barry Warsaw | f64b405 | 1998-02-12 16:52:14 +0000 | [diff] [blame] | 2145 | (beginning-of-line) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 2146 | (let* ((bod (py-point 'bod)) |
Barry Warsaw | 3b3ff4e | 1997-11-26 20:58:48 +0000 | [diff] [blame] | 2147 | (pps (parse-partial-sexp bod (point))) |
Barry Warsaw | 9c1696c | 1998-12-15 04:36:22 +0000 | [diff] [blame] | 2148 | (boipps (parse-partial-sexp bod (py-point 'boi))) |
| 2149 | placeholder) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 2150 | (cond |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 2151 | ;; are we inside a multi-line string or comment? |
Barry Warsaw | 3b3ff4e | 1997-11-26 20:58:48 +0000 | [diff] [blame] | 2152 | ((or (and (nth 3 pps) (nth 3 boipps)) |
| 2153 | (and (nth 4 pps) (nth 4 boipps))) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 2154 | (save-excursion |
| 2155 | (if (not py-align-multiline-strings-p) 0 |
| 2156 | ;; skip back over blank & non-indenting comment lines |
| 2157 | ;; note: will skip a blank or non-indenting comment line |
| 2158 | ;; that happens to be a continuation line too |
| 2159 | (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move) |
| 2160 | (back-to-indentation) |
| 2161 | (current-column)))) |
| 2162 | ;; are we on a continuation line? |
| 2163 | ((py-continuation-line-p) |
| 2164 | (let ((startpos (point)) |
| 2165 | (open-bracket-pos (py-nesting-level)) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 2166 | endpos searching found state cind cline) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 2167 | (if open-bracket-pos |
| 2168 | (progn |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 2169 | (setq endpos (py-point 'bol)) |
| 2170 | (py-goto-initial-line) |
| 2171 | (setq cind (current-indentation)) |
| 2172 | (setq cline cind) |
| 2173 | (dolist (bp |
| 2174 | (nth 9 (save-excursion |
| 2175 | (parse-partial-sexp (point) endpos))) |
| 2176 | cind) |
| 2177 | (if (search-forward "\n" bp t) (setq cline cind)) |
| 2178 | (goto-char (1+ bp)) |
| 2179 | (skip-chars-forward " \t") |
| 2180 | (setq cind (if (memq (following-char) '(?\n ?# ?\\)) |
| 2181 | (+ cline py-indent-offset) |
| 2182 | (current-column))))) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 2183 | ;; else on backslash continuation line |
| 2184 | (forward-line -1) |
| 2185 | (if (py-continuation-line-p) ; on at least 3rd line in block |
| 2186 | (current-indentation) ; so just continue the pattern |
| 2187 | ;; else started on 2nd line in block, so indent more. |
| 2188 | ;; if base line is an assignment with a start on a RHS, |
| 2189 | ;; indent to 2 beyond the leftmost "="; else skip first |
| 2190 | ;; chunk of non-whitespace characters on base line, + 1 more |
| 2191 | ;; column |
| 2192 | (end-of-line) |
Barry Warsaw | fd4c9e8 | 2001-06-18 23:40:35 +0000 | [diff] [blame] | 2193 | (setq endpos (point) |
| 2194 | searching t) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 2195 | (back-to-indentation) |
| 2196 | (setq startpos (point)) |
| 2197 | ;; look at all "=" from left to right, stopping at first |
| 2198 | ;; one not nested in a list or string |
| 2199 | (while searching |
| 2200 | (skip-chars-forward "^=" endpos) |
| 2201 | (if (= (point) endpos) |
| 2202 | (setq searching nil) |
| 2203 | (forward-char 1) |
| 2204 | (setq state (parse-partial-sexp startpos (point))) |
| 2205 | (if (and (zerop (car state)) ; not in a bracket |
| 2206 | (null (nth 3 state))) ; & not in a string |
| 2207 | (progn |
| 2208 | (setq searching nil) ; done searching in any case |
| 2209 | (setq found |
| 2210 | (not (or |
| 2211 | (eq (following-char) ?=) |
| 2212 | (memq (char-after (- (point) 2)) |
| 2213 | '(?< ?> ?!))))))))) |
| 2214 | (if (or (not found) ; not an assignment |
| 2215 | (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash> |
| 2216 | (progn |
| 2217 | (goto-char startpos) |
| 2218 | (skip-chars-forward "^ \t\n"))) |
Barry Warsaw | 40fb452 | 2001-07-06 20:07:13 +0000 | [diff] [blame] | 2219 | ;; if this is a continuation for a block opening |
| 2220 | ;; statement, add some extra offset. |
| 2221 | (+ (current-column) (if (py-statement-opens-block-p) |
| 2222 | py-continuation-offset 0) |
| 2223 | 1) |
Barry Warsaw | fd4c9e8 | 2001-06-18 23:40:35 +0000 | [diff] [blame] | 2224 | )))) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 2225 | |
| 2226 | ;; not on a continuation line |
Barry Warsaw | a789171 | 1996-08-01 15:53:09 +0000 | [diff] [blame] | 2227 | ((bobp) (current-indentation)) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 2228 | |
Barry Warsaw | a789171 | 1996-08-01 15:53:09 +0000 | [diff] [blame] | 2229 | ;; Dfn: "Indenting comment line". A line containing only a |
| 2230 | ;; comment, but which is treated like a statement for |
| 2231 | ;; indentation calculation purposes. Such lines are only |
| 2232 | ;; treated specially by the mode; they are not treated |
| 2233 | ;; specially by the Python interpreter. |
| 2234 | |
| 2235 | ;; The rules for indenting comment lines are a line where: |
| 2236 | ;; - the first non-whitespace character is `#', and |
| 2237 | ;; - the character following the `#' is whitespace, and |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2238 | ;; - the line is dedented with respect to (i.e. to the left |
Barry Warsaw | a789171 | 1996-08-01 15:53:09 +0000 | [diff] [blame] | 2239 | ;; of) the indentation of the preceding non-blank line. |
| 2240 | |
| 2241 | ;; The first non-blank line following an indenting comment |
| 2242 | ;; line is given the same amount of indentation as the |
| 2243 | ;; indenting comment line. |
| 2244 | |
| 2245 | ;; All other comment-only lines are ignored for indentation |
| 2246 | ;; purposes. |
| 2247 | |
| 2248 | ;; Are we looking at a comment-only line which is *not* an |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 2249 | ;; indenting comment line? If so, we assume that it's been |
Barry Warsaw | a789171 | 1996-08-01 15:53:09 +0000 | [diff] [blame] | 2250 | ;; placed at the desired indentation, so leave it alone. |
| 2251 | ;; Indenting comment lines are aligned as statements down |
| 2252 | ;; below. |
| 2253 | ((and (looking-at "[ \t]*#[^ \t\n]") |
| 2254 | ;; NOTE: this test will not be performed in older Emacsen |
| 2255 | (fboundp 'forward-comment) |
| 2256 | (<= (current-indentation) |
| 2257 | (save-excursion |
| 2258 | (forward-comment (- (point-max))) |
| 2259 | (current-indentation)))) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 2260 | (current-indentation)) |
| 2261 | |
| 2262 | ;; else indentation based on that of the statement that |
| 2263 | ;; precedes us; use the first line of that statement to |
| 2264 | ;; establish the base, in case the user forced a non-std |
| 2265 | ;; indentation for the continuation lines (if any) |
| 2266 | (t |
Barry Warsaw | c01c5c8 | 1995-09-14 18:49:11 +0000 | [diff] [blame] | 2267 | ;; skip back over blank & non-indenting comment lines note: |
| 2268 | ;; will skip a blank or non-indenting comment line that |
Barry Warsaw | fd0fb38 | 1996-03-04 17:15:40 +0000 | [diff] [blame] | 2269 | ;; happens to be a continuation line too. use fast Emacs 19 |
| 2270 | ;; function if it's there. |
Barry Warsaw | 6d62775 | 1996-03-06 18:41:38 +0000 | [diff] [blame] | 2271 | (if (and (eq py-honor-comment-indentation nil) |
Barry Warsaw | 33d6ec0 | 1996-03-05 16:28:07 +0000 | [diff] [blame] | 2272 | (fboundp 'forward-comment)) |
Barry Warsaw | fd0fb38 | 1996-03-04 17:15:40 +0000 | [diff] [blame] | 2273 | (forward-comment (- (point-max))) |
Barry Warsaw | 218eb75 | 1998-09-22 19:51:47 +0000 | [diff] [blame] | 2274 | (let ((prefix-re (concat py-block-comment-prefix "[ \t]*")) |
| 2275 | done) |
Barry Warsaw | 6d62775 | 1996-03-06 18:41:38 +0000 | [diff] [blame] | 2276 | (while (not done) |
Barry Warsaw | 12c9294 | 1998-08-10 21:44:37 +0000 | [diff] [blame] | 2277 | (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#\\)" nil 'move) |
| 2278 | (setq done (or (bobp) |
| 2279 | (and (eq py-honor-comment-indentation t) |
| 2280 | (save-excursion |
| 2281 | (back-to-indentation) |
Barry Warsaw | 218eb75 | 1998-09-22 19:51:47 +0000 | [diff] [blame] | 2282 | (not (looking-at prefix-re)) |
Barry Warsaw | 12c9294 | 1998-08-10 21:44:37 +0000 | [diff] [blame] | 2283 | )) |
| 2284 | (and (not (eq py-honor-comment-indentation t)) |
| 2285 | (save-excursion |
| 2286 | (back-to-indentation) |
Barry Warsaw | d36cfe4 | 2002-03-15 16:46:46 +0000 | [diff] [blame] | 2287 | (and (not (looking-at prefix-re)) |
| 2288 | (or (looking-at "[^#]") |
| 2289 | (not (zerop (current-column))) |
| 2290 | )) |
| 2291 | )) |
Barry Warsaw | 12c9294 | 1998-08-10 21:44:37 +0000 | [diff] [blame] | 2292 | )) |
Barry Warsaw | 6d62775 | 1996-03-06 18:41:38 +0000 | [diff] [blame] | 2293 | ))) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 2294 | ;; if we landed inside a string, go to the beginning of that |
| 2295 | ;; string. this handles triple quoted, multi-line spanning |
| 2296 | ;; strings. |
Barry Warsaw | 9c1696c | 1998-12-15 04:36:22 +0000 | [diff] [blame] | 2297 | (py-goto-beginning-of-tqs (nth 3 (parse-partial-sexp bod (point)))) |
Barry Warsaw | c210e69 | 1998-01-20 22:52:56 +0000 | [diff] [blame] | 2298 | ;; now skip backward over continued lines |
Barry Warsaw | 9c1696c | 1998-12-15 04:36:22 +0000 | [diff] [blame] | 2299 | (setq placeholder (point)) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 2300 | (py-goto-initial-line) |
Barry Warsaw | 9c1696c | 1998-12-15 04:36:22 +0000 | [diff] [blame] | 2301 | ;; we may *now* have landed in a TQS, so find the beginning of |
| 2302 | ;; this string. |
| 2303 | (py-goto-beginning-of-tqs |
| 2304 | (save-excursion (nth 3 (parse-partial-sexp |
| 2305 | placeholder (point))))) |
Barry Warsaw | f831d81 | 1996-07-31 20:42:59 +0000 | [diff] [blame] | 2306 | (+ (current-indentation) |
| 2307 | (if (py-statement-opens-block-p) |
| 2308 | py-indent-offset |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 2309 | (if (and honor-block-close-p (py-statement-closes-block-p)) |
Barry Warsaw | f831d81 | 1996-07-31 20:42:59 +0000 | [diff] [blame] | 2310 | (- py-indent-offset) |
| 2311 | 0))) |
| 2312 | ))))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2313 | |
| 2314 | (defun py-guess-indent-offset (&optional global) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2315 | "Guess a good value for, and change, `py-indent-offset'. |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2316 | |
| 2317 | By default, make a buffer-local copy of `py-indent-offset' with the |
| 2318 | new value, so that other Python buffers are not affected. With |
| 2319 | \\[universal-argument] (programmatically, optional argument GLOBAL), |
| 2320 | change the global value of `py-indent-offset'. This affects all |
| 2321 | Python buffers (that don't have their own buffer-local copy), both |
| 2322 | those currently existing and those created later in the Emacs session. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2323 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2324 | Some people use a different value for `py-indent-offset' than you use. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2325 | There's no excuse for such foolishness, but sometimes you have to deal |
| 2326 | with their ugly code anyway. This function examines the file and sets |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2327 | `py-indent-offset' to what it thinks it was when they created the |
| 2328 | mess. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2329 | |
| 2330 | Specifically, it searches forward from the statement containing point, |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2331 | looking for a line that opens a block of code. `py-indent-offset' is |
| 2332 | set to the difference in indentation between that line and the Python |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2333 | statement following it. If the search doesn't succeed going forward, |
| 2334 | it's tried again going backward." |
| 2335 | (interactive "P") ; raw prefix arg |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2336 | (let (new-value |
| 2337 | (start (point)) |
Barry Warsaw | e908b6b | 1998-03-19 22:48:02 +0000 | [diff] [blame] | 2338 | (restart (point)) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2339 | (found nil) |
| 2340 | colon-indent) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2341 | (py-goto-initial-line) |
| 2342 | (while (not (or found (eobp))) |
Barry Warsaw | e908b6b | 1998-03-19 22:48:02 +0000 | [diff] [blame] | 2343 | (when (and (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move) |
| 2344 | (not (py-in-literal restart))) |
| 2345 | (setq restart (point)) |
| 2346 | (py-goto-initial-line) |
| 2347 | (if (py-statement-opens-block-p) |
| 2348 | (setq found t) |
| 2349 | (goto-char restart)))) |
| 2350 | (unless found |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2351 | (goto-char start) |
| 2352 | (py-goto-initial-line) |
| 2353 | (while (not (or found (bobp))) |
Barry Warsaw | e908b6b | 1998-03-19 22:48:02 +0000 | [diff] [blame] | 2354 | (setq found (and |
| 2355 | (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move) |
| 2356 | (or (py-goto-initial-line) t) ; always true -- side effect |
| 2357 | (py-statement-opens-block-p))))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2358 | (setq colon-indent (current-indentation) |
| 2359 | found (and found (zerop (py-next-statement 1))) |
| 2360 | new-value (- (current-indentation) colon-indent)) |
| 2361 | (goto-char start) |
Barry Warsaw | e908b6b | 1998-03-19 22:48:02 +0000 | [diff] [blame] | 2362 | (if (not found) |
| 2363 | (error "Sorry, couldn't guess a value for py-indent-offset") |
| 2364 | (funcall (if global 'kill-local-variable 'make-local-variable) |
| 2365 | 'py-indent-offset) |
| 2366 | (setq py-indent-offset new-value) |
Barry Warsaw | d35c255 | 1998-09-25 00:08:38 +0000 | [diff] [blame] | 2367 | (or noninteractive |
| 2368 | (message "%s value of py-indent-offset set to %d" |
| 2369 | (if global "Global" "Local") |
| 2370 | py-indent-offset))) |
Barry Warsaw | e908b6b | 1998-03-19 22:48:02 +0000 | [diff] [blame] | 2371 | )) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2372 | |
Barry Warsaw | 003932a | 1998-07-07 15:11:24 +0000 | [diff] [blame] | 2373 | (defun py-comment-indent-function () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2374 | "Python version of `comment-indent-function'." |
| 2375 | ;; This is required when filladapt is turned off. Without it, when |
| 2376 | ;; filladapt is not used, comments which start in column zero |
| 2377 | ;; cascade one character to the right |
Barry Warsaw | 003932a | 1998-07-07 15:11:24 +0000 | [diff] [blame] | 2378 | (save-excursion |
| 2379 | (beginning-of-line) |
| 2380 | (let ((eol (py-point 'eol))) |
| 2381 | (and comment-start-skip |
| 2382 | (re-search-forward comment-start-skip eol t) |
| 2383 | (setq eol (match-beginning 0))) |
| 2384 | (goto-char eol) |
| 2385 | (skip-chars-backward " \t") |
| 2386 | (max comment-column (+ (current-column) (if (bolp) 0 1))) |
| 2387 | ))) |
| 2388 | |
Barry Warsaw | f8ddb6a | 1999-01-18 21:49:39 +0000 | [diff] [blame] | 2389 | (defun py-narrow-to-defun (&optional class) |
| 2390 | "Make text outside current defun invisible. |
| 2391 | The defun visible is the one that contains point or follows point. |
| 2392 | Optional CLASS is passed directly to `py-beginning-of-def-or-class'." |
| 2393 | (interactive "P") |
| 2394 | (save-excursion |
| 2395 | (widen) |
| 2396 | (py-end-of-def-or-class class) |
| 2397 | (let ((end (point))) |
| 2398 | (py-beginning-of-def-or-class class) |
| 2399 | (narrow-to-region (point) end)))) |
| 2400 | |
Barry Warsaw | 003932a | 1998-07-07 15:11:24 +0000 | [diff] [blame] | 2401 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2402 | (defun py-shift-region (start end count) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2403 | "Indent lines from START to END by COUNT spaces." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2404 | (save-excursion |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2405 | (goto-char end) |
| 2406 | (beginning-of-line) |
| 2407 | (setq end (point)) |
| 2408 | (goto-char start) |
| 2409 | (beginning-of-line) |
| 2410 | (setq start (point)) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2411 | (indent-rigidly start end count))) |
| 2412 | |
| 2413 | (defun py-shift-region-left (start end &optional count) |
| 2414 | "Shift region of Python code to the left. |
| 2415 | The lines from the line containing the start of the current region up |
| 2416 | to (but not including) the line containing the end of the region are |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2417 | shifted to the left, by `py-indent-offset' columns. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2418 | |
| 2419 | If a prefix argument is given, the region is instead shifted by that |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2420 | many columns. With no active region, dedent only the current line. |
| 2421 | You cannot dedent the region if any line is already at column zero." |
Barry Warsaw | dea4a29 | 1996-07-03 22:59:12 +0000 | [diff] [blame] | 2422 | (interactive |
| 2423 | (let ((p (point)) |
| 2424 | (m (mark)) |
| 2425 | (arg current-prefix-arg)) |
| 2426 | (if m |
| 2427 | (list (min p m) (max p m) arg) |
| 2428 | (list p (save-excursion (forward-line 1) (point)) arg)))) |
| 2429 | ;; if any line is at column zero, don't shift the region |
| 2430 | (save-excursion |
| 2431 | (goto-char start) |
| 2432 | (while (< (point) end) |
| 2433 | (back-to-indentation) |
Barry Warsaw | 71e315b | 1996-07-23 15:03:16 +0000 | [diff] [blame] | 2434 | (if (and (zerop (current-column)) |
| 2435 | (not (looking-at "\\s *$"))) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2436 | (error "Region is at left edge")) |
Barry Warsaw | dea4a29 | 1996-07-03 22:59:12 +0000 | [diff] [blame] | 2437 | (forward-line 1))) |
| 2438 | (py-shift-region start end (- (prefix-numeric-value |
| 2439 | (or count py-indent-offset)))) |
| 2440 | (py-keep-region-active)) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2441 | |
| 2442 | (defun py-shift-region-right (start end &optional count) |
| 2443 | "Shift region of Python code to the right. |
| 2444 | The lines from the line containing the start of the current region up |
| 2445 | to (but not including) the line containing the end of the region are |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2446 | shifted to the right, by `py-indent-offset' columns. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2447 | |
| 2448 | If a prefix argument is given, the region is instead shifted by that |
Barry Warsaw | dea4a29 | 1996-07-03 22:59:12 +0000 | [diff] [blame] | 2449 | many columns. With no active region, indent only the current line." |
| 2450 | (interactive |
| 2451 | (let ((p (point)) |
| 2452 | (m (mark)) |
| 2453 | (arg current-prefix-arg)) |
| 2454 | (if m |
| 2455 | (list (min p m) (max p m) arg) |
| 2456 | (list p (save-excursion (forward-line 1) (point)) arg)))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2457 | (py-shift-region start end (prefix-numeric-value |
Barry Warsaw | dea4a29 | 1996-07-03 22:59:12 +0000 | [diff] [blame] | 2458 | (or count py-indent-offset))) |
| 2459 | (py-keep-region-active)) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2460 | |
| 2461 | (defun py-indent-region (start end &optional indent-offset) |
| 2462 | "Reindent a region of Python code. |
Barry Warsaw | 867a32a | 1996-03-07 18:30:26 +0000 | [diff] [blame] | 2463 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2464 | The lines from the line containing the start of the current region up |
| 2465 | to (but not including) the line containing the end of the region are |
| 2466 | reindented. If the first line of the region has a non-whitespace |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2467 | character in the first column, the first line is left alone and the |
| 2468 | rest of the region is reindented with respect to it. Else the entire |
Barry Warsaw | 867a32a | 1996-03-07 18:30:26 +0000 | [diff] [blame] | 2469 | region is reindented with respect to the (closest code or indenting |
| 2470 | comment) statement immediately preceding the region. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2471 | |
| 2472 | This is useful when code blocks are moved or yanked, when enclosing |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2473 | control structures are introduced or removed, or to reformat code |
| 2474 | using a new value for the indentation offset. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2475 | |
| 2476 | If a numeric prefix argument is given, it will be used as the value of |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2477 | the indentation offset. Else the value of `py-indent-offset' will be |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2478 | used. |
| 2479 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2480 | Warning: The region must be consistently indented before this function |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2481 | is called! This function does not compute proper indentation from |
| 2482 | scratch (that's impossible in Python), it merely adjusts the existing |
| 2483 | indentation to be correct in context. |
| 2484 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2485 | Warning: This function really has no idea what to do with |
| 2486 | non-indenting comment lines, and shifts them as if they were indenting |
| 2487 | comment lines. Fixing this appears to require telepathy. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2488 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2489 | Special cases: whitespace is deleted from blank lines; continuation |
| 2490 | lines are shifted by the same amount their initial line was shifted, |
| 2491 | in order to preserve their relative indentation with respect to their |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2492 | initial line; and comment lines beginning in column 1 are ignored." |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2493 | (interactive "*r\nP") ; region; raw prefix arg |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2494 | (save-excursion |
| 2495 | (goto-char end) (beginning-of-line) (setq end (point-marker)) |
| 2496 | (goto-char start) (beginning-of-line) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2497 | (let ((py-indent-offset (prefix-numeric-value |
| 2498 | (or indent-offset py-indent-offset))) |
| 2499 | (indents '(-1)) ; stack of active indent levels |
| 2500 | (target-column 0) ; column to which to indent |
| 2501 | (base-shifted-by 0) ; amount last base line was shifted |
| 2502 | (indent-base (if (looking-at "[ \t\n]") |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 2503 | (py-compute-indentation t) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2504 | 0)) |
| 2505 | ci) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2506 | (while (< (point) end) |
| 2507 | (setq ci (current-indentation)) |
| 2508 | ;; figure out appropriate target column |
| 2509 | (cond |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2510 | ((or (eq (following-char) ?#) ; comment in column 1 |
| 2511 | (looking-at "[ \t]*$")) ; entirely blank |
| 2512 | (setq target-column 0)) |
| 2513 | ((py-continuation-line-p) ; shift relative to base line |
| 2514 | (setq target-column (+ ci base-shifted-by))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2515 | (t ; new base line |
| 2516 | (if (> ci (car indents)) ; going deeper; push it |
| 2517 | (setq indents (cons ci indents)) |
| 2518 | ;; else we should have seen this indent before |
| 2519 | (setq indents (memq ci indents)) ; pop deeper indents |
| 2520 | (if (null indents) |
| 2521 | (error "Bad indentation in region, at line %d" |
| 2522 | (save-restriction |
| 2523 | (widen) |
| 2524 | (1+ (count-lines 1 (point))))))) |
| 2525 | (setq target-column (+ indent-base |
| 2526 | (* py-indent-offset |
| 2527 | (- (length indents) 2)))) |
| 2528 | (setq base-shifted-by (- target-column ci)))) |
| 2529 | ;; shift as needed |
| 2530 | (if (/= ci target-column) |
| 2531 | (progn |
| 2532 | (delete-horizontal-space) |
| 2533 | (indent-to target-column))) |
| 2534 | (forward-line 1)))) |
| 2535 | (set-marker end nil)) |
| 2536 | |
Barry Warsaw | a789171 | 1996-08-01 15:53:09 +0000 | [diff] [blame] | 2537 | (defun py-comment-region (beg end &optional arg) |
| 2538 | "Like `comment-region' but uses double hash (`#') comment starter." |
| 2539 | (interactive "r\nP") |
Barry Warsaw | 3fcaf61 | 1996-08-01 20:11:51 +0000 | [diff] [blame] | 2540 | (let ((comment-start py-block-comment-prefix)) |
Barry Warsaw | a789171 | 1996-08-01 15:53:09 +0000 | [diff] [blame] | 2541 | (comment-region beg end arg))) |
| 2542 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2543 | |
| 2544 | ;; Functions for moving point |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2545 | (defun py-previous-statement (count) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2546 | "Go to the start of the COUNTth preceding Python statement. |
| 2547 | By default, goes to the previous statement. If there is no such |
| 2548 | statement, goes to the first statement. Return count of statements |
| 2549 | left to move. `Statements' do not include blank, comment, or |
| 2550 | continuation lines." |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2551 | (interactive "p") ; numeric prefix arg |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2552 | (if (< count 0) (py-next-statement (- count)) |
| 2553 | (py-goto-initial-line) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2554 | (let (start) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2555 | (while (and |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2556 | (setq start (point)) ; always true -- side effect |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2557 | (> count 0) |
| 2558 | (zerop (forward-line -1)) |
| 2559 | (py-goto-statement-at-or-above)) |
| 2560 | (setq count (1- count))) |
| 2561 | (if (> count 0) (goto-char start))) |
| 2562 | count)) |
| 2563 | |
| 2564 | (defun py-next-statement (count) |
| 2565 | "Go to the start of next Python statement. |
| 2566 | If the statement at point is the i'th Python statement, goes to the |
| 2567 | start of statement i+COUNT. If there is no such statement, goes to the |
| 2568 | last statement. Returns count of statements left to move. `Statements' |
| 2569 | do not include blank, comment, or continuation lines." |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2570 | (interactive "p") ; numeric prefix arg |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2571 | (if (< count 0) (py-previous-statement (- count)) |
| 2572 | (beginning-of-line) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2573 | (let (start) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2574 | (while (and |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2575 | (setq start (point)) ; always true -- side effect |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2576 | (> count 0) |
| 2577 | (py-goto-statement-below)) |
| 2578 | (setq count (1- count))) |
| 2579 | (if (> count 0) (goto-char start))) |
| 2580 | count)) |
| 2581 | |
| 2582 | (defun py-goto-block-up (&optional nomark) |
| 2583 | "Move up to start of current block. |
| 2584 | Go to the statement that starts the smallest enclosing block; roughly |
| 2585 | speaking, this will be the closest preceding statement that ends with a |
| 2586 | colon and is indented less than the statement you started on. If |
| 2587 | successful, also sets the mark to the starting point. |
| 2588 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2589 | `\\[py-mark-block]' can be used afterward to mark the whole code |
| 2590 | block, if desired. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2591 | |
| 2592 | If called from a program, the mark will not be set if optional argument |
| 2593 | NOMARK is not nil." |
| 2594 | (interactive) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2595 | (let ((start (point)) |
| 2596 | (found nil) |
| 2597 | initial-indent) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2598 | (py-goto-initial-line) |
| 2599 | ;; if on blank or non-indenting comment line, use the preceding stmt |
| 2600 | (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)") |
| 2601 | (progn |
| 2602 | (py-goto-statement-at-or-above) |
| 2603 | (setq found (py-statement-opens-block-p)))) |
| 2604 | ;; search back for colon line indented less |
| 2605 | (setq initial-indent (current-indentation)) |
| 2606 | (if (zerop initial-indent) |
| 2607 | ;; force fast exit |
| 2608 | (goto-char (point-min))) |
| 2609 | (while (not (or found (bobp))) |
| 2610 | (setq found |
| 2611 | (and |
| 2612 | (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move) |
| 2613 | (or (py-goto-initial-line) t) ; always true -- side effect |
| 2614 | (< (current-indentation) initial-indent) |
| 2615 | (py-statement-opens-block-p)))) |
| 2616 | (if found |
| 2617 | (progn |
| 2618 | (or nomark (push-mark start)) |
| 2619 | (back-to-indentation)) |
| 2620 | (goto-char start) |
| 2621 | (error "Enclosing block not found")))) |
| 2622 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2623 | (defun py-beginning-of-def-or-class (&optional class count) |
| 2624 | "Move point to start of `def' or `class'. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2625 | |
| 2626 | Searches back for the closest preceding `def'. If you supply a prefix |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2627 | arg, looks for a `class' instead. The docs below assume the `def' |
| 2628 | case; just substitute `class' for `def' for the other case. |
Barry Warsaw | 7c29b23 | 1998-07-07 17:45:38 +0000 | [diff] [blame] | 2629 | Programmatically, if CLASS is `either', then moves to either `class' |
| 2630 | or `def'. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2631 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2632 | When second optional argument is given programmatically, move to the |
| 2633 | COUNTth start of `def'. |
| 2634 | |
| 2635 | If point is in a `def' statement already, and after the `d', simply |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2636 | moves point to the start of the statement. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2637 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2638 | Otherwise (i.e. when point is not in a `def' statement, or at or |
| 2639 | before the `d' of a `def' statement), searches for the closest |
| 2640 | preceding `def' statement, and leaves point at its start. If no such |
| 2641 | statement can be found, leaves point at the start of the buffer. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2642 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2643 | Returns t iff a `def' statement is found by these rules. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2644 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2645 | Note that doing this command repeatedly will take you closer to the |
| 2646 | start of the buffer each time. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2647 | |
Barry Warsaw | 7c29b23 | 1998-07-07 17:45:38 +0000 | [diff] [blame] | 2648 | To mark the current `def', see `\\[py-mark-def-or-class]'." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2649 | (interactive "P") ; raw prefix arg |
Barry Warsaw | 6839d3a | 1998-10-28 00:10:45 +0000 | [diff] [blame] | 2650 | (setq count (or count 1)) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2651 | (let ((at-or-before-p (<= (current-column) (current-indentation))) |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2652 | (start-of-line (goto-char (py-point 'bol))) |
| 2653 | (start-of-stmt (goto-char (py-point 'bos))) |
Barry Warsaw | 7c29b23 | 1998-07-07 17:45:38 +0000 | [diff] [blame] | 2654 | (start-re (cond ((eq class 'either) "^[ \t]*\\(class\\|def\\)\\>") |
| 2655 | (class "^[ \t]*class\\>") |
| 2656 | (t "^[ \t]*def\\>"))) |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2657 | ) |
| 2658 | ;; searching backward |
| 2659 | (if (and (< 0 count) |
| 2660 | (or (/= start-of-stmt start-of-line) |
| 2661 | (not at-or-before-p))) |
| 2662 | (end-of-line)) |
| 2663 | ;; search forward |
| 2664 | (if (and (> 0 count) |
| 2665 | (zerop (current-column)) |
| 2666 | (looking-at start-re)) |
| 2667 | (end-of-line)) |
Barry Warsaw | ddc4696 | 1999-07-27 21:40:02 +0000 | [diff] [blame] | 2668 | (if (re-search-backward start-re nil 'move count) |
| 2669 | (goto-char (match-beginning 0))))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2670 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2671 | ;; Backwards compatibility |
| 2672 | (defalias 'beginning-of-python-def-or-class 'py-beginning-of-def-or-class) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2673 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2674 | (defun py-end-of-def-or-class (&optional class count) |
| 2675 | "Move point beyond end of `def' or `class' body. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2676 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2677 | By default, looks for an appropriate `def'. If you supply a prefix |
| 2678 | arg, looks for a `class' instead. The docs below assume the `def' |
| 2679 | case; just substitute `class' for `def' for the other case. |
Barry Warsaw | 7c29b23 | 1998-07-07 17:45:38 +0000 | [diff] [blame] | 2680 | Programmatically, if CLASS is `either', then moves to either `class' |
| 2681 | or `def'. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2682 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2683 | When second optional argument is given programmatically, move to the |
| 2684 | COUNTth end of `def'. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2685 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2686 | If point is in a `def' statement already, this is the `def' we use. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2687 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2688 | Else, if the `def' found by `\\[py-beginning-of-def-or-class]' |
| 2689 | contains the statement you started on, that's the `def' we use. |
| 2690 | |
| 2691 | Otherwise, we search forward for the closest following `def', and use that. |
| 2692 | |
| 2693 | If a `def' can be found by these rules, point is moved to the start of |
| 2694 | the line immediately following the `def' block, and the position of the |
| 2695 | start of the `def' is returned. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2696 | |
| 2697 | Else point is moved to the end of the buffer, and nil is returned. |
| 2698 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2699 | Note that doing this command repeatedly will take you closer to the |
| 2700 | end of the buffer each time. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2701 | |
Barry Warsaw | 7c29b23 | 1998-07-07 17:45:38 +0000 | [diff] [blame] | 2702 | To mark the current `def', see `\\[py-mark-def-or-class]'." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2703 | (interactive "P") ; raw prefix arg |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2704 | (if (and count (/= count 1)) |
| 2705 | (py-beginning-of-def-or-class (- 1 count))) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2706 | (let ((start (progn (py-goto-initial-line) (point))) |
Barry Warsaw | 7c29b23 | 1998-07-07 17:45:38 +0000 | [diff] [blame] | 2707 | (which (cond ((eq class 'either) "\\(class\\|def\\)") |
| 2708 | (class "class") |
| 2709 | (t "def"))) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2710 | (state 'not-found)) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2711 | ;; move point to start of appropriate def/class |
| 2712 | (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one |
| 2713 | (setq state 'at-beginning) |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2714 | ;; else see if py-beginning-of-def-or-class hits container |
| 2715 | (if (and (py-beginning-of-def-or-class class) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2716 | (progn (py-goto-beyond-block) |
| 2717 | (> (point) start))) |
| 2718 | (setq state 'at-end) |
| 2719 | ;; else search forward |
| 2720 | (goto-char start) |
| 2721 | (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move) |
| 2722 | (progn (setq state 'at-beginning) |
| 2723 | (beginning-of-line))))) |
| 2724 | (cond |
| 2725 | ((eq state 'at-beginning) (py-goto-beyond-block) t) |
| 2726 | ((eq state 'at-end) t) |
| 2727 | ((eq state 'not-found) nil) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2728 | (t (error "Internal error in `py-end-of-def-or-class'"))))) |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2729 | |
| 2730 | ;; Backwards compabitility |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 2731 | (defalias 'end-of-python-def-or-class 'py-end-of-def-or-class) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2732 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2733 | |
| 2734 | ;; Functions for marking regions |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2735 | (defun py-mark-block (&optional extend just-move) |
| 2736 | "Mark following block of lines. With prefix arg, mark structure. |
| 2737 | Easier to use than explain. It sets the region to an `interesting' |
| 2738 | block of succeeding lines. If point is on a blank line, it goes down to |
| 2739 | the next non-blank line. That will be the start of the region. The end |
| 2740 | of the region depends on the kind of line at the start: |
| 2741 | |
| 2742 | - If a comment, the region will include all succeeding comment lines up |
| 2743 | to (but not including) the next non-comment line (if any). |
| 2744 | |
| 2745 | - Else if a prefix arg is given, and the line begins one of these |
| 2746 | structures: |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2747 | |
| 2748 | if elif else try except finally for while def class |
| 2749 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2750 | the region will be set to the body of the structure, including |
| 2751 | following blocks that `belong' to it, but excluding trailing blank |
| 2752 | and comment lines. E.g., if on a `try' statement, the `try' block |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2753 | and all (if any) of the following `except' and `finally' blocks |
| 2754 | that belong to the `try' structure will be in the region. Ditto |
| 2755 | for if/elif/else, for/else and while/else structures, and (a bit |
| 2756 | degenerate, since they're always one-block structures) def and |
| 2757 | class blocks. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2758 | |
| 2759 | - Else if no prefix argument is given, and the line begins a Python |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2760 | block (see list above), and the block is not a `one-liner' (i.e., |
| 2761 | the statement ends with a colon, not with code), the region will |
| 2762 | include all succeeding lines up to (but not including) the next |
| 2763 | code statement (if any) that's indented no more than the starting |
| 2764 | line, except that trailing blank and comment lines are excluded. |
| 2765 | E.g., if the starting line begins a multi-statement `def' |
| 2766 | structure, the region will be set to the full function definition, |
| 2767 | but without any trailing `noise' lines. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2768 | |
| 2769 | - Else the region will include all succeeding lines up to (but not |
| 2770 | including) the next blank line, or code or indenting-comment line |
| 2771 | indented strictly less than the starting line. Trailing indenting |
| 2772 | comment lines are included in this case, but not trailing blank |
| 2773 | lines. |
| 2774 | |
| 2775 | A msg identifying the location of the mark is displayed in the echo |
| 2776 | area; or do `\\[exchange-point-and-mark]' to flip down to the end. |
| 2777 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2778 | If called from a program, optional argument EXTEND plays the role of |
| 2779 | the prefix arg, and if optional argument JUST-MOVE is not nil, just |
| 2780 | moves to the end of the block (& does not set mark or display a msg)." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2781 | (interactive "P") ; raw prefix arg |
| 2782 | (py-goto-initial-line) |
| 2783 | ;; skip over blank lines |
| 2784 | (while (and |
| 2785 | (looking-at "[ \t]*$") ; while blank line |
| 2786 | (not (eobp))) ; & somewhere to go |
| 2787 | (forward-line 1)) |
| 2788 | (if (eobp) |
| 2789 | (error "Hit end of buffer without finding a non-blank stmt")) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2790 | (let ((initial-pos (point)) |
| 2791 | (initial-indent (current-indentation)) |
| 2792 | last-pos ; position of last stmt in region |
| 2793 | (followers |
| 2794 | '((if elif else) (elif elif else) (else) |
| 2795 | (try except finally) (except except) (finally) |
| 2796 | (for else) (while else) |
| 2797 | (def) (class) ) ) |
| 2798 | first-symbol next-symbol) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2799 | |
| 2800 | (cond |
| 2801 | ;; if comment line, suck up the following comment lines |
| 2802 | ((looking-at "[ \t]*#") |
| 2803 | (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment |
| 2804 | (re-search-backward "^[ \t]*#") ; and back to last comment in block |
| 2805 | (setq last-pos (point))) |
| 2806 | |
| 2807 | ;; else if line is a block line and EXTEND given, suck up |
| 2808 | ;; the whole structure |
| 2809 | ((and extend |
| 2810 | (setq first-symbol (py-suck-up-first-keyword) ) |
| 2811 | (assq first-symbol followers)) |
| 2812 | (while (and |
| 2813 | (or (py-goto-beyond-block) t) ; side effect |
| 2814 | (forward-line -1) ; side effect |
| 2815 | (setq last-pos (point)) ; side effect |
| 2816 | (py-goto-statement-below) |
| 2817 | (= (current-indentation) initial-indent) |
| 2818 | (setq next-symbol (py-suck-up-first-keyword)) |
| 2819 | (memq next-symbol (cdr (assq first-symbol followers)))) |
| 2820 | (setq first-symbol next-symbol))) |
| 2821 | |
| 2822 | ;; else if line *opens* a block, search for next stmt indented <= |
| 2823 | ((py-statement-opens-block-p) |
| 2824 | (while (and |
| 2825 | (setq last-pos (point)) ; always true -- side effect |
| 2826 | (py-goto-statement-below) |
Barry Warsaw | f070cce | 2002-05-23 19:42:16 +0000 | [diff] [blame] | 2827 | (> (current-indentation) initial-indent) |
| 2828 | ))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2829 | |
| 2830 | ;; else plain code line; stop at next blank line, or stmt or |
| 2831 | ;; indenting comment line indented < |
| 2832 | (t |
| 2833 | (while (and |
| 2834 | (setq last-pos (point)) ; always true -- side effect |
| 2835 | (or (py-goto-beyond-final-line) t) |
| 2836 | (not (looking-at "[ \t]*$")) ; stop at blank line |
| 2837 | (or |
| 2838 | (>= (current-indentation) initial-indent) |
| 2839 | (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting # |
| 2840 | nil))) |
| 2841 | |
| 2842 | ;; skip to end of last stmt |
| 2843 | (goto-char last-pos) |
| 2844 | (py-goto-beyond-final-line) |
| 2845 | |
| 2846 | ;; set mark & display |
| 2847 | (if just-move |
| 2848 | () ; just return |
| 2849 | (push-mark (point) 'no-msg) |
| 2850 | (forward-line -1) |
| 2851 | (message "Mark set after: %s" (py-suck-up-leading-text)) |
| 2852 | (goto-char initial-pos)))) |
| 2853 | |
Barry Warsaw | 2518c67 | 1997-11-05 00:51:08 +0000 | [diff] [blame] | 2854 | (defun py-mark-def-or-class (&optional class) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2855 | "Set region to body of def (or class, with prefix arg) enclosing point. |
| 2856 | Pushes the current mark, then point, on the mark ring (all language |
| 2857 | modes do this, but although it's handy it's never documented ...). |
| 2858 | |
| 2859 | In most Emacs language modes, this function bears at least a |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2860 | hallucinogenic resemblance to `\\[py-end-of-def-or-class]' and |
| 2861 | `\\[py-beginning-of-def-or-class]'. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2862 | |
| 2863 | And in earlier versions of Python mode, all 3 were tightly connected. |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2864 | Turned out that was more confusing than useful: the `goto start' and |
| 2865 | `goto end' commands are usually used to search through a file, and |
| 2866 | people expect them to act a lot like `search backward' and `search |
| 2867 | forward' string-search commands. But because Python `def' and `class' |
| 2868 | can nest to arbitrary levels, finding the smallest def containing |
| 2869 | point cannot be done via a simple backward search: the def containing |
| 2870 | point may not be the closest preceding def, or even the closest |
| 2871 | preceding def that's indented less. The fancy algorithm required is |
| 2872 | appropriate for the usual uses of this `mark' command, but not for the |
| 2873 | `goto' variations. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2874 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2875 | So the def marked by this command may not be the one either of the |
| 2876 | `goto' commands find: If point is on a blank or non-indenting comment |
| 2877 | line, moves back to start of the closest preceding code statement or |
| 2878 | indenting comment line. If this is a `def' statement, that's the def |
| 2879 | we use. Else searches for the smallest enclosing `def' block and uses |
| 2880 | that. Else signals an error. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2881 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2882 | When an enclosing def is found: The mark is left immediately beyond |
| 2883 | the last line of the def block. Point is left at the start of the |
| 2884 | def, except that: if the def is preceded by a number of comment lines |
| 2885 | followed by (at most) one optional blank line, point is left at the |
| 2886 | start of the comments; else if the def is preceded by a blank line, |
| 2887 | point is left at its start. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2888 | |
| 2889 | The intent is to mark the containing def/class and its associated |
| 2890 | documentation, to make moving and duplicating functions and classes |
| 2891 | pleasant." |
| 2892 | (interactive "P") ; raw prefix arg |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2893 | (let ((start (point)) |
Barry Warsaw | 7c29b23 | 1998-07-07 17:45:38 +0000 | [diff] [blame] | 2894 | (which (cond ((eq class 'either) "\\(class\\|def\\)") |
| 2895 | (class "class") |
| 2896 | (t "def")))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2897 | (push-mark start) |
| 2898 | (if (not (py-go-up-tree-to-keyword which)) |
| 2899 | (progn (goto-char start) |
Barry Warsaw | 7c29b23 | 1998-07-07 17:45:38 +0000 | [diff] [blame] | 2900 | (error "Enclosing %s not found" |
| 2901 | (if (eq class 'either) |
| 2902 | "def or class" |
| 2903 | which))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2904 | ;; else enclosing def/class found |
| 2905 | (setq start (point)) |
| 2906 | (py-goto-beyond-block) |
| 2907 | (push-mark (point)) |
| 2908 | (goto-char start) |
| 2909 | (if (zerop (forward-line -1)) ; if there is a preceding line |
| 2910 | (progn |
| 2911 | (if (looking-at "[ \t]*$") ; it's blank |
| 2912 | (setq start (point)) ; so reset start point |
| 2913 | (goto-char start)) ; else try again |
| 2914 | (if (zerop (forward-line -1)) |
| 2915 | (if (looking-at "[ \t]*#") ; a comment |
| 2916 | ;; look back for non-comment line |
| 2917 | ;; tricky: note that the regexp matches a blank |
| 2918 | ;; line, cuz \n is in the 2nd character class |
| 2919 | (and |
| 2920 | (re-search-backward "^[ \t]*[^ \t#]" nil 'move) |
| 2921 | (forward-line 1)) |
| 2922 | ;; no comment, so go back |
Barry Warsaw | 4da6bd5 | 1997-11-26 06:00:26 +0000 | [diff] [blame] | 2923 | (goto-char start))))))) |
| 2924 | (exchange-point-and-mark) |
| 2925 | (py-keep-region-active)) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2926 | |
Barry Warsaw | 9e5a9c8 | 1996-07-24 18:26:53 +0000 | [diff] [blame] | 2927 | ;; ripped from cc-mode |
| 2928 | (defun py-forward-into-nomenclature (&optional arg) |
| 2929 | "Move forward to end of a nomenclature section or word. |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 2930 | With \\[universal-argument] (programmatically, optional argument ARG), |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 2931 | do it that many times. |
Barry Warsaw | 9e5a9c8 | 1996-07-24 18:26:53 +0000 | [diff] [blame] | 2932 | |
| 2933 | A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores." |
| 2934 | (interactive "p") |
| 2935 | (let ((case-fold-search nil)) |
| 2936 | (if (> arg 0) |
Barry Warsaw | c5a8cbd | 1996-08-05 21:53:02 +0000 | [diff] [blame] | 2937 | (re-search-forward |
| 2938 | "\\(\\W\\|[_]\\)*\\([A-Z]*[a-z0-9]*\\)" |
| 2939 | (point-max) t arg) |
Barry Warsaw | 9e5a9c8 | 1996-07-24 18:26:53 +0000 | [diff] [blame] | 2940 | (while (and (< arg 0) |
| 2941 | (re-search-backward |
Barry Warsaw | c5a8cbd | 1996-08-05 21:53:02 +0000 | [diff] [blame] | 2942 | "\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\(\\W\\|[_]\\)\\w+" |
Barry Warsaw | 9e5a9c8 | 1996-07-24 18:26:53 +0000 | [diff] [blame] | 2943 | (point-min) 0)) |
| 2944 | (forward-char 1) |
| 2945 | (setq arg (1+ arg))))) |
| 2946 | (py-keep-region-active)) |
| 2947 | |
| 2948 | (defun py-backward-into-nomenclature (&optional arg) |
| 2949 | "Move backward to beginning of a nomenclature section or word. |
| 2950 | With optional ARG, move that many times. If ARG is negative, move |
| 2951 | forward. |
| 2952 | |
| 2953 | A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores." |
| 2954 | (interactive "p") |
| 2955 | (py-forward-into-nomenclature (- arg)) |
| 2956 | (py-keep-region-active)) |
| 2957 | |
| 2958 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2959 | |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 2960 | ;; pdbtrack functions |
| 2961 | (defun py-pdbtrack-toggle-stack-tracking (arg) |
| 2962 | (interactive "P") |
| 2963 | (if (not (get-buffer-process (current-buffer))) |
| 2964 | (error "No process associated with buffer '%s'" (current-buffer))) |
| 2965 | ;; missing or 0 is toggle, >0 turn on, <0 turn off |
| 2966 | (if (or (not arg) |
| 2967 | (zerop (setq arg (prefix-numeric-value arg)))) |
| 2968 | (setq py-pdbtrack-do-tracking-p (not py-pdbtrack-do-tracking-p)) |
| 2969 | (setq py-pdbtrack-do-tracking-p (> arg 0))) |
| 2970 | (message "%sabled Python's pdbtrack" |
| 2971 | (if py-pdbtrack-do-tracking-p "En" "Dis"))) |
| 2972 | |
| 2973 | (defun turn-on-pdbtrack () |
| 2974 | (interactive) |
| 2975 | (py-pdbtrack-toggle-stack-tracking 1)) |
| 2976 | |
| 2977 | (defun turn-off-pdbtrack () |
| 2978 | (interactive) |
| 2979 | (py-pdbtrack-toggle-stack-tracking 0)) |
| 2980 | |
| 2981 | |
| 2982 | |
Barry Warsaw | 29a90f0 | 2002-04-22 21:48:20 +0000 | [diff] [blame] | 2983 | ;; Pychecker |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 2984 | |
| 2985 | ;; hack for FSF Emacs |
| 2986 | (unless (fboundp 'read-shell-command) |
| 2987 | (defalias 'read-shell-command 'read-string)) |
| 2988 | |
Barry Warsaw | 29a90f0 | 2002-04-22 21:48:20 +0000 | [diff] [blame] | 2989 | (defun py-pychecker-run (command) |
| 2990 | "*Run pychecker (default on the file currently visited)." |
| 2991 | (interactive |
| 2992 | (let ((default |
| 2993 | (format "%s %s %s" py-pychecker-command |
| 2994 | (mapconcat 'identity py-pychecker-command-args " ") |
Barry Warsaw | 7f23212 | 2002-09-28 18:17:56 +0000 | [diff] [blame] | 2995 | (buffer-file-name))) |
| 2996 | (last (when py-pychecker-history |
| 2997 | (let* ((lastcmd (car py-pychecker-history)) |
| 2998 | (cmd (cdr (reverse (split-string lastcmd)))) |
| 2999 | (newcmd (reverse (cons (buffer-file-name) cmd)))) |
| 3000 | (mapconcat 'identity newcmd " "))))) |
Barry Warsaw | 29a90f0 | 2002-04-22 21:48:20 +0000 | [diff] [blame] | 3001 | |
| 3002 | (list |
Skip Montanaro | 302e550 | 2003-12-18 21:53:33 +0000 | [diff] [blame] | 3003 | (if (fboundp 'read-shell-command) |
| 3004 | (read-shell-command "Run pychecker like this: " |
| 3005 | (if last |
| 3006 | last |
| 3007 | default) |
| 3008 | 'py-pychecker-history) |
| 3009 | (read-string "Run pychecker like this: " |
| 3010 | (if last |
| 3011 | last |
| 3012 | default) |
| 3013 | 'py-pychecker-history)) |
| 3014 | ))) |
Barry Warsaw | 29a90f0 | 2002-04-22 21:48:20 +0000 | [diff] [blame] | 3015 | (save-some-buffers (not py-ask-about-save) nil) |
Barry Warsaw | daa1921 | 2002-07-16 15:56:28 +0000 | [diff] [blame] | 3016 | (compile-internal command "No more errors")) |
Barry Warsaw | 29a90f0 | 2002-04-22 21:48:20 +0000 | [diff] [blame] | 3017 | |
| 3018 | |
| 3019 | |
| 3020 | ;; pydoc commands. The guts of this function is stolen from XEmacs's |
| 3021 | ;; symbol-near-point, but without the useless regexp-quote call on the |
| 3022 | ;; results, nor the interactive bit. Also, we've added the temporary |
| 3023 | ;; syntax table setting, which Skip originally had broken out into a |
| 3024 | ;; separate function. Note that Emacs doesn't have the original |
| 3025 | ;; function. |
Barry Warsaw | a7cc43b | 2002-04-22 17:15:19 +0000 | [diff] [blame] | 3026 | (defun py-symbol-near-point () |
| 3027 | "Return the first textual item to the nearest point." |
| 3028 | ;; alg stolen from etag.el |
| 3029 | (save-excursion |
| 3030 | (with-syntax-table py-dotted-expression-syntax-table |
| 3031 | (if (or (bobp) (not (memq (char-syntax (char-before)) '(?w ?_)))) |
| 3032 | (while (not (looking-at "\\sw\\|\\s_\\|\\'")) |
| 3033 | (forward-char 1))) |
| 3034 | (while (looking-at "\\sw\\|\\s_") |
| 3035 | (forward-char 1)) |
| 3036 | (if (re-search-backward "\\sw\\|\\s_" nil t) |
| 3037 | (progn (forward-char 1) |
| 3038 | (buffer-substring (point) |
| 3039 | (progn (forward-sexp -1) |
| 3040 | (while (looking-at "\\s'") |
| 3041 | (forward-char 1)) |
| 3042 | (point)))) |
| 3043 | nil)))) |
| 3044 | |
| 3045 | (defun py-help-at-point () |
| 3046 | "Get help from Python based on the symbol nearest point." |
| 3047 | (interactive) |
| 3048 | (let* ((sym (py-symbol-near-point)) |
| 3049 | (base (substring sym 0 (or (search "." sym :from-end t) 0))) |
| 3050 | cmd) |
| 3051 | (if (not (equal base "")) |
| 3052 | (setq cmd (concat "import " base "\n"))) |
| 3053 | (setq cmd (concat "import pydoc\n" |
| 3054 | cmd |
Barry Warsaw | 0494955 | 2002-04-22 22:05:49 +0000 | [diff] [blame] | 3055 | "try: pydoc.help('" sym "')\n" |
Barry Warsaw | a7cc43b | 2002-04-22 17:15:19 +0000 | [diff] [blame] | 3056 | "except: print 'No help available on:', \"" sym "\"")) |
| 3057 | (message cmd) |
Barry Warsaw | 0494955 | 2002-04-22 22:05:49 +0000 | [diff] [blame] | 3058 | (py-execute-string cmd) |
| 3059 | (set-buffer "*Python Output*") |
| 3060 | ;; BAW: Should we really be leaving the output buffer in help-mode? |
| 3061 | (help-mode))) |
Barry Warsaw | a7cc43b | 2002-04-22 17:15:19 +0000 | [diff] [blame] | 3062 | |
| 3063 | |
| 3064 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3065 | ;; Documentation functions |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3066 | |
| 3067 | ;; dump the long form of the mode blurb; does the usual doc escapes, |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3068 | ;; plus lines of the form ^[vc]:name$ to suck variable & command docs |
| 3069 | ;; out of the right places, along with the keys they're on & current |
| 3070 | ;; values |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3071 | (defun py-dump-help-string (str) |
| 3072 | (with-output-to-temp-buffer "*Help*" |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3073 | (let ((locals (buffer-local-variables)) |
| 3074 | funckind funcname func funcdoc |
| 3075 | (start 0) mstart end |
| 3076 | keys ) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3077 | (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start) |
| 3078 | (setq mstart (match-beginning 0) end (match-end 0) |
| 3079 | funckind (substring str (match-beginning 1) (match-end 1)) |
| 3080 | funcname (substring str (match-beginning 2) (match-end 2)) |
| 3081 | func (intern funcname)) |
| 3082 | (princ (substitute-command-keys (substring str start mstart))) |
| 3083 | (cond |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3084 | ((equal funckind "c") ; command |
| 3085 | (setq funcdoc (documentation func) |
| 3086 | keys (concat |
| 3087 | "Key(s): " |
| 3088 | (mapconcat 'key-description |
| 3089 | (where-is-internal func py-mode-map) |
| 3090 | ", ")))) |
| 3091 | ((equal funckind "v") ; variable |
Barry Warsaw | 604cefa | 1996-09-03 18:17:04 +0000 | [diff] [blame] | 3092 | (setq funcdoc (documentation-property func 'variable-documentation) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3093 | keys (if (assq func locals) |
| 3094 | (concat |
| 3095 | "Local/Global values: " |
| 3096 | (prin1-to-string (symbol-value func)) |
| 3097 | " / " |
| 3098 | (prin1-to-string (default-value func))) |
| 3099 | (concat |
| 3100 | "Value: " |
| 3101 | (prin1-to-string (symbol-value func)))))) |
| 3102 | (t ; unexpected |
| 3103 | (error "Error in py-dump-help-string, tag `%s'" funckind))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3104 | (princ (format "\n-> %s:\t%s\t%s\n\n" |
| 3105 | (if (equal funckind "c") "Command" "Variable") |
| 3106 | funcname keys)) |
| 3107 | (princ funcdoc) |
| 3108 | (terpri) |
| 3109 | (setq start end)) |
| 3110 | (princ (substitute-command-keys (substring str start)))) |
| 3111 | (print-help-return-message))) |
| 3112 | |
| 3113 | (defun py-describe-mode () |
| 3114 | "Dump long form of Python-mode docs." |
| 3115 | (interactive) |
| 3116 | (py-dump-help-string "Major mode for editing Python files. |
| 3117 | Knows about Python indentation, tokens, comments and continuation lines. |
| 3118 | Paragraphs are separated by blank lines only. |
| 3119 | |
| 3120 | Major sections below begin with the string `@'; specific function and |
| 3121 | variable docs begin with `->'. |
| 3122 | |
| 3123 | @EXECUTING PYTHON CODE |
| 3124 | |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 3125 | \\[py-execute-import-or-reload]\timports or reloads the file in the Python interpreter |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3126 | \\[py-execute-buffer]\tsends the entire buffer to the Python interpreter |
| 3127 | \\[py-execute-region]\tsends the current region |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 3128 | \\[py-execute-def-or-class]\tsends the current function or class definition |
| 3129 | \\[py-execute-string]\tsends an arbitrary string |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3130 | \\[py-shell]\tstarts a Python interpreter window; this will be used by |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 3131 | \tsubsequent Python execution commands |
| 3132 | %c:py-execute-import-or-reload |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3133 | %c:py-execute-buffer |
| 3134 | %c:py-execute-region |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame] | 3135 | %c:py-execute-def-or-class |
| 3136 | %c:py-execute-string |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3137 | %c:py-shell |
| 3138 | |
| 3139 | @VARIABLES |
| 3140 | |
| 3141 | py-indent-offset\tindentation increment |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 3142 | py-block-comment-prefix\tcomment string used by comment-region |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3143 | |
| 3144 | py-python-command\tshell command to invoke Python interpreter |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3145 | py-temp-directory\tdirectory used for temp files (if needed) |
| 3146 | |
| 3147 | py-beep-if-tab-change\tring the bell if tab-width is changed |
| 3148 | %v:py-indent-offset |
| 3149 | %v:py-block-comment-prefix |
| 3150 | %v:py-python-command |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3151 | %v:py-temp-directory |
| 3152 | %v:py-beep-if-tab-change |
| 3153 | |
| 3154 | @KINDS OF LINES |
| 3155 | |
| 3156 | Each physical line in the file is either a `continuation line' (the |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3157 | preceding line ends with a backslash that's not part of a comment, or |
| 3158 | the paren/bracket/brace nesting level at the start of the line is |
| 3159 | non-zero, or both) or an `initial line' (everything else). |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3160 | |
| 3161 | An initial line is in turn a `blank line' (contains nothing except |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3162 | possibly blanks or tabs), a `comment line' (leftmost non-blank |
| 3163 | character is `#'), or a `code line' (everything else). |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3164 | |
| 3165 | Comment Lines |
| 3166 | |
| 3167 | Although all comment lines are treated alike by Python, Python mode |
| 3168 | recognizes two kinds that act differently with respect to indentation. |
| 3169 | |
| 3170 | An `indenting comment line' is a comment line with a blank, tab or |
| 3171 | nothing after the initial `#'. The indentation commands (see below) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3172 | treat these exactly as if they were code lines: a line following an |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3173 | indenting comment line will be indented like the comment line. All |
| 3174 | other comment lines (those with a non-whitespace character immediately |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3175 | following the initial `#') are `non-indenting comment lines', and |
| 3176 | their indentation is ignored by the indentation commands. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3177 | |
| 3178 | Indenting comment lines are by far the usual case, and should be used |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3179 | whenever possible. Non-indenting comment lines are useful in cases |
| 3180 | like these: |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3181 | |
| 3182 | \ta = b # a very wordy single-line comment that ends up being |
| 3183 | \t #... continued onto another line |
| 3184 | |
| 3185 | \tif a == b: |
| 3186 | ##\t\tprint 'panic!' # old code we've `commented out' |
| 3187 | \t\treturn a |
| 3188 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3189 | Since the `#...' and `##' comment lines have a non-whitespace |
| 3190 | character following the initial `#', Python mode ignores them when |
| 3191 | computing the proper indentation for the next line. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3192 | |
| 3193 | Continuation Lines and Statements |
| 3194 | |
| 3195 | The Python-mode commands generally work on statements instead of on |
| 3196 | individual lines, where a `statement' is a comment or blank line, or a |
| 3197 | code line and all of its following continuation lines (if any) |
| 3198 | considered as a single logical unit. The commands in this mode |
| 3199 | generally (when it makes sense) automatically move to the start of the |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3200 | statement containing point, even if point happens to be in the middle |
| 3201 | of some continuation line. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3202 | |
| 3203 | |
| 3204 | @INDENTATION |
| 3205 | |
| 3206 | Primarily for entering new code: |
| 3207 | \t\\[indent-for-tab-command]\t indent line appropriately |
| 3208 | \t\\[py-newline-and-indent]\t insert newline, then indent |
Barry Warsaw | 27ee115 | 1997-12-03 05:03:44 +0000 | [diff] [blame] | 3209 | \t\\[py-electric-backspace]\t reduce indentation, or delete single character |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3210 | |
| 3211 | Primarily for reindenting existing code: |
| 3212 | \t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally |
| 3213 | \t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally |
| 3214 | |
| 3215 | \t\\[py-indent-region]\t reindent region to match its context |
| 3216 | \t\\[py-shift-region-left]\t shift region left by py-indent-offset |
| 3217 | \t\\[py-shift-region-right]\t shift region right by py-indent-offset |
| 3218 | |
| 3219 | Unlike most programming languages, Python uses indentation, and only |
| 3220 | indentation, to specify block structure. Hence the indentation supplied |
| 3221 | automatically by Python-mode is just an educated guess: only you know |
| 3222 | the block structure you intend, so only you can supply correct |
| 3223 | indentation. |
| 3224 | |
| 3225 | The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on |
| 3226 | the indentation of preceding statements. E.g., assuming |
| 3227 | py-indent-offset is 4, after you enter |
| 3228 | \tif a > 0: \\[py-newline-and-indent] |
| 3229 | the cursor will be moved to the position of the `_' (_ is not a |
| 3230 | character in the file, it's just used here to indicate the location of |
| 3231 | the cursor): |
| 3232 | \tif a > 0: |
| 3233 | \t _ |
| 3234 | If you then enter `c = d' \\[py-newline-and-indent], the cursor will move |
| 3235 | to |
| 3236 | \tif a > 0: |
| 3237 | \t c = d |
| 3238 | \t _ |
| 3239 | Python-mode cannot know whether that's what you intended, or whether |
| 3240 | \tif a > 0: |
| 3241 | \t c = d |
| 3242 | \t_ |
| 3243 | was your intent. In general, Python-mode either reproduces the |
| 3244 | indentation of the (closest code or indenting-comment) preceding |
| 3245 | statement, or adds an extra py-indent-offset blanks if the preceding |
| 3246 | statement has `:' as its last significant (non-whitespace and non- |
| 3247 | comment) character. If the suggested indentation is too much, use |
Barry Warsaw | 27ee115 | 1997-12-03 05:03:44 +0000 | [diff] [blame] | 3248 | \\[py-electric-backspace] to reduce it. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3249 | |
| 3250 | Continuation lines are given extra indentation. If you don't like the |
| 3251 | suggested indentation, change it to something you do like, and Python- |
| 3252 | mode will strive to indent later lines of the statement in the same way. |
| 3253 | |
| 3254 | If a line is a continuation line by virtue of being in an unclosed |
| 3255 | paren/bracket/brace structure (`list', for short), the suggested |
| 3256 | indentation depends on whether the current line contains the first item |
| 3257 | in the list. If it does, it's indented py-indent-offset columns beyond |
| 3258 | the indentation of the line containing the open bracket. If you don't |
| 3259 | like that, change it by hand. The remaining items in the list will mimic |
| 3260 | whatever indentation you give to the first item. |
| 3261 | |
| 3262 | If a line is a continuation line because the line preceding it ends with |
| 3263 | a backslash, the third and following lines of the statement inherit their |
| 3264 | indentation from the line preceding them. The indentation of the second |
| 3265 | line in the statement depends on the form of the first (base) line: if |
| 3266 | the base line is an assignment statement with anything more interesting |
| 3267 | than the backslash following the leftmost assigning `=', the second line |
| 3268 | is indented two columns beyond that `='. Else it's indented to two |
| 3269 | columns beyond the leftmost solid chunk of non-whitespace characters on |
| 3270 | the base line. |
| 3271 | |
| 3272 | Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command] |
| 3273 | repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block |
| 3274 | structure you intend. |
| 3275 | %c:indent-for-tab-command |
| 3276 | %c:py-newline-and-indent |
Barry Warsaw | 27ee115 | 1997-12-03 05:03:44 +0000 | [diff] [blame] | 3277 | %c:py-electric-backspace |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3278 | |
| 3279 | |
| 3280 | The next function may be handy when editing code you didn't write: |
| 3281 | %c:py-guess-indent-offset |
| 3282 | |
| 3283 | |
| 3284 | The remaining `indent' functions apply to a region of Python code. They |
| 3285 | assume the block structure (equals indentation, in Python) of the region |
| 3286 | is correct, and alter the indentation in various ways while preserving |
| 3287 | the block structure: |
| 3288 | %c:py-indent-region |
| 3289 | %c:py-shift-region-left |
| 3290 | %c:py-shift-region-right |
| 3291 | |
| 3292 | @MARKING & MANIPULATING REGIONS OF CODE |
| 3293 | |
| 3294 | \\[py-mark-block]\t mark block of lines |
Barry Warsaw | 2518c67 | 1997-11-05 00:51:08 +0000 | [diff] [blame] | 3295 | \\[py-mark-def-or-class]\t mark smallest enclosing def |
| 3296 | \\[universal-argument] \\[py-mark-def-or-class]\t mark smallest enclosing class |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 3297 | \\[comment-region]\t comment out region of code |
| 3298 | \\[universal-argument] \\[comment-region]\t uncomment region of code |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3299 | %c:py-mark-block |
Barry Warsaw | 2518c67 | 1997-11-05 00:51:08 +0000 | [diff] [blame] | 3300 | %c:py-mark-def-or-class |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 3301 | %c:comment-region |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3302 | |
| 3303 | @MOVING POINT |
| 3304 | |
| 3305 | \\[py-previous-statement]\t move to statement preceding point |
| 3306 | \\[py-next-statement]\t move to statement following point |
| 3307 | \\[py-goto-block-up]\t move up to start of current block |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 3308 | \\[py-beginning-of-def-or-class]\t move to start of def |
| 3309 | \\[universal-argument] \\[py-beginning-of-def-or-class]\t move to start of class |
| 3310 | \\[py-end-of-def-or-class]\t move to end of def |
| 3311 | \\[universal-argument] \\[py-end-of-def-or-class]\t move to end of class |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3312 | |
| 3313 | The first two move to one statement beyond the statement that contains |
| 3314 | point. A numeric prefix argument tells them to move that many |
| 3315 | statements instead. Blank lines, comment lines, and continuation lines |
| 3316 | do not count as `statements' for these commands. So, e.g., you can go |
| 3317 | to the first code statement in a file by entering |
| 3318 | \t\\[beginning-of-buffer]\t to move to the top of the file |
| 3319 | \t\\[py-next-statement]\t to skip over initial comments and blank lines |
| 3320 | Or do `\\[py-previous-statement]' with a huge prefix argument. |
| 3321 | %c:py-previous-statement |
| 3322 | %c:py-next-statement |
| 3323 | %c:py-goto-block-up |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 3324 | %c:py-beginning-of-def-or-class |
| 3325 | %c:py-end-of-def-or-class |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3326 | |
| 3327 | @LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE |
| 3328 | |
| 3329 | `\\[indent-new-comment-line]' is handy for entering a multi-line comment. |
| 3330 | |
| 3331 | `\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the |
| 3332 | overall class and def structure of a module. |
| 3333 | |
| 3334 | `\\[back-to-indentation]' moves point to a line's first non-blank character. |
| 3335 | |
| 3336 | `\\[indent-relative]' is handy for creating odd indentation. |
| 3337 | |
| 3338 | @OTHER EMACS HINTS |
| 3339 | |
| 3340 | If you don't like the default value of a variable, change its value to |
| 3341 | whatever you do like by putting a `setq' line in your .emacs file. |
| 3342 | E.g., to set the indentation increment to 4, put this line in your |
| 3343 | .emacs: |
| 3344 | \t(setq py-indent-offset 4) |
| 3345 | To see the value of a variable, do `\\[describe-variable]' and enter the variable |
| 3346 | name at the prompt. |
| 3347 | |
| 3348 | When entering a key sequence like `C-c C-n', it is not necessary to |
| 3349 | release the CONTROL key after doing the `C-c' part -- it suffices to |
| 3350 | press the CONTROL key, press and release `c' (while still holding down |
| 3351 | CONTROL), press and release `n' (while still holding down CONTROL), & |
| 3352 | then release CONTROL. |
| 3353 | |
| 3354 | Entering Python mode calls with no arguments the value of the variable |
| 3355 | `python-mode-hook', if that value exists and is not nil; for backward |
| 3356 | compatibility it also tries `py-mode-hook'; see the `Hooks' section of |
| 3357 | the Elisp manual for details. |
| 3358 | |
| 3359 | Obscure: When python-mode is first loaded, it looks for all bindings |
| 3360 | to newline-and-indent in the global keymap, and shadows them with |
| 3361 | local bindings to py-newline-and-indent.")) |
| 3362 | |
Barry Warsaw | 56bd2ed | 2002-04-25 15:44:17 +0000 | [diff] [blame] | 3363 | (require 'info-look) |
Barry Warsaw | 5dfc7af | 2002-04-30 18:58:52 +0000 | [diff] [blame] | 3364 | ;; The info-look package does not always provide this function (it |
| 3365 | ;; appears this is the case with XEmacs 21.1) |
| 3366 | (when (fboundp 'info-lookup-maybe-add-help) |
| 3367 | (info-lookup-maybe-add-help |
| 3368 | :mode 'python-mode |
| 3369 | :regexp "[a-zA-Z0-9_]+" |
| 3370 | :doc-spec '(("(python-lib)Module Index") |
| 3371 | ("(python-lib)Class-Exception-Object Index") |
| 3372 | ("(python-lib)Function-Method-Variable Index") |
| 3373 | ("(python-lib)Miscellaneous Index"))) |
| 3374 | ) |
Barry Warsaw | 56bd2ed | 2002-04-25 15:44:17 +0000 | [diff] [blame] | 3375 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3376 | |
| 3377 | ;; Helper functions |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3378 | (defvar py-parse-state-re |
| 3379 | (concat |
Barry Warsaw | 644991f | 2002-10-21 15:58:29 +0000 | [diff] [blame] | 3380 | "^[ \t]*\\(elif\\|else\\|while\\|def\\|class\\)\\>" |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3381 | "\\|" |
| 3382 | "^[^ #\t\n]")) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3383 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3384 | (defun py-parse-state () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3385 | "Return the parse state at point (see `parse-partial-sexp' docs)." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3386 | (save-excursion |
Barry Warsaw | 43ecf8e | 1996-04-06 00:00:19 +0000 | [diff] [blame] | 3387 | (let ((here (point)) |
Barry Warsaw | b2d5e62 | 2002-04-22 15:29:27 +0000 | [diff] [blame] | 3388 | pps done) |
Barry Warsaw | 43ecf8e | 1996-04-06 00:00:19 +0000 | [diff] [blame] | 3389 | (while (not done) |
| 3390 | ;; back up to the first preceding line (if any; else start of |
| 3391 | ;; buffer) that begins with a popular Python keyword, or a |
| 3392 | ;; non- whitespace and non-comment character. These are good |
| 3393 | ;; places to start parsing to see whether where we started is |
| 3394 | ;; at a non-zero nesting level. It may be slow for people who |
| 3395 | ;; write huge code blocks or huge lists ... tough beans. |
| 3396 | (re-search-backward py-parse-state-re nil 'move) |
| 3397 | (beginning-of-line) |
Barry Warsaw | f64b405 | 1998-02-12 16:52:14 +0000 | [diff] [blame] | 3398 | ;; In XEmacs, we have a much better way to test for whether |
| 3399 | ;; we're in a triple-quoted string or not. Emacs does not |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 3400 | ;; have this built-in function, which is its loss because |
Barry Warsaw | f64b405 | 1998-02-12 16:52:14 +0000 | [diff] [blame] | 3401 | ;; without scanning from the beginning of the buffer, there's |
| 3402 | ;; no accurate way to determine this otherwise. |
Barry Warsaw | b2d5e62 | 2002-04-22 15:29:27 +0000 | [diff] [blame] | 3403 | (save-excursion (setq pps (parse-partial-sexp (point) here))) |
| 3404 | ;; make sure we don't land inside a triple-quoted string |
| 3405 | (setq done (or (not (nth 3 pps)) |
| 3406 | (bobp))) |
| 3407 | ;; Just go ahead and short circuit the test back to the |
| 3408 | ;; beginning of the buffer. This will be slow, but not |
| 3409 | ;; nearly as slow as looping through many |
| 3410 | ;; re-search-backwards. |
| 3411 | (if (not done) |
| 3412 | (goto-char (point-min)))) |
Barry Warsaw | 43ecf8e | 1996-04-06 00:00:19 +0000 | [diff] [blame] | 3413 | pps))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3414 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3415 | (defun py-nesting-level () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3416 | "Return the buffer position of the last unclosed enclosing list. |
| 3417 | If nesting level is zero, return nil." |
Barry Warsaw | f64b405 | 1998-02-12 16:52:14 +0000 | [diff] [blame] | 3418 | (let ((status (py-parse-state))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3419 | (if (zerop (car status)) |
| 3420 | nil ; not in a nest |
| 3421 | (car (cdr status))))) ; char# of open bracket |
| 3422 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3423 | (defun py-backslash-continuation-line-p () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3424 | "Return t iff preceding line ends with backslash that is not in a comment." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3425 | (save-excursion |
| 3426 | (beginning-of-line) |
| 3427 | (and |
| 3428 | ;; use a cheap test first to avoid the regexp if possible |
| 3429 | ;; use 'eq' because char-after may return nil |
| 3430 | (eq (char-after (- (point) 2)) ?\\ ) |
| 3431 | ;; make sure; since eq test passed, there is a preceding line |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3432 | (forward-line -1) ; always true -- side effect |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3433 | (looking-at py-continued-re)))) |
| 3434 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3435 | (defun py-continuation-line-p () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3436 | "Return t iff current line is a continuation line." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3437 | (save-excursion |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3438 | (beginning-of-line) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3439 | (or (py-backslash-continuation-line-p) |
| 3440 | (py-nesting-level)))) |
| 3441 | |
Barry Warsaw | 9c1696c | 1998-12-15 04:36:22 +0000 | [diff] [blame] | 3442 | (defun py-goto-beginning-of-tqs (delim) |
| 3443 | "Go to the beginning of the triple quoted string we find ourselves in. |
| 3444 | DELIM is the TQS string delimiter character we're searching backwards |
| 3445 | for." |
Barry Warsaw | 3c34bb3 | 2000-10-27 05:00:25 +0000 | [diff] [blame] | 3446 | (let ((skip (and delim (make-string 1 delim))) |
| 3447 | (continue t)) |
Barry Warsaw | 9c1696c | 1998-12-15 04:36:22 +0000 | [diff] [blame] | 3448 | (when skip |
| 3449 | (save-excursion |
Barry Warsaw | 3c34bb3 | 2000-10-27 05:00:25 +0000 | [diff] [blame] | 3450 | (while continue |
| 3451 | (py-safe (search-backward skip)) |
| 3452 | (setq continue (and (not (bobp)) |
| 3453 | (= (char-before) ?\\)))) |
| 3454 | (if (and (= (char-before) delim) |
| 3455 | (= (char-before (1- (point))) delim)) |
Barry Warsaw | 9c1696c | 1998-12-15 04:36:22 +0000 | [diff] [blame] | 3456 | (setq skip (make-string 3 delim)))) |
| 3457 | ;; we're looking at a triple-quoted string |
| 3458 | (py-safe (search-backward skip))))) |
| 3459 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3460 | (defun py-goto-initial-line () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3461 | "Go to the initial line of the current statement. |
| 3462 | Usually this is the line we're on, but if we're on the 2nd or |
| 3463 | following lines of a continuation block, we need to go up to the first |
| 3464 | line of the block." |
| 3465 | ;; Tricky: We want to avoid quadratic-time behavior for long |
| 3466 | ;; continued blocks, whether of the backslash or open-bracket |
| 3467 | ;; varieties, or a mix of the two. The following manages to do that |
| 3468 | ;; in the usual cases. |
| 3469 | ;; |
| 3470 | ;; Also, if we're sitting inside a triple quoted string, this will |
| 3471 | ;; drop us at the line that begins the string. |
Barry Warsaw | 9ec9fbc | 1998-01-21 05:15:57 +0000 | [diff] [blame] | 3472 | (let (open-bracket-pos) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3473 | (while (py-continuation-line-p) |
| 3474 | (beginning-of-line) |
| 3475 | (if (py-backslash-continuation-line-p) |
| 3476 | (while (py-backslash-continuation-line-p) |
| 3477 | (forward-line -1)) |
| 3478 | ;; else zip out of nested brackets/braces/parens |
| 3479 | (while (setq open-bracket-pos (py-nesting-level)) |
| 3480 | (goto-char open-bracket-pos))))) |
| 3481 | (beginning-of-line)) |
| 3482 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3483 | (defun py-goto-beyond-final-line () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3484 | "Go to the point just beyond the fine line of the current statement. |
| 3485 | Usually this is the start of the next line, but if this is a |
| 3486 | multi-line statement we need to skip over the continuation lines." |
| 3487 | ;; Tricky: Again we need to be clever to avoid quadratic time |
| 3488 | ;; behavior. |
| 3489 | ;; |
| 3490 | ;; XXX: Not quite the right solution, but deals with multi-line doc |
| 3491 | ;; strings |
Barry Warsaw | 751f493 | 1998-05-19 16:06:21 +0000 | [diff] [blame] | 3492 | (if (looking-at (concat "[ \t]*\\(" py-stringlit-re "\\)")) |
| 3493 | (goto-char (match-end 0))) |
| 3494 | ;; |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3495 | (forward-line 1) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3496 | (let (state) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3497 | (while (and (py-continuation-line-p) |
| 3498 | (not (eobp))) |
| 3499 | ;; skip over the backslash flavor |
| 3500 | (while (and (py-backslash-continuation-line-p) |
| 3501 | (not (eobp))) |
| 3502 | (forward-line 1)) |
| 3503 | ;; if in nest, zip to the end of the nest |
| 3504 | (setq state (py-parse-state)) |
| 3505 | (if (and (not (zerop (car state))) |
| 3506 | (not (eobp))) |
| 3507 | (progn |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 3508 | (parse-partial-sexp (point) (point-max) 0 nil state) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3509 | (forward-line 1)))))) |
| 3510 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3511 | (defun py-statement-opens-block-p () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3512 | "Return t iff the current statement opens a block. |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 3513 | I.e., iff it ends with a colon that is not in a comment. Point should |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3514 | be at the start of a statement." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3515 | (save-excursion |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3516 | (let ((start (point)) |
| 3517 | (finish (progn (py-goto-beyond-final-line) (1- (point)))) |
| 3518 | (searching t) |
| 3519 | (answer nil) |
| 3520 | state) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3521 | (goto-char start) |
| 3522 | (while searching |
| 3523 | ;; look for a colon with nothing after it except whitespace, and |
| 3524 | ;; maybe a comment |
| 3525 | (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$" |
| 3526 | finish t) |
| 3527 | (if (eq (point) finish) ; note: no `else' clause; just |
| 3528 | ; keep searching if we're not at |
| 3529 | ; the end yet |
| 3530 | ;; sure looks like it opens a block -- but it might |
| 3531 | ;; be in a comment |
| 3532 | (progn |
| 3533 | (setq searching nil) ; search is done either way |
| 3534 | (setq state (parse-partial-sexp start |
| 3535 | (match-beginning 0))) |
| 3536 | (setq answer (not (nth 4 state))))) |
| 3537 | ;; search failed: couldn't find another interesting colon |
| 3538 | (setq searching nil))) |
| 3539 | answer))) |
| 3540 | |
Barry Warsaw | f831d81 | 1996-07-31 20:42:59 +0000 | [diff] [blame] | 3541 | (defun py-statement-closes-block-p () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3542 | "Return t iff the current statement closes a block. |
| 3543 | I.e., if the line starts with `return', `raise', `break', `continue', |
| 3544 | and `pass'. This doesn't catch embedded statements." |
Barry Warsaw | f831d81 | 1996-07-31 20:42:59 +0000 | [diff] [blame] | 3545 | (let ((here (point))) |
Barry Warsaw | a8f99ba | 1999-05-24 18:37:57 +0000 | [diff] [blame] | 3546 | (py-goto-initial-line) |
Barry Warsaw | c0d2d51 | 1999-06-03 22:18:59 +0000 | [diff] [blame] | 3547 | (back-to-indentation) |
Barry Warsaw | f831d81 | 1996-07-31 20:42:59 +0000 | [diff] [blame] | 3548 | (prog1 |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 3549 | (looking-at (concat py-block-closing-keywords-re "\\>")) |
Barry Warsaw | f831d81 | 1996-07-31 20:42:59 +0000 | [diff] [blame] | 3550 | (goto-char here)))) |
| 3551 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3552 | (defun py-goto-beyond-block () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3553 | "Go to point just beyond the final line of block begun by the current line. |
| 3554 | This is the same as where `py-goto-beyond-final-line' goes unless |
| 3555 | we're on colon line, in which case we go to the end of the block. |
| 3556 | Assumes point is at the beginning of the line." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3557 | (if (py-statement-opens-block-p) |
| 3558 | (py-mark-block nil 'just-move) |
| 3559 | (py-goto-beyond-final-line))) |
| 3560 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3561 | (defun py-goto-statement-at-or-above () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3562 | "Go to the start of the first statement at or preceding point. |
| 3563 | Return t if there is such a statement, otherwise nil. `Statement' |
| 3564 | does not include blank lines, comments, or continuation lines." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3565 | (py-goto-initial-line) |
| 3566 | (if (looking-at py-blank-or-comment-re) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3567 | ;; skip back over blank & comment lines |
| 3568 | ;; note: will skip a blank or comment line that happens to be |
| 3569 | ;; a continuation line too |
| 3570 | (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t) |
| 3571 | (progn (py-goto-initial-line) t) |
| 3572 | nil) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3573 | t)) |
| 3574 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3575 | (defun py-goto-statement-below () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3576 | "Go to start of the first statement following the statement containing point. |
| 3577 | Return t if there is such a statement, otherwise nil. `Statement' |
| 3578 | does not include blank lines, comments, or continuation lines." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3579 | (beginning-of-line) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3580 | (let ((start (point))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3581 | (py-goto-beyond-final-line) |
| 3582 | (while (and |
Barry Warsaw | f070cce | 2002-05-23 19:42:16 +0000 | [diff] [blame] | 3583 | (or (looking-at py-blank-or-comment-re) |
| 3584 | (py-in-literal)) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3585 | (not (eobp))) |
| 3586 | (forward-line 1)) |
| 3587 | (if (eobp) |
| 3588 | (progn (goto-char start) nil) |
| 3589 | t))) |
| 3590 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3591 | (defun py-go-up-tree-to-keyword (key) |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3592 | "Go to begining of statement starting with KEY, at or preceding point. |
| 3593 | |
| 3594 | KEY is a regular expression describing a Python keyword. Skip blank |
| 3595 | lines and non-indenting comments. If the statement found starts with |
| 3596 | KEY, then stop, otherwise go back to first enclosing block starting |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 3597 | with KEY. If successful, leave point at the start of the KEY line and |
| 3598 | return t. Otherwise, leave point at an undefined place and return nil." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3599 | ;; skip blanks and non-indenting # |
| 3600 | (py-goto-initial-line) |
| 3601 | (while (and |
| 3602 | (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)") |
| 3603 | (zerop (forward-line -1))) ; go back |
| 3604 | nil) |
| 3605 | (py-goto-initial-line) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 3606 | (let* ((re (concat "[ \t]*" key "\\>")) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3607 | (case-fold-search nil) ; let* so looking-at sees this |
| 3608 | (found (looking-at re)) |
| 3609 | (dead nil)) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3610 | (while (not (or found dead)) |
| 3611 | (condition-case nil ; in case no enclosing block |
| 3612 | (py-goto-block-up 'no-mark) |
| 3613 | (error (setq dead t))) |
| 3614 | (or dead (setq found (looking-at re)))) |
| 3615 | (beginning-of-line) |
| 3616 | found)) |
| 3617 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3618 | (defun py-suck-up-leading-text () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3619 | "Return string in buffer from start of indentation to end of line. |
| 3620 | Prefix with \"...\" if leading whitespace was skipped." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3621 | (save-excursion |
| 3622 | (back-to-indentation) |
| 3623 | (concat |
| 3624 | (if (bolp) "" "...") |
| 3625 | (buffer-substring (point) (progn (end-of-line) (point)))))) |
| 3626 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3627 | (defun py-suck-up-first-keyword () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3628 | "Return first keyword on the line as a Lisp symbol. |
| 3629 | `Keyword' is defined (essentially) as the regular expression |
| 3630 | ([a-z]+). Returns nil if none was found." |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3631 | (let ((case-fold-search nil)) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 3632 | (if (looking-at "[ \t]*\\([a-z]+\\)\\>") |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 3633 | (intern (buffer-substring (match-beginning 1) (match-end 1))) |
| 3634 | nil))) |
| 3635 | |
Barry Warsaw | b3e81d5 | 1996-09-04 15:12:42 +0000 | [diff] [blame] | 3636 | (defun py-current-defun () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3637 | "Python value for `add-log-current-defun-function'. |
| 3638 | This tells add-log.el how to find the current function/method/variable." |
Barry Warsaw | b3e81d5 | 1996-09-04 15:12:42 +0000 | [diff] [blame] | 3639 | (save-excursion |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 3640 | |
| 3641 | ;; Move back to start of the current statement. |
| 3642 | |
| 3643 | (py-goto-initial-line) |
| 3644 | (back-to-indentation) |
| 3645 | (while (and (or (looking-at py-blank-or-comment-re) |
| 3646 | (py-in-literal)) |
| 3647 | (not (bobp))) |
| 3648 | (backward-to-indentation 1)) |
| 3649 | (py-goto-initial-line) |
| 3650 | |
| 3651 | (let ((scopes "") |
| 3652 | (sep "") |
| 3653 | dead assignment) |
| 3654 | |
| 3655 | ;; Check for an assignment. If this assignment exists inside a |
| 3656 | ;; def, it will be overwritten inside the while loop. If it |
| 3657 | ;; exists at top lever or inside a class, it will be preserved. |
| 3658 | |
| 3659 | (when (looking-at "[ \t]*\\([a-zA-Z0-9_]+\\)[ \t]*=") |
| 3660 | (setq scopes (buffer-substring (match-beginning 1) (match-end 1))) |
| 3661 | (setq assignment t) |
| 3662 | (setq sep ".")) |
| 3663 | |
| 3664 | ;; Prepend the name of each outer socpe (def or class). |
| 3665 | |
| 3666 | (while (not dead) |
| 3667 | (if (and (py-go-up-tree-to-keyword "\\(class\\|def\\)") |
| 3668 | (looking-at |
| 3669 | "[ \t]*\\(class\\|def\\)[ \t]*\\([a-zA-Z0-9_]+\\)[ \t]*")) |
| 3670 | (let ((name (buffer-substring (match-beginning 2) (match-end 2)))) |
| 3671 | (if (and assignment (looking-at "[ \t]*def")) |
| 3672 | (setq scopes name) |
| 3673 | (setq scopes (concat name sep scopes)) |
| 3674 | (setq sep ".")))) |
| 3675 | (setq assignment nil) |
| 3676 | (condition-case nil ; Terminate nicely at top level. |
| 3677 | (py-goto-block-up 'no-mark) |
| 3678 | (error (setq dead t)))) |
| 3679 | (if (string= scopes "") |
| 3680 | nil |
| 3681 | scopes)))) |
| 3682 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3683 | |
| 3684 | |
Barry Warsaw | fec75d6 | 1995-07-05 23:26:15 +0000 | [diff] [blame] | 3685 | (defconst py-help-address "python-mode@python.org" |
Barry Warsaw | 850437a | 1995-03-08 21:50:28 +0000 | [diff] [blame] | 3686 | "Address accepting submission of bug reports.") |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3687 | |
Barry Warsaw | 850437a | 1995-03-08 21:50:28 +0000 | [diff] [blame] | 3688 | (defun py-version () |
| 3689 | "Echo the current version of `python-mode' in the minibuffer." |
| 3690 | (interactive) |
| 3691 | (message "Using `python-mode' version %s" py-version) |
| 3692 | (py-keep-region-active)) |
| 3693 | |
| 3694 | ;; only works under Emacs 19 |
| 3695 | ;(eval-when-compile |
| 3696 | ; (require 'reporter)) |
| 3697 | |
| 3698 | (defun py-submit-bug-report (enhancement-p) |
| 3699 | "Submit via mail a bug report on `python-mode'. |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3700 | With \\[universal-argument] (programmatically, argument ENHANCEMENT-P |
| 3701 | non-nil) just submit an enhancement request." |
Barry Warsaw | 850437a | 1995-03-08 21:50:28 +0000 | [diff] [blame] | 3702 | (interactive |
| 3703 | (list (not (y-or-n-p |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3704 | "Is this a bug report (hit `n' to send other comments)? ")))) |
Barry Warsaw | b5e0ecb | 1995-03-14 18:32:54 +0000 | [diff] [blame] | 3705 | (let ((reporter-prompt-for-summary-p (if enhancement-p |
| 3706 | "(Very) brief summary: " |
| 3707 | t))) |
Barry Warsaw | 850437a | 1995-03-08 21:50:28 +0000 | [diff] [blame] | 3708 | (require 'reporter) |
| 3709 | (reporter-submit-bug-report |
| 3710 | py-help-address ;address |
Barry Warsaw | b5e0ecb | 1995-03-14 18:32:54 +0000 | [diff] [blame] | 3711 | (concat "python-mode " py-version) ;pkgname |
Barry Warsaw | 850437a | 1995-03-08 21:50:28 +0000 | [diff] [blame] | 3712 | ;; varlist |
| 3713 | (if enhancement-p nil |
| 3714 | '(py-python-command |
| 3715 | py-indent-offset |
| 3716 | py-block-comment-prefix |
Barry Warsaw | 850437a | 1995-03-08 21:50:28 +0000 | [diff] [blame] | 3717 | py-temp-directory |
| 3718 | py-beep-if-tab-change)) |
| 3719 | nil ;pre-hooks |
| 3720 | nil ;post-hooks |
| 3721 | "Dear Barry,") ;salutation |
| 3722 | (if enhancement-p nil |
| 3723 | (set-mark (point)) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 3724 | (insert |
Barry Warsaw | 850437a | 1995-03-08 21:50:28 +0000 | [diff] [blame] | 3725 | "Please replace this text with a sufficiently large code sample\n\ |
| 3726 | and an exact recipe so that I can reproduce your problem. Failure\n\ |
| 3727 | to do so may mean a greater delay in fixing your bug.\n\n") |
| 3728 | (exchange-point-and-mark) |
| 3729 | (py-keep-region-active)))) |
| 3730 | |
| 3731 | |
Barry Warsaw | 4738478 | 1997-11-26 05:27:45 +0000 | [diff] [blame] | 3732 | (defun py-kill-emacs-hook () |
Barry Warsaw | 41a05c7 | 1998-08-10 16:33:12 +0000 | [diff] [blame] | 3733 | "Delete files in `py-file-queue'. |
| 3734 | These are Python temporary files awaiting execution." |
Barry Warsaw | 4738478 | 1997-11-26 05:27:45 +0000 | [diff] [blame] | 3735 | (mapcar #'(lambda (filename) |
| 3736 | (py-safe (delete-file filename))) |
| 3737 | py-file-queue)) |
| 3738 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3739 | ;; arrange to kill temp files when Emacs exists |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 3740 | (add-hook 'kill-emacs-hook 'py-kill-emacs-hook) |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 3741 | (add-hook 'comint-output-filter-functions 'py-pdbtrack-track-stack-file) |
| 3742 | |
| 3743 | ;; Add a designator to the minor mode strings |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 3744 | (or (assq 'py-pdbtrack-is-tracking-p minor-mode-alist) |
Barry Warsaw | 4f577d2 | 2001-04-11 20:23:17 +0000 | [diff] [blame] | 3745 | (push '(py-pdbtrack-is-tracking-p py-pdbtrack-minor-mode-string) |
| 3746 | minor-mode-alist)) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3747 | |
| 3748 | |
| 3749 | |
Skip Montanaro | 6c5bc34 | 2002-12-31 16:56:20 +0000 | [diff] [blame] | 3750 | ;;; paragraph and string filling code from Bernhard Herzog |
| 3751 | ;;; see http://mail.python.org/pipermail/python-list/2002-May/103189.html |
| 3752 | |
| 3753 | (defun py-fill-comment (&optional justify) |
| 3754 | "Fill the comment paragraph around point" |
| 3755 | (let (;; Non-nil if the current line contains a comment. |
| 3756 | has-comment |
| 3757 | |
| 3758 | ;; If has-comment, the appropriate fill-prefix for the comment. |
| 3759 | comment-fill-prefix) |
| 3760 | |
| 3761 | ;; Figure out what kind of comment we are looking at. |
| 3762 | (save-excursion |
| 3763 | (beginning-of-line) |
| 3764 | (cond |
| 3765 | ;; A line with nothing but a comment on it? |
| 3766 | ((looking-at "[ \t]*#[# \t]*") |
| 3767 | (setq has-comment t |
| 3768 | comment-fill-prefix (buffer-substring (match-beginning 0) |
| 3769 | (match-end 0)))) |
| 3770 | |
| 3771 | ;; A line with some code, followed by a comment? Remember that the hash |
| 3772 | ;; which starts the comment shouldn't be part of a string or character. |
| 3773 | ((progn |
| 3774 | (while (not (looking-at "#\\|$")) |
| 3775 | (skip-chars-forward "^#\n\"'\\") |
| 3776 | (cond |
| 3777 | ((eq (char-after (point)) ?\\) (forward-char 2)) |
| 3778 | ((memq (char-after (point)) '(?\" ?')) (forward-sexp 1)))) |
| 3779 | (looking-at "#+[\t ]*")) |
| 3780 | (setq has-comment t) |
| 3781 | (setq comment-fill-prefix |
| 3782 | (concat (make-string (current-column) ? ) |
| 3783 | (buffer-substring (match-beginning 0) (match-end 0))))))) |
| 3784 | |
| 3785 | (if (not has-comment) |
| 3786 | (fill-paragraph justify) |
| 3787 | |
| 3788 | ;; Narrow to include only the comment, and then fill the region. |
| 3789 | (save-restriction |
| 3790 | (narrow-to-region |
| 3791 | |
| 3792 | ;; Find the first line we should include in the region to fill. |
| 3793 | (save-excursion |
| 3794 | (while (and (zerop (forward-line -1)) |
| 3795 | (looking-at "^[ \t]*#"))) |
| 3796 | |
| 3797 | ;; We may have gone to far. Go forward again. |
| 3798 | (or (looking-at "^[ \t]*#") |
| 3799 | (forward-line 1)) |
| 3800 | (point)) |
| 3801 | |
| 3802 | ;; Find the beginning of the first line past the region to fill. |
| 3803 | (save-excursion |
| 3804 | (while (progn (forward-line 1) |
| 3805 | (looking-at "^[ \t]*#"))) |
| 3806 | (point))) |
| 3807 | |
| 3808 | ;; Lines with only hashes on them can be paragraph boundaries. |
| 3809 | (let ((paragraph-start (concat paragraph-start "\\|[ \t#]*$")) |
| 3810 | (paragraph-separate (concat paragraph-separate "\\|[ \t#]*$")) |
| 3811 | (fill-prefix comment-fill-prefix)) |
| 3812 | ;;(message "paragraph-start %S paragraph-separate %S" |
| 3813 | ;;paragraph-start paragraph-separate) |
| 3814 | (fill-paragraph justify)))) |
| 3815 | t)) |
| 3816 | |
| 3817 | |
| 3818 | (defun py-fill-string (start &optional justify) |
| 3819 | "Fill the paragraph around (point) in the string starting at start" |
| 3820 | ;; basic strategy: narrow to the string and call the default |
| 3821 | ;; implementation |
| 3822 | (let (;; the start of the string's contents |
| 3823 | string-start |
| 3824 | ;; the end of the string's contents |
| 3825 | string-end |
| 3826 | ;; length of the string's delimiter |
| 3827 | delim-length |
| 3828 | ;; The string delimiter |
| 3829 | delim |
| 3830 | ) |
| 3831 | |
| 3832 | (save-excursion |
| 3833 | (goto-char start) |
| 3834 | (if (looking-at "\\('''\\|\"\"\"\\|'\\|\"\\)\\\\?\n?") |
| 3835 | (setq string-start (match-end 0) |
| 3836 | delim-length (- (match-end 1) (match-beginning 1)) |
| 3837 | delim (buffer-substring-no-properties (match-beginning 1) |
| 3838 | (match-end 1))) |
| 3839 | (error "The parameter start is not the beginning of a python string")) |
| 3840 | |
| 3841 | ;; if the string is the first token on a line and doesn't start with |
| 3842 | ;; a newline, fill as if the string starts at the beginning of the |
| 3843 | ;; line. this helps with one line docstrings |
| 3844 | (save-excursion |
| 3845 | (beginning-of-line) |
| 3846 | (and (/= (char-before string-start) ?\n) |
| 3847 | (looking-at (concat "[ \t]*" delim)) |
| 3848 | (setq string-start (point)))) |
| 3849 | |
| 3850 | (forward-sexp (if (= delim-length 3) 2 1)) |
| 3851 | |
| 3852 | ;; with both triple quoted strings and single/double quoted strings |
| 3853 | ;; we're now directly behind the first char of the end delimiter |
| 3854 | ;; (this doesn't work correctly when the triple quoted string |
| 3855 | ;; contains the quote mark itself). The end of the string's contents |
| 3856 | ;; is one less than point |
| 3857 | (setq string-end (1- (point)))) |
| 3858 | |
| 3859 | ;; Narrow to the string's contents and fill the current paragraph |
| 3860 | (save-restriction |
| 3861 | (narrow-to-region string-start string-end) |
| 3862 | (let ((ends-with-newline (= (char-before (point-max)) ?\n))) |
| 3863 | (fill-paragraph justify) |
| 3864 | (if (and (not ends-with-newline) |
| 3865 | (= (char-before (point-max)) ?\n)) |
| 3866 | ;; the default fill-paragraph implementation has inserted a |
| 3867 | ;; newline at the end. Remove it again. |
| 3868 | (save-excursion |
| 3869 | (goto-char (point-max)) |
| 3870 | (delete-char -1))))) |
| 3871 | |
| 3872 | ;; return t to indicate that we've done our work |
| 3873 | t)) |
| 3874 | |
| 3875 | (defun py-fill-paragraph (&optional justify) |
| 3876 | "Like \\[fill-paragraph], but handle Python comments and strings. |
| 3877 | If any of the current line is a comment, fill the comment or the |
| 3878 | paragraph of it that point is in, preserving the comment's indentation |
| 3879 | and initial `#'s. |
| 3880 | If point is inside a string, narrow to that string and fill. |
| 3881 | " |
| 3882 | (interactive "P") |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 3883 | ;; fill-paragraph will narrow incorrectly |
| 3884 | (save-restriction |
| 3885 | (widen) |
| 3886 | (let* ((bod (py-point 'bod)) |
| 3887 | (pps (parse-partial-sexp bod (point)))) |
| 3888 | (cond |
| 3889 | ;; are we inside a comment or on a line with only whitespace before |
| 3890 | ;; the comment start? |
| 3891 | ((or (nth 4 pps) |
| 3892 | (save-excursion (beginning-of-line) (looking-at "[ \t]*#"))) |
| 3893 | (py-fill-comment justify)) |
| 3894 | ;; are we inside a string? |
| 3895 | ((nth 3 pps) |
| 3896 | (py-fill-string (nth 8 pps))) |
| 3897 | ;; are we at the opening quote of a string, or in the indentation? |
| 3898 | ((save-excursion |
| 3899 | (forward-word 1) |
| 3900 | (eq (py-in-literal) 'string)) |
| 3901 | (save-excursion |
| 3902 | (py-fill-string (py-point 'boi)))) |
| 3903 | ;; are we at or after the closing quote of a string? |
| 3904 | ((save-excursion |
| 3905 | (backward-word 1) |
| 3906 | (eq (py-in-literal) 'string)) |
| 3907 | (save-excursion |
| 3908 | (py-fill-string (py-point 'boi)))) |
| 3909 | ;; otherwise use the default |
| 3910 | (t |
| 3911 | (fill-paragraph justify)))))) |
Skip Montanaro | 6c5bc34 | 2002-12-31 16:56:20 +0000 | [diff] [blame] | 3912 | |
| 3913 | |
| 3914 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3915 | (provide 'python-mode) |
| 3916 | ;;; python-mode.el ends here |