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