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