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