blob: 1e9be74de8b56b99f20b592419c9ea7cdf89df68 [file] [log] [blame]
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001;;; python-mode.el --- Major mode for editing Python programs
2
3;; Copyright (C) 1992,1993,1994 Tim Peters
4
Skip Montanarodc6d9e12008-02-05 02:32:16 +00005;; Author: 2003-2007 http://sf.net/projects/python-mode
6;; 1995-2002 Barry A. Warsaw
Barry Warsawfec75d61995-07-05 23:26:15 +00007;; 1992-1994 Tim Peters
Barry Warsawa97a3f31997-11-04 18:47:06 +00008;; Maintainer: python-mode@python.org
9;; Created: Feb 1992
10;; Keywords: python languages oop
Barry Warsaw7b0f5681995-03-08 21:33:04 +000011
Barry Warsaw19b1c612001-07-06 20:27:29 +000012(defconst py-version "$Revision$"
Barry Warsawc72c11c1997-08-09 06:42:08 +000013 "`python-mode' version number.")
14
Barry Warsawcfec3591995-03-10 15:58:16 +000015;; 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 Warsaw7b0f5681995-03-08 21:33:04 +000020
21;;; Commentary:
Barry Warsaw755c6711996-08-01 20:02:55 +000022
Skip Montanarodc6d9e12008-02-05 02:32:16 +000023;; 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 Warsaw7b0f5681995-03-08 21:33:04 +000030
Skip Montanarodc6d9e12008-02-05 02:32:16 +000031;; pdbtrack support contributed by Ken Manheimer, April 2001. Skip Montanaro
32;; has also contributed significantly to python-mode's development.
Barry Warsaw4f577d22001-04-11 20:23:17 +000033
Barry Warsawb2d5e622002-04-22 15:29:27 +000034;; Please use the SourceForge Python project to submit bugs or
35;; patches:
Barry Warsaw4f577d22001-04-11 20:23:17 +000036;;
37;; http://sourceforge.net/projects/python
Barry Warsaw37231521997-12-11 17:23:13 +000038
Skip Montanarodc6d9e12008-02-05 02:32:16 +000039;; 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 Warsaw37231521997-12-11 17:23:13 +000055;; FOR MORE INFORMATION:
56
Barry Warsaw4f577d22001-04-11 20:23:17 +000057;; There is some information on python-mode.el at
58
Barry Warsawf7039e21998-08-20 22:10:46 +000059;; http://www.python.org/emacs/python-mode/
60;;
Barry Warsawb2d5e622002-04-22 15:29:27 +000061;; It does contain links to other packages that you might find useful,
62;; such as pdb interfaces, OO-Browser links, etc.
Barry Warsaw37231521997-12-11 17:23:13 +000063
64;; BUG REPORTING:
Barry Warsaw7ae77681994-12-12 20:38:05 +000065
Barry Warsaw4f577d22001-04-11 20:23:17 +000066;; 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 Warsawaffc0ca1997-11-03 16:59:38 +000070;; 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 Warsaw37231521997-12-11 17:23:13 +000074;; doubtful that a texinfo manual would be very useful, but if you
75;; want to contribute one, I'll certainly accept it!
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000076
Barry Warsaw7b0f5681995-03-08 21:33:04 +000077;;; Code:
78
Barry Warsaw6dfbe5d1998-08-20 21:51:27 +000079(require 'comint)
Barry Warsawc72c11c1997-08-09 06:42:08 +000080(require 'custom)
Barry Warsaw4f577d22001-04-11 20:23:17 +000081(require 'cl)
Barry Warsaw1f4fed62002-07-17 13:45:00 +000082(require 'compile)
Skip Montanarodc6d9e12008-02-05 02:32:16 +000083(require 'ansi-color)
Barry Warsawc72c11c1997-08-09 06:42:08 +000084
Barry Warsaw7b0f5681995-03-08 21:33:04 +000085
86;; user definable variables
87;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Barry Warsaw7ae77681994-12-12 20:38:05 +000088
Barry Warsawc72c11c1997-08-09 06:42:08 +000089(defgroup python nil
90 "Support for the Python programming language, <http://www.python.org/>"
Barry Warsaw5ed843f1999-07-28 22:06:06 +000091 :group 'languages
92 :prefix "py-")
Barry Warsaw7ae77681994-12-12 20:38:05 +000093
Skip Montanarodc6d9e12008-02-05 02:32:16 +000094(defcustom py-tab-always-indent t
95 "*Non-nil means TAB in Python mode should always reindent the current line,
96regardless of where in the line point is when the TAB command is used."
97 :type 'boolean
98 :group 'python)
99
Barry Warsawc72c11c1997-08-09 06:42:08 +0000100(defcustom py-python-command "python"
101 "*Shell command used to start Python interpreter."
102 :type 'string
103 :group 'python)
104
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000105(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 Warsawa2398801998-04-09 23:28:20 +0000108 :type 'string
Barry Warsaw5ed843f1999-07-28 22:06:06 +0000109 :group 'python
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000110 :tag "Jython Command")
Barry Warsawa2398801998-04-09 23:28:20 +0000111
Barry Warsawaa384fd1999-02-16 23:36:16 +0000112(defcustom py-default-interpreter 'cpython
113 "*Which Python interpreter is used by default.
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000114The value for this variable can be either `cpython' or `jython'.
Barry Warsawaa384fd1999-02-16 23:36:16 +0000115
116When the value is `cpython', the variables `py-python-command' and
117`py-python-command-args' are consulted to determine the interpreter
118and arguments to use.
119
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000120When the value is `jython', the variables `py-jython-command' and
121`py-jython-command-args' are consulted to determine the interpreter
Barry Warsawaa384fd1999-02-16 23:36:16 +0000122and arguments to use.
123
Barry Warsaw11f21561999-07-28 21:59:43 +0000124Note that this variable is consulted only the first time that a Python
125mode buffer is visited during an Emacs session. After that, use
Barry Warsawaa384fd1999-02-16 23:36:16 +0000126\\[py-toggle-shells] to change the interpreter shell."
127 :type '(choice (const :tag "Python (a.k.a. CPython)" cpython)
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000128 (const :tag "Jython" jython))
Barry Warsawaa384fd1999-02-16 23:36:16 +0000129 :group 'python)
130
Barry Warsaw8f972b71998-02-05 20:45:49 +0000131(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
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000136(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 Warsawa2398801998-04-09 23:28:20 +0000139 :type '(repeat string)
Barry Warsaw5ed843f1999-07-28 22:06:06 +0000140 :group 'python
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000141 :tag "Jython Command Args")
Barry Warsawa2398801998-04-09 23:28:20 +0000142
Barry Warsawc72c11c1997-08-09 06:42:08 +0000143(defcustom py-indent-offset 4
Barry Warsaw41a05c71998-08-10 16:33:12 +0000144 "*Amount of offset per level of indentation.
145`\\[py-guess-indent-offset]' can usually guess a good value when
146you're editing someone else's Python code."
Barry Warsawc72c11c1997-08-09 06:42:08 +0000147 :type 'integer
148 :group 'python)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000149
Barry Warsawfd4c9e82001-06-18 23:40:35 +0000150(defcustom py-continuation-offset 4
Barry Warsaw40fb4522001-07-06 20:07:13 +0000151 "*Additional amount of offset to give for some continuation lines.
Barry Warsawfd4c9e82001-06-18 23:40:35 +0000152Continuation lines are those that immediately follow a backslash
Barry Warsaw40fb4522001-07-06 20:07:13 +0000153terminated line. Only those continuation lines for a block opening
154statement are given this extra offset."
Barry Warsawfd4c9e82001-06-18 23:40:35 +0000155 :type 'integer
156 :group 'python)
157
Barry Warsaw742a5111998-03-13 17:29:15 +0000158(defcustom py-smart-indentation t
159 "*Should `python-mode' try to automagically set some indentation variables?
160When this variable is non-nil, two things happen when a buffer is set
161to `python-mode':
162
Barry Warsawc0d2d511999-06-03 22:18:59 +0000163 1. `py-indent-offset' is guessed from existing code in the buffer.
Barry Warsaw639eea61998-03-16 18:12:13 +0000164 Only guessed values between 2 and 8 are considered. If a valid
Barry Warsaw742a5111998-03-13 17:29:15 +0000165 guess can't be made (perhaps because you are visiting a new
Barry Warsaw639eea61998-03-16 18:12:13 +0000166 file), then the value in `py-indent-offset' is used.
Barry Warsaw742a5111998-03-13 17:29:15 +0000167
Barry Warsaw639eea61998-03-16 18:12:13 +0000168 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 Warsaw742a5111998-03-13 17:29:15 +0000173
Barry Warsaw8046bef1998-03-13 20:04:52 +0000174Note that both these settings occur *after* `python-mode-hook' is run,
175so if you want to defeat the automagic configuration, you must also
176set `py-smart-indentation' to nil in your `python-mode-hook'."
Barry Warsaw742a5111998-03-13 17:29:15 +0000177 :type 'boolean
178 :group 'python)
179
Barry Warsawc72c11c1997-08-09 06:42:08 +0000180(defcustom py-align-multiline-strings-p t
181 "*Flag describing how multi-line triple quoted strings are aligned.
Barry Warsaw095e9c61995-09-19 20:01:42 +0000182When this flag is non-nil, continuation lines are lined up under the
183preceding line's indentation. When this flag is nil, continuation
Barry Warsawc72c11c1997-08-09 06:42:08 +0000184lines 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 Warsaw095e9c61995-09-19 20:01:42 +0000188
Barry Warsaw218eb751998-09-22 19:51:47 +0000189(defcustom py-block-comment-prefix "##"
Barry Warsaw867a32a1996-03-07 18:30:26 +0000190 "*String used by \\[comment-region] to comment out a block of code.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000191This should follow the convention for non-indenting comment lines so
192that the indentation commands won't get confused (i.e., the string
193should be of the form `#x...' where `x' is not a blank or a tab, and
Barry Warsaw218eb751998-09-22 19:51:47 +0000194`...' is arbitrary). However, this string should not end in whitespace."
Barry Warsawc72c11c1997-08-09 06:42:08 +0000195 :type 'string
196 :group 'python)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000197
Barry Warsawc72c11c1997-08-09 06:42:08 +0000198(defcustom py-honor-comment-indentation t
Barry Warsaw6d627751996-03-06 18:41:38 +0000199 "*Controls how comment lines influence subsequent indentation.
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000200
Barry Warsaw6d627751996-03-06 18:41:38 +0000201When nil, all comment lines are skipped for indentation purposes, and
Barry Warsawc72c11c1997-08-09 06:42:08 +0000202if possible, a faster algorithm is used (i.e. X/Emacs 19 and beyond).
Barry Warsaw6d627751996-03-06 18:41:38 +0000203
204When t, lines that begin with a single `#' are a hint to subsequent
205line indentation. If the previous line is such a comment line (as
Barry Warsaw145ab1c1998-05-19 14:49:49 +0000206opposed to one that starts with `py-block-comment-prefix'), then its
Barry Warsaw6d627751996-03-06 18:41:38 +0000207indentation is used as a hint for this line's indentation. Lines that
208begin with `py-block-comment-prefix' are ignored for indentation
209purposes.
210
Barry Warsawd36cfe42002-03-15 16:46:46 +0000211When not nil or t, comment lines that begin with a single `#' are used
212as indentation hints, unless the comment character is in column zero."
Barry Warsawc72c11c1997-08-09 06:42:08 +0000213 :type '(choice
214 (const :tag "Skip all comment lines (fast)" nil)
215 (const :tag "Single # `sets' indentation for next line" t)
Barry Warsawaffc0ca1997-11-03 16:59:38 +0000216 (const :tag "Single # `sets' indentation except at column zero"
217 other)
Barry Warsawc72c11c1997-08-09 06:42:08 +0000218 )
219 :group 'python)
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000220
Barry Warsaw516b6201997-08-09 06:43:20 +0000221(defcustom py-temp-directory
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000222 (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 Warsaw7ae77681994-12-12 20:38:05 +0000228 (or (funcall ok (getenv "TMPDIR"))
229 (funcall ok "/usr/tmp")
230 (funcall ok "/tmp")
Barry Warsawd1648372002-03-18 18:53:56 +0000231 (funcall ok "/var/tmp")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000232 (funcall ok ".")
233 (error
Barry Warsaw6c6db0a1998-02-25 16:33:56 +0000234 "Couldn't find a usable temp directory -- set `py-temp-directory'")))
Barry Warsawd1648372002-03-18 18:53:56 +0000235 "*Directory used for temporary files created by a *Python* process.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000236By default, the first directory from this list that exists and that you
Barry Warsawd1648372002-03-18 18:53:56 +0000237can write into: the value (if any) of the environment variable TMPDIR,
238/usr/tmp, /tmp, /var/tmp, or the current directory."
Barry Warsawc72c11c1997-08-09 06:42:08 +0000239 :type 'string
240 :group 'python)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000241
Barry Warsaw516b6201997-08-09 06:43:20 +0000242(defcustom py-beep-if-tab-change t
Barry Warsaw41a05c71998-08-10 16:33:12 +0000243 "*Ring the bell if `tab-width' is changed.
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000244If a comment of the form
245
246 \t# vi:set tabsize=<number>:
247
248is found before the first code line when the file is entered, and the
249current value of (the general Emacs variable) `tab-width' does not
250equal <number>, `tab-width' is set to <number>, a message saying so is
251displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
Barry Warsawc72c11c1997-08-09 06:42:08 +0000252the Emacs bell is also rung as a warning."
253 :type 'boolean
254 :group 'python)
255
Barry Warsaw9981d221997-12-03 05:25:48 +0000256(defcustom py-jump-on-exception t
257 "*Jump to innermost exception frame in *Python Output* buffer.
Barry Warsaw12c92941998-08-10 21:44:37 +0000258When this variable is non-nil and an exception occurs when running
Barry Warsaw9981d221997-12-03 05:25:48 +0000259Python code synchronously in a subprocess, jump immediately to the
Barry Warsaw12c92941998-08-10 21:44:37 +0000260source code of the innermost traceback frame."
Barry Warsaw3bfed5b1998-05-19 16:25:04 +0000261 :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.
266Otherwise, all modified buffers are saved without asking."
267 :type 'boolean
268 :group 'python)
Barry Warsaw9981d221997-12-03 05:25:48 +0000269
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000270(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
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000280(defcustom py-imenu-show-method-args-p nil
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000281 "*Controls echoing of arguments of functions & methods in the Imenu buffer.
282When non-nil, arguments are printed."
283 :type 'boolean
284 :group 'python)
285(make-variable-buffer-local 'py-indent-offset)
286
Barry Warsaw4f577d22001-04-11 20:23:17 +0000287(defcustom py-pdbtrack-do-tracking-p t
288 "*Controls whether the pdbtrack feature is enabled or not.
289When non-nil, pdbtrack is enabled in all comint-based buffers,
290e.g. shell buffers and the *Python* buffer. When using pdb to debug a
291Python program, pdbtrack notices the pdb prompt and displays the
292source file and line that the program is stopped at, much the same way
293as 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 Warsaw88491612002-04-25 21:31:47 +0000303(defcustom py-import-check-point-max
304 20000
305 "Maximum number of characters to search for a Java-ish import statement.
306When `python-mode' tries to calculate the shell to use (either a
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000307CPython or a Jython shell), it looks at the so-called `shebang' line
Barry Warsaw88491612002-04-25 21:31:47 +0000308-- i.e. #! line. If that's not available, it looks at some of the
309file heading imports to see if they look Java-like."
310 :type 'integer
311 :group 'python
312 )
313
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000314(make-obsolete-variable 'py-jpython-packages 'py-jython-packages)
315(defcustom py-jython-packages
Barry Warsaw88491612002-04-25 21:31:47 +0000316 '("java" "javax" "org" "com")
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000317 "Imported packages that imply `jython-mode'."
Barry Warsaw88491612002-04-25 21:31:47 +0000318 :type '(repeat string)
319 :group 'python)
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000320
Barry Warsawc0ecb531998-01-20 21:43:34 +0000321;; Not customizable
322(defvar py-master-file nil
323 "If non-nil, execute the named file instead of the buffer's file.
Barry Warsaw50b3eb61998-02-25 15:57:47 +0000324The intent is to allow you to set this variable in the file's local
Barry Warsawc0ecb531998-01-20 21:43:34 +0000325variable section, e.g.:
326
327 # Local Variables:
328 # py-master-file: \"master.py\"
329 # End:
330
Barry Warsaw41a05c71998-08-10 16:33:12 +0000331so that typing \\[py-execute-buffer] in that buffer executes the named
Barry Warsaw1bbc0311998-10-27 21:54:56 +0000332master file instead of the buffer's file. If the file name has a
333relative path, the value of variable `default-directory' for the
334buffer is prepended to come up with a file name.")
Barry Warsawc0ecb531998-01-20 21:43:34 +0000335(make-variable-buffer-local 'py-master-file)
336
Barry Warsaw29a90f02002-04-22 21:48:20 +0000337(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 Warsaw88491612002-04-25 21:31:47 +0000349(defvar py-shell-alist
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000350 '(("jython" . 'jython)
Barry Warsaw88491612002-04-25 21:31:47 +0000351 ("python" . 'cpython))
352 "*Alist of interpreters and python shells. Used by `py-choose-shell'
353to select the appropriate python interpreter mode for a file.")
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000354
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000355(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 Warsawc72c11c1997-08-09 06:42:08 +0000373
374;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
375;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
376
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000377(defvar py-line-number-offset 0
378 "When an exception occurs as a result of py-execute-region, a
379subsequent py-up-exception needs the line number where the region
380started, in order to jump to the correct file line. This variable is
381set in py-execute-region and used in py-jump-to-exception.")
382
Barry Warsaw5e21cb01997-11-05 18:41:11 +0000383(defconst py-emacs-features
384 (let (features)
Barry Warsaw5e21cb01997-11-05 18:41:11 +0000385 features)
Barry Warsawc12c62e1997-09-04 04:18:07 +0000386 "A list of features extant in the Emacs you are using.
Barry Warsaw6ae21ad1997-11-06 14:36:49 +0000387There are many flavors of Emacs out there, with different levels of
388support for features needed by `python-mode'.")
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000389
Barry Warsaw29a90f02002-04-22 21:48:20 +0000390;; 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
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000395;; 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 Warsaw29a90f02002-04-22 21:48:20 +0000410(defun py-font-lock-mode-hook ()
411 (or (face-differs-from-default-p 'py-pseudo-keyword-face)
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000412 (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 Warsaw29a90f02002-04-22 21:48:20 +0000420(add-hook 'font-lock-mode-hook 'py-font-lock-mode-hook)
421
Barry Warsawfb07f401997-02-24 03:37:22 +0000422(defvar python-font-lock-keywords
Barry Warsawb8f11661997-11-06 14:35:15 +0000423 (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"
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000430 "return" "while" "with" "yield"
Barry Warsawb8f11661997-11-06 14:35:15 +0000431 )
432 "\\|"))
433 (kw2 (mapconcat 'identity
434 '("else:" "except:" "finally:" "try:")
435 "\\|"))
Skip Montanaro302e5502003-12-18 21:53:33 +0000436 (kw3 (mapconcat 'identity
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000437 ;; Don't include True, False, None, or
438 ;; Ellipsis in this list, since they are
439 ;; already defined as pseudo keywords.
440 '("__debug__"
Skip Montanaro302e5502003-12-18 21:53:33 +0000441 "__import__" "__name__" "abs" "apply" "basestring"
442 "bool" "buffer" "callable" "chr" "classmethod"
443 "cmp" "coerce" "compile" "complex" "copyright"
444 "delattr" "dict" "dir" "divmod"
445 "enumerate" "eval" "execfile" "exit" "file"
446 "filter" "float" "getattr" "globals" "hasattr"
447 "hash" "hex" "id" "input" "int" "intern"
448 "isinstance" "issubclass" "iter" "len" "license"
449 "list" "locals" "long" "map" "max" "min" "object"
450 "oct" "open" "ord" "pow" "property" "range"
451 "raw_input" "reduce" "reload" "repr" "round"
452 "setattr" "slice" "staticmethod" "str" "sum"
453 "super" "tuple" "type" "unichr" "unicode" "vars"
454 "xrange" "zip")
455 "\\|"))
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000456 (kw4 (mapconcat 'identity
457 ;; Exceptions and warnings
458 '("ArithmeticError" "AssertionError"
459 "AttributeError" "DeprecationWarning" "EOFError"
460 "EnvironmentError" "Exception"
461 "FloatingPointError" "FutureWarning" "IOError"
462 "ImportError" "IndentationError" "IndexError"
463 "KeyError" "KeyboardInterrupt" "LookupError"
464 "MemoryError" "NameError" "NotImplemented"
465 "NotImplementedError" "OSError" "OverflowError"
466 "OverflowWarning" "PendingDeprecationWarning"
467 "ReferenceError" "RuntimeError" "RuntimeWarning"
468 "StandardError" "StopIteration" "SyntaxError"
469 "SyntaxWarning" "SystemError" "SystemExit"
470 "TabError" "TypeError" "UnboundLocalError"
471 "UnicodeDecodeError" "UnicodeEncodeError"
472 "UnicodeError" "UnicodeTranslateError"
473 "UserWarning" "ValueError" "Warning"
474 "ZeroDivisionError")
475 "\\|"))
Barry Warsawb8f11661997-11-06 14:35:15 +0000476 )
Barry Warsaw33ab6e41996-03-05 00:44:31 +0000477 (list
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000478 '("^[ \t]*\\(@.+\\)" 1 'py-decorators-face)
Barry Warsawef3c8911997-11-05 18:55:50 +0000479 ;; keywords
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000480 (cons (concat "\\<\\(" kw1 "\\)\\>[ \n\t(]") 1)
Skip Montanaro302e5502003-12-18 21:53:33 +0000481 ;; builtins when they don't appear as object attributes
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000482 (list (concat "\\([^. \t]\\|^\\)[ \t]*\\<\\(" kw3 "\\)\\>[ \n\t(]") 2
483 'py-builtins-face)
Barry Warsawb8f11661997-11-06 14:35:15 +0000484 ;; block introducing keywords with immediately following colons.
485 ;; Yes "except" is in both lists.
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000486 (cons (concat "\\<\\(" kw2 "\\)[ \n\t(]") 1)
487 ;; Exceptions
488 (list (concat "\\<\\(" kw4 "\\)[ \n\t:,(]") 1 'py-builtins-face)
489 ;; `as' but only in "import foo as bar" or "with foo as bar"
490 '("[ \t]*\\(\\<from\\>.*\\)?\\<import\\>.*\\<\\(as\\)\\>" . 2)
491 '("[ \t]*\\<with\\>.*\\<\\(as\\)\\>" . 1)
Barry Warsaw33ab6e41996-03-05 00:44:31 +0000492 ;; classes
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000493 '("\\<class[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)" 1 font-lock-type-face)
Barry Warsaw33ab6e41996-03-05 00:44:31 +0000494 ;; functions
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000495 '("\\<def[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
Barry Warsaw33ab6e41996-03-05 00:44:31 +0000496 1 font-lock-function-name-face)
Barry Warsaw29a90f02002-04-22 21:48:20 +0000497 ;; pseudo-keywords
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000498 '("\\<\\(self\\|None\\|True\\|False\\|Ellipsis\\)\\>"
Barry Warsaw29a90f02002-04-22 21:48:20 +0000499 1 py-pseudo-keyword-face)
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000500 ;; XXX, TODO, and FIXME tags
501 '("XXX\\|TODO\\|FIXME" 0 py-XXX-tag-face t)
Barry Warsaw33ab6e41996-03-05 00:44:31 +0000502 ))
Barry Warsaw62d9d6e1996-03-06 20:32:27 +0000503 "Additional expressions to highlight in Python mode.")
Barry Warsawfb07f401997-02-24 03:37:22 +0000504(put 'python-mode 'font-lock-defaults '(python-font-lock-keywords))
505
Barry Warsawe467bfb1997-11-26 05:40:58 +0000506;; have to bind py-file-queue before installing the kill-emacs-hook
Barry Warsaw7ae77681994-12-12 20:38:05 +0000507(defvar py-file-queue nil
508 "Queue of Python temp files awaiting execution.
509Currently-active file is at the head of the list.")
510
Barry Warsaw4f577d22001-04-11 20:23:17 +0000511(defvar py-pdbtrack-is-tracking-p nil)
Ken Manheimered6000a2003-03-03 17:09:44 +0000512
Barry Warsaw29a90f02002-04-22 21:48:20 +0000513(defvar py-pychecker-history nil)
Barry Warsaw4f577d22001-04-11 20:23:17 +0000514
515
Barry Warsawc72c11c1997-08-09 06:42:08 +0000516
517;; Constants
518
Barry Warsawc72c11c1997-08-09 06:42:08 +0000519(defconst py-stringlit-re
520 (concat
Barry Warsaw751f4931998-05-19 16:06:21 +0000521 ;; These fail if backslash-quote ends the string (not worth
522 ;; fixing?). They precede the short versions so that the first two
523 ;; quotes don't look like an empty short string.
524 ;;
525 ;; (maybe raw), long single quoted triple quoted strings (SQTQ),
526 ;; with potential embedded single quotes
527 "[rR]?'''[^']*\\(\\('[^']\\|''[^']\\)[^']*\\)*'''"
528 "\\|"
529 ;; (maybe raw), long double quoted triple quoted strings (DQTQ),
530 ;; with potential embedded double quotes
531 "[rR]?\"\"\"[^\"]*\\(\\(\"[^\"]\\|\"\"[^\"]\\)[^\"]*\\)*\"\"\""
532 "\\|"
533 "[rR]?'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
Barry Warsawc72c11c1997-08-09 06:42:08 +0000534 "\\|" ; or
Barry Warsaw751f4931998-05-19 16:06:21 +0000535 "[rR]?\"\\([^\"\n\\]\\|\\\\.\\)*\"" ; double-quoted
Barry Warsaw41a05c71998-08-10 16:33:12 +0000536 )
537 "Regular expression matching a Python string literal.")
Barry Warsaw751f4931998-05-19 16:06:21 +0000538
Barry Warsawc72c11c1997-08-09 06:42:08 +0000539(defconst py-continued-re
Barry Warsaw41a05c71998-08-10 16:33:12 +0000540 ;; This is tricky because a trailing backslash does not mean
541 ;; continuation if it's in a comment
Barry Warsawc72c11c1997-08-09 06:42:08 +0000542 (concat
543 "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
Barry Warsaw41a05c71998-08-10 16:33:12 +0000544 "\\\\$")
545 "Regular expression matching Python backslash continuation lines.")
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000546
Barry Warsaw41a05c71998-08-10 16:33:12 +0000547(defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
Barry Warsaw71c3adb1998-08-10 16:34:33 +0000548 "Regular expression matching a blank or comment line.")
Barry Warsawc72c11c1997-08-09 06:42:08 +0000549
Barry Warsawc72c11c1997-08-09 06:42:08 +0000550(defconst py-outdent-re
551 (concat "\\(" (mapconcat 'identity
552 '("else:"
553 "except\\(\\s +.*\\)?:"
554 "finally:"
555 "elif\\s +.*:")
556 "\\|")
Barry Warsaw41a05c71998-08-10 16:33:12 +0000557 "\\)")
558 "Regular expression matching statements to be dedented one level.")
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000559
Barry Warsawaffc0ca1997-11-03 16:59:38 +0000560(defconst py-block-closing-keywords-re
Barry Warsaw41a05c71998-08-10 16:33:12 +0000561 "\\(return\\|raise\\|break\\|continue\\|pass\\)"
562 "Regular expression matching keywords which typically close a block.")
Barry Warsawaffc0ca1997-11-03 16:59:38 +0000563
Barry Warsawc72c11c1997-08-09 06:42:08 +0000564(defconst py-no-outdent-re
Barry Warsawaffc0ca1997-11-03 16:59:38 +0000565 (concat
566 "\\("
567 (mapconcat 'identity
568 (list "try:"
569 "except\\(\\s +.*\\)?:"
570 "while\\s +.*:"
571 "for\\s +.*:"
572 "if\\s +.*:"
573 "elif\\s +.*:"
574 (concat py-block-closing-keywords-re "[ \t\n]")
575 )
576 "\\|")
Barry Warsaw41a05c71998-08-10 16:33:12 +0000577 "\\)")
578 "Regular expression matching lines not to dedent after.")
Barry Warsawc72c11c1997-08-09 06:42:08 +0000579
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000580(defvar py-traceback-line-re
Barry Warsaw41a05c71998-08-10 16:33:12 +0000581 "[ \t]+File \"\\([^\"]+\\)\", line \\([0-9]+\\)"
582 "Regular expression that describes tracebacks.")
Barry Warsawc72c11c1997-08-09 06:42:08 +0000583
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000584;; pdbtrack constants
Barry Warsaw4f577d22001-04-11 20:23:17 +0000585(defconst py-pdbtrack-stack-entry-regexp
Ken Manheimere7aca522003-03-03 00:35:32 +0000586; "^> \\([^(]+\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_]+\\)()"
587 "^> \\(.*\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_]+\\)()"
Barry Warsaw4f577d22001-04-11 20:23:17 +0000588 "Regular expression pdbtrack uses to find a stack trace entry.")
589
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000590(defconst py-pdbtrack-input-prompt "\n[(<]*[Pp]db[>)]+ "
Barry Warsaw4f577d22001-04-11 20:23:17 +0000591 "Regular expression pdbtrack uses to recognize a pdb prompt.")
592
593(defconst py-pdbtrack-track-range 10000
594 "Max number of characters from end of buffer to search for stack entry.")
595
Barry Warsawc72c11c1997-08-09 06:42:08 +0000596
597
Barry Warsawc72c11c1997-08-09 06:42:08 +0000598;; Major mode boilerplate
599
Barry Warsaw7ae77681994-12-12 20:38:05 +0000600;; define a mode-specific abbrev table for those who use such things
601(defvar python-mode-abbrev-table nil
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000602 "Abbrev table in use in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000603(define-abbrev-table 'python-mode-abbrev-table nil)
604
Barry Warsaw7ae77681994-12-12 20:38:05 +0000605(defvar python-mode-hook nil
606 "*Hook called by `python-mode'.")
607
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000608(make-obsolete-variable 'jpython-mode-hook 'jython-mode-hook)
609(defvar jython-mode-hook nil
610 "*Hook called by `jython-mode'. `jython-mode' also calls
Barry Warsaw88491612002-04-25 21:31:47 +0000611`python-mode-hook'.")
612
Barry Warsaw56bd2ed2002-04-25 15:44:17 +0000613(defvar py-shell-hook nil
614 "*Hook called by `py-shell'.")
615
Barry Warsaw145ab1c1998-05-19 14:49:49 +0000616;; In previous version of python-mode.el, the hook was incorrectly
617;; called py-mode-hook, and was not defvar'd. Deprecate its use.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000618(and (fboundp 'make-obsolete-variable)
619 (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
620
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000621(defvar py-mode-map ()
622 "Keymap used in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000623(if py-mode-map
Barry Warsawc72c11c1997-08-09 06:42:08 +0000624 nil
Barry Warsaw7ae77681994-12-12 20:38:05 +0000625 (setq py-mode-map (make-sparse-keymap))
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000626 ;; electric keys
627 (define-key py-mode-map ":" 'py-electric-colon)
628 ;; indentation level modifiers
629 (define-key py-mode-map "\C-c\C-l" 'py-shift-region-left)
630 (define-key py-mode-map "\C-c\C-r" 'py-shift-region-right)
631 (define-key py-mode-map "\C-c<" 'py-shift-region-left)
632 (define-key py-mode-map "\C-c>" 'py-shift-region-right)
633 ;; subprocess commands
634 (define-key py-mode-map "\C-c\C-c" 'py-execute-buffer)
Barry Warsaw820273d1998-05-19 15:54:45 +0000635 (define-key py-mode-map "\C-c\C-m" 'py-execute-import-or-reload)
Barry Warsaw1d0364b1998-05-19 16:15:26 +0000636 (define-key py-mode-map "\C-c\C-s" 'py-execute-string)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000637 (define-key py-mode-map "\C-c|" 'py-execute-region)
Barry Warsaw820273d1998-05-19 15:54:45 +0000638 (define-key py-mode-map "\e\C-x" 'py-execute-def-or-class)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000639 (define-key py-mode-map "\C-c!" 'py-shell)
Barry Warsawa2398801998-04-09 23:28:20 +0000640 (define-key py-mode-map "\C-c\C-t" 'py-toggle-shells)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000641 ;; Caution! Enter here at your own risk. We are trying to support
642 ;; several behaviors and it gets disgusting. :-( This logic ripped
643 ;; largely from CC Mode.
644 ;;
645 ;; In XEmacs 19, Emacs 19, and Emacs 20, we use this to bind
646 ;; backwards deletion behavior to DEL, which both Delete and
647 ;; Backspace get translated to. There's no way to separate this
648 ;; behavior in a clean way, so deal with it! Besides, it's been
649 ;; this way since the dawn of time.
650 (if (not (boundp 'delete-key-deletes-forward))
651 (define-key py-mode-map "\177" 'py-electric-backspace)
652 ;; However, XEmacs 20 actually achieved enlightenment. It is
653 ;; possible to sanely define both backward and forward deletion
654 ;; behavior under X separately (TTYs are forever beyond hope, but
655 ;; who cares? XEmacs 20 does the right thing with these too).
656 (define-key py-mode-map [delete] 'py-electric-delete)
657 (define-key py-mode-map [backspace] 'py-electric-backspace))
Barry Warsaw8c4a8de1997-11-26 20:30:33 +0000658 ;; Separate M-BS from C-M-h. The former should remain
659 ;; backward-kill-word.
660 (define-key py-mode-map [(control meta h)] 'py-mark-def-or-class)
Barry Warsaw2518c671997-11-05 00:51:08 +0000661 (define-key py-mode-map "\C-c\C-k" 'py-mark-block)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000662 ;; Miscellaneous
663 (define-key py-mode-map "\C-c:" 'py-guess-indent-offset)
664 (define-key py-mode-map "\C-c\t" 'py-indent-region)
Barry Warsaw4f577d22001-04-11 20:23:17 +0000665 (define-key py-mode-map "\C-c\C-d" 'py-pdbtrack-toggle-stack-tracking)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000666 (define-key py-mode-map "\C-c\C-n" 'py-next-statement)
667 (define-key py-mode-map "\C-c\C-p" 'py-previous-statement)
668 (define-key py-mode-map "\C-c\C-u" 'py-goto-block-up)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000669 (define-key py-mode-map "\C-c#" 'py-comment-region)
670 (define-key py-mode-map "\C-c?" 'py-describe-mode)
Barry Warsawa7cc43b2002-04-22 17:15:19 +0000671 (define-key py-mode-map "\C-c\C-h" 'py-help-at-point)
Barry Warsawab0e86c1998-05-19 15:31:46 +0000672 (define-key py-mode-map "\e\C-a" 'py-beginning-of-def-or-class)
673 (define-key py-mode-map "\e\C-e" 'py-end-of-def-or-class)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000674 (define-key py-mode-map "\C-c-" 'py-up-exception)
675 (define-key py-mode-map "\C-c=" 'py-down-exception)
Barry Warsawf8ddb6a1999-01-18 21:49:39 +0000676 ;; stuff that is `standard' but doesn't interface well with
677 ;; python-mode, which forces us to rebind to special commands
678 (define-key py-mode-map "\C-xnd" 'py-narrow-to-defun)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +0000679 ;; information
680 (define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
681 (define-key py-mode-map "\C-c\C-v" 'py-version)
Barry Warsaw29a90f02002-04-22 21:48:20 +0000682 (define-key py-mode-map "\C-c\C-w" 'py-pychecker-run)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000683 ;; shadow global bindings for newline-and-indent w/ the py- version.
684 ;; BAW - this is extremely bad form, but I'm not going to change it
685 ;; for now.
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000686 (mapcar #'(lambda (key)
687 (define-key py-mode-map key 'py-newline-and-indent))
688 (where-is-internal 'newline-and-indent))
Barry Warsawf19feb81999-01-21 17:06:11 +0000689 ;; Force RET to be py-newline-and-indent even if it didn't get
690 ;; mapped by the above code. motivation: Emacs' default binding for
691 ;; RET is `newline' and C-j is `newline-and-indent'. Most Pythoneers
692 ;; expect RET to do a `py-newline-and-indent' and any Emacsers who
693 ;; dislike this are probably knowledgeable enough to do a rebind.
694 ;; However, we do *not* change C-j since many Emacsers have already
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000695 ;; swapped RET and C-j and they don't want C-j bound to `newline' to
Barry Warsawf19feb81999-01-21 17:06:11 +0000696 ;; change.
697 (define-key py-mode-map "\C-m" 'py-newline-and-indent)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000698 )
699
700(defvar py-mode-output-map nil
Barry Warsaw41a05c71998-08-10 16:33:12 +0000701 "Keymap used in *Python Output* buffers.")
Barry Warsawa0ee8cd1997-11-26 01:04:44 +0000702(if py-mode-output-map
703 nil
704 (setq py-mode-output-map (make-sparse-keymap))
705 (define-key py-mode-output-map [button2] 'py-mouseto-exception)
706 (define-key py-mode-output-map "\C-c\C-c" 'py-goto-exception)
707 ;; TBD: Disable all self-inserting keys. This is bogus, we should
708 ;; really implement this as *Python Output* buffer being read-only
709 (mapcar #' (lambda (key)
710 (define-key py-mode-output-map key
711 #'(lambda () (interactive) (beep))))
712 (where-is-internal 'self-insert-command))
Barry Warsaw850437a1995-03-08 21:50:28 +0000713 )
Barry Warsaw7ae77681994-12-12 20:38:05 +0000714
Barry Warsaw6dfbe5d1998-08-20 21:51:27 +0000715(defvar py-shell-map nil
716 "Keymap used in *Python* shell buffers.")
717(if py-shell-map
718 nil
719 (setq py-shell-map (copy-keymap comint-mode-map))
720 (define-key py-shell-map [tab] 'tab-to-tab-stop)
721 (define-key py-shell-map "\C-c-" 'py-up-exception)
722 (define-key py-shell-map "\C-c=" 'py-down-exception)
723 )
724
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000725(defvar py-mode-syntax-table nil
726 "Syntax table used in `python-mode' buffers.")
Barry Warsawa7cc43b2002-04-22 17:15:19 +0000727(when (not py-mode-syntax-table)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000728 (setq py-mode-syntax-table (make-syntax-table))
Barry Warsawc72c11c1997-08-09 06:42:08 +0000729 (modify-syntax-entry ?\( "()" py-mode-syntax-table)
730 (modify-syntax-entry ?\) ")(" py-mode-syntax-table)
731 (modify-syntax-entry ?\[ "(]" py-mode-syntax-table)
732 (modify-syntax-entry ?\] ")[" py-mode-syntax-table)
733 (modify-syntax-entry ?\{ "(}" py-mode-syntax-table)
734 (modify-syntax-entry ?\} "){" py-mode-syntax-table)
735 ;; Add operator symbols misassigned in the std table
736 (modify-syntax-entry ?\$ "." py-mode-syntax-table)
737 (modify-syntax-entry ?\% "." py-mode-syntax-table)
738 (modify-syntax-entry ?\& "." py-mode-syntax-table)
739 (modify-syntax-entry ?\* "." py-mode-syntax-table)
740 (modify-syntax-entry ?\+ "." py-mode-syntax-table)
741 (modify-syntax-entry ?\- "." py-mode-syntax-table)
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 ;; For historical reasons, underscore is word class instead of
748 ;; symbol class. GNU conventions say it should be symbol class, but
749 ;; there's a natural conflict between what major mode authors want
750 ;; and what users expect from `forward-word' and `backward-word'.
751 ;; Guido and I have hashed this out and have decided to keep
752 ;; underscore in word class. If you're tempted to change it, try
753 ;; binding M-f and M-b to py-forward-into-nomenclature and
Barry Warsawaa384fd1999-02-16 23:36:16 +0000754 ;; py-backward-into-nomenclature instead. This doesn't help in all
755 ;; situations where you'd want the different behavior
756 ;; (e.g. backward-kill-word).
Barry Warsawc72c11c1997-08-09 06:42:08 +0000757 (modify-syntax-entry ?\_ "w" py-mode-syntax-table)
758 ;; Both single quote and double quote are string delimiters
759 (modify-syntax-entry ?\' "\"" py-mode-syntax-table)
760 (modify-syntax-entry ?\" "\"" py-mode-syntax-table)
761 ;; backquote is open and close paren
762 (modify-syntax-entry ?\` "$" py-mode-syntax-table)
763 ;; comment delimiters
764 (modify-syntax-entry ?\# "<" py-mode-syntax-table)
765 (modify-syntax-entry ?\n ">" py-mode-syntax-table)
766 )
Barry Warsaw7ae77681994-12-12 20:38:05 +0000767
Barry Warsawa7cc43b2002-04-22 17:15:19 +0000768;; An auxiliary syntax table which places underscore and dot in the
769;; symbol class for simplicity
770(defvar py-dotted-expression-syntax-table nil
771 "Syntax table used to identify Python dotted expressions.")
772(when (not py-dotted-expression-syntax-table)
773 (setq py-dotted-expression-syntax-table
774 (copy-syntax-table py-mode-syntax-table))
775 (modify-syntax-entry ?_ "_" py-dotted-expression-syntax-table)
776 (modify-syntax-entry ?. "_" py-dotted-expression-syntax-table))
777
Barry Warsawb3e81d51996-09-04 15:12:42 +0000778
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000779
Barry Warsawbc3760b1998-09-14 16:16:18 +0000780;; Utilities
Barry Warsawbc3760b1998-09-14 16:16:18 +0000781(defmacro py-safe (&rest body)
782 "Safely execute BODY, return nil if an error occurred."
783 (` (condition-case nil
784 (progn (,@ body))
785 (error nil))))
786
787(defsubst py-keep-region-active ()
788 "Keep the region active in XEmacs."
789 ;; Ignore byte-compiler warnings you might see. Also note that
790 ;; FSF's Emacs 19 does it differently; its policy doesn't require us
791 ;; to take explicit action.
792 (and (boundp 'zmacs-region-stays)
793 (setq zmacs-region-stays t)))
794
795(defsubst py-point (position)
796 "Returns the value of point at certain commonly referenced POSITIONs.
797POSITION can be one of the following symbols:
798
799 bol -- beginning of line
800 eol -- end of line
801 bod -- beginning of def or class
802 eod -- end of def or class
803 bob -- beginning of buffer
804 eob -- end of buffer
805 boi -- back to indentation
806 bos -- beginning of statement
807
808This function does not modify point or mark."
809 (let ((here (point)))
810 (cond
811 ((eq position 'bol) (beginning-of-line))
812 ((eq position 'eol) (end-of-line))
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000813 ((eq position 'bod) (py-beginning-of-def-or-class 'either))
814 ((eq position 'eod) (py-end-of-def-or-class 'either))
Barry Warsawbc3760b1998-09-14 16:16:18 +0000815 ;; Kind of funny, I know, but useful for py-up-exception.
816 ((eq position 'bob) (beginning-of-buffer))
817 ((eq position 'eob) (end-of-buffer))
818 ((eq position 'boi) (back-to-indentation))
819 ((eq position 'bos) (py-goto-initial-line))
820 (t (error "Unknown buffer position requested: %s" position))
821 )
822 (prog1
823 (point)
824 (goto-char here))))
825
826(defsubst py-highlight-line (from to file line)
827 (cond
828 ((fboundp 'make-extent)
829 ;; XEmacs
830 (let ((e (make-extent from to)))
831 (set-extent-property e 'mouse-face 'highlight)
832 (set-extent-property e 'py-exc-info (cons file line))
833 (set-extent-property e 'keymap py-mode-output-map)))
834 (t
835 ;; Emacs -- Please port this!
836 )
837 ))
838
839(defun py-in-literal (&optional lim)
840 "Return non-nil if point is in a Python literal (a comment or string).
841Optional argument LIM indicates the beginning of the containing form,
842i.e. the limit on how far back to scan."
843 ;; This is the version used for non-XEmacs, which has a nicer
844 ;; interface.
845 ;;
846 ;; WARNING: Watch out for infinite recursion.
847 (let* ((lim (or lim (py-point 'bod)))
848 (state (parse-partial-sexp lim (point))))
849 (cond
850 ((nth 3 state) 'string)
851 ((nth 4 state) 'comment)
852 (t nil))))
853
854;; XEmacs has a built-in function that should make this much quicker.
855;; In this case, lim is ignored
856(defun py-fast-in-literal (&optional lim)
857 "Fast version of `py-in-literal', used only by XEmacs.
858Optional LIM is ignored."
859 ;; don't have to worry about context == 'block-comment
860 (buffer-syntactic-context))
861
862(if (fboundp 'buffer-syntactic-context)
863 (defalias 'py-in-literal 'py-fast-in-literal))
864
865
866
Barry Warsaw42f707f1996-07-29 21:05:05 +0000867;; Menu definitions, only relevent if you have the easymenu.el package
868;; (standard in the latest Emacs 19 and XEmacs 19 distributions).
Barry Warsaw5490a061996-08-06 15:43:33 +0000869(defvar py-menu nil
870 "Menu for Python Mode.
Barry Warsawc72c11c1997-08-09 06:42:08 +0000871This menu will get created automatically if you have the `easymenu'
872package. Note that the latest X/Emacs releases contain this package.")
Barry Warsaw5490a061996-08-06 15:43:33 +0000873
Barry Warsawc72c11c1997-08-09 06:42:08 +0000874(and (py-safe (require 'easymenu) t)
875 (easy-menu-define
876 py-menu py-mode-map "Python Mode menu"
877 '("Python"
878 ["Comment Out Region" py-comment-region (mark)]
879 ["Uncomment Region" (py-comment-region (point) (mark) '(4)) (mark)]
880 "-"
881 ["Mark current block" py-mark-block t]
Barry Warsaw2518c671997-11-05 00:51:08 +0000882 ["Mark current def" py-mark-def-or-class t]
883 ["Mark current class" (py-mark-def-or-class t) t]
Barry Warsawc72c11c1997-08-09 06:42:08 +0000884 "-"
885 ["Shift region left" py-shift-region-left (mark)]
886 ["Shift region right" py-shift-region-right (mark)]
887 "-"
Barry Warsaw820273d1998-05-19 15:54:45 +0000888 ["Import/reload file" py-execute-import-or-reload t]
Barry Warsawc72c11c1997-08-09 06:42:08 +0000889 ["Execute buffer" py-execute-buffer t]
890 ["Execute region" py-execute-region (mark)]
Barry Warsaw820273d1998-05-19 15:54:45 +0000891 ["Execute def or class" py-execute-def-or-class (mark)]
Barry Warsaw1d0364b1998-05-19 16:15:26 +0000892 ["Execute string" py-execute-string t]
Barry Warsawc72c11c1997-08-09 06:42:08 +0000893 ["Start interpreter..." py-shell t]
894 "-"
895 ["Go to start of block" py-goto-block-up t]
Barry Warsawab0e86c1998-05-19 15:31:46 +0000896 ["Go to start of class" (py-beginning-of-def-or-class t) t]
897 ["Move to end of class" (py-end-of-def-or-class t) t]
898 ["Move to start of def" py-beginning-of-def-or-class t]
899 ["Move to end of def" py-end-of-def-or-class t]
Barry Warsawc72c11c1997-08-09 06:42:08 +0000900 "-"
901 ["Describe mode" py-describe-mode t]
902 )))
Barry Warsaw42f707f1996-07-29 21:05:05 +0000903
Barry Warsaw81437461996-08-01 19:48:02 +0000904
905
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000906;; Imenu definitions
907(defvar py-imenu-class-regexp
Barry Warsaw81437461996-08-01 19:48:02 +0000908 (concat ; <<classes>>
909 "\\(" ;
910 "^[ \t]*" ; newline and maybe whitespace
911 "\\(class[ \t]+[a-zA-Z0-9_]+\\)" ; class name
912 ; possibly multiple superclasses
Barry Warsaw3179fe01998-04-04 21:36:53 +0000913 "\\([ \t]*\\((\\([a-zA-Z0-9_,. \t\n]\\)*)\\)?\\)"
Barry Warsaw81437461996-08-01 19:48:02 +0000914 "[ \t]*:" ; and the final :
915 "\\)" ; >>classes<<
916 )
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000917 "Regexp for Python classes for use with the Imenu package."
Barry Warsaw81437461996-08-01 19:48:02 +0000918 )
919
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000920(defvar py-imenu-method-regexp
Barry Warsaw81437461996-08-01 19:48:02 +0000921 (concat ; <<methods and functions>>
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000922 "\\(" ;
Barry Warsaw81437461996-08-01 19:48:02 +0000923 "^[ \t]*" ; new line and maybe whitespace
924 "\\(def[ \t]+" ; function definitions start with def
925 "\\([a-zA-Z0-9_]+\\)" ; name is here
926 ; function arguments...
Barry Warsaw1d5f9881998-10-28 04:08:13 +0000927;; "[ \t]*(\\([-+/a-zA-Z0-9_=,\* \t\n.()\"'#]*\\))"
928 "[ \t]*(\\([^:#]*\\))"
Barry Warsaw81437461996-08-01 19:48:02 +0000929 "\\)" ; end of def
930 "[ \t]*:" ; and then the :
931 "\\)" ; >>methods and functions<<
932 )
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000933 "Regexp for Python methods/functions for use with the Imenu package."
Barry Warsaw81437461996-08-01 19:48:02 +0000934 )
935
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000936(defvar py-imenu-method-no-arg-parens '(2 8)
937 "Indices into groups of the Python regexp for use with Imenu.
Barry Warsaw81437461996-08-01 19:48:02 +0000938
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000939Using these values will result in smaller Imenu lists, as arguments to
Barry Warsaw81437461996-08-01 19:48:02 +0000940functions are not listed.
941
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000942See the variable `py-imenu-show-method-args-p' for more
Barry Warsaw81437461996-08-01 19:48:02 +0000943information.")
944
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000945(defvar py-imenu-method-arg-parens '(2 7)
Barry Warsaw1bbc0311998-10-27 21:54:56 +0000946 "Indices into groups of the Python regexp for use with imenu.
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000947Using these values will result in large Imenu lists, as arguments to
Barry Warsaw81437461996-08-01 19:48:02 +0000948functions are listed.
949
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000950See the variable `py-imenu-show-method-args-p' for more
Barry Warsaw81437461996-08-01 19:48:02 +0000951information.")
952
953;; Note that in this format, this variable can still be used with the
954;; imenu--generic-function. Otherwise, there is no real reason to have
955;; it.
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000956(defvar py-imenu-generic-expression
Barry Warsaw81437461996-08-01 19:48:02 +0000957 (cons
Skip Montanarodc6d9e12008-02-05 02:32:16 +0000958 (concat
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000959 py-imenu-class-regexp
Barry Warsaw81437461996-08-01 19:48:02 +0000960 "\\|" ; or...
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000961 py-imenu-method-regexp
Barry Warsaw81437461996-08-01 19:48:02 +0000962 )
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000963 py-imenu-method-no-arg-parens)
964 "Generic Python expression which may be used directly with Imenu.
Barry Warsaw81437461996-08-01 19:48:02 +0000965Used by setting the variable `imenu-generic-expression' to this value.
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000966Also, see the function \\[py-imenu-create-index] for a better
967alternative for finding the index.")
Barry Warsaw81437461996-08-01 19:48:02 +0000968
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000969;; These next two variables are used when searching for the Python
Barry Warsaw81437461996-08-01 19:48:02 +0000970;; class/definitions. Just saving some time in accessing the
971;; generic-python-expression, really.
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000972(defvar py-imenu-generic-regexp nil)
973(defvar py-imenu-generic-parens nil)
Barry Warsaw81437461996-08-01 19:48:02 +0000974
975
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000976(defun py-imenu-create-index-function ()
977 "Python interface function for the Imenu package.
978Finds all Python classes and functions/methods. Calls function
979\\[py-imenu-create-index-engine]. See that function for the details
980of how this works."
981 (setq py-imenu-generic-regexp (car py-imenu-generic-expression)
982 py-imenu-generic-parens (if py-imenu-show-method-args-p
983 py-imenu-method-arg-parens
984 py-imenu-method-no-arg-parens))
Barry Warsaw81437461996-08-01 19:48:02 +0000985 (goto-char (point-min))
Barry Warsaw1d5f9881998-10-28 04:08:13 +0000986 ;; Warning: When the buffer has no classes or functions, this will
987 ;; return nil, which seems proper according to the Imenu API, but
988 ;; causes an error in the XEmacs port of Imenu. Sigh.
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000989 (py-imenu-create-index-engine nil))
Barry Warsaw81437461996-08-01 19:48:02 +0000990
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000991(defun py-imenu-create-index-engine (&optional start-indent)
992 "Function for finding Imenu definitions in Python.
Barry Warsaw81437461996-08-01 19:48:02 +0000993
994Finds all definitions (classes, methods, or functions) in a Python
Barry Warsaw6839d3a1998-10-28 00:10:45 +0000995file for the Imenu package.
Barry Warsaw81437461996-08-01 19:48:02 +0000996
997Returns a possibly nested alist of the form
998
999 (INDEX-NAME . INDEX-POSITION)
1000
1001The second element of the alist may be an alist, producing a nested
1002list as in
1003
1004 (INDEX-NAME . INDEX-ALIST)
1005
1006This function should not be called directly, as it calls itself
1007recursively and requires some setup. Rather this is the engine for
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001008the function \\[py-imenu-create-index-function].
Barry Warsaw81437461996-08-01 19:48:02 +00001009
1010It works recursively by looking for all definitions at the current
1011indention level. When it finds one, it adds it to the alist. If it
1012finds a definition at a greater indentation level, it removes the
Barry Warsaw145ab1c1998-05-19 14:49:49 +00001013previous definition from the alist. In its place it adds all
Barry Warsaw81437461996-08-01 19:48:02 +00001014definitions found at the next indentation level. When it finds a
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001015definition that is less indented then the current level, it returns
1016the alist it has created thus far.
Barry Warsaw81437461996-08-01 19:48:02 +00001017
1018The optional argument START-INDENT indicates the starting indentation
1019at which to continue looking for Python classes, methods, or
1020functions. If this is not supplied, the function uses the indentation
1021of the first definition found."
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001022 (let (index-alist
1023 sub-method-alist
Barry Warsaw81437461996-08-01 19:48:02 +00001024 looking-p
1025 def-name prev-name
1026 cur-indent def-pos
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001027 (class-paren (first py-imenu-generic-parens))
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001028 (def-paren (second py-imenu-generic-parens)))
Barry Warsaw81437461996-08-01 19:48:02 +00001029 (setq looking-p
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001030 (re-search-forward py-imenu-generic-regexp (point-max) t))
Barry Warsaw81437461996-08-01 19:48:02 +00001031 (while looking-p
1032 (save-excursion
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001033 ;; used to set def-name to this value but generic-extract-name
1034 ;; is new to imenu-1.14. this way it still works with
1035 ;; imenu-1.11
1036 ;;(imenu--generic-extract-name py-imenu-generic-parens))
Barry Warsaw81437461996-08-01 19:48:02 +00001037 (let ((cur-paren (if (match-beginning class-paren)
1038 class-paren def-paren)))
1039 (setq def-name
Barry Warsawc8520351997-11-26 06:14:40 +00001040 (buffer-substring-no-properties (match-beginning cur-paren)
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001041 (match-end cur-paren))))
Barry Warsaw93c88cc1998-08-18 02:00:44 +00001042 (save-match-data
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001043 (py-beginning-of-def-or-class 'either))
Barry Warsaw81437461996-08-01 19:48:02 +00001044 (beginning-of-line)
1045 (setq cur-indent (current-indentation)))
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001046 ;; HACK: want to go to the next correct definition location. We
1047 ;; explicitly list them here but it would be better to have them
1048 ;; in a list.
Barry Warsaw81437461996-08-01 19:48:02 +00001049 (setq def-pos
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001050 (or (match-beginning class-paren)
1051 (match-beginning def-paren)))
Barry Warsaw81437461996-08-01 19:48:02 +00001052 ;; if we don't have a starting indent level, take this one
1053 (or start-indent
1054 (setq start-indent cur-indent))
Barry Warsaw81437461996-08-01 19:48:02 +00001055 ;; if we don't have class name yet, take this one
1056 (or prev-name
1057 (setq prev-name def-name))
Barry Warsaw81437461996-08-01 19:48:02 +00001058 ;; what level is the next definition on? must be same, deeper
1059 ;; or shallower indentation
1060 (cond
Barry Warsaw32a03962002-07-16 16:04:13 +00001061 ;; Skip code in comments and strings
1062 ((py-in-literal))
Barry Warsaw81437461996-08-01 19:48:02 +00001063 ;; at the same indent level, add it to the list...
1064 ((= start-indent cur-indent)
Barry Warsaw81437461996-08-01 19:48:02 +00001065 (push (cons def-name def-pos) index-alist))
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001066 ;; deeper indented expression, recurse
Barry Warsaw81437461996-08-01 19:48:02 +00001067 ((< start-indent cur-indent)
Barry Warsaw81437461996-08-01 19:48:02 +00001068 ;; the point is currently on the expression we're supposed to
1069 ;; start on, so go back to the last expression. The recursive
1070 ;; call will find this place again and add it to the correct
1071 ;; list
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001072 (re-search-backward py-imenu-generic-regexp (point-min) 'move)
1073 (setq sub-method-alist (py-imenu-create-index-engine cur-indent))
Barry Warsaw81437461996-08-01 19:48:02 +00001074 (if sub-method-alist
1075 ;; we put the last element on the index-alist on the start
1076 ;; of the submethod alist so the user can still get to it.
1077 (let ((save-elmt (pop index-alist)))
Barry Warsawc8520351997-11-26 06:14:40 +00001078 (push (cons prev-name
Barry Warsaw81437461996-08-01 19:48:02 +00001079 (cons save-elmt sub-method-alist))
1080 index-alist))))
Barry Warsaw81437461996-08-01 19:48:02 +00001081 ;; found less indented expression, we're done.
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001082 (t
Barry Warsaw81437461996-08-01 19:48:02 +00001083 (setq looking-p nil)
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001084 (re-search-backward py-imenu-generic-regexp (point-min) t)))
1085 ;; end-cond
Barry Warsaw81437461996-08-01 19:48:02 +00001086 (setq prev-name def-name)
1087 (and looking-p
1088 (setq looking-p
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001089 (re-search-forward py-imenu-generic-regexp
Barry Warsaw81437461996-08-01 19:48:02 +00001090 (point-max) 'move))))
1091 (nreverse index-alist)))
1092
Barry Warsaw88491612002-04-25 21:31:47 +00001093
1094
1095(defun py-choose-shell-by-shebang ()
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001096 "Choose CPython or Jython mode by looking at #! on the first line.
Barry Warsaw88491612002-04-25 21:31:47 +00001097Returns the appropriate mode function.
1098Used by `py-choose-shell', and similar to but distinct from
1099`set-auto-mode', though it uses `auto-mode-interpreter-regexp' (if available)."
1100 ;; look for an interpreter specified in the first line
1101 ;; similar to set-auto-mode (files.el)
1102 (let* ((re (if (boundp 'auto-mode-interpreter-regexp)
1103 auto-mode-interpreter-regexp
1104 ;; stolen from Emacs 21.2
1105 "#![ \t]?\\([^ \t\n]*/bin/env[ \t]\\)?\\([^ \t\n]+\\)"))
1106 (interpreter (save-excursion
1107 (goto-char (point-min))
1108 (if (looking-at re)
1109 (match-string 2)
1110 "")))
1111 elt)
1112 ;; Map interpreter name to a mode.
1113 (setq elt (assoc (file-name-nondirectory interpreter)
1114 py-shell-alist))
1115 (and elt (caddr elt))))
1116
1117
1118
1119(defun py-choose-shell-by-import ()
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001120 "Choose CPython or Jython mode based imports.
1121If a file imports any packages in `py-jython-packages', within
Barry Warsaw88491612002-04-25 21:31:47 +00001122`py-import-check-point-max' characters from the start of the file,
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001123return `jython', otherwise return nil."
Barry Warsaw88491612002-04-25 21:31:47 +00001124 (let (mode)
1125 (save-excursion
1126 (goto-char (point-min))
1127 (while (and (not mode)
1128 (search-forward-regexp
1129 "^\\(\\(from\\)\\|\\(import\\)\\) \\([^ \t\n.]+\\)"
1130 py-import-check-point-max t))
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001131 (setq mode (and (member (match-string 4) py-jython-packages)
1132 'jython
Barry Warsaw88491612002-04-25 21:31:47 +00001133 ))))
1134 mode))
1135
1136
1137(defun py-choose-shell ()
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001138 "Choose CPython or Jython mode. Returns the appropriate mode function.
Barry Warsaw88491612002-04-25 21:31:47 +00001139This does the following:
1140 - look for an interpreter with `py-choose-shell-by-shebang'
1141 - examine imports using `py-choose-shell-by-import'
1142 - default to the variable `py-default-interpreter'"
1143 (interactive)
1144 (or (py-choose-shell-by-shebang)
1145 (py-choose-shell-by-import)
1146 py-default-interpreter
1147; 'cpython ;; don't use to py-default-interpreter, because default
1148; ;; is only way to choose CPython
1149 ))
1150
Barry Warsaw42f707f1996-07-29 21:05:05 +00001151
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001152;;;###autoload
Barry Warsaw7ae77681994-12-12 20:38:05 +00001153(defun python-mode ()
1154 "Major mode for editing Python files.
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001155To submit a problem report, enter `\\[py-submit-bug-report]' from a
1156`python-mode' buffer. Do `\\[py-describe-mode]' for detailed
1157documentation. To see what version of `python-mode' you are running,
1158enter `\\[py-version]'.
1159
1160This mode knows about Python indentation, tokens, comments and
1161continuation lines. Paragraphs are separated by blank lines only.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001162
1163COMMANDS
1164\\{py-mode-map}
1165VARIABLES
1166
Barry Warsaw42f707f1996-07-29 21:05:05 +00001167py-indent-offset\t\tindentation increment
Barry Warsaw41a05c71998-08-10 16:33:12 +00001168py-block-comment-prefix\t\tcomment string used by `comment-region'
Barry Warsaw42f707f1996-07-29 21:05:05 +00001169py-python-command\t\tshell command to invoke Python interpreter
Barry Warsaw42f707f1996-07-29 21:05:05 +00001170py-temp-directory\t\tdirectory used for temp files (if needed)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001171py-beep-if-tab-change\t\tring the bell if `tab-width' is changed"
Barry Warsaw7ae77681994-12-12 20:38:05 +00001172 (interactive)
Barry Warsaw615d4a41996-09-04 14:14:10 +00001173 ;; set up local variables
Barry Warsaw7ae77681994-12-12 20:38:05 +00001174 (kill-all-local-variables)
Barry Warsaw615d4a41996-09-04 14:14:10 +00001175 (make-local-variable 'font-lock-defaults)
1176 (make-local-variable 'paragraph-separate)
1177 (make-local-variable 'paragraph-start)
1178 (make-local-variable 'require-final-newline)
1179 (make-local-variable 'comment-start)
Barry Warsaw5c8bef11996-12-17 21:56:10 +00001180 (make-local-variable 'comment-end)
Barry Warsaw615d4a41996-09-04 14:14:10 +00001181 (make-local-variable 'comment-start-skip)
1182 (make-local-variable 'comment-column)
Barry Warsaw003932a1998-07-07 15:11:24 +00001183 (make-local-variable 'comment-indent-function)
Barry Warsaw615d4a41996-09-04 14:14:10 +00001184 (make-local-variable 'indent-region-function)
1185 (make-local-variable 'indent-line-function)
Barry Warsawb3e81d51996-09-04 15:12:42 +00001186 (make-local-variable 'add-log-current-defun-function)
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001187 (make-local-variable 'fill-paragraph-function)
Barry Warsaw615d4a41996-09-04 14:14:10 +00001188 ;;
Barry Warsaw7ae77681994-12-12 20:38:05 +00001189 (set-syntax-table py-mode-syntax-table)
Barry Warsaw003932a1998-07-07 15:11:24 +00001190 (setq major-mode 'python-mode
1191 mode-name "Python"
1192 local-abbrev-table python-mode-abbrev-table
1193 font-lock-defaults '(python-font-lock-keywords)
1194 paragraph-separate "^[ \t]*$"
1195 paragraph-start "^[ \t]*$"
1196 require-final-newline t
1197 comment-start "# "
1198 comment-end ""
1199 comment-start-skip "# *"
1200 comment-column 40
1201 comment-indent-function 'py-comment-indent-function
1202 indent-region-function 'py-indent-region
1203 indent-line-function 'py-indent-line
Barry Warsawb3e81d51996-09-04 15:12:42 +00001204 ;; tell add-log.el how to find the current function/method/variable
1205 add-log-current-defun-function 'py-current-defun
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001206
1207 fill-paragraph-function 'py-fill-paragraph
Barry Warsawb1f89511996-09-03 16:38:30 +00001208 )
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001209 (use-local-map py-mode-map)
Barry Warsaw42f707f1996-07-29 21:05:05 +00001210 ;; add the menu
1211 (if py-menu
1212 (easy-menu-add py-menu))
Barry Warsaw57697af1995-09-14 20:01:14 +00001213 ;; Emacs 19 requires this
Barry Warsawc72c11c1997-08-09 06:42:08 +00001214 (if (boundp 'comment-multi-line)
Barry Warsaw57697af1995-09-14 20:01:14 +00001215 (setq comment-multi-line nil))
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001216 ;; Install Imenu if available
Barry Warsaw742a5111998-03-13 17:29:15 +00001217 (when (py-safe (require 'imenu))
Barry Warsaw6839d3a1998-10-28 00:10:45 +00001218 (setq imenu-create-index-function #'py-imenu-create-index-function)
1219 (setq imenu-generic-expression py-imenu-generic-expression)
Barry Warsaw742a5111998-03-13 17:29:15 +00001220 (if (fboundp 'imenu-add-to-menubar)
1221 (imenu-add-to-menubar (format "%s-%s" "IM" mode-name)))
1222 )
1223 ;; Run the mode hook. Note that py-mode-hook is deprecated.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001224 (if python-mode-hook
1225 (run-hooks 'python-mode-hook)
Barry Warsaw742a5111998-03-13 17:29:15 +00001226 (run-hooks 'py-mode-hook))
1227 ;; Now do the automagical guessing
1228 (if py-smart-indentation
1229 (let ((offset py-indent-offset))
Barry Warsaw145ab1c1998-05-19 14:49:49 +00001230 ;; It's okay if this fails to guess a good value
Barry Warsaw742a5111998-03-13 17:29:15 +00001231 (if (and (py-safe (py-guess-indent-offset))
1232 (<= py-indent-offset 8)
1233 (>= py-indent-offset 2))
1234 (setq offset py-indent-offset))
1235 (setq py-indent-offset offset)
Barry Warsaw639eea61998-03-16 18:12:13 +00001236 ;; Only turn indent-tabs-mode off if tab-width !=
1237 ;; py-indent-offset. Never turn it on, because the user must
1238 ;; have explicitly turned it off.
1239 (if (/= tab-width py-indent-offset)
1240 (setq indent-tabs-mode nil))
Barry Warsaw11f21561999-07-28 21:59:43 +00001241 ))
1242 ;; Set the default shell if not already set
1243 (when (null py-which-shell)
Barry Warsaw88491612002-04-25 21:31:47 +00001244 (py-toggle-shells (py-choose-shell))))
1245
1246
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001247(make-obsolete 'jpython-mode 'jython-mode)
1248(defun jython-mode ()
1249 "Major mode for editing Jython/Jython files.
Barry Warsaw88491612002-04-25 21:31:47 +00001250This is a simple wrapper around `python-mode'.
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001251It runs `jython-mode-hook' then calls `python-mode.'
Barry Warsaw88491612002-04-25 21:31:47 +00001252It is added to `interpreter-mode-alist' and `py-choose-shell'.
1253"
1254 (interactive)
1255 (python-mode)
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001256 (py-toggle-shells 'jython)
1257 (when jython-mode-hook
1258 (run-hooks 'jython-mode-hook)))
Barry Warsaw88491612002-04-25 21:31:47 +00001259
1260
1261;; It's handy to add recognition of Python files to the
1262;; interpreter-mode-alist and to auto-mode-alist. With the former, we
1263;; can specify different `derived-modes' based on the #! line, but
1264;; with the latter, we can't. So we just won't add them if they're
1265;; already added.
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001266;;;###autoload
1267(let ((modes '(("jython" . jython-mode)
Barry Warsaw88491612002-04-25 21:31:47 +00001268 ("python" . python-mode))))
1269 (while modes
1270 (when (not (assoc (car modes) interpreter-mode-alist))
Barry Warsawcf22c822002-04-25 21:46:33 +00001271 (push (car modes) interpreter-mode-alist))
Barry Warsaw88491612002-04-25 21:31:47 +00001272 (setq modes (cdr modes))))
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001273;;;###autoload
Barry Warsaw88491612002-04-25 21:31:47 +00001274(when (not (or (rassq 'python-mode auto-mode-alist)
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001275 (rassq 'jython-mode auto-mode-alist)))
Barry Warsaw88491612002-04-25 21:31:47 +00001276 (push '("\\.py$" . python-mode) auto-mode-alist))
1277
Barry Warsaw7ae77681994-12-12 20:38:05 +00001278
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001279
Barry Warsawb91b7431995-03-14 15:55:20 +00001280;; electric characters
Barry Warsaw3874a3d1995-03-14 22:05:53 +00001281(defun py-outdent-p ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00001282 "Returns non-nil if the current line should dedent one level."
Barry Warsaw3874a3d1995-03-14 22:05:53 +00001283 (save-excursion
1284 (and (progn (back-to-indentation)
1285 (looking-at py-outdent-re))
Barry Warsaw1a1c6bb1999-01-09 17:22:38 +00001286 ;; short circuit infloop on illegal construct
1287 (not (bobp))
Barry Warsawf06777d1998-01-21 05:36:18 +00001288 (progn (forward-line -1)
1289 (py-goto-initial-line)
1290 (back-to-indentation)
Barry Warsaw3874a3d1995-03-14 22:05:53 +00001291 (while (or (looking-at py-blank-or-comment-re)
1292 (bobp))
1293 (backward-to-indentation 1))
1294 (not (looking-at py-no-outdent-re)))
1295 )))
Skip Montanaro98a4fbe2003-08-01 04:00:32 +00001296
Barry Warsawb91b7431995-03-14 15:55:20 +00001297(defun py-electric-colon (arg)
1298 "Insert a colon.
Barry Warsaw41a05c71998-08-10 16:33:12 +00001299In certain cases the line is dedented appropriately. If a numeric
1300argument ARG is provided, that many colons are inserted
1301non-electrically. Electric behavior is inhibited inside a string or
1302comment."
Skip Montanaro98a4fbe2003-08-01 04:00:32 +00001303 (interactive "*P")
Barry Warsawb91b7431995-03-14 15:55:20 +00001304 (self-insert-command (prefix-numeric-value arg))
Barry Warsaw0c6563f1995-09-14 20:57:02 +00001305 ;; are we in a string or comment?
1306 (if (save-excursion
1307 (let ((pps (parse-partial-sexp (save-excursion
Barry Warsawab0e86c1998-05-19 15:31:46 +00001308 (py-beginning-of-def-or-class)
Barry Warsaw0c6563f1995-09-14 20:57:02 +00001309 (point))
1310 (point))))
1311 (not (or (nth 3 pps) (nth 4 pps)))))
1312 (save-excursion
1313 (let ((here (point))
1314 (outdent 0)
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001315 (indent (py-compute-indentation t)))
Barry Warsaw0c6563f1995-09-14 20:57:02 +00001316 (if (and (not arg)
1317 (py-outdent-p)
1318 (= indent (save-excursion
Barry Warsawa7661821996-08-02 16:22:43 +00001319 (py-next-statement -1)
Barry Warsaw7cb505c1996-10-23 20:44:59 +00001320 (py-compute-indentation t)))
Barry Warsaw0c6563f1995-09-14 20:57:02 +00001321 )
1322 (setq outdent py-indent-offset))
Barry Warsaw41a05c71998-08-10 16:33:12 +00001323 ;; Don't indent, only dedent. This assumes that any lines
1324 ;; that are already dedented relative to
1325 ;; py-compute-indentation were put there on purpose. It's
1326 ;; highly annoying to have `:' indent for you. Use TAB, C-c
1327 ;; C-l or C-c C-r to adjust. TBD: Is there a better way to
1328 ;; determine this???
Barry Warsaw0c6563f1995-09-14 20:57:02 +00001329 (if (< (current-indentation) indent) nil
1330 (goto-char here)
1331 (beginning-of-line)
1332 (delete-horizontal-space)
1333 (indent-to (- indent outdent))
1334 )))))
Barry Warsawb91b7431995-03-14 15:55:20 +00001335
1336
Barry Warsawa97a3f31997-11-04 18:47:06 +00001337;; Python subprocess utilities and filters
1338(defun py-execute-file (proc filename)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001339 "Send to Python interpreter process PROC \"execfile('FILENAME')\".
1340Make that process's buffer visible and force display. Also make
1341comint believe the user typed this string so that
1342`kill-output-from-shell' does The Right Thing."
Barry Warsawa97a3f31997-11-04 18:47:06 +00001343 (let ((curbuf (current-buffer))
1344 (procbuf (process-buffer proc))
Barry Warsaw3c96f6f1998-08-20 19:44:51 +00001345; (comint-scroll-to-bottom-on-output t)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001346 (msg (format "## working on region in file %s...\n" filename))
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001347 ;; add some comment, so that we can filter it out of history
1348 (cmd (format "execfile(r'%s') # PYTHON-MODE\n" filename)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001349 (unwind-protect
Barry Warsaw3c96f6f1998-08-20 19:44:51 +00001350 (save-excursion
Barry Warsawa97a3f31997-11-04 18:47:06 +00001351 (set-buffer procbuf)
1352 (goto-char (point-max))
1353 (move-marker (process-mark proc) (point))
1354 (funcall (process-filter proc) proc msg))
1355 (set-buffer curbuf))
1356 (process-send-string proc cmd)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001357
Barry Warsaw3c96f6f1998-08-20 19:44:51 +00001358(defun py-comint-output-filter-function (string)
1359 "Watch output for Python prompt and exec next file waiting in queue.
1360This function is appropriate for `comint-output-filter-functions'."
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001361 ;;remove ansi terminal escape sequences from string, not sure why they are
1362 ;;still around...
1363 (setq string (ansi-color-filter-apply string))
1364 (when (and (string-match py-shell-input-prompt-1-regexp string)
1365 py-file-queue)
1366 (if py-shell-switch-buffers-on-execute
1367 (pop-to-buffer (current-buffer)))
Barry Warsaw3c96f6f1998-08-20 19:44:51 +00001368 (py-safe (delete-file (car py-file-queue)))
1369 (setq py-file-queue (cdr py-file-queue))
1370 (if py-file-queue
1371 (let ((pyproc (get-buffer-process (current-buffer))))
1372 (py-execute-file pyproc (car py-file-queue))))
1373 ))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001374
Barry Warsaw4f577d22001-04-11 20:23:17 +00001375(defun py-pdbtrack-overlay-arrow (activation)
1376 "Activate or de arrow at beginning-of-line in current buffer."
1377 ;; This was derived/simplified from edebug-overlay-arrow
1378 (cond (activation
1379 (setq overlay-arrow-position (make-marker))
1380 (setq overlay-arrow-string "=>")
1381 (set-marker overlay-arrow-position (py-point 'bol) (current-buffer))
1382 (setq py-pdbtrack-is-tracking-p t))
1383 (overlay-arrow-position
1384 (setq overlay-arrow-position nil)
1385 (setq py-pdbtrack-is-tracking-p nil))
1386 ))
1387
1388(defun py-pdbtrack-track-stack-file (text)
1389 "Show the file indicated by the pdb stack entry line, in a separate window.
1390
1391Activity is disabled if the buffer-local variable
1392`py-pdbtrack-do-tracking-p' is nil.
1393
1394We depend on the pdb input prompt matching `py-pdbtrack-input-prompt'
Ken Manheimere7aca522003-03-03 00:35:32 +00001395at the beginning of the line.
1396
1397If the traceback target file path is invalid, we look for the most
1398recently visited python-mode buffer which either has the name of the
1399current function \(or class) or which defines the function \(or
1400class). This is to provide for remote scripts, eg, Zope's 'Script
Ken Manheimer595adce2003-05-01 20:46:14 +00001401(Python)' - put a _copy_ of the script in a buffer named for the
1402script, and set to python-mode, and pdbtrack will find it.)"
Barry Warsaw4f577d22001-04-11 20:23:17 +00001403 ;; Instead of trying to piece things together from partial text
1404 ;; (which can be almost useless depending on Emacs version), we
1405 ;; monitor to the point where we have the next pdb prompt, and then
1406 ;; check all text from comint-last-input-end to process-mark.
1407 ;;
Ken Manheimere7aca522003-03-03 00:35:32 +00001408 ;; Also, we're very conservative about clearing the overlay arrow,
1409 ;; to minimize residue. This means, for instance, that executing
1410 ;; other pdb commands wipe out the highlight. You can always do a
1411 ;; 'where' (aka 'w') command to reveal the overlay arrow.
Barry Warsaw4f577d22001-04-11 20:23:17 +00001412 (let* ((origbuf (current-buffer))
1413 (currproc (get-buffer-process origbuf)))
Ken Manheimere7aca522003-03-03 00:35:32 +00001414
Barry Warsaw4f577d22001-04-11 20:23:17 +00001415 (if (not (and currproc py-pdbtrack-do-tracking-p))
1416 (py-pdbtrack-overlay-arrow nil)
Ken Manheimere7aca522003-03-03 00:35:32 +00001417
1418 (let* ((procmark (process-mark currproc))
Barry Warsaw4f577d22001-04-11 20:23:17 +00001419 (block (buffer-substring (max comint-last-input-end
1420 (- procmark
1421 py-pdbtrack-track-range))
1422 procmark))
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001423 target target_fname target_lineno target_buffer)
Ken Manheimere7aca522003-03-03 00:35:32 +00001424
Barry Warsaw4f577d22001-04-11 20:23:17 +00001425 (if (not (string-match (concat py-pdbtrack-input-prompt "$") block))
1426 (py-pdbtrack-overlay-arrow nil)
Ken Manheimere7aca522003-03-03 00:35:32 +00001427
1428 (setq target (py-pdbtrack-get-source-buffer block))
1429
1430 (if (stringp target)
1431 (message "pdbtrack: %s" target)
1432
1433 (setq target_lineno (car target))
1434 (setq target_buffer (cadr target))
1435 (setq target_fname (buffer-file-name target_buffer))
1436 (switch-to-buffer-other-window target_buffer)
1437 (goto-line target_lineno)
1438 (message "pdbtrack: line %s, file %s" target_lineno target_fname)
1439 (py-pdbtrack-overlay-arrow t)
1440 (pop-to-buffer origbuf t)
1441
1442 )))))
1443 )
1444
1445(defun py-pdbtrack-get-source-buffer (block)
1446 "Return line number and buffer of code indicated by block's traceback text.
1447
1448We look first to visit the file indicated in the trace.
1449
1450Failing that, we look for the most recently visited python-mode buffer
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001451with the same name or having the named function.
Ken Manheimere7aca522003-03-03 00:35:32 +00001452
1453If we're unable find the source code we return a string describing the
1454problem as best as we can determine."
1455
1456 (if (not (string-match py-pdbtrack-stack-entry-regexp block))
1457
1458 "Traceback cue not found"
1459
1460 (let* ((filename (match-string 1 block))
1461 (lineno (string-to-int (match-string 2 block)))
1462 (funcname (match-string 3 block))
1463 funcbuffer)
1464
1465 (cond ((file-exists-p filename)
1466 (list lineno (find-file-noselect filename)))
1467
1468 ((setq funcbuffer (py-pdbtrack-grub-for-buffer funcname lineno))
1469 (if (string-match "/Script (Python)$" filename)
1470 ;; Add in number of lines for leading '##' comments:
1471 (setq lineno
1472 (+ lineno
1473 (save-excursion
1474 (set-buffer funcbuffer)
1475 (count-lines
1476 (point-min)
Ken Manheimerad428cd2003-03-03 04:05:03 +00001477 (max (point-min)
1478 (string-match "^\\([^#]\\|#[^#]\\|#$\\)"
1479 (buffer-substring (point-min)
Ken Manheimer8e9b80f2003-06-17 19:18:57 +00001480 (point-max)))
Ken Manheimerad428cd2003-03-03 04:05:03 +00001481 ))))))
Ken Manheimere7aca522003-03-03 00:35:32 +00001482 (list lineno funcbuffer))
1483
1484 ((= (elt filename 0) ?\<)
1485 (format "(Non-file source: '%s')" filename))
1486
Ken Manheimer595adce2003-05-01 20:46:14 +00001487 (t (format "Not found: %s(), %s" funcname filename)))
Ken Manheimere7aca522003-03-03 00:35:32 +00001488 )
1489 )
1490 )
1491
1492(defun py-pdbtrack-grub-for-buffer (funcname lineno)
1493 "Find most recent buffer itself named or having function funcname.
1494
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001495We walk the buffer-list history for python-mode buffers that are
Ken Manheimered6000a2003-03-03 17:09:44 +00001496named for funcname or define a function funcname."
Ken Manheimere7aca522003-03-03 00:35:32 +00001497 (let ((buffers (buffer-list))
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001498 buf
Ken Manheimere7aca522003-03-03 00:35:32 +00001499 got)
1500 (while (and buffers (not got))
1501 (setq buf (car buffers)
1502 buffers (cdr buffers))
Ken Manheimered6000a2003-03-03 17:09:44 +00001503 (if (and (save-excursion (set-buffer buf)
1504 (string= major-mode "python-mode"))
1505 (or (string-match funcname (buffer-name buf))
Ken Manheimere7aca522003-03-03 00:35:32 +00001506 (string-match (concat "^\\s-*\\(def\\|class\\)\\s-+"
1507 funcname "\\s-*(")
Ken Manheimer8e9b80f2003-06-17 19:18:57 +00001508 (save-excursion
1509 (set-buffer buf)
1510 (buffer-substring (point-min)
1511 (point-max))))))
Ken Manheimere7aca522003-03-03 00:35:32 +00001512 (setq got buf)))
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001513 got))
Barry Warsaw4f577d22001-04-11 20:23:17 +00001514
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001515(defun py-postprocess-output-buffer (buf)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001516 "Highlight exceptions found in BUF.
1517If an exception occurred return t, otherwise return nil. BUF must exist."
Barry Warsawf9b99f41998-03-26 16:08:59 +00001518 (let (line file bol err-p)
Barry Warsaw9981d221997-12-03 05:25:48 +00001519 (save-excursion
1520 (set-buffer buf)
1521 (beginning-of-buffer)
1522 (while (re-search-forward py-traceback-line-re nil t)
1523 (setq file (match-string 1)
1524 line (string-to-int (match-string 2))
1525 bol (py-point 'bol))
Barry Warsawf9b99f41998-03-26 16:08:59 +00001526 (py-highlight-line bol (py-point 'eol) file line)))
1527 (when (and py-jump-on-exception line)
1528 (beep)
1529 (py-jump-to-exception file line)
1530 (setq err-p t))
1531 err-p))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001532
Barry Warsaw9981d221997-12-03 05:25:48 +00001533
Barry Warsawa97a3f31997-11-04 18:47:06 +00001534
1535;;; Subprocess commands
1536
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001537;; only used when (memq 'broken-temp-names py-emacs-features)
1538(defvar py-serial-number 0)
1539(defvar py-exception-buffer nil)
1540(defconst py-output-buffer "*Python Output*")
Barry Warsawa2398801998-04-09 23:28:20 +00001541(make-variable-buffer-local 'py-output-buffer)
1542
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001543;; for toggling between CPython and Jython
Barry Warsawaa384fd1999-02-16 23:36:16 +00001544(defvar py-which-shell nil)
Barry Warsawa2398801998-04-09 23:28:20 +00001545(defvar py-which-args py-python-command-args)
1546(defvar py-which-bufname "Python")
1547(make-variable-buffer-local 'py-which-shell)
1548(make-variable-buffer-local 'py-which-args)
1549(make-variable-buffer-local 'py-which-bufname)
1550
1551(defun py-toggle-shells (arg)
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001552 "Toggles between the CPython and Jython shells.
Barry Warsaw11f21561999-07-28 21:59:43 +00001553
Barry Warsaw41a05c71998-08-10 16:33:12 +00001554With positive argument ARG (interactively \\[universal-argument]),
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001555uses the CPython shell, with negative ARG uses the Jython shell, and
Barry Warsaw11f21561999-07-28 21:59:43 +00001556with a zero argument, toggles the shell.
1557
1558Programmatically, ARG can also be one of the symbols `cpython' or
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001559`jython', equivalent to positive arg and negative arg respectively."
Barry Warsawa2398801998-04-09 23:28:20 +00001560 (interactive "P")
1561 ;; default is to toggle
1562 (if (null arg)
1563 (setq arg 0))
Barry Warsaw11f21561999-07-28 21:59:43 +00001564 ;; preprocess arg
1565 (cond
1566 ((equal arg 0)
1567 ;; toggle
1568 (if (string-equal py-which-bufname "Python")
1569 (setq arg -1)
1570 (setq arg 1)))
1571 ((equal arg 'cpython) (setq arg 1))
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001572 ((equal arg 'jython) (setq arg -1)))
Barry Warsawa2398801998-04-09 23:28:20 +00001573 (let (msg)
1574 (cond
1575 ((< 0 arg)
1576 ;; set to CPython
1577 (setq py-which-shell py-python-command
1578 py-which-args py-python-command-args
1579 py-which-bufname "Python"
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001580 msg "CPython")
1581 (if (string-equal py-which-bufname "Jython")
1582 (setq mode-name "Python")))
Barry Warsawa2398801998-04-09 23:28:20 +00001583 ((> 0 arg)
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001584 (setq py-which-shell py-jython-command
1585 py-which-args py-jython-command-args
1586 py-which-bufname "Jython"
1587 msg "Jython")
1588 (if (string-equal py-which-bufname "Python")
1589 (setq mode-name "Jython")))
Barry Warsawa2398801998-04-09 23:28:20 +00001590 )
Barry Warsawea609c11998-04-10 16:08:26 +00001591 (message "Using the %s shell" msg)
Barry Warsawa2398801998-04-09 23:28:20 +00001592 (setq py-output-buffer (format "*%s Output*" py-which-bufname))))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001593
Barry Warsawa97a3f31997-11-04 18:47:06 +00001594;;;###autoload
Barry Warsaw3b4e2f01999-02-16 23:52:46 +00001595(defun py-shell (&optional argprompt)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001596 "Start an interactive Python interpreter in another window.
1597This is like Shell mode, except that Python is running in the window
1598instead of a shell. See the `Interactive Shell' and `Shell Mode'
1599sections of the Emacs manual for details, especially for the key
1600bindings active in the `*Python*' buffer.
1601
Barry Warsaw3b4e2f01999-02-16 23:52:46 +00001602With optional \\[universal-argument], the user is prompted for the
1603flags to pass to the Python interpreter. This has no effect when this
1604command is used to switch to an existing process, only when a new
1605process is started. If you use this, you will probably want to ensure
1606that the current arguments are retained (they will be included in the
1607prompt). This argument is ignored when this function is called
1608programmatically, or when running in Emacs 19.34 or older.
1609
Barry Warsawa2398801998-04-09 23:28:20 +00001610Note: You can toggle between using the CPython interpreter and the
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001611Jython interpreter by hitting \\[py-toggle-shells]. This toggles
Barry Warsawa2398801998-04-09 23:28:20 +00001612buffer local variables which control whether all your subshell
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001613interactions happen to the `*Jython*' or `*Python*' buffers (the
Barry Warsawa2398801998-04-09 23:28:20 +00001614latter is the name used for the CPython buffer).
1615
Barry Warsawa97a3f31997-11-04 18:47:06 +00001616Warning: Don't use an interactive Python if you change sys.ps1 or
1617sys.ps2 from their default values, or if you're running code that
1618prints `>>> ' or `... ' at the start of a line. `python-mode' can't
1619distinguish your output from Python's output, and assumes that `>>> '
1620at the start of a line is a prompt from Python. Similarly, the Emacs
1621Shell mode code assumes that both `>>> ' and `... ' at the start of a
1622line are Python prompts. Bad things can happen if you fool either
1623mode.
1624
1625Warning: If you do any editing *in* the process buffer *while* the
1626buffer is accepting output from Python, do NOT attempt to `undo' the
1627changes. Some of the output (nowhere near the parts you changed!) may
1628be lost if you do. This appears to be an Emacs bug, an unfortunate
1629interaction between undo and process filters; the same problem exists in
1630non-Python process buffers using the default (Emacs-supplied) process
1631filter."
Barry Warsaw3b4e2f01999-02-16 23:52:46 +00001632 (interactive "P")
Barry Warsaw50765ab1999-08-10 21:49:00 +00001633 ;; Set the default shell if not already set
1634 (when (null py-which-shell)
1635 (py-toggle-shells py-default-interpreter))
Barry Warsaw3b4e2f01999-02-16 23:52:46 +00001636 (let ((args py-which-args))
1637 (when (and argprompt
1638 (interactive-p)
1639 (fboundp 'split-string))
1640 ;; TBD: Perhaps force "-i" in the final list?
1641 (setq args (split-string
1642 (read-string (concat py-which-bufname
1643 " arguments: ")
1644 (concat
1645 (mapconcat 'identity py-which-args " ") " ")
1646 ))))
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001647 (if (not (equal (buffer-name) "*Python*"))
1648 (switch-to-buffer-other-window
1649 (apply 'make-comint py-which-bufname py-which-shell nil args))
1650 (apply 'make-comint py-which-bufname py-which-shell nil args))
Barry Warsaw3b4e2f01999-02-16 23:52:46 +00001651 (make-local-variable 'comint-prompt-regexp)
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001652 (setq comint-prompt-regexp (concat py-shell-input-prompt-1-regexp "\\|"
1653 py-shell-input-prompt-2-regexp "\\|"
1654 "^([Pp]db) "))
Barry Warsaw3b4e2f01999-02-16 23:52:46 +00001655 (add-hook 'comint-output-filter-functions
1656 'py-comint-output-filter-function)
Barry Warsaw4f577d22001-04-11 20:23:17 +00001657 ;; pdbtrack
1658 (add-hook 'comint-output-filter-functions 'py-pdbtrack-track-stack-file)
1659 (setq py-pdbtrack-do-tracking-p t)
Barry Warsaw3b4e2f01999-02-16 23:52:46 +00001660 (set-syntax-table py-mode-syntax-table)
1661 (use-local-map py-shell-map)
Barry Warsaw56bd2ed2002-04-25 15:44:17 +00001662 (run-hooks 'py-shell-hook)
Barry Warsaw3b4e2f01999-02-16 23:52:46 +00001663 ))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001664
Barry Warsawa97a3f31997-11-04 18:47:06 +00001665(defun py-clear-queue ()
1666 "Clear the queue of temporary files waiting to execute."
1667 (interactive)
1668 (let ((n (length py-file-queue)))
1669 (mapcar 'delete-file py-file-queue)
1670 (setq py-file-queue nil)
1671 (message "%d pending files de-queued." n)))
1672
Barry Warsaw820273d1998-05-19 15:54:45 +00001673
Barry Warsawa97a3f31997-11-04 18:47:06 +00001674(defun py-execute-region (start end &optional async)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001675 "Execute the region in a Python interpreter.
1676
Barry Warsawa97a3f31997-11-04 18:47:06 +00001677The region is first copied into a temporary file (in the directory
1678`py-temp-directory'). If there is no Python interpreter shell
1679running, this file is executed synchronously using
Barry Warsaw41a05c71998-08-10 16:33:12 +00001680`shell-command-on-region'. If the program is long running, use
1681\\[universal-argument] to run the command asynchronously in its own
1682buffer.
1683
1684When this function is used programmatically, arguments START and END
1685specify the region to execute, and optional third argument ASYNC, if
1686non-nil, specifies to run the command asynchronously in its own
1687buffer.
Barry Warsawa97a3f31997-11-04 18:47:06 +00001688
1689If the Python interpreter shell is running, the region is execfile()'d
1690in that shell. If you try to execute regions too quickly,
1691`python-mode' will queue them up and execute them one at a time when
1692it sees a `>>> ' prompt from Python. Each time this happens, the
1693process buffer is popped into a window (if it's not already in some
1694window) so you can see it, and a comment of the form
1695
1696 \t## working on region in file <name>...
1697
1698is inserted at the end. See also the command `py-clear-queue'."
1699 (interactive "r\nP")
Barry Warsaw4aab68e2002-04-25 19:17:42 +00001700 ;; Skip ahead to the first non-blank line
Barry Warsaw014e0e21998-11-17 19:24:47 +00001701 (let* ((proc (get-process py-which-bufname))
Barry Warsaw5e21cb01997-11-05 18:41:11 +00001702 (temp (if (memq 'broken-temp-names py-emacs-features)
Barry Warsaw1b344241998-08-07 22:24:16 +00001703 (let
1704 ((sn py-serial-number)
1705 (pid (and (fboundp 'emacs-pid) (emacs-pid))))
1706 (setq py-serial-number (1+ py-serial-number))
1707 (if pid
1708 (format "python-%d-%d" sn pid)
1709 (format "python-%d" sn)))
Barry Warsaw2f32fbb1998-02-25 16:45:43 +00001710 (make-temp-name "python-")))
Barry Warsawb2d5e622002-04-22 15:29:27 +00001711 (file (concat (expand-file-name temp py-temp-directory) ".py"))
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001712 (cur (current-buffer))
Barry Warsawb2d5e622002-04-22 15:29:27 +00001713 (buf (get-buffer-create file))
1714 shell)
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001715 ;; Write the contents of the buffer, watching out for indented regions.
1716 (save-excursion
1717 (goto-char start)
Barry Warsaw69c92662002-05-12 17:37:46 +00001718 (beginning-of-line)
1719 (while (and (looking-at "\\s *$")
1720 (< (point) end))
1721 (forward-line 1))
1722 (setq start (point))
1723 (or (< start end)
1724 (error "Region is empty"))
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001725 (setq py-line-number-offset (count-lines 1 start))
Barry Warsaw99eadf42000-06-23 20:24:25 +00001726 (let ((needs-if (/= (py-point 'bol) (py-point 'boi))))
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001727 (set-buffer buf)
Barry Warsawb2d5e622002-04-22 15:29:27 +00001728 (python-mode)
Barry Warsaw99eadf42000-06-23 20:24:25 +00001729 (when needs-if
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001730 (insert "if 1:\n")
1731 (setq py-line-number-offset (- py-line-number-offset 1)))
Barry Warsawa0113cd2002-04-22 16:23:29 +00001732 (insert-buffer-substring cur start end)
1733 ;; Set the shell either to the #! line command, or to the
1734 ;; py-which-shell buffer local variable.
Barry Warsaw88491612002-04-25 21:31:47 +00001735 (setq shell (or (py-choose-shell-by-shebang)
1736 (py-choose-shell-by-import)
1737 py-which-shell))))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001738 (cond
Barry Warsaw145ab1c1998-05-19 14:49:49 +00001739 ;; always run the code in its own asynchronous subprocess
Barry Warsawa97a3f31997-11-04 18:47:06 +00001740 (async
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001741 ;; User explicitly wants this to run in its own async subprocess
1742 (save-excursion
1743 (set-buffer buf)
1744 (write-region (point-min) (point-max) file nil 'nomsg))
Barry Warsaw34d83171998-11-20 03:04:07 +00001745 (let* ((buf (generate-new-buffer-name py-output-buffer))
1746 ;; TBD: a horrible hack, but why create new Custom variables?
1747 (arg (if (string-equal py-which-bufname "Python")
1748 "-u" "")))
Barry Warsawb2d5e622002-04-22 15:29:27 +00001749 (start-process py-which-bufname buf shell arg file)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001750 (pop-to-buffer buf)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001751 (py-postprocess-output-buffer buf)
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001752 ;; TBD: clean up the temporary file!
Barry Warsawa97a3f31997-11-04 18:47:06 +00001753 ))
1754 ;; if the Python interpreter shell is running, queue it up for
1755 ;; execution there.
1756 (proc
1757 ;; use the existing python shell
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001758 (save-excursion
1759 (set-buffer buf)
1760 (write-region (point-min) (point-max) file nil 'nomsg))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001761 (if (not py-file-queue)
1762 (py-execute-file proc file)
Barry Warsawa97a3f31997-11-04 18:47:06 +00001763 (message "File %s queued for execution" file))
Barry Warsaw3c96f6f1998-08-20 19:44:51 +00001764 (setq py-file-queue (append py-file-queue (list file)))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001765 (setq py-exception-buffer (cons file (current-buffer))))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001766 (t
Barry Warsawa0113cd2002-04-22 16:23:29 +00001767 ;; TBD: a horrible hack, but why create new Custom variables?
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001768 (let ((cmd (concat py-which-shell (if (string-equal py-which-bufname
1769 "Jython")
1770 " -" ""))))
Barry Warsaw34d83171998-11-20 03:04:07 +00001771 ;; otherwise either run it synchronously in a subprocess
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001772 (save-excursion
1773 (set-buffer buf)
1774 (shell-command-on-region (point-min) (point-max)
1775 cmd py-output-buffer))
Barry Warsaw34d83171998-11-20 03:04:07 +00001776 ;; shell-command-on-region kills the output buffer if it never
1777 ;; existed and there's no output from the command
1778 (if (not (get-buffer py-output-buffer))
1779 (message "No output.")
1780 (setq py-exception-buffer (current-buffer))
1781 (let ((err-p (py-postprocess-output-buffer py-output-buffer)))
1782 (pop-to-buffer py-output-buffer)
1783 (if err-p
1784 (pop-to-buffer py-exception-buffer)))
Barry Warsawcaee2fe2000-05-23 05:47:43 +00001785 ))
Barry Warsaw29a90f02002-04-22 21:48:20 +00001786 ))
Barry Warsaw71534602001-02-20 23:07:56 +00001787 ;; Clean up after ourselves.
1788 (kill-buffer buf)))
Barry Warsawa97a3f31997-11-04 18:47:06 +00001789
Barry Warsaw820273d1998-05-19 15:54:45 +00001790
1791;; Code execution commands
Barry Warsawa97a3f31997-11-04 18:47:06 +00001792(defun py-execute-buffer (&optional async)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001793 "Send the contents of the buffer to a Python interpreter.
Barry Warsawc0ecb531998-01-20 21:43:34 +00001794If the file local variable `py-master-file' is non-nil, execute the
1795named file instead of the buffer's file.
1796
Barry Warsaw7ae77681994-12-12 20:38:05 +00001797If there is a *Python* process buffer it is used. If a clipping
1798restriction is in effect, only the accessible portion of the buffer is
1799sent. A trailing newline will be supplied if needed.
1800
Barry Warsaw41a05c71998-08-10 16:33:12 +00001801See the `\\[py-execute-region]' docs for an account of some
1802subtleties, including the use of the optional ASYNC argument."
Barry Warsawa97a3f31997-11-04 18:47:06 +00001803 (interactive "P")
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001804 (let ((old-buffer (current-buffer)))
1805 (if py-master-file
1806 (let* ((filename (expand-file-name py-master-file))
1807 (buffer (or (get-file-buffer filename)
1808 (find-file-noselect filename))))
1809 (set-buffer buffer)))
1810 (py-execute-region (point-min) (point-max) async)
1811 (pop-to-buffer old-buffer)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001812
Barry Warsaw820273d1998-05-19 15:54:45 +00001813(defun py-execute-import-or-reload (&optional async)
1814 "Import the current buffer's file in a Python interpreter.
1815
1816If the file has already been imported, then do reload instead to get
Barry Warsaw7c29b231998-07-07 17:45:38 +00001817the latest version.
1818
1819If the file's name does not end in \".py\", then do execfile instead.
1820
1821If the current buffer is not visiting a file, do `py-execute-buffer'
1822instead.
1823
1824If the file local variable `py-master-file' is non-nil, import or
1825reload the named file instead of the buffer's file. The file may be
1826saved based on the value of `py-execute-import-or-reload-save-p'.
Barry Warsaw820273d1998-05-19 15:54:45 +00001827
Barry Warsaw41a05c71998-08-10 16:33:12 +00001828See the `\\[py-execute-region]' docs for an account of some
1829subtleties, including the use of the optional ASYNC argument.
Barry Warsaw820273d1998-05-19 15:54:45 +00001830
Barry Warsaw7c29b231998-07-07 17:45:38 +00001831This may be preferable to `\\[py-execute-buffer]' because:
Barry Warsaw820273d1998-05-19 15:54:45 +00001832
1833 - Definitions stay in their module rather than appearing at top
1834 level, where they would clutter the global namespace and not affect
1835 uses of qualified names (MODULE.NAME).
1836
1837 - The Python debugger gets line number information about the functions."
1838 (interactive "P")
1839 ;; Check file local variable py-master-file
1840 (if py-master-file
1841 (let* ((filename (expand-file-name py-master-file))
1842 (buffer (or (get-file-buffer filename)
1843 (find-file-noselect filename))))
1844 (set-buffer buffer)))
1845 (let ((file (buffer-file-name (current-buffer))))
1846 (if file
1847 (progn
Barry Warsaw3bfed5b1998-05-19 16:25:04 +00001848 ;; Maybe save some buffers
1849 (save-some-buffers (not py-ask-about-save) nil)
Barry Warsaw820273d1998-05-19 15:54:45 +00001850 (py-execute-string
1851 (if (string-match "\\.py$" file)
1852 (let ((f (file-name-sans-extension
1853 (file-name-nondirectory file))))
1854 (format "if globals().has_key('%s'):\n reload(%s)\nelse:\n import %s\n"
1855 f f f))
Barry Warsaw02e5f691998-09-24 23:48:40 +00001856 (format "execfile(r'%s')\n" file))
Barry Warsaw820273d1998-05-19 15:54:45 +00001857 async))
1858 ;; else
1859 (py-execute-buffer async))))
1860
1861
1862(defun py-execute-def-or-class (&optional async)
1863 "Send the current function or class definition to a Python interpreter.
1864
1865If there is a *Python* process buffer it is used.
1866
Barry Warsaw41a05c71998-08-10 16:33:12 +00001867See the `\\[py-execute-region]' docs for an account of some
1868subtleties, including the use of the optional ASYNC argument."
Barry Warsaw820273d1998-05-19 15:54:45 +00001869 (interactive "P")
1870 (save-excursion
1871 (py-mark-def-or-class)
1872 ;; mark is before point
1873 (py-execute-region (mark) (point) async)))
1874
1875
1876(defun py-execute-string (string &optional async)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001877 "Send the argument STRING to a Python interpreter.
Barry Warsaw820273d1998-05-19 15:54:45 +00001878
1879If there is a *Python* process buffer it is used.
1880
Barry Warsaw41a05c71998-08-10 16:33:12 +00001881See the `\\[py-execute-region]' docs for an account of some
1882subtleties, including the use of the optional ASYNC argument."
Barry Warsaw820273d1998-05-19 15:54:45 +00001883 (interactive "sExecute Python command: ")
1884 (save-excursion
1885 (set-buffer (get-buffer-create
1886 (generate-new-buffer-name " *Python Command*")))
1887 (insert string)
1888 (py-execute-region (point-min) (point-max) async)))
1889
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001890
1891
1892(defun py-jump-to-exception (file line)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001893 "Jump to the Python code in FILE at LINE."
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001894 (let ((buffer (cond ((string-equal file "<stdin>")
Barry Warsawebc7b7a1998-05-19 15:01:06 +00001895 (if (consp py-exception-buffer)
1896 (cdr py-exception-buffer)
1897 py-exception-buffer))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001898 ((and (consp py-exception-buffer)
1899 (string-equal file (car py-exception-buffer)))
1900 (cdr py-exception-buffer))
1901 ((py-safe (find-file-noselect file)))
1902 ;; could not figure out what file the exception
1903 ;; is pointing to, so prompt for it
1904 (t (find-file (read-file-name "Exception file: "
1905 nil
1906 file t))))))
Skip Montanarodc6d9e12008-02-05 02:32:16 +00001907 ;; Fiddle about with line number
1908 (setq line (+ py-line-number-offset line))
1909
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001910 (pop-to-buffer buffer)
Barry Warsawebc7b7a1998-05-19 15:01:06 +00001911 ;; Force Python mode
Barry Warsaw820273d1998-05-19 15:54:45 +00001912 (if (not (eq major-mode 'python-mode))
Barry Warsawebc7b7a1998-05-19 15:01:06 +00001913 (python-mode))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001914 (goto-line line)
1915 (message "Jumping to exception in file %s on line %d" file line)))
1916
1917(defun py-mouseto-exception (event)
Barry Warsaw12c92941998-08-10 21:44:37 +00001918 "Jump to the code which caused the Python exception at EVENT.
Barry Warsaw41a05c71998-08-10 16:33:12 +00001919EVENT is usually a mouse click."
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001920 (interactive "e")
1921 (cond
1922 ((fboundp 'event-point)
1923 ;; XEmacs
1924 (let* ((point (event-point event))
1925 (buffer (event-buffer event))
1926 (e (and point buffer (extent-at point buffer 'py-exc-info)))
1927 (info (and e (extent-property e 'py-exc-info))))
1928 (message "Event point: %d, info: %s" point info)
1929 (and info
1930 (py-jump-to-exception (car info) (cdr info)))
1931 ))
1932 ;; Emacs -- Please port this!
1933 ))
1934
1935(defun py-goto-exception ()
1936 "Go to the line indicated by the traceback."
1937 (interactive)
1938 (let (file line)
1939 (save-excursion
1940 (beginning-of-line)
1941 (if (looking-at py-traceback-line-re)
1942 (setq file (match-string 1)
1943 line (string-to-int (match-string 2)))))
1944 (if (not file)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001945 (error "Not on a traceback line"))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001946 (py-jump-to-exception file line)))
1947
1948(defun py-find-next-exception (start buffer searchdir errwhere)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001949 "Find the next Python exception and jump to the code that caused it.
1950START is the buffer position in BUFFER from which to begin searching
1951for an exception. SEARCHDIR is a function, either
1952`re-search-backward' or `re-search-forward' indicating the direction
1953to search. ERRWHERE is used in an error message if the limit (top or
1954bottom) of the trackback stack is encountered."
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001955 (let (file line)
1956 (save-excursion
1957 (set-buffer buffer)
1958 (goto-char (py-point start))
1959 (if (funcall searchdir py-traceback-line-re nil t)
1960 (setq file (match-string 1)
1961 line (string-to-int (match-string 2)))))
1962 (if (and file line)
1963 (py-jump-to-exception file line)
1964 (error "%s of traceback" errwhere))))
1965
1966(defun py-down-exception (&optional bottom)
1967 "Go to the next line down in the traceback.
Barry Warsaw41a05c71998-08-10 16:33:12 +00001968With \\[univeral-argument] (programmatically, optional argument
1969BOTTOM), jump to the bottom (innermost) exception in the exception
1970stack."
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001971 (interactive "P")
1972 (let* ((proc (get-process "Python"))
1973 (buffer (if proc "*Python*" py-output-buffer)))
1974 (if bottom
1975 (py-find-next-exception 'eob buffer 're-search-backward "Bottom")
1976 (py-find-next-exception 'eol buffer 're-search-forward "Bottom"))))
1977
1978(defun py-up-exception (&optional top)
1979 "Go to the previous line up in the traceback.
Barry Warsaw41a05c71998-08-10 16:33:12 +00001980With \\[universal-argument] (programmatically, optional argument TOP)
1981jump to the top (outermost) exception in the exception stack."
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001982 (interactive "P")
1983 (let* ((proc (get-process "Python"))
1984 (buffer (if proc "*Python*" py-output-buffer)))
1985 (if top
1986 (py-find-next-exception 'bob buffer 're-search-forward "Top")
Barry Warsawffbc17d1997-11-27 20:08:14 +00001987 (py-find-next-exception 'bol buffer 're-search-backward "Top"))))
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00001988
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001989
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001990;; Electric deletion
1991(defun py-electric-backspace (arg)
Barry Warsaw41a05c71998-08-10 16:33:12 +00001992 "Delete preceding character or levels of indentation.
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001993Deletion is performed by calling the function in `py-backspace-function'
Barry Warsawb0539931996-12-17 22:05:07 +00001994with a single argument (the number of characters to delete).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001995
Barry Warsaw41a05c71998-08-10 16:33:12 +00001996If point is at the leftmost column, delete the preceding newline.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001997
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00001998Otherwise, if point is at the leftmost non-whitespace character of a
1999line that is neither a continuation line nor a non-indenting comment
2000line, or if point is at the end of a blank line, this command reduces
2001the indentation to match that of the line that opened the current
2002block of code. The line that opened the block is displayed in the
Barry Warsaw41a05c71998-08-10 16:33:12 +00002003echo area to help you keep track of where you are. With
2004\\[universal-argument] dedents that many blocks (but not past column
2005zero).
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00002006
2007Otherwise the preceding character is deleted, converting a tab to
2008spaces if needed so that only a single column position is deleted.
Barry Warsawfa2def21999-05-24 21:43:37 +00002009\\[universal-argument] specifies how many characters to delete;
2010default is 1.
Barry Warsaw41a05c71998-08-10 16:33:12 +00002011
2012When used programmatically, argument ARG specifies the number of
2013blocks to dedent, or the number of characters to delete, as indicated
2014above."
Barry Warsaw03970731996-07-03 23:12:52 +00002015 (interactive "*p")
Barry Warsaw7ae77681994-12-12 20:38:05 +00002016 (if (or (/= (current-indentation) (current-column))
2017 (bolp)
2018 (py-continuation-line-p)
Barry Warsawfa2def21999-05-24 21:43:37 +00002019; (not py-honor-comment-indentation)
2020; (looking-at "#[^ \t\n]") ; non-indenting #
2021 )
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00002022 (funcall py-backspace-function arg)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002023 ;; else indent the same as the colon line that opened the block
Barry Warsaw7ae77681994-12-12 20:38:05 +00002024 ;; force non-blank so py-goto-block-up doesn't ignore it
2025 (insert-char ?* 1)
2026 (backward-char)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002027 (let ((base-indent 0) ; indentation of base line
2028 (base-text "") ; and text of base line
2029 (base-found-p nil))
Barry Warsaw03970731996-07-03 23:12:52 +00002030 (save-excursion
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00002031 (while (< 0 arg)
Barry Warsaw03970731996-07-03 23:12:52 +00002032 (condition-case nil ; in case no enclosing block
2033 (progn
2034 (py-goto-block-up 'no-mark)
2035 (setq base-indent (current-indentation)
2036 base-text (py-suck-up-leading-text)
2037 base-found-p t))
2038 (error nil))
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00002039 (setq arg (1- arg))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002040 (delete-char 1) ; toss the dummy character
2041 (delete-horizontal-space)
2042 (indent-to base-indent)
2043 (if base-found-p
2044 (message "Closes block: %s" base-text)))))
2045
Barry Warsawfc8a01f1995-03-09 16:07:29 +00002046
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00002047(defun py-electric-delete (arg)
Barry Warsaw41a05c71998-08-10 16:33:12 +00002048 "Delete preceding or following character or levels of whitespace.
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00002049
2050The behavior of this function depends on the variable
2051`delete-key-deletes-forward'. If this variable is nil (or does not
Barry Warsaw41a05c71998-08-10 16:33:12 +00002052exist, as in older Emacsen and non-XEmacs versions), then this
2053function behaves identically to \\[c-electric-backspace].
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00002054
2055If `delete-key-deletes-forward' is non-nil and is supported in your
2056Emacs, then deletion occurs in the forward direction, by calling the
Barry Warsaw41a05c71998-08-10 16:33:12 +00002057function in `py-delete-function'.
2058
2059\\[universal-argument] (programmatically, argument ARG) specifies the
2060number of characters to delete (default is 1)."
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00002061 (interactive "*p")
Barry Warsaw1d7b0fa1999-01-15 02:12:31 +00002062 (if (or (and (fboundp 'delete-forward-p) ;XEmacs 21
2063 (delete-forward-p))
2064 (and (boundp 'delete-key-deletes-forward) ;XEmacs 20
2065 delete-key-deletes-forward))
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00002066 (funcall py-delete-function arg)
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00002067 (py-electric-backspace arg)))
2068
2069;; required for pending-del and delsel modes
Skip Montanaro98a4fbe2003-08-01 04:00:32 +00002070(put 'py-electric-colon 'delete-selection t) ;delsel
2071(put 'py-electric-colon 'pending-delete t) ;pending-del
Barry Warsaw6d48c4a1997-11-04 19:21:50 +00002072(put 'py-electric-backspace 'delete-selection 'supersede) ;delsel
2073(put 'py-electric-backspace 'pending-delete 'supersede) ;pending-del
2074(put 'py-electric-delete 'delete-selection 'supersede) ;delsel
2075(put 'py-electric-delete 'pending-delete 'supersede) ;pending-del
2076
2077
2078
Barry Warsaw7cb505c1996-10-23 20:44:59 +00002079(defun py-indent-line (&optional arg)
2080 "Fix the indentation of the current line according to Python rules.
Barry Warsaw41a05c71998-08-10 16:33:12 +00002081With \\[universal-argument] (programmatically, the optional argument
2082ARG non-nil), ignore dedenting rules for block closing statements
2083(e.g. return, raise, break, continue, pass)
Barry Warsaw7cb505c1996-10-23 20:44:59 +00002084
2085This function is normally bound to `indent-line-function' so
2086\\[indent-for-tab-command] will call it."
2087 (interactive "P")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002088 (let* ((ci (current-indentation))
2089 (move-to-indentation-p (<= (current-column) ci))
Skip Montanarodc6d9e12008-02-05 02:32:16 +00002090 (need (py-compute-indentation (not arg)))
2091 (cc (current-column)))
2092 ;; dedent out a level if previous command was the same unless we're in
2093 ;; column 1
2094 (if (and (equal last-command this-command)
2095 (/= cc 0))
2096 (progn
2097 (beginning-of-line)
2098 (delete-horizontal-space)
2099 (indent-to (* (/ (- cc 1) py-indent-offset) py-indent-offset)))
2100 (progn
2101 ;; see if we need to dedent
2102 (if (py-outdent-p)
2103 (setq need (- need py-indent-offset)))
2104 (if (or py-tab-always-indent
2105 move-to-indentation-p)
2106 (progn (if (/= ci need)
2107 (save-excursion
2108 (beginning-of-line)
2109 (delete-horizontal-space)
2110 (indent-to need)))
2111 (if move-to-indentation-p (back-to-indentation)))
2112 (insert-tab))))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002113
2114(defun py-newline-and-indent ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002115 "Strives to act like the Emacs `newline-and-indent'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002116This is just `strives to' because correct indentation can't be computed
2117from scratch for Python code. In general, deletes the whitespace before
2118point, inserts a newline, and takes an educated guess as to how you want
2119the new line indented."
2120 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002121 (let ((ci (current-indentation)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002122 (if (< ci (current-column)) ; if point beyond indentation
2123 (newline-and-indent)
2124 ;; else try to act like newline-and-indent "normally" acts
2125 (beginning-of-line)
2126 (insert-char ?\n 1)
2127 (move-to-column ci))))
2128
Barry Warsaw7cb505c1996-10-23 20:44:59 +00002129(defun py-compute-indentation (honor-block-close-p)
Barry Warsaw41a05c71998-08-10 16:33:12 +00002130 "Compute Python indentation.
2131When HONOR-BLOCK-CLOSE-P is non-nil, statements such as `return',
2132`raise', `break', `continue', and `pass' force one level of
2133dedenting."
Barry Warsaw7ae77681994-12-12 20:38:05 +00002134 (save-excursion
Barry Warsawf64b4051998-02-12 16:52:14 +00002135 (beginning-of-line)
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00002136 (let* ((bod (py-point 'bod))
Barry Warsaw3b3ff4e1997-11-26 20:58:48 +00002137 (pps (parse-partial-sexp bod (point)))
Barry Warsaw9c1696c1998-12-15 04:36:22 +00002138 (boipps (parse-partial-sexp bod (py-point 'boi)))
2139 placeholder)
Barry Warsaw095e9c61995-09-19 20:01:42 +00002140 (cond
Barry Warsawa0ee8cd1997-11-26 01:04:44 +00002141 ;; are we inside a multi-line string or comment?
Barry Warsaw3b3ff4e1997-11-26 20:58:48 +00002142 ((or (and (nth 3 pps) (nth 3 boipps))
2143 (and (nth 4 pps) (nth 4 boipps)))
Barry Warsaw095e9c61995-09-19 20:01:42 +00002144 (save-excursion
2145 (if (not py-align-multiline-strings-p) 0
2146 ;; skip back over blank & non-indenting comment lines
2147 ;; note: will skip a blank or non-indenting comment line
2148 ;; that happens to be a continuation line too
2149 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move)
2150 (back-to-indentation)
2151 (current-column))))
2152 ;; are we on a continuation line?
2153 ((py-continuation-line-p)
2154 (let ((startpos (point))
2155 (open-bracket-pos (py-nesting-level))
Skip Montanarodc6d9e12008-02-05 02:32:16 +00002156 endpos searching found state cind cline)
Barry Warsaw095e9c61995-09-19 20:01:42 +00002157 (if open-bracket-pos
2158 (progn
Skip Montanarodc6d9e12008-02-05 02:32:16 +00002159 (setq endpos (py-point 'bol))
2160 (py-goto-initial-line)
2161 (setq cind (current-indentation))
2162 (setq cline cind)
2163 (dolist (bp
2164 (nth 9 (save-excursion
2165 (parse-partial-sexp (point) endpos)))
2166 cind)
2167 (if (search-forward "\n" bp t) (setq cline cind))
2168 (goto-char (1+ bp))
2169 (skip-chars-forward " \t")
2170 (setq cind (if (memq (following-char) '(?\n ?# ?\\))
2171 (+ cline py-indent-offset)
2172 (current-column)))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00002173 ;; else on backslash continuation line
2174 (forward-line -1)
2175 (if (py-continuation-line-p) ; on at least 3rd line in block
2176 (current-indentation) ; so just continue the pattern
2177 ;; else started on 2nd line in block, so indent more.
2178 ;; if base line is an assignment with a start on a RHS,
2179 ;; indent to 2 beyond the leftmost "="; else skip first
2180 ;; chunk of non-whitespace characters on base line, + 1 more
2181 ;; column
2182 (end-of-line)
Barry Warsawfd4c9e82001-06-18 23:40:35 +00002183 (setq endpos (point)
2184 searching t)
Barry Warsaw095e9c61995-09-19 20:01:42 +00002185 (back-to-indentation)
2186 (setq startpos (point))
2187 ;; look at all "=" from left to right, stopping at first
2188 ;; one not nested in a list or string
2189 (while searching
2190 (skip-chars-forward "^=" endpos)
2191 (if (= (point) endpos)
2192 (setq searching nil)
2193 (forward-char 1)
2194 (setq state (parse-partial-sexp startpos (point)))
2195 (if (and (zerop (car state)) ; not in a bracket
2196 (null (nth 3 state))) ; & not in a string
2197 (progn
2198 (setq searching nil) ; done searching in any case
2199 (setq found
2200 (not (or
2201 (eq (following-char) ?=)
2202 (memq (char-after (- (point) 2))
2203 '(?< ?> ?!)))))))))
2204 (if (or (not found) ; not an assignment
2205 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
2206 (progn
2207 (goto-char startpos)
2208 (skip-chars-forward "^ \t\n")))
Barry Warsaw40fb4522001-07-06 20:07:13 +00002209 ;; if this is a continuation for a block opening
2210 ;; statement, add some extra offset.
2211 (+ (current-column) (if (py-statement-opens-block-p)
2212 py-continuation-offset 0)
2213 1)
Barry Warsawfd4c9e82001-06-18 23:40:35 +00002214 ))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00002215
2216 ;; not on a continuation line
Barry Warsawa7891711996-08-01 15:53:09 +00002217 ((bobp) (current-indentation))
Barry Warsaw095e9c61995-09-19 20:01:42 +00002218
Barry Warsawa7891711996-08-01 15:53:09 +00002219 ;; Dfn: "Indenting comment line". A line containing only a
2220 ;; comment, but which is treated like a statement for
2221 ;; indentation calculation purposes. Such lines are only
2222 ;; treated specially by the mode; they are not treated
2223 ;; specially by the Python interpreter.
2224
2225 ;; The rules for indenting comment lines are a line where:
2226 ;; - the first non-whitespace character is `#', and
2227 ;; - the character following the `#' is whitespace, and
Barry Warsaw41a05c71998-08-10 16:33:12 +00002228 ;; - the line is dedented with respect to (i.e. to the left
Barry Warsawa7891711996-08-01 15:53:09 +00002229 ;; of) the indentation of the preceding non-blank line.
2230
2231 ;; The first non-blank line following an indenting comment
2232 ;; line is given the same amount of indentation as the
2233 ;; indenting comment line.
2234
2235 ;; All other comment-only lines are ignored for indentation
2236 ;; purposes.
2237
2238 ;; Are we looking at a comment-only line which is *not* an
Barry Warsaw145ab1c1998-05-19 14:49:49 +00002239 ;; indenting comment line? If so, we assume that it's been
Barry Warsawa7891711996-08-01 15:53:09 +00002240 ;; placed at the desired indentation, so leave it alone.
2241 ;; Indenting comment lines are aligned as statements down
2242 ;; below.
2243 ((and (looking-at "[ \t]*#[^ \t\n]")
2244 ;; NOTE: this test will not be performed in older Emacsen
2245 (fboundp 'forward-comment)
2246 (<= (current-indentation)
2247 (save-excursion
2248 (forward-comment (- (point-max)))
2249 (current-indentation))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00002250 (current-indentation))
2251
2252 ;; else indentation based on that of the statement that
2253 ;; precedes us; use the first line of that statement to
2254 ;; establish the base, in case the user forced a non-std
2255 ;; indentation for the continuation lines (if any)
2256 (t
Barry Warsawc01c5c81995-09-14 18:49:11 +00002257 ;; skip back over blank & non-indenting comment lines note:
2258 ;; will skip a blank or non-indenting comment line that
Barry Warsawfd0fb381996-03-04 17:15:40 +00002259 ;; happens to be a continuation line too. use fast Emacs 19
2260 ;; function if it's there.
Barry Warsaw6d627751996-03-06 18:41:38 +00002261 (if (and (eq py-honor-comment-indentation nil)
Barry Warsaw33d6ec01996-03-05 16:28:07 +00002262 (fboundp 'forward-comment))
Barry Warsawfd0fb381996-03-04 17:15:40 +00002263 (forward-comment (- (point-max)))
Barry Warsaw218eb751998-09-22 19:51:47 +00002264 (let ((prefix-re (concat py-block-comment-prefix "[ \t]*"))
2265 done)
Barry Warsaw6d627751996-03-06 18:41:38 +00002266 (while (not done)
Barry Warsaw12c92941998-08-10 21:44:37 +00002267 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#\\)" nil 'move)
2268 (setq done (or (bobp)
2269 (and (eq py-honor-comment-indentation t)
2270 (save-excursion
2271 (back-to-indentation)
Barry Warsaw218eb751998-09-22 19:51:47 +00002272 (not (looking-at prefix-re))
Barry Warsaw12c92941998-08-10 21:44:37 +00002273 ))
2274 (and (not (eq py-honor-comment-indentation t))
2275 (save-excursion
2276 (back-to-indentation)
Barry Warsawd36cfe42002-03-15 16:46:46 +00002277 (and (not (looking-at prefix-re))
2278 (or (looking-at "[^#]")
2279 (not (zerop (current-column)))
2280 ))
2281 ))
Barry Warsaw12c92941998-08-10 21:44:37 +00002282 ))
Barry Warsaw6d627751996-03-06 18:41:38 +00002283 )))
Barry Warsaw095e9c61995-09-19 20:01:42 +00002284 ;; if we landed inside a string, go to the beginning of that
2285 ;; string. this handles triple quoted, multi-line spanning
2286 ;; strings.
Barry Warsaw9c1696c1998-12-15 04:36:22 +00002287 (py-goto-beginning-of-tqs (nth 3 (parse-partial-sexp bod (point))))
Barry Warsawc210e691998-01-20 22:52:56 +00002288 ;; now skip backward over continued lines
Barry Warsaw9c1696c1998-12-15 04:36:22 +00002289 (setq placeholder (point))
Barry Warsaw095e9c61995-09-19 20:01:42 +00002290 (py-goto-initial-line)
Barry Warsaw9c1696c1998-12-15 04:36:22 +00002291 ;; we may *now* have landed in a TQS, so find the beginning of
2292 ;; this string.
2293 (py-goto-beginning-of-tqs
2294 (save-excursion (nth 3 (parse-partial-sexp
2295 placeholder (point)))))
Barry Warsawf831d811996-07-31 20:42:59 +00002296 (+ (current-indentation)
2297 (if (py-statement-opens-block-p)
2298 py-indent-offset
Barry Warsaw7cb505c1996-10-23 20:44:59 +00002299 (if (and honor-block-close-p (py-statement-closes-block-p))
Barry Warsawf831d811996-07-31 20:42:59 +00002300 (- py-indent-offset)
2301 0)))
2302 )))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002303
2304(defun py-guess-indent-offset (&optional global)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002305 "Guess a good value for, and change, `py-indent-offset'.
Barry Warsaw41a05c71998-08-10 16:33:12 +00002306
2307By default, make a buffer-local copy of `py-indent-offset' with the
2308new value, so that other Python buffers are not affected. With
2309\\[universal-argument] (programmatically, optional argument GLOBAL),
2310change the global value of `py-indent-offset'. This affects all
2311Python buffers (that don't have their own buffer-local copy), both
2312those currently existing and those created later in the Emacs session.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002313
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002314Some people use a different value for `py-indent-offset' than you use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002315There's no excuse for such foolishness, but sometimes you have to deal
2316with their ugly code anyway. This function examines the file and sets
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002317`py-indent-offset' to what it thinks it was when they created the
2318mess.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002319
2320Specifically, it searches forward from the statement containing point,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002321looking for a line that opens a block of code. `py-indent-offset' is
2322set to the difference in indentation between that line and the Python
Barry Warsaw7ae77681994-12-12 20:38:05 +00002323statement following it. If the search doesn't succeed going forward,
2324it's tried again going backward."
2325 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002326 (let (new-value
2327 (start (point))
Barry Warsawe908b6b1998-03-19 22:48:02 +00002328 (restart (point))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002329 (found nil)
2330 colon-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002331 (py-goto-initial-line)
2332 (while (not (or found (eobp)))
Barry Warsawe908b6b1998-03-19 22:48:02 +00002333 (when (and (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
2334 (not (py-in-literal restart)))
2335 (setq restart (point))
2336 (py-goto-initial-line)
2337 (if (py-statement-opens-block-p)
2338 (setq found t)
2339 (goto-char restart))))
2340 (unless found
Barry Warsaw7ae77681994-12-12 20:38:05 +00002341 (goto-char start)
2342 (py-goto-initial-line)
2343 (while (not (or found (bobp)))
Barry Warsawe908b6b1998-03-19 22:48:02 +00002344 (setq found (and
2345 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
2346 (or (py-goto-initial-line) t) ; always true -- side effect
2347 (py-statement-opens-block-p)))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002348 (setq colon-indent (current-indentation)
2349 found (and found (zerop (py-next-statement 1)))
2350 new-value (- (current-indentation) colon-indent))
2351 (goto-char start)
Barry Warsawe908b6b1998-03-19 22:48:02 +00002352 (if (not found)
2353 (error "Sorry, couldn't guess a value for py-indent-offset")
2354 (funcall (if global 'kill-local-variable 'make-local-variable)
2355 'py-indent-offset)
2356 (setq py-indent-offset new-value)
Barry Warsawd35c2551998-09-25 00:08:38 +00002357 (or noninteractive
2358 (message "%s value of py-indent-offset set to %d"
2359 (if global "Global" "Local")
2360 py-indent-offset)))
Barry Warsawe908b6b1998-03-19 22:48:02 +00002361 ))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002362
Barry Warsaw003932a1998-07-07 15:11:24 +00002363(defun py-comment-indent-function ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00002364 "Python version of `comment-indent-function'."
2365 ;; This is required when filladapt is turned off. Without it, when
2366 ;; filladapt is not used, comments which start in column zero
2367 ;; cascade one character to the right
Barry Warsaw003932a1998-07-07 15:11:24 +00002368 (save-excursion
2369 (beginning-of-line)
2370 (let ((eol (py-point 'eol)))
2371 (and comment-start-skip
2372 (re-search-forward comment-start-skip eol t)
2373 (setq eol (match-beginning 0)))
2374 (goto-char eol)
2375 (skip-chars-backward " \t")
2376 (max comment-column (+ (current-column) (if (bolp) 0 1)))
2377 )))
2378
Barry Warsawf8ddb6a1999-01-18 21:49:39 +00002379(defun py-narrow-to-defun (&optional class)
2380 "Make text outside current defun invisible.
2381The defun visible is the one that contains point or follows point.
2382Optional CLASS is passed directly to `py-beginning-of-def-or-class'."
2383 (interactive "P")
2384 (save-excursion
2385 (widen)
2386 (py-end-of-def-or-class class)
2387 (let ((end (point)))
2388 (py-beginning-of-def-or-class class)
2389 (narrow-to-region (point) end))))
2390
Barry Warsaw003932a1998-07-07 15:11:24 +00002391
Barry Warsaw7ae77681994-12-12 20:38:05 +00002392(defun py-shift-region (start end count)
Barry Warsaw41a05c71998-08-10 16:33:12 +00002393 "Indent lines from START to END by COUNT spaces."
Barry Warsaw7ae77681994-12-12 20:38:05 +00002394 (save-excursion
Barry Warsaw41a05c71998-08-10 16:33:12 +00002395 (goto-char end)
2396 (beginning-of-line)
2397 (setq end (point))
2398 (goto-char start)
2399 (beginning-of-line)
2400 (setq start (point))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002401 (indent-rigidly start end count)))
2402
2403(defun py-shift-region-left (start end &optional count)
2404 "Shift region of Python code to the left.
2405The lines from the line containing the start of the current region up
2406to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002407shifted to the left, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002408
2409If a prefix argument is given, the region is instead shifted by that
Barry Warsaw41a05c71998-08-10 16:33:12 +00002410many columns. With no active region, dedent only the current line.
2411You cannot dedent the region if any line is already at column zero."
Barry Warsawdea4a291996-07-03 22:59:12 +00002412 (interactive
2413 (let ((p (point))
2414 (m (mark))
2415 (arg current-prefix-arg))
2416 (if m
2417 (list (min p m) (max p m) arg)
2418 (list p (save-excursion (forward-line 1) (point)) arg))))
2419 ;; if any line is at column zero, don't shift the region
2420 (save-excursion
2421 (goto-char start)
2422 (while (< (point) end)
2423 (back-to-indentation)
Barry Warsaw71e315b1996-07-23 15:03:16 +00002424 (if (and (zerop (current-column))
2425 (not (looking-at "\\s *$")))
Barry Warsaw41a05c71998-08-10 16:33:12 +00002426 (error "Region is at left edge"))
Barry Warsawdea4a291996-07-03 22:59:12 +00002427 (forward-line 1)))
2428 (py-shift-region start end (- (prefix-numeric-value
2429 (or count py-indent-offset))))
2430 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002431
2432(defun py-shift-region-right (start end &optional count)
2433 "Shift region of Python code to the right.
2434The lines from the line containing the start of the current region up
2435to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002436shifted to the right, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002437
2438If a prefix argument is given, the region is instead shifted by that
Barry Warsawdea4a291996-07-03 22:59:12 +00002439many columns. With no active region, indent only the current line."
2440 (interactive
2441 (let ((p (point))
2442 (m (mark))
2443 (arg current-prefix-arg))
2444 (if m
2445 (list (min p m) (max p m) arg)
2446 (list p (save-excursion (forward-line 1) (point)) arg))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002447 (py-shift-region start end (prefix-numeric-value
Barry Warsawdea4a291996-07-03 22:59:12 +00002448 (or count py-indent-offset)))
2449 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002450
2451(defun py-indent-region (start end &optional indent-offset)
2452 "Reindent a region of Python code.
Barry Warsaw867a32a1996-03-07 18:30:26 +00002453
Barry Warsaw7ae77681994-12-12 20:38:05 +00002454The lines from the line containing the start of the current region up
2455to (but not including) the line containing the end of the region are
2456reindented. If the first line of the region has a non-whitespace
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002457character in the first column, the first line is left alone and the
2458rest of the region is reindented with respect to it. Else the entire
Barry Warsaw867a32a1996-03-07 18:30:26 +00002459region is reindented with respect to the (closest code or indenting
2460comment) statement immediately preceding the region.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002461
2462This is useful when code blocks are moved or yanked, when enclosing
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002463control structures are introduced or removed, or to reformat code
2464using a new value for the indentation offset.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002465
2466If a numeric prefix argument is given, it will be used as the value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002467the indentation offset. Else the value of `py-indent-offset' will be
Barry Warsaw7ae77681994-12-12 20:38:05 +00002468used.
2469
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002470Warning: The region must be consistently indented before this function
Barry Warsaw7ae77681994-12-12 20:38:05 +00002471is called! This function does not compute proper indentation from
2472scratch (that's impossible in Python), it merely adjusts the existing
2473indentation to be correct in context.
2474
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002475Warning: This function really has no idea what to do with
2476non-indenting comment lines, and shifts them as if they were indenting
2477comment lines. Fixing this appears to require telepathy.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002478
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002479Special cases: whitespace is deleted from blank lines; continuation
2480lines are shifted by the same amount their initial line was shifted,
2481in order to preserve their relative indentation with respect to their
Barry Warsaw7ae77681994-12-12 20:38:05 +00002482initial line; and comment lines beginning in column 1 are ignored."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002483 (interactive "*r\nP") ; region; raw prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00002484 (save-excursion
2485 (goto-char end) (beginning-of-line) (setq end (point-marker))
2486 (goto-char start) (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002487 (let ((py-indent-offset (prefix-numeric-value
2488 (or indent-offset py-indent-offset)))
2489 (indents '(-1)) ; stack of active indent levels
2490 (target-column 0) ; column to which to indent
2491 (base-shifted-by 0) ; amount last base line was shifted
2492 (indent-base (if (looking-at "[ \t\n]")
Barry Warsaw7cb505c1996-10-23 20:44:59 +00002493 (py-compute-indentation t)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002494 0))
2495 ci)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002496 (while (< (point) end)
2497 (setq ci (current-indentation))
2498 ;; figure out appropriate target column
2499 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002500 ((or (eq (following-char) ?#) ; comment in column 1
2501 (looking-at "[ \t]*$")) ; entirely blank
2502 (setq target-column 0))
2503 ((py-continuation-line-p) ; shift relative to base line
2504 (setq target-column (+ ci base-shifted-by)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002505 (t ; new base line
2506 (if (> ci (car indents)) ; going deeper; push it
2507 (setq indents (cons ci indents))
2508 ;; else we should have seen this indent before
2509 (setq indents (memq ci indents)) ; pop deeper indents
2510 (if (null indents)
2511 (error "Bad indentation in region, at line %d"
2512 (save-restriction
2513 (widen)
2514 (1+ (count-lines 1 (point)))))))
2515 (setq target-column (+ indent-base
2516 (* py-indent-offset
2517 (- (length indents) 2))))
2518 (setq base-shifted-by (- target-column ci))))
2519 ;; shift as needed
2520 (if (/= ci target-column)
2521 (progn
2522 (delete-horizontal-space)
2523 (indent-to target-column)))
2524 (forward-line 1))))
2525 (set-marker end nil))
2526
Barry Warsawa7891711996-08-01 15:53:09 +00002527(defun py-comment-region (beg end &optional arg)
2528 "Like `comment-region' but uses double hash (`#') comment starter."
2529 (interactive "r\nP")
Barry Warsaw3fcaf611996-08-01 20:11:51 +00002530 (let ((comment-start py-block-comment-prefix))
Barry Warsawa7891711996-08-01 15:53:09 +00002531 (comment-region beg end arg)))
2532
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002533
2534;; Functions for moving point
Barry Warsaw7ae77681994-12-12 20:38:05 +00002535(defun py-previous-statement (count)
Barry Warsaw41a05c71998-08-10 16:33:12 +00002536 "Go to the start of the COUNTth preceding Python statement.
2537By default, goes to the previous statement. If there is no such
2538statement, goes to the first statement. Return count of statements
2539left to move. `Statements' do not include blank, comment, or
2540continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002541 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00002542 (if (< count 0) (py-next-statement (- count))
2543 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002544 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002545 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002546 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00002547 (> count 0)
2548 (zerop (forward-line -1))
2549 (py-goto-statement-at-or-above))
2550 (setq count (1- count)))
2551 (if (> count 0) (goto-char start)))
2552 count))
2553
2554(defun py-next-statement (count)
2555 "Go to the start of next Python statement.
2556If the statement at point is the i'th Python statement, goes to the
2557start of statement i+COUNT. If there is no such statement, goes to the
2558last statement. Returns count of statements left to move. `Statements'
2559do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002560 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00002561 (if (< count 0) (py-previous-statement (- count))
2562 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002563 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002564 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002565 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00002566 (> count 0)
2567 (py-goto-statement-below))
2568 (setq count (1- count)))
2569 (if (> count 0) (goto-char start)))
2570 count))
2571
2572(defun py-goto-block-up (&optional nomark)
2573 "Move up to start of current block.
2574Go to the statement that starts the smallest enclosing block; roughly
2575speaking, this will be the closest preceding statement that ends with a
2576colon and is indented less than the statement you started on. If
2577successful, also sets the mark to the starting point.
2578
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002579`\\[py-mark-block]' can be used afterward to mark the whole code
2580block, if desired.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002581
2582If called from a program, the mark will not be set if optional argument
2583NOMARK is not nil."
2584 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002585 (let ((start (point))
2586 (found nil)
2587 initial-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002588 (py-goto-initial-line)
2589 ;; if on blank or non-indenting comment line, use the preceding stmt
2590 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
2591 (progn
2592 (py-goto-statement-at-or-above)
2593 (setq found (py-statement-opens-block-p))))
2594 ;; search back for colon line indented less
2595 (setq initial-indent (current-indentation))
2596 (if (zerop initial-indent)
2597 ;; force fast exit
2598 (goto-char (point-min)))
2599 (while (not (or found (bobp)))
2600 (setq found
2601 (and
2602 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
2603 (or (py-goto-initial-line) t) ; always true -- side effect
2604 (< (current-indentation) initial-indent)
2605 (py-statement-opens-block-p))))
2606 (if found
2607 (progn
2608 (or nomark (push-mark start))
2609 (back-to-indentation))
2610 (goto-char start)
2611 (error "Enclosing block not found"))))
2612
Barry Warsawab0e86c1998-05-19 15:31:46 +00002613(defun py-beginning-of-def-or-class (&optional class count)
2614 "Move point to start of `def' or `class'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002615
2616Searches back for the closest preceding `def'. If you supply a prefix
Barry Warsawab0e86c1998-05-19 15:31:46 +00002617arg, looks for a `class' instead. The docs below assume the `def'
2618case; just substitute `class' for `def' for the other case.
Barry Warsaw7c29b231998-07-07 17:45:38 +00002619Programmatically, if CLASS is `either', then moves to either `class'
2620or `def'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002621
Barry Warsawab0e86c1998-05-19 15:31:46 +00002622When second optional argument is given programmatically, move to the
2623COUNTth start of `def'.
2624
2625If point is in a `def' statement already, and after the `d', simply
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002626moves point to the start of the statement.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002627
Barry Warsawab0e86c1998-05-19 15:31:46 +00002628Otherwise (i.e. when point is not in a `def' statement, or at or
2629before the `d' of a `def' statement), searches for the closest
2630preceding `def' statement, and leaves point at its start. If no such
2631statement can be found, leaves point at the start of the buffer.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002632
Barry Warsawab0e86c1998-05-19 15:31:46 +00002633Returns t iff a `def' statement is found by these rules.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002634
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002635Note that doing this command repeatedly will take you closer to the
2636start of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002637
Barry Warsaw7c29b231998-07-07 17:45:38 +00002638To mark the current `def', see `\\[py-mark-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00002639 (interactive "P") ; raw prefix arg
Barry Warsaw6839d3a1998-10-28 00:10:45 +00002640 (setq count (or count 1))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002641 (let ((at-or-before-p (<= (current-column) (current-indentation)))
Barry Warsawab0e86c1998-05-19 15:31:46 +00002642 (start-of-line (goto-char (py-point 'bol)))
2643 (start-of-stmt (goto-char (py-point 'bos)))
Barry Warsaw7c29b231998-07-07 17:45:38 +00002644 (start-re (cond ((eq class 'either) "^[ \t]*\\(class\\|def\\)\\>")
2645 (class "^[ \t]*class\\>")
2646 (t "^[ \t]*def\\>")))
Barry Warsawab0e86c1998-05-19 15:31:46 +00002647 )
2648 ;; searching backward
2649 (if (and (< 0 count)
2650 (or (/= start-of-stmt start-of-line)
2651 (not at-or-before-p)))
2652 (end-of-line))
2653 ;; search forward
2654 (if (and (> 0 count)
2655 (zerop (current-column))
2656 (looking-at start-re))
2657 (end-of-line))
Barry Warsawddc46961999-07-27 21:40:02 +00002658 (if (re-search-backward start-re nil 'move count)
2659 (goto-char (match-beginning 0)))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002660
Barry Warsawab0e86c1998-05-19 15:31:46 +00002661;; Backwards compatibility
2662(defalias 'beginning-of-python-def-or-class 'py-beginning-of-def-or-class)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002663
Barry Warsawab0e86c1998-05-19 15:31:46 +00002664(defun py-end-of-def-or-class (&optional class count)
2665 "Move point beyond end of `def' or `class' body.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002666
Barry Warsawab0e86c1998-05-19 15:31:46 +00002667By default, looks for an appropriate `def'. If you supply a prefix
2668arg, looks for a `class' instead. The docs below assume the `def'
2669case; just substitute `class' for `def' for the other case.
Barry Warsaw7c29b231998-07-07 17:45:38 +00002670Programmatically, if CLASS is `either', then moves to either `class'
2671or `def'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002672
Barry Warsawab0e86c1998-05-19 15:31:46 +00002673When second optional argument is given programmatically, move to the
2674COUNTth end of `def'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002675
Barry Warsawab0e86c1998-05-19 15:31:46 +00002676If point is in a `def' statement already, this is the `def' we use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002677
Barry Warsawab0e86c1998-05-19 15:31:46 +00002678Else, if the `def' found by `\\[py-beginning-of-def-or-class]'
2679contains the statement you started on, that's the `def' we use.
2680
2681Otherwise, we search forward for the closest following `def', and use that.
2682
2683If a `def' can be found by these rules, point is moved to the start of
2684the line immediately following the `def' block, and the position of the
2685start of the `def' is returned.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002686
2687Else point is moved to the end of the buffer, and nil is returned.
2688
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002689Note that doing this command repeatedly will take you closer to the
2690end of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002691
Barry Warsaw7c29b231998-07-07 17:45:38 +00002692To mark the current `def', see `\\[py-mark-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00002693 (interactive "P") ; raw prefix arg
Barry Warsawab0e86c1998-05-19 15:31:46 +00002694 (if (and count (/= count 1))
2695 (py-beginning-of-def-or-class (- 1 count)))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002696 (let ((start (progn (py-goto-initial-line) (point)))
Barry Warsaw7c29b231998-07-07 17:45:38 +00002697 (which (cond ((eq class 'either) "\\(class\\|def\\)")
2698 (class "class")
2699 (t "def")))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002700 (state 'not-found))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002701 ;; move point to start of appropriate def/class
2702 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
2703 (setq state 'at-beginning)
Barry Warsawab0e86c1998-05-19 15:31:46 +00002704 ;; else see if py-beginning-of-def-or-class hits container
2705 (if (and (py-beginning-of-def-or-class class)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002706 (progn (py-goto-beyond-block)
2707 (> (point) start)))
2708 (setq state 'at-end)
2709 ;; else search forward
2710 (goto-char start)
2711 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
2712 (progn (setq state 'at-beginning)
2713 (beginning-of-line)))))
2714 (cond
2715 ((eq state 'at-beginning) (py-goto-beyond-block) t)
2716 ((eq state 'at-end) t)
2717 ((eq state 'not-found) nil)
Barry Warsaw41a05c71998-08-10 16:33:12 +00002718 (t (error "Internal error in `py-end-of-def-or-class'")))))
Barry Warsawab0e86c1998-05-19 15:31:46 +00002719
2720;; Backwards compabitility
Barry Warsaw820273d1998-05-19 15:54:45 +00002721(defalias 'end-of-python-def-or-class 'py-end-of-def-or-class)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002722
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002723
2724;; Functions for marking regions
Barry Warsaw7ae77681994-12-12 20:38:05 +00002725(defun py-mark-block (&optional extend just-move)
2726 "Mark following block of lines. With prefix arg, mark structure.
2727Easier to use than explain. It sets the region to an `interesting'
2728block of succeeding lines. If point is on a blank line, it goes down to
2729the next non-blank line. That will be the start of the region. The end
2730of the region depends on the kind of line at the start:
2731
2732 - If a comment, the region will include all succeeding comment lines up
2733 to (but not including) the next non-comment line (if any).
2734
2735 - Else if a prefix arg is given, and the line begins one of these
2736 structures:
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002737
2738 if elif else try except finally for while def class
2739
Barry Warsaw7ae77681994-12-12 20:38:05 +00002740 the region will be set to the body of the structure, including
2741 following blocks that `belong' to it, but excluding trailing blank
2742 and comment lines. E.g., if on a `try' statement, the `try' block
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002743 and all (if any) of the following `except' and `finally' blocks
2744 that belong to the `try' structure will be in the region. Ditto
2745 for if/elif/else, for/else and while/else structures, and (a bit
2746 degenerate, since they're always one-block structures) def and
2747 class blocks.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002748
2749 - Else if no prefix argument is given, and the line begins a Python
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002750 block (see list above), and the block is not a `one-liner' (i.e.,
2751 the statement ends with a colon, not with code), the region will
2752 include all succeeding lines up to (but not including) the next
2753 code statement (if any) that's indented no more than the starting
2754 line, except that trailing blank and comment lines are excluded.
2755 E.g., if the starting line begins a multi-statement `def'
2756 structure, the region will be set to the full function definition,
2757 but without any trailing `noise' lines.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002758
2759 - Else the region will include all succeeding lines up to (but not
2760 including) the next blank line, or code or indenting-comment line
2761 indented strictly less than the starting line. Trailing indenting
2762 comment lines are included in this case, but not trailing blank
2763 lines.
2764
2765A msg identifying the location of the mark is displayed in the echo
2766area; or do `\\[exchange-point-and-mark]' to flip down to the end.
2767
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002768If called from a program, optional argument EXTEND plays the role of
2769the prefix arg, and if optional argument JUST-MOVE is not nil, just
2770moves to the end of the block (& does not set mark or display a msg)."
Barry Warsaw7ae77681994-12-12 20:38:05 +00002771 (interactive "P") ; raw prefix arg
2772 (py-goto-initial-line)
2773 ;; skip over blank lines
2774 (while (and
2775 (looking-at "[ \t]*$") ; while blank line
2776 (not (eobp))) ; & somewhere to go
2777 (forward-line 1))
2778 (if (eobp)
2779 (error "Hit end of buffer without finding a non-blank stmt"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002780 (let ((initial-pos (point))
2781 (initial-indent (current-indentation))
2782 last-pos ; position of last stmt in region
2783 (followers
2784 '((if elif else) (elif elif else) (else)
2785 (try except finally) (except except) (finally)
2786 (for else) (while else)
2787 (def) (class) ) )
2788 first-symbol next-symbol)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002789
2790 (cond
2791 ;; if comment line, suck up the following comment lines
2792 ((looking-at "[ \t]*#")
2793 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
2794 (re-search-backward "^[ \t]*#") ; and back to last comment in block
2795 (setq last-pos (point)))
2796
2797 ;; else if line is a block line and EXTEND given, suck up
2798 ;; the whole structure
2799 ((and extend
2800 (setq first-symbol (py-suck-up-first-keyword) )
2801 (assq first-symbol followers))
2802 (while (and
2803 (or (py-goto-beyond-block) t) ; side effect
2804 (forward-line -1) ; side effect
2805 (setq last-pos (point)) ; side effect
2806 (py-goto-statement-below)
2807 (= (current-indentation) initial-indent)
2808 (setq next-symbol (py-suck-up-first-keyword))
2809 (memq next-symbol (cdr (assq first-symbol followers))))
2810 (setq first-symbol next-symbol)))
2811
2812 ;; else if line *opens* a block, search for next stmt indented <=
2813 ((py-statement-opens-block-p)
2814 (while (and
2815 (setq last-pos (point)) ; always true -- side effect
2816 (py-goto-statement-below)
Barry Warsawf070cce2002-05-23 19:42:16 +00002817 (> (current-indentation) initial-indent)
2818 )))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002819
2820 ;; else plain code line; stop at next blank line, or stmt or
2821 ;; indenting comment line indented <
2822 (t
2823 (while (and
2824 (setq last-pos (point)) ; always true -- side effect
2825 (or (py-goto-beyond-final-line) t)
2826 (not (looking-at "[ \t]*$")) ; stop at blank line
2827 (or
2828 (>= (current-indentation) initial-indent)
2829 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
2830 nil)))
2831
2832 ;; skip to end of last stmt
2833 (goto-char last-pos)
2834 (py-goto-beyond-final-line)
2835
2836 ;; set mark & display
2837 (if just-move
2838 () ; just return
2839 (push-mark (point) 'no-msg)
2840 (forward-line -1)
2841 (message "Mark set after: %s" (py-suck-up-leading-text))
2842 (goto-char initial-pos))))
2843
Barry Warsaw2518c671997-11-05 00:51:08 +00002844(defun py-mark-def-or-class (&optional class)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002845 "Set region to body of def (or class, with prefix arg) enclosing point.
2846Pushes the current mark, then point, on the mark ring (all language
2847modes do this, but although it's handy it's never documented ...).
2848
2849In most Emacs language modes, this function bears at least a
Barry Warsawab0e86c1998-05-19 15:31:46 +00002850hallucinogenic resemblance to `\\[py-end-of-def-or-class]' and
2851`\\[py-beginning-of-def-or-class]'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002852
2853And in earlier versions of Python mode, all 3 were tightly connected.
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002854Turned out that was more confusing than useful: the `goto start' and
2855`goto end' commands are usually used to search through a file, and
2856people expect them to act a lot like `search backward' and `search
2857forward' string-search commands. But because Python `def' and `class'
2858can nest to arbitrary levels, finding the smallest def containing
2859point cannot be done via a simple backward search: the def containing
2860point may not be the closest preceding def, or even the closest
2861preceding def that's indented less. The fancy algorithm required is
2862appropriate for the usual uses of this `mark' command, but not for the
2863`goto' variations.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002864
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002865So the def marked by this command may not be the one either of the
2866`goto' commands find: If point is on a blank or non-indenting comment
2867line, moves back to start of the closest preceding code statement or
2868indenting comment line. If this is a `def' statement, that's the def
2869we use. Else searches for the smallest enclosing `def' block and uses
2870that. Else signals an error.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002871
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002872When an enclosing def is found: The mark is left immediately beyond
2873the last line of the def block. Point is left at the start of the
2874def, except that: if the def is preceded by a number of comment lines
2875followed by (at most) one optional blank line, point is left at the
2876start of the comments; else if the def is preceded by a blank line,
2877point is left at its start.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002878
2879The intent is to mark the containing def/class and its associated
2880documentation, to make moving and duplicating functions and classes
2881pleasant."
2882 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002883 (let ((start (point))
Barry Warsaw7c29b231998-07-07 17:45:38 +00002884 (which (cond ((eq class 'either) "\\(class\\|def\\)")
2885 (class "class")
2886 (t "def"))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002887 (push-mark start)
2888 (if (not (py-go-up-tree-to-keyword which))
2889 (progn (goto-char start)
Barry Warsaw7c29b231998-07-07 17:45:38 +00002890 (error "Enclosing %s not found"
2891 (if (eq class 'either)
2892 "def or class"
2893 which)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002894 ;; else enclosing def/class found
2895 (setq start (point))
2896 (py-goto-beyond-block)
2897 (push-mark (point))
2898 (goto-char start)
2899 (if (zerop (forward-line -1)) ; if there is a preceding line
2900 (progn
2901 (if (looking-at "[ \t]*$") ; it's blank
2902 (setq start (point)) ; so reset start point
2903 (goto-char start)) ; else try again
2904 (if (zerop (forward-line -1))
2905 (if (looking-at "[ \t]*#") ; a comment
2906 ;; look back for non-comment line
2907 ;; tricky: note that the regexp matches a blank
2908 ;; line, cuz \n is in the 2nd character class
2909 (and
2910 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
2911 (forward-line 1))
2912 ;; no comment, so go back
Barry Warsaw4da6bd51997-11-26 06:00:26 +00002913 (goto-char start)))))))
2914 (exchange-point-and-mark)
2915 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002916
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002917;; ripped from cc-mode
2918(defun py-forward-into-nomenclature (&optional arg)
2919 "Move forward to end of a nomenclature section or word.
Skip Montanarodc6d9e12008-02-05 02:32:16 +00002920With \\[universal-argument] (programmatically, optional argument ARG),
Barry Warsaw41a05c71998-08-10 16:33:12 +00002921do it that many times.
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002922
2923A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
2924 (interactive "p")
2925 (let ((case-fold-search nil))
2926 (if (> arg 0)
Barry Warsawc5a8cbd1996-08-05 21:53:02 +00002927 (re-search-forward
2928 "\\(\\W\\|[_]\\)*\\([A-Z]*[a-z0-9]*\\)"
2929 (point-max) t arg)
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002930 (while (and (< arg 0)
2931 (re-search-backward
Barry Warsawc5a8cbd1996-08-05 21:53:02 +00002932 "\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\(\\W\\|[_]\\)\\w+"
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00002933 (point-min) 0))
2934 (forward-char 1)
2935 (setq arg (1+ arg)))))
2936 (py-keep-region-active))
2937
2938(defun py-backward-into-nomenclature (&optional arg)
2939 "Move backward to beginning of a nomenclature section or word.
2940With optional ARG, move that many times. If ARG is negative, move
2941forward.
2942
2943A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
2944 (interactive "p")
2945 (py-forward-into-nomenclature (- arg))
2946 (py-keep-region-active))
2947
2948
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002949
Barry Warsaw4f577d22001-04-11 20:23:17 +00002950;; pdbtrack functions
2951(defun py-pdbtrack-toggle-stack-tracking (arg)
2952 (interactive "P")
2953 (if (not (get-buffer-process (current-buffer)))
2954 (error "No process associated with buffer '%s'" (current-buffer)))
2955 ;; missing or 0 is toggle, >0 turn on, <0 turn off
2956 (if (or (not arg)
2957 (zerop (setq arg (prefix-numeric-value arg))))
2958 (setq py-pdbtrack-do-tracking-p (not py-pdbtrack-do-tracking-p))
2959 (setq py-pdbtrack-do-tracking-p (> arg 0)))
2960 (message "%sabled Python's pdbtrack"
2961 (if py-pdbtrack-do-tracking-p "En" "Dis")))
2962
2963(defun turn-on-pdbtrack ()
2964 (interactive)
2965 (py-pdbtrack-toggle-stack-tracking 1))
2966
2967(defun turn-off-pdbtrack ()
2968 (interactive)
2969 (py-pdbtrack-toggle-stack-tracking 0))
2970
2971
2972
Barry Warsaw29a90f02002-04-22 21:48:20 +00002973;; Pychecker
Skip Montanarodc6d9e12008-02-05 02:32:16 +00002974
2975;; hack for FSF Emacs
2976(unless (fboundp 'read-shell-command)
2977 (defalias 'read-shell-command 'read-string))
2978
Barry Warsaw29a90f02002-04-22 21:48:20 +00002979(defun py-pychecker-run (command)
2980 "*Run pychecker (default on the file currently visited)."
2981 (interactive
2982 (let ((default
2983 (format "%s %s %s" py-pychecker-command
2984 (mapconcat 'identity py-pychecker-command-args " ")
Barry Warsaw7f232122002-09-28 18:17:56 +00002985 (buffer-file-name)))
2986 (last (when py-pychecker-history
2987 (let* ((lastcmd (car py-pychecker-history))
2988 (cmd (cdr (reverse (split-string lastcmd))))
2989 (newcmd (reverse (cons (buffer-file-name) cmd))))
2990 (mapconcat 'identity newcmd " ")))))
Barry Warsaw29a90f02002-04-22 21:48:20 +00002991
2992 (list
Skip Montanaro302e5502003-12-18 21:53:33 +00002993 (if (fboundp 'read-shell-command)
2994 (read-shell-command "Run pychecker like this: "
2995 (if last
2996 last
2997 default)
2998 'py-pychecker-history)
2999 (read-string "Run pychecker like this: "
3000 (if last
3001 last
3002 default)
3003 'py-pychecker-history))
3004 )))
Barry Warsaw29a90f02002-04-22 21:48:20 +00003005 (save-some-buffers (not py-ask-about-save) nil)
Barry Warsawdaa19212002-07-16 15:56:28 +00003006 (compile-internal command "No more errors"))
Barry Warsaw29a90f02002-04-22 21:48:20 +00003007
3008
3009
3010;; pydoc commands. The guts of this function is stolen from XEmacs's
3011;; symbol-near-point, but without the useless regexp-quote call on the
3012;; results, nor the interactive bit. Also, we've added the temporary
3013;; syntax table setting, which Skip originally had broken out into a
3014;; separate function. Note that Emacs doesn't have the original
3015;; function.
Barry Warsawa7cc43b2002-04-22 17:15:19 +00003016(defun py-symbol-near-point ()
3017 "Return the first textual item to the nearest point."
3018 ;; alg stolen from etag.el
3019 (save-excursion
3020 (with-syntax-table py-dotted-expression-syntax-table
3021 (if (or (bobp) (not (memq (char-syntax (char-before)) '(?w ?_))))
3022 (while (not (looking-at "\\sw\\|\\s_\\|\\'"))
3023 (forward-char 1)))
3024 (while (looking-at "\\sw\\|\\s_")
3025 (forward-char 1))
3026 (if (re-search-backward "\\sw\\|\\s_" nil t)
3027 (progn (forward-char 1)
3028 (buffer-substring (point)
3029 (progn (forward-sexp -1)
3030 (while (looking-at "\\s'")
3031 (forward-char 1))
3032 (point))))
3033 nil))))
3034
3035(defun py-help-at-point ()
3036 "Get help from Python based on the symbol nearest point."
3037 (interactive)
3038 (let* ((sym (py-symbol-near-point))
3039 (base (substring sym 0 (or (search "." sym :from-end t) 0)))
3040 cmd)
3041 (if (not (equal base ""))
3042 (setq cmd (concat "import " base "\n")))
3043 (setq cmd (concat "import pydoc\n"
3044 cmd
Barry Warsaw04949552002-04-22 22:05:49 +00003045 "try: pydoc.help('" sym "')\n"
Barry Warsawa7cc43b2002-04-22 17:15:19 +00003046 "except: print 'No help available on:', \"" sym "\""))
3047 (message cmd)
Barry Warsaw04949552002-04-22 22:05:49 +00003048 (py-execute-string cmd)
3049 (set-buffer "*Python Output*")
3050 ;; BAW: Should we really be leaving the output buffer in help-mode?
3051 (help-mode)))
Barry Warsawa7cc43b2002-04-22 17:15:19 +00003052
3053
3054
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003055;; Documentation functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00003056
3057;; dump the long form of the mode blurb; does the usual doc escapes,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003058;; plus lines of the form ^[vc]:name$ to suck variable & command docs
3059;; out of the right places, along with the keys they're on & current
3060;; values
Barry Warsaw7ae77681994-12-12 20:38:05 +00003061(defun py-dump-help-string (str)
3062 (with-output-to-temp-buffer "*Help*"
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003063 (let ((locals (buffer-local-variables))
3064 funckind funcname func funcdoc
3065 (start 0) mstart end
3066 keys )
Barry Warsaw7ae77681994-12-12 20:38:05 +00003067 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
3068 (setq mstart (match-beginning 0) end (match-end 0)
3069 funckind (substring str (match-beginning 1) (match-end 1))
3070 funcname (substring str (match-beginning 2) (match-end 2))
3071 func (intern funcname))
3072 (princ (substitute-command-keys (substring str start mstart)))
3073 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003074 ((equal funckind "c") ; command
3075 (setq funcdoc (documentation func)
3076 keys (concat
3077 "Key(s): "
3078 (mapconcat 'key-description
3079 (where-is-internal func py-mode-map)
3080 ", "))))
3081 ((equal funckind "v") ; variable
Barry Warsaw604cefa1996-09-03 18:17:04 +00003082 (setq funcdoc (documentation-property func 'variable-documentation)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003083 keys (if (assq func locals)
3084 (concat
3085 "Local/Global values: "
3086 (prin1-to-string (symbol-value func))
3087 " / "
3088 (prin1-to-string (default-value func)))
3089 (concat
3090 "Value: "
3091 (prin1-to-string (symbol-value func))))))
3092 (t ; unexpected
3093 (error "Error in py-dump-help-string, tag `%s'" funckind)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00003094 (princ (format "\n-> %s:\t%s\t%s\n\n"
3095 (if (equal funckind "c") "Command" "Variable")
3096 funcname keys))
3097 (princ funcdoc)
3098 (terpri)
3099 (setq start end))
3100 (princ (substitute-command-keys (substring str start))))
3101 (print-help-return-message)))
3102
3103(defun py-describe-mode ()
3104 "Dump long form of Python-mode docs."
3105 (interactive)
3106 (py-dump-help-string "Major mode for editing Python files.
3107Knows about Python indentation, tokens, comments and continuation lines.
3108Paragraphs are separated by blank lines only.
3109
3110Major sections below begin with the string `@'; specific function and
3111variable docs begin with `->'.
3112
3113@EXECUTING PYTHON CODE
3114
Barry Warsaw820273d1998-05-19 15:54:45 +00003115\\[py-execute-import-or-reload]\timports or reloads the file in the Python interpreter
Barry Warsaw7ae77681994-12-12 20:38:05 +00003116\\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
3117\\[py-execute-region]\tsends the current region
Barry Warsaw820273d1998-05-19 15:54:45 +00003118\\[py-execute-def-or-class]\tsends the current function or class definition
3119\\[py-execute-string]\tsends an arbitrary string
Barry Warsaw7ae77681994-12-12 20:38:05 +00003120\\[py-shell]\tstarts a Python interpreter window; this will be used by
Barry Warsaw820273d1998-05-19 15:54:45 +00003121\tsubsequent Python execution commands
3122%c:py-execute-import-or-reload
Barry Warsaw7ae77681994-12-12 20:38:05 +00003123%c:py-execute-buffer
3124%c:py-execute-region
Barry Warsaw820273d1998-05-19 15:54:45 +00003125%c:py-execute-def-or-class
3126%c:py-execute-string
Barry Warsaw7ae77681994-12-12 20:38:05 +00003127%c:py-shell
3128
3129@VARIABLES
3130
3131py-indent-offset\tindentation increment
Barry Warsaw42f707f1996-07-29 21:05:05 +00003132py-block-comment-prefix\tcomment string used by comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00003133
3134py-python-command\tshell command to invoke Python interpreter
Barry Warsaw7ae77681994-12-12 20:38:05 +00003135py-temp-directory\tdirectory used for temp files (if needed)
3136
3137py-beep-if-tab-change\tring the bell if tab-width is changed
3138%v:py-indent-offset
3139%v:py-block-comment-prefix
3140%v:py-python-command
Barry Warsaw7ae77681994-12-12 20:38:05 +00003141%v:py-temp-directory
3142%v:py-beep-if-tab-change
3143
3144@KINDS OF LINES
3145
3146Each physical line in the file is either a `continuation line' (the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003147preceding line ends with a backslash that's not part of a comment, or
3148the paren/bracket/brace nesting level at the start of the line is
3149non-zero, or both) or an `initial line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00003150
3151An initial line is in turn a `blank line' (contains nothing except
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003152possibly blanks or tabs), a `comment line' (leftmost non-blank
3153character is `#'), or a `code line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00003154
3155Comment Lines
3156
3157Although all comment lines are treated alike by Python, Python mode
3158recognizes two kinds that act differently with respect to indentation.
3159
3160An `indenting comment line' is a comment line with a blank, tab or
3161nothing after the initial `#'. The indentation commands (see below)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003162treat these exactly as if they were code lines: a line following an
Barry Warsaw7ae77681994-12-12 20:38:05 +00003163indenting comment line will be indented like the comment line. All
3164other comment lines (those with a non-whitespace character immediately
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003165following the initial `#') are `non-indenting comment lines', and
3166their indentation is ignored by the indentation commands.
Barry Warsaw7ae77681994-12-12 20:38:05 +00003167
3168Indenting comment lines are by far the usual case, and should be used
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003169whenever possible. Non-indenting comment lines are useful in cases
3170like these:
Barry Warsaw7ae77681994-12-12 20:38:05 +00003171
3172\ta = b # a very wordy single-line comment that ends up being
3173\t #... continued onto another line
3174
3175\tif a == b:
3176##\t\tprint 'panic!' # old code we've `commented out'
3177\t\treturn a
3178
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003179Since the `#...' and `##' comment lines have a non-whitespace
3180character following the initial `#', Python mode ignores them when
3181computing the proper indentation for the next line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00003182
3183Continuation Lines and Statements
3184
3185The Python-mode commands generally work on statements instead of on
3186individual lines, where a `statement' is a comment or blank line, or a
3187code line and all of its following continuation lines (if any)
3188considered as a single logical unit. The commands in this mode
3189generally (when it makes sense) automatically move to the start of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003190statement containing point, even if point happens to be in the middle
3191of some continuation line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00003192
3193
3194@INDENTATION
3195
3196Primarily for entering new code:
3197\t\\[indent-for-tab-command]\t indent line appropriately
3198\t\\[py-newline-and-indent]\t insert newline, then indent
Barry Warsaw27ee1151997-12-03 05:03:44 +00003199\t\\[py-electric-backspace]\t reduce indentation, or delete single character
Barry Warsaw7ae77681994-12-12 20:38:05 +00003200
3201Primarily for reindenting existing code:
3202\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
3203\t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
3204
3205\t\\[py-indent-region]\t reindent region to match its context
3206\t\\[py-shift-region-left]\t shift region left by py-indent-offset
3207\t\\[py-shift-region-right]\t shift region right by py-indent-offset
3208
3209Unlike most programming languages, Python uses indentation, and only
3210indentation, to specify block structure. Hence the indentation supplied
3211automatically by Python-mode is just an educated guess: only you know
3212the block structure you intend, so only you can supply correct
3213indentation.
3214
3215The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
3216the indentation of preceding statements. E.g., assuming
3217py-indent-offset is 4, after you enter
3218\tif a > 0: \\[py-newline-and-indent]
3219the cursor will be moved to the position of the `_' (_ is not a
3220character in the file, it's just used here to indicate the location of
3221the cursor):
3222\tif a > 0:
3223\t _
3224If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
3225to
3226\tif a > 0:
3227\t c = d
3228\t _
3229Python-mode cannot know whether that's what you intended, or whether
3230\tif a > 0:
3231\t c = d
3232\t_
3233was your intent. In general, Python-mode either reproduces the
3234indentation of the (closest code or indenting-comment) preceding
3235statement, or adds an extra py-indent-offset blanks if the preceding
3236statement has `:' as its last significant (non-whitespace and non-
3237comment) character. If the suggested indentation is too much, use
Barry Warsaw27ee1151997-12-03 05:03:44 +00003238\\[py-electric-backspace] to reduce it.
Barry Warsaw7ae77681994-12-12 20:38:05 +00003239
3240Continuation lines are given extra indentation. If you don't like the
3241suggested indentation, change it to something you do like, and Python-
3242mode will strive to indent later lines of the statement in the same way.
3243
3244If a line is a continuation line by virtue of being in an unclosed
3245paren/bracket/brace structure (`list', for short), the suggested
3246indentation depends on whether the current line contains the first item
3247in the list. If it does, it's indented py-indent-offset columns beyond
3248the indentation of the line containing the open bracket. If you don't
3249like that, change it by hand. The remaining items in the list will mimic
3250whatever indentation you give to the first item.
3251
3252If a line is a continuation line because the line preceding it ends with
3253a backslash, the third and following lines of the statement inherit their
3254indentation from the line preceding them. The indentation of the second
3255line in the statement depends on the form of the first (base) line: if
3256the base line is an assignment statement with anything more interesting
3257than the backslash following the leftmost assigning `=', the second line
3258is indented two columns beyond that `='. Else it's indented to two
3259columns beyond the leftmost solid chunk of non-whitespace characters on
3260the base line.
3261
3262Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
3263repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
3264structure you intend.
3265%c:indent-for-tab-command
3266%c:py-newline-and-indent
Barry Warsaw27ee1151997-12-03 05:03:44 +00003267%c:py-electric-backspace
Barry Warsaw7ae77681994-12-12 20:38:05 +00003268
3269
3270The next function may be handy when editing code you didn't write:
3271%c:py-guess-indent-offset
3272
3273
3274The remaining `indent' functions apply to a region of Python code. They
3275assume the block structure (equals indentation, in Python) of the region
3276is correct, and alter the indentation in various ways while preserving
3277the block structure:
3278%c:py-indent-region
3279%c:py-shift-region-left
3280%c:py-shift-region-right
3281
3282@MARKING & MANIPULATING REGIONS OF CODE
3283
3284\\[py-mark-block]\t mark block of lines
Barry Warsaw2518c671997-11-05 00:51:08 +00003285\\[py-mark-def-or-class]\t mark smallest enclosing def
3286\\[universal-argument] \\[py-mark-def-or-class]\t mark smallest enclosing class
Barry Warsaw42f707f1996-07-29 21:05:05 +00003287\\[comment-region]\t comment out region of code
3288\\[universal-argument] \\[comment-region]\t uncomment region of code
Barry Warsaw7ae77681994-12-12 20:38:05 +00003289%c:py-mark-block
Barry Warsaw2518c671997-11-05 00:51:08 +00003290%c:py-mark-def-or-class
Barry Warsaw42f707f1996-07-29 21:05:05 +00003291%c:comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00003292
3293@MOVING POINT
3294
3295\\[py-previous-statement]\t move to statement preceding point
3296\\[py-next-statement]\t move to statement following point
3297\\[py-goto-block-up]\t move up to start of current block
Barry Warsawab0e86c1998-05-19 15:31:46 +00003298\\[py-beginning-of-def-or-class]\t move to start of def
3299\\[universal-argument] \\[py-beginning-of-def-or-class]\t move to start of class
3300\\[py-end-of-def-or-class]\t move to end of def
3301\\[universal-argument] \\[py-end-of-def-or-class]\t move to end of class
Barry Warsaw7ae77681994-12-12 20:38:05 +00003302
3303The first two move to one statement beyond the statement that contains
3304point. A numeric prefix argument tells them to move that many
3305statements instead. Blank lines, comment lines, and continuation lines
3306do not count as `statements' for these commands. So, e.g., you can go
3307to the first code statement in a file by entering
3308\t\\[beginning-of-buffer]\t to move to the top of the file
3309\t\\[py-next-statement]\t to skip over initial comments and blank lines
3310Or do `\\[py-previous-statement]' with a huge prefix argument.
3311%c:py-previous-statement
3312%c:py-next-statement
3313%c:py-goto-block-up
Barry Warsawab0e86c1998-05-19 15:31:46 +00003314%c:py-beginning-of-def-or-class
3315%c:py-end-of-def-or-class
Barry Warsaw7ae77681994-12-12 20:38:05 +00003316
3317@LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
3318
3319`\\[indent-new-comment-line]' is handy for entering a multi-line comment.
3320
3321`\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
3322overall class and def structure of a module.
3323
3324`\\[back-to-indentation]' moves point to a line's first non-blank character.
3325
3326`\\[indent-relative]' is handy for creating odd indentation.
3327
3328@OTHER EMACS HINTS
3329
3330If you don't like the default value of a variable, change its value to
3331whatever you do like by putting a `setq' line in your .emacs file.
3332E.g., to set the indentation increment to 4, put this line in your
3333.emacs:
3334\t(setq py-indent-offset 4)
3335To see the value of a variable, do `\\[describe-variable]' and enter the variable
3336name at the prompt.
3337
3338When entering a key sequence like `C-c C-n', it is not necessary to
3339release the CONTROL key after doing the `C-c' part -- it suffices to
3340press the CONTROL key, press and release `c' (while still holding down
3341CONTROL), press and release `n' (while still holding down CONTROL), &
3342then release CONTROL.
3343
3344Entering Python mode calls with no arguments the value of the variable
3345`python-mode-hook', if that value exists and is not nil; for backward
3346compatibility it also tries `py-mode-hook'; see the `Hooks' section of
3347the Elisp manual for details.
3348
3349Obscure: When python-mode is first loaded, it looks for all bindings
3350to newline-and-indent in the global keymap, and shadows them with
3351local bindings to py-newline-and-indent."))
3352
Barry Warsaw56bd2ed2002-04-25 15:44:17 +00003353(require 'info-look)
Barry Warsaw5dfc7af2002-04-30 18:58:52 +00003354;; The info-look package does not always provide this function (it
3355;; appears this is the case with XEmacs 21.1)
3356(when (fboundp 'info-lookup-maybe-add-help)
3357 (info-lookup-maybe-add-help
3358 :mode 'python-mode
3359 :regexp "[a-zA-Z0-9_]+"
3360 :doc-spec '(("(python-lib)Module Index")
3361 ("(python-lib)Class-Exception-Object Index")
3362 ("(python-lib)Function-Method-Variable Index")
3363 ("(python-lib)Miscellaneous Index")))
3364 )
Barry Warsaw56bd2ed2002-04-25 15:44:17 +00003365
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003366
3367;; Helper functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00003368(defvar py-parse-state-re
3369 (concat
Barry Warsaw644991f2002-10-21 15:58:29 +00003370 "^[ \t]*\\(elif\\|else\\|while\\|def\\|class\\)\\>"
Barry Warsaw7ae77681994-12-12 20:38:05 +00003371 "\\|"
3372 "^[^ #\t\n]"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003373
Barry Warsaw7ae77681994-12-12 20:38:05 +00003374(defun py-parse-state ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003375 "Return the parse state at point (see `parse-partial-sexp' docs)."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003376 (save-excursion
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00003377 (let ((here (point))
Barry Warsawb2d5e622002-04-22 15:29:27 +00003378 pps done)
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00003379 (while (not done)
3380 ;; back up to the first preceding line (if any; else start of
3381 ;; buffer) that begins with a popular Python keyword, or a
3382 ;; non- whitespace and non-comment character. These are good
3383 ;; places to start parsing to see whether where we started is
3384 ;; at a non-zero nesting level. It may be slow for people who
3385 ;; write huge code blocks or huge lists ... tough beans.
3386 (re-search-backward py-parse-state-re nil 'move)
3387 (beginning-of-line)
Barry Warsawf64b4051998-02-12 16:52:14 +00003388 ;; In XEmacs, we have a much better way to test for whether
3389 ;; we're in a triple-quoted string or not. Emacs does not
Barry Warsaw145ab1c1998-05-19 14:49:49 +00003390 ;; have this built-in function, which is its loss because
Barry Warsawf64b4051998-02-12 16:52:14 +00003391 ;; without scanning from the beginning of the buffer, there's
3392 ;; no accurate way to determine this otherwise.
Barry Warsawb2d5e622002-04-22 15:29:27 +00003393 (save-excursion (setq pps (parse-partial-sexp (point) here)))
3394 ;; make sure we don't land inside a triple-quoted string
3395 (setq done (or (not (nth 3 pps))
3396 (bobp)))
3397 ;; Just go ahead and short circuit the test back to the
3398 ;; beginning of the buffer. This will be slow, but not
3399 ;; nearly as slow as looping through many
3400 ;; re-search-backwards.
3401 (if (not done)
3402 (goto-char (point-min))))
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00003403 pps)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00003404
Barry Warsaw7ae77681994-12-12 20:38:05 +00003405(defun py-nesting-level ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003406 "Return the buffer position of the last unclosed enclosing list.
3407If nesting level is zero, return nil."
Barry Warsawf64b4051998-02-12 16:52:14 +00003408 (let ((status (py-parse-state)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00003409 (if (zerop (car status))
3410 nil ; not in a nest
3411 (car (cdr status))))) ; char# of open bracket
3412
Barry Warsaw7ae77681994-12-12 20:38:05 +00003413(defun py-backslash-continuation-line-p ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003414 "Return t iff preceding line ends with backslash that is not in a comment."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003415 (save-excursion
3416 (beginning-of-line)
3417 (and
3418 ;; use a cheap test first to avoid the regexp if possible
3419 ;; use 'eq' because char-after may return nil
3420 (eq (char-after (- (point) 2)) ?\\ )
3421 ;; make sure; since eq test passed, there is a preceding line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003422 (forward-line -1) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00003423 (looking-at py-continued-re))))
3424
Barry Warsaw7ae77681994-12-12 20:38:05 +00003425(defun py-continuation-line-p ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003426 "Return t iff current line is a continuation line."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003427 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003428 (beginning-of-line)
Barry Warsaw7ae77681994-12-12 20:38:05 +00003429 (or (py-backslash-continuation-line-p)
3430 (py-nesting-level))))
3431
Barry Warsaw9c1696c1998-12-15 04:36:22 +00003432(defun py-goto-beginning-of-tqs (delim)
3433 "Go to the beginning of the triple quoted string we find ourselves in.
3434DELIM is the TQS string delimiter character we're searching backwards
3435for."
Barry Warsaw3c34bb32000-10-27 05:00:25 +00003436 (let ((skip (and delim (make-string 1 delim)))
3437 (continue t))
Barry Warsaw9c1696c1998-12-15 04:36:22 +00003438 (when skip
3439 (save-excursion
Barry Warsaw3c34bb32000-10-27 05:00:25 +00003440 (while continue
3441 (py-safe (search-backward skip))
3442 (setq continue (and (not (bobp))
3443 (= (char-before) ?\\))))
3444 (if (and (= (char-before) delim)
3445 (= (char-before (1- (point))) delim))
Barry Warsaw9c1696c1998-12-15 04:36:22 +00003446 (setq skip (make-string 3 delim))))
3447 ;; we're looking at a triple-quoted string
3448 (py-safe (search-backward skip)))))
3449
Barry Warsaw7ae77681994-12-12 20:38:05 +00003450(defun py-goto-initial-line ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003451 "Go to the initial line of the current statement.
3452Usually this is the line we're on, but if we're on the 2nd or
3453following lines of a continuation block, we need to go up to the first
3454line of the block."
3455 ;; Tricky: We want to avoid quadratic-time behavior for long
3456 ;; continued blocks, whether of the backslash or open-bracket
3457 ;; varieties, or a mix of the two. The following manages to do that
3458 ;; in the usual cases.
3459 ;;
3460 ;; Also, if we're sitting inside a triple quoted string, this will
3461 ;; drop us at the line that begins the string.
Barry Warsaw9ec9fbc1998-01-21 05:15:57 +00003462 (let (open-bracket-pos)
Barry Warsaw7ae77681994-12-12 20:38:05 +00003463 (while (py-continuation-line-p)
3464 (beginning-of-line)
3465 (if (py-backslash-continuation-line-p)
3466 (while (py-backslash-continuation-line-p)
3467 (forward-line -1))
3468 ;; else zip out of nested brackets/braces/parens
3469 (while (setq open-bracket-pos (py-nesting-level))
3470 (goto-char open-bracket-pos)))))
3471 (beginning-of-line))
3472
Barry Warsaw7ae77681994-12-12 20:38:05 +00003473(defun py-goto-beyond-final-line ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003474 "Go to the point just beyond the fine line of the current statement.
3475Usually this is the start of the next line, but if this is a
3476multi-line statement we need to skip over the continuation lines."
3477 ;; Tricky: Again we need to be clever to avoid quadratic time
3478 ;; behavior.
3479 ;;
3480 ;; XXX: Not quite the right solution, but deals with multi-line doc
3481 ;; strings
Barry Warsaw751f4931998-05-19 16:06:21 +00003482 (if (looking-at (concat "[ \t]*\\(" py-stringlit-re "\\)"))
3483 (goto-char (match-end 0)))
3484 ;;
Barry Warsaw7ae77681994-12-12 20:38:05 +00003485 (forward-line 1)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003486 (let (state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00003487 (while (and (py-continuation-line-p)
3488 (not (eobp)))
3489 ;; skip over the backslash flavor
3490 (while (and (py-backslash-continuation-line-p)
3491 (not (eobp)))
3492 (forward-line 1))
3493 ;; if in nest, zip to the end of the nest
3494 (setq state (py-parse-state))
3495 (if (and (not (zerop (car state)))
3496 (not (eobp)))
3497 (progn
Barry Warsawaffc0ca1997-11-03 16:59:38 +00003498 (parse-partial-sexp (point) (point-max) 0 nil state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00003499 (forward-line 1))))))
3500
Barry Warsaw7ae77681994-12-12 20:38:05 +00003501(defun py-statement-opens-block-p ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003502 "Return t iff the current statement opens a block.
Skip Montanarodc6d9e12008-02-05 02:32:16 +00003503I.e., iff it ends with a colon that is not in a comment. Point should
Barry Warsaw41a05c71998-08-10 16:33:12 +00003504be at the start of a statement."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003505 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003506 (let ((start (point))
3507 (finish (progn (py-goto-beyond-final-line) (1- (point))))
3508 (searching t)
3509 (answer nil)
3510 state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00003511 (goto-char start)
3512 (while searching
3513 ;; look for a colon with nothing after it except whitespace, and
3514 ;; maybe a comment
3515 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
3516 finish t)
3517 (if (eq (point) finish) ; note: no `else' clause; just
3518 ; keep searching if we're not at
3519 ; the end yet
3520 ;; sure looks like it opens a block -- but it might
3521 ;; be in a comment
3522 (progn
3523 (setq searching nil) ; search is done either way
3524 (setq state (parse-partial-sexp start
3525 (match-beginning 0)))
3526 (setq answer (not (nth 4 state)))))
3527 ;; search failed: couldn't find another interesting colon
3528 (setq searching nil)))
3529 answer)))
3530
Barry Warsawf831d811996-07-31 20:42:59 +00003531(defun py-statement-closes-block-p ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003532 "Return t iff the current statement closes a block.
3533I.e., if the line starts with `return', `raise', `break', `continue',
3534and `pass'. This doesn't catch embedded statements."
Barry Warsawf831d811996-07-31 20:42:59 +00003535 (let ((here (point)))
Barry Warsawa8f99ba1999-05-24 18:37:57 +00003536 (py-goto-initial-line)
Barry Warsawc0d2d511999-06-03 22:18:59 +00003537 (back-to-indentation)
Barry Warsawf831d811996-07-31 20:42:59 +00003538 (prog1
Barry Warsawaffc0ca1997-11-03 16:59:38 +00003539 (looking-at (concat py-block-closing-keywords-re "\\>"))
Barry Warsawf831d811996-07-31 20:42:59 +00003540 (goto-char here))))
3541
Barry Warsaw7ae77681994-12-12 20:38:05 +00003542(defun py-goto-beyond-block ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003543 "Go to point just beyond the final line of block begun by the current line.
3544This is the same as where `py-goto-beyond-final-line' goes unless
3545we're on colon line, in which case we go to the end of the block.
3546Assumes point is at the beginning of the line."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003547 (if (py-statement-opens-block-p)
3548 (py-mark-block nil 'just-move)
3549 (py-goto-beyond-final-line)))
3550
Barry Warsaw7ae77681994-12-12 20:38:05 +00003551(defun py-goto-statement-at-or-above ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003552 "Go to the start of the first statement at or preceding point.
3553Return t if there is such a statement, otherwise nil. `Statement'
3554does not include blank lines, comments, or continuation lines."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003555 (py-goto-initial-line)
3556 (if (looking-at py-blank-or-comment-re)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003557 ;; skip back over blank & comment lines
3558 ;; note: will skip a blank or comment line that happens to be
3559 ;; a continuation line too
3560 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
3561 (progn (py-goto-initial-line) t)
3562 nil)
Barry Warsaw7ae77681994-12-12 20:38:05 +00003563 t))
3564
Barry Warsaw7ae77681994-12-12 20:38:05 +00003565(defun py-goto-statement-below ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003566 "Go to start of the first statement following the statement containing point.
3567Return t if there is such a statement, otherwise nil. `Statement'
3568does not include blank lines, comments, or continuation lines."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003569 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003570 (let ((start (point)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00003571 (py-goto-beyond-final-line)
3572 (while (and
Barry Warsawf070cce2002-05-23 19:42:16 +00003573 (or (looking-at py-blank-or-comment-re)
3574 (py-in-literal))
Barry Warsaw7ae77681994-12-12 20:38:05 +00003575 (not (eobp)))
3576 (forward-line 1))
3577 (if (eobp)
3578 (progn (goto-char start) nil)
3579 t)))
3580
Barry Warsaw7ae77681994-12-12 20:38:05 +00003581(defun py-go-up-tree-to-keyword (key)
Barry Warsaw41a05c71998-08-10 16:33:12 +00003582 "Go to begining of statement starting with KEY, at or preceding point.
3583
3584KEY is a regular expression describing a Python keyword. Skip blank
3585lines and non-indenting comments. If the statement found starts with
3586KEY, then stop, otherwise go back to first enclosing block starting
Skip Montanarodc6d9e12008-02-05 02:32:16 +00003587with KEY. If successful, leave point at the start of the KEY line and
3588return t. Otherwise, leave point at an undefined place and return nil."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003589 ;; skip blanks and non-indenting #
3590 (py-goto-initial-line)
3591 (while (and
3592 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
3593 (zerop (forward-line -1))) ; go back
3594 nil)
3595 (py-goto-initial-line)
Skip Montanarodc6d9e12008-02-05 02:32:16 +00003596 (let* ((re (concat "[ \t]*" key "\\>"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003597 (case-fold-search nil) ; let* so looking-at sees this
3598 (found (looking-at re))
3599 (dead nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00003600 (while (not (or found dead))
3601 (condition-case nil ; in case no enclosing block
3602 (py-goto-block-up 'no-mark)
3603 (error (setq dead t)))
3604 (or dead (setq found (looking-at re))))
3605 (beginning-of-line)
3606 found))
3607
Barry Warsaw7ae77681994-12-12 20:38:05 +00003608(defun py-suck-up-leading-text ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003609 "Return string in buffer from start of indentation to end of line.
3610Prefix with \"...\" if leading whitespace was skipped."
Barry Warsaw7ae77681994-12-12 20:38:05 +00003611 (save-excursion
3612 (back-to-indentation)
3613 (concat
3614 (if (bolp) "" "...")
3615 (buffer-substring (point) (progn (end-of-line) (point))))))
3616
Barry Warsaw7ae77681994-12-12 20:38:05 +00003617(defun py-suck-up-first-keyword ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003618 "Return first keyword on the line as a Lisp symbol.
3619`Keyword' is defined (essentially) as the regular expression
3620([a-z]+). Returns nil if none was found."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003621 (let ((case-fold-search nil))
Skip Montanarodc6d9e12008-02-05 02:32:16 +00003622 (if (looking-at "[ \t]*\\([a-z]+\\)\\>")
Barry Warsaw7ae77681994-12-12 20:38:05 +00003623 (intern (buffer-substring (match-beginning 1) (match-end 1)))
3624 nil)))
3625
Barry Warsawb3e81d51996-09-04 15:12:42 +00003626(defun py-current-defun ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003627 "Python value for `add-log-current-defun-function'.
3628This tells add-log.el how to find the current function/method/variable."
Barry Warsawb3e81d51996-09-04 15:12:42 +00003629 (save-excursion
Skip Montanarodc6d9e12008-02-05 02:32:16 +00003630
3631 ;; Move back to start of the current statement.
3632
3633 (py-goto-initial-line)
3634 (back-to-indentation)
3635 (while (and (or (looking-at py-blank-or-comment-re)
3636 (py-in-literal))
3637 (not (bobp)))
3638 (backward-to-indentation 1))
3639 (py-goto-initial-line)
3640
3641 (let ((scopes "")
3642 (sep "")
3643 dead assignment)
3644
3645 ;; Check for an assignment. If this assignment exists inside a
3646 ;; def, it will be overwritten inside the while loop. If it
3647 ;; exists at top lever or inside a class, it will be preserved.
3648
3649 (when (looking-at "[ \t]*\\([a-zA-Z0-9_]+\\)[ \t]*=")
3650 (setq scopes (buffer-substring (match-beginning 1) (match-end 1)))
3651 (setq assignment t)
3652 (setq sep "."))
3653
3654 ;; Prepend the name of each outer socpe (def or class).
3655
3656 (while (not dead)
3657 (if (and (py-go-up-tree-to-keyword "\\(class\\|def\\)")
3658 (looking-at
3659 "[ \t]*\\(class\\|def\\)[ \t]*\\([a-zA-Z0-9_]+\\)[ \t]*"))
3660 (let ((name (buffer-substring (match-beginning 2) (match-end 2))))
3661 (if (and assignment (looking-at "[ \t]*def"))
3662 (setq scopes name)
3663 (setq scopes (concat name sep scopes))
3664 (setq sep "."))))
3665 (setq assignment nil)
3666 (condition-case nil ; Terminate nicely at top level.
3667 (py-goto-block-up 'no-mark)
3668 (error (setq dead t))))
3669 (if (string= scopes "")
3670 nil
3671 scopes))))
3672
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003673
3674
Barry Warsawfec75d61995-07-05 23:26:15 +00003675(defconst py-help-address "python-mode@python.org"
Barry Warsaw850437a1995-03-08 21:50:28 +00003676 "Address accepting submission of bug reports.")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003677
Barry Warsaw850437a1995-03-08 21:50:28 +00003678(defun py-version ()
3679 "Echo the current version of `python-mode' in the minibuffer."
3680 (interactive)
3681 (message "Using `python-mode' version %s" py-version)
3682 (py-keep-region-active))
3683
3684;; only works under Emacs 19
3685;(eval-when-compile
3686; (require 'reporter))
3687
3688(defun py-submit-bug-report (enhancement-p)
3689 "Submit via mail a bug report on `python-mode'.
Barry Warsaw41a05c71998-08-10 16:33:12 +00003690With \\[universal-argument] (programmatically, argument ENHANCEMENT-P
3691non-nil) just submit an enhancement request."
Barry Warsaw850437a1995-03-08 21:50:28 +00003692 (interactive
3693 (list (not (y-or-n-p
Barry Warsaw41a05c71998-08-10 16:33:12 +00003694 "Is this a bug report (hit `n' to send other comments)? "))))
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00003695 (let ((reporter-prompt-for-summary-p (if enhancement-p
3696 "(Very) brief summary: "
3697 t)))
Barry Warsaw850437a1995-03-08 21:50:28 +00003698 (require 'reporter)
3699 (reporter-submit-bug-report
3700 py-help-address ;address
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00003701 (concat "python-mode " py-version) ;pkgname
Barry Warsaw850437a1995-03-08 21:50:28 +00003702 ;; varlist
3703 (if enhancement-p nil
3704 '(py-python-command
3705 py-indent-offset
3706 py-block-comment-prefix
Barry Warsaw850437a1995-03-08 21:50:28 +00003707 py-temp-directory
3708 py-beep-if-tab-change))
3709 nil ;pre-hooks
3710 nil ;post-hooks
3711 "Dear Barry,") ;salutation
3712 (if enhancement-p nil
3713 (set-mark (point))
Skip Montanarodc6d9e12008-02-05 02:32:16 +00003714 (insert
Barry Warsaw850437a1995-03-08 21:50:28 +00003715"Please replace this text with a sufficiently large code sample\n\
3716and an exact recipe so that I can reproduce your problem. Failure\n\
3717to do so may mean a greater delay in fixing your bug.\n\n")
3718 (exchange-point-and-mark)
3719 (py-keep-region-active))))
3720
3721
Barry Warsaw47384781997-11-26 05:27:45 +00003722(defun py-kill-emacs-hook ()
Barry Warsaw41a05c71998-08-10 16:33:12 +00003723 "Delete files in `py-file-queue'.
3724These are Python temporary files awaiting execution."
Barry Warsaw47384781997-11-26 05:27:45 +00003725 (mapcar #'(lambda (filename)
3726 (py-safe (delete-file filename)))
3727 py-file-queue))
3728
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003729;; arrange to kill temp files when Emacs exists
Barry Warsawc72c11c1997-08-09 06:42:08 +00003730(add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
Barry Warsaw4f577d22001-04-11 20:23:17 +00003731(add-hook 'comint-output-filter-functions 'py-pdbtrack-track-stack-file)
3732
3733;; Add a designator to the minor mode strings
Skip Montanarodc6d9e12008-02-05 02:32:16 +00003734(or (assq 'py-pdbtrack-is-tracking-p minor-mode-alist)
Barry Warsaw4f577d22001-04-11 20:23:17 +00003735 (push '(py-pdbtrack-is-tracking-p py-pdbtrack-minor-mode-string)
3736 minor-mode-alist))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003737
3738
3739
Skip Montanaro6c5bc342002-12-31 16:56:20 +00003740;;; paragraph and string filling code from Bernhard Herzog
3741;;; see http://mail.python.org/pipermail/python-list/2002-May/103189.html
3742
3743(defun py-fill-comment (&optional justify)
3744 "Fill the comment paragraph around point"
3745 (let (;; Non-nil if the current line contains a comment.
3746 has-comment
3747
3748 ;; If has-comment, the appropriate fill-prefix for the comment.
3749 comment-fill-prefix)
3750
3751 ;; Figure out what kind of comment we are looking at.
3752 (save-excursion
3753 (beginning-of-line)
3754 (cond
3755 ;; A line with nothing but a comment on it?
3756 ((looking-at "[ \t]*#[# \t]*")
3757 (setq has-comment t
3758 comment-fill-prefix (buffer-substring (match-beginning 0)
3759 (match-end 0))))
3760
3761 ;; A line with some code, followed by a comment? Remember that the hash
3762 ;; which starts the comment shouldn't be part of a string or character.
3763 ((progn
3764 (while (not (looking-at "#\\|$"))
3765 (skip-chars-forward "^#\n\"'\\")
3766 (cond
3767 ((eq (char-after (point)) ?\\) (forward-char 2))
3768 ((memq (char-after (point)) '(?\" ?')) (forward-sexp 1))))
3769 (looking-at "#+[\t ]*"))
3770 (setq has-comment t)
3771 (setq comment-fill-prefix
3772 (concat (make-string (current-column) ? )
3773 (buffer-substring (match-beginning 0) (match-end 0)))))))
3774
3775 (if (not has-comment)
3776 (fill-paragraph justify)
3777
3778 ;; Narrow to include only the comment, and then fill the region.
3779 (save-restriction
3780 (narrow-to-region
3781
3782 ;; Find the first line we should include in the region to fill.
3783 (save-excursion
3784 (while (and (zerop (forward-line -1))
3785 (looking-at "^[ \t]*#")))
3786
3787 ;; We may have gone to far. Go forward again.
3788 (or (looking-at "^[ \t]*#")
3789 (forward-line 1))
3790 (point))
3791
3792 ;; Find the beginning of the first line past the region to fill.
3793 (save-excursion
3794 (while (progn (forward-line 1)
3795 (looking-at "^[ \t]*#")))
3796 (point)))
3797
3798 ;; Lines with only hashes on them can be paragraph boundaries.
3799 (let ((paragraph-start (concat paragraph-start "\\|[ \t#]*$"))
3800 (paragraph-separate (concat paragraph-separate "\\|[ \t#]*$"))
3801 (fill-prefix comment-fill-prefix))
3802 ;;(message "paragraph-start %S paragraph-separate %S"
3803 ;;paragraph-start paragraph-separate)
3804 (fill-paragraph justify))))
3805 t))
3806
3807
3808(defun py-fill-string (start &optional justify)
3809 "Fill the paragraph around (point) in the string starting at start"
3810 ;; basic strategy: narrow to the string and call the default
3811 ;; implementation
3812 (let (;; the start of the string's contents
3813 string-start
3814 ;; the end of the string's contents
3815 string-end
3816 ;; length of the string's delimiter
3817 delim-length
3818 ;; The string delimiter
3819 delim
3820 )
3821
3822 (save-excursion
3823 (goto-char start)
3824 (if (looking-at "\\('''\\|\"\"\"\\|'\\|\"\\)\\\\?\n?")
3825 (setq string-start (match-end 0)
3826 delim-length (- (match-end 1) (match-beginning 1))
3827 delim (buffer-substring-no-properties (match-beginning 1)
3828 (match-end 1)))
3829 (error "The parameter start is not the beginning of a python string"))
3830
3831 ;; if the string is the first token on a line and doesn't start with
3832 ;; a newline, fill as if the string starts at the beginning of the
3833 ;; line. this helps with one line docstrings
3834 (save-excursion
3835 (beginning-of-line)
3836 (and (/= (char-before string-start) ?\n)
3837 (looking-at (concat "[ \t]*" delim))
3838 (setq string-start (point))))
3839
3840 (forward-sexp (if (= delim-length 3) 2 1))
3841
3842 ;; with both triple quoted strings and single/double quoted strings
3843 ;; we're now directly behind the first char of the end delimiter
3844 ;; (this doesn't work correctly when the triple quoted string
3845 ;; contains the quote mark itself). The end of the string's contents
3846 ;; is one less than point
3847 (setq string-end (1- (point))))
3848
3849 ;; Narrow to the string's contents and fill the current paragraph
3850 (save-restriction
3851 (narrow-to-region string-start string-end)
3852 (let ((ends-with-newline (= (char-before (point-max)) ?\n)))
3853 (fill-paragraph justify)
3854 (if (and (not ends-with-newline)
3855 (= (char-before (point-max)) ?\n))
3856 ;; the default fill-paragraph implementation has inserted a
3857 ;; newline at the end. Remove it again.
3858 (save-excursion
3859 (goto-char (point-max))
3860 (delete-char -1)))))
3861
3862 ;; return t to indicate that we've done our work
3863 t))
3864
3865(defun py-fill-paragraph (&optional justify)
3866 "Like \\[fill-paragraph], but handle Python comments and strings.
3867If any of the current line is a comment, fill the comment or the
3868paragraph of it that point is in, preserving the comment's indentation
3869and initial `#'s.
3870If point is inside a string, narrow to that string and fill.
3871"
3872 (interactive "P")
Skip Montanarodc6d9e12008-02-05 02:32:16 +00003873 ;; fill-paragraph will narrow incorrectly
3874 (save-restriction
3875 (widen)
3876 (let* ((bod (py-point 'bod))
3877 (pps (parse-partial-sexp bod (point))))
3878 (cond
3879 ;; are we inside a comment or on a line with only whitespace before
3880 ;; the comment start?
3881 ((or (nth 4 pps)
3882 (save-excursion (beginning-of-line) (looking-at "[ \t]*#")))
3883 (py-fill-comment justify))
3884 ;; are we inside a string?
3885 ((nth 3 pps)
3886 (py-fill-string (nth 8 pps)))
3887 ;; are we at the opening quote of a string, or in the indentation?
3888 ((save-excursion
3889 (forward-word 1)
3890 (eq (py-in-literal) 'string))
3891 (save-excursion
3892 (py-fill-string (py-point 'boi))))
3893 ;; are we at or after the closing quote of a string?
3894 ((save-excursion
3895 (backward-word 1)
3896 (eq (py-in-literal) 'string))
3897 (save-excursion
3898 (py-fill-string (py-point 'boi))))
3899 ;; otherwise use the default
3900 (t
3901 (fill-paragraph justify))))))
Skip Montanaro6c5bc342002-12-31 16:56:20 +00003902
3903
3904
Barry Warsaw7b0f5681995-03-08 21:33:04 +00003905(provide 'python-mode)
3906;;; python-mode.el ends here