Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1 | ;;; python-mode.el --- Major mode for editing Python programs |
| 2 | |
| 3 | ;; Copyright (C) 1992,1993,1994 Tim Peters |
| 4 | |
Barry Warsaw | f64b405 | 1998-02-12 16:52:14 +0000 | [diff] [blame] | 5 | ;; Author: 1995-1998 Barry A. Warsaw |
Barry Warsaw | fec75d6 | 1995-07-05 23:26:15 +0000 | [diff] [blame] | 6 | ;; 1992-1994 Tim Peters |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 7 | ;; Maintainer: python-mode@python.org |
| 8 | ;; Created: Feb 1992 |
| 9 | ;; Keywords: python languages oop |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 10 | |
Barry Warsaw | 5204b4a | 1998-04-02 19:27:18 +0000 | [diff] [blame] | 11 | (defconst py-version "$Revision$" |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 12 | "`python-mode' version number.") |
| 13 | |
Barry Warsaw | cfec359 | 1995-03-10 15:58:16 +0000 | [diff] [blame] | 14 | ;; This software is provided as-is, without express or implied |
| 15 | ;; warranty. Permission to use, copy, modify, distribute or sell this |
| 16 | ;; software, without fee, for any purpose and by any individual or |
| 17 | ;; organization, is hereby granted, provided that the above copyright |
| 18 | ;; notice and this paragraph appear in all copies. |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 19 | |
| 20 | ;;; Commentary: |
Barry Warsaw | 755c671 | 1996-08-01 20:02:55 +0000 | [diff] [blame] | 21 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 22 | ;; This is a major mode for editing Python programs. It was developed |
Barry Warsaw | 261f87d | 1996-08-20 19:57:34 +0000 | [diff] [blame] | 23 | ;; by Tim Peters after an original idea by Michael A. Guravage. Tim |
| 24 | ;; subsequently left the net; in 1995, Barry Warsaw inherited the |
| 25 | ;; mode and is the current maintainer. |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 26 | |
Barry Warsaw | 3723152 | 1997-12-11 17:23:13 +0000 | [diff] [blame] | 27 | ;; COMPATIBILITY: |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 28 | |
Barry Warsaw | 3723152 | 1997-12-11 17:23:13 +0000 | [diff] [blame] | 29 | ;; This version of python-mode.el is no longer compatible with Emacs |
| 30 | ;; 18. For a gabazillion reasons, I highly recommend upgrading to |
| 31 | ;; X/Emacs 19 or X/Emacs 20. I recommend at least Emacs 19.34 or |
| 32 | ;; XEmacs 19.15. Any of the v20 X/Emacsen should be fine. |
Barry Warsaw | 673d05f | 1997-12-02 21:51:57 +0000 | [diff] [blame] | 33 | |
Barry Warsaw | 3723152 | 1997-12-11 17:23:13 +0000 | [diff] [blame] | 34 | ;; NOTE TO FSF EMACS USERS: |
| 35 | |
| 36 | ;; You may need to acquire the Custom library -- this applies to users |
| 37 | ;; of Emacs 19.34 and NTEmacs based on 19.34, but not to Emacs 20 |
| 38 | ;; users. You must also byte-compile this file before use -- this |
| 39 | ;; applies to FSF's Emacs 19.34, 20.x, and NTEmacs based on 19.34. |
| 40 | ;; None of this applies to XEmacs (although byte compilation is still |
| 41 | ;; recommended). You will also need to add the following to your |
| 42 | ;; .emacs file so that the .py files come up in python-mode: |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 43 | ;; |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 44 | ;; (autoload 'python-mode "python-mode" "Python editing mode." t) |
| 45 | ;; (setq auto-mode-alist |
| 46 | ;; (cons '("\\.py$" . python-mode) auto-mode-alist)) |
| 47 | ;; (setq interpreter-mode-alist |
| 48 | ;; (cons '("python" . python-mode) interpreter-mode-alist)) |
Barry Warsaw | 44b7220 | 1996-07-05 20:11:35 +0000 | [diff] [blame] | 49 | ;; |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 50 | ;; Assuming python-mode.el is on your load-path, it will be invoked |
| 51 | ;; when you visit a .py file, or a file with a first line that looks |
| 52 | ;; like: |
| 53 | ;; |
| 54 | ;; #! /usr/bin/env python |
| 55 | |
Barry Warsaw | 3723152 | 1997-12-11 17:23:13 +0000 | [diff] [blame] | 56 | ;; NOTE TO XEMACS USERS: |
| 57 | |
| 58 | ;; An older version of this file was distributed with XEmacs 19.15, |
| 59 | ;; 19.16 and 20.3. By default, in XEmacs when you visit a .py file, |
| 60 | ;; the buffer is put in Python mode. Likewise for executable scripts |
| 61 | ;; with the word `python' on the first line. You shouldn't need to do |
| 62 | ;; much except make sure this new version is earlier in your |
| 63 | ;; load-path, and byte-compile this file. |
| 64 | |
| 65 | ;; FOR MORE INFORMATION: |
| 66 | |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 67 | ;; Please see <http://www.python.org/emacs/python-mode/> for the |
Barry Warsaw | 3723152 | 1997-12-11 17:23:13 +0000 | [diff] [blame] | 68 | ;; latest information and compatibility notes. |
| 69 | |
| 70 | ;; BUG REPORTING: |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 71 | |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 72 | ;; To submit bug reports, use C-c C-b. Please include a complete, but |
| 73 | ;; concise code sample and a recipe for reproducing the bug. Send |
| 74 | ;; suggestions and other comments to python-mode@python.org. |
| 75 | |
| 76 | ;; When in a Python mode buffer, do a C-h m for more help. It's |
Barry Warsaw | 3723152 | 1997-12-11 17:23:13 +0000 | [diff] [blame] | 77 | ;; doubtful that a texinfo manual would be very useful, but if you |
| 78 | ;; want to contribute one, I'll certainly accept it! |
Barry Warsaw | b5e0ecb | 1995-03-14 18:32:54 +0000 | [diff] [blame] | 79 | |
Barry Warsaw | 5ea20d5 | 1997-12-06 00:00:47 +0000 | [diff] [blame] | 80 | ;; If you are using XEmacs, you may also want to check out OO-Browser |
| 81 | ;; that comes bundled with it, including documentation in the info |
| 82 | ;; pages. For GNU Emacs you have to install it yourself. To read |
| 83 | ;; more about OO-Browser, follow these links: |
| 84 | |
| 85 | ;; http://www.python.org/workshops/1996-06/papers/h.pasanen/oobr_contents.html |
| 86 | ;; http://www.infodock.com/manuals/alt-oobr-cover.html |
| 87 | |
Barry Warsaw | 3723152 | 1997-12-11 17:23:13 +0000 | [diff] [blame] | 88 | ;; You may also want to take a look at Harri Pasanen's "Python Library |
| 89 | ;; Reference Hot-Key Help System for XEmacs (or PLRHKHSX for short ;), |
| 90 | ;; version 1.0" |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 91 | ;; |
Barry Warsaw | 3723152 | 1997-12-11 17:23:13 +0000 | [diff] [blame] | 92 | ;; <http://www.iki.fi/hpa/> |
| 93 | |
| 94 | ;; TO DO LIST: |
| 95 | |
| 96 | ;; - Better integration with pdb.py and gud-mode for debugging. |
Barry Warsaw | b5e0ecb | 1995-03-14 18:32:54 +0000 | [diff] [blame] | 97 | ;; - Rewrite according to GNU Emacs Lisp standards. |
Barry Warsaw | 5c0d00f | 1996-07-31 21:30:21 +0000 | [diff] [blame] | 98 | ;; - possibly force indent-tabs-mode == nil, and add a |
| 99 | ;; write-file-hooks that runs untabify on the whole buffer (to work |
| 100 | ;; around potential tab/space mismatch problems). In practice this |
| 101 | ;; hasn't been a problem... yet. |
Barry Warsaw | 9e277db | 1996-07-31 22:33:40 +0000 | [diff] [blame] | 102 | ;; - have py-execute-region on indented code act as if the region is |
Barry Warsaw | 3723152 | 1997-12-11 17:23:13 +0000 | [diff] [blame] | 103 | ;; left justified. Avoids syntax errors. |
| 104 | ;; - add a py-goto-block-down, bound to C-c C-d |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 105 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 106 | ;;; Code: |
| 107 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 108 | (require 'custom) |
Barry Warsaw | 8529ebb | 1997-12-01 20:03:12 +0000 | [diff] [blame] | 109 | (eval-when-compile |
Barry Warsaw | 673d05f | 1997-12-02 21:51:57 +0000 | [diff] [blame] | 110 | (require 'cl) |
Barry Warsaw | b6c1f1f | 1998-03-19 22:33:06 +0000 | [diff] [blame] | 111 | (if (not (and (condition-case nil |
| 112 | (require 'custom) |
| 113 | (error nil)) |
| 114 | ;; Stock Emacs 19.34 has a broken/old Custom library |
| 115 | ;; that does more harm than good. Fortunately, it is |
| 116 | ;; missing defcustom |
| 117 | (fboundp 'defcustom))) |
Barry Warsaw | 673d05f | 1997-12-02 21:51:57 +0000 | [diff] [blame] | 118 | (error "STOP! STOP! STOP! STOP! |
| 119 | |
| 120 | The Custom library was not found or is out of date. A more current |
| 121 | version is required. Please download and install the latest version |
| 122 | of the Custom library from: |
| 123 | |
| 124 | <http://www.dina.kvl.dk/~abraham/custom/> |
| 125 | |
| 126 | See the Python Mode home page for details: |
| 127 | |
| 128 | <http://www.python.org/ftp/emacs/> |
| 129 | "))) |
| 130 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 131 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 132 | |
| 133 | ;; user definable variables |
| 134 | ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 135 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 136 | (defgroup python nil |
| 137 | "Support for the Python programming language, <http://www.python.org/>" |
| 138 | :group 'languages) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 139 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 140 | (defcustom py-python-command "python" |
| 141 | "*Shell command used to start Python interpreter." |
| 142 | :type 'string |
| 143 | :group 'python) |
| 144 | |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 145 | (defcustom py-jpython-command "jpython" |
| 146 | "*Shell command used to start the JPython interpreter." |
| 147 | :type 'string |
| 148 | :group 'python) |
| 149 | |
Barry Warsaw | 8f972b7 | 1998-02-05 20:45:49 +0000 | [diff] [blame] | 150 | (defcustom py-python-command-args '("-i") |
| 151 | "*List of string arguments to be used when starting a Python shell." |
| 152 | :type '(repeat string) |
| 153 | :group 'python) |
| 154 | |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 155 | (defcustom py-jpython-command-args '("-i") |
| 156 | "*List of string arguments to be used when starting a JPython shell." |
| 157 | :type '(repeat string) |
| 158 | :group 'python) |
| 159 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 160 | (defcustom py-indent-offset 4 |
| 161 | "*Amount of offset per level of indentation |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 162 | Note that `\\[py-guess-indent-offset]' can usually guess a good value |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 163 | when you're editing someone else's Python code." |
| 164 | :type 'integer |
| 165 | :group 'python) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 166 | |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 167 | (defcustom py-smart-indentation t |
| 168 | "*Should `python-mode' try to automagically set some indentation variables? |
| 169 | When this variable is non-nil, two things happen when a buffer is set |
| 170 | to `python-mode': |
| 171 | |
| 172 | 1. `py-indent-offset' is guess from existing code in the buffer. |
Barry Warsaw | 639eea6 | 1998-03-16 18:12:13 +0000 | [diff] [blame] | 173 | Only guessed values between 2 and 8 are considered. If a valid |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 174 | guess can't be made (perhaps because you are visiting a new |
Barry Warsaw | 639eea6 | 1998-03-16 18:12:13 +0000 | [diff] [blame] | 175 | file), then the value in `py-indent-offset' is used. |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 176 | |
Barry Warsaw | 639eea6 | 1998-03-16 18:12:13 +0000 | [diff] [blame] | 177 | 2. `indent-tabs-mode' is turned off if `py-indent-offset' does not |
| 178 | equal `tab-width' (`indent-tabs-mode' is never turned on by |
| 179 | Python mode). This means that for newly written code, tabs are |
| 180 | only inserted in indentation if one tab is one indentation |
| 181 | level, otherwise only spaces are used. |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 182 | |
Barry Warsaw | 8046bef | 1998-03-13 20:04:52 +0000 | [diff] [blame] | 183 | Note that both these settings occur *after* `python-mode-hook' is run, |
| 184 | so if you want to defeat the automagic configuration, you must also |
| 185 | set `py-smart-indentation' to nil in your `python-mode-hook'." |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 186 | :type 'boolean |
| 187 | :group 'python) |
| 188 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 189 | (defcustom py-align-multiline-strings-p t |
| 190 | "*Flag describing how multi-line triple quoted strings are aligned. |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 191 | When this flag is non-nil, continuation lines are lined up under the |
| 192 | preceding line's indentation. When this flag is nil, continuation |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 193 | lines are aligned to column zero." |
| 194 | :type '(choice (const :tag "Align under preceding line" t) |
| 195 | (const :tag "Align to column zero" nil)) |
| 196 | :group 'python) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 197 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 198 | (defcustom py-block-comment-prefix "## " |
Barry Warsaw | 867a32a | 1996-03-07 18:30:26 +0000 | [diff] [blame] | 199 | "*String used by \\[comment-region] to comment out a block of code. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 200 | This should follow the convention for non-indenting comment lines so |
| 201 | that the indentation commands won't get confused (i.e., the string |
| 202 | should be of the form `#x...' where `x' is not a blank or a tab, and |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 203 | `...' is arbitrary)." |
| 204 | :type 'string |
| 205 | :group 'python) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 206 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 207 | (defcustom py-honor-comment-indentation t |
Barry Warsaw | 6d62775 | 1996-03-06 18:41:38 +0000 | [diff] [blame] | 208 | "*Controls how comment lines influence subsequent indentation. |
Barry Warsaw | 33d6ec0 | 1996-03-05 16:28:07 +0000 | [diff] [blame] | 209 | |
Barry Warsaw | 6d62775 | 1996-03-06 18:41:38 +0000 | [diff] [blame] | 210 | When nil, all comment lines are skipped for indentation purposes, and |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 211 | if possible, a faster algorithm is used (i.e. X/Emacs 19 and beyond). |
Barry Warsaw | 6d62775 | 1996-03-06 18:41:38 +0000 | [diff] [blame] | 212 | |
| 213 | When t, lines that begin with a single `#' are a hint to subsequent |
| 214 | line indentation. If the previous line is such a comment line (as |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 215 | opposed to one that starts with `py-block-comment-prefix'), then its |
Barry Warsaw | 6d62775 | 1996-03-06 18:41:38 +0000 | [diff] [blame] | 216 | indentation is used as a hint for this line's indentation. Lines that |
| 217 | begin with `py-block-comment-prefix' are ignored for indentation |
| 218 | purposes. |
| 219 | |
| 220 | When not nil or t, comment lines that begin with a `#' are used as |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 221 | indentation hints, unless the comment character is in column zero." |
| 222 | :type '(choice |
| 223 | (const :tag "Skip all comment lines (fast)" nil) |
| 224 | (const :tag "Single # `sets' indentation for next line" t) |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 225 | (const :tag "Single # `sets' indentation except at column zero" |
| 226 | other) |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 227 | ) |
| 228 | :group 'python) |
Barry Warsaw | 33d6ec0 | 1996-03-05 16:28:07 +0000 | [diff] [blame] | 229 | |
Barry Warsaw | 8ee4a60 | 1998-02-06 16:01:52 +0000 | [diff] [blame] | 230 | (defcustom py-scroll-process-buffer nil |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 231 | "*Scroll Python process buffer as output arrives. |
| 232 | If nil, the Python process buffer acts, with respect to scrolling, like |
| 233 | Shell-mode buffers normally act. This is surprisingly complicated and |
| 234 | so won't be explained here; in fact, you can't get the whole story |
| 235 | without studying the Emacs C code. |
| 236 | |
| 237 | If non-nil, the behavior is different in two respects (which are |
| 238 | slightly inaccurate in the interest of brevity): |
| 239 | |
| 240 | - If the buffer is in a window, and you left point at its end, the |
| 241 | window will scroll as new output arrives, and point will move to the |
| 242 | buffer's end, even if the window is not the selected window (that |
| 243 | being the one the cursor is in). The usual behavior for shell-mode |
| 244 | windows is not to scroll, and to leave point where it was, if the |
| 245 | buffer is in a window other than the selected window. |
| 246 | |
| 247 | - If the buffer is not visible in any window, and you left point at |
| 248 | its end, the buffer will be popped into a window as soon as more |
| 249 | output arrives. This is handy if you have a long-running |
| 250 | computation and don't want to tie up screen area waiting for the |
| 251 | output. The usual behavior for a shell-mode buffer is to stay |
| 252 | invisible until you explicitly visit it. |
| 253 | |
| 254 | Note the `and if you left point at its end' clauses in both of the |
| 255 | above: you can `turn off' the special behaviors while output is in |
| 256 | progress, by visiting the Python buffer and moving point to anywhere |
| 257 | besides the end. Then the buffer won't scroll, point will remain where |
| 258 | you leave it, and if you hide the buffer it will stay hidden until you |
| 259 | visit it again. You can enable and disable the special behaviors as |
| 260 | often as you like, while output is in progress, by (respectively) moving |
| 261 | point to, or away from, the end of the buffer. |
| 262 | |
| 263 | Warning: If you expect a large amount of output, you'll probably be |
| 264 | happier setting this option to nil. |
| 265 | |
| 266 | Obscure: `End of buffer' above should really say `at or beyond the |
| 267 | process mark', but if you know what that means you didn't need to be |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 268 | told <grin>." |
| 269 | :type 'boolean |
| 270 | :group 'python) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 271 | |
Barry Warsaw | 516b620 | 1997-08-09 06:43:20 +0000 | [diff] [blame] | 272 | (defcustom py-temp-directory |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 273 | (let ((ok '(lambda (x) |
| 274 | (and x |
| 275 | (setq x (expand-file-name x)) ; always true |
| 276 | (file-directory-p x) |
| 277 | (file-writable-p x) |
| 278 | x)))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 279 | (or (funcall ok (getenv "TMPDIR")) |
| 280 | (funcall ok "/usr/tmp") |
| 281 | (funcall ok "/tmp") |
| 282 | (funcall ok ".") |
| 283 | (error |
Barry Warsaw | 6c6db0a | 1998-02-25 16:33:56 +0000 | [diff] [blame] | 284 | "Couldn't find a usable temp directory -- set `py-temp-directory'"))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 285 | "*Directory used for temp files created by a *Python* process. |
| 286 | By default, the first directory from this list that exists and that you |
| 287 | can write into: the value (if any) of the environment variable TMPDIR, |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 288 | /usr/tmp, /tmp, or the current directory." |
| 289 | :type 'string |
| 290 | :group 'python) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 291 | |
Barry Warsaw | 516b620 | 1997-08-09 06:43:20 +0000 | [diff] [blame] | 292 | (defcustom py-beep-if-tab-change t |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 293 | "*Ring the bell if tab-width is changed. |
| 294 | If a comment of the form |
| 295 | |
| 296 | \t# vi:set tabsize=<number>: |
| 297 | |
| 298 | is found before the first code line when the file is entered, and the |
| 299 | current value of (the general Emacs variable) `tab-width' does not |
| 300 | equal <number>, `tab-width' is set to <number>, a message saying so is |
| 301 | displayed in the echo area, and if `py-beep-if-tab-change' is non-nil |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 302 | the Emacs bell is also rung as a warning." |
| 303 | :type 'boolean |
| 304 | :group 'python) |
| 305 | |
Barry Warsaw | 9981d22 | 1997-12-03 05:25:48 +0000 | [diff] [blame] | 306 | (defcustom py-jump-on-exception t |
| 307 | "*Jump to innermost exception frame in *Python Output* buffer. |
| 308 | When this variable is non-nil and ane exception occurs when running |
| 309 | Python code synchronously in a subprocess, jump immediately to the |
| 310 | source code of the innermost frame.") |
| 311 | |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 312 | (defcustom py-backspace-function 'backward-delete-char-untabify |
| 313 | "*Function called by `py-electric-backspace' when deleting backwards." |
| 314 | :type 'function |
| 315 | :group 'python) |
| 316 | |
| 317 | (defcustom py-delete-function 'delete-char |
| 318 | "*Function called by `py-electric-delete' when deleting forwards." |
| 319 | :type 'function |
| 320 | :group 'python) |
| 321 | |
Barry Warsaw | c0ecb53 | 1998-01-20 21:43:34 +0000 | [diff] [blame] | 322 | ;; Not customizable |
| 323 | (defvar py-master-file nil |
| 324 | "If non-nil, execute the named file instead of the buffer's file. |
Barry Warsaw | 50b3eb6 | 1998-02-25 15:57:47 +0000 | [diff] [blame] | 325 | The intent is to allow you to set this variable in the file's local |
Barry Warsaw | c0ecb53 | 1998-01-20 21:43:34 +0000 | [diff] [blame] | 326 | variable section, e.g.: |
| 327 | |
| 328 | # Local Variables: |
| 329 | # py-master-file: \"master.py\" |
| 330 | # End: |
| 331 | |
Barry Warsaw | 50b3eb6 | 1998-02-25 15:57:47 +0000 | [diff] [blame] | 332 | so that typing \\[py-execute-buffer] in that buffer executes the |
Barry Warsaw | c0ecb53 | 1998-01-20 21:43:34 +0000 | [diff] [blame] | 333 | named master file instead of the buffer's file. Note that if the file |
Barry Warsaw | 50b3eb6 | 1998-02-25 15:57:47 +0000 | [diff] [blame] | 334 | name has a relative path, the `default-directory' for the buffer is |
| 335 | prepended to come up with a file name.") |
Barry Warsaw | c0ecb53 | 1998-01-20 21:43:34 +0000 | [diff] [blame] | 336 | (make-variable-buffer-local 'py-master-file) |
| 337 | |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 338 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 339 | |
| 340 | ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 341 | ;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT |
| 342 | |
Barry Warsaw | 5e21cb0 | 1997-11-05 18:41:11 +0000 | [diff] [blame] | 343 | (defconst py-emacs-features |
| 344 | (let (features) |
| 345 | ;; NTEmacs 19.34.6 has a broken make-temp-name; it always returns |
| 346 | ;; the same string. |
| 347 | (let ((tmp1 (make-temp-name "")) |
| 348 | (tmp2 (make-temp-name ""))) |
| 349 | (if (string-equal tmp1 tmp2) |
| 350 | (push 'broken-temp-names features))) |
| 351 | ;; return the features |
| 352 | features) |
Barry Warsaw | c12c62e | 1997-09-04 04:18:07 +0000 | [diff] [blame] | 353 | "A list of features extant in the Emacs you are using. |
Barry Warsaw | 6ae21ad | 1997-11-06 14:36:49 +0000 | [diff] [blame] | 354 | There are many flavors of Emacs out there, with different levels of |
| 355 | support for features needed by `python-mode'.") |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 356 | |
Barry Warsaw | fb07f40 | 1997-02-24 03:37:22 +0000 | [diff] [blame] | 357 | (defvar python-font-lock-keywords |
Barry Warsaw | b8f1166 | 1997-11-06 14:35:15 +0000 | [diff] [blame] | 358 | (let ((kw1 (mapconcat 'identity |
| 359 | '("and" "assert" "break" "class" |
| 360 | "continue" "def" "del" "elif" |
| 361 | "else" "except" "exec" "for" |
| 362 | "from" "global" "if" "import" |
| 363 | "in" "is" "lambda" "not" |
| 364 | "or" "pass" "print" "raise" |
| 365 | "return" "while" |
| 366 | ) |
| 367 | "\\|")) |
| 368 | (kw2 (mapconcat 'identity |
| 369 | '("else:" "except:" "finally:" "try:") |
| 370 | "\\|")) |
| 371 | ) |
Barry Warsaw | 33ab6e4 | 1996-03-05 00:44:31 +0000 | [diff] [blame] | 372 | (list |
Barry Warsaw | ef3c891 | 1997-11-05 18:55:50 +0000 | [diff] [blame] | 373 | ;; keywords |
Barry Warsaw | b8f1166 | 1997-11-06 14:35:15 +0000 | [diff] [blame] | 374 | (cons (concat "\\b\\(" kw1 "\\)\\b[ \n\t(]") 1) |
| 375 | ;; block introducing keywords with immediately following colons. |
| 376 | ;; Yes "except" is in both lists. |
| 377 | (cons (concat "\\b\\(" kw2 "\\)[ \n\t(]") 1) |
Barry Warsaw | 33ab6e4 | 1996-03-05 00:44:31 +0000 | [diff] [blame] | 378 | ;; classes |
| 379 | '("\\bclass[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)" |
| 380 | 1 font-lock-type-face) |
| 381 | ;; functions |
| 382 | '("\\bdef[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)" |
| 383 | 1 font-lock-function-name-face) |
| 384 | )) |
Barry Warsaw | 62d9d6e | 1996-03-06 20:32:27 +0000 | [diff] [blame] | 385 | "Additional expressions to highlight in Python mode.") |
Barry Warsaw | fb07f40 | 1997-02-24 03:37:22 +0000 | [diff] [blame] | 386 | (put 'python-mode 'font-lock-defaults '(python-font-lock-keywords)) |
| 387 | |
Barry Warsaw | b01b4fa | 1995-06-20 18:55:34 +0000 | [diff] [blame] | 388 | |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 389 | (defvar imenu-example--python-show-method-args-p nil |
| 390 | "*Controls echoing of arguments of functions & methods in the imenu buffer. |
| 391 | When non-nil, arguments are printed.") |
| 392 | |
Barry Warsaw | 52bc17c | 1995-10-12 21:15:49 +0000 | [diff] [blame] | 393 | (make-variable-buffer-local 'py-indent-offset) |
| 394 | |
Barry Warsaw | e467bfb | 1997-11-26 05:40:58 +0000 | [diff] [blame] | 395 | ;; have to bind py-file-queue before installing the kill-emacs-hook |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 396 | (defvar py-file-queue nil |
| 397 | "Queue of Python temp files awaiting execution. |
| 398 | Currently-active file is at the head of the list.") |
| 399 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 400 | |
| 401 | ;; Constants |
| 402 | |
| 403 | ;; Regexp matching a Python string literal |
| 404 | (defconst py-stringlit-re |
| 405 | (concat |
| 406 | "'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted |
| 407 | "\\|" ; or |
| 408 | "\"\\([^\"\n\\]\\|\\\\.\\)*\"")) ; double-quoted |
| 409 | |
| 410 | ;; Regexp matching Python lines that are continued via backslash. |
| 411 | ;; This is tricky because a trailing backslash does not mean |
| 412 | ;; continuation if it's in a comment |
| 413 | (defconst py-continued-re |
| 414 | (concat |
| 415 | "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*" |
| 416 | "\\\\$")) |
| 417 | |
| 418 | ;; Regexp matching blank or comment lines. |
| 419 | (defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)") |
| 420 | |
| 421 | ;; Regexp matching clauses to be outdented one level. |
| 422 | (defconst py-outdent-re |
| 423 | (concat "\\(" (mapconcat 'identity |
| 424 | '("else:" |
| 425 | "except\\(\\s +.*\\)?:" |
| 426 | "finally:" |
| 427 | "elif\\s +.*:") |
| 428 | "\\|") |
| 429 | "\\)")) |
| 430 | |
| 431 | |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 432 | ;; Regexp matching keywords which typically close a block |
| 433 | (defconst py-block-closing-keywords-re |
| 434 | "\\(return\\|raise\\|break\\|continue\\|pass\\)") |
| 435 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 436 | ;; Regexp matching lines to not outdent after. |
| 437 | (defconst py-no-outdent-re |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 438 | (concat |
| 439 | "\\(" |
| 440 | (mapconcat 'identity |
| 441 | (list "try:" |
| 442 | "except\\(\\s +.*\\)?:" |
| 443 | "while\\s +.*:" |
| 444 | "for\\s +.*:" |
| 445 | "if\\s +.*:" |
| 446 | "elif\\s +.*:" |
| 447 | (concat py-block-closing-keywords-re "[ \t\n]") |
| 448 | ) |
| 449 | "\\|") |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 450 | "\\)")) |
| 451 | |
| 452 | ;; Regexp matching a function, method or variable assignment. If you |
| 453 | ;; change this, you probably have to change `py-current-defun' as |
| 454 | ;; well. This is only used by `py-current-defun' to find the name for |
| 455 | ;; add-log.el. |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 456 | (defconst py-defun-start-re |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 457 | "^\\([ \t]*\\)def[ \t]+\\([a-zA-Z_0-9]+\\)\\|\\(^[a-zA-Z_0-9]+\\)[ \t]*=") |
| 458 | |
| 459 | ;; Regexp for finding a class name. If you change this, you probably |
| 460 | ;; have to change `py-current-defun' as well. This is only used by |
| 461 | ;; `py-current-defun' to find the name for add-log.el. |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 462 | (defconst py-class-start-re "^class[ \t]*\\([a-zA-Z_0-9]+\\)") |
| 463 | |
| 464 | ;; Regexp that describes tracebacks |
| 465 | (defconst py-traceback-line-re |
Barry Warsaw | ffbc17d | 1997-11-27 20:08:14 +0000 | [diff] [blame] | 466 | "[ \t]+File \"\\([^\"]+\\)\", line \\([0-9]+\\)") |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 467 | |
| 468 | |
| 469 | |
| 470 | ;; Utilities |
| 471 | |
| 472 | (defmacro py-safe (&rest body) |
| 473 | ;; safely execute BODY, return nil if an error occurred |
| 474 | (` (condition-case nil |
| 475 | (progn (,@ body)) |
| 476 | (error nil)))) |
| 477 | |
| 478 | (defsubst py-keep-region-active () |
| 479 | ;; Do whatever is necessary to keep the region active in XEmacs. |
| 480 | ;; Ignore byte-compiler warnings you might see. Also note that |
| 481 | ;; FSF's Emacs 19 does it differently; its policy doesn't require us |
| 482 | ;; to take explicit action. |
| 483 | (and (boundp 'zmacs-region-stays) |
| 484 | (setq zmacs-region-stays t))) |
| 485 | |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 486 | (defsubst py-point (position) |
| 487 | ;; Returns the value of point at certain commonly referenced POSITIONs. |
| 488 | ;; POSITION can be one of the following symbols: |
| 489 | ;; |
| 490 | ;; bol -- beginning of line |
| 491 | ;; eol -- end of line |
| 492 | ;; bod -- beginning of defun |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 493 | ;; bob -- beginning of buffer |
| 494 | ;; eob -- end of buffer |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 495 | ;; boi -- back to indentation |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 496 | ;; bos -- beginning of statement |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 497 | ;; |
| 498 | ;; This function does not modify point or mark. |
| 499 | (let ((here (point))) |
| 500 | (cond |
| 501 | ((eq position 'bol) (beginning-of-line)) |
| 502 | ((eq position 'eol) (end-of-line)) |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 503 | ((eq position 'bod) (py-beginning-of-def-or-class)) |
| 504 | ;; Kind of funny, I know, but useful for py-up-exception. |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 505 | ((eq position 'bob) (beginning-of-buffer)) |
| 506 | ((eq position 'eob) (end-of-buffer)) |
| 507 | ((eq position 'boi) (back-to-indentation)) |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 508 | ((eq position 'bos) (py-goto-initial-line)) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 509 | (t (error "unknown buffer position requested: %s" position)) |
| 510 | ) |
| 511 | (prog1 |
| 512 | (point) |
| 513 | (goto-char here)))) |
| 514 | |
| 515 | (defsubst py-highlight-line (from to file line) |
| 516 | (cond |
| 517 | ((fboundp 'make-extent) |
| 518 | ;; XEmacs |
| 519 | (let ((e (make-extent from to))) |
| 520 | (set-extent-property e 'mouse-face 'highlight) |
| 521 | (set-extent-property e 'py-exc-info (cons file line)) |
| 522 | (set-extent-property e 'keymap py-mode-output-map))) |
| 523 | (t |
| 524 | ;; Emacs -- Please port this! |
| 525 | ) |
| 526 | )) |
| 527 | |
Barry Warsaw | e908b6b | 1998-03-19 22:48:02 +0000 | [diff] [blame] | 528 | (defun py-in-literal (&optional lim) |
| 529 | ;; Determine if point is in a Python literal, defined as a comment |
| 530 | ;; or string. This is the version used for non-XEmacs, which has a |
| 531 | ;; nicer interface. |
| 532 | ;; |
| 533 | ;; WARNING: Watch out for infinite recursion. |
| 534 | (let* ((lim (or lim (c-point 'bod))) |
| 535 | (state (parse-partial-sexp lim (point)))) |
| 536 | (cond |
| 537 | ((nth 3 state) 'string) |
| 538 | ((nth 4 state) 'comment) |
| 539 | (t nil)))) |
| 540 | |
| 541 | ;; XEmacs has a built-in function that should make this much quicker. |
| 542 | ;; In this case, lim is ignored |
| 543 | (defun py-fast-in-literal (&optional lim) |
| 544 | ;; don't have to worry about context == 'block-comment |
| 545 | (buffer-syntactic-context)) |
| 546 | |
| 547 | (if (fboundp 'buffer-syntactic-context) |
| 548 | (defalias 'c-in-literal 'c-fast-in-literal)) |
| 549 | |
| 550 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 551 | |
| 552 | ;; Major mode boilerplate |
| 553 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 554 | ;; define a mode-specific abbrev table for those who use such things |
| 555 | (defvar python-mode-abbrev-table nil |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 556 | "Abbrev table in use in `python-mode' buffers.") |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 557 | (define-abbrev-table 'python-mode-abbrev-table nil) |
| 558 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 559 | (defvar python-mode-hook nil |
| 560 | "*Hook called by `python-mode'.") |
| 561 | |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 562 | ;; In previous version of python-mode.el, the hook was incorrectly |
| 563 | ;; called py-mode-hook, and was not defvar'd. Deprecate its use. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 564 | (and (fboundp 'make-obsolete-variable) |
| 565 | (make-obsolete-variable 'py-mode-hook 'python-mode-hook)) |
| 566 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 567 | (defvar py-mode-map () |
| 568 | "Keymap used in `python-mode' buffers.") |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 569 | (if py-mode-map |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 570 | nil |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 571 | (setq py-mode-map (make-sparse-keymap)) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 572 | ;; electric keys |
| 573 | (define-key py-mode-map ":" 'py-electric-colon) |
| 574 | ;; indentation level modifiers |
| 575 | (define-key py-mode-map "\C-c\C-l" 'py-shift-region-left) |
| 576 | (define-key py-mode-map "\C-c\C-r" 'py-shift-region-right) |
| 577 | (define-key py-mode-map "\C-c<" 'py-shift-region-left) |
| 578 | (define-key py-mode-map "\C-c>" 'py-shift-region-right) |
| 579 | ;; subprocess commands |
| 580 | (define-key py-mode-map "\C-c\C-c" 'py-execute-buffer) |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame^] | 581 | (define-key py-mode-map "\C-c\C-m" 'py-execute-import-or-reload) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 582 | (define-key py-mode-map "\C-c|" 'py-execute-region) |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame^] | 583 | (define-key py-mode-map "\e\C-x" 'py-execute-def-or-class) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 584 | (define-key py-mode-map "\C-c!" 'py-shell) |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 585 | (define-key py-mode-map "\C-c\C-t" 'py-toggle-shells) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 586 | ;; Caution! Enter here at your own risk. We are trying to support |
| 587 | ;; several behaviors and it gets disgusting. :-( This logic ripped |
| 588 | ;; largely from CC Mode. |
| 589 | ;; |
| 590 | ;; In XEmacs 19, Emacs 19, and Emacs 20, we use this to bind |
| 591 | ;; backwards deletion behavior to DEL, which both Delete and |
| 592 | ;; Backspace get translated to. There's no way to separate this |
| 593 | ;; behavior in a clean way, so deal with it! Besides, it's been |
| 594 | ;; this way since the dawn of time. |
| 595 | (if (not (boundp 'delete-key-deletes-forward)) |
| 596 | (define-key py-mode-map "\177" 'py-electric-backspace) |
| 597 | ;; However, XEmacs 20 actually achieved enlightenment. It is |
| 598 | ;; possible to sanely define both backward and forward deletion |
| 599 | ;; behavior under X separately (TTYs are forever beyond hope, but |
| 600 | ;; who cares? XEmacs 20 does the right thing with these too). |
| 601 | (define-key py-mode-map [delete] 'py-electric-delete) |
| 602 | (define-key py-mode-map [backspace] 'py-electric-backspace)) |
Barry Warsaw | 8c4a8de | 1997-11-26 20:30:33 +0000 | [diff] [blame] | 603 | ;; Separate M-BS from C-M-h. The former should remain |
| 604 | ;; backward-kill-word. |
| 605 | (define-key py-mode-map [(control meta h)] 'py-mark-def-or-class) |
Barry Warsaw | 2518c67 | 1997-11-05 00:51:08 +0000 | [diff] [blame] | 606 | (define-key py-mode-map "\C-c\C-k" 'py-mark-block) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 607 | ;; Miscellaneous |
| 608 | (define-key py-mode-map "\C-c:" 'py-guess-indent-offset) |
| 609 | (define-key py-mode-map "\C-c\t" 'py-indent-region) |
| 610 | (define-key py-mode-map "\C-c\C-n" 'py-next-statement) |
| 611 | (define-key py-mode-map "\C-c\C-p" 'py-previous-statement) |
| 612 | (define-key py-mode-map "\C-c\C-u" 'py-goto-block-up) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 613 | (define-key py-mode-map "\C-c#" 'py-comment-region) |
| 614 | (define-key py-mode-map "\C-c?" 'py-describe-mode) |
| 615 | (define-key py-mode-map "\C-c\C-hm" 'py-describe-mode) |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 616 | (define-key py-mode-map "\e\C-a" 'py-beginning-of-def-or-class) |
| 617 | (define-key py-mode-map "\e\C-e" 'py-end-of-def-or-class) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 618 | (define-key py-mode-map "\C-c-" 'py-up-exception) |
| 619 | (define-key py-mode-map "\C-c=" 'py-down-exception) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 620 | ;; information |
| 621 | (define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report) |
| 622 | (define-key py-mode-map "\C-c\C-v" 'py-version) |
| 623 | ;; py-newline-and-indent mappings |
Barry Warsaw | 82aecb9 | 1998-01-21 05:14:24 +0000 | [diff] [blame] | 624 | (define-key py-mode-map "\n" 'py-newline-and-indent) |
| 625 | (define-key py-mode-map "\C-m" 'py-newline-and-indent) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 626 | ;; shadow global bindings for newline-and-indent w/ the py- version. |
| 627 | ;; BAW - this is extremely bad form, but I'm not going to change it |
| 628 | ;; for now. |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 629 | (mapcar #'(lambda (key) |
| 630 | (define-key py-mode-map key 'py-newline-and-indent)) |
| 631 | (where-is-internal 'newline-and-indent)) |
| 632 | ) |
| 633 | |
| 634 | (defvar py-mode-output-map nil |
| 635 | "Keymap used in *Python Output* buffers*") |
| 636 | (if py-mode-output-map |
| 637 | nil |
| 638 | (setq py-mode-output-map (make-sparse-keymap)) |
| 639 | (define-key py-mode-output-map [button2] 'py-mouseto-exception) |
| 640 | (define-key py-mode-output-map "\C-c\C-c" 'py-goto-exception) |
| 641 | ;; TBD: Disable all self-inserting keys. This is bogus, we should |
| 642 | ;; really implement this as *Python Output* buffer being read-only |
| 643 | (mapcar #' (lambda (key) |
| 644 | (define-key py-mode-output-map key |
| 645 | #'(lambda () (interactive) (beep)))) |
| 646 | (where-is-internal 'self-insert-command)) |
Barry Warsaw | 850437a | 1995-03-08 21:50:28 +0000 | [diff] [blame] | 647 | ) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 648 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 649 | (defvar py-mode-syntax-table nil |
| 650 | "Syntax table used in `python-mode' buffers.") |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 651 | (if py-mode-syntax-table |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 652 | nil |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 653 | (setq py-mode-syntax-table (make-syntax-table)) |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 654 | (modify-syntax-entry ?\( "()" py-mode-syntax-table) |
| 655 | (modify-syntax-entry ?\) ")(" py-mode-syntax-table) |
| 656 | (modify-syntax-entry ?\[ "(]" py-mode-syntax-table) |
| 657 | (modify-syntax-entry ?\] ")[" py-mode-syntax-table) |
| 658 | (modify-syntax-entry ?\{ "(}" py-mode-syntax-table) |
| 659 | (modify-syntax-entry ?\} "){" py-mode-syntax-table) |
| 660 | ;; Add operator symbols misassigned in the std table |
| 661 | (modify-syntax-entry ?\$ "." py-mode-syntax-table) |
| 662 | (modify-syntax-entry ?\% "." py-mode-syntax-table) |
| 663 | (modify-syntax-entry ?\& "." py-mode-syntax-table) |
| 664 | (modify-syntax-entry ?\* "." py-mode-syntax-table) |
| 665 | (modify-syntax-entry ?\+ "." py-mode-syntax-table) |
| 666 | (modify-syntax-entry ?\- "." py-mode-syntax-table) |
| 667 | (modify-syntax-entry ?\/ "." py-mode-syntax-table) |
| 668 | (modify-syntax-entry ?\< "." py-mode-syntax-table) |
| 669 | (modify-syntax-entry ?\= "." py-mode-syntax-table) |
| 670 | (modify-syntax-entry ?\> "." py-mode-syntax-table) |
| 671 | (modify-syntax-entry ?\| "." py-mode-syntax-table) |
| 672 | ;; For historical reasons, underscore is word class instead of |
| 673 | ;; symbol class. GNU conventions say it should be symbol class, but |
| 674 | ;; there's a natural conflict between what major mode authors want |
| 675 | ;; and what users expect from `forward-word' and `backward-word'. |
| 676 | ;; Guido and I have hashed this out and have decided to keep |
| 677 | ;; underscore in word class. If you're tempted to change it, try |
| 678 | ;; binding M-f and M-b to py-forward-into-nomenclature and |
| 679 | ;; py-backward-into-nomenclature instead. |
| 680 | (modify-syntax-entry ?\_ "w" py-mode-syntax-table) |
| 681 | ;; Both single quote and double quote are string delimiters |
| 682 | (modify-syntax-entry ?\' "\"" py-mode-syntax-table) |
| 683 | (modify-syntax-entry ?\" "\"" py-mode-syntax-table) |
| 684 | ;; backquote is open and close paren |
| 685 | (modify-syntax-entry ?\` "$" py-mode-syntax-table) |
| 686 | ;; comment delimiters |
| 687 | (modify-syntax-entry ?\# "<" py-mode-syntax-table) |
| 688 | (modify-syntax-entry ?\n ">" py-mode-syntax-table) |
| 689 | ) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 690 | |
Barry Warsaw | b3e81d5 | 1996-09-04 15:12:42 +0000 | [diff] [blame] | 691 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 692 | |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 693 | ;; Menu definitions, only relevent if you have the easymenu.el package |
| 694 | ;; (standard in the latest Emacs 19 and XEmacs 19 distributions). |
Barry Warsaw | 5490a06 | 1996-08-06 15:43:33 +0000 | [diff] [blame] | 695 | (defvar py-menu nil |
| 696 | "Menu for Python Mode. |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 697 | This menu will get created automatically if you have the `easymenu' |
| 698 | package. Note that the latest X/Emacs releases contain this package.") |
Barry Warsaw | 5490a06 | 1996-08-06 15:43:33 +0000 | [diff] [blame] | 699 | |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 700 | (and (py-safe (require 'easymenu) t) |
| 701 | (easy-menu-define |
| 702 | py-menu py-mode-map "Python Mode menu" |
| 703 | '("Python" |
| 704 | ["Comment Out Region" py-comment-region (mark)] |
| 705 | ["Uncomment Region" (py-comment-region (point) (mark) '(4)) (mark)] |
| 706 | "-" |
| 707 | ["Mark current block" py-mark-block t] |
Barry Warsaw | 2518c67 | 1997-11-05 00:51:08 +0000 | [diff] [blame] | 708 | ["Mark current def" py-mark-def-or-class t] |
| 709 | ["Mark current class" (py-mark-def-or-class t) t] |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 710 | "-" |
| 711 | ["Shift region left" py-shift-region-left (mark)] |
| 712 | ["Shift region right" py-shift-region-right (mark)] |
| 713 | "-" |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame^] | 714 | ["Import/reload file" py-execute-import-or-reload t] |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 715 | ["Execute buffer" py-execute-buffer t] |
| 716 | ["Execute region" py-execute-region (mark)] |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame^] | 717 | ["Execute def or class" py-execute-def-or-class (mark)] |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 718 | ["Start interpreter..." py-shell t] |
| 719 | "-" |
| 720 | ["Go to start of block" py-goto-block-up t] |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 721 | ["Go to start of class" (py-beginning-of-def-or-class t) t] |
| 722 | ["Move to end of class" (py-end-of-def-or-class t) t] |
| 723 | ["Move to start of def" py-beginning-of-def-or-class t] |
| 724 | ["Move to end of def" py-end-of-def-or-class t] |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 725 | "-" |
| 726 | ["Describe mode" py-describe-mode t] |
| 727 | ))) |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 728 | |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 729 | |
| 730 | |
| 731 | ;; imenu definitions, courtesy of Perry A. Stoll <stoll@atr-sw.atr.co.jp> |
| 732 | (defvar imenu-example--python-class-regexp |
| 733 | (concat ; <<classes>> |
| 734 | "\\(" ; |
| 735 | "^[ \t]*" ; newline and maybe whitespace |
| 736 | "\\(class[ \t]+[a-zA-Z0-9_]+\\)" ; class name |
| 737 | ; possibly multiple superclasses |
Barry Warsaw | 3179fe0 | 1998-04-04 21:36:53 +0000 | [diff] [blame] | 738 | "\\([ \t]*\\((\\([a-zA-Z0-9_,. \t\n]\\)*)\\)?\\)" |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 739 | "[ \t]*:" ; and the final : |
| 740 | "\\)" ; >>classes<< |
| 741 | ) |
| 742 | "Regexp for Python classes for use with the imenu package." |
| 743 | ) |
| 744 | |
| 745 | (defvar imenu-example--python-method-regexp |
| 746 | (concat ; <<methods and functions>> |
| 747 | "\\(" ; |
| 748 | "^[ \t]*" ; new line and maybe whitespace |
| 749 | "\\(def[ \t]+" ; function definitions start with def |
| 750 | "\\([a-zA-Z0-9_]+\\)" ; name is here |
| 751 | ; function arguments... |
| 752 | "[ \t]*(\\([a-zA-Z0-9_=,\* \t\n]*\\))" |
| 753 | "\\)" ; end of def |
| 754 | "[ \t]*:" ; and then the : |
| 755 | "\\)" ; >>methods and functions<< |
| 756 | ) |
| 757 | "Regexp for Python methods/functions for use with the imenu package." |
| 758 | ) |
| 759 | |
| 760 | (defvar imenu-example--python-method-no-arg-parens '(2 8) |
| 761 | "Indicies into groups of the Python regexp for use with imenu. |
| 762 | |
| 763 | Using these values will result in smaller imenu lists, as arguments to |
| 764 | functions are not listed. |
| 765 | |
| 766 | See the variable `imenu-example--python-show-method-args-p' for more |
| 767 | information.") |
| 768 | |
| 769 | (defvar imenu-example--python-method-arg-parens '(2 7) |
| 770 | "Indicies into groups of the Python regexp for use with imenu. |
| 771 | Using these values will result in large imenu lists, as arguments to |
| 772 | functions are listed. |
| 773 | |
| 774 | See the variable `imenu-example--python-show-method-args-p' for more |
| 775 | information.") |
| 776 | |
| 777 | ;; Note that in this format, this variable can still be used with the |
| 778 | ;; imenu--generic-function. Otherwise, there is no real reason to have |
| 779 | ;; it. |
| 780 | (defvar imenu-example--generic-python-expression |
| 781 | (cons |
| 782 | (concat |
| 783 | imenu-example--python-class-regexp |
| 784 | "\\|" ; or... |
| 785 | imenu-example--python-method-regexp |
| 786 | ) |
| 787 | imenu-example--python-method-no-arg-parens) |
| 788 | "Generic Python expression which may be used directly with imenu. |
| 789 | Used by setting the variable `imenu-generic-expression' to this value. |
| 790 | Also, see the function \\[imenu-example--create-python-index] for a |
| 791 | better alternative for finding the index.") |
| 792 | |
| 793 | ;; These next two variables are used when searching for the python |
| 794 | ;; class/definitions. Just saving some time in accessing the |
| 795 | ;; generic-python-expression, really. |
Barry Warsaw | 01af401 | 1996-09-04 14:57:22 +0000 | [diff] [blame] | 796 | (defvar imenu-example--python-generic-regexp nil) |
| 797 | (defvar imenu-example--python-generic-parens nil) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 798 | |
| 799 | |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 800 | (defun imenu-example--create-python-index () |
| 801 | "Python interface function for imenu package. |
| 802 | Finds all python classes and functions/methods. Calls function |
| 803 | \\[imenu-example--create-python-index-engine]. See that function for |
| 804 | the details of how this works." |
| 805 | (setq imenu-example--python-generic-regexp |
| 806 | (car imenu-example--generic-python-expression)) |
| 807 | (setq imenu-example--python-generic-parens |
| 808 | (if imenu-example--python-show-method-args-p |
| 809 | imenu-example--python-method-arg-parens |
| 810 | imenu-example--python-method-no-arg-parens)) |
| 811 | (goto-char (point-min)) |
| 812 | (imenu-example--create-python-index-engine nil)) |
| 813 | |
| 814 | (defun imenu-example--create-python-index-engine (&optional start-indent) |
| 815 | "Function for finding imenu definitions in Python. |
| 816 | |
| 817 | Finds all definitions (classes, methods, or functions) in a Python |
| 818 | file for the imenu package. |
| 819 | |
| 820 | Returns a possibly nested alist of the form |
| 821 | |
| 822 | (INDEX-NAME . INDEX-POSITION) |
| 823 | |
| 824 | The second element of the alist may be an alist, producing a nested |
| 825 | list as in |
| 826 | |
| 827 | (INDEX-NAME . INDEX-ALIST) |
| 828 | |
| 829 | This function should not be called directly, as it calls itself |
| 830 | recursively and requires some setup. Rather this is the engine for |
| 831 | the function \\[imenu-example--create-python-index]. |
| 832 | |
| 833 | It works recursively by looking for all definitions at the current |
| 834 | indention level. When it finds one, it adds it to the alist. If it |
| 835 | finds a definition at a greater indentation level, it removes the |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 836 | previous definition from the alist. In its place it adds all |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 837 | definitions found at the next indentation level. When it finds a |
| 838 | definition that is less indented then the current level, it retuns the |
| 839 | alist it has created thus far. |
| 840 | |
| 841 | The optional argument START-INDENT indicates the starting indentation |
| 842 | at which to continue looking for Python classes, methods, or |
| 843 | functions. If this is not supplied, the function uses the indentation |
| 844 | of the first definition found." |
| 845 | (let ((index-alist '()) |
| 846 | (sub-method-alist '()) |
| 847 | looking-p |
| 848 | def-name prev-name |
| 849 | cur-indent def-pos |
| 850 | (class-paren (first imenu-example--python-generic-parens)) |
| 851 | (def-paren (second imenu-example--python-generic-parens))) |
| 852 | (setq looking-p |
| 853 | (re-search-forward imenu-example--python-generic-regexp |
| 854 | (point-max) t)) |
| 855 | (while looking-p |
| 856 | (save-excursion |
| 857 | ;; used to set def-name to this value but generic-extract-name is |
| 858 | ;; new to imenu-1.14. this way it still works with imenu-1.11 |
| 859 | ;;(imenu--generic-extract-name imenu-example--python-generic-parens)) |
| 860 | (let ((cur-paren (if (match-beginning class-paren) |
| 861 | class-paren def-paren))) |
| 862 | (setq def-name |
Barry Warsaw | c852035 | 1997-11-26 06:14:40 +0000 | [diff] [blame] | 863 | (buffer-substring-no-properties (match-beginning cur-paren) |
| 864 | (match-end cur-paren)))) |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 865 | (beginning-of-line) |
| 866 | (setq cur-indent (current-indentation))) |
| 867 | |
| 868 | ;; HACK: want to go to the next correct definition location. we |
| 869 | ;; explicitly list them here. would be better to have them in a |
| 870 | ;; list. |
| 871 | (setq def-pos |
| 872 | (or (match-beginning class-paren) |
| 873 | (match-beginning def-paren))) |
| 874 | |
| 875 | ;; if we don't have a starting indent level, take this one |
| 876 | (or start-indent |
| 877 | (setq start-indent cur-indent)) |
| 878 | |
| 879 | ;; if we don't have class name yet, take this one |
| 880 | (or prev-name |
| 881 | (setq prev-name def-name)) |
| 882 | |
| 883 | ;; what level is the next definition on? must be same, deeper |
| 884 | ;; or shallower indentation |
| 885 | (cond |
| 886 | ;; at the same indent level, add it to the list... |
| 887 | ((= start-indent cur-indent) |
| 888 | |
| 889 | ;; if we don't have push, use the following... |
| 890 | ;;(setf index-alist (cons (cons def-name def-pos) index-alist)) |
| 891 | (push (cons def-name def-pos) index-alist)) |
| 892 | |
| 893 | ;; deeper indented expression, recur... |
| 894 | ((< start-indent cur-indent) |
| 895 | |
| 896 | ;; the point is currently on the expression we're supposed to |
| 897 | ;; start on, so go back to the last expression. The recursive |
| 898 | ;; call will find this place again and add it to the correct |
| 899 | ;; list |
| 900 | (re-search-backward imenu-example--python-generic-regexp |
| 901 | (point-min) 'move) |
| 902 | (setq sub-method-alist (imenu-example--create-python-index-engine |
| 903 | cur-indent)) |
| 904 | |
| 905 | (if sub-method-alist |
| 906 | ;; we put the last element on the index-alist on the start |
| 907 | ;; of the submethod alist so the user can still get to it. |
| 908 | (let ((save-elmt (pop index-alist))) |
Barry Warsaw | c852035 | 1997-11-26 06:14:40 +0000 | [diff] [blame] | 909 | (push (cons prev-name |
Barry Warsaw | 8143746 | 1996-08-01 19:48:02 +0000 | [diff] [blame] | 910 | (cons save-elmt sub-method-alist)) |
| 911 | index-alist)))) |
| 912 | |
| 913 | ;; found less indented expression, we're done. |
| 914 | (t |
| 915 | (setq looking-p nil) |
| 916 | (re-search-backward imenu-example--python-generic-regexp |
| 917 | (point-min) t))) |
| 918 | (setq prev-name def-name) |
| 919 | (and looking-p |
| 920 | (setq looking-p |
| 921 | (re-search-forward imenu-example--python-generic-regexp |
| 922 | (point-max) 'move)))) |
| 923 | (nreverse index-alist))) |
| 924 | |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 925 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 926 | ;;;###autoload |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 927 | (defun python-mode () |
| 928 | "Major mode for editing Python files. |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 929 | To submit a problem report, enter `\\[py-submit-bug-report]' from a |
| 930 | `python-mode' buffer. Do `\\[py-describe-mode]' for detailed |
| 931 | documentation. To see what version of `python-mode' you are running, |
| 932 | enter `\\[py-version]'. |
| 933 | |
| 934 | This mode knows about Python indentation, tokens, comments and |
| 935 | continuation lines. Paragraphs are separated by blank lines only. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 936 | |
| 937 | COMMANDS |
| 938 | \\{py-mode-map} |
| 939 | VARIABLES |
| 940 | |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 941 | py-indent-offset\t\tindentation increment |
| 942 | py-block-comment-prefix\t\tcomment string used by comment-region |
| 943 | py-python-command\t\tshell command to invoke Python interpreter |
| 944 | py-scroll-process-buffer\t\talways scroll Python process buffer |
| 945 | py-temp-directory\t\tdirectory used for temp files (if needed) |
| 946 | py-beep-if-tab-change\t\tring the bell if tab-width is changed" |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 947 | (interactive) |
Barry Warsaw | 615d4a4 | 1996-09-04 14:14:10 +0000 | [diff] [blame] | 948 | ;; set up local variables |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 949 | (kill-all-local-variables) |
Barry Warsaw | 615d4a4 | 1996-09-04 14:14:10 +0000 | [diff] [blame] | 950 | (make-local-variable 'font-lock-defaults) |
| 951 | (make-local-variable 'paragraph-separate) |
| 952 | (make-local-variable 'paragraph-start) |
| 953 | (make-local-variable 'require-final-newline) |
| 954 | (make-local-variable 'comment-start) |
Barry Warsaw | 5c8bef1 | 1996-12-17 21:56:10 +0000 | [diff] [blame] | 955 | (make-local-variable 'comment-end) |
Barry Warsaw | 615d4a4 | 1996-09-04 14:14:10 +0000 | [diff] [blame] | 956 | (make-local-variable 'comment-start-skip) |
| 957 | (make-local-variable 'comment-column) |
| 958 | (make-local-variable 'indent-region-function) |
| 959 | (make-local-variable 'indent-line-function) |
Barry Warsaw | b3e81d5 | 1996-09-04 15:12:42 +0000 | [diff] [blame] | 960 | (make-local-variable 'add-log-current-defun-function) |
Barry Warsaw | 615d4a4 | 1996-09-04 14:14:10 +0000 | [diff] [blame] | 961 | ;; |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 962 | (set-syntax-table py-mode-syntax-table) |
Barry Warsaw | 615d4a4 | 1996-09-04 14:14:10 +0000 | [diff] [blame] | 963 | (setq major-mode 'python-mode |
| 964 | mode-name "Python" |
| 965 | local-abbrev-table python-mode-abbrev-table |
Barry Warsaw | 5c38bf6 | 1997-12-02 22:01:04 +0000 | [diff] [blame] | 966 | font-lock-defaults '(python-font-lock-keywords) |
Barry Warsaw | 615d4a4 | 1996-09-04 14:14:10 +0000 | [diff] [blame] | 967 | paragraph-separate "^[ \t]*$" |
| 968 | paragraph-start "^[ \t]*$" |
| 969 | require-final-newline t |
| 970 | comment-start "# " |
Barry Warsaw | 5c8bef1 | 1996-12-17 21:56:10 +0000 | [diff] [blame] | 971 | comment-end "" |
Barry Warsaw | 615d4a4 | 1996-09-04 14:14:10 +0000 | [diff] [blame] | 972 | comment-start-skip "# *" |
| 973 | comment-column 40 |
Barry Warsaw | 550a02e | 1996-09-04 14:23:00 +0000 | [diff] [blame] | 974 | indent-region-function 'py-indent-region |
| 975 | indent-line-function 'py-indent-line |
Barry Warsaw | b3e81d5 | 1996-09-04 15:12:42 +0000 | [diff] [blame] | 976 | ;; tell add-log.el how to find the current function/method/variable |
| 977 | add-log-current-defun-function 'py-current-defun |
Barry Warsaw | b1f8951 | 1996-09-03 16:38:30 +0000 | [diff] [blame] | 978 | ) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 979 | (use-local-map py-mode-map) |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 980 | ;; add the menu |
| 981 | (if py-menu |
| 982 | (easy-menu-add py-menu)) |
Barry Warsaw | 57697af | 1995-09-14 20:01:14 +0000 | [diff] [blame] | 983 | ;; Emacs 19 requires this |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 984 | (if (boundp 'comment-multi-line) |
Barry Warsaw | 57697af | 1995-09-14 20:01:14 +0000 | [diff] [blame] | 985 | (setq comment-multi-line nil)) |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 986 | ;; Install Imenu, only works for Emacs. |
| 987 | (when (py-safe (require 'imenu)) |
| 988 | (make-variable-buffer-local 'imenu-create-index-function) |
| 989 | (setq imenu-create-index-function |
| 990 | (function imenu-example--create-python-index)) |
| 991 | (setq imenu-generic-expression |
| 992 | imenu-example--generic-python-expression) |
| 993 | (if (fboundp 'imenu-add-to-menubar) |
| 994 | (imenu-add-to-menubar (format "%s-%s" "IM" mode-name))) |
| 995 | ) |
| 996 | ;; Run the mode hook. Note that py-mode-hook is deprecated. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 997 | (if python-mode-hook |
| 998 | (run-hooks 'python-mode-hook) |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 999 | (run-hooks 'py-mode-hook)) |
| 1000 | ;; Now do the automagical guessing |
| 1001 | (if py-smart-indentation |
| 1002 | (let ((offset py-indent-offset)) |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 1003 | ;; It's okay if this fails to guess a good value |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 1004 | (if (and (py-safe (py-guess-indent-offset)) |
| 1005 | (<= py-indent-offset 8) |
| 1006 | (>= py-indent-offset 2)) |
| 1007 | (setq offset py-indent-offset)) |
| 1008 | (setq py-indent-offset offset) |
Barry Warsaw | 639eea6 | 1998-03-16 18:12:13 +0000 | [diff] [blame] | 1009 | ;; Only turn indent-tabs-mode off if tab-width != |
| 1010 | ;; py-indent-offset. Never turn it on, because the user must |
| 1011 | ;; have explicitly turned it off. |
| 1012 | (if (/= tab-width py-indent-offset) |
| 1013 | (setq indent-tabs-mode nil)) |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 1014 | ))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1015 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1016 | |
Barry Warsaw | b91b743 | 1995-03-14 15:55:20 +0000 | [diff] [blame] | 1017 | ;; electric characters |
Barry Warsaw | 3874a3d | 1995-03-14 22:05:53 +0000 | [diff] [blame] | 1018 | (defun py-outdent-p () |
| 1019 | ;; returns non-nil if the current line should outdent one level |
| 1020 | (save-excursion |
| 1021 | (and (progn (back-to-indentation) |
| 1022 | (looking-at py-outdent-re)) |
Barry Warsaw | f06777d | 1998-01-21 05:36:18 +0000 | [diff] [blame] | 1023 | (progn (forward-line -1) |
| 1024 | (py-goto-initial-line) |
| 1025 | (back-to-indentation) |
Barry Warsaw | 3874a3d | 1995-03-14 22:05:53 +0000 | [diff] [blame] | 1026 | (while (or (looking-at py-blank-or-comment-re) |
| 1027 | (bobp)) |
| 1028 | (backward-to-indentation 1)) |
| 1029 | (not (looking-at py-no-outdent-re))) |
| 1030 | ))) |
| 1031 | |
Barry Warsaw | b91b743 | 1995-03-14 15:55:20 +0000 | [diff] [blame] | 1032 | (defun py-electric-colon (arg) |
| 1033 | "Insert a colon. |
| 1034 | In certain cases the line is outdented appropriately. If a numeric |
Barry Warsaw | 0c6563f | 1995-09-14 20:57:02 +0000 | [diff] [blame] | 1035 | argument is provided, that many colons are inserted non-electrically. |
| 1036 | Electric behavior is inhibited inside a string or comment." |
Barry Warsaw | b91b743 | 1995-03-14 15:55:20 +0000 | [diff] [blame] | 1037 | (interactive "P") |
| 1038 | (self-insert-command (prefix-numeric-value arg)) |
Barry Warsaw | 0c6563f | 1995-09-14 20:57:02 +0000 | [diff] [blame] | 1039 | ;; are we in a string or comment? |
| 1040 | (if (save-excursion |
| 1041 | (let ((pps (parse-partial-sexp (save-excursion |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 1042 | (py-beginning-of-def-or-class) |
Barry Warsaw | 0c6563f | 1995-09-14 20:57:02 +0000 | [diff] [blame] | 1043 | (point)) |
| 1044 | (point)))) |
| 1045 | (not (or (nth 3 pps) (nth 4 pps))))) |
| 1046 | (save-excursion |
| 1047 | (let ((here (point)) |
| 1048 | (outdent 0) |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 1049 | (indent (py-compute-indentation t))) |
Barry Warsaw | 0c6563f | 1995-09-14 20:57:02 +0000 | [diff] [blame] | 1050 | (if (and (not arg) |
| 1051 | (py-outdent-p) |
| 1052 | (= indent (save-excursion |
Barry Warsaw | a766182 | 1996-08-02 16:22:43 +0000 | [diff] [blame] | 1053 | (py-next-statement -1) |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 1054 | (py-compute-indentation t))) |
Barry Warsaw | 0c6563f | 1995-09-14 20:57:02 +0000 | [diff] [blame] | 1055 | ) |
| 1056 | (setq outdent py-indent-offset)) |
| 1057 | ;; Don't indent, only outdent. This assumes that any lines that |
| 1058 | ;; are already outdented relative to py-compute-indentation were |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 1059 | ;; put there on purpose. It's highly annoying to have `:' indent |
Barry Warsaw | 0c6563f | 1995-09-14 20:57:02 +0000 | [diff] [blame] | 1060 | ;; for you. Use TAB, C-c C-l or C-c C-r to adjust. TBD: Is |
| 1061 | ;; there a better way to determine this??? |
| 1062 | (if (< (current-indentation) indent) nil |
| 1063 | (goto-char here) |
| 1064 | (beginning-of-line) |
| 1065 | (delete-horizontal-space) |
| 1066 | (indent-to (- indent outdent)) |
| 1067 | ))))) |
Barry Warsaw | b91b743 | 1995-03-14 15:55:20 +0000 | [diff] [blame] | 1068 | |
| 1069 | |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1070 | ;; Python subprocess utilities and filters |
| 1071 | (defun py-execute-file (proc filename) |
| 1072 | ;; Send a properly formatted execfile('FILENAME') to the underlying |
| 1073 | ;; Python interpreter process FILENAME. Make that process's buffer |
| 1074 | ;; visible and force display. Also make comint believe the user |
| 1075 | ;; typed this string so that kill-output-from-shell does The Right |
| 1076 | ;; Thing. |
| 1077 | (let ((curbuf (current-buffer)) |
| 1078 | (procbuf (process-buffer proc)) |
| 1079 | (comint-scroll-to-bottom-on-output t) |
| 1080 | (msg (format "## working on region in file %s...\n" filename)) |
| 1081 | (cmd (format "execfile('%s')\n" filename))) |
| 1082 | (unwind-protect |
| 1083 | (progn |
| 1084 | (set-buffer procbuf) |
| 1085 | (goto-char (point-max)) |
| 1086 | (move-marker (process-mark proc) (point)) |
| 1087 | (funcall (process-filter proc) proc msg)) |
| 1088 | (set-buffer curbuf)) |
| 1089 | (process-send-string proc cmd))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1090 | |
| 1091 | (defun py-process-filter (pyproc string) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1092 | (let ((curbuf (current-buffer)) |
| 1093 | (pbuf (process-buffer pyproc)) |
| 1094 | (pmark (process-mark pyproc)) |
| 1095 | file-finished) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1096 | ;; make sure we switch to a different buffer at least once. if we |
| 1097 | ;; *don't* do this, then if the process buffer is in the selected |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1098 | ;; window, and point is before the end, and lots of output is |
| 1099 | ;; coming at a fast pace, then (a) simple cursor-movement commands |
| 1100 | ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time |
| 1101 | ;; to have a visible effect (the window just doesn't get updated, |
| 1102 | ;; sometimes for minutes(!)), and (b) it takes about 5x longer to |
| 1103 | ;; get all the process output (until the next python prompt). |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1104 | ;; |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1105 | ;; #b makes no sense to me at all. #a almost makes sense: unless |
| 1106 | ;; we actually change buffers, set_buffer_internal in buffer.c |
| 1107 | ;; doesn't set windows_or_buffers_changed to 1, & that in turn |
| 1108 | ;; seems to make the Emacs command loop reluctant to update the |
| 1109 | ;; display. Perhaps the default process filter in process.c's |
| 1110 | ;; read_process_output has update_mode_lines++ for a similar |
| 1111 | ;; reason? beats me ... |
| 1112 | |
Barry Warsaw | 7a73ef8 | 1996-09-30 23:00:40 +0000 | [diff] [blame] | 1113 | (unwind-protect |
| 1114 | ;; make sure current buffer is restored |
| 1115 | ;; BAW - we want to check to see if this still applies |
| 1116 | (progn |
| 1117 | ;; mysterious ugly hack |
| 1118 | (if (eq curbuf pbuf) |
| 1119 | (set-buffer (get-buffer-create "*scratch*"))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1120 | |
Barry Warsaw | 7a73ef8 | 1996-09-30 23:00:40 +0000 | [diff] [blame] | 1121 | (set-buffer pbuf) |
| 1122 | (let* ((start (point)) |
| 1123 | (goback (< start pmark)) |
| 1124 | (goend (and (not goback) (= start (point-max)))) |
| 1125 | (buffer-read-only nil)) |
| 1126 | (goto-char pmark) |
| 1127 | (insert string) |
| 1128 | (move-marker pmark (point)) |
| 1129 | (setq file-finished |
| 1130 | (and py-file-queue |
| 1131 | (equal ">>> " |
| 1132 | (buffer-substring |
| 1133 | (prog2 (beginning-of-line) (point) |
| 1134 | (goto-char pmark)) |
| 1135 | (point))))) |
| 1136 | (if goback (goto-char start) |
| 1137 | ;; else |
| 1138 | (if py-scroll-process-buffer |
| 1139 | (let* ((pop-up-windows t) |
| 1140 | (pwin (display-buffer pbuf))) |
| 1141 | (set-window-point pwin (point))))) |
| 1142 | (set-buffer curbuf) |
| 1143 | (if file-finished |
| 1144 | (progn |
Barry Warsaw | f471056 | 1997-11-26 21:00:36 +0000 | [diff] [blame] | 1145 | (py-safe (delete-file (car py-file-queue))) |
Barry Warsaw | 7a73ef8 | 1996-09-30 23:00:40 +0000 | [diff] [blame] | 1146 | (setq py-file-queue (cdr py-file-queue)) |
| 1147 | (if py-file-queue |
| 1148 | (py-execute-file pyproc (car py-file-queue))))) |
| 1149 | (and goend |
| 1150 | (progn (set-buffer pbuf) |
| 1151 | (goto-char (point-max)))) |
| 1152 | )) |
| 1153 | (set-buffer curbuf)))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1154 | |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1155 | (defun py-postprocess-output-buffer (buf) |
Barry Warsaw | f9b99f4 | 1998-03-26 16:08:59 +0000 | [diff] [blame] | 1156 | ;; Highlight exceptions found in BUF. If an exception occurred |
| 1157 | ;; return t, otherwise return nil. BUF must exist. |
| 1158 | (let (line file bol err-p) |
Barry Warsaw | 9981d22 | 1997-12-03 05:25:48 +0000 | [diff] [blame] | 1159 | (save-excursion |
| 1160 | (set-buffer buf) |
| 1161 | (beginning-of-buffer) |
| 1162 | (while (re-search-forward py-traceback-line-re nil t) |
| 1163 | (setq file (match-string 1) |
| 1164 | line (string-to-int (match-string 2)) |
| 1165 | bol (py-point 'bol)) |
Barry Warsaw | f9b99f4 | 1998-03-26 16:08:59 +0000 | [diff] [blame] | 1166 | (py-highlight-line bol (py-point 'eol) file line))) |
| 1167 | (when (and py-jump-on-exception line) |
| 1168 | (beep) |
| 1169 | (py-jump-to-exception file line) |
| 1170 | (setq err-p t)) |
| 1171 | err-p)) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1172 | |
Barry Warsaw | 9981d22 | 1997-12-03 05:25:48 +0000 | [diff] [blame] | 1173 | |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1174 | |
| 1175 | ;;; Subprocess commands |
| 1176 | |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1177 | ;; only used when (memq 'broken-temp-names py-emacs-features) |
| 1178 | (defvar py-serial-number 0) |
| 1179 | (defvar py-exception-buffer nil) |
| 1180 | (defconst py-output-buffer "*Python Output*") |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 1181 | (make-variable-buffer-local 'py-output-buffer) |
| 1182 | |
| 1183 | ;; for toggling between CPython and JPython |
| 1184 | (defvar py-which-shell py-python-command) |
| 1185 | (defvar py-which-args py-python-command-args) |
| 1186 | (defvar py-which-bufname "Python") |
| 1187 | (make-variable-buffer-local 'py-which-shell) |
| 1188 | (make-variable-buffer-local 'py-which-args) |
| 1189 | (make-variable-buffer-local 'py-which-bufname) |
| 1190 | |
| 1191 | (defun py-toggle-shells (arg) |
| 1192 | "Toggles between the CPython and JPython shells. |
| 1193 | With positive \\[universal-argument], uses the CPython shell, with |
| 1194 | negative \\[universal-argument] uses the JPython shell, and with a |
| 1195 | zero argument, toggles the shell." |
| 1196 | (interactive "P") |
| 1197 | ;; default is to toggle |
| 1198 | (if (null arg) |
| 1199 | (setq arg 0)) |
| 1200 | ;; toggle if zero |
| 1201 | (if (= arg 0) |
| 1202 | (if (string-equal py-which-bufname "Python") |
| 1203 | (setq arg -1) |
| 1204 | (setq arg 1))) |
| 1205 | (let (msg) |
| 1206 | (cond |
| 1207 | ((< 0 arg) |
| 1208 | ;; set to CPython |
| 1209 | (setq py-which-shell py-python-command |
| 1210 | py-which-args py-python-command-args |
| 1211 | py-which-bufname "Python" |
| 1212 | msg "CPython" |
| 1213 | mode-name "Python")) |
| 1214 | ((> 0 arg) |
| 1215 | (setq py-which-shell py-jpython-command |
| 1216 | py-which-args py-jpython-command-args |
| 1217 | py-which-bufname "JPython" |
| 1218 | msg "JPython" |
| 1219 | mode-name "JPython")) |
| 1220 | ) |
Barry Warsaw | ea609c1 | 1998-04-10 16:08:26 +0000 | [diff] [blame] | 1221 | (message "Using the %s shell" msg) |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 1222 | (setq py-output-buffer (format "*%s Output*" py-which-bufname)))) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1223 | |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1224 | ;;;###autoload |
| 1225 | (defun py-shell () |
| 1226 | "Start an interactive Python interpreter in another window. |
| 1227 | This is like Shell mode, except that Python is running in the window |
| 1228 | instead of a shell. See the `Interactive Shell' and `Shell Mode' |
| 1229 | sections of the Emacs manual for details, especially for the key |
| 1230 | bindings active in the `*Python*' buffer. |
| 1231 | |
| 1232 | See the docs for variable `py-scroll-buffer' for info on scrolling |
| 1233 | behavior in the process window. |
| 1234 | |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 1235 | Note: You can toggle between using the CPython interpreter and the |
| 1236 | JPython interpreter by hitting \\[py-toggle-shells]. This toggles |
| 1237 | buffer local variables which control whether all your subshell |
| 1238 | interactions happen to the `*JPython*' or `*Python*' buffers (the |
| 1239 | latter is the name used for the CPython buffer). |
| 1240 | |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1241 | Warning: Don't use an interactive Python if you change sys.ps1 or |
| 1242 | sys.ps2 from their default values, or if you're running code that |
| 1243 | prints `>>> ' or `... ' at the start of a line. `python-mode' can't |
| 1244 | distinguish your output from Python's output, and assumes that `>>> ' |
| 1245 | at the start of a line is a prompt from Python. Similarly, the Emacs |
| 1246 | Shell mode code assumes that both `>>> ' and `... ' at the start of a |
| 1247 | line are Python prompts. Bad things can happen if you fool either |
| 1248 | mode. |
| 1249 | |
| 1250 | Warning: If you do any editing *in* the process buffer *while* the |
| 1251 | buffer is accepting output from Python, do NOT attempt to `undo' the |
| 1252 | changes. Some of the output (nowhere near the parts you changed!) may |
| 1253 | be lost if you do. This appears to be an Emacs bug, an unfortunate |
| 1254 | interaction between undo and process filters; the same problem exists in |
| 1255 | non-Python process buffers using the default (Emacs-supplied) process |
| 1256 | filter." |
| 1257 | ;; BAW - should undo be disabled in the python process buffer, if |
| 1258 | ;; this bug still exists? |
| 1259 | (interactive) |
| 1260 | (require 'comint) |
Barry Warsaw | 2518c67 | 1997-11-05 00:51:08 +0000 | [diff] [blame] | 1261 | (switch-to-buffer-other-window |
Barry Warsaw | a239880 | 1998-04-09 23:28:20 +0000 | [diff] [blame] | 1262 | (apply 'make-comint py-which-bufname py-which-shell nil py-which-args)) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1263 | (make-local-variable 'comint-prompt-regexp) |
Barry Warsaw | ebc7b7a | 1998-05-19 15:01:06 +0000 | [diff] [blame] | 1264 | (setq comint-prompt-regexp "^>>> \\|^[.][.][.] \\|^(pdb) ") |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1265 | (set-process-filter (get-buffer-process (current-buffer)) 'py-process-filter) |
| 1266 | (set-syntax-table py-mode-syntax-table) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1267 | ;; set up keybindings for this subshell |
| 1268 | (local-set-key [tab] 'self-insert-command) |
| 1269 | (local-set-key "\C-c-" 'py-up-exception) |
| 1270 | (local-set-key "\C-c=" 'py-down-exception) |
| 1271 | ) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1272 | |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1273 | (defun py-clear-queue () |
| 1274 | "Clear the queue of temporary files waiting to execute." |
| 1275 | (interactive) |
| 1276 | (let ((n (length py-file-queue))) |
| 1277 | (mapcar 'delete-file py-file-queue) |
| 1278 | (setq py-file-queue nil) |
| 1279 | (message "%d pending files de-queued." n))) |
| 1280 | |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame^] | 1281 | |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1282 | (defun py-execute-region (start end &optional async) |
| 1283 | "Execute the the region in a Python interpreter. |
| 1284 | The region is first copied into a temporary file (in the directory |
| 1285 | `py-temp-directory'). If there is no Python interpreter shell |
| 1286 | running, this file is executed synchronously using |
| 1287 | `shell-command-on-region'. If the program is long running, use an |
| 1288 | optional \\[universal-argument] to run the command asynchronously in |
| 1289 | its own buffer. |
| 1290 | |
| 1291 | If the Python interpreter shell is running, the region is execfile()'d |
| 1292 | in that shell. If you try to execute regions too quickly, |
| 1293 | `python-mode' will queue them up and execute them one at a time when |
| 1294 | it sees a `>>> ' prompt from Python. Each time this happens, the |
| 1295 | process buffer is popped into a window (if it's not already in some |
| 1296 | window) so you can see it, and a comment of the form |
| 1297 | |
| 1298 | \t## working on region in file <name>... |
| 1299 | |
| 1300 | is inserted at the end. See also the command `py-clear-queue'." |
| 1301 | (interactive "r\nP") |
| 1302 | (or (< start end) |
| 1303 | (error "Region is empty")) |
| 1304 | (let* ((proc (get-process "Python")) |
Barry Warsaw | 5e21cb0 | 1997-11-05 18:41:11 +0000 | [diff] [blame] | 1305 | (temp (if (memq 'broken-temp-names py-emacs-features) |
| 1306 | (prog1 |
| 1307 | (format "python-%d" py-serial-number) |
| 1308 | (setq py-serial-number (1+ py-serial-number))) |
Barry Warsaw | 2f32fbb | 1998-02-25 16:45:43 +0000 | [diff] [blame] | 1309 | (make-temp-name "python-"))) |
| 1310 | (file (expand-file-name temp py-temp-directory))) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1311 | (write-region start end file nil 'nomsg) |
| 1312 | (cond |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 1313 | ;; always run the code in its own asynchronous subprocess |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1314 | (async |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1315 | (let* ((buf (generate-new-buffer-name py-output-buffer))) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1316 | (start-process "Python" buf py-python-command "-u" file) |
| 1317 | (pop-to-buffer buf) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1318 | (py-postprocess-output-buffer buf) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1319 | )) |
| 1320 | ;; if the Python interpreter shell is running, queue it up for |
| 1321 | ;; execution there. |
| 1322 | (proc |
| 1323 | ;; use the existing python shell |
| 1324 | (if (not py-file-queue) |
| 1325 | (py-execute-file proc file) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1326 | (message "File %s queued for execution" file)) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1327 | (push file py-file-queue) |
| 1328 | (setq py-exception-buffer (cons file (current-buffer)))) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1329 | (t |
| 1330 | ;; otherwise either run it synchronously in a subprocess |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1331 | (shell-command-on-region start end py-python-command py-output-buffer) |
Barry Warsaw | 512af04 | 1998-03-25 23:27:17 +0000 | [diff] [blame] | 1332 | ;; shell-command-on-region kills the output buffer if it never |
| 1333 | ;; existed and there's no output from the command |
| 1334 | (if (not (get-buffer py-output-buffer)) |
| 1335 | (message "No output.") |
| 1336 | (setq py-exception-buffer (current-buffer)) |
Barry Warsaw | f9b99f4 | 1998-03-26 16:08:59 +0000 | [diff] [blame] | 1337 | (let ((err-p (py-postprocess-output-buffer py-output-buffer))) |
| 1338 | (pop-to-buffer py-output-buffer) |
| 1339 | (if err-p |
| 1340 | (pop-to-buffer py-exception-buffer))) |
Barry Warsaw | 512af04 | 1998-03-25 23:27:17 +0000 | [diff] [blame] | 1341 | )) |
| 1342 | ))) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1343 | |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame^] | 1344 | |
| 1345 | ;; Code execution commands |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1346 | (defun py-execute-buffer (&optional async) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1347 | "Send the contents of the buffer to a Python interpreter. |
Barry Warsaw | c0ecb53 | 1998-01-20 21:43:34 +0000 | [diff] [blame] | 1348 | If the file local variable `py-master-file' is non-nil, execute the |
| 1349 | named file instead of the buffer's file. |
| 1350 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1351 | If there is a *Python* process buffer it is used. If a clipping |
| 1352 | restriction is in effect, only the accessible portion of the buffer is |
| 1353 | sent. A trailing newline will be supplied if needed. |
| 1354 | |
| 1355 | See the `\\[py-execute-region]' docs for an account of some subtleties." |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1356 | (interactive "P") |
Barry Warsaw | c0ecb53 | 1998-01-20 21:43:34 +0000 | [diff] [blame] | 1357 | (if py-master-file |
| 1358 | (let* ((filename (expand-file-name py-master-file)) |
| 1359 | (buffer (or (get-file-buffer filename) |
| 1360 | (find-file-noselect filename)))) |
| 1361 | (set-buffer buffer))) |
Barry Warsaw | a97a3f3 | 1997-11-04 18:47:06 +0000 | [diff] [blame] | 1362 | (py-execute-region (point-min) (point-max) async)) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1363 | |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame^] | 1364 | (defun py-execute-import-or-reload (&optional async) |
| 1365 | "Import the current buffer's file in a Python interpreter. |
| 1366 | |
| 1367 | If the file has already been imported, then do reload instead to get |
| 1368 | the latest version. If the file's name does not end in \".py\", then |
| 1369 | do execfile instead. If the current buffer is not visiting a file, do |
| 1370 | `py-execute-buffer' instead. If the file local variable |
| 1371 | `py-master-file' is non-nil, import or reload the named file instead |
| 1372 | of the buffer's file. The file is saved if necessary. |
| 1373 | |
| 1374 | See the `\\[py-execute-region]' docs for an account of some subtleties. |
| 1375 | |
| 1376 | This is may be preferable to `\\[py-execute-buffer]' because: |
| 1377 | |
| 1378 | - Definitions stay in their module rather than appearing at top |
| 1379 | level, where they would clutter the global namespace and not affect |
| 1380 | uses of qualified names (MODULE.NAME). |
| 1381 | |
| 1382 | - The Python debugger gets line number information about the functions." |
| 1383 | (interactive "P") |
| 1384 | ;; Check file local variable py-master-file |
| 1385 | (if py-master-file |
| 1386 | (let* ((filename (expand-file-name py-master-file)) |
| 1387 | (buffer (or (get-file-buffer filename) |
| 1388 | (find-file-noselect filename)))) |
| 1389 | (set-buffer buffer))) |
| 1390 | (let ((file (buffer-file-name (current-buffer)))) |
| 1391 | (if file |
| 1392 | (progn |
| 1393 | ; (if (buffer-modified-p) |
| 1394 | ; ;; avoid message if unmodified |
| 1395 | ; (save-buffer)) |
| 1396 | (py-execute-string |
| 1397 | (if (string-match "\\.py$" file) |
| 1398 | (let ((f (file-name-sans-extension |
| 1399 | (file-name-nondirectory file)))) |
| 1400 | (format "if globals().has_key('%s'):\n reload(%s)\nelse:\n import %s\n" |
| 1401 | f f f)) |
| 1402 | (format "execfile('%s')\n" file)) |
| 1403 | async)) |
| 1404 | ;; else |
| 1405 | (py-execute-buffer async)))) |
| 1406 | |
| 1407 | |
| 1408 | (defun py-execute-def-or-class (&optional async) |
| 1409 | "Send the current function or class definition to a Python interpreter. |
| 1410 | |
| 1411 | If there is a *Python* process buffer it is used. |
| 1412 | |
| 1413 | See the `\\[py-execute-region]' docs for an account of some subtleties." |
| 1414 | (interactive "P") |
| 1415 | (save-excursion |
| 1416 | (py-mark-def-or-class) |
| 1417 | ;; mark is before point |
| 1418 | (py-execute-region (mark) (point) async))) |
| 1419 | |
| 1420 | |
| 1421 | (defun py-execute-string (string &optional async) |
| 1422 | "Send the argument string to a Python interpreter. |
| 1423 | |
| 1424 | If there is a *Python* process buffer it is used. |
| 1425 | |
| 1426 | See the `\\[py-execute-region]' docs for an account of some subtleties." |
| 1427 | (interactive "sExecute Python command: ") |
| 1428 | (save-excursion |
| 1429 | (set-buffer (get-buffer-create |
| 1430 | (generate-new-buffer-name " *Python Command*"))) |
| 1431 | (insert string) |
| 1432 | (py-execute-region (point-min) (point-max) async))) |
| 1433 | |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1434 | |
| 1435 | |
| 1436 | (defun py-jump-to-exception (file line) |
| 1437 | (let ((buffer (cond ((string-equal file "<stdin>") |
Barry Warsaw | ebc7b7a | 1998-05-19 15:01:06 +0000 | [diff] [blame] | 1438 | (if (consp py-exception-buffer) |
| 1439 | (cdr py-exception-buffer) |
| 1440 | py-exception-buffer)) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1441 | ((and (consp py-exception-buffer) |
| 1442 | (string-equal file (car py-exception-buffer))) |
| 1443 | (cdr py-exception-buffer)) |
| 1444 | ((py-safe (find-file-noselect file))) |
| 1445 | ;; could not figure out what file the exception |
| 1446 | ;; is pointing to, so prompt for it |
| 1447 | (t (find-file (read-file-name "Exception file: " |
| 1448 | nil |
| 1449 | file t)))))) |
| 1450 | (pop-to-buffer buffer) |
Barry Warsaw | ebc7b7a | 1998-05-19 15:01:06 +0000 | [diff] [blame] | 1451 | ;; Force Python mode |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame^] | 1452 | (if (not (eq major-mode 'python-mode)) |
Barry Warsaw | ebc7b7a | 1998-05-19 15:01:06 +0000 | [diff] [blame] | 1453 | (python-mode)) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1454 | (goto-line line) |
| 1455 | (message "Jumping to exception in file %s on line %d" file line))) |
| 1456 | |
| 1457 | (defun py-mouseto-exception (event) |
| 1458 | (interactive "e") |
| 1459 | (cond |
| 1460 | ((fboundp 'event-point) |
| 1461 | ;; XEmacs |
| 1462 | (let* ((point (event-point event)) |
| 1463 | (buffer (event-buffer event)) |
| 1464 | (e (and point buffer (extent-at point buffer 'py-exc-info))) |
| 1465 | (info (and e (extent-property e 'py-exc-info)))) |
| 1466 | (message "Event point: %d, info: %s" point info) |
| 1467 | (and info |
| 1468 | (py-jump-to-exception (car info) (cdr info))) |
| 1469 | )) |
| 1470 | ;; Emacs -- Please port this! |
| 1471 | )) |
| 1472 | |
| 1473 | (defun py-goto-exception () |
| 1474 | "Go to the line indicated by the traceback." |
| 1475 | (interactive) |
| 1476 | (let (file line) |
| 1477 | (save-excursion |
| 1478 | (beginning-of-line) |
| 1479 | (if (looking-at py-traceback-line-re) |
| 1480 | (setq file (match-string 1) |
| 1481 | line (string-to-int (match-string 2))))) |
| 1482 | (if (not file) |
| 1483 | (error "Not on a traceback line.")) |
| 1484 | (py-jump-to-exception file line))) |
| 1485 | |
| 1486 | (defun py-find-next-exception (start buffer searchdir errwhere) |
| 1487 | ;; Go to start position in buffer, search in the specified |
| 1488 | ;; direction, and jump to the exception found. If at the end of the |
| 1489 | ;; exception, print error message |
| 1490 | (let (file line) |
| 1491 | (save-excursion |
| 1492 | (set-buffer buffer) |
| 1493 | (goto-char (py-point start)) |
| 1494 | (if (funcall searchdir py-traceback-line-re nil t) |
| 1495 | (setq file (match-string 1) |
| 1496 | line (string-to-int (match-string 2))))) |
| 1497 | (if (and file line) |
| 1498 | (py-jump-to-exception file line) |
| 1499 | (error "%s of traceback" errwhere)))) |
| 1500 | |
| 1501 | (defun py-down-exception (&optional bottom) |
| 1502 | "Go to the next line down in the traceback. |
| 1503 | With optional \\[universal-argument], jump to the bottom (innermost) |
| 1504 | exception in the exception stack." |
| 1505 | (interactive "P") |
| 1506 | (let* ((proc (get-process "Python")) |
| 1507 | (buffer (if proc "*Python*" py-output-buffer))) |
| 1508 | (if bottom |
| 1509 | (py-find-next-exception 'eob buffer 're-search-backward "Bottom") |
| 1510 | (py-find-next-exception 'eol buffer 're-search-forward "Bottom")))) |
| 1511 | |
| 1512 | (defun py-up-exception (&optional top) |
| 1513 | "Go to the previous line up in the traceback. |
| 1514 | With optional \\[universal-argument], jump to the top (outermost) |
| 1515 | exception in the exception stack." |
| 1516 | (interactive "P") |
| 1517 | (let* ((proc (get-process "Python")) |
| 1518 | (buffer (if proc "*Python*" py-output-buffer))) |
| 1519 | (if top |
| 1520 | (py-find-next-exception 'bob buffer 're-search-forward "Top") |
Barry Warsaw | ffbc17d | 1997-11-27 20:08:14 +0000 | [diff] [blame] | 1521 | (py-find-next-exception 'bol buffer 're-search-backward "Top")))) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1522 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1523 | |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 1524 | ;; Electric deletion |
| 1525 | (defun py-electric-backspace (arg) |
| 1526 | "Deletes preceding character or levels of indentation. |
| 1527 | Deletion is performed by calling the function in `py-backspace-function' |
Barry Warsaw | b053993 | 1996-12-17 22:05:07 +0000 | [diff] [blame] | 1528 | with a single argument (the number of characters to delete). |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1529 | |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 1530 | If point is at the leftmost column, deletes the preceding newline. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1531 | |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 1532 | Otherwise, if point is at the leftmost non-whitespace character of a |
| 1533 | line that is neither a continuation line nor a non-indenting comment |
| 1534 | line, or if point is at the end of a blank line, this command reduces |
| 1535 | the indentation to match that of the line that opened the current |
| 1536 | block of code. The line that opened the block is displayed in the |
| 1537 | echo area to help you keep track of where you are. With numeric arg, |
| 1538 | outdents that many blocks (but not past column zero). |
| 1539 | |
| 1540 | Otherwise the preceding character is deleted, converting a tab to |
| 1541 | spaces if needed so that only a single column position is deleted. |
| 1542 | Numeric argument deletes that many preceding characters." |
Barry Warsaw | 0397073 | 1996-07-03 23:12:52 +0000 | [diff] [blame] | 1543 | (interactive "*p") |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1544 | (if (or (/= (current-indentation) (current-column)) |
| 1545 | (bolp) |
| 1546 | (py-continuation-line-p) |
Barry Warsaw | 6e527d2 | 1996-08-01 15:57:48 +0000 | [diff] [blame] | 1547 | (not py-honor-comment-indentation) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1548 | (looking-at "#[^ \t\n]")) ; non-indenting # |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 1549 | (funcall py-backspace-function arg) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1550 | ;; else indent the same as the colon line that opened the block |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1551 | ;; force non-blank so py-goto-block-up doesn't ignore it |
| 1552 | (insert-char ?* 1) |
| 1553 | (backward-char) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1554 | (let ((base-indent 0) ; indentation of base line |
| 1555 | (base-text "") ; and text of base line |
| 1556 | (base-found-p nil)) |
Barry Warsaw | 0397073 | 1996-07-03 23:12:52 +0000 | [diff] [blame] | 1557 | (save-excursion |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 1558 | (while (< 0 arg) |
Barry Warsaw | 0397073 | 1996-07-03 23:12:52 +0000 | [diff] [blame] | 1559 | (condition-case nil ; in case no enclosing block |
| 1560 | (progn |
| 1561 | (py-goto-block-up 'no-mark) |
| 1562 | (setq base-indent (current-indentation) |
| 1563 | base-text (py-suck-up-leading-text) |
| 1564 | base-found-p t)) |
| 1565 | (error nil)) |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 1566 | (setq arg (1- arg)))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1567 | (delete-char 1) ; toss the dummy character |
| 1568 | (delete-horizontal-space) |
| 1569 | (indent-to base-indent) |
| 1570 | (if base-found-p |
| 1571 | (message "Closes block: %s" base-text))))) |
| 1572 | |
Barry Warsaw | fc8a01f | 1995-03-09 16:07:29 +0000 | [diff] [blame] | 1573 | |
Barry Warsaw | 6d48c4a | 1997-11-04 19:21:50 +0000 | [diff] [blame] | 1574 | (defun py-electric-delete (arg) |
| 1575 | "Deletes preceding or following character or levels of whitespace. |
| 1576 | |
| 1577 | The behavior of this function depends on the variable |
| 1578 | `delete-key-deletes-forward'. If this variable is nil (or does not |
| 1579 | exist, as in older Emacsen), then this function behaves identical to |
| 1580 | \\[c-electric-backspace]. |
| 1581 | |
| 1582 | If `delete-key-deletes-forward' is non-nil and is supported in your |
| 1583 | Emacs, then deletion occurs in the forward direction, by calling the |
| 1584 | function in `py-delete-function'." |
| 1585 | (interactive "*p") |
| 1586 | (if (and (boundp 'delete-key-deletes-forward) |
| 1587 | delete-key-deletes-forward) |
| 1588 | (funcall py-delete-function arg) |
| 1589 | ;; else |
| 1590 | (py-electric-backspace arg))) |
| 1591 | |
| 1592 | ;; required for pending-del and delsel modes |
| 1593 | (put 'py-electric-backspace 'delete-selection 'supersede) ;delsel |
| 1594 | (put 'py-electric-backspace 'pending-delete 'supersede) ;pending-del |
| 1595 | (put 'py-electric-delete 'delete-selection 'supersede) ;delsel |
| 1596 | (put 'py-electric-delete 'pending-delete 'supersede) ;pending-del |
| 1597 | |
| 1598 | |
| 1599 | |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 1600 | (defun py-indent-line (&optional arg) |
| 1601 | "Fix the indentation of the current line according to Python rules. |
| 1602 | With \\[universal-argument], ignore outdenting rules for block |
| 1603 | closing statements (e.g. return, raise, break, continue, pass) |
| 1604 | |
| 1605 | This function is normally bound to `indent-line-function' so |
| 1606 | \\[indent-for-tab-command] will call it." |
| 1607 | (interactive "P") |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1608 | (let* ((ci (current-indentation)) |
| 1609 | (move-to-indentation-p (<= (current-column) ci)) |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 1610 | (need (py-compute-indentation (not arg)))) |
Barry Warsaw | 4f009fb | 1995-03-14 20:53:08 +0000 | [diff] [blame] | 1611 | ;; see if we need to outdent |
Barry Warsaw | 3874a3d | 1995-03-14 22:05:53 +0000 | [diff] [blame] | 1612 | (if (py-outdent-p) |
Barry Warsaw | 0012c1e | 1995-03-14 16:32:55 +0000 | [diff] [blame] | 1613 | (setq need (- need py-indent-offset))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1614 | (if (/= ci need) |
| 1615 | (save-excursion |
| 1616 | (beginning-of-line) |
| 1617 | (delete-horizontal-space) |
| 1618 | (indent-to need))) |
| 1619 | (if move-to-indentation-p (back-to-indentation)))) |
| 1620 | |
| 1621 | (defun py-newline-and-indent () |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1622 | "Strives to act like the Emacs `newline-and-indent'. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1623 | This is just `strives to' because correct indentation can't be computed |
| 1624 | from scratch for Python code. In general, deletes the whitespace before |
| 1625 | point, inserts a newline, and takes an educated guess as to how you want |
| 1626 | the new line indented." |
| 1627 | (interactive) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1628 | (let ((ci (current-indentation))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1629 | (if (< ci (current-column)) ; if point beyond indentation |
| 1630 | (newline-and-indent) |
| 1631 | ;; else try to act like newline-and-indent "normally" acts |
| 1632 | (beginning-of-line) |
| 1633 | (insert-char ?\n 1) |
| 1634 | (move-to-column ci)))) |
| 1635 | |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 1636 | (defun py-compute-indentation (honor-block-close-p) |
| 1637 | ;; implements all the rules for indentation computation. when |
| 1638 | ;; honor-block-close-p is non-nil, statements such as return, raise, |
| 1639 | ;; break, continue, and pass force one level of outdenting. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1640 | (save-excursion |
Barry Warsaw | f64b405 | 1998-02-12 16:52:14 +0000 | [diff] [blame] | 1641 | (beginning-of-line) |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1642 | (let* ((bod (py-point 'bod)) |
Barry Warsaw | 3b3ff4e | 1997-11-26 20:58:48 +0000 | [diff] [blame] | 1643 | (pps (parse-partial-sexp bod (point))) |
| 1644 | (boipps (parse-partial-sexp bod (py-point 'boi)))) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 1645 | (cond |
Barry Warsaw | a0ee8cd | 1997-11-26 01:04:44 +0000 | [diff] [blame] | 1646 | ;; are we inside a multi-line string or comment? |
Barry Warsaw | 3b3ff4e | 1997-11-26 20:58:48 +0000 | [diff] [blame] | 1647 | ((or (and (nth 3 pps) (nth 3 boipps)) |
| 1648 | (and (nth 4 pps) (nth 4 boipps))) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 1649 | (save-excursion |
| 1650 | (if (not py-align-multiline-strings-p) 0 |
| 1651 | ;; skip back over blank & non-indenting comment lines |
| 1652 | ;; note: will skip a blank or non-indenting comment line |
| 1653 | ;; that happens to be a continuation line too |
| 1654 | (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move) |
| 1655 | (back-to-indentation) |
| 1656 | (current-column)))) |
| 1657 | ;; are we on a continuation line? |
| 1658 | ((py-continuation-line-p) |
| 1659 | (let ((startpos (point)) |
| 1660 | (open-bracket-pos (py-nesting-level)) |
Barry Warsaw | ce60bc7 | 1996-08-01 18:17:14 +0000 | [diff] [blame] | 1661 | endpos searching found state) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 1662 | (if open-bracket-pos |
| 1663 | (progn |
| 1664 | ;; align with first item in list; else a normal |
| 1665 | ;; indent beyond the line with the open bracket |
| 1666 | (goto-char (1+ open-bracket-pos)) ; just beyond bracket |
| 1667 | ;; is the first list item on the same line? |
| 1668 | (skip-chars-forward " \t") |
| 1669 | (if (null (memq (following-char) '(?\n ?# ?\\))) |
| 1670 | ; yes, so line up with it |
| 1671 | (current-column) |
| 1672 | ;; first list item on another line, or doesn't exist yet |
| 1673 | (forward-line 1) |
| 1674 | (while (and (< (point) startpos) |
| 1675 | (looking-at "[ \t]*[#\n\\\\]")) ; skip noise |
| 1676 | (forward-line 1)) |
Barry Warsaw | 92166d9 | 1998-04-01 21:59:41 +0000 | [diff] [blame] | 1677 | (if (and (< (point) startpos) |
| 1678 | (/= startpos |
| 1679 | (save-excursion |
| 1680 | (goto-char (1+ open-bracket-pos)) |
Barry Warsaw | 77d1fce | 1998-04-16 20:04:59 +0000 | [diff] [blame] | 1681 | (forward-comment (point-max)) |
Barry Warsaw | 92166d9 | 1998-04-01 21:59:41 +0000 | [diff] [blame] | 1682 | (point)))) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 1683 | ;; again mimic the first list item |
| 1684 | (current-indentation) |
| 1685 | ;; else they're about to enter the first item |
| 1686 | (goto-char open-bracket-pos) |
| 1687 | (+ (current-indentation) py-indent-offset)))) |
| 1688 | |
| 1689 | ;; else on backslash continuation line |
| 1690 | (forward-line -1) |
| 1691 | (if (py-continuation-line-p) ; on at least 3rd line in block |
| 1692 | (current-indentation) ; so just continue the pattern |
| 1693 | ;; else started on 2nd line in block, so indent more. |
| 1694 | ;; if base line is an assignment with a start on a RHS, |
| 1695 | ;; indent to 2 beyond the leftmost "="; else skip first |
| 1696 | ;; chunk of non-whitespace characters on base line, + 1 more |
| 1697 | ;; column |
| 1698 | (end-of-line) |
| 1699 | (setq endpos (point) searching t) |
| 1700 | (back-to-indentation) |
| 1701 | (setq startpos (point)) |
| 1702 | ;; look at all "=" from left to right, stopping at first |
| 1703 | ;; one not nested in a list or string |
| 1704 | (while searching |
| 1705 | (skip-chars-forward "^=" endpos) |
| 1706 | (if (= (point) endpos) |
| 1707 | (setq searching nil) |
| 1708 | (forward-char 1) |
| 1709 | (setq state (parse-partial-sexp startpos (point))) |
| 1710 | (if (and (zerop (car state)) ; not in a bracket |
| 1711 | (null (nth 3 state))) ; & not in a string |
| 1712 | (progn |
| 1713 | (setq searching nil) ; done searching in any case |
| 1714 | (setq found |
| 1715 | (not (or |
| 1716 | (eq (following-char) ?=) |
| 1717 | (memq (char-after (- (point) 2)) |
| 1718 | '(?< ?> ?!))))))))) |
| 1719 | (if (or (not found) ; not an assignment |
| 1720 | (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash> |
| 1721 | (progn |
| 1722 | (goto-char startpos) |
| 1723 | (skip-chars-forward "^ \t\n"))) |
| 1724 | (1+ (current-column)))))) |
| 1725 | |
| 1726 | ;; not on a continuation line |
Barry Warsaw | a789171 | 1996-08-01 15:53:09 +0000 | [diff] [blame] | 1727 | ((bobp) (current-indentation)) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 1728 | |
Barry Warsaw | a789171 | 1996-08-01 15:53:09 +0000 | [diff] [blame] | 1729 | ;; Dfn: "Indenting comment line". A line containing only a |
| 1730 | ;; comment, but which is treated like a statement for |
| 1731 | ;; indentation calculation purposes. Such lines are only |
| 1732 | ;; treated specially by the mode; they are not treated |
| 1733 | ;; specially by the Python interpreter. |
| 1734 | |
| 1735 | ;; The rules for indenting comment lines are a line where: |
| 1736 | ;; - the first non-whitespace character is `#', and |
| 1737 | ;; - the character following the `#' is whitespace, and |
| 1738 | ;; - the line is outdented with respect to (i.e. to the left |
| 1739 | ;; of) the indentation of the preceding non-blank line. |
| 1740 | |
| 1741 | ;; The first non-blank line following an indenting comment |
| 1742 | ;; line is given the same amount of indentation as the |
| 1743 | ;; indenting comment line. |
| 1744 | |
| 1745 | ;; All other comment-only lines are ignored for indentation |
| 1746 | ;; purposes. |
| 1747 | |
| 1748 | ;; Are we looking at a comment-only line which is *not* an |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 1749 | ;; indenting comment line? If so, we assume that it's been |
Barry Warsaw | a789171 | 1996-08-01 15:53:09 +0000 | [diff] [blame] | 1750 | ;; placed at the desired indentation, so leave it alone. |
| 1751 | ;; Indenting comment lines are aligned as statements down |
| 1752 | ;; below. |
| 1753 | ((and (looking-at "[ \t]*#[^ \t\n]") |
| 1754 | ;; NOTE: this test will not be performed in older Emacsen |
| 1755 | (fboundp 'forward-comment) |
| 1756 | (<= (current-indentation) |
| 1757 | (save-excursion |
| 1758 | (forward-comment (- (point-max))) |
| 1759 | (current-indentation)))) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 1760 | (current-indentation)) |
| 1761 | |
| 1762 | ;; else indentation based on that of the statement that |
| 1763 | ;; precedes us; use the first line of that statement to |
| 1764 | ;; establish the base, in case the user forced a non-std |
| 1765 | ;; indentation for the continuation lines (if any) |
| 1766 | (t |
Barry Warsaw | c01c5c8 | 1995-09-14 18:49:11 +0000 | [diff] [blame] | 1767 | ;; skip back over blank & non-indenting comment lines note: |
| 1768 | ;; will skip a blank or non-indenting comment line that |
Barry Warsaw | fd0fb38 | 1996-03-04 17:15:40 +0000 | [diff] [blame] | 1769 | ;; happens to be a continuation line too. use fast Emacs 19 |
| 1770 | ;; function if it's there. |
Barry Warsaw | 6d62775 | 1996-03-06 18:41:38 +0000 | [diff] [blame] | 1771 | (if (and (eq py-honor-comment-indentation nil) |
Barry Warsaw | 33d6ec0 | 1996-03-05 16:28:07 +0000 | [diff] [blame] | 1772 | (fboundp 'forward-comment)) |
Barry Warsaw | fd0fb38 | 1996-03-04 17:15:40 +0000 | [diff] [blame] | 1773 | (forward-comment (- (point-max))) |
Barry Warsaw | 6d62775 | 1996-03-06 18:41:38 +0000 | [diff] [blame] | 1774 | (let (done) |
| 1775 | (while (not done) |
| 1776 | (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" |
| 1777 | nil 'move) |
| 1778 | (setq done (or (eq py-honor-comment-indentation t) |
| 1779 | (bobp) |
| 1780 | (/= (following-char) ?#) |
| 1781 | (not (zerop (current-column))))) |
| 1782 | ))) |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 1783 | ;; if we landed inside a string, go to the beginning of that |
| 1784 | ;; string. this handles triple quoted, multi-line spanning |
| 1785 | ;; strings. |
Barry Warsaw | 585f733 | 1998-04-01 21:13:51 +0000 | [diff] [blame] | 1786 | (let* ((delim (nth 3 (parse-partial-sexp bod (point)))) |
Barry Warsaw | f64b405 | 1998-02-12 16:52:14 +0000 | [diff] [blame] | 1787 | (skip (and delim (make-string 1 delim)))) |
| 1788 | (when skip |
| 1789 | (save-excursion |
| 1790 | (py-safe (search-backward skip)) |
| 1791 | (if (and (eq (char-before) delim) |
| 1792 | (eq (char-before (1- (point))) delim)) |
| 1793 | (setq skip (make-string 3 delim)))) |
| 1794 | ;; we're looking at a triple-quoted string |
| 1795 | (py-safe (search-backward skip)))) |
Barry Warsaw | c210e69 | 1998-01-20 22:52:56 +0000 | [diff] [blame] | 1796 | ;; now skip backward over continued lines |
Barry Warsaw | 095e9c6 | 1995-09-19 20:01:42 +0000 | [diff] [blame] | 1797 | (py-goto-initial-line) |
Barry Warsaw | f831d81 | 1996-07-31 20:42:59 +0000 | [diff] [blame] | 1798 | (+ (current-indentation) |
| 1799 | (if (py-statement-opens-block-p) |
| 1800 | py-indent-offset |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 1801 | (if (and honor-block-close-p (py-statement-closes-block-p)) |
Barry Warsaw | f831d81 | 1996-07-31 20:42:59 +0000 | [diff] [blame] | 1802 | (- py-indent-offset) |
| 1803 | 0))) |
| 1804 | ))))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1805 | |
| 1806 | (defun py-guess-indent-offset (&optional global) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1807 | "Guess a good value for, and change, `py-indent-offset'. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1808 | By default (without a prefix arg), makes a buffer-local copy of |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1809 | `py-indent-offset' with the new value. This will not affect any other |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1810 | Python buffers. With a prefix arg, changes the global value of |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1811 | `py-indent-offset'. This affects all Python buffers (that don't have |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1812 | their own buffer-local copy), both those currently existing and those |
| 1813 | created later in the Emacs session. |
| 1814 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1815 | Some people use a different value for `py-indent-offset' than you use. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1816 | There's no excuse for such foolishness, but sometimes you have to deal |
| 1817 | with their ugly code anyway. This function examines the file and sets |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1818 | `py-indent-offset' to what it thinks it was when they created the |
| 1819 | mess. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1820 | |
| 1821 | Specifically, it searches forward from the statement containing point, |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1822 | looking for a line that opens a block of code. `py-indent-offset' is |
| 1823 | set to the difference in indentation between that line and the Python |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1824 | statement following it. If the search doesn't succeed going forward, |
| 1825 | it's tried again going backward." |
| 1826 | (interactive "P") ; raw prefix arg |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1827 | (let (new-value |
| 1828 | (start (point)) |
Barry Warsaw | e908b6b | 1998-03-19 22:48:02 +0000 | [diff] [blame] | 1829 | (restart (point)) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1830 | (found nil) |
| 1831 | colon-indent) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1832 | (py-goto-initial-line) |
| 1833 | (while (not (or found (eobp))) |
Barry Warsaw | e908b6b | 1998-03-19 22:48:02 +0000 | [diff] [blame] | 1834 | (when (and (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move) |
| 1835 | (not (py-in-literal restart))) |
| 1836 | (setq restart (point)) |
| 1837 | (py-goto-initial-line) |
| 1838 | (if (py-statement-opens-block-p) |
| 1839 | (setq found t) |
| 1840 | (goto-char restart)))) |
| 1841 | (unless found |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1842 | (goto-char start) |
| 1843 | (py-goto-initial-line) |
| 1844 | (while (not (or found (bobp))) |
Barry Warsaw | e908b6b | 1998-03-19 22:48:02 +0000 | [diff] [blame] | 1845 | (setq found (and |
| 1846 | (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move) |
| 1847 | (or (py-goto-initial-line) t) ; always true -- side effect |
| 1848 | (py-statement-opens-block-p))))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1849 | (setq colon-indent (current-indentation) |
| 1850 | found (and found (zerop (py-next-statement 1))) |
| 1851 | new-value (- (current-indentation) colon-indent)) |
| 1852 | (goto-char start) |
Barry Warsaw | e908b6b | 1998-03-19 22:48:02 +0000 | [diff] [blame] | 1853 | (if (not found) |
| 1854 | (error "Sorry, couldn't guess a value for py-indent-offset") |
| 1855 | (funcall (if global 'kill-local-variable 'make-local-variable) |
| 1856 | 'py-indent-offset) |
| 1857 | (setq py-indent-offset new-value) |
| 1858 | (message "%s value of py-indent-offset set to %d" |
| 1859 | (if global "Global" "Local") |
| 1860 | py-indent-offset)) |
| 1861 | )) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1862 | |
| 1863 | (defun py-shift-region (start end count) |
| 1864 | (save-excursion |
| 1865 | (goto-char end) (beginning-of-line) (setq end (point)) |
| 1866 | (goto-char start) (beginning-of-line) (setq start (point)) |
| 1867 | (indent-rigidly start end count))) |
| 1868 | |
| 1869 | (defun py-shift-region-left (start end &optional count) |
| 1870 | "Shift region of Python code to the left. |
| 1871 | The lines from the line containing the start of the current region up |
| 1872 | to (but not including) the line containing the end of the region are |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1873 | shifted to the left, by `py-indent-offset' columns. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1874 | |
| 1875 | If a prefix argument is given, the region is instead shifted by that |
Barry Warsaw | dea4a29 | 1996-07-03 22:59:12 +0000 | [diff] [blame] | 1876 | many columns. With no active region, outdent only the current line. |
| 1877 | You cannot outdent the region if any line is already at column zero." |
| 1878 | (interactive |
| 1879 | (let ((p (point)) |
| 1880 | (m (mark)) |
| 1881 | (arg current-prefix-arg)) |
| 1882 | (if m |
| 1883 | (list (min p m) (max p m) arg) |
| 1884 | (list p (save-excursion (forward-line 1) (point)) arg)))) |
| 1885 | ;; if any line is at column zero, don't shift the region |
| 1886 | (save-excursion |
| 1887 | (goto-char start) |
| 1888 | (while (< (point) end) |
| 1889 | (back-to-indentation) |
Barry Warsaw | 71e315b | 1996-07-23 15:03:16 +0000 | [diff] [blame] | 1890 | (if (and (zerop (current-column)) |
| 1891 | (not (looking-at "\\s *$"))) |
Barry Warsaw | dea4a29 | 1996-07-03 22:59:12 +0000 | [diff] [blame] | 1892 | (error "Region is at left edge.")) |
| 1893 | (forward-line 1))) |
| 1894 | (py-shift-region start end (- (prefix-numeric-value |
| 1895 | (or count py-indent-offset)))) |
| 1896 | (py-keep-region-active)) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1897 | |
| 1898 | (defun py-shift-region-right (start end &optional count) |
| 1899 | "Shift region of Python code to the right. |
| 1900 | The lines from the line containing the start of the current region up |
| 1901 | to (but not including) the line containing the end of the region are |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1902 | shifted to the right, by `py-indent-offset' columns. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1903 | |
| 1904 | If a prefix argument is given, the region is instead shifted by that |
Barry Warsaw | dea4a29 | 1996-07-03 22:59:12 +0000 | [diff] [blame] | 1905 | many columns. With no active region, indent only the current line." |
| 1906 | (interactive |
| 1907 | (let ((p (point)) |
| 1908 | (m (mark)) |
| 1909 | (arg current-prefix-arg)) |
| 1910 | (if m |
| 1911 | (list (min p m) (max p m) arg) |
| 1912 | (list p (save-excursion (forward-line 1) (point)) arg)))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1913 | (py-shift-region start end (prefix-numeric-value |
Barry Warsaw | dea4a29 | 1996-07-03 22:59:12 +0000 | [diff] [blame] | 1914 | (or count py-indent-offset))) |
| 1915 | (py-keep-region-active)) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1916 | |
| 1917 | (defun py-indent-region (start end &optional indent-offset) |
| 1918 | "Reindent a region of Python code. |
Barry Warsaw | 867a32a | 1996-03-07 18:30:26 +0000 | [diff] [blame] | 1919 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1920 | The lines from the line containing the start of the current region up |
| 1921 | to (but not including) the line containing the end of the region are |
| 1922 | reindented. If the first line of the region has a non-whitespace |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1923 | character in the first column, the first line is left alone and the |
| 1924 | rest of the region is reindented with respect to it. Else the entire |
Barry Warsaw | 867a32a | 1996-03-07 18:30:26 +0000 | [diff] [blame] | 1925 | region is reindented with respect to the (closest code or indenting |
| 1926 | comment) statement immediately preceding the region. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1927 | |
| 1928 | This is useful when code blocks are moved or yanked, when enclosing |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1929 | control structures are introduced or removed, or to reformat code |
| 1930 | using a new value for the indentation offset. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1931 | |
| 1932 | If a numeric prefix argument is given, it will be used as the value of |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1933 | the indentation offset. Else the value of `py-indent-offset' will be |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1934 | used. |
| 1935 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1936 | Warning: The region must be consistently indented before this function |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1937 | is called! This function does not compute proper indentation from |
| 1938 | scratch (that's impossible in Python), it merely adjusts the existing |
| 1939 | indentation to be correct in context. |
| 1940 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1941 | Warning: This function really has no idea what to do with |
| 1942 | non-indenting comment lines, and shifts them as if they were indenting |
| 1943 | comment lines. Fixing this appears to require telepathy. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1944 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1945 | Special cases: whitespace is deleted from blank lines; continuation |
| 1946 | lines are shifted by the same amount their initial line was shifted, |
| 1947 | in order to preserve their relative indentation with respect to their |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1948 | initial line; and comment lines beginning in column 1 are ignored." |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1949 | (interactive "*r\nP") ; region; raw prefix arg |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1950 | (save-excursion |
| 1951 | (goto-char end) (beginning-of-line) (setq end (point-marker)) |
| 1952 | (goto-char start) (beginning-of-line) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1953 | (let ((py-indent-offset (prefix-numeric-value |
| 1954 | (or indent-offset py-indent-offset))) |
| 1955 | (indents '(-1)) ; stack of active indent levels |
| 1956 | (target-column 0) ; column to which to indent |
| 1957 | (base-shifted-by 0) ; amount last base line was shifted |
| 1958 | (indent-base (if (looking-at "[ \t\n]") |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 1959 | (py-compute-indentation t) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1960 | 0)) |
| 1961 | ci) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1962 | (while (< (point) end) |
| 1963 | (setq ci (current-indentation)) |
| 1964 | ;; figure out appropriate target column |
| 1965 | (cond |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1966 | ((or (eq (following-char) ?#) ; comment in column 1 |
| 1967 | (looking-at "[ \t]*$")) ; entirely blank |
| 1968 | (setq target-column 0)) |
| 1969 | ((py-continuation-line-p) ; shift relative to base line |
| 1970 | (setq target-column (+ ci base-shifted-by))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 1971 | (t ; new base line |
| 1972 | (if (> ci (car indents)) ; going deeper; push it |
| 1973 | (setq indents (cons ci indents)) |
| 1974 | ;; else we should have seen this indent before |
| 1975 | (setq indents (memq ci indents)) ; pop deeper indents |
| 1976 | (if (null indents) |
| 1977 | (error "Bad indentation in region, at line %d" |
| 1978 | (save-restriction |
| 1979 | (widen) |
| 1980 | (1+ (count-lines 1 (point))))))) |
| 1981 | (setq target-column (+ indent-base |
| 1982 | (* py-indent-offset |
| 1983 | (- (length indents) 2)))) |
| 1984 | (setq base-shifted-by (- target-column ci)))) |
| 1985 | ;; shift as needed |
| 1986 | (if (/= ci target-column) |
| 1987 | (progn |
| 1988 | (delete-horizontal-space) |
| 1989 | (indent-to target-column))) |
| 1990 | (forward-line 1)))) |
| 1991 | (set-marker end nil)) |
| 1992 | |
Barry Warsaw | a789171 | 1996-08-01 15:53:09 +0000 | [diff] [blame] | 1993 | (defun py-comment-region (beg end &optional arg) |
| 1994 | "Like `comment-region' but uses double hash (`#') comment starter." |
| 1995 | (interactive "r\nP") |
Barry Warsaw | 3fcaf61 | 1996-08-01 20:11:51 +0000 | [diff] [blame] | 1996 | (let ((comment-start py-block-comment-prefix)) |
Barry Warsaw | a789171 | 1996-08-01 15:53:09 +0000 | [diff] [blame] | 1997 | (comment-region beg end arg))) |
| 1998 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 1999 | |
| 2000 | ;; Functions for moving point |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2001 | (defun py-previous-statement (count) |
| 2002 | "Go to the start of previous Python statement. |
| 2003 | If the statement at point is the i'th Python statement, goes to the |
| 2004 | start of statement i-COUNT. If there is no such statement, goes to the |
| 2005 | first statement. Returns count of statements left to move. |
| 2006 | `Statements' do not include blank, comment, or continuation lines." |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2007 | (interactive "p") ; numeric prefix arg |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2008 | (if (< count 0) (py-next-statement (- count)) |
| 2009 | (py-goto-initial-line) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2010 | (let (start) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2011 | (while (and |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2012 | (setq start (point)) ; always true -- side effect |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2013 | (> count 0) |
| 2014 | (zerop (forward-line -1)) |
| 2015 | (py-goto-statement-at-or-above)) |
| 2016 | (setq count (1- count))) |
| 2017 | (if (> count 0) (goto-char start))) |
| 2018 | count)) |
| 2019 | |
| 2020 | (defun py-next-statement (count) |
| 2021 | "Go to the start of next Python statement. |
| 2022 | If the statement at point is the i'th Python statement, goes to the |
| 2023 | start of statement i+COUNT. If there is no such statement, goes to the |
| 2024 | last statement. Returns count of statements left to move. `Statements' |
| 2025 | do not include blank, comment, or continuation lines." |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2026 | (interactive "p") ; numeric prefix arg |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2027 | (if (< count 0) (py-previous-statement (- count)) |
| 2028 | (beginning-of-line) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2029 | (let (start) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2030 | (while (and |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2031 | (setq start (point)) ; always true -- side effect |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2032 | (> count 0) |
| 2033 | (py-goto-statement-below)) |
| 2034 | (setq count (1- count))) |
| 2035 | (if (> count 0) (goto-char start))) |
| 2036 | count)) |
| 2037 | |
| 2038 | (defun py-goto-block-up (&optional nomark) |
| 2039 | "Move up to start of current block. |
| 2040 | Go to the statement that starts the smallest enclosing block; roughly |
| 2041 | speaking, this will be the closest preceding statement that ends with a |
| 2042 | colon and is indented less than the statement you started on. If |
| 2043 | successful, also sets the mark to the starting point. |
| 2044 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2045 | `\\[py-mark-block]' can be used afterward to mark the whole code |
| 2046 | block, if desired. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2047 | |
| 2048 | If called from a program, the mark will not be set if optional argument |
| 2049 | NOMARK is not nil." |
| 2050 | (interactive) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2051 | (let ((start (point)) |
| 2052 | (found nil) |
| 2053 | initial-indent) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2054 | (py-goto-initial-line) |
| 2055 | ;; if on blank or non-indenting comment line, use the preceding stmt |
| 2056 | (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)") |
| 2057 | (progn |
| 2058 | (py-goto-statement-at-or-above) |
| 2059 | (setq found (py-statement-opens-block-p)))) |
| 2060 | ;; search back for colon line indented less |
| 2061 | (setq initial-indent (current-indentation)) |
| 2062 | (if (zerop initial-indent) |
| 2063 | ;; force fast exit |
| 2064 | (goto-char (point-min))) |
| 2065 | (while (not (or found (bobp))) |
| 2066 | (setq found |
| 2067 | (and |
| 2068 | (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move) |
| 2069 | (or (py-goto-initial-line) t) ; always true -- side effect |
| 2070 | (< (current-indentation) initial-indent) |
| 2071 | (py-statement-opens-block-p)))) |
| 2072 | (if found |
| 2073 | (progn |
| 2074 | (or nomark (push-mark start)) |
| 2075 | (back-to-indentation)) |
| 2076 | (goto-char start) |
| 2077 | (error "Enclosing block not found")))) |
| 2078 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2079 | (defun py-beginning-of-def-or-class (&optional class count) |
| 2080 | "Move point to start of `def' or `class'. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2081 | |
| 2082 | Searches back for the closest preceding `def'. If you supply a prefix |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2083 | arg, looks for a `class' instead. The docs below assume the `def' |
| 2084 | case; just substitute `class' for `def' for the other case. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2085 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2086 | When second optional argument is given programmatically, move to the |
| 2087 | COUNTth start of `def'. |
| 2088 | |
| 2089 | If point is in a `def' statement already, and after the `d', simply |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2090 | moves point to the start of the statement. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2091 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2092 | Otherwise (i.e. when point is not in a `def' statement, or at or |
| 2093 | before the `d' of a `def' statement), searches for the closest |
| 2094 | preceding `def' statement, and leaves point at its start. If no such |
| 2095 | statement can be found, leaves point at the start of the buffer. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2096 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2097 | Returns t iff a `def' statement is found by these rules. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2098 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2099 | Note that doing this command repeatedly will take you closer to the |
| 2100 | start of the buffer each time. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2101 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2102 | If you want to mark the current `def', see `\\[py-mark-def-or-class]'." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2103 | (interactive "P") ; raw prefix arg |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2104 | (if (not count) |
| 2105 | (setq count 1)) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2106 | (let ((at-or-before-p (<= (current-column) (current-indentation))) |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2107 | (start-of-line (goto-char (py-point 'bol))) |
| 2108 | (start-of-stmt (goto-char (py-point 'bos))) |
| 2109 | (start-re (if class |
| 2110 | "^[ \t]*class\\>" |
| 2111 | "^[ \t]*def\\>")) |
| 2112 | ) |
| 2113 | ;; searching backward |
| 2114 | (if (and (< 0 count) |
| 2115 | (or (/= start-of-stmt start-of-line) |
| 2116 | (not at-or-before-p))) |
| 2117 | (end-of-line)) |
| 2118 | ;; search forward |
| 2119 | (if (and (> 0 count) |
| 2120 | (zerop (current-column)) |
| 2121 | (looking-at start-re)) |
| 2122 | (end-of-line)) |
| 2123 | (re-search-backward start-re nil 'move count) |
| 2124 | (goto-char (match-beginning 0)))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2125 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2126 | ;; Backwards compatibility |
| 2127 | (defalias 'beginning-of-python-def-or-class 'py-beginning-of-def-or-class) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2128 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2129 | (defun py-end-of-def-or-class (&optional class count) |
| 2130 | "Move point beyond end of `def' or `class' body. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2131 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2132 | By default, looks for an appropriate `def'. If you supply a prefix |
| 2133 | arg, looks for a `class' instead. The docs below assume the `def' |
| 2134 | case; just substitute `class' for `def' for the other case. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2135 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2136 | When second optional argument is given programmatically, move to the |
| 2137 | COUNTth end of `def'. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2138 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2139 | If point is in a `def' statement already, this is the `def' we use. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2140 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2141 | Else, if the `def' found by `\\[py-beginning-of-def-or-class]' |
| 2142 | contains the statement you started on, that's the `def' we use. |
| 2143 | |
| 2144 | Otherwise, we search forward for the closest following `def', and use that. |
| 2145 | |
| 2146 | If a `def' can be found by these rules, point is moved to the start of |
| 2147 | the line immediately following the `def' block, and the position of the |
| 2148 | start of the `def' is returned. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2149 | |
| 2150 | Else point is moved to the end of the buffer, and nil is returned. |
| 2151 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2152 | Note that doing this command repeatedly will take you closer to the |
| 2153 | end of the buffer each time. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2154 | |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2155 | If you want to mark the current `def', see `\\[py-mark-def-or-class]'." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2156 | (interactive "P") ; raw prefix arg |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2157 | (if (and count (/= count 1)) |
| 2158 | (py-beginning-of-def-or-class (- 1 count))) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2159 | (let ((start (progn (py-goto-initial-line) (point))) |
| 2160 | (which (if class "class" "def")) |
| 2161 | (state 'not-found)) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2162 | ;; move point to start of appropriate def/class |
| 2163 | (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one |
| 2164 | (setq state 'at-beginning) |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2165 | ;; else see if py-beginning-of-def-or-class hits container |
| 2166 | (if (and (py-beginning-of-def-or-class class) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2167 | (progn (py-goto-beyond-block) |
| 2168 | (> (point) start))) |
| 2169 | (setq state 'at-end) |
| 2170 | ;; else search forward |
| 2171 | (goto-char start) |
| 2172 | (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move) |
| 2173 | (progn (setq state 'at-beginning) |
| 2174 | (beginning-of-line))))) |
| 2175 | (cond |
| 2176 | ((eq state 'at-beginning) (py-goto-beyond-block) t) |
| 2177 | ((eq state 'at-end) t) |
| 2178 | ((eq state 'not-found) nil) |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2179 | (t (error "internal error in py-end-of-def-or-class"))))) |
| 2180 | |
| 2181 | ;; Backwards compabitility |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame^] | 2182 | (defalias 'end-of-python-def-or-class 'py-end-of-def-or-class) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2183 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2184 | |
| 2185 | ;; Functions for marking regions |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2186 | (defun py-mark-block (&optional extend just-move) |
| 2187 | "Mark following block of lines. With prefix arg, mark structure. |
| 2188 | Easier to use than explain. It sets the region to an `interesting' |
| 2189 | block of succeeding lines. If point is on a blank line, it goes down to |
| 2190 | the next non-blank line. That will be the start of the region. The end |
| 2191 | of the region depends on the kind of line at the start: |
| 2192 | |
| 2193 | - If a comment, the region will include all succeeding comment lines up |
| 2194 | to (but not including) the next non-comment line (if any). |
| 2195 | |
| 2196 | - Else if a prefix arg is given, and the line begins one of these |
| 2197 | structures: |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2198 | |
| 2199 | if elif else try except finally for while def class |
| 2200 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2201 | the region will be set to the body of the structure, including |
| 2202 | following blocks that `belong' to it, but excluding trailing blank |
| 2203 | and comment lines. E.g., if on a `try' statement, the `try' block |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2204 | and all (if any) of the following `except' and `finally' blocks |
| 2205 | that belong to the `try' structure will be in the region. Ditto |
| 2206 | for if/elif/else, for/else and while/else structures, and (a bit |
| 2207 | degenerate, since they're always one-block structures) def and |
| 2208 | class blocks. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2209 | |
| 2210 | - Else if no prefix argument is given, and the line begins a Python |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2211 | block (see list above), and the block is not a `one-liner' (i.e., |
| 2212 | the statement ends with a colon, not with code), the region will |
| 2213 | include all succeeding lines up to (but not including) the next |
| 2214 | code statement (if any) that's indented no more than the starting |
| 2215 | line, except that trailing blank and comment lines are excluded. |
| 2216 | E.g., if the starting line begins a multi-statement `def' |
| 2217 | structure, the region will be set to the full function definition, |
| 2218 | but without any trailing `noise' lines. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2219 | |
| 2220 | - Else the region will include all succeeding lines up to (but not |
| 2221 | including) the next blank line, or code or indenting-comment line |
| 2222 | indented strictly less than the starting line. Trailing indenting |
| 2223 | comment lines are included in this case, but not trailing blank |
| 2224 | lines. |
| 2225 | |
| 2226 | A msg identifying the location of the mark is displayed in the echo |
| 2227 | area; or do `\\[exchange-point-and-mark]' to flip down to the end. |
| 2228 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2229 | If called from a program, optional argument EXTEND plays the role of |
| 2230 | the prefix arg, and if optional argument JUST-MOVE is not nil, just |
| 2231 | moves to the end of the block (& does not set mark or display a msg)." |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2232 | (interactive "P") ; raw prefix arg |
| 2233 | (py-goto-initial-line) |
| 2234 | ;; skip over blank lines |
| 2235 | (while (and |
| 2236 | (looking-at "[ \t]*$") ; while blank line |
| 2237 | (not (eobp))) ; & somewhere to go |
| 2238 | (forward-line 1)) |
| 2239 | (if (eobp) |
| 2240 | (error "Hit end of buffer without finding a non-blank stmt")) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2241 | (let ((initial-pos (point)) |
| 2242 | (initial-indent (current-indentation)) |
| 2243 | last-pos ; position of last stmt in region |
| 2244 | (followers |
| 2245 | '((if elif else) (elif elif else) (else) |
| 2246 | (try except finally) (except except) (finally) |
| 2247 | (for else) (while else) |
| 2248 | (def) (class) ) ) |
| 2249 | first-symbol next-symbol) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2250 | |
| 2251 | (cond |
| 2252 | ;; if comment line, suck up the following comment lines |
| 2253 | ((looking-at "[ \t]*#") |
| 2254 | (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment |
| 2255 | (re-search-backward "^[ \t]*#") ; and back to last comment in block |
| 2256 | (setq last-pos (point))) |
| 2257 | |
| 2258 | ;; else if line is a block line and EXTEND given, suck up |
| 2259 | ;; the whole structure |
| 2260 | ((and extend |
| 2261 | (setq first-symbol (py-suck-up-first-keyword) ) |
| 2262 | (assq first-symbol followers)) |
| 2263 | (while (and |
| 2264 | (or (py-goto-beyond-block) t) ; side effect |
| 2265 | (forward-line -1) ; side effect |
| 2266 | (setq last-pos (point)) ; side effect |
| 2267 | (py-goto-statement-below) |
| 2268 | (= (current-indentation) initial-indent) |
| 2269 | (setq next-symbol (py-suck-up-first-keyword)) |
| 2270 | (memq next-symbol (cdr (assq first-symbol followers)))) |
| 2271 | (setq first-symbol next-symbol))) |
| 2272 | |
| 2273 | ;; else if line *opens* a block, search for next stmt indented <= |
| 2274 | ((py-statement-opens-block-p) |
| 2275 | (while (and |
| 2276 | (setq last-pos (point)) ; always true -- side effect |
| 2277 | (py-goto-statement-below) |
| 2278 | (> (current-indentation) initial-indent)) |
| 2279 | nil)) |
| 2280 | |
| 2281 | ;; else plain code line; stop at next blank line, or stmt or |
| 2282 | ;; indenting comment line indented < |
| 2283 | (t |
| 2284 | (while (and |
| 2285 | (setq last-pos (point)) ; always true -- side effect |
| 2286 | (or (py-goto-beyond-final-line) t) |
| 2287 | (not (looking-at "[ \t]*$")) ; stop at blank line |
| 2288 | (or |
| 2289 | (>= (current-indentation) initial-indent) |
| 2290 | (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting # |
| 2291 | nil))) |
| 2292 | |
| 2293 | ;; skip to end of last stmt |
| 2294 | (goto-char last-pos) |
| 2295 | (py-goto-beyond-final-line) |
| 2296 | |
| 2297 | ;; set mark & display |
| 2298 | (if just-move |
| 2299 | () ; just return |
| 2300 | (push-mark (point) 'no-msg) |
| 2301 | (forward-line -1) |
| 2302 | (message "Mark set after: %s" (py-suck-up-leading-text)) |
| 2303 | (goto-char initial-pos)))) |
| 2304 | |
Barry Warsaw | 2518c67 | 1997-11-05 00:51:08 +0000 | [diff] [blame] | 2305 | (defun py-mark-def-or-class (&optional class) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2306 | "Set region to body of def (or class, with prefix arg) enclosing point. |
| 2307 | Pushes the current mark, then point, on the mark ring (all language |
| 2308 | modes do this, but although it's handy it's never documented ...). |
| 2309 | |
| 2310 | In most Emacs language modes, this function bears at least a |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2311 | hallucinogenic resemblance to `\\[py-end-of-def-or-class]' and |
| 2312 | `\\[py-beginning-of-def-or-class]'. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2313 | |
| 2314 | And in earlier versions of Python mode, all 3 were tightly connected. |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2315 | Turned out that was more confusing than useful: the `goto start' and |
| 2316 | `goto end' commands are usually used to search through a file, and |
| 2317 | people expect them to act a lot like `search backward' and `search |
| 2318 | forward' string-search commands. But because Python `def' and `class' |
| 2319 | can nest to arbitrary levels, finding the smallest def containing |
| 2320 | point cannot be done via a simple backward search: the def containing |
| 2321 | point may not be the closest preceding def, or even the closest |
| 2322 | preceding def that's indented less. The fancy algorithm required is |
| 2323 | appropriate for the usual uses of this `mark' command, but not for the |
| 2324 | `goto' variations. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2325 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2326 | So the def marked by this command may not be the one either of the |
| 2327 | `goto' commands find: If point is on a blank or non-indenting comment |
| 2328 | line, moves back to start of the closest preceding code statement or |
| 2329 | indenting comment line. If this is a `def' statement, that's the def |
| 2330 | we use. Else searches for the smallest enclosing `def' block and uses |
| 2331 | that. Else signals an error. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2332 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2333 | When an enclosing def is found: The mark is left immediately beyond |
| 2334 | the last line of the def block. Point is left at the start of the |
| 2335 | def, except that: if the def is preceded by a number of comment lines |
| 2336 | followed by (at most) one optional blank line, point is left at the |
| 2337 | start of the comments; else if the def is preceded by a blank line, |
| 2338 | point is left at its start. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2339 | |
| 2340 | The intent is to mark the containing def/class and its associated |
| 2341 | documentation, to make moving and duplicating functions and classes |
| 2342 | pleasant." |
| 2343 | (interactive "P") ; raw prefix arg |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2344 | (let ((start (point)) |
| 2345 | (which (if class "class" "def"))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2346 | (push-mark start) |
| 2347 | (if (not (py-go-up-tree-to-keyword which)) |
| 2348 | (progn (goto-char start) |
| 2349 | (error "Enclosing %s not found" which)) |
| 2350 | ;; else enclosing def/class found |
| 2351 | (setq start (point)) |
| 2352 | (py-goto-beyond-block) |
| 2353 | (push-mark (point)) |
| 2354 | (goto-char start) |
| 2355 | (if (zerop (forward-line -1)) ; if there is a preceding line |
| 2356 | (progn |
| 2357 | (if (looking-at "[ \t]*$") ; it's blank |
| 2358 | (setq start (point)) ; so reset start point |
| 2359 | (goto-char start)) ; else try again |
| 2360 | (if (zerop (forward-line -1)) |
| 2361 | (if (looking-at "[ \t]*#") ; a comment |
| 2362 | ;; look back for non-comment line |
| 2363 | ;; tricky: note that the regexp matches a blank |
| 2364 | ;; line, cuz \n is in the 2nd character class |
| 2365 | (and |
| 2366 | (re-search-backward "^[ \t]*[^ \t#]" nil 'move) |
| 2367 | (forward-line 1)) |
| 2368 | ;; no comment, so go back |
Barry Warsaw | 4da6bd5 | 1997-11-26 06:00:26 +0000 | [diff] [blame] | 2369 | (goto-char start))))))) |
| 2370 | (exchange-point-and-mark) |
| 2371 | (py-keep-region-active)) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2372 | |
Barry Warsaw | 9e5a9c8 | 1996-07-24 18:26:53 +0000 | [diff] [blame] | 2373 | ;; ripped from cc-mode |
| 2374 | (defun py-forward-into-nomenclature (&optional arg) |
| 2375 | "Move forward to end of a nomenclature section or word. |
| 2376 | With arg, to it arg times. |
| 2377 | |
| 2378 | A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores." |
| 2379 | (interactive "p") |
| 2380 | (let ((case-fold-search nil)) |
| 2381 | (if (> arg 0) |
Barry Warsaw | c5a8cbd | 1996-08-05 21:53:02 +0000 | [diff] [blame] | 2382 | (re-search-forward |
| 2383 | "\\(\\W\\|[_]\\)*\\([A-Z]*[a-z0-9]*\\)" |
| 2384 | (point-max) t arg) |
Barry Warsaw | 9e5a9c8 | 1996-07-24 18:26:53 +0000 | [diff] [blame] | 2385 | (while (and (< arg 0) |
| 2386 | (re-search-backward |
Barry Warsaw | c5a8cbd | 1996-08-05 21:53:02 +0000 | [diff] [blame] | 2387 | "\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\(\\W\\|[_]\\)\\w+" |
Barry Warsaw | 9e5a9c8 | 1996-07-24 18:26:53 +0000 | [diff] [blame] | 2388 | (point-min) 0)) |
| 2389 | (forward-char 1) |
| 2390 | (setq arg (1+ arg))))) |
| 2391 | (py-keep-region-active)) |
| 2392 | |
| 2393 | (defun py-backward-into-nomenclature (&optional arg) |
| 2394 | "Move backward to beginning of a nomenclature section or word. |
| 2395 | With optional ARG, move that many times. If ARG is negative, move |
| 2396 | forward. |
| 2397 | |
| 2398 | A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores." |
| 2399 | (interactive "p") |
| 2400 | (py-forward-into-nomenclature (- arg)) |
| 2401 | (py-keep-region-active)) |
| 2402 | |
| 2403 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2404 | |
| 2405 | ;; Documentation functions |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2406 | |
| 2407 | ;; dump the long form of the mode blurb; does the usual doc escapes, |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2408 | ;; plus lines of the form ^[vc]:name$ to suck variable & command docs |
| 2409 | ;; out of the right places, along with the keys they're on & current |
| 2410 | ;; values |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2411 | (defun py-dump-help-string (str) |
| 2412 | (with-output-to-temp-buffer "*Help*" |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2413 | (let ((locals (buffer-local-variables)) |
| 2414 | funckind funcname func funcdoc |
| 2415 | (start 0) mstart end |
| 2416 | keys ) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2417 | (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start) |
| 2418 | (setq mstart (match-beginning 0) end (match-end 0) |
| 2419 | funckind (substring str (match-beginning 1) (match-end 1)) |
| 2420 | funcname (substring str (match-beginning 2) (match-end 2)) |
| 2421 | func (intern funcname)) |
| 2422 | (princ (substitute-command-keys (substring str start mstart))) |
| 2423 | (cond |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2424 | ((equal funckind "c") ; command |
| 2425 | (setq funcdoc (documentation func) |
| 2426 | keys (concat |
| 2427 | "Key(s): " |
| 2428 | (mapconcat 'key-description |
| 2429 | (where-is-internal func py-mode-map) |
| 2430 | ", ")))) |
| 2431 | ((equal funckind "v") ; variable |
Barry Warsaw | 604cefa | 1996-09-03 18:17:04 +0000 | [diff] [blame] | 2432 | (setq funcdoc (documentation-property func 'variable-documentation) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2433 | keys (if (assq func locals) |
| 2434 | (concat |
| 2435 | "Local/Global values: " |
| 2436 | (prin1-to-string (symbol-value func)) |
| 2437 | " / " |
| 2438 | (prin1-to-string (default-value func))) |
| 2439 | (concat |
| 2440 | "Value: " |
| 2441 | (prin1-to-string (symbol-value func)))))) |
| 2442 | (t ; unexpected |
| 2443 | (error "Error in py-dump-help-string, tag `%s'" funckind))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2444 | (princ (format "\n-> %s:\t%s\t%s\n\n" |
| 2445 | (if (equal funckind "c") "Command" "Variable") |
| 2446 | funcname keys)) |
| 2447 | (princ funcdoc) |
| 2448 | (terpri) |
| 2449 | (setq start end)) |
| 2450 | (princ (substitute-command-keys (substring str start)))) |
| 2451 | (print-help-return-message))) |
| 2452 | |
| 2453 | (defun py-describe-mode () |
| 2454 | "Dump long form of Python-mode docs." |
| 2455 | (interactive) |
| 2456 | (py-dump-help-string "Major mode for editing Python files. |
| 2457 | Knows about Python indentation, tokens, comments and continuation lines. |
| 2458 | Paragraphs are separated by blank lines only. |
| 2459 | |
| 2460 | Major sections below begin with the string `@'; specific function and |
| 2461 | variable docs begin with `->'. |
| 2462 | |
| 2463 | @EXECUTING PYTHON CODE |
| 2464 | |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame^] | 2465 | \\[py-execute-import-or-reload]\timports or reloads the file in the Python interpreter |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2466 | \\[py-execute-buffer]\tsends the entire buffer to the Python interpreter |
| 2467 | \\[py-execute-region]\tsends the current region |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame^] | 2468 | \\[py-execute-def-or-class]\tsends the current function or class definition |
| 2469 | \\[py-execute-string]\tsends an arbitrary string |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2470 | \\[py-shell]\tstarts a Python interpreter window; this will be used by |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame^] | 2471 | \tsubsequent Python execution commands |
| 2472 | %c:py-execute-import-or-reload |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2473 | %c:py-execute-buffer |
| 2474 | %c:py-execute-region |
Barry Warsaw | 820273d | 1998-05-19 15:54:45 +0000 | [diff] [blame^] | 2475 | %c:py-execute-def-or-class |
| 2476 | %c:py-execute-string |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2477 | %c:py-shell |
| 2478 | |
| 2479 | @VARIABLES |
| 2480 | |
| 2481 | py-indent-offset\tindentation increment |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 2482 | py-block-comment-prefix\tcomment string used by comment-region |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2483 | |
| 2484 | py-python-command\tshell command to invoke Python interpreter |
| 2485 | py-scroll-process-buffer\talways scroll Python process buffer |
| 2486 | py-temp-directory\tdirectory used for temp files (if needed) |
| 2487 | |
| 2488 | py-beep-if-tab-change\tring the bell if tab-width is changed |
| 2489 | %v:py-indent-offset |
| 2490 | %v:py-block-comment-prefix |
| 2491 | %v:py-python-command |
| 2492 | %v:py-scroll-process-buffer |
| 2493 | %v:py-temp-directory |
| 2494 | %v:py-beep-if-tab-change |
| 2495 | |
| 2496 | @KINDS OF LINES |
| 2497 | |
| 2498 | Each physical line in the file is either a `continuation line' (the |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2499 | preceding line ends with a backslash that's not part of a comment, or |
| 2500 | the paren/bracket/brace nesting level at the start of the line is |
| 2501 | non-zero, or both) or an `initial line' (everything else). |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2502 | |
| 2503 | An initial line is in turn a `blank line' (contains nothing except |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2504 | possibly blanks or tabs), a `comment line' (leftmost non-blank |
| 2505 | character is `#'), or a `code line' (everything else). |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2506 | |
| 2507 | Comment Lines |
| 2508 | |
| 2509 | Although all comment lines are treated alike by Python, Python mode |
| 2510 | recognizes two kinds that act differently with respect to indentation. |
| 2511 | |
| 2512 | An `indenting comment line' is a comment line with a blank, tab or |
| 2513 | nothing after the initial `#'. The indentation commands (see below) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2514 | treat these exactly as if they were code lines: a line following an |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2515 | indenting comment line will be indented like the comment line. All |
| 2516 | other comment lines (those with a non-whitespace character immediately |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2517 | following the initial `#') are `non-indenting comment lines', and |
| 2518 | their indentation is ignored by the indentation commands. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2519 | |
| 2520 | Indenting comment lines are by far the usual case, and should be used |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2521 | whenever possible. Non-indenting comment lines are useful in cases |
| 2522 | like these: |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2523 | |
| 2524 | \ta = b # a very wordy single-line comment that ends up being |
| 2525 | \t #... continued onto another line |
| 2526 | |
| 2527 | \tif a == b: |
| 2528 | ##\t\tprint 'panic!' # old code we've `commented out' |
| 2529 | \t\treturn a |
| 2530 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2531 | Since the `#...' and `##' comment lines have a non-whitespace |
| 2532 | character following the initial `#', Python mode ignores them when |
| 2533 | computing the proper indentation for the next line. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2534 | |
| 2535 | Continuation Lines and Statements |
| 2536 | |
| 2537 | The Python-mode commands generally work on statements instead of on |
| 2538 | individual lines, where a `statement' is a comment or blank line, or a |
| 2539 | code line and all of its following continuation lines (if any) |
| 2540 | considered as a single logical unit. The commands in this mode |
| 2541 | generally (when it makes sense) automatically move to the start of the |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2542 | statement containing point, even if point happens to be in the middle |
| 2543 | of some continuation line. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2544 | |
| 2545 | |
| 2546 | @INDENTATION |
| 2547 | |
| 2548 | Primarily for entering new code: |
| 2549 | \t\\[indent-for-tab-command]\t indent line appropriately |
| 2550 | \t\\[py-newline-and-indent]\t insert newline, then indent |
Barry Warsaw | 27ee115 | 1997-12-03 05:03:44 +0000 | [diff] [blame] | 2551 | \t\\[py-electric-backspace]\t reduce indentation, or delete single character |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2552 | |
| 2553 | Primarily for reindenting existing code: |
| 2554 | \t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally |
| 2555 | \t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally |
| 2556 | |
| 2557 | \t\\[py-indent-region]\t reindent region to match its context |
| 2558 | \t\\[py-shift-region-left]\t shift region left by py-indent-offset |
| 2559 | \t\\[py-shift-region-right]\t shift region right by py-indent-offset |
| 2560 | |
| 2561 | Unlike most programming languages, Python uses indentation, and only |
| 2562 | indentation, to specify block structure. Hence the indentation supplied |
| 2563 | automatically by Python-mode is just an educated guess: only you know |
| 2564 | the block structure you intend, so only you can supply correct |
| 2565 | indentation. |
| 2566 | |
| 2567 | The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on |
| 2568 | the indentation of preceding statements. E.g., assuming |
| 2569 | py-indent-offset is 4, after you enter |
| 2570 | \tif a > 0: \\[py-newline-and-indent] |
| 2571 | the cursor will be moved to the position of the `_' (_ is not a |
| 2572 | character in the file, it's just used here to indicate the location of |
| 2573 | the cursor): |
| 2574 | \tif a > 0: |
| 2575 | \t _ |
| 2576 | If you then enter `c = d' \\[py-newline-and-indent], the cursor will move |
| 2577 | to |
| 2578 | \tif a > 0: |
| 2579 | \t c = d |
| 2580 | \t _ |
| 2581 | Python-mode cannot know whether that's what you intended, or whether |
| 2582 | \tif a > 0: |
| 2583 | \t c = d |
| 2584 | \t_ |
| 2585 | was your intent. In general, Python-mode either reproduces the |
| 2586 | indentation of the (closest code or indenting-comment) preceding |
| 2587 | statement, or adds an extra py-indent-offset blanks if the preceding |
| 2588 | statement has `:' as its last significant (non-whitespace and non- |
| 2589 | comment) character. If the suggested indentation is too much, use |
Barry Warsaw | 27ee115 | 1997-12-03 05:03:44 +0000 | [diff] [blame] | 2590 | \\[py-electric-backspace] to reduce it. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2591 | |
| 2592 | Continuation lines are given extra indentation. If you don't like the |
| 2593 | suggested indentation, change it to something you do like, and Python- |
| 2594 | mode will strive to indent later lines of the statement in the same way. |
| 2595 | |
| 2596 | If a line is a continuation line by virtue of being in an unclosed |
| 2597 | paren/bracket/brace structure (`list', for short), the suggested |
| 2598 | indentation depends on whether the current line contains the first item |
| 2599 | in the list. If it does, it's indented py-indent-offset columns beyond |
| 2600 | the indentation of the line containing the open bracket. If you don't |
| 2601 | like that, change it by hand. The remaining items in the list will mimic |
| 2602 | whatever indentation you give to the first item. |
| 2603 | |
| 2604 | If a line is a continuation line because the line preceding it ends with |
| 2605 | a backslash, the third and following lines of the statement inherit their |
| 2606 | indentation from the line preceding them. The indentation of the second |
| 2607 | line in the statement depends on the form of the first (base) line: if |
| 2608 | the base line is an assignment statement with anything more interesting |
| 2609 | than the backslash following the leftmost assigning `=', the second line |
| 2610 | is indented two columns beyond that `='. Else it's indented to two |
| 2611 | columns beyond the leftmost solid chunk of non-whitespace characters on |
| 2612 | the base line. |
| 2613 | |
| 2614 | Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command] |
| 2615 | repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block |
| 2616 | structure you intend. |
| 2617 | %c:indent-for-tab-command |
| 2618 | %c:py-newline-and-indent |
Barry Warsaw | 27ee115 | 1997-12-03 05:03:44 +0000 | [diff] [blame] | 2619 | %c:py-electric-backspace |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2620 | |
| 2621 | |
| 2622 | The next function may be handy when editing code you didn't write: |
| 2623 | %c:py-guess-indent-offset |
| 2624 | |
| 2625 | |
| 2626 | The remaining `indent' functions apply to a region of Python code. They |
| 2627 | assume the block structure (equals indentation, in Python) of the region |
| 2628 | is correct, and alter the indentation in various ways while preserving |
| 2629 | the block structure: |
| 2630 | %c:py-indent-region |
| 2631 | %c:py-shift-region-left |
| 2632 | %c:py-shift-region-right |
| 2633 | |
| 2634 | @MARKING & MANIPULATING REGIONS OF CODE |
| 2635 | |
| 2636 | \\[py-mark-block]\t mark block of lines |
Barry Warsaw | 2518c67 | 1997-11-05 00:51:08 +0000 | [diff] [blame] | 2637 | \\[py-mark-def-or-class]\t mark smallest enclosing def |
| 2638 | \\[universal-argument] \\[py-mark-def-or-class]\t mark smallest enclosing class |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 2639 | \\[comment-region]\t comment out region of code |
| 2640 | \\[universal-argument] \\[comment-region]\t uncomment region of code |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2641 | %c:py-mark-block |
Barry Warsaw | 2518c67 | 1997-11-05 00:51:08 +0000 | [diff] [blame] | 2642 | %c:py-mark-def-or-class |
Barry Warsaw | 42f707f | 1996-07-29 21:05:05 +0000 | [diff] [blame] | 2643 | %c:comment-region |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2644 | |
| 2645 | @MOVING POINT |
| 2646 | |
| 2647 | \\[py-previous-statement]\t move to statement preceding point |
| 2648 | \\[py-next-statement]\t move to statement following point |
| 2649 | \\[py-goto-block-up]\t move up to start of current block |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2650 | \\[py-beginning-of-def-or-class]\t move to start of def |
| 2651 | \\[universal-argument] \\[py-beginning-of-def-or-class]\t move to start of class |
| 2652 | \\[py-end-of-def-or-class]\t move to end of def |
| 2653 | \\[universal-argument] \\[py-end-of-def-or-class]\t move to end of class |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2654 | |
| 2655 | The first two move to one statement beyond the statement that contains |
| 2656 | point. A numeric prefix argument tells them to move that many |
| 2657 | statements instead. Blank lines, comment lines, and continuation lines |
| 2658 | do not count as `statements' for these commands. So, e.g., you can go |
| 2659 | to the first code statement in a file by entering |
| 2660 | \t\\[beginning-of-buffer]\t to move to the top of the file |
| 2661 | \t\\[py-next-statement]\t to skip over initial comments and blank lines |
| 2662 | Or do `\\[py-previous-statement]' with a huge prefix argument. |
| 2663 | %c:py-previous-statement |
| 2664 | %c:py-next-statement |
| 2665 | %c:py-goto-block-up |
Barry Warsaw | ab0e86c | 1998-05-19 15:31:46 +0000 | [diff] [blame] | 2666 | %c:py-beginning-of-def-or-class |
| 2667 | %c:py-end-of-def-or-class |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2668 | |
| 2669 | @LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE |
| 2670 | |
| 2671 | `\\[indent-new-comment-line]' is handy for entering a multi-line comment. |
| 2672 | |
| 2673 | `\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the |
| 2674 | overall class and def structure of a module. |
| 2675 | |
| 2676 | `\\[back-to-indentation]' moves point to a line's first non-blank character. |
| 2677 | |
| 2678 | `\\[indent-relative]' is handy for creating odd indentation. |
| 2679 | |
| 2680 | @OTHER EMACS HINTS |
| 2681 | |
| 2682 | If you don't like the default value of a variable, change its value to |
| 2683 | whatever you do like by putting a `setq' line in your .emacs file. |
| 2684 | E.g., to set the indentation increment to 4, put this line in your |
| 2685 | .emacs: |
| 2686 | \t(setq py-indent-offset 4) |
| 2687 | To see the value of a variable, do `\\[describe-variable]' and enter the variable |
| 2688 | name at the prompt. |
| 2689 | |
| 2690 | When entering a key sequence like `C-c C-n', it is not necessary to |
| 2691 | release the CONTROL key after doing the `C-c' part -- it suffices to |
| 2692 | press the CONTROL key, press and release `c' (while still holding down |
| 2693 | CONTROL), press and release `n' (while still holding down CONTROL), & |
| 2694 | then release CONTROL. |
| 2695 | |
| 2696 | Entering Python mode calls with no arguments the value of the variable |
| 2697 | `python-mode-hook', if that value exists and is not nil; for backward |
| 2698 | compatibility it also tries `py-mode-hook'; see the `Hooks' section of |
| 2699 | the Elisp manual for details. |
| 2700 | |
| 2701 | Obscure: When python-mode is first loaded, it looks for all bindings |
| 2702 | to newline-and-indent in the global keymap, and shadows them with |
| 2703 | local bindings to py-newline-and-indent.")) |
| 2704 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2705 | |
| 2706 | ;; Helper functions |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2707 | (defvar py-parse-state-re |
| 2708 | (concat |
| 2709 | "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>" |
| 2710 | "\\|" |
| 2711 | "^[^ #\t\n]")) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2712 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2713 | ;; returns the parse state at point (see parse-partial-sexp docs) |
| 2714 | (defun py-parse-state () |
| 2715 | (save-excursion |
Barry Warsaw | 43ecf8e | 1996-04-06 00:00:19 +0000 | [diff] [blame] | 2716 | (let ((here (point)) |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 2717 | pps done) |
Barry Warsaw | 43ecf8e | 1996-04-06 00:00:19 +0000 | [diff] [blame] | 2718 | (while (not done) |
| 2719 | ;; back up to the first preceding line (if any; else start of |
| 2720 | ;; buffer) that begins with a popular Python keyword, or a |
| 2721 | ;; non- whitespace and non-comment character. These are good |
| 2722 | ;; places to start parsing to see whether where we started is |
| 2723 | ;; at a non-zero nesting level. It may be slow for people who |
| 2724 | ;; write huge code blocks or huge lists ... tough beans. |
| 2725 | (re-search-backward py-parse-state-re nil 'move) |
| 2726 | (beginning-of-line) |
Barry Warsaw | f64b405 | 1998-02-12 16:52:14 +0000 | [diff] [blame] | 2727 | ;; In XEmacs, we have a much better way to test for whether |
| 2728 | ;; we're in a triple-quoted string or not. Emacs does not |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 2729 | ;; have this built-in function, which is its loss because |
Barry Warsaw | f64b405 | 1998-02-12 16:52:14 +0000 | [diff] [blame] | 2730 | ;; without scanning from the beginning of the buffer, there's |
| 2731 | ;; no accurate way to determine this otherwise. |
| 2732 | (if (not (fboundp 'buffer-syntactic-context)) |
| 2733 | ;; Emacs |
Barry Warsaw | 585f733 | 1998-04-01 21:13:51 +0000 | [diff] [blame] | 2734 | (progn |
| 2735 | (save-excursion (setq pps (parse-partial-sexp (point) here))) |
Barry Warsaw | f64b405 | 1998-02-12 16:52:14 +0000 | [diff] [blame] | 2736 | ;; make sure we don't land inside a triple-quoted string |
Barry Warsaw | 742a511 | 1998-03-13 17:29:15 +0000 | [diff] [blame] | 2737 | (setq done (or (not (nth 3 pps)) |
| 2738 | (bobp)))) |
Barry Warsaw | f64b405 | 1998-02-12 16:52:14 +0000 | [diff] [blame] | 2739 | ;; XEmacs |
| 2740 | (setq done (or (not (buffer-syntactic-context)) |
| 2741 | (bobp))) |
| 2742 | (when done |
| 2743 | (setq pps (parse-partial-sexp (point) here))) |
| 2744 | )) |
Barry Warsaw | 43ecf8e | 1996-04-06 00:00:19 +0000 | [diff] [blame] | 2745 | pps))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2746 | |
| 2747 | ;; if point is at a non-zero nesting level, returns the number of the |
| 2748 | ;; character that opens the smallest enclosing unclosed list; else |
| 2749 | ;; returns nil. |
| 2750 | (defun py-nesting-level () |
Barry Warsaw | f64b405 | 1998-02-12 16:52:14 +0000 | [diff] [blame] | 2751 | (let ((status (py-parse-state))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2752 | (if (zerop (car status)) |
| 2753 | nil ; not in a nest |
| 2754 | (car (cdr status))))) ; char# of open bracket |
| 2755 | |
| 2756 | ;; t iff preceding line ends with backslash that's not in a comment |
| 2757 | (defun py-backslash-continuation-line-p () |
| 2758 | (save-excursion |
| 2759 | (beginning-of-line) |
| 2760 | (and |
| 2761 | ;; use a cheap test first to avoid the regexp if possible |
| 2762 | ;; use 'eq' because char-after may return nil |
| 2763 | (eq (char-after (- (point) 2)) ?\\ ) |
| 2764 | ;; make sure; since eq test passed, there is a preceding line |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2765 | (forward-line -1) ; always true -- side effect |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2766 | (looking-at py-continued-re)))) |
| 2767 | |
| 2768 | ;; t iff current line is a continuation line |
| 2769 | (defun py-continuation-line-p () |
| 2770 | (save-excursion |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2771 | (beginning-of-line) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2772 | (or (py-backslash-continuation-line-p) |
| 2773 | (py-nesting-level)))) |
| 2774 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2775 | ;; go to initial line of current statement; usually this is the line |
| 2776 | ;; we're on, but if we're on the 2nd or following lines of a |
| 2777 | ;; continuation block, we need to go up to the first line of the |
| 2778 | ;; block. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2779 | ;; |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2780 | ;; Tricky: We want to avoid quadratic-time behavior for long continued |
| 2781 | ;; blocks, whether of the backslash or open-bracket varieties, or a |
| 2782 | ;; mix of the two. The following manages to do that in the usual |
| 2783 | ;; cases. |
Barry Warsaw | c210e69 | 1998-01-20 22:52:56 +0000 | [diff] [blame] | 2784 | ;; |
| 2785 | ;; Also, if we're sitting inside a triple quoted string, this will |
| 2786 | ;; drop us at the line that begins the string. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2787 | (defun py-goto-initial-line () |
Barry Warsaw | 9ec9fbc | 1998-01-21 05:15:57 +0000 | [diff] [blame] | 2788 | (let (open-bracket-pos) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2789 | (while (py-continuation-line-p) |
| 2790 | (beginning-of-line) |
| 2791 | (if (py-backslash-continuation-line-p) |
| 2792 | (while (py-backslash-continuation-line-p) |
| 2793 | (forward-line -1)) |
| 2794 | ;; else zip out of nested brackets/braces/parens |
| 2795 | (while (setq open-bracket-pos (py-nesting-level)) |
| 2796 | (goto-char open-bracket-pos))))) |
| 2797 | (beginning-of-line)) |
| 2798 | |
| 2799 | ;; go to point right beyond final line of current statement; usually |
| 2800 | ;; this is the start of the next line, but if this is a multi-line |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2801 | ;; statement we need to skip over the continuation lines. Tricky: |
| 2802 | ;; Again we need to be clever to avoid quadratic time behavior. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2803 | (defun py-goto-beyond-final-line () |
| 2804 | (forward-line 1) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2805 | (let (state) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2806 | (while (and (py-continuation-line-p) |
| 2807 | (not (eobp))) |
| 2808 | ;; skip over the backslash flavor |
| 2809 | (while (and (py-backslash-continuation-line-p) |
| 2810 | (not (eobp))) |
| 2811 | (forward-line 1)) |
| 2812 | ;; if in nest, zip to the end of the nest |
| 2813 | (setq state (py-parse-state)) |
| 2814 | (if (and (not (zerop (car state))) |
| 2815 | (not (eobp))) |
| 2816 | (progn |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 2817 | (parse-partial-sexp (point) (point-max) 0 nil state) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2818 | (forward-line 1)))))) |
| 2819 | |
| 2820 | ;; t iff statement opens a block == iff it ends with a colon that's |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2821 | ;; not in a comment. point should be at the start of a statement |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2822 | (defun py-statement-opens-block-p () |
| 2823 | (save-excursion |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2824 | (let ((start (point)) |
| 2825 | (finish (progn (py-goto-beyond-final-line) (1- (point)))) |
| 2826 | (searching t) |
| 2827 | (answer nil) |
| 2828 | state) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2829 | (goto-char start) |
| 2830 | (while searching |
| 2831 | ;; look for a colon with nothing after it except whitespace, and |
| 2832 | ;; maybe a comment |
| 2833 | (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$" |
| 2834 | finish t) |
| 2835 | (if (eq (point) finish) ; note: no `else' clause; just |
| 2836 | ; keep searching if we're not at |
| 2837 | ; the end yet |
| 2838 | ;; sure looks like it opens a block -- but it might |
| 2839 | ;; be in a comment |
| 2840 | (progn |
| 2841 | (setq searching nil) ; search is done either way |
| 2842 | (setq state (parse-partial-sexp start |
| 2843 | (match-beginning 0))) |
| 2844 | (setq answer (not (nth 4 state))))) |
| 2845 | ;; search failed: couldn't find another interesting colon |
| 2846 | (setq searching nil))) |
| 2847 | answer))) |
| 2848 | |
Barry Warsaw | f831d81 | 1996-07-31 20:42:59 +0000 | [diff] [blame] | 2849 | (defun py-statement-closes-block-p () |
| 2850 | ;; true iff the current statement `closes' a block == the line |
Barry Warsaw | 7cb505c | 1996-10-23 20:44:59 +0000 | [diff] [blame] | 2851 | ;; starts with `return', `raise', `break', `continue', and `pass'. |
| 2852 | ;; doesn't catch embedded statements |
Barry Warsaw | f831d81 | 1996-07-31 20:42:59 +0000 | [diff] [blame] | 2853 | (let ((here (point))) |
| 2854 | (back-to-indentation) |
| 2855 | (prog1 |
Barry Warsaw | affc0ca | 1997-11-03 16:59:38 +0000 | [diff] [blame] | 2856 | (looking-at (concat py-block-closing-keywords-re "\\>")) |
Barry Warsaw | f831d81 | 1996-07-31 20:42:59 +0000 | [diff] [blame] | 2857 | (goto-char here)))) |
| 2858 | |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2859 | ;; go to point right beyond final line of block begun by the current |
| 2860 | ;; line. This is the same as where py-goto-beyond-final-line goes |
| 2861 | ;; unless we're on colon line, in which case we go to the end of the |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2862 | ;; block. assumes point is at bolp |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2863 | (defun py-goto-beyond-block () |
| 2864 | (if (py-statement-opens-block-p) |
| 2865 | (py-mark-block nil 'just-move) |
| 2866 | (py-goto-beyond-final-line))) |
| 2867 | |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 2868 | ;; Go to start of first statement (not blank or comment or |
| 2869 | ;; continuation line) at or preceding point. Returns t if there is |
| 2870 | ;; one, else nil. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2871 | (defun py-goto-statement-at-or-above () |
| 2872 | (py-goto-initial-line) |
| 2873 | (if (looking-at py-blank-or-comment-re) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2874 | ;; skip back over blank & comment lines |
| 2875 | ;; note: will skip a blank or comment line that happens to be |
| 2876 | ;; a continuation line too |
| 2877 | (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t) |
| 2878 | (progn (py-goto-initial-line) t) |
| 2879 | nil) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2880 | t)) |
| 2881 | |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 2882 | ;; Go to start of first statement (not blank or comment or |
| 2883 | ;; continuation line) following the statement containing point. |
| 2884 | ;; Returns t if there is one, else nil. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2885 | (defun py-goto-statement-below () |
| 2886 | (beginning-of-line) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2887 | (let ((start (point))) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2888 | (py-goto-beyond-final-line) |
| 2889 | (while (and |
| 2890 | (looking-at py-blank-or-comment-re) |
| 2891 | (not (eobp))) |
| 2892 | (forward-line 1)) |
| 2893 | (if (eobp) |
| 2894 | (progn (goto-char start) nil) |
| 2895 | t))) |
| 2896 | |
Barry Warsaw | 145ab1c | 1998-05-19 14:49:49 +0000 | [diff] [blame] | 2897 | ;; Go to start of statement, at or preceding point, starting with |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2898 | ;; keyword KEY. Skips blank lines and non-indenting comments upward |
| 2899 | ;; first. If that statement starts with KEY, done, else go back to |
| 2900 | ;; first enclosing block starting with KEY. If successful, leaves |
| 2901 | ;; point at the start of the KEY line & returns t. Else leaves point |
| 2902 | ;; at an undefined place & returns nil. |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2903 | (defun py-go-up-tree-to-keyword (key) |
| 2904 | ;; skip blanks and non-indenting # |
| 2905 | (py-goto-initial-line) |
| 2906 | (while (and |
| 2907 | (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)") |
| 2908 | (zerop (forward-line -1))) ; go back |
| 2909 | nil) |
| 2910 | (py-goto-initial-line) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2911 | (let* ((re (concat "[ \t]*" key "\\b")) |
| 2912 | (case-fold-search nil) ; let* so looking-at sees this |
| 2913 | (found (looking-at re)) |
| 2914 | (dead nil)) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2915 | (while (not (or found dead)) |
| 2916 | (condition-case nil ; in case no enclosing block |
| 2917 | (py-goto-block-up 'no-mark) |
| 2918 | (error (setq dead t))) |
| 2919 | (or dead (setq found (looking-at re)))) |
| 2920 | (beginning-of-line) |
| 2921 | found)) |
| 2922 | |
| 2923 | ;; return string in buffer from start of indentation to end of line; |
| 2924 | ;; prefix "..." if leading whitespace was skipped |
| 2925 | (defun py-suck-up-leading-text () |
| 2926 | (save-excursion |
| 2927 | (back-to-indentation) |
| 2928 | (concat |
| 2929 | (if (bolp) "" "...") |
| 2930 | (buffer-substring (point) (progn (end-of-line) (point)))))) |
| 2931 | |
| 2932 | ;; assuming point at bolp, return first keyword ([a-z]+) on the line, |
| 2933 | ;; as a Lisp symbol; return nil if none |
| 2934 | (defun py-suck-up-first-keyword () |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2935 | (let ((case-fold-search nil)) |
Barry Warsaw | 7ae7768 | 1994-12-12 20:38:05 +0000 | [diff] [blame] | 2936 | (if (looking-at "[ \t]*\\([a-z]+\\)\\b") |
| 2937 | (intern (buffer-substring (match-beginning 1) (match-end 1))) |
| 2938 | nil))) |
| 2939 | |
Barry Warsaw | b3e81d5 | 1996-09-04 15:12:42 +0000 | [diff] [blame] | 2940 | (defun py-current-defun () |
| 2941 | ;; tell add-log.el how to find the current function/method/variable |
| 2942 | (save-excursion |
| 2943 | (if (re-search-backward py-defun-start-re nil t) |
| 2944 | (or (match-string 3) |
| 2945 | (let ((method (match-string 2))) |
| 2946 | (if (and (not (zerop (length (match-string 1)))) |
| 2947 | (re-search-backward py-class-start-re nil t)) |
| 2948 | (concat (match-string 1) "." method) |
| 2949 | method))) |
| 2950 | nil))) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2951 | |
| 2952 | |
Barry Warsaw | fec75d6 | 1995-07-05 23:26:15 +0000 | [diff] [blame] | 2953 | (defconst py-help-address "python-mode@python.org" |
Barry Warsaw | 850437a | 1995-03-08 21:50:28 +0000 | [diff] [blame] | 2954 | "Address accepting submission of bug reports.") |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 2955 | |
Barry Warsaw | 850437a | 1995-03-08 21:50:28 +0000 | [diff] [blame] | 2956 | (defun py-version () |
| 2957 | "Echo the current version of `python-mode' in the minibuffer." |
| 2958 | (interactive) |
| 2959 | (message "Using `python-mode' version %s" py-version) |
| 2960 | (py-keep-region-active)) |
| 2961 | |
| 2962 | ;; only works under Emacs 19 |
| 2963 | ;(eval-when-compile |
| 2964 | ; (require 'reporter)) |
| 2965 | |
| 2966 | (defun py-submit-bug-report (enhancement-p) |
| 2967 | "Submit via mail a bug report on `python-mode'. |
| 2968 | With \\[universal-argument] just submit an enhancement request." |
| 2969 | (interactive |
| 2970 | (list (not (y-or-n-p |
| 2971 | "Is this a bug report? (hit `n' to send other comments) ")))) |
Barry Warsaw | b5e0ecb | 1995-03-14 18:32:54 +0000 | [diff] [blame] | 2972 | (let ((reporter-prompt-for-summary-p (if enhancement-p |
| 2973 | "(Very) brief summary: " |
| 2974 | t))) |
Barry Warsaw | 850437a | 1995-03-08 21:50:28 +0000 | [diff] [blame] | 2975 | (require 'reporter) |
| 2976 | (reporter-submit-bug-report |
| 2977 | py-help-address ;address |
Barry Warsaw | b5e0ecb | 1995-03-14 18:32:54 +0000 | [diff] [blame] | 2978 | (concat "python-mode " py-version) ;pkgname |
Barry Warsaw | 850437a | 1995-03-08 21:50:28 +0000 | [diff] [blame] | 2979 | ;; varlist |
| 2980 | (if enhancement-p nil |
| 2981 | '(py-python-command |
| 2982 | py-indent-offset |
| 2983 | py-block-comment-prefix |
| 2984 | py-scroll-process-buffer |
| 2985 | py-temp-directory |
| 2986 | py-beep-if-tab-change)) |
| 2987 | nil ;pre-hooks |
| 2988 | nil ;post-hooks |
| 2989 | "Dear Barry,") ;salutation |
| 2990 | (if enhancement-p nil |
| 2991 | (set-mark (point)) |
| 2992 | (insert |
| 2993 | "Please replace this text with a sufficiently large code sample\n\ |
| 2994 | and an exact recipe so that I can reproduce your problem. Failure\n\ |
| 2995 | to do so may mean a greater delay in fixing your bug.\n\n") |
| 2996 | (exchange-point-and-mark) |
| 2997 | (py-keep-region-active)))) |
| 2998 | |
| 2999 | |
Barry Warsaw | 4738478 | 1997-11-26 05:27:45 +0000 | [diff] [blame] | 3000 | (defun py-kill-emacs-hook () |
| 3001 | (mapcar #'(lambda (filename) |
| 3002 | (py-safe (delete-file filename))) |
| 3003 | py-file-queue)) |
| 3004 | |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3005 | ;; arrange to kill temp files when Emacs exists |
Barry Warsaw | c72c11c | 1997-08-09 06:42:08 +0000 | [diff] [blame] | 3006 | (add-hook 'kill-emacs-hook 'py-kill-emacs-hook) |
Barry Warsaw | 7b0f568 | 1995-03-08 21:33:04 +0000 | [diff] [blame] | 3007 | |
| 3008 | |
| 3009 | |
| 3010 | (provide 'python-mode) |
| 3011 | ;;; python-mode.el ends here |