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