Attachment 'python-mode.el'

Download

   1 ;;; python-mode.el --- Major mode for editing Python programs
   2 
   3 ;; Copyright (C) 1992,1993,1994  Tim Peters
   4 
   5 ;; Author: 2003-2004 http://sf.net/projects/python-mode
   6 ;;         1995-2002 Barry A. Warsaw
   7 ;;         1992-1994 Tim Peters
   8 ;; Maintainer: [email protected]
   9 ;; Created:    Feb 1992
  10 ;; Keywords:   python languages oop
  11 
  12 (defconst py-version "$Revision: 4.63 $"
  13   "`python-mode' version number.")
  14 
  15 ;; This software is provided as-is, without express or implied
  16 ;; warranty.  Permission to use, copy, modify, distribute or sell this
  17 ;; software, without fee, for any purpose and by any individual or
  18 ;; organization, is hereby granted, provided that the above copyright
  19 ;; notice and this paragraph appear in all copies.
  20 
  21 ;;; Commentary:
  22 
  23 ;; This is a major mode for editing Python programs.  It was developed by Tim
  24 ;; Peters after an original idea by Michael A. Guravage.  Tim subsequently
  25 ;; left the net and in 1995, Barry Warsaw inherited the mode.  Tim's now back
  26 ;; but disavows all responsibility for the mode.  In fact, we suspect he
  27 ;; doesn't even use Emacs any more.  In 2003, python-mode.el was moved to its
  28 ;; own SourceForge project apart from the Python project, and now is
  29 ;; maintained by the volunteers at the [email protected] mailing list.
  30 
  31 ;; pdbtrack support contributed by Ken Manheimer, April 2001.  Skip Montanaro
  32 ;; has also contributed significantly to python-mode's development.
  33 
  34 ;; Please use the SourceForge Python project to submit bugs or
  35 ;; patches:
  36 ;;
  37 ;;     http://sourceforge.net/projects/python
  38 
  39 ;; INSTALLATION:
  40 
  41 ;; To install, just drop this file into a directory on your load-path and
  42 ;; byte-compile it.  To set up Emacs to automatically edit files ending in
  43 ;; ".py" using python-mode add the following to your ~/.emacs file (GNU
  44 ;; Emacs) or ~/.xemacs/init.el file (XEmacs):
  45 ;;    (setq auto-mode-alist (cons '("\\.py$" . python-mode) auto-mode-alist))
  46 ;;    (setq interpreter-mode-alist (cons '("python" . python-mode)
  47 ;;                                       interpreter-mode-alist))
  48 ;;    (autoload 'python-mode "python-mode" "Python editing mode." t)
  49 ;;
  50 ;; In XEmacs syntax highlighting should be enabled automatically.  In GNU
  51 ;; Emacs you may have to add these lines to your ~/.emacs file:
  52 ;;    (global-font-lock-mode t)
  53 ;;    (setq font-lock-maximum-decoration t)
  54 
  55 ;; FOR MORE INFORMATION:
  56 
  57 ;; There is some information on python-mode.el at
  58 
  59 ;;     http://www.python.org/emacs/python-mode/
  60 ;;
  61 ;; It does contain links to other packages that you might find useful,
  62 ;; such as pdb interfaces, OO-Browser links, etc.
  63 
  64 ;; BUG REPORTING:
  65 
  66 ;; As mentioned above, please use the SourceForge Python project for
  67 ;; submitting bug reports or patches.  The old recommendation, to use
  68 ;; C-c C-b will still work, but those reports have a higher chance of
  69 ;; getting buried in my mailbox.  Please include a complete, but
  70 ;; concise code sample and a recipe for reproducing the bug.  Send
  71 ;; suggestions and other comments to [email protected].
  72 
  73 ;; When in a Python mode buffer, do a C-h m for more help.  It's
  74 ;; doubtful that a texinfo manual would be very useful, but if you
  75 ;; want to contribute one, I'll certainly accept it!
  76 
  77 ;;; Code:
  78 
  79 (require 'comint)
  80 (require 'custom)
  81 (require 'cl)
  82 (require 'compile)
  83 
  84 
  85 ;; user definable variables
  86 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  87 
  88 (defgroup python nil
  89   "Support for the Python programming language, <http://www.python.org/>"
  90   :group 'languages
  91   :prefix "py-")
  92 
  93 (defcustom py-python-command "python"
  94   "*Shell command used to start Python interpreter."
  95   :type 'string
  96   :group 'python)
  97 
  98 (defcustom py-jpython-command "jpython"
  99   "*Shell command used to start the JPython interpreter."
 100   :type 'string
 101   :group 'python
 102   :tag "JPython Command")
 103 
 104 (defcustom py-default-interpreter 'cpython
 105   "*Which Python interpreter is used by default.
 106 The value for this variable can be either `cpython' or `jpython'.
 107 
 108 When the value is `cpython', the variables `py-python-command' and
 109 `py-python-command-args' are consulted to determine the interpreter
 110 and arguments to use.
 111 
 112 When the value is `jpython', the variables `py-jpython-command' and
 113 `py-jpython-command-args' are consulted to determine the interpreter
 114 and arguments to use.
 115 
 116 Note that this variable is consulted only the first time that a Python
 117 mode buffer is visited during an Emacs session.  After that, use
 118 \\[py-toggle-shells] to change the interpreter shell."
 119   :type '(choice (const :tag "Python (a.k.a. CPython)" cpython)
 120 		 (const :tag "JPython" jpython))
 121   :group 'python)
 122 
 123 (defcustom py-python-command-args '("-i")
 124   "*List of string arguments to be used when starting a Python shell."
 125   :type '(repeat string)
 126   :group 'python)
 127 
 128 (defcustom py-jpython-command-args '("-i")
 129   "*List of string arguments to be used when starting a JPython shell."
 130   :type '(repeat string)
 131   :group 'python
 132   :tag "JPython Command Args")
 133 
 134 (defcustom py-indent-offset 4
 135   "*Amount of offset per level of indentation.
 136 `\\[py-guess-indent-offset]' can usually guess a good value when
 137 you're editing someone else's Python code."
 138   :type 'integer
 139   :group 'python)
 140 
 141 (defcustom py-continuation-offset 4
 142   "*Additional amount of offset to give for some continuation lines.
 143 Continuation lines are those that immediately follow a backslash
 144 terminated line.  Only those continuation lines for a block opening
 145 statement are given this extra offset."
 146   :type 'integer
 147   :group 'python)
 148 
 149 (defcustom py-smart-indentation t
 150   "*Should `python-mode' try to automagically set some indentation variables?
 151 When this variable is non-nil, two things happen when a buffer is set
 152 to `python-mode':
 153 
 154     1. `py-indent-offset' is guessed from existing code in the buffer.
 155        Only guessed values between 2 and 8 are considered.  If a valid
 156        guess can't be made (perhaps because you are visiting a new
 157        file), then the value in `py-indent-offset' is used.
 158 
 159     2. `indent-tabs-mode' is turned off if `py-indent-offset' does not
 160        equal `tab-width' (`indent-tabs-mode' is never turned on by
 161        Python mode).  This means that for newly written code, tabs are
 162        only inserted in indentation if one tab is one indentation
 163        level, otherwise only spaces are used.
 164 
 165 Note that both these settings occur *after* `python-mode-hook' is run,
 166 so if you want to defeat the automagic configuration, you must also
 167 set `py-smart-indentation' to nil in your `python-mode-hook'."
 168   :type 'boolean
 169   :group 'python)
 170 
 171 (defcustom py-align-multiline-strings-p t
 172   "*Flag describing how multi-line triple quoted strings are aligned.
 173 When this flag is non-nil, continuation lines are lined up under the
 174 preceding line's indentation.  When this flag is nil, continuation
 175 lines are aligned to column zero."
 176   :type '(choice (const :tag "Align under preceding line" t)
 177 		 (const :tag "Align to column zero" nil))
 178   :group 'python)
 179 
 180 (defcustom py-block-comment-prefix "##"
 181   "*String used by \\[comment-region] to comment out a block of code.
 182 This should follow the convention for non-indenting comment lines so
 183 that the indentation commands won't get confused (i.e., the string
 184 should be of the form `#x...' where `x' is not a blank or a tab, and
 185 `...' is arbitrary).  However, this string should not end in whitespace."
 186   :type 'string
 187   :group 'python)
 188 
 189 (defcustom py-honor-comment-indentation t
 190   "*Controls how comment lines influence subsequent indentation.
 191 
 192 When nil, all comment lines are skipped for indentation purposes, and
 193 if possible, a faster algorithm is used (i.e. X/Emacs 19 and beyond).
 194 
 195 When t, lines that begin with a single `#' are a hint to subsequent
 196 line indentation.  If the previous line is such a comment line (as
 197 opposed to one that starts with `py-block-comment-prefix'), then its
 198 indentation is used as a hint for this line's indentation.  Lines that
 199 begin with `py-block-comment-prefix' are ignored for indentation
 200 purposes.
 201 
 202 When not nil or t, comment lines that begin with a single `#' are used
 203 as indentation hints, unless the comment character is in column zero."
 204   :type '(choice
 205 	  (const :tag "Skip all comment lines (fast)" nil)
 206 	  (const :tag "Single # `sets' indentation for next line" t)
 207 	  (const :tag "Single # `sets' indentation except at column zero"
 208 		 other)
 209 	  )
 210   :group 'python)
 211 
 212 (defcustom py-temp-directory
 213   (let ((ok '(lambda (x)
 214 	       (and x
 215 		    (setq x (expand-file-name x)) ; always true
 216 		    (file-directory-p x)
 217 		    (file-writable-p x)
 218 		    x))))
 219     (or (funcall ok (getenv "TMPDIR"))
 220 	(funcall ok "/usr/tmp")
 221 	(funcall ok "/tmp")
 222 	(funcall ok "/var/tmp")
 223 	(funcall ok  ".")
 224 	(error
 225 	 "Couldn't find a usable temp directory -- set `py-temp-directory'")))
 226   "*Directory used for temporary files created by a *Python* process.
 227 By default, the first directory from this list that exists and that you
 228 can write into: the value (if any) of the environment variable TMPDIR,
 229 /usr/tmp, /tmp, /var/tmp, or the current directory."
 230   :type 'string
 231   :group 'python)
 232 
 233 (defcustom py-beep-if-tab-change t
 234   "*Ring the bell if `tab-width' is changed.
 235 If a comment of the form
 236 
 237   \t# vi:set tabsize=<number>:
 238 
 239 is found before the first code line when the file is entered, and the
 240 current value of (the general Emacs variable) `tab-width' does not
 241 equal <number>, `tab-width' is set to <number>, a message saying so is
 242 displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
 243 the Emacs bell is also rung as a warning."
 244   :type 'boolean
 245   :group 'python)
 246 
 247 (defcustom py-jump-on-exception t
 248   "*Jump to innermost exception frame in *Python Output* buffer.
 249 When this variable is non-nil and an exception occurs when running
 250 Python code synchronously in a subprocess, jump immediately to the
 251 source code of the innermost traceback frame."
 252   :type 'boolean
 253   :group 'python)
 254 
 255 (defcustom py-ask-about-save t
 256   "If not nil, ask about which buffers to save before executing some code.
 257 Otherwise, all modified buffers are saved without asking."
 258   :type 'boolean
 259   :group 'python)
 260 
 261 (defcustom py-backspace-function 'backward-delete-char-untabify
 262   "*Function called by `py-electric-backspace' when deleting backwards."
 263   :type 'function
 264   :group 'python)
 265 
 266 (defcustom py-delete-function 'delete-char
 267   "*Function called by `py-electric-delete' when deleting forwards."
 268   :type 'function
 269   :group 'python)
 270 
 271 (defcustom py-imenu-show-method-args-p nil 
 272   "*Controls echoing of arguments of functions & methods in the Imenu buffer.
 273 When non-nil, arguments are printed."
 274   :type 'boolean
 275   :group 'python)
 276 (make-variable-buffer-local 'py-indent-offset)
 277 
 278 (defcustom py-pdbtrack-do-tracking-p t
 279   "*Controls whether the pdbtrack feature is enabled or not.
 280 When non-nil, pdbtrack is enabled in all comint-based buffers,
 281 e.g. shell buffers and the *Python* buffer.  When using pdb to debug a
 282 Python program, pdbtrack notices the pdb prompt and displays the
 283 source file and line that the program is stopped at, much the same way
 284 as gud-mode does for debugging C programs with gdb."
 285   :type 'boolean
 286   :group 'python)
 287 (make-variable-buffer-local 'py-pdbtrack-do-tracking-p)
 288 
 289 (defcustom py-pdbtrack-minor-mode-string " PDB"
 290   "*String to use in the minor mode list when pdbtrack is enabled."
 291   :type 'string
 292   :group 'python)
 293 
 294 (defcustom py-import-check-point-max
 295   20000
 296   "Maximum number of characters to search for a Java-ish import statement.
 297 When `python-mode' tries to calculate the shell to use (either a
 298 CPython or a JPython shell), it looks at the so-called `shebang' line
 299 -- i.e. #! line.  If that's not available, it looks at some of the
 300 file heading imports to see if they look Java-like."
 301   :type 'integer
 302   :group 'python
 303   )
 304 
 305 (defcustom py-jpython-packages
 306   '("java" "javax" "org" "com")
 307   "Imported packages that imply `jpython-mode'."
 308   :type '(repeat string)
 309   :group 'python)
 310   
 311 ;; Not customizable
 312 (defvar py-master-file nil
 313   "If non-nil, execute the named file instead of the buffer's file.
 314 The intent is to allow you to set this variable in the file's local
 315 variable section, e.g.:
 316 
 317     # Local Variables:
 318     # py-master-file: \"master.py\"
 319     # End:
 320 
 321 so that typing \\[py-execute-buffer] in that buffer executes the named
 322 master file instead of the buffer's file.  If the file name has a
 323 relative path, the value of variable `default-directory' for the
 324 buffer is prepended to come up with a file name.")
 325 (make-variable-buffer-local 'py-master-file)
 326 
 327 (defcustom py-pychecker-command "pychecker"
 328   "*Shell command used to run Pychecker."
 329   :type 'string
 330   :group 'python
 331   :tag "Pychecker Command")
 332 
 333 (defcustom py-pychecker-command-args '("--stdlib")
 334   "*List of string arguments to be passed to pychecker."
 335   :type '(repeat string)
 336   :group 'python
 337   :tag "Pychecker Command Args")
 338 
 339 (defvar py-shell-alist
 340   '(("jpython" . 'jpython)
 341     ("jython" . 'jpython)
 342     ("python" . 'cpython))
 343   "*Alist of interpreters and python shells. Used by `py-choose-shell'
 344 to select the appropriate python interpreter mode for a file.")
 345 
 346 
 347 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 348 ;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
 349 
 350 (defconst py-emacs-features
 351   (let (features)
 352    features)
 353   "A list of features extant in the Emacs you are using.
 354 There are many flavors of Emacs out there, with different levels of
 355 support for features needed by `python-mode'.")
 356 
 357 ;; Face for None, True, False, self, and Ellipsis
 358 (defvar py-pseudo-keyword-face 'py-pseudo-keyword-face
 359   "Face for pseudo keywords in Python mode, like self, True, False, Ellipsis.")
 360 (make-face 'py-pseudo-keyword-face)
 361 
 362 ;; PEP 318 decorators
 363 (defvar py-decorators-face 'py-decorators-face
 364   "Face method decorators.")
 365 (make-face 'py-decorators-face)
 366 
 367 ;; Face for builtins
 368 (defvar py-builtins-face 'py-builtins-face
 369   "Face for builtins like TypeError, object, open, and exec.")
 370 (make-face 'py-builtins-face)
 371 
 372 (defun py-font-lock-mode-hook ()
 373   (or (face-differs-from-default-p 'py-pseudo-keyword-face)
 374       (copy-face 'font-lock-keyword-face 'py-pseudo-keyword-face))
 375   (or (face-differs-from-default-p 'py-builtins-face)
 376       (copy-face 'font-lock-keyword-face 'py-builtins-face))
 377   (or (face-differs-from-default-p 'py-decorators-face)
 378       (copy-face 'py-pseudo-keyword-face 'py-decorators-face))
 379   )
 380 (add-hook 'font-lock-mode-hook 'py-font-lock-mode-hook)
 381 
 382 (defvar python-font-lock-keywords
 383   (let ((kw1 (mapconcat 'identity
 384 			'("and"      "assert"   "break"   "class"
 385 			  "continue" "def"      "del"     "elif"
 386 			  "else"     "except"   "exec"    "for"
 387 			  "from"     "global"   "if"      "import"
 388 			  "in"       "is"       "lambda"  "not"
 389 			  "or"       "pass"     "print"   "raise"
 390 			  "return"   "while"    "yield"
 391 			  )
 392 			"\\|"))
 393 	(kw2 (mapconcat 'identity
 394 			'("else:" "except:" "finally:" "try:")
 395 			"\\|"))
 396 	(kw3 (mapconcat 'identity
 397 			;; Don't include True, False, None, or
 398 			;; Ellipsis in this list, since they are
 399 			;; already defined as pseudo keywords.
 400 			'("__debug__"
 401 			  "__import__" "__name__" "abs" "apply" "basestring"
 402 			  "bool" "buffer" "callable" "chr" "classmethod"
 403 			  "cmp" "coerce" "compile" "complex" "copyright"
 404 			  "delattr" "dict" "dir" "divmod"
 405 			  "enumerate" "eval" "execfile" "exit" "file"
 406 			  "filter" "float" "getattr" "globals" "hasattr"
 407 			  "hash" "hex" "id" "input" "int" "intern"
 408 			  "isinstance" "issubclass" "iter" "len" "license"
 409 			  "list" "locals" "long" "map" "max" "min" "object"
 410 			  "oct" "open" "ord" "pow" "property" "range"
 411 			  "raw_input" "reduce" "reload" "repr" "round"
 412 			  "setattr" "slice" "staticmethod" "str" "sum"
 413 			  "super" "tuple" "type" "unichr" "unicode" "vars"
 414 			  "xrange" "zip")
 415 			"\\|"))
 416 	(kw4 (mapconcat 'identity
 417 			;; Exceptions and warnings
 418 			'("ArithmeticError" "AssertionError"
 419 			  "AttributeError" "DeprecationWarning" "EOFError"
 420 			  "EnvironmentError" "Exception"
 421 			  "FloatingPointError" "FutureWarning" "IOError"
 422 			  "ImportError" "IndentationError" "IndexError"
 423 			  "KeyError" "KeyboardInterrupt" "LookupError"
 424 			  "MemoryError" "NameError" "NotImplemented"
 425 			  "NotImplementedError" "OSError" "OverflowError"
 426 			  "OverflowWarning" "PendingDeprecationWarning"
 427 			  "ReferenceError" "RuntimeError" "RuntimeWarning"
 428 			  "StandardError" "StopIteration" "SyntaxError"
 429 			  "SyntaxWarning" "SystemError" "SystemExit"
 430 			  "TabError" "TypeError" "UnboundLocalError"
 431 			  "UnicodeDecodeError" "UnicodeEncodeError"
 432 			  "UnicodeError" "UnicodeTranslateError"
 433 			  "UserWarning" "ValueError" "Warning"
 434 			  "ZeroDivisionError")
 435 			"\\|"))
 436 	)
 437     (list
 438      '("^[ \t]*\\(@.+\\)" 1 'py-decorators-face)
 439      ;; keywords
 440      (cons (concat "\\b\\(" kw1 "\\)\\b[ \n\t(]") 1)
 441      ;; builtins when they don't appear as object attributes
 442      (list (concat "\\([^. \t]\\|^\\)[ \t]*\\b\\(" kw3 "\\)\\b[ \n\t(]") 2
 443 	   'py-builtins-face)
 444      ;; block introducing keywords with immediately following colons.
 445      ;; Yes "except" is in both lists.
 446      (cons (concat "\\b\\(" kw2 "\\)[ \n\t(]") 1)
 447      ;; Exceptions
 448      (list (concat "\\b\\(" kw4 "\\)[ \n\t:,(]") 1 'py-builtins-face)
 449      ;; `as' but only in "import foo as bar"
 450      '("[ \t]*\\(\\bfrom\\b.*\\)?\\bimport\\b.*\\b\\(as\\)\\b" . 2)
 451      ;; classes
 452      '("\\bclass[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)" 1 font-lock-type-face)
 453      ;; functions
 454      '("\\bdef[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
 455        1 font-lock-function-name-face)
 456      ;; pseudo-keywords
 457      '("\\b\\(self\\|None\\|True\\|False\\|Ellipsis\\)\\b"
 458        1 py-pseudo-keyword-face)
 459      ))
 460   "Additional expressions to highlight in Python mode.")
 461 (put 'python-mode 'font-lock-defaults '(python-font-lock-keywords))
 462 
 463 ;; have to bind py-file-queue before installing the kill-emacs-hook
 464 (defvar py-file-queue nil
 465   "Queue of Python temp files awaiting execution.
 466 Currently-active file is at the head of the list.")
 467 
 468 (defvar py-pdbtrack-is-tracking-p nil)
 469 
 470 (defvar py-pychecker-history nil)
 471 
 472 
 473 
 474 ;; Constants
 475 
 476 (defconst py-stringlit-re
 477   (concat
 478    ;; These fail if backslash-quote ends the string (not worth
 479    ;; fixing?).  They precede the short versions so that the first two
 480    ;; quotes don't look like an empty short string.
 481    ;;
 482    ;; (maybe raw), long single quoted triple quoted strings (SQTQ),
 483    ;; with potential embedded single quotes
 484    "[rR]?'''[^']*\\(\\('[^']\\|''[^']\\)[^']*\\)*'''"
 485    "\\|"
 486    ;; (maybe raw), long double quoted triple quoted strings (DQTQ),
 487    ;; with potential embedded double quotes
 488    "[rR]?\"\"\"[^\"]*\\(\\(\"[^\"]\\|\"\"[^\"]\\)[^\"]*\\)*\"\"\""
 489    "\\|"
 490    "[rR]?'\\([^'\n\\]\\|\\\\.\\)*'"	; single-quoted
 491    "\\|"				; or
 492    "[rR]?\"\\([^\"\n\\]\\|\\\\.\\)*\""	; double-quoted
 493    )
 494   "Regular expression matching a Python string literal.")
 495 
 496 (defconst py-continued-re
 497   ;; This is tricky because a trailing backslash does not mean
 498   ;; continuation if it's in a comment
 499   (concat
 500    "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
 501    "\\\\$")
 502   "Regular expression matching Python backslash continuation lines.")
 503   
 504 (defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
 505   "Regular expression matching a blank or comment line.")
 506 
 507 (defconst py-outdent-re
 508   (concat "\\(" (mapconcat 'identity
 509 			   '("else:"
 510 			     "except\\(\\s +.*\\)?:"
 511 			     "finally:"
 512 			     "elif\\s +.*:")
 513 			   "\\|")
 514 	  "\\)")
 515   "Regular expression matching statements to be dedented one level.")
 516   
 517 (defconst py-block-closing-keywords-re
 518   "\\(return\\|raise\\|break\\|continue\\|pass\\)"
 519   "Regular expression matching keywords which typically close a block.")
 520 
 521 (defconst py-no-outdent-re
 522   (concat
 523    "\\("
 524    (mapconcat 'identity
 525 	      (list "try:"
 526 		    "except\\(\\s +.*\\)?:"
 527 		    "while\\s +.*:"
 528 		    "for\\s +.*:"
 529 		    "if\\s +.*:"
 530 		    "elif\\s +.*:"
 531 		    (concat py-block-closing-keywords-re "[ \t\n]")
 532 		    )
 533 	      "\\|")
 534 	  "\\)")
 535   "Regular expression matching lines not to dedent after.")
 536 
 537 (defconst py-traceback-line-re
 538   "[ \t]+File \"\\([^\"]+\\)\", line \\([0-9]+\\)"
 539   "Regular expression that describes tracebacks.")
 540 
 541 ;; pdbtrack constants
 542 (defconst py-pdbtrack-stack-entry-regexp
 543 ;  "^> \\([^(]+\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_]+\\)()"
 544   "^> \\(.*\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_]+\\)()"
 545   "Regular expression pdbtrack uses to find a stack trace entry.")
 546 
 547 (defconst py-pdbtrack-input-prompt "\n[(<]*[Pp]db[>)]+ "
 548   "Regular expression pdbtrack uses to recognize a pdb prompt.")
 549 
 550 (defconst py-pdbtrack-track-range 10000
 551   "Max number of characters from end of buffer to search for stack entry.")
 552 
 553 
 554 
 555 ;; Major mode boilerplate
 556 
 557 ;; define a mode-specific abbrev table for those who use such things
 558 (defvar python-mode-abbrev-table nil
 559   "Abbrev table in use in `python-mode' buffers.")
 560 (define-abbrev-table 'python-mode-abbrev-table nil)
 561 
 562 (defvar python-mode-hook nil
 563   "*Hook called by `python-mode'.")
 564 
 565 (defvar jpython-mode-hook nil
 566   "*Hook called by `jpython-mode'. `jpython-mode' also calls
 567 `python-mode-hook'.")
 568 
 569 (defvar py-shell-hook nil
 570   "*Hook called by `py-shell'.")
 571 
 572 ;; In previous version of python-mode.el, the hook was incorrectly
 573 ;; called py-mode-hook, and was not defvar'd.  Deprecate its use.
 574 (and (fboundp 'make-obsolete-variable)
 575      (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
 576 
 577 (defvar py-mode-map ()
 578   "Keymap used in `python-mode' buffers.")
 579 (if py-mode-map
 580     nil
 581   (setq py-mode-map (make-sparse-keymap))
 582   ;; electric keys
 583   (define-key py-mode-map ":" 'py-electric-colon)
 584   ;; indentation level modifiers
 585   (define-key py-mode-map "\C-c\C-l"  'py-shift-region-left)
 586   (define-key py-mode-map "\C-c\C-r"  'py-shift-region-right)
 587   (define-key py-mode-map "\C-c<"     'py-shift-region-left)
 588   (define-key py-mode-map "\C-c>"     'py-shift-region-right)
 589   ;; subprocess commands
 590   (define-key py-mode-map "\C-c\C-c"  'py-execute-buffer)
 591   (define-key py-mode-map "\C-c\C-m"  'py-execute-import-or-reload)
 592   (define-key py-mode-map "\C-c\C-s"  'py-execute-string)
 593   (define-key py-mode-map "\C-c|"     'py-execute-region)
 594   (define-key py-mode-map "\e\C-x"    'py-execute-def-or-class)
 595   (define-key py-mode-map "\C-c!"     'py-shell)
 596   (define-key py-mode-map "\C-c\C-t"  'py-toggle-shells)
 597   ;; Caution!  Enter here at your own risk.  We are trying to support
 598   ;; several behaviors and it gets disgusting. :-( This logic ripped
 599   ;; largely from CC Mode.
 600   ;;
 601   ;; In XEmacs 19, Emacs 19, and Emacs 20, we use this to bind
 602   ;; backwards deletion behavior to DEL, which both Delete and
 603   ;; Backspace get translated to.  There's no way to separate this
 604   ;; behavior in a clean way, so deal with it!  Besides, it's been
 605   ;; this way since the dawn of time.
 606   (if (not (boundp 'delete-key-deletes-forward))
 607       (define-key py-mode-map "\177" 'py-electric-backspace)
 608     ;; However, XEmacs 20 actually achieved enlightenment.  It is
 609     ;; possible to sanely define both backward and forward deletion
 610     ;; behavior under X separately (TTYs are forever beyond hope, but
 611     ;; who cares?  XEmacs 20 does the right thing with these too).
 612     (define-key py-mode-map [delete]    'py-electric-delete)
 613     (define-key py-mode-map [backspace] 'py-electric-backspace))
 614   ;; Separate M-BS from C-M-h.  The former should remain
 615   ;; backward-kill-word.
 616   (define-key py-mode-map [(control meta h)] 'py-mark-def-or-class)
 617   (define-key py-mode-map "\C-c\C-k"  'py-mark-block)
 618   ;; Miscellaneous
 619   (define-key py-mode-map "\C-c:"     'py-guess-indent-offset)
 620   (define-key py-mode-map "\C-c\t"    'py-indent-region)
 621   (define-key py-mode-map "\C-c\C-d"  'py-pdbtrack-toggle-stack-tracking)
 622   (define-key py-mode-map "\C-c\C-n"  'py-next-statement)
 623   (define-key py-mode-map "\C-c\C-p"  'py-previous-statement)
 624   (define-key py-mode-map "\C-c\C-u"  'py-goto-block-up)
 625   (define-key py-mode-map "\C-c#"     'py-comment-region)
 626   (define-key py-mode-map "\C-c?"     'py-describe-mode)
 627   (define-key py-mode-map "\C-c\C-h"  'py-help-at-point)
 628   (define-key py-mode-map "\e\C-a"    'py-beginning-of-def-or-class)
 629   (define-key py-mode-map "\e\C-e"    'py-end-of-def-or-class)
 630   (define-key py-mode-map "\C-c-"     'py-up-exception)
 631   (define-key py-mode-map "\C-c="     'py-down-exception)
 632   ;; stuff that is `standard' but doesn't interface well with
 633   ;; python-mode, which forces us to rebind to special commands
 634   (define-key py-mode-map "\C-xnd"    'py-narrow-to-defun)
 635   ;; information
 636   (define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
 637   (define-key py-mode-map "\C-c\C-v" 'py-version)
 638   (define-key py-mode-map "\C-c\C-w" 'py-pychecker-run)
 639   ;; shadow global bindings for newline-and-indent w/ the py- version.
 640   ;; BAW - this is extremely bad form, but I'm not going to change it
 641   ;; for now.
 642   (mapcar #'(lambda (key)
 643 	      (define-key py-mode-map key 'py-newline-and-indent))
 644 	  (where-is-internal 'newline-and-indent))
 645   ;; Force RET to be py-newline-and-indent even if it didn't get
 646   ;; mapped by the above code.  motivation: Emacs' default binding for
 647   ;; RET is `newline' and C-j is `newline-and-indent'.  Most Pythoneers
 648   ;; expect RET to do a `py-newline-and-indent' and any Emacsers who
 649   ;; dislike this are probably knowledgeable enough to do a rebind.
 650   ;; However, we do *not* change C-j since many Emacsers have already
 651   ;; swapped RET and C-j and they don't want C-j bound to `newline' to 
 652   ;; change.
 653   (define-key py-mode-map "\C-m" 'py-newline-and-indent)
 654   )
 655 
 656 (defvar py-mode-output-map nil
 657   "Keymap used in *Python Output* buffers.")
 658 (if py-mode-output-map
 659     nil
 660   (setq py-mode-output-map (make-sparse-keymap))
 661   (define-key py-mode-output-map [button2]  'py-mouseto-exception)
 662   (define-key py-mode-output-map "\C-c\C-c" 'py-goto-exception)
 663   ;; TBD: Disable all self-inserting keys.  This is bogus, we should
 664   ;; really implement this as *Python Output* buffer being read-only
 665   (mapcar #' (lambda (key)
 666 	       (define-key py-mode-output-map key
 667 		 #'(lambda () (interactive) (beep))))
 668 	     (where-is-internal 'self-insert-command))
 669   )
 670 
 671 (defvar py-shell-map nil
 672   "Keymap used in *Python* shell buffers.")
 673 (if py-shell-map
 674     nil
 675   (setq py-shell-map (copy-keymap comint-mode-map))
 676   (define-key py-shell-map [tab]   'tab-to-tab-stop)
 677   (define-key py-shell-map "\C-c-" 'py-up-exception)
 678   (define-key py-shell-map "\C-c=" 'py-down-exception)
 679   )
 680 
 681 (defvar py-mode-syntax-table nil
 682   "Syntax table used in `python-mode' buffers.")
 683 (when (not py-mode-syntax-table)
 684   (setq py-mode-syntax-table (make-syntax-table))
 685   (modify-syntax-entry ?\( "()" py-mode-syntax-table)
 686   (modify-syntax-entry ?\) ")(" py-mode-syntax-table)
 687   (modify-syntax-entry ?\[ "(]" py-mode-syntax-table)
 688   (modify-syntax-entry ?\] ")[" py-mode-syntax-table)
 689   (modify-syntax-entry ?\{ "(}" py-mode-syntax-table)
 690   (modify-syntax-entry ?\} "){" py-mode-syntax-table)
 691   ;; Add operator symbols misassigned in the std table
 692   (modify-syntax-entry ?\$ "."  py-mode-syntax-table)
 693   (modify-syntax-entry ?\% "."  py-mode-syntax-table)
 694   (modify-syntax-entry ?\& "."  py-mode-syntax-table)
 695   (modify-syntax-entry ?\* "."  py-mode-syntax-table)
 696   (modify-syntax-entry ?\+ "."  py-mode-syntax-table)
 697   (modify-syntax-entry ?\- "."  py-mode-syntax-table)
 698   (modify-syntax-entry ?\/ "."  py-mode-syntax-table)
 699   (modify-syntax-entry ?\< "."  py-mode-syntax-table)
 700   (modify-syntax-entry ?\= "."  py-mode-syntax-table)
 701   (modify-syntax-entry ?\> "."  py-mode-syntax-table)
 702   (modify-syntax-entry ?\| "."  py-mode-syntax-table)
 703   ;; For historical reasons, underscore is word class instead of
 704   ;; symbol class.  GNU conventions say it should be symbol class, but
 705   ;; there's a natural conflict between what major mode authors want
 706   ;; and what users expect from `forward-word' and `backward-word'.
 707   ;; Guido and I have hashed this out and have decided to keep
 708   ;; underscore in word class.  If you're tempted to change it, try
 709   ;; binding M-f and M-b to py-forward-into-nomenclature and
 710   ;; py-backward-into-nomenclature instead.  This doesn't help in all
 711   ;; situations where you'd want the different behavior
 712   ;; (e.g. backward-kill-word).
 713   (modify-syntax-entry ?\_ "w"  py-mode-syntax-table)
 714   ;; Both single quote and double quote are string delimiters
 715   (modify-syntax-entry ?\' "\"" py-mode-syntax-table)
 716   (modify-syntax-entry ?\" "\"" py-mode-syntax-table)
 717   ;; backquote is open and close paren
 718   (modify-syntax-entry ?\` "$"  py-mode-syntax-table)
 719   ;; comment delimiters
 720   (modify-syntax-entry ?\# "<"  py-mode-syntax-table)
 721   (modify-syntax-entry ?\n ">"  py-mode-syntax-table)
 722   )
 723 
 724 ;; An auxiliary syntax table which places underscore and dot in the
 725 ;; symbol class for simplicity
 726 (defvar py-dotted-expression-syntax-table nil
 727   "Syntax table used to identify Python dotted expressions.")
 728 (when (not py-dotted-expression-syntax-table)
 729   (setq py-dotted-expression-syntax-table
 730 	(copy-syntax-table py-mode-syntax-table))
 731   (modify-syntax-entry ?_ "_" py-dotted-expression-syntax-table)
 732   (modify-syntax-entry ?. "_" py-dotted-expression-syntax-table))
 733 
 734 
 735 
 736 ;; Utilities
 737 (defmacro py-safe (&rest body)
 738   "Safely execute BODY, return nil if an error occurred."
 739   (` (condition-case nil
 740 	 (progn (,@ body))
 741        (error nil))))
 742 
 743 (defsubst py-keep-region-active ()
 744   "Keep the region active in XEmacs."
 745   ;; Ignore byte-compiler warnings you might see.  Also note that
 746   ;; FSF's Emacs 19 does it differently; its policy doesn't require us
 747   ;; to take explicit action.
 748   (and (boundp 'zmacs-region-stays)
 749        (setq zmacs-region-stays t)))
 750 
 751 (defsubst py-point (position)
 752   "Returns the value of point at certain commonly referenced POSITIONs.
 753 POSITION can be one of the following symbols:
 754 
 755   bol  -- beginning of line
 756   eol  -- end of line
 757   bod  -- beginning of def or class
 758   eod  -- end of def or class
 759   bob  -- beginning of buffer
 760   eob  -- end of buffer
 761   boi  -- back to indentation
 762   bos  -- beginning of statement
 763 
 764 This function does not modify point or mark."
 765   (let ((here (point)))
 766     (cond
 767      ((eq position 'bol) (beginning-of-line))
 768      ((eq position 'eol) (end-of-line))
 769      ((eq position 'bod) (py-beginning-of-def-or-class 'either))
 770      ((eq position 'eod) (py-end-of-def-or-class 'either))
 771      ;; Kind of funny, I know, but useful for py-up-exception.
 772      ((eq position 'bob) (beginning-of-buffer))
 773      ((eq position 'eob) (end-of-buffer))
 774      ((eq position 'boi) (back-to-indentation))
 775      ((eq position 'bos) (py-goto-initial-line))
 776      (t (error "Unknown buffer position requested: %s" position))
 777      )
 778     (prog1
 779 	(point)
 780       (goto-char here))))
 781 
 782 (defsubst py-highlight-line (from to file line)
 783   (cond
 784    ((fboundp 'make-extent)
 785     ;; XEmacs
 786     (let ((e (make-extent from to)))
 787       (set-extent-property e 'mouse-face 'highlight)
 788       (set-extent-property e 'py-exc-info (cons file line))
 789       (set-extent-property e 'keymap py-mode-output-map)))
 790    (t
 791     ;; Emacs -- Please port this!
 792     )
 793    ))
 794 
 795 (defun py-in-literal (&optional lim)
 796   "Return non-nil if point is in a Python literal (a comment or string).
 797 Optional argument LIM indicates the beginning of the containing form,
 798 i.e. the limit on how far back to scan."
 799   ;; This is the version used for non-XEmacs, which has a nicer
 800   ;; interface.
 801   ;;
 802   ;; WARNING: Watch out for infinite recursion.
 803   (let* ((lim (or lim (py-point 'bod)))
 804 	 (state (parse-partial-sexp lim (point))))
 805     (cond
 806      ((nth 3 state) 'string)
 807      ((nth 4 state) 'comment)
 808      (t nil))))
 809 
 810 ;; XEmacs has a built-in function that should make this much quicker.
 811 ;; In this case, lim is ignored
 812 (defun py-fast-in-literal (&optional lim)
 813   "Fast version of `py-in-literal', used only by XEmacs.
 814 Optional LIM is ignored."
 815   ;; don't have to worry about context == 'block-comment
 816   (buffer-syntactic-context))
 817 
 818 (if (fboundp 'buffer-syntactic-context)
 819     (defalias 'py-in-literal 'py-fast-in-literal))
 820 
 821 
 822 
 823 ;; Menu definitions, only relevent if you have the easymenu.el package
 824 ;; (standard in the latest Emacs 19 and XEmacs 19 distributions).
 825 (defvar py-menu nil
 826   "Menu for Python Mode.
 827 This menu will get created automatically if you have the `easymenu'
 828 package.  Note that the latest X/Emacs releases contain this package.")
 829 
 830 (and (py-safe (require 'easymenu) t)
 831      (easy-menu-define
 832       py-menu py-mode-map "Python Mode menu"
 833       '("Python"
 834 	["Comment Out Region"   py-comment-region  (mark)]
 835 	["Uncomment Region"     (py-comment-region (point) (mark) '(4)) (mark)]
 836 	"-"
 837 	["Mark current block"   py-mark-block t]
 838 	["Mark current def"     py-mark-def-or-class t]
 839 	["Mark current class"   (py-mark-def-or-class t) t]
 840 	"-"
 841 	["Shift region left"    py-shift-region-left (mark)]
 842 	["Shift region right"   py-shift-region-right (mark)]
 843 	"-"
 844 	["Import/reload file"   py-execute-import-or-reload t]
 845 	["Execute buffer"       py-execute-buffer t]
 846 	["Execute region"       py-execute-region (mark)]
 847 	["Execute def or class" py-execute-def-or-class (mark)]
 848 	["Execute string"       py-execute-string t]
 849 	["Start interpreter..." py-shell t]
 850 	"-"
 851 	["Go to start of block" py-goto-block-up t]
 852 	["Go to start of class" (py-beginning-of-def-or-class t) t]
 853 	["Move to end of class" (py-end-of-def-or-class t) t]
 854 	["Move to start of def" py-beginning-of-def-or-class t]
 855 	["Move to end of def"   py-end-of-def-or-class t]
 856 	"-"
 857 	["Describe mode"        py-describe-mode t]
 858 	)))
 859 
 860 
 861 
 862 ;; Imenu definitions
 863 (defvar py-imenu-class-regexp
 864   (concat				; <<classes>>
 865    "\\("				;
 866    "^[ \t]*"				; newline and maybe whitespace
 867    "\\(class[ \t]+[a-zA-Z0-9_]+\\)"	; class name
 868 					; possibly multiple superclasses
 869    "\\([ \t]*\\((\\([a-zA-Z0-9_,. \t\n]\\)*)\\)?\\)"
 870    "[ \t]*:"				; and the final :
 871    "\\)"				; >>classes<<
 872    )
 873   "Regexp for Python classes for use with the Imenu package."
 874   )
 875 
 876 (defvar py-imenu-method-regexp
 877   (concat                               ; <<methods and functions>>
 878    "\\("                                ; 
 879    "^[ \t]*"                            ; new line and maybe whitespace
 880    "\\(def[ \t]+"                       ; function definitions start with def
 881    "\\([a-zA-Z0-9_]+\\)"                ;   name is here
 882 					;   function arguments...
 883 ;;   "[ \t]*(\\([-+/a-zA-Z0-9_=,\* \t\n.()\"'#]*\\))"
 884    "[ \t]*(\\([^:#]*\\))"
 885    "\\)"                                ; end of def
 886    "[ \t]*:"                            ; and then the :
 887    "\\)"                                ; >>methods and functions<<
 888    )
 889   "Regexp for Python methods/functions for use with the Imenu package."
 890   )
 891 
 892 (defvar py-imenu-method-no-arg-parens '(2 8)
 893   "Indices into groups of the Python regexp for use with Imenu.
 894 
 895 Using these values will result in smaller Imenu lists, as arguments to
 896 functions are not listed.
 897 
 898 See the variable `py-imenu-show-method-args-p' for more
 899 information.")
 900 
 901 (defvar py-imenu-method-arg-parens '(2 7)
 902   "Indices into groups of the Python regexp for use with imenu.
 903 Using these values will result in large Imenu lists, as arguments to
 904 functions are listed.
 905 
 906 See the variable `py-imenu-show-method-args-p' for more
 907 information.")
 908 
 909 ;; Note that in this format, this variable can still be used with the
 910 ;; imenu--generic-function. Otherwise, there is no real reason to have
 911 ;; it.
 912 (defvar py-imenu-generic-expression
 913   (cons
 914    (concat 
 915     py-imenu-class-regexp
 916     "\\|"				; or...
 917     py-imenu-method-regexp
 918     )
 919    py-imenu-method-no-arg-parens)
 920   "Generic Python expression which may be used directly with Imenu.
 921 Used by setting the variable `imenu-generic-expression' to this value.
 922 Also, see the function \\[py-imenu-create-index] for a better
 923 alternative for finding the index.")
 924 
 925 ;; These next two variables are used when searching for the Python
 926 ;; class/definitions. Just saving some time in accessing the
 927 ;; generic-python-expression, really.
 928 (defvar py-imenu-generic-regexp nil)
 929 (defvar py-imenu-generic-parens nil)
 930 
 931 
 932 (defun py-imenu-create-index-function ()
 933   "Python interface function for the Imenu package.
 934 Finds all Python classes and functions/methods. Calls function
 935 \\[py-imenu-create-index-engine].  See that function for the details
 936 of how this works."
 937   (setq py-imenu-generic-regexp (car py-imenu-generic-expression)
 938 	py-imenu-generic-parens (if py-imenu-show-method-args-p
 939 				    py-imenu-method-arg-parens
 940 				  py-imenu-method-no-arg-parens))
 941   (goto-char (point-min))
 942   ;; Warning: When the buffer has no classes or functions, this will
 943   ;; return nil, which seems proper according to the Imenu API, but
 944   ;; causes an error in the XEmacs port of Imenu.  Sigh.
 945   (py-imenu-create-index-engine nil))
 946 
 947 (defun py-imenu-create-index-engine (&optional start-indent)
 948   "Function for finding Imenu definitions in Python.
 949 
 950 Finds all definitions (classes, methods, or functions) in a Python
 951 file for the Imenu package.
 952 
 953 Returns a possibly nested alist of the form
 954 
 955 	(INDEX-NAME . INDEX-POSITION)
 956 
 957 The second element of the alist may be an alist, producing a nested
 958 list as in
 959 
 960 	(INDEX-NAME . INDEX-ALIST)
 961 
 962 This function should not be called directly, as it calls itself
 963 recursively and requires some setup.  Rather this is the engine for
 964 the function \\[py-imenu-create-index-function].
 965 
 966 It works recursively by looking for all definitions at the current
 967 indention level.  When it finds one, it adds it to the alist.  If it
 968 finds a definition at a greater indentation level, it removes the
 969 previous definition from the alist. In its place it adds all
 970 definitions found at the next indentation level.  When it finds a
 971 definition that is less indented then the current level, it returns
 972 the alist it has created thus far.
 973 
 974 The optional argument START-INDENT indicates the starting indentation
 975 at which to continue looking for Python classes, methods, or
 976 functions.  If this is not supplied, the function uses the indentation
 977 of the first definition found."
 978   (let (index-alist
 979 	sub-method-alist
 980 	looking-p
 981 	def-name prev-name
 982 	cur-indent def-pos
 983 	(class-paren (first  py-imenu-generic-parens)) 
 984 	(def-paren   (second py-imenu-generic-parens)))
 985     (setq looking-p
 986 	  (re-search-forward py-imenu-generic-regexp (point-max) t))
 987     (while looking-p
 988       (save-excursion
 989 	;; used to set def-name to this value but generic-extract-name
 990 	;; is new to imenu-1.14. this way it still works with
 991 	;; imenu-1.11
 992 	;;(imenu--generic-extract-name py-imenu-generic-parens))
 993 	(let ((cur-paren (if (match-beginning class-paren)
 994 			     class-paren def-paren)))
 995 	  (setq def-name
 996 		(buffer-substring-no-properties (match-beginning cur-paren)
 997 						(match-end cur-paren))))
 998 	(save-match-data
 999 	  (py-beginning-of-def-or-class 'either))
1000 	(beginning-of-line)
1001 	(setq cur-indent (current-indentation)))
1002       ;; HACK: want to go to the next correct definition location.  We
1003       ;; explicitly list them here but it would be better to have them
1004       ;; in a list.
1005       (setq def-pos
1006 	    (or (match-beginning class-paren)
1007 		(match-beginning def-paren)))
1008       ;; if we don't have a starting indent level, take this one
1009       (or start-indent
1010 	  (setq start-indent cur-indent))
1011       ;; if we don't have class name yet, take this one
1012       (or prev-name
1013 	  (setq prev-name def-name))
1014       ;; what level is the next definition on?  must be same, deeper
1015       ;; or shallower indentation
1016       (cond
1017        ;; Skip code in comments and strings
1018        ((py-in-literal))
1019        ;; at the same indent level, add it to the list...
1020        ((= start-indent cur-indent)
1021 	(push (cons def-name def-pos) index-alist))
1022        ;; deeper indented expression, recurse
1023        ((< start-indent cur-indent)
1024 	;; the point is currently on the expression we're supposed to
1025 	;; start on, so go back to the last expression. The recursive
1026 	;; call will find this place again and add it to the correct
1027 	;; list
1028 	(re-search-backward py-imenu-generic-regexp (point-min) 'move)
1029 	(setq sub-method-alist (py-imenu-create-index-engine cur-indent))
1030 	(if sub-method-alist
1031 	    ;; we put the last element on the index-alist on the start
1032 	    ;; of the submethod alist so the user can still get to it.
1033 	    (let ((save-elmt (pop index-alist)))
1034 	      (push (cons prev-name
1035 			  (cons save-elmt sub-method-alist))
1036 		    index-alist))))
1037        ;; found less indented expression, we're done.
1038        (t 
1039 	(setq looking-p nil)
1040 	(re-search-backward py-imenu-generic-regexp (point-min) t)))
1041       ;; end-cond
1042       (setq prev-name def-name)
1043       (and looking-p
1044 	   (setq looking-p
1045 		 (re-search-forward py-imenu-generic-regexp
1046 				    (point-max) 'move))))
1047     (nreverse index-alist)))
1048 
1049 
1050 
1051 (defun py-choose-shell-by-shebang ()
1052   "Choose CPython or JPython mode by looking at #! on the first line.
1053 Returns the appropriate mode function.
1054 Used by `py-choose-shell', and similar to but distinct from
1055 `set-auto-mode', though it uses `auto-mode-interpreter-regexp' (if available)."
1056   ;; look for an interpreter specified in the first line
1057   ;; similar to set-auto-mode (files.el)
1058   (let* ((re (if (boundp 'auto-mode-interpreter-regexp)
1059 		 auto-mode-interpreter-regexp
1060 	       ;; stolen from Emacs 21.2
1061 	       "#![ \t]?\\([^ \t\n]*/bin/env[ \t]\\)?\\([^ \t\n]+\\)"))
1062 	 (interpreter (save-excursion
1063 			(goto-char (point-min))
1064 			(if (looking-at re)
1065 			    (match-string 2)
1066 			  "")))
1067 	 elt)
1068     ;; Map interpreter name to a mode.
1069     (setq elt (assoc (file-name-nondirectory interpreter)
1070 		     py-shell-alist))
1071     (and elt (caddr elt))))
1072 
1073 
1074 
1075 (defun py-choose-shell-by-import ()
1076   "Choose CPython or JPython mode based imports.
1077 If a file imports any packages in `py-jpython-packages', within
1078 `py-import-check-point-max' characters from the start of the file,
1079 return `jpython', otherwise return nil."
1080   (let (mode)
1081     (save-excursion
1082       (goto-char (point-min))
1083       (while (and (not mode)
1084 		  (search-forward-regexp
1085 		   "^\\(\\(from\\)\\|\\(import\\)\\) \\([^ \t\n.]+\\)"
1086 		   py-import-check-point-max t))
1087 	(setq mode (and (member (match-string 4) py-jpython-packages)
1088 			'jpython
1089 			))))
1090     mode))
1091 
1092 
1093 (defun py-choose-shell ()
1094   "Choose CPython or JPython mode. Returns the appropriate mode function.
1095 This does the following:
1096  - look for an interpreter with `py-choose-shell-by-shebang'
1097  - examine imports using `py-choose-shell-by-import'
1098  - default to the variable `py-default-interpreter'"
1099   (interactive)
1100   (or (py-choose-shell-by-shebang)
1101       (py-choose-shell-by-import)
1102       py-default-interpreter
1103 ;      'cpython ;; don't use to py-default-interpreter, because default
1104 ;               ;; is only way to choose CPython
1105       ))
1106 
1107 
1108 ;;;###autoload
1109 (defun python-mode ()
1110   "Major mode for editing Python files.
1111 To submit a problem report, enter `\\[py-submit-bug-report]' from a
1112 `python-mode' buffer.  Do `\\[py-describe-mode]' for detailed
1113 documentation.  To see what version of `python-mode' you are running,
1114 enter `\\[py-version]'.
1115 
1116 This mode knows about Python indentation, tokens, comments and
1117 continuation lines.  Paragraphs are separated by blank lines only.
1118 
1119 COMMANDS
1120 \\{py-mode-map}
1121 VARIABLES
1122 
1123 py-indent-offset\t\tindentation increment
1124 py-block-comment-prefix\t\tcomment string used by `comment-region'
1125 py-python-command\t\tshell command to invoke Python interpreter
1126 py-temp-directory\t\tdirectory used for temp files (if needed)
1127 py-beep-if-tab-change\t\tring the bell if `tab-width' is changed"
1128   (interactive)
1129   ;; set up local variables
1130   (kill-all-local-variables)
1131   (make-local-variable 'font-lock-defaults)
1132   (make-local-variable 'paragraph-separate)
1133   (make-local-variable 'paragraph-start)
1134   (make-local-variable 'require-final-newline)
1135   (make-local-variable 'comment-start)
1136   (make-local-variable 'comment-end)
1137   (make-local-variable 'comment-start-skip)
1138   (make-local-variable 'comment-column)
1139   (make-local-variable 'comment-indent-function)
1140   (make-local-variable 'indent-region-function)
1141   (make-local-variable 'indent-line-function)
1142   (make-local-variable 'add-log-current-defun-function)
1143   (make-local-variable 'fill-paragraph-function)
1144   ;;
1145   (set-syntax-table py-mode-syntax-table)
1146   (setq major-mode              'python-mode
1147 	mode-name               "Python"
1148 	local-abbrev-table      python-mode-abbrev-table
1149 	font-lock-defaults      '(python-font-lock-keywords)
1150 	paragraph-separate      "^[ \t]*$"
1151 	paragraph-start         "^[ \t]*$"
1152 	require-final-newline   t
1153 	comment-start           "# "
1154 	comment-end             ""
1155 	comment-start-skip      "# *"
1156 	comment-column          40
1157 	comment-indent-function 'py-comment-indent-function
1158 	indent-region-function  'py-indent-region
1159 	indent-line-function    'py-indent-line
1160 	;; tell add-log.el how to find the current function/method/variable
1161 	add-log-current-defun-function 'py-current-defun
1162 
1163 	fill-paragraph-function 'py-fill-paragraph
1164 	)
1165   (use-local-map py-mode-map)
1166   ;; add the menu
1167   (if py-menu
1168       (easy-menu-add py-menu))
1169   ;; Emacs 19 requires this
1170   (if (boundp 'comment-multi-line)
1171       (setq comment-multi-line nil))
1172   ;; Install Imenu if available
1173   (when (py-safe (require 'imenu))
1174     (setq imenu-create-index-function #'py-imenu-create-index-function)
1175     (setq imenu-generic-expression py-imenu-generic-expression)
1176     (if (fboundp 'imenu-add-to-menubar)
1177 	(imenu-add-to-menubar (format "%s-%s" "IM" mode-name)))
1178     )
1179   ;; Run the mode hook.  Note that py-mode-hook is deprecated.
1180   (if python-mode-hook
1181       (run-hooks 'python-mode-hook)
1182     (run-hooks 'py-mode-hook))
1183   ;; Now do the automagical guessing
1184   (if py-smart-indentation
1185     (let ((offset py-indent-offset))
1186       ;; It's okay if this fails to guess a good value
1187       (if (and (py-safe (py-guess-indent-offset))
1188 	       (<= py-indent-offset 8)
1189 	       (>= py-indent-offset 2))
1190 	  (setq offset py-indent-offset))
1191       (setq py-indent-offset offset)
1192       ;; Only turn indent-tabs-mode off if tab-width !=
1193       ;; py-indent-offset.  Never turn it on, because the user must
1194       ;; have explicitly turned it off.
1195       (if (/= tab-width py-indent-offset)
1196 	  (setq indent-tabs-mode nil))
1197       ))
1198   ;; Set the default shell if not already set
1199   (when (null py-which-shell)
1200     (py-toggle-shells (py-choose-shell))))
1201 
1202 
1203 (defun jpython-mode ()
1204   "Major mode for editing JPython/Jython files.
1205 This is a simple wrapper around `python-mode'.
1206 It runs `jpython-mode-hook' then calls `python-mode.'
1207 It is added to `interpreter-mode-alist' and `py-choose-shell'.
1208 "
1209   (interactive)
1210   (python-mode)
1211   (py-toggle-shells 'jpython)
1212   (when jpython-mode-hook
1213       (run-hooks 'jpython-mode-hook)))
1214 
1215 
1216 ;; It's handy to add recognition of Python files to the
1217 ;; interpreter-mode-alist and to auto-mode-alist.  With the former, we
1218 ;; can specify different `derived-modes' based on the #! line, but
1219 ;; with the latter, we can't.  So we just won't add them if they're
1220 ;; already added.
1221 ;;;###autoload
1222 (let ((modes '(("jpython" . jpython-mode)
1223 	       ("jython" . jpython-mode)
1224 	       ("python" . python-mode))))
1225   (while modes
1226     (when (not (assoc (car modes) interpreter-mode-alist))
1227       (push (car modes) interpreter-mode-alist))
1228     (setq modes (cdr modes))))
1229 ;;;###autoload
1230 (when (not (or (rassq 'python-mode auto-mode-alist)
1231 	       (rassq 'jpython-mode auto-mode-alist)))
1232   (push '("\\.py$" . python-mode) auto-mode-alist))
1233 
1234 
1235 
1236 ;; electric characters
1237 (defun py-outdent-p ()
1238   "Returns non-nil if the current line should dedent one level."
1239   (save-excursion
1240     (and (progn (back-to-indentation)
1241 		(looking-at py-outdent-re))
1242 	 ;; short circuit infloop on illegal construct
1243 	 (not (bobp))
1244 	 (progn (forward-line -1)
1245 		(py-goto-initial-line)
1246 		(back-to-indentation)
1247 		(while (or (looking-at py-blank-or-comment-re)
1248 			   (bobp))
1249 		  (backward-to-indentation 1))
1250 		(not (looking-at py-no-outdent-re)))
1251 	 )))
1252 
1253 (defun py-electric-colon (arg)
1254   "Insert a colon.
1255 In certain cases the line is dedented appropriately.  If a numeric
1256 argument ARG is provided, that many colons are inserted
1257 non-electrically.  Electric behavior is inhibited inside a string or
1258 comment."
1259   (interactive "*P")
1260   (self-insert-command (prefix-numeric-value arg))
1261   ;; are we in a string or comment?
1262   (if (save-excursion
1263 	(let ((pps (parse-partial-sexp (save-excursion
1264 					 (py-beginning-of-def-or-class)
1265 					 (point))
1266 				       (point))))
1267 	  (not (or (nth 3 pps) (nth 4 pps)))))
1268       (save-excursion
1269 	(let ((here (point))
1270 	      (outdent 0)
1271 	      (indent (py-compute-indentation t)))
1272 	  (if (and (not arg)
1273 		   (py-outdent-p)
1274 		   (= indent (save-excursion
1275 			       (py-next-statement -1)
1276 			       (py-compute-indentation t)))
1277 		   )
1278 	      (setq outdent py-indent-offset))
1279 	  ;; Don't indent, only dedent.  This assumes that any lines
1280 	  ;; that are already dedented relative to
1281 	  ;; py-compute-indentation were put there on purpose.  It's
1282 	  ;; highly annoying to have `:' indent for you.  Use TAB, C-c
1283 	  ;; C-l or C-c C-r to adjust.  TBD: Is there a better way to
1284 	  ;; determine this???
1285 	  (if (< (current-indentation) indent) nil
1286 	    (goto-char here)
1287 	    (beginning-of-line)
1288 	    (delete-horizontal-space)
1289 	    (indent-to (- indent outdent))
1290 	    )))))
1291 
1292 
1293 ;; Python subprocess utilities and filters
1294 (defun py-execute-file (proc filename)
1295   "Send to Python interpreter process PROC \"execfile('FILENAME')\".
1296 Make that process's buffer visible and force display.  Also make
1297 comint believe the user typed this string so that
1298 `kill-output-from-shell' does The Right Thing."
1299   (let ((curbuf (current-buffer))
1300 	(procbuf (process-buffer proc))
1301 ;	(comint-scroll-to-bottom-on-output t)
1302 	(msg (format "## working on region in file %s...\n" filename))
1303 	(cmd (format "execfile(r'%s')\n" filename)))
1304     (unwind-protect
1305 	(save-excursion
1306 	  (set-buffer procbuf)
1307 	  (goto-char (point-max))
1308 	  (move-marker (process-mark proc) (point))
1309 	  (funcall (process-filter proc) proc msg))
1310       (set-buffer curbuf))
1311     (process-send-string proc cmd)))
1312 
1313 (defun py-comint-output-filter-function (string)
1314   "Watch output for Python prompt and exec next file waiting in queue.
1315 This function is appropriate for `comint-output-filter-functions'."
1316   ;; TBD: this should probably use split-string
1317   (when (and (or (string-equal string ">>> ")
1318 		 (and (>= (length string) 5)
1319 		      (string-equal (substring string -5) "\n>>> ")))
1320 	     py-file-queue)
1321     (pop-to-buffer (current-buffer))
1322     (py-safe (delete-file (car py-file-queue)))
1323     (setq py-file-queue (cdr py-file-queue))
1324     (if py-file-queue
1325 	(let ((pyproc (get-buffer-process (current-buffer))))
1326 	  (py-execute-file pyproc (car py-file-queue))))
1327     ))
1328 
1329 (defun py-pdbtrack-overlay-arrow (activation)
1330   "Activate or de arrow at beginning-of-line in current buffer."
1331   ;; This was derived/simplified from edebug-overlay-arrow
1332   (cond (activation
1333 	 (setq overlay-arrow-position (make-marker))
1334 	 (setq overlay-arrow-string "=>")
1335 	 (set-marker overlay-arrow-position (py-point 'bol) (current-buffer))
1336 	 (setq py-pdbtrack-is-tracking-p t))
1337 	(overlay-arrow-position
1338 	 (setq overlay-arrow-position nil)
1339 	 (setq py-pdbtrack-is-tracking-p nil))
1340 	))
1341 
1342 (defun py-pdbtrack-track-stack-file (text)
1343   "Show the file indicated by the pdb stack entry line, in a separate window.
1344 
1345 Activity is disabled if the buffer-local variable
1346 `py-pdbtrack-do-tracking-p' is nil.
1347 
1348 We depend on the pdb input prompt matching `py-pdbtrack-input-prompt'
1349 at the beginning of the line.
1350 
1351 If the traceback target file path is invalid, we look for the most
1352 recently visited python-mode buffer which either has the name of the
1353 current function \(or class) or which defines the function \(or
1354 class).  This is to provide for remote scripts, eg, Zope's 'Script
1355 (Python)' - put a _copy_ of the script in a buffer named for the
1356 script, and set to python-mode, and pdbtrack will find it.)"
1357   ;; Instead of trying to piece things together from partial text
1358   ;; (which can be almost useless depending on Emacs version), we
1359   ;; monitor to the point where we have the next pdb prompt, and then
1360   ;; check all text from comint-last-input-end to process-mark.
1361   ;;
1362   ;; Also, we're very conservative about clearing the overlay arrow,
1363   ;; to minimize residue.  This means, for instance, that executing
1364   ;; other pdb commands wipe out the highlight.  You can always do a
1365   ;; 'where' (aka 'w') command to reveal the overlay arrow.
1366   (let* ((origbuf (current-buffer))
1367 	 (currproc (get-buffer-process origbuf)))
1368 
1369     (if (not (and currproc py-pdbtrack-do-tracking-p))
1370         (py-pdbtrack-overlay-arrow nil)
1371 
1372       (let* ((procmark (process-mark currproc))
1373              (block (buffer-substring (max comint-last-input-end
1374                                            (- procmark
1375                                               py-pdbtrack-track-range))
1376                                       procmark))
1377              target target_fname target_lineno target_buffer)
1378 
1379         (if (not (string-match (concat py-pdbtrack-input-prompt "$") block))
1380             (py-pdbtrack-overlay-arrow nil)
1381 
1382           (setq target (py-pdbtrack-get-source-buffer block))
1383 
1384           (if (stringp target)
1385               (message "pdbtrack: %s" target)
1386 
1387             (setq target_lineno (car target))
1388             (setq target_buffer (cadr target))
1389             (setq target_fname (buffer-file-name target_buffer))
1390             (switch-to-buffer-other-window target_buffer)
1391             (goto-line target_lineno)
1392             (message "pdbtrack: line %s, file %s" target_lineno target_fname)
1393             (py-pdbtrack-overlay-arrow t)
1394             (pop-to-buffer origbuf t)
1395 
1396             )))))
1397   )
1398 
1399 (defun py-pdbtrack-get-source-buffer (block)
1400   "Return line number and buffer of code indicated by block's traceback text.
1401 
1402 We look first to visit the file indicated in the trace.
1403 
1404 Failing that, we look for the most recently visited python-mode buffer
1405 with the same name or having 
1406 having the named function.
1407 
1408 If we're unable find the source code we return a string describing the
1409 problem as best as we can determine."
1410 
1411   (if (not (string-match py-pdbtrack-stack-entry-regexp block))
1412 
1413       "Traceback cue not found"
1414 
1415     (let* ((filename (match-string 1 block))
1416            (lineno (string-to-int (match-string 2 block)))
1417            (funcname (match-string 3 block))
1418            funcbuffer)
1419 
1420       (cond ((file-exists-p filename)
1421              (list lineno (find-file-noselect filename)))
1422 
1423             ((setq funcbuffer (py-pdbtrack-grub-for-buffer funcname lineno))
1424              (if (string-match "/Script (Python)$" filename)
1425                  ;; Add in number of lines for leading '##' comments:
1426                  (setq lineno
1427                        (+ lineno
1428                           (save-excursion
1429                             (set-buffer funcbuffer)
1430                             (count-lines
1431                              (point-min)
1432                              (max (point-min)
1433                                   (string-match "^\\([^#]\\|#[^#]\\|#$\\)"
1434                                                 (buffer-substring (point-min)
1435                                                                   (point-max)))
1436                                   ))))))
1437              (list lineno funcbuffer))
1438 
1439             ((= (elt filename 0) ?\<)
1440              (format "(Non-file source: '%s')" filename))
1441 
1442             (t (format "Not found: %s(), %s" funcname filename)))
1443       )
1444     )
1445   )
1446 
1447 (defun py-pdbtrack-grub-for-buffer (funcname lineno)
1448   "Find most recent buffer itself named or having function funcname.
1449 
1450 We walk the buffer-list history for python-mode buffers that are
1451 named for funcname or define a function funcname."
1452   (let ((buffers (buffer-list))
1453         buf
1454         got)
1455     (while (and buffers (not got))
1456       (setq buf (car buffers)
1457             buffers (cdr buffers))
1458       (if (and (save-excursion (set-buffer buf)
1459                                (string= major-mode "python-mode"))
1460                (or (string-match funcname (buffer-name buf))
1461                    (string-match (concat "^\\s-*\\(def\\|class\\)\\s-+"
1462                                          funcname "\\s-*(")
1463                                  (save-excursion
1464                                    (set-buffer buf)
1465                                    (buffer-substring (point-min)
1466                                                      (point-max))))))
1467           (setq got buf)))
1468     got))
1469 
1470 (defun py-postprocess-output-buffer (buf)
1471   "Highlight exceptions found in BUF.
1472 If an exception occurred return t, otherwise return nil.  BUF must exist."
1473   (let (line file bol err-p)
1474     (save-excursion
1475       (set-buffer buf)
1476       (beginning-of-buffer)
1477       (while (re-search-forward py-traceback-line-re nil t)
1478 	(setq file (match-string 1)
1479 	      line (string-to-int (match-string 2))
1480 	      bol (py-point 'bol))
1481 	(py-highlight-line bol (py-point 'eol) file line)))
1482     (when (and py-jump-on-exception line)
1483       (beep)
1484       (py-jump-to-exception file line)
1485       (setq err-p t))
1486     err-p))
1487 
1488 
1489 
1490 ;;; Subprocess commands
1491 
1492 ;; only used when (memq 'broken-temp-names py-emacs-features)
1493 (defvar py-serial-number 0)
1494 (defvar py-exception-buffer nil)
1495 (defconst py-output-buffer "*Python Output*")
1496 (make-variable-buffer-local 'py-output-buffer)
1497 
1498 ;; for toggling between CPython and JPython
1499 (defvar py-which-shell nil)
1500 (defvar py-which-args  py-python-command-args)
1501 (defvar py-which-bufname "Python")
1502 (make-variable-buffer-local 'py-which-shell)
1503 (make-variable-buffer-local 'py-which-args)
1504 (make-variable-buffer-local 'py-which-bufname)
1505 
1506 (defun py-toggle-shells (arg)
1507   "Toggles between the CPython and JPython shells.
1508 
1509 With positive argument ARG (interactively \\[universal-argument]),
1510 uses the CPython shell, with negative ARG uses the JPython shell, and
1511 with a zero argument, toggles the shell.
1512 
1513 Programmatically, ARG can also be one of the symbols `cpython' or
1514 `jpython', equivalent to positive arg and negative arg respectively."
1515   (interactive "P")
1516   ;; default is to toggle
1517   (if (null arg)
1518       (setq arg 0))
1519   ;; preprocess arg
1520   (cond
1521    ((equal arg 0)
1522     ;; toggle
1523     (if (string-equal py-which-bufname "Python")
1524 	(setq arg -1)
1525       (setq arg 1)))
1526    ((equal arg 'cpython) (setq arg 1))
1527    ((equal arg 'jpython) (setq arg -1)))
1528   (let (msg)
1529     (cond
1530      ((< 0 arg)
1531       ;; set to CPython
1532       (setq py-which-shell py-python-command
1533 	    py-which-args py-python-command-args
1534 	    py-which-bufname "Python"
1535 	    msg "CPython"
1536 	    mode-name "Python"))
1537      ((> 0 arg)
1538       (setq py-which-shell py-jpython-command
1539 	    py-which-args py-jpython-command-args
1540 	    py-which-bufname "JPython"
1541 	    msg "JPython"
1542 	    mode-name "JPython"))
1543      )
1544     (message "Using the %s shell" msg)
1545     (setq py-output-buffer (format "*%s Output*" py-which-bufname))))
1546 
1547 ;;;###autoload
1548 (defun py-shell (&optional argprompt)
1549   "Start an interactive Python interpreter in another window.
1550 This is like Shell mode, except that Python is running in the window
1551 instead of a shell.  See the `Interactive Shell' and `Shell Mode'
1552 sections of the Emacs manual for details, especially for the key
1553 bindings active in the `*Python*' buffer.
1554 
1555 With optional \\[universal-argument], the user is prompted for the
1556 flags to pass to the Python interpreter.  This has no effect when this
1557 command is used to switch to an existing process, only when a new
1558 process is started.  If you use this, you will probably want to ensure
1559 that the current arguments are retained (they will be included in the
1560 prompt).  This argument is ignored when this function is called
1561 programmatically, or when running in Emacs 19.34 or older.
1562 
1563 Note: You can toggle between using the CPython interpreter and the
1564 JPython interpreter by hitting \\[py-toggle-shells].  This toggles
1565 buffer local variables which control whether all your subshell
1566 interactions happen to the `*JPython*' or `*Python*' buffers (the
1567 latter is the name used for the CPython buffer).
1568 
1569 Warning: Don't use an interactive Python if you change sys.ps1 or
1570 sys.ps2 from their default values, or if you're running code that
1571 prints `>>> ' or `... ' at the start of a line.  `python-mode' can't
1572 distinguish your output from Python's output, and assumes that `>>> '
1573 at the start of a line is a prompt from Python.  Similarly, the Emacs
1574 Shell mode code assumes that both `>>> ' and `... ' at the start of a
1575 line are Python prompts.  Bad things can happen if you fool either
1576 mode.
1577 
1578 Warning:  If you do any editing *in* the process buffer *while* the
1579 buffer is accepting output from Python, do NOT attempt to `undo' the
1580 changes.  Some of the output (nowhere near the parts you changed!) may
1581 be lost if you do.  This appears to be an Emacs bug, an unfortunate
1582 interaction between undo and process filters; the same problem exists in
1583 non-Python process buffers using the default (Emacs-supplied) process
1584 filter."
1585   (interactive "P")
1586   ;; Set the default shell if not already set
1587   (when (null py-which-shell)
1588     (py-toggle-shells py-default-interpreter))
1589   (let ((args py-which-args))
1590     (when (and argprompt
1591 	       (interactive-p)
1592 	       (fboundp 'split-string))
1593       ;; TBD: Perhaps force "-i" in the final list?
1594       (setq args (split-string
1595 		  (read-string (concat py-which-bufname
1596 				       " arguments: ")
1597 			       (concat
1598 				(mapconcat 'identity py-which-args " ") " ")
1599 			       ))))
1600     (switch-to-buffer-other-window
1601      (apply 'make-comint py-which-bufname py-which-shell nil args))
1602     (make-local-variable 'comint-prompt-regexp)
1603     (setq comint-prompt-regexp "^>>> \\|^[.][.][.] \\|^(pdb) ")
1604     (add-hook 'comint-output-filter-functions
1605 	      'py-comint-output-filter-function)
1606     ;; pdbtrack
1607     (add-hook 'comint-output-filter-functions 'py-pdbtrack-track-stack-file)
1608     (setq py-pdbtrack-do-tracking-p t)
1609     (set-syntax-table py-mode-syntax-table)
1610     (use-local-map py-shell-map)
1611     (run-hooks 'py-shell-hook)
1612     ))
1613 
1614 (defun py-clear-queue ()
1615   "Clear the queue of temporary files waiting to execute."
1616   (interactive)
1617   (let ((n (length py-file-queue)))
1618     (mapcar 'delete-file py-file-queue)
1619     (setq py-file-queue nil)
1620     (message "%d pending files de-queued." n)))
1621 
1622 
1623 (defun py-execute-region (start end &optional async)
1624   "Execute the region in a Python interpreter.
1625 
1626 The region is first copied into a temporary file (in the directory
1627 `py-temp-directory').  If there is no Python interpreter shell
1628 running, this file is executed synchronously using
1629 `shell-command-on-region'.  If the program is long running, use
1630 \\[universal-argument] to run the command asynchronously in its own
1631 buffer.
1632 
1633 When this function is used programmatically, arguments START and END
1634 specify the region to execute, and optional third argument ASYNC, if
1635 non-nil, specifies to run the command asynchronously in its own
1636 buffer.
1637 
1638 If the Python interpreter shell is running, the region is execfile()'d
1639 in that shell.  If you try to execute regions too quickly,
1640 `python-mode' will queue them up and execute them one at a time when
1641 it sees a `>>> ' prompt from Python.  Each time this happens, the
1642 process buffer is popped into a window (if it's not already in some
1643 window) so you can see it, and a comment of the form
1644 
1645     \t## working on region in file <name>...
1646 
1647 is inserted at the end.  See also the command `py-clear-queue'."
1648   (interactive "r\nP")
1649   ;; Skip ahead to the first non-blank line
1650   (let* ((proc (get-process py-which-bufname))
1651 	 (temp (if (memq 'broken-temp-names py-emacs-features)
1652 		   (let
1653 		       ((sn py-serial-number)
1654 			(pid (and (fboundp 'emacs-pid) (emacs-pid))))
1655 		     (setq py-serial-number (1+ py-serial-number))
1656 		     (if pid
1657 			 (format "python-%d-%d" sn pid)
1658 		       (format "python-%d" sn)))
1659 		 (make-temp-name "python-")))
1660 	 (file (concat (expand-file-name temp py-temp-directory) ".py"))
1661 	 (cur (current-buffer))
1662 	 (buf (get-buffer-create file))
1663 	 shell)
1664     ;; Write the contents of the buffer, watching out for indented regions.
1665     (save-excursion
1666       (goto-char start)
1667       (beginning-of-line)
1668       (while (and (looking-at "\\s *$")
1669 		  (< (point) end))
1670 	(forward-line 1))
1671       (setq start (point))
1672       (or (< start end)
1673 	  (error "Region is empty"))
1674       (let ((needs-if (/= (py-point 'bol) (py-point 'boi))))
1675 	(set-buffer buf)
1676 	(python-mode)
1677 	(when needs-if
1678 	  (insert "if 1:\n"))
1679 	(insert-buffer-substring cur start end)
1680 	;; Set the shell either to the #! line command, or to the
1681 	;; py-which-shell buffer local variable.
1682 	(setq shell (or (py-choose-shell-by-shebang)
1683 			(py-choose-shell-by-import)
1684 			py-which-shell))))
1685     (cond
1686      ;; always run the code in its own asynchronous subprocess
1687      (async
1688       ;; User explicitly wants this to run in its own async subprocess
1689       (save-excursion
1690 	(set-buffer buf)
1691 	(write-region (point-min) (point-max) file nil 'nomsg))
1692       (let* ((buf (generate-new-buffer-name py-output-buffer))
1693 	     ;; TBD: a horrible hack, but why create new Custom variables?
1694 	     (arg (if (string-equal py-which-bufname "Python")
1695 		      "-u" "")))
1696 	(start-process py-which-bufname buf shell arg file)
1697 	(pop-to-buffer buf)
1698 	(py-postprocess-output-buffer buf)
1699 	;; TBD: clean up the temporary file!
1700 	))
1701      ;; if the Python interpreter shell is running, queue it up for
1702      ;; execution there.
1703      (proc
1704       ;; use the existing python shell
1705       (save-excursion
1706 	(set-buffer buf)
1707 	(write-region (point-min) (point-max) file nil 'nomsg))
1708       (if (not py-file-queue)
1709 	  (py-execute-file proc file)
1710 	(message "File %s queued for execution" file))
1711       (setq py-file-queue (append py-file-queue (list file)))
1712       (setq py-exception-buffer (cons file (current-buffer))))
1713      (t
1714       ;; TBD: a horrible hack, but why create new Custom variables?
1715       (let ((cmd (concat shell (if (string-equal py-which-bufname "JPython")
1716 				   " -" ""))))
1717 	;; otherwise either run it synchronously in a subprocess
1718 	(save-excursion
1719 	  (set-buffer buf)
1720 	  (shell-command-on-region (point-min) (point-max)
1721 				   cmd py-output-buffer))
1722 	;; shell-command-on-region kills the output buffer if it never
1723 	;; existed and there's no output from the command
1724 	(if (not (get-buffer py-output-buffer))
1725 	    (message "No output.")
1726 	  (setq py-exception-buffer (current-buffer))
1727 	  (let ((err-p (py-postprocess-output-buffer py-output-buffer)))
1728 	    (pop-to-buffer py-output-buffer)
1729 	    (if err-p
1730 		(pop-to-buffer py-exception-buffer)))
1731 	  ))
1732       ))
1733     ;; Clean up after ourselves.
1734     (kill-buffer buf)))
1735 
1736 
1737 ;; Code execution commands
1738 (defun py-execute-buffer (&optional async)
1739   "Send the contents of the buffer to a Python interpreter.
1740 If the file local variable `py-master-file' is non-nil, execute the
1741 named file instead of the buffer's file.
1742 
1743 If there is a *Python* process buffer it is used.  If a clipping
1744 restriction is in effect, only the accessible portion of the buffer is
1745 sent.  A trailing newline will be supplied if needed.
1746 
1747 See the `\\[py-execute-region]' docs for an account of some
1748 subtleties, including the use of the optional ASYNC argument."
1749   (interactive "P")
1750   (if py-master-file
1751       (let* ((filename (expand-file-name py-master-file))
1752 	     (buffer (or (get-file-buffer filename)
1753 			 (find-file-noselect filename))))
1754 	(set-buffer buffer)))
1755   (py-execute-region (point-min) (point-max) async))
1756 
1757 (defun py-execute-import-or-reload (&optional async)
1758   "Import the current buffer's file in a Python interpreter.
1759 
1760 If the file has already been imported, then do reload instead to get
1761 the latest version.
1762 
1763 If the file's name does not end in \".py\", then do execfile instead.
1764 
1765 If the current buffer is not visiting a file, do `py-execute-buffer'
1766 instead.
1767 
1768 If the file local variable `py-master-file' is non-nil, import or
1769 reload the named file instead of the buffer's file.  The file may be
1770 saved based on the value of `py-execute-import-or-reload-save-p'.
1771 
1772 See the `\\[py-execute-region]' docs for an account of some
1773 subtleties, including the use of the optional ASYNC argument.
1774 
1775 This may be preferable to `\\[py-execute-buffer]' because:
1776 
1777  - Definitions stay in their module rather than appearing at top
1778    level, where they would clutter the global namespace and not affect
1779    uses of qualified names (MODULE.NAME).
1780 
1781  - The Python debugger gets line number information about the functions."
1782   (interactive "P")
1783   ;; Check file local variable py-master-file
1784   (if py-master-file
1785       (let* ((filename (expand-file-name py-master-file))
1786              (buffer (or (get-file-buffer filename)
1787                          (find-file-noselect filename))))
1788         (set-buffer buffer)))
1789   (let ((file (buffer-file-name (current-buffer))))
1790     (if file
1791         (progn
1792 	  ;; Maybe save some buffers
1793 	  (save-some-buffers (not py-ask-about-save) nil)
1794           (py-execute-string
1795            (if (string-match "\\.py$" file)
1796                (let ((f (file-name-sans-extension
1797 			 (file-name-nondirectory file))))
1798                  (format "if globals().has_key('%s'):\n    reload(%s)\nelse:\n    import %s\n"
1799                          f f f))
1800              (format "execfile(r'%s')\n" file))
1801            async))
1802       ;; else
1803       (py-execute-buffer async))))
1804 
1805 
1806 (defun py-execute-def-or-class (&optional async)
1807   "Send the current function or class definition to a Python interpreter.
1808 
1809 If there is a *Python* process buffer it is used.
1810 
1811 See the `\\[py-execute-region]' docs for an account of some
1812 subtleties, including the use of the optional ASYNC argument."
1813   (interactive "P")
1814   (save-excursion
1815     (py-mark-def-or-class)
1816     ;; mark is before point
1817     (py-execute-region (mark) (point) async)))
1818 
1819 
1820 (defun py-execute-string (string &optional async)
1821   "Send the argument STRING to a Python interpreter.
1822 
1823 If there is a *Python* process buffer it is used.
1824 
1825 See the `\\[py-execute-region]' docs for an account of some
1826 subtleties, including the use of the optional ASYNC argument."
1827   (interactive "sExecute Python command: ")
1828   (save-excursion
1829     (set-buffer (get-buffer-create
1830                  (generate-new-buffer-name " *Python Command*")))
1831     (insert string)
1832     (py-execute-region (point-min) (point-max) async)))
1833 
1834 
1835 
1836 (defun py-jump-to-exception (file line)
1837   "Jump to the Python code in FILE at LINE."
1838   (let ((buffer (cond ((string-equal file "<stdin>")
1839 		       (if (consp py-exception-buffer)
1840 			   (cdr py-exception-buffer)
1841 			 py-exception-buffer))
1842 		      ((and (consp py-exception-buffer)
1843 			    (string-equal file (car py-exception-buffer)))
1844 		       (cdr py-exception-buffer))
1845 		      ((py-safe (find-file-noselect file)))
1846 		      ;; could not figure out what file the exception
1847 		      ;; is pointing to, so prompt for it
1848 		      (t (find-file (read-file-name "Exception file: "
1849 						    nil
1850 						    file t))))))
1851     (pop-to-buffer buffer)
1852     ;; Force Python mode
1853     (if (not (eq major-mode 'python-mode))
1854 	(python-mode))
1855     (goto-line line)
1856     (message "Jumping to exception in file %s on line %d" file line)))
1857 
1858 (defun py-mouseto-exception (event)
1859   "Jump to the code which caused the Python exception at EVENT.
1860 EVENT is usually a mouse click."
1861   (interactive "e")
1862   (cond
1863    ((fboundp 'event-point)
1864     ;; XEmacs
1865     (let* ((point (event-point event))
1866 	   (buffer (event-buffer event))
1867 	   (e (and point buffer (extent-at point buffer 'py-exc-info)))
1868 	   (info (and e (extent-property e 'py-exc-info))))
1869       (message "Event point: %d, info: %s" point info)
1870       (and info
1871 	   (py-jump-to-exception (car info) (cdr info)))
1872       ))
1873    ;; Emacs -- Please port this!
1874    ))
1875 
1876 (defun py-goto-exception ()
1877   "Go to the line indicated by the traceback."
1878   (interactive)
1879   (let (file line)
1880     (save-excursion
1881       (beginning-of-line)
1882       (if (looking-at py-traceback-line-re)
1883 	  (setq file (match-string 1)
1884 		line (string-to-int (match-string 2)))))
1885     (if (not file)
1886 	(error "Not on a traceback line"))
1887     (py-jump-to-exception file line)))
1888 
1889 (defun py-find-next-exception (start buffer searchdir errwhere)
1890   "Find the next Python exception and jump to the code that caused it.
1891 START is the buffer position in BUFFER from which to begin searching
1892 for an exception.  SEARCHDIR is a function, either
1893 `re-search-backward' or `re-search-forward' indicating the direction
1894 to search.  ERRWHERE is used in an error message if the limit (top or
1895 bottom) of the trackback stack is encountered."
1896   (let (file line)
1897     (save-excursion
1898       (set-buffer buffer)
1899       (goto-char (py-point start))
1900       (if (funcall searchdir py-traceback-line-re nil t)
1901 	  (setq file (match-string 1)
1902 		line (string-to-int (match-string 2)))))
1903     (if (and file line)
1904 	(py-jump-to-exception file line)
1905       (error "%s of traceback" errwhere))))
1906 
1907 (defun py-down-exception (&optional bottom)
1908   "Go to the next line down in the traceback.
1909 With \\[univeral-argument] (programmatically, optional argument
1910 BOTTOM), jump to the bottom (innermost) exception in the exception
1911 stack."
1912   (interactive "P")
1913   (let* ((proc (get-process "Python"))
1914 	 (buffer (if proc "*Python*" py-output-buffer)))
1915     (if bottom
1916 	(py-find-next-exception 'eob buffer 're-search-backward "Bottom")
1917       (py-find-next-exception 'eol buffer 're-search-forward "Bottom"))))
1918 
1919 (defun py-up-exception (&optional top)
1920   "Go to the previous line up in the traceback.
1921 With \\[universal-argument] (programmatically, optional argument TOP)
1922 jump to the top (outermost) exception in the exception stack."
1923   (interactive "P")
1924   (let* ((proc (get-process "Python"))
1925 	 (buffer (if proc "*Python*" py-output-buffer)))
1926     (if top
1927 	(py-find-next-exception 'bob buffer 're-search-forward "Top")
1928       (py-find-next-exception 'bol buffer 're-search-backward "Top"))))
1929 
1930 
1931 ;; Electric deletion
1932 (defun py-electric-backspace (arg)
1933   "Delete preceding character or levels of indentation.
1934 Deletion is performed by calling the function in `py-backspace-function'
1935 with a single argument (the number of characters to delete).
1936 
1937 If point is at the leftmost column, delete the preceding newline.
1938 
1939 Otherwise, if point is at the leftmost non-whitespace character of a
1940 line that is neither a continuation line nor a non-indenting comment
1941 line, or if point is at the end of a blank line, this command reduces
1942 the indentation to match that of the line that opened the current
1943 block of code.  The line that opened the block is displayed in the
1944 echo area to help you keep track of where you are.  With
1945 \\[universal-argument] dedents that many blocks (but not past column
1946 zero).
1947 
1948 Otherwise the preceding character is deleted, converting a tab to
1949 spaces if needed so that only a single column position is deleted.
1950 \\[universal-argument] specifies how many characters to delete;
1951 default is 1.
1952 
1953 When used programmatically, argument ARG specifies the number of
1954 blocks to dedent, or the number of characters to delete, as indicated
1955 above."
1956   (interactive "*p")
1957   (if (or (/= (current-indentation) (current-column))
1958 	  (bolp)
1959 	  (py-continuation-line-p)
1960 ;	  (not py-honor-comment-indentation)
1961 ;	  (looking-at "#[^ \t\n]")	; non-indenting #
1962 	  )
1963       (funcall py-backspace-function arg)
1964     ;; else indent the same as the colon line that opened the block
1965     ;; force non-blank so py-goto-block-up doesn't ignore it
1966     (insert-char ?* 1)
1967     (backward-char)
1968     (let ((base-indent 0)		; indentation of base line
1969 	  (base-text "")		; and text of base line
1970 	  (base-found-p nil))
1971       (save-excursion
1972 	(while (< 0 arg)
1973 	  (condition-case nil		; in case no enclosing block
1974 	      (progn
1975 		(py-goto-block-up 'no-mark)
1976 		(setq base-indent (current-indentation)
1977 		      base-text   (py-suck-up-leading-text)
1978 		      base-found-p t))
1979 	    (error nil))
1980 	  (setq arg (1- arg))))
1981       (delete-char 1)			; toss the dummy character
1982       (delete-horizontal-space)
1983       (indent-to base-indent)
1984       (if base-found-p
1985 	  (message "Closes block: %s" base-text)))))
1986 
1987 
1988 (defun py-electric-delete (arg)
1989   "Delete preceding or following character or levels of whitespace.
1990 
1991 The behavior of this function depends on the variable
1992 `delete-key-deletes-forward'.  If this variable is nil (or does not
1993 exist, as in older Emacsen and non-XEmacs versions), then this
1994 function behaves identically to \\[c-electric-backspace].
1995 
1996 If `delete-key-deletes-forward' is non-nil and is supported in your
1997 Emacs, then deletion occurs in the forward direction, by calling the
1998 function in `py-delete-function'.
1999 
2000 \\[universal-argument] (programmatically, argument ARG) specifies the
2001 number of characters to delete (default is 1)."
2002   (interactive "*p")
2003   (if (or (and (fboundp 'delete-forward-p) ;XEmacs 21
2004 	       (delete-forward-p))
2005 	  (and (boundp 'delete-key-deletes-forward) ;XEmacs 20
2006 	       delete-key-deletes-forward))
2007       (funcall py-delete-function arg)
2008     (py-electric-backspace arg)))
2009 
2010 ;; required for pending-del and delsel modes
2011 (put 'py-electric-colon 'delete-selection t) ;delsel
2012 (put 'py-electric-colon 'pending-delete   t) ;pending-del
2013 (put 'py-electric-backspace 'delete-selection 'supersede) ;delsel
2014 (put 'py-electric-backspace 'pending-delete   'supersede) ;pending-del
2015 (put 'py-electric-delete    'delete-selection 'supersede) ;delsel
2016 (put 'py-electric-delete    'pending-delete   'supersede) ;pending-del
2017 
2018 
2019 
2020 (defun py-indent-line (&optional arg)
2021   "Fix the indentation of the current line according to Python rules.
2022 With \\[universal-argument] (programmatically, the optional argument
2023 ARG non-nil), ignore dedenting rules for block closing statements
2024 (e.g. return, raise, break, continue, pass)
2025 
2026 This function is normally bound to `indent-line-function' so
2027 \\[indent-for-tab-command] will call it."
2028   (interactive "P")
2029   (let* ((ci (current-indentation))
2030 	 (move-to-indentation-p (<= (current-column) ci))
2031 	 (need (py-compute-indentation (not arg)))
2032          (cc (current-column)))
2033     ;; dedent out a level unless we're in column 1
2034     (if (and (equal last-command this-command)
2035              (/= cc 0))
2036         (progn
2037           (beginning-of-line)
2038           (delete-horizontal-space)
2039           (indent-to (* (/ (- cc 1) py-indent-offset) py-indent-offset)))
2040       (progn
2041 	;; see if we need to dedent
2042 	(if (py-outdent-p)
2043 	    (setq need (- need py-indent-offset)))
2044 	(if (/= ci need)
2045 	    (save-excursion
2046 	      (beginning-of-line)
2047 	      (delete-horizontal-space)
2048 	      (indent-to need)))
2049 	(if move-to-indentation-p (back-to-indentation))))))
2050 
2051 (defun py-newline-and-indent ()
2052   "Strives to act like the Emacs `newline-and-indent'.
2053 This is just `strives to' because correct indentation can't be computed
2054 from scratch for Python code.  In general, deletes the whitespace before
2055 point, inserts a newline, and takes an educated guess as to how you want
2056 the new line indented."
2057   (interactive)
2058   (let ((ci (current-indentation)))
2059     (if (< ci (current-column))		; if point beyond indentation
2060 	(newline-and-indent)
2061       ;; else try to act like newline-and-indent "normally" acts
2062       (beginning-of-line)
2063       (insert-char ?\n 1)
2064       (move-to-column ci))))
2065 
2066 (defun py-compute-indentation (honor-block-close-p)
2067   "Compute Python indentation.
2068 When HONOR-BLOCK-CLOSE-P is non-nil, statements such as `return',
2069 `raise', `break', `continue', and `pass' force one level of
2070 dedenting."
2071   (save-excursion
2072     (beginning-of-line)
2073     (let* ((bod (py-point 'bod))
2074 	   (pps (parse-partial-sexp bod (point)))
2075 	   (boipps (parse-partial-sexp bod (py-point 'boi)))
2076 	   placeholder)
2077       (cond
2078        ;; are we inside a multi-line string or comment?
2079        ((or (and (nth 3 pps) (nth 3 boipps))
2080 	    (and (nth 4 pps) (nth 4 boipps)))
2081 	(save-excursion
2082 	  (if (not py-align-multiline-strings-p) 0
2083 	    ;; skip back over blank & non-indenting comment lines
2084 	    ;; note: will skip a blank or non-indenting comment line
2085 	    ;; that happens to be a continuation line too
2086 	    (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move)
2087 	    (back-to-indentation)
2088 	    (current-column))))
2089        ;; are we on a continuation line?
2090        ((py-continuation-line-p)
2091 	(let ((startpos (point))
2092 	      (open-bracket-pos (py-nesting-level))
2093 	      endpos searching found state)
2094 	  (if open-bracket-pos
2095 	      (progn
2096 		;; align with first item in list; else a normal
2097 		;; indent beyond the line with the open bracket
2098 		(goto-char (1+ open-bracket-pos)) ; just beyond bracket
2099 		;; is the first list item on the same line?
2100 		(skip-chars-forward " \t")
2101 		(if (null (memq (following-char) '(?\n ?# ?\\)))
2102 					; yes, so line up with it
2103 		    (current-column)
2104 		  ;; first list item on another line, or doesn't exist yet
2105 		  (forward-line 1)
2106 		  (while (and (< (point) startpos)
2107 			      (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
2108 		    (forward-line 1))
2109 		  (if (and (< (point) startpos)
2110 			   (/= startpos
2111 			       (save-excursion
2112 				 (goto-char (1+ open-bracket-pos))
2113 				 (forward-comment (point-max))
2114 				 (point))))
2115 		      ;; again mimic the first list item
2116 		      (current-indentation)
2117 		    ;; else they're about to enter the first item
2118 		    (goto-char open-bracket-pos)
2119 		    (setq placeholder (point))
2120 		    (py-goto-initial-line)
2121 		    (py-goto-beginning-of-tqs
2122 		     (save-excursion (nth 3 (parse-partial-sexp
2123 					     placeholder (point)))))
2124 		    (+ (current-indentation) py-indent-offset))))
2125 
2126 	    ;; else on backslash continuation line
2127 	    (forward-line -1)
2128 	    (if (py-continuation-line-p) ; on at least 3rd line in block
2129 		(current-indentation)	; so just continue the pattern
2130 	      ;; else started on 2nd line in block, so indent more.
2131 	      ;; if base line is an assignment with a start on a RHS,
2132 	      ;; indent to 2 beyond the leftmost "="; else skip first
2133 	      ;; chunk of non-whitespace characters on base line, + 1 more
2134 	      ;; column
2135 	      (end-of-line)
2136 	      (setq endpos (point)
2137 		    searching t)
2138 	      (back-to-indentation)
2139 	      (setq startpos (point))
2140 	      ;; look at all "=" from left to right, stopping at first
2141 	      ;; one not nested in a list or string
2142 	      (while searching
2143 		(skip-chars-forward "^=" endpos)
2144 		(if (= (point) endpos)
2145 		    (setq searching nil)
2146 		  (forward-char 1)
2147 		  (setq state (parse-partial-sexp startpos (point)))
2148 		  (if (and (zerop (car state)) ; not in a bracket
2149 			   (null (nth 3 state))) ; & not in a string
2150 		      (progn
2151 			(setq searching nil) ; done searching in any case
2152 			(setq found
2153 			      (not (or
2154 				    (eq (following-char) ?=)
2155 				    (memq (char-after (- (point) 2))
2156 					  '(?< ?> ?!)))))))))
2157 	      (if (or (not found)	; not an assignment
2158 		      (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
2159 		  (progn
2160 		    (goto-char startpos)
2161 		    (skip-chars-forward "^ \t\n")))
2162 	      ;; if this is a continuation for a block opening
2163 	      ;; statement, add some extra offset.
2164 	      (+ (current-column) (if (py-statement-opens-block-p)
2165 				      py-continuation-offset 0)
2166 		 1)
2167 	      ))))
2168 
2169        ;; not on a continuation line
2170        ((bobp) (current-indentation))
2171 
2172        ;; Dfn: "Indenting comment line".  A line containing only a
2173        ;; comment, but which is treated like a statement for
2174        ;; indentation calculation purposes.  Such lines are only
2175        ;; treated specially by the mode; they are not treated
2176        ;; specially by the Python interpreter.
2177 
2178        ;; The rules for indenting comment lines are a line where:
2179        ;;   - the first non-whitespace character is `#', and
2180        ;;   - the character following the `#' is whitespace, and
2181        ;;   - the line is dedented with respect to (i.e. to the left
2182        ;;     of) the indentation of the preceding non-blank line.
2183 
2184        ;; The first non-blank line following an indenting comment
2185        ;; line is given the same amount of indentation as the
2186        ;; indenting comment line.
2187 
2188        ;; All other comment-only lines are ignored for indentation
2189        ;; purposes.
2190 
2191        ;; Are we looking at a comment-only line which is *not* an
2192        ;; indenting comment line?  If so, we assume that it's been
2193        ;; placed at the desired indentation, so leave it alone.
2194        ;; Indenting comment lines are aligned as statements down
2195        ;; below.
2196        ((and (looking-at "[ \t]*#[^ \t\n]")
2197 	     ;; NOTE: this test will not be performed in older Emacsen
2198 	     (fboundp 'forward-comment)
2199 	     (<= (current-indentation)
2200 		 (save-excursion
2201 		   (forward-comment (- (point-max)))
2202 		   (current-indentation))))
2203 	(current-indentation))
2204 
2205        ;; else indentation based on that of the statement that
2206        ;; precedes us; use the first line of that statement to
2207        ;; establish the base, in case the user forced a non-std
2208        ;; indentation for the continuation lines (if any)
2209        (t
2210 	;; skip back over blank & non-indenting comment lines note:
2211 	;; will skip a blank or non-indenting comment line that
2212 	;; happens to be a continuation line too.  use fast Emacs 19
2213 	;; function if it's there.
2214 	(if (and (eq py-honor-comment-indentation nil)
2215 		 (fboundp 'forward-comment))
2216 	    (forward-comment (- (point-max)))
2217 	  (let ((prefix-re (concat py-block-comment-prefix "[ \t]*"))
2218 		done)
2219 	    (while (not done)
2220 	      (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#\\)" nil 'move)
2221 	      (setq done (or (bobp)
2222 			     (and (eq py-honor-comment-indentation t)
2223 				  (save-excursion
2224 				    (back-to-indentation)
2225 				    (not (looking-at prefix-re))
2226 				    ))
2227 			     (and (not (eq py-honor-comment-indentation t))
2228 				  (save-excursion
2229 				    (back-to-indentation)
2230 				    (and (not (looking-at prefix-re))
2231 					 (or (looking-at "[^#]")
2232 					     (not (zerop (current-column)))
2233 					     ))
2234 				    ))
2235 			     ))
2236 	      )))
2237 	;; if we landed inside a string, go to the beginning of that
2238 	;; string. this handles triple quoted, multi-line spanning
2239 	;; strings.
2240 	(py-goto-beginning-of-tqs (nth 3 (parse-partial-sexp bod (point))))
2241 	;; now skip backward over continued lines
2242 	(setq placeholder (point))
2243 	(py-goto-initial-line)
2244 	;; we may *now* have landed in a TQS, so find the beginning of
2245 	;; this string.
2246 	(py-goto-beginning-of-tqs
2247 	 (save-excursion (nth 3 (parse-partial-sexp
2248 				 placeholder (point)))))
2249 	(+ (current-indentation)
2250 	   (if (py-statement-opens-block-p)
2251 	       py-indent-offset
2252 	     (if (and honor-block-close-p (py-statement-closes-block-p))
2253 		 (- py-indent-offset)
2254 	       0)))
2255 	)))))
2256 
2257 (defun py-guess-indent-offset (&optional global)
2258   "Guess a good value for, and change, `py-indent-offset'.
2259 
2260 By default, make a buffer-local copy of `py-indent-offset' with the
2261 new value, so that other Python buffers are not affected.  With
2262 \\[universal-argument] (programmatically, optional argument GLOBAL),
2263 change the global value of `py-indent-offset'.  This affects all
2264 Python buffers (that don't have their own buffer-local copy), both
2265 those currently existing and those created later in the Emacs session.
2266 
2267 Some people use a different value for `py-indent-offset' than you use.
2268 There's no excuse for such foolishness, but sometimes you have to deal
2269 with their ugly code anyway.  This function examines the file and sets
2270 `py-indent-offset' to what it thinks it was when they created the
2271 mess.
2272 
2273 Specifically, it searches forward from the statement containing point,
2274 looking for a line that opens a block of code.  `py-indent-offset' is
2275 set to the difference in indentation between that line and the Python
2276 statement following it.  If the search doesn't succeed going forward,
2277 it's tried again going backward."
2278   (interactive "P")			; raw prefix arg
2279   (let (new-value
2280 	(start (point))
2281 	(restart (point))
2282 	(found nil)
2283 	colon-indent)
2284     (py-goto-initial-line)
2285     (while (not (or found (eobp)))
2286       (when (and (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
2287 		 (not (py-in-literal restart)))
2288 	(setq restart (point))
2289 	(py-goto-initial-line)
2290 	(if (py-statement-opens-block-p)
2291 	    (setq found t)
2292 	  (goto-char restart))))
2293     (unless found
2294       (goto-char start)
2295       (py-goto-initial-line)
2296       (while (not (or found (bobp)))
2297 	(setq found (and
2298 		     (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
2299 		     (or (py-goto-initial-line) t) ; always true -- side effect
2300 		     (py-statement-opens-block-p)))))
2301     (setq colon-indent (current-indentation)
2302 	  found (and found (zerop (py-next-statement 1)))
2303 	  new-value (- (current-indentation) colon-indent))
2304     (goto-char start)
2305     (if (not found)
2306 	(error "Sorry, couldn't guess a value for py-indent-offset")
2307       (funcall (if global 'kill-local-variable 'make-local-variable)
2308 	       'py-indent-offset)
2309       (setq py-indent-offset new-value)
2310       (or noninteractive
2311 	  (message "%s value of py-indent-offset set to %d"
2312 		   (if global "Global" "Local")
2313 		   py-indent-offset)))
2314     ))
2315 
2316 (defun py-comment-indent-function ()
2317   "Python version of `comment-indent-function'."
2318   ;; This is required when filladapt is turned off.  Without it, when
2319   ;; filladapt is not used, comments which start in column zero
2320   ;; cascade one character to the right
2321   (save-excursion
2322     (beginning-of-line)
2323     (let ((eol (py-point 'eol)))
2324       (and comment-start-skip
2325 	   (re-search-forward comment-start-skip eol t)
2326 	   (setq eol (match-beginning 0)))
2327       (goto-char eol)
2328       (skip-chars-backward " \t")
2329       (max comment-column (+ (current-column) (if (bolp) 0 1)))
2330       )))
2331 
2332 (defun py-narrow-to-defun (&optional class)
2333   "Make text outside current defun invisible.
2334 The defun visible is the one that contains point or follows point.
2335 Optional CLASS is passed directly to `py-beginning-of-def-or-class'."
2336   (interactive "P")
2337   (save-excursion
2338     (widen)
2339     (py-end-of-def-or-class class)
2340     (let ((end (point)))
2341       (py-beginning-of-def-or-class class)
2342       (narrow-to-region (point) end))))
2343 
2344 
2345 (defun py-shift-region (start end count)
2346   "Indent lines from START to END by COUNT spaces."
2347   (save-excursion
2348     (goto-char end)
2349     (beginning-of-line)
2350     (setq end (point))
2351     (goto-char start)
2352     (beginning-of-line)
2353     (setq start (point))
2354     (indent-rigidly start end count)))
2355 
2356 (defun py-shift-region-left (start end &optional count)
2357   "Shift region of Python code to the left.
2358 The lines from the line containing the start of the current region up
2359 to (but not including) the line containing the end of the region are
2360 shifted to the left, by `py-indent-offset' columns.
2361 
2362 If a prefix argument is given, the region is instead shifted by that
2363 many columns.  With no active region, dedent only the current line.
2364 You cannot dedent the region if any line is already at column zero."
2365   (interactive
2366    (let ((p (point))
2367 	 (m (mark))
2368 	 (arg current-prefix-arg))
2369      (if m
2370 	 (list (min p m) (max p m) arg)
2371        (list p (save-excursion (forward-line 1) (point)) arg))))
2372   ;; if any line is at column zero, don't shift the region
2373   (save-excursion
2374     (goto-char start)
2375     (while (< (point) end)
2376       (back-to-indentation)
2377       (if (and (zerop (current-column))
2378 	       (not (looking-at "\\s *$")))
2379 	  (error "Region is at left edge"))
2380       (forward-line 1)))
2381   (py-shift-region start end (- (prefix-numeric-value
2382 				 (or count py-indent-offset))))
2383   (py-keep-region-active))
2384 
2385 (defun py-shift-region-right (start end &optional count)
2386   "Shift region of Python code to the right.
2387 The lines from the line containing the start of the current region up
2388 to (but not including) the line containing the end of the region are
2389 shifted to the right, by `py-indent-offset' columns.
2390 
2391 If a prefix argument is given, the region is instead shifted by that
2392 many columns.  With no active region, indent only the current line."
2393   (interactive
2394    (let ((p (point))
2395 	 (m (mark))
2396 	 (arg current-prefix-arg))
2397      (if m
2398 	 (list (min p m) (max p m) arg)
2399        (list p (save-excursion (forward-line 1) (point)) arg))))
2400   (py-shift-region start end (prefix-numeric-value
2401 			      (or count py-indent-offset)))
2402   (py-keep-region-active))
2403 
2404 (defun py-indent-region (start end &optional indent-offset)
2405   "Reindent a region of Python code.
2406 
2407 The lines from the line containing the start of the current region up
2408 to (but not including) the line containing the end of the region are
2409 reindented.  If the first line of the region has a non-whitespace
2410 character in the first column, the first line is left alone and the
2411 rest of the region is reindented with respect to it.  Else the entire
2412 region is reindented with respect to the (closest code or indenting
2413 comment) statement immediately preceding the region.
2414 
2415 This is useful when code blocks are moved or yanked, when enclosing
2416 control structures are introduced or removed, or to reformat code
2417 using a new value for the indentation offset.
2418 
2419 If a numeric prefix argument is given, it will be used as the value of
2420 the indentation offset.  Else the value of `py-indent-offset' will be
2421 used.
2422 
2423 Warning: The region must be consistently indented before this function
2424 is called!  This function does not compute proper indentation from
2425 scratch (that's impossible in Python), it merely adjusts the existing
2426 indentation to be correct in context.
2427 
2428 Warning: This function really has no idea what to do with
2429 non-indenting comment lines, and shifts them as if they were indenting
2430 comment lines.  Fixing this appears to require telepathy.
2431 
2432 Special cases: whitespace is deleted from blank lines; continuation
2433 lines are shifted by the same amount their initial line was shifted,
2434 in order to preserve their relative indentation with respect to their
2435 initial line; and comment lines beginning in column 1 are ignored."
2436   (interactive "*r\nP")			; region; raw prefix arg
2437   (save-excursion
2438     (goto-char end)   (beginning-of-line) (setq end (point-marker))
2439     (goto-char start) (beginning-of-line)
2440     (let ((py-indent-offset (prefix-numeric-value
2441 			     (or indent-offset py-indent-offset)))
2442 	  (indents '(-1))		; stack of active indent levels
2443 	  (target-column 0)		; column to which to indent
2444 	  (base-shifted-by 0)		; amount last base line was shifted
2445 	  (indent-base (if (looking-at "[ \t\n]")
2446 			   (py-compute-indentation t)
2447 			 0))
2448 	  ci)
2449       (while (< (point) end)
2450 	(setq ci (current-indentation))
2451 	;; figure out appropriate target column
2452 	(cond
2453 	 ((or (eq (following-char) ?#)	; comment in column 1
2454 	      (looking-at "[ \t]*$"))	; entirely blank
2455 	  (setq target-column 0))
2456 	 ((py-continuation-line-p)	; shift relative to base line
2457 	  (setq target-column (+ ci base-shifted-by)))
2458 	 (t				; new base line
2459 	  (if (> ci (car indents))	; going deeper; push it
2460 	      (setq indents (cons ci indents))
2461 	    ;; else we should have seen this indent before
2462 	    (setq indents (memq ci indents)) ; pop deeper indents
2463 	    (if (null indents)
2464 		(error "Bad indentation in region, at line %d"
2465 		       (save-restriction
2466 			 (widen)
2467 			 (1+ (count-lines 1 (point)))))))
2468 	  (setq target-column (+ indent-base
2469 				 (* py-indent-offset
2470 				    (- (length indents) 2))))
2471 	  (setq base-shifted-by (- target-column ci))))
2472 	;; shift as needed
2473 	(if (/= ci target-column)
2474 	    (progn
2475 	      (delete-horizontal-space)
2476 	      (indent-to target-column)))
2477 	(forward-line 1))))
2478   (set-marker end nil))
2479 
2480 (defun py-comment-region (beg end &optional arg)
2481   "Like `comment-region' but uses double hash (`#') comment starter."
2482   (interactive "r\nP")
2483   (let ((comment-start py-block-comment-prefix))
2484     (comment-region beg end arg)))
2485 
2486 
2487 ;; Functions for moving point
2488 (defun py-previous-statement (count)
2489   "Go to the start of the COUNTth preceding Python statement.
2490 By default, goes to the previous statement.  If there is no such
2491 statement, goes to the first statement.  Return count of statements
2492 left to move.  `Statements' do not include blank, comment, or
2493 continuation lines."
2494   (interactive "p")			; numeric prefix arg
2495   (if (< count 0) (py-next-statement (- count))
2496     (py-goto-initial-line)
2497     (let (start)
2498       (while (and
2499 	      (setq start (point))	; always true -- side effect
2500 	      (> count 0)
2501 	      (zerop (forward-line -1))
2502 	      (py-goto-statement-at-or-above))
2503 	(setq count (1- count)))
2504       (if (> count 0) (goto-char start)))
2505     count))
2506 
2507 (defun py-next-statement (count)
2508   "Go to the start of next Python statement.
2509 If the statement at point is the i'th Python statement, goes to the
2510 start of statement i+COUNT.  If there is no such statement, goes to the
2511 last statement.  Returns count of statements left to move.  `Statements'
2512 do not include blank, comment, or continuation lines."
2513   (interactive "p")			; numeric prefix arg
2514   (if (< count 0) (py-previous-statement (- count))
2515     (beginning-of-line)
2516     (let (start)
2517       (while (and
2518 	      (setq start (point))	; always true -- side effect
2519 	      (> count 0)
2520 	      (py-goto-statement-below))
2521 	(setq count (1- count)))
2522       (if (> count 0) (goto-char start)))
2523     count))
2524 
2525 (defun py-goto-block-up (&optional nomark)
2526   "Move up to start of current block.
2527 Go to the statement that starts the smallest enclosing block; roughly
2528 speaking, this will be the closest preceding statement that ends with a
2529 colon and is indented less than the statement you started on.  If
2530 successful, also sets the mark to the starting point.
2531 
2532 `\\[py-mark-block]' can be used afterward to mark the whole code
2533 block, if desired.
2534 
2535 If called from a program, the mark will not be set if optional argument
2536 NOMARK is not nil."
2537   (interactive)
2538   (let ((start (point))
2539 	(found nil)
2540 	initial-indent)
2541     (py-goto-initial-line)
2542     ;; if on blank or non-indenting comment line, use the preceding stmt
2543     (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
2544 	(progn
2545 	  (py-goto-statement-at-or-above)
2546 	  (setq found (py-statement-opens-block-p))))
2547     ;; search back for colon line indented less
2548     (setq initial-indent (current-indentation))
2549     (if (zerop initial-indent)
2550 	;; force fast exit
2551 	(goto-char (point-min)))
2552     (while (not (or found (bobp)))
2553       (setq found
2554 	    (and
2555 	     (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
2556 	     (or (py-goto-initial-line) t) ; always true -- side effect
2557 	     (< (current-indentation) initial-indent)
2558 	     (py-statement-opens-block-p))))
2559     (if found
2560 	(progn
2561 	  (or nomark (push-mark start))
2562 	  (back-to-indentation))
2563       (goto-char start)
2564       (error "Enclosing block not found"))))
2565 
2566 (defun py-beginning-of-def-or-class (&optional class count)
2567   "Move point to start of `def' or `class'.
2568 
2569 Searches back for the closest preceding `def'.  If you supply a prefix
2570 arg, looks for a `class' instead.  The docs below assume the `def'
2571 case; just substitute `class' for `def' for the other case.
2572 Programmatically, if CLASS is `either', then moves to either `class'
2573 or `def'.
2574 
2575 When second optional argument is given programmatically, move to the
2576 COUNTth start of `def'.
2577 
2578 If point is in a `def' statement already, and after the `d', simply
2579 moves point to the start of the statement.
2580 
2581 Otherwise (i.e. when point is not in a `def' statement, or at or
2582 before the `d' of a `def' statement), searches for the closest
2583 preceding `def' statement, and leaves point at its start.  If no such
2584 statement can be found, leaves point at the start of the buffer.
2585 
2586 Returns t iff a `def' statement is found by these rules.
2587 
2588 Note that doing this command repeatedly will take you closer to the
2589 start of the buffer each time.
2590 
2591 To mark the current `def', see `\\[py-mark-def-or-class]'."
2592   (interactive "P")			; raw prefix arg
2593   (setq count (or count 1))
2594   (let ((at-or-before-p (<= (current-column) (current-indentation)))
2595 	(start-of-line (goto-char (py-point 'bol)))
2596 	(start-of-stmt (goto-char (py-point 'bos)))
2597 	(start-re (cond ((eq class 'either) "^[ \t]*\\(class\\|def\\)\\>")
2598 			(class "^[ \t]*class\\>")
2599 			(t "^[ \t]*def\\>")))
2600 	)
2601     ;; searching backward
2602     (if (and (< 0 count)
2603 	     (or (/= start-of-stmt start-of-line)
2604 		 (not at-or-before-p)))
2605 	(end-of-line))
2606     ;; search forward
2607     (if (and (> 0 count)
2608 	     (zerop (current-column))
2609 	     (looking-at start-re))
2610 	(end-of-line))
2611     (if (re-search-backward start-re nil 'move count)
2612 	(goto-char (match-beginning 0)))))
2613 
2614 ;; Backwards compatibility
2615 (defalias 'beginning-of-python-def-or-class 'py-beginning-of-def-or-class)
2616 
2617 (defun py-end-of-def-or-class (&optional class count)
2618   "Move point beyond end of `def' or `class' body.
2619 
2620 By default, looks for an appropriate `def'.  If you supply a prefix
2621 arg, looks for a `class' instead.  The docs below assume the `def'
2622 case; just substitute `class' for `def' for the other case.
2623 Programmatically, if CLASS is `either', then moves to either `class'
2624 or `def'.
2625 
2626 When second optional argument is given programmatically, move to the
2627 COUNTth end of `def'.
2628 
2629 If point is in a `def' statement already, this is the `def' we use.
2630 
2631 Else, if the `def' found by `\\[py-beginning-of-def-or-class]'
2632 contains the statement you started on, that's the `def' we use.
2633 
2634 Otherwise, we search forward for the closest following `def', and use that.
2635 
2636 If a `def' can be found by these rules, point is moved to the start of
2637 the line immediately following the `def' block, and the position of the
2638 start of the `def' is returned.
2639 
2640 Else point is moved to the end of the buffer, and nil is returned.
2641 
2642 Note that doing this command repeatedly will take you closer to the
2643 end of the buffer each time.
2644 
2645 To mark the current `def', see `\\[py-mark-def-or-class]'."
2646   (interactive "P")			; raw prefix arg
2647   (if (and count (/= count 1))
2648       (py-beginning-of-def-or-class (- 1 count)))
2649   (let ((start (progn (py-goto-initial-line) (point)))
2650 	(which (cond ((eq class 'either) "\\(class\\|def\\)")
2651 		     (class "class")
2652 		     (t "def")))
2653 	(state 'not-found))
2654     ;; move point to start of appropriate def/class
2655     (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
2656 	(setq state 'at-beginning)
2657       ;; else see if py-beginning-of-def-or-class hits container
2658       (if (and (py-beginning-of-def-or-class class)
2659 	       (progn (py-goto-beyond-block)
2660 		      (> (point) start)))
2661 	  (setq state 'at-end)
2662 	;; else search forward
2663 	(goto-char start)
2664 	(if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
2665 	    (progn (setq state 'at-beginning)
2666 		   (beginning-of-line)))))
2667     (cond
2668      ((eq state 'at-beginning) (py-goto-beyond-block) t)
2669      ((eq state 'at-end) t)
2670      ((eq state 'not-found) nil)
2671      (t (error "Internal error in `py-end-of-def-or-class'")))))
2672 
2673 ;; Backwards compabitility
2674 (defalias 'end-of-python-def-or-class 'py-end-of-def-or-class)
2675 
2676 
2677 ;; Functions for marking regions
2678 (defun py-mark-block (&optional extend just-move)
2679   "Mark following block of lines.  With prefix arg, mark structure.
2680 Easier to use than explain.  It sets the region to an `interesting'
2681 block of succeeding lines.  If point is on a blank line, it goes down to
2682 the next non-blank line.  That will be the start of the region.  The end
2683 of the region depends on the kind of line at the start:
2684 
2685  - If a comment, the region will include all succeeding comment lines up
2686    to (but not including) the next non-comment line (if any).
2687 
2688  - Else if a prefix arg is given, and the line begins one of these
2689    structures:
2690 
2691      if elif else try except finally for while def class
2692 
2693    the region will be set to the body of the structure, including
2694    following blocks that `belong' to it, but excluding trailing blank
2695    and comment lines.  E.g., if on a `try' statement, the `try' block
2696    and all (if any) of the following `except' and `finally' blocks
2697    that belong to the `try' structure will be in the region.  Ditto
2698    for if/elif/else, for/else and while/else structures, and (a bit
2699    degenerate, since they're always one-block structures) def and
2700    class blocks.
2701 
2702  - Else if no prefix argument is given, and the line begins a Python
2703    block (see list above), and the block is not a `one-liner' (i.e.,
2704    the statement ends with a colon, not with code), the region will
2705    include all succeeding lines up to (but not including) the next
2706    code statement (if any) that's indented no more than the starting
2707    line, except that trailing blank and comment lines are excluded.
2708    E.g., if the starting line begins a multi-statement `def'
2709    structure, the region will be set to the full function definition,
2710    but without any trailing `noise' lines.
2711 
2712  - Else the region will include all succeeding lines up to (but not
2713    including) the next blank line, or code or indenting-comment line
2714    indented strictly less than the starting line.  Trailing indenting
2715    comment lines are included in this case, but not trailing blank
2716    lines.
2717 
2718 A msg identifying the location of the mark is displayed in the echo
2719 area; or do `\\[exchange-point-and-mark]' to flip down to the end.
2720 
2721 If called from a program, optional argument EXTEND plays the role of
2722 the prefix arg, and if optional argument JUST-MOVE is not nil, just
2723 moves to the end of the block (& does not set mark or display a msg)."
2724   (interactive "P")			; raw prefix arg
2725   (py-goto-initial-line)
2726   ;; skip over blank lines
2727   (while (and
2728 	  (looking-at "[ \t]*$")	; while blank line
2729 	  (not (eobp)))			; & somewhere to go
2730     (forward-line 1))
2731   (if (eobp)
2732       (error "Hit end of buffer without finding a non-blank stmt"))
2733   (let ((initial-pos (point))
2734 	(initial-indent (current-indentation))
2735 	last-pos			; position of last stmt in region
2736 	(followers
2737 	 '((if elif else) (elif elif else) (else)
2738 	   (try except finally) (except except) (finally)
2739 	   (for else) (while else)
2740 	   (def) (class) ) )
2741 	first-symbol next-symbol)
2742 
2743     (cond
2744      ;; if comment line, suck up the following comment lines
2745      ((looking-at "[ \t]*#")
2746       (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
2747       (re-search-backward "^[ \t]*#")	; and back to last comment in block
2748       (setq last-pos (point)))
2749 
2750      ;; else if line is a block line and EXTEND given, suck up
2751      ;; the whole structure
2752      ((and extend
2753 	   (setq first-symbol (py-suck-up-first-keyword) )
2754 	   (assq first-symbol followers))
2755       (while (and
2756 	      (or (py-goto-beyond-block) t) ; side effect
2757 	      (forward-line -1)		; side effect
2758 	      (setq last-pos (point))	; side effect
2759 	      (py-goto-statement-below)
2760 	      (= (current-indentation) initial-indent)
2761 	      (setq next-symbol (py-suck-up-first-keyword))
2762 	      (memq next-symbol (cdr (assq first-symbol followers))))
2763 	(setq first-symbol next-symbol)))
2764 
2765      ;; else if line *opens* a block, search for next stmt indented <=
2766      ((py-statement-opens-block-p)
2767       (while (and
2768 	      (setq last-pos (point))	; always true -- side effect
2769 	      (py-goto-statement-below)
2770 	      (> (current-indentation) initial-indent)
2771 	      )))
2772 
2773      ;; else plain code line; stop at next blank line, or stmt or
2774      ;; indenting comment line indented <
2775      (t
2776       (while (and
2777 	      (setq last-pos (point))	; always true -- side effect
2778 	      (or (py-goto-beyond-final-line) t)
2779 	      (not (looking-at "[ \t]*$")) ; stop at blank line
2780 	      (or
2781 	       (>= (current-indentation) initial-indent)
2782 	       (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
2783 	nil)))
2784 
2785     ;; skip to end of last stmt
2786     (goto-char last-pos)
2787     (py-goto-beyond-final-line)
2788 
2789     ;; set mark & display
2790     (if just-move
2791 	()				; just return
2792       (push-mark (point) 'no-msg)
2793       (forward-line -1)
2794       (message "Mark set after: %s" (py-suck-up-leading-text))
2795       (goto-char initial-pos))))
2796 
2797 (defun py-mark-def-or-class (&optional class)
2798   "Set region to body of def (or class, with prefix arg) enclosing point.
2799 Pushes the current mark, then point, on the mark ring (all language
2800 modes do this, but although it's handy it's never documented ...).
2801 
2802 In most Emacs language modes, this function bears at least a
2803 hallucinogenic resemblance to `\\[py-end-of-def-or-class]' and
2804 `\\[py-beginning-of-def-or-class]'.
2805 
2806 And in earlier versions of Python mode, all 3 were tightly connected.
2807 Turned out that was more confusing than useful: the `goto start' and
2808 `goto end' commands are usually used to search through a file, and
2809 people expect them to act a lot like `search backward' and `search
2810 forward' string-search commands.  But because Python `def' and `class'
2811 can nest to arbitrary levels, finding the smallest def containing
2812 point cannot be done via a simple backward search: the def containing
2813 point may not be the closest preceding def, or even the closest
2814 preceding def that's indented less.  The fancy algorithm required is
2815 appropriate for the usual uses of this `mark' command, but not for the
2816 `goto' variations.
2817 
2818 So the def marked by this command may not be the one either of the
2819 `goto' commands find: If point is on a blank or non-indenting comment
2820 line, moves back to start of the closest preceding code statement or
2821 indenting comment line.  If this is a `def' statement, that's the def
2822 we use.  Else searches for the smallest enclosing `def' block and uses
2823 that.  Else signals an error.
2824 
2825 When an enclosing def is found: The mark is left immediately beyond
2826 the last line of the def block.  Point is left at the start of the
2827 def, except that: if the def is preceded by a number of comment lines
2828 followed by (at most) one optional blank line, point is left at the
2829 start of the comments; else if the def is preceded by a blank line,
2830 point is left at its start.
2831 
2832 The intent is to mark the containing def/class and its associated
2833 documentation, to make moving and duplicating functions and classes
2834 pleasant."
2835   (interactive "P")			; raw prefix arg
2836   (let ((start (point))
2837 	(which (cond ((eq class 'either) "\\(class\\|def\\)")
2838 		     (class "class")
2839 		     (t "def"))))
2840     (push-mark start)
2841     (if (not (py-go-up-tree-to-keyword which))
2842 	(progn (goto-char start)
2843 	       (error "Enclosing %s not found"
2844 		      (if (eq class 'either)
2845 			  "def or class"
2846 			which)))
2847       ;; else enclosing def/class found
2848       (setq start (point))
2849       (py-goto-beyond-block)
2850       (push-mark (point))
2851       (goto-char start)
2852       (if (zerop (forward-line -1))	; if there is a preceding line
2853 	  (progn
2854 	    (if (looking-at "[ \t]*$")	; it's blank
2855 		(setq start (point))	; so reset start point
2856 	      (goto-char start))	; else try again
2857 	    (if (zerop (forward-line -1))
2858 		(if (looking-at "[ \t]*#") ; a comment
2859 		    ;; look back for non-comment line
2860 		    ;; tricky: note that the regexp matches a blank
2861 		    ;; line, cuz \n is in the 2nd character class
2862 		    (and
2863 		     (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
2864 		     (forward-line 1))
2865 		  ;; no comment, so go back
2866 		  (goto-char start)))))))
2867   (exchange-point-and-mark)
2868   (py-keep-region-active))
2869 
2870 ;; ripped from cc-mode
2871 (defun py-forward-into-nomenclature (&optional arg)
2872   "Move forward to end of a nomenclature section or word.
2873 With \\[universal-argument] (programmatically, optional argument ARG), 
2874 do it that many times.
2875 
2876 A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
2877   (interactive "p")
2878   (let ((case-fold-search nil))
2879     (if (> arg 0)
2880 	(re-search-forward
2881 	 "\\(\\W\\|[_]\\)*\\([A-Z]*[a-z0-9]*\\)"
2882 	 (point-max) t arg)
2883       (while (and (< arg 0)
2884 		  (re-search-backward
2885 		   "\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\(\\W\\|[_]\\)\\w+"
2886 		   (point-min) 0))
2887 	(forward-char 1)
2888 	(setq arg (1+ arg)))))
2889   (py-keep-region-active))
2890 
2891 (defun py-backward-into-nomenclature (&optional arg)
2892   "Move backward to beginning of a nomenclature section or word.
2893 With optional ARG, move that many times.  If ARG is negative, move
2894 forward.
2895 
2896 A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
2897   (interactive "p")
2898   (py-forward-into-nomenclature (- arg))
2899   (py-keep-region-active))
2900 
2901 
2902 
2903 ;; pdbtrack functions
2904 (defun py-pdbtrack-toggle-stack-tracking (arg)
2905   (interactive "P")
2906   (if (not (get-buffer-process (current-buffer)))
2907       (error "No process associated with buffer '%s'" (current-buffer)))
2908   ;; missing or 0 is toggle, >0 turn on, <0 turn off
2909   (if (or (not arg)
2910 	  (zerop (setq arg (prefix-numeric-value arg))))
2911       (setq py-pdbtrack-do-tracking-p (not py-pdbtrack-do-tracking-p))
2912     (setq py-pdbtrack-do-tracking-p (> arg 0)))
2913   (message "%sabled Python's pdbtrack"
2914            (if py-pdbtrack-do-tracking-p "En" "Dis")))
2915 
2916 (defun turn-on-pdbtrack ()
2917   (interactive)
2918   (py-pdbtrack-toggle-stack-tracking 1))
2919 
2920 (defun turn-off-pdbtrack ()
2921   (interactive)
2922   (py-pdbtrack-toggle-stack-tracking 0))
2923 
2924 
2925 
2926 ;; Pychecker
2927 (defun py-pychecker-run (command)
2928   "*Run pychecker (default on the file currently visited)."
2929   (interactive
2930    (let ((default
2931            (format "%s %s %s" py-pychecker-command
2932 		   (mapconcat 'identity py-pychecker-command-args " ")
2933 		   (buffer-file-name)))
2934 	 (last (when py-pychecker-history
2935 		 (let* ((lastcmd (car py-pychecker-history))
2936 			(cmd (cdr (reverse (split-string lastcmd))))
2937 			(newcmd (reverse (cons (buffer-file-name) cmd))))
2938 		   (mapconcat 'identity newcmd " ")))))
2939 
2940      (list
2941       (if (fboundp 'read-shell-command)
2942 	  (read-shell-command "Run pychecker like this: "
2943 			      (if last
2944 				  last
2945 				default)
2946 			      'py-pychecker-history)
2947 	(read-string "Run pychecker like this: "
2948 		     (if last
2949 			 last
2950 		       default)
2951 		     'py-pychecker-history))
2952 	)))
2953   (save-some-buffers (not py-ask-about-save) nil)
2954   (compile-internal command "No more errors"))
2955 
2956 
2957 
2958 ;; pydoc commands. The guts of this function is stolen from XEmacs's
2959 ;; symbol-near-point, but without the useless regexp-quote call on the
2960 ;; results, nor the interactive bit.  Also, we've added the temporary
2961 ;; syntax table setting, which Skip originally had broken out into a
2962 ;; separate function.  Note that Emacs doesn't have the original
2963 ;; function.
2964 (defun py-symbol-near-point ()
2965   "Return the first textual item to the nearest point."
2966   ;; alg stolen from etag.el
2967   (save-excursion
2968     (with-syntax-table py-dotted-expression-syntax-table
2969       (if (or (bobp) (not (memq (char-syntax (char-before)) '(?w ?_))))
2970 	  (while (not (looking-at "\\sw\\|\\s_\\|\\'"))
2971 	    (forward-char 1)))
2972       (while (looking-at "\\sw\\|\\s_")
2973 	(forward-char 1))
2974       (if (re-search-backward "\\sw\\|\\s_" nil t)
2975 	  (progn (forward-char 1)
2976 		 (buffer-substring (point)
2977 				   (progn (forward-sexp -1)
2978 					  (while (looking-at "\\s'")
2979 					    (forward-char 1))
2980 					  (point))))
2981 	nil))))
2982 
2983 (defun py-help-at-point ()
2984   "Get help from Python based on the symbol nearest point."
2985   (interactive)
2986   (let* ((sym (py-symbol-near-point))
2987 	 (base (substring sym 0 (or (search "." sym :from-end t) 0)))
2988 	 cmd)
2989     (if (not (equal base ""))
2990         (setq cmd (concat "import " base "\n")))
2991     (setq cmd (concat "import pydoc\n"
2992                       cmd
2993 		      "try: pydoc.help('" sym "')\n"
2994 		      "except: print 'No help available on:', \"" sym "\""))
2995     (message cmd)
2996     (py-execute-string cmd)
2997     (set-buffer "*Python Output*")
2998     ;; BAW: Should we really be leaving the output buffer in help-mode?
2999     (help-mode)))
3000 
3001 
3002 
3003 ;; Documentation functions
3004 
3005 ;; dump the long form of the mode blurb; does the usual doc escapes,
3006 ;; plus lines of the form ^[vc]:name$ to suck variable & command docs
3007 ;; out of the right places, along with the keys they're on & current
3008 ;; values
3009 (defun py-dump-help-string (str)
3010   (with-output-to-temp-buffer "*Help*"
3011     (let ((locals (buffer-local-variables))
3012 	  funckind funcname func funcdoc
3013 	  (start 0) mstart end
3014 	  keys )
3015       (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
3016 	(setq mstart (match-beginning 0)  end (match-end 0)
3017 	      funckind (substring str (match-beginning 1) (match-end 1))
3018 	      funcname (substring str (match-beginning 2) (match-end 2))
3019 	      func (intern funcname))
3020 	(princ (substitute-command-keys (substring str start mstart)))
3021 	(cond
3022 	 ((equal funckind "c")		; command
3023 	  (setq funcdoc (documentation func)
3024 		keys (concat
3025 		      "Key(s): "
3026 		      (mapconcat 'key-description
3027 				 (where-is-internal func py-mode-map)
3028 				 ", "))))
3029 	 ((equal funckind "v")		; variable
3030 	  (setq funcdoc (documentation-property func 'variable-documentation)
3031 		keys (if (assq func locals)
3032 			 (concat
3033 			  "Local/Global values: "
3034 			  (prin1-to-string (symbol-value func))
3035 			  " / "
3036 			  (prin1-to-string (default-value func)))
3037 		       (concat
3038 			"Value: "
3039 			(prin1-to-string (symbol-value func))))))
3040 	 (t				; unexpected
3041 	  (error "Error in py-dump-help-string, tag `%s'" funckind)))
3042 	(princ (format "\n-> %s:\t%s\t%s\n\n"
3043 		       (if (equal funckind "c") "Command" "Variable")
3044 		       funcname keys))
3045 	(princ funcdoc)
3046 	(terpri)
3047 	(setq start end))
3048       (princ (substitute-command-keys (substring str start))))
3049     (print-help-return-message)))
3050 
3051 (defun py-describe-mode ()
3052   "Dump long form of Python-mode docs."
3053   (interactive)
3054   (py-dump-help-string "Major mode for editing Python files.
3055 Knows about Python indentation, tokens, comments and continuation lines.
3056 Paragraphs are separated by blank lines only.
3057 
3058 Major sections below begin with the string `@'; specific function and
3059 variable docs begin with `->'.
3060 
3061 @EXECUTING PYTHON CODE
3062 
3063 \\[py-execute-import-or-reload]\timports or reloads the file in the Python interpreter
3064 \\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
3065 \\[py-execute-region]\tsends the current region
3066 \\[py-execute-def-or-class]\tsends the current function or class definition
3067 \\[py-execute-string]\tsends an arbitrary string
3068 \\[py-shell]\tstarts a Python interpreter window; this will be used by
3069 \tsubsequent Python execution commands
3070 %c:py-execute-import-or-reload
3071 %c:py-execute-buffer
3072 %c:py-execute-region
3073 %c:py-execute-def-or-class
3074 %c:py-execute-string
3075 %c:py-shell
3076 
3077 @VARIABLES
3078 
3079 py-indent-offset\tindentation increment
3080 py-block-comment-prefix\tcomment string used by comment-region
3081 
3082 py-python-command\tshell command to invoke Python interpreter
3083 py-temp-directory\tdirectory used for temp files (if needed)
3084 
3085 py-beep-if-tab-change\tring the bell if tab-width is changed
3086 %v:py-indent-offset
3087 %v:py-block-comment-prefix
3088 %v:py-python-command
3089 %v:py-temp-directory
3090 %v:py-beep-if-tab-change
3091 
3092 @KINDS OF LINES
3093 
3094 Each physical line in the file is either a `continuation line' (the
3095 preceding line ends with a backslash that's not part of a comment, or
3096 the paren/bracket/brace nesting level at the start of the line is
3097 non-zero, or both) or an `initial line' (everything else).
3098 
3099 An initial line is in turn a `blank line' (contains nothing except
3100 possibly blanks or tabs), a `comment line' (leftmost non-blank
3101 character is `#'), or a `code line' (everything else).
3102 
3103 Comment Lines
3104 
3105 Although all comment lines are treated alike by Python, Python mode
3106 recognizes two kinds that act differently with respect to indentation.
3107 
3108 An `indenting comment line' is a comment line with a blank, tab or
3109 nothing after the initial `#'.  The indentation commands (see below)
3110 treat these exactly as if they were code lines: a line following an
3111 indenting comment line will be indented like the comment line.  All
3112 other comment lines (those with a non-whitespace character immediately
3113 following the initial `#') are `non-indenting comment lines', and
3114 their indentation is ignored by the indentation commands.
3115 
3116 Indenting comment lines are by far the usual case, and should be used
3117 whenever possible.  Non-indenting comment lines are useful in cases
3118 like these:
3119 
3120 \ta = b   # a very wordy single-line comment that ends up being
3121 \t        #... continued onto another line
3122 
3123 \tif a == b:
3124 ##\t\tprint 'panic!' # old code we've `commented out'
3125 \t\treturn a
3126 
3127 Since the `#...' and `##' comment lines have a non-whitespace
3128 character following the initial `#', Python mode ignores them when
3129 computing the proper indentation for the next line.
3130 
3131 Continuation Lines and Statements
3132 
3133 The Python-mode commands generally work on statements instead of on
3134 individual lines, where a `statement' is a comment or blank line, or a
3135 code line and all of its following continuation lines (if any)
3136 considered as a single logical unit.  The commands in this mode
3137 generally (when it makes sense) automatically move to the start of the
3138 statement containing point, even if point happens to be in the middle
3139 of some continuation line.
3140 
3141 
3142 @INDENTATION
3143 
3144 Primarily for entering new code:
3145 \t\\[indent-for-tab-command]\t indent line appropriately
3146 \t\\[py-newline-and-indent]\t insert newline, then indent
3147 \t\\[py-electric-backspace]\t reduce indentation, or delete single character
3148 
3149 Primarily for reindenting existing code:
3150 \t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
3151 \t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
3152 
3153 \t\\[py-indent-region]\t reindent region to match its context
3154 \t\\[py-shift-region-left]\t shift region left by py-indent-offset
3155 \t\\[py-shift-region-right]\t shift region right by py-indent-offset
3156 
3157 Unlike most programming languages, Python uses indentation, and only
3158 indentation, to specify block structure.  Hence the indentation supplied
3159 automatically by Python-mode is just an educated guess:  only you know
3160 the block structure you intend, so only you can supply correct
3161 indentation.
3162 
3163 The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
3164 the indentation of preceding statements.  E.g., assuming
3165 py-indent-offset is 4, after you enter
3166 \tif a > 0: \\[py-newline-and-indent]
3167 the cursor will be moved to the position of the `_' (_ is not a
3168 character in the file, it's just used here to indicate the location of
3169 the cursor):
3170 \tif a > 0:
3171 \t    _
3172 If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
3173 to
3174 \tif a > 0:
3175 \t    c = d
3176 \t    _
3177 Python-mode cannot know whether that's what you intended, or whether
3178 \tif a > 0:
3179 \t    c = d
3180 \t_
3181 was your intent.  In general, Python-mode either reproduces the
3182 indentation of the (closest code or indenting-comment) preceding
3183 statement, or adds an extra py-indent-offset blanks if the preceding
3184 statement has `:' as its last significant (non-whitespace and non-
3185 comment) character.  If the suggested indentation is too much, use
3186 \\[py-electric-backspace] to reduce it.
3187 
3188 Continuation lines are given extra indentation.  If you don't like the
3189 suggested indentation, change it to something you do like, and Python-
3190 mode will strive to indent later lines of the statement in the same way.
3191 
3192 If a line is a continuation line by virtue of being in an unclosed
3193 paren/bracket/brace structure (`list', for short), the suggested
3194 indentation depends on whether the current line contains the first item
3195 in the list.  If it does, it's indented py-indent-offset columns beyond
3196 the indentation of the line containing the open bracket.  If you don't
3197 like that, change it by hand.  The remaining items in the list will mimic
3198 whatever indentation you give to the first item.
3199 
3200 If a line is a continuation line because the line preceding it ends with
3201 a backslash, the third and following lines of the statement inherit their
3202 indentation from the line preceding them.  The indentation of the second
3203 line in the statement depends on the form of the first (base) line:  if
3204 the base line is an assignment statement with anything more interesting
3205 than the backslash following the leftmost assigning `=', the second line
3206 is indented two columns beyond that `='.  Else it's indented to two
3207 columns beyond the leftmost solid chunk of non-whitespace characters on
3208 the base line.
3209 
3210 Warning:  indent-region should not normally be used!  It calls \\[indent-for-tab-command]
3211 repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
3212 structure you intend.
3213 %c:indent-for-tab-command
3214 %c:py-newline-and-indent
3215 %c:py-electric-backspace
3216 
3217 
3218 The next function may be handy when editing code you didn't write:
3219 %c:py-guess-indent-offset
3220 
3221 
3222 The remaining `indent' functions apply to a region of Python code.  They
3223 assume the block structure (equals indentation, in Python) of the region
3224 is correct, and alter the indentation in various ways while preserving
3225 the block structure:
3226 %c:py-indent-region
3227 %c:py-shift-region-left
3228 %c:py-shift-region-right
3229 
3230 @MARKING & MANIPULATING REGIONS OF CODE
3231 
3232 \\[py-mark-block]\t mark block of lines
3233 \\[py-mark-def-or-class]\t mark smallest enclosing def
3234 \\[universal-argument] \\[py-mark-def-or-class]\t mark smallest enclosing class
3235 \\[comment-region]\t comment out region of code
3236 \\[universal-argument] \\[comment-region]\t uncomment region of code
3237 %c:py-mark-block
3238 %c:py-mark-def-or-class
3239 %c:comment-region
3240 
3241 @MOVING POINT
3242 
3243 \\[py-previous-statement]\t move to statement preceding point
3244 \\[py-next-statement]\t move to statement following point
3245 \\[py-goto-block-up]\t move up to start of current block
3246 \\[py-beginning-of-def-or-class]\t move to start of def
3247 \\[universal-argument] \\[py-beginning-of-def-or-class]\t move to start of class
3248 \\[py-end-of-def-or-class]\t move to end of def
3249 \\[universal-argument] \\[py-end-of-def-or-class]\t move to end of class
3250 
3251 The first two move to one statement beyond the statement that contains
3252 point.  A numeric prefix argument tells them to move that many
3253 statements instead.  Blank lines, comment lines, and continuation lines
3254 do not count as `statements' for these commands.  So, e.g., you can go
3255 to the first code statement in a file by entering
3256 \t\\[beginning-of-buffer]\t to move to the top of the file
3257 \t\\[py-next-statement]\t to skip over initial comments and blank lines
3258 Or do `\\[py-previous-statement]' with a huge prefix argument.
3259 %c:py-previous-statement
3260 %c:py-next-statement
3261 %c:py-goto-block-up
3262 %c:py-beginning-of-def-or-class
3263 %c:py-end-of-def-or-class
3264 
3265 @LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
3266 
3267 `\\[indent-new-comment-line]' is handy for entering a multi-line comment.
3268 
3269 `\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
3270 overall class and def structure of a module.
3271 
3272 `\\[back-to-indentation]' moves point to a line's first non-blank character.
3273 
3274 `\\[indent-relative]' is handy for creating odd indentation.
3275 
3276 @OTHER EMACS HINTS
3277 
3278 If you don't like the default value of a variable, change its value to
3279 whatever you do like by putting a `setq' line in your .emacs file.
3280 E.g., to set the indentation increment to 4, put this line in your
3281 .emacs:
3282 \t(setq  py-indent-offset  4)
3283 To see the value of a variable, do `\\[describe-variable]' and enter the variable
3284 name at the prompt.
3285 
3286 When entering a key sequence like `C-c C-n', it is not necessary to
3287 release the CONTROL key after doing the `C-c' part -- it suffices to
3288 press the CONTROL key, press and release `c' (while still holding down
3289 CONTROL), press and release `n' (while still holding down CONTROL), &
3290 then release CONTROL.
3291 
3292 Entering Python mode calls with no arguments the value of the variable
3293 `python-mode-hook', if that value exists and is not nil; for backward
3294 compatibility it also tries `py-mode-hook'; see the `Hooks' section of
3295 the Elisp manual for details.
3296 
3297 Obscure:  When python-mode is first loaded, it looks for all bindings
3298 to newline-and-indent in the global keymap, and shadows them with
3299 local bindings to py-newline-and-indent."))
3300 
3301 (require 'info-look)
3302 ;; The info-look package does not always provide this function (it
3303 ;; appears this is the case with XEmacs 21.1)
3304 (when (fboundp 'info-lookup-maybe-add-help)
3305   (info-lookup-maybe-add-help
3306    :mode 'python-mode
3307    :regexp "[a-zA-Z0-9_]+"
3308    :doc-spec '(("(python-lib)Module Index")
3309 	       ("(python-lib)Class-Exception-Object Index")
3310 	       ("(python-lib)Function-Method-Variable Index")
3311 	       ("(python-lib)Miscellaneous Index")))
3312   )
3313 
3314 
3315 ;; Helper functions
3316 (defvar py-parse-state-re
3317   (concat
3318    "^[ \t]*\\(elif\\|else\\|while\\|def\\|class\\)\\>"
3319    "\\|"
3320    "^[^ #\t\n]"))
3321 
3322 (defun py-parse-state ()
3323   "Return the parse state at point (see `parse-partial-sexp' docs)."
3324   (save-excursion
3325     (let ((here (point))
3326 	  pps done)
3327       (while (not done)
3328 	;; back up to the first preceding line (if any; else start of
3329 	;; buffer) that begins with a popular Python keyword, or a
3330 	;; non- whitespace and non-comment character.  These are good
3331 	;; places to start parsing to see whether where we started is
3332 	;; at a non-zero nesting level.  It may be slow for people who
3333 	;; write huge code blocks or huge lists ... tough beans.
3334 	(re-search-backward py-parse-state-re nil 'move)
3335 	(beginning-of-line)
3336 	;; In XEmacs, we have a much better way to test for whether
3337 	;; we're in a triple-quoted string or not.  Emacs does not
3338 	;; have this built-in function, which is its loss because
3339 	;; without scanning from the beginning of the buffer, there's
3340 	;; no accurate way to determine this otherwise.
3341 	(save-excursion (setq pps (parse-partial-sexp (point) here)))
3342 	;; make sure we don't land inside a triple-quoted string
3343 	(setq done (or (not (nth 3 pps))
3344 		       (bobp)))
3345 	;; Just go ahead and short circuit the test back to the
3346 	;; beginning of the buffer.  This will be slow, but not
3347 	;; nearly as slow as looping through many
3348 	;; re-search-backwards.
3349 	(if (not done)
3350 	    (goto-char (point-min))))
3351       pps)))
3352 
3353 (defun py-nesting-level ()
3354   "Return the buffer position of the last unclosed enclosing list.
3355 If nesting level is zero, return nil."
3356   (let ((status (py-parse-state)))
3357     (if (zerop (car status))
3358 	nil				; not in a nest
3359       (car (cdr status)))))		; char# of open bracket
3360 
3361 (defun py-backslash-continuation-line-p ()
3362   "Return t iff preceding line ends with backslash that is not in a comment."
3363   (save-excursion
3364     (beginning-of-line)
3365     (and
3366      ;; use a cheap test first to avoid the regexp if possible
3367      ;; use 'eq' because char-after may return nil
3368      (eq (char-after (- (point) 2)) ?\\ )
3369      ;; make sure; since eq test passed, there is a preceding line
3370      (forward-line -1)			; always true -- side effect
3371      (looking-at py-continued-re))))
3372 
3373 (defun py-continuation-line-p ()
3374   "Return t iff current line is a continuation line."
3375   (save-excursion
3376     (beginning-of-line)
3377     (or (py-backslash-continuation-line-p)
3378 	(py-nesting-level))))
3379 
3380 (defun py-goto-beginning-of-tqs (delim)
3381   "Go to the beginning of the triple quoted string we find ourselves in.
3382 DELIM is the TQS string delimiter character we're searching backwards
3383 for."
3384   (let ((skip (and delim (make-string 1 delim)))
3385 	(continue t))
3386     (when skip
3387       (save-excursion
3388 	(while continue
3389 	  (py-safe (search-backward skip))
3390 	  (setq continue (and (not (bobp))
3391 			      (= (char-before) ?\\))))
3392 	(if (and (= (char-before) delim)
3393 		 (= (char-before (1- (point))) delim))
3394 	    (setq skip (make-string 3 delim))))
3395       ;; we're looking at a triple-quoted string
3396       (py-safe (search-backward skip)))))
3397 
3398 (defun py-goto-initial-line ()
3399   "Go to the initial line of the current statement.
3400 Usually this is the line we're on, but if we're on the 2nd or
3401 following lines of a continuation block, we need to go up to the first
3402 line of the block."
3403   ;; Tricky: We want to avoid quadratic-time behavior for long
3404   ;; continued blocks, whether of the backslash or open-bracket
3405   ;; varieties, or a mix of the two.  The following manages to do that
3406   ;; in the usual cases.
3407   ;;
3408   ;; Also, if we're sitting inside a triple quoted string, this will
3409   ;; drop us at the line that begins the string.
3410   (let (open-bracket-pos)
3411     (while (py-continuation-line-p)
3412       (beginning-of-line)
3413       (if (py-backslash-continuation-line-p)
3414 	  (while (py-backslash-continuation-line-p)
3415 	    (forward-line -1))
3416 	;; else zip out of nested brackets/braces/parens
3417 	(while (setq open-bracket-pos (py-nesting-level))
3418 	  (goto-char open-bracket-pos)))))
3419   (beginning-of-line))
3420 
3421 (defun py-goto-beyond-final-line ()
3422   "Go to the point just beyond the fine line of the current statement.
3423 Usually this is the start of the next line, but if this is a
3424 multi-line statement we need to skip over the continuation lines."
3425   ;; Tricky: Again we need to be clever to avoid quadratic time
3426   ;; behavior.
3427   ;;
3428   ;; XXX: Not quite the right solution, but deals with multi-line doc
3429   ;; strings
3430   (if (looking-at (concat "[ \t]*\\(" py-stringlit-re "\\)"))
3431       (goto-char (match-end 0)))
3432   ;;
3433   (forward-line 1)
3434   (let (state)
3435     (while (and (py-continuation-line-p)
3436 		(not (eobp)))
3437       ;; skip over the backslash flavor
3438       (while (and (py-backslash-continuation-line-p)
3439 		  (not (eobp)))
3440 	(forward-line 1))
3441       ;; if in nest, zip to the end of the nest
3442       (setq state (py-parse-state))
3443       (if (and (not (zerop (car state)))
3444 	       (not (eobp)))
3445 	  (progn
3446 	    (parse-partial-sexp (point) (point-max) 0 nil state)
3447 	    (forward-line 1))))))
3448 
3449 (defun py-statement-opens-block-p ()
3450   "Return t iff the current statement opens a block.
3451 I.e., iff it ends with a colon that is not in a comment.  Point should 
3452 be at the start of a statement."
3453   (save-excursion
3454     (let ((start (point))
3455 	  (finish (progn (py-goto-beyond-final-line) (1- (point))))
3456 	  (searching t)
3457 	  (answer nil)
3458 	  state)
3459       (goto-char start)
3460       (while searching
3461 	;; look for a colon with nothing after it except whitespace, and
3462 	;; maybe a comment
3463 	(if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
3464 			       finish t)
3465 	    (if (eq (point) finish)	; note: no `else' clause; just
3466 					; keep searching if we're not at
3467 					; the end yet
3468 		;; sure looks like it opens a block -- but it might
3469 		;; be in a comment
3470 		(progn
3471 		  (setq searching nil)	; search is done either way
3472 		  (setq state (parse-partial-sexp start
3473 						  (match-beginning 0)))
3474 		  (setq answer (not (nth 4 state)))))
3475 	  ;; search failed: couldn't find another interesting colon
3476 	  (setq searching nil)))
3477       answer)))
3478 
3479 (defun py-statement-closes-block-p ()
3480   "Return t iff the current statement closes a block.
3481 I.e., if the line starts with `return', `raise', `break', `continue',
3482 and `pass'.  This doesn't catch embedded statements."
3483   (let ((here (point)))
3484     (py-goto-initial-line)
3485     (back-to-indentation)
3486     (prog1
3487 	(looking-at (concat py-block-closing-keywords-re "\\>"))
3488       (goto-char here))))
3489 
3490 (defun py-goto-beyond-block ()
3491   "Go to point just beyond the final line of block begun by the current line.
3492 This is the same as where `py-goto-beyond-final-line' goes unless
3493 we're on colon line, in which case we go to the end of the block.
3494 Assumes point is at the beginning of the line."
3495   (if (py-statement-opens-block-p)
3496       (py-mark-block nil 'just-move)
3497     (py-goto-beyond-final-line)))
3498 
3499 (defun py-goto-statement-at-or-above ()
3500   "Go to the start of the first statement at or preceding point.
3501 Return t if there is such a statement, otherwise nil.  `Statement'
3502 does not include blank lines, comments, or continuation lines."
3503   (py-goto-initial-line)
3504   (if (looking-at py-blank-or-comment-re)
3505       ;; skip back over blank & comment lines
3506       ;; note:  will skip a blank or comment line that happens to be
3507       ;; a continuation line too
3508       (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
3509 	  (progn (py-goto-initial-line) t)
3510 	nil)
3511     t))
3512 
3513 (defun py-goto-statement-below ()
3514   "Go to start of the first statement following the statement containing point.
3515 Return t if there is such a statement, otherwise nil.  `Statement'
3516 does not include blank lines, comments, or continuation lines."
3517   (beginning-of-line)
3518   (let ((start (point)))
3519     (py-goto-beyond-final-line)
3520     (while (and
3521 	    (or (looking-at py-blank-or-comment-re)
3522 		(py-in-literal))
3523 	    (not (eobp)))
3524       (forward-line 1))
3525     (if (eobp)
3526 	(progn (goto-char start) nil)
3527       t)))
3528 
3529 (defun py-go-up-tree-to-keyword (key)
3530   "Go to begining of statement starting with KEY, at or preceding point.
3531 
3532 KEY is a regular expression describing a Python keyword.  Skip blank
3533 lines and non-indenting comments.  If the statement found starts with
3534 KEY, then stop, otherwise go back to first enclosing block starting
3535 with KEY.  If successful, leave point at the start of the KEY line and 
3536 return t.  Otherwise, leav point at an undefined place and return nil."
3537   ;; skip blanks and non-indenting #
3538   (py-goto-initial-line)
3539   (while (and
3540 	  (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
3541 	  (zerop (forward-line -1)))	; go back
3542     nil)
3543   (py-goto-initial-line)
3544   (let* ((re (concat "[ \t]*" key "\\b"))
3545 	 (case-fold-search nil)		; let* so looking-at sees this
3546 	 (found (looking-at re))
3547 	 (dead nil))
3548     (while (not (or found dead))
3549       (condition-case nil		; in case no enclosing block
3550 	  (py-goto-block-up 'no-mark)
3551 	(error (setq dead t)))
3552       (or dead (setq found (looking-at re))))
3553     (beginning-of-line)
3554     found))
3555 
3556 (defun py-suck-up-leading-text ()
3557   "Return string in buffer from start of indentation to end of line.
3558 Prefix with \"...\" if leading whitespace was skipped."
3559   (save-excursion
3560     (back-to-indentation)
3561     (concat
3562      (if (bolp) "" "...")
3563      (buffer-substring (point) (progn (end-of-line) (point))))))
3564 
3565 (defun py-suck-up-first-keyword ()
3566   "Return first keyword on the line as a Lisp symbol.
3567 `Keyword' is defined (essentially) as the regular expression
3568 ([a-z]+).  Returns nil if none was found."
3569   (let ((case-fold-search nil))
3570     (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
3571 	(intern (buffer-substring (match-beginning 1) (match-end 1)))
3572       nil)))
3573 
3574 (defun py-current-defun ()
3575   "Python value for `add-log-current-defun-function'.
3576 This tells add-log.el how to find the current function/method/variable."
3577   (save-excursion
3578 
3579     ;; Move back to start of the current statement.
3580 
3581     (py-goto-initial-line)
3582     (back-to-indentation)
3583     (while (and (or (looking-at py-blank-or-comment-re)
3584 		    (py-in-literal))
3585 		(not (bobp)))
3586       (backward-to-indentation 1))
3587     (py-goto-initial-line)
3588 
3589     (let ((scopes "")
3590 	  (sep "")
3591 	  dead assignment)
3592 
3593       ;; Check for an assignment.  If this assignment exists inside a
3594       ;; def, it will be overwritten inside the while loop.  If it
3595       ;; exists at top lever or inside a class, it will be preserved.
3596 
3597       (when (looking-at "[ \t]*\\([a-zA-Z0-9_]+\\)[ \t]*=")
3598 	(setq scopes (buffer-substring (match-beginning 1) (match-end 1)))
3599 	(setq assignment t)
3600 	(setq sep "."))
3601 
3602       ;; Prepend the name of each outer socpe (def or class).
3603 
3604       (while (not dead)
3605 	(if (and (py-go-up-tree-to-keyword "\\(class\\|def\\)")
3606 		 (looking-at
3607 		  "[ \t]*\\(class\\|def\\)[ \t]*\\([a-zA-Z0-9_]+\\)[ \t]*"))
3608 	    (let ((name (buffer-substring (match-beginning 2) (match-end 2))))
3609 	      (if (and assignment (looking-at "[ \t]*def"))
3610 		  (setq scopes name)
3611 		(setq scopes (concat name sep scopes))
3612 		(setq sep "."))))
3613 	(setq assignment nil)
3614 	(condition-case nil		; Terminate nicely at top level.
3615 	    (py-goto-block-up 'no-mark)
3616 	  (error (setq dead t))))
3617       (if (string= scopes "")
3618 	  nil
3619 	scopes))))
3620 
3621 
3622 
3623 (defconst py-help-address "[email protected]"
3624   "Address accepting submission of bug reports.")
3625 
3626 (defun py-version ()
3627   "Echo the current version of `python-mode' in the minibuffer."
3628   (interactive)
3629   (message "Using `python-mode' version %s" py-version)
3630   (py-keep-region-active))
3631 
3632 ;; only works under Emacs 19
3633 ;(eval-when-compile
3634 ;  (require 'reporter))
3635 
3636 (defun py-submit-bug-report (enhancement-p)
3637   "Submit via mail a bug report on `python-mode'.
3638 With \\[universal-argument] (programmatically, argument ENHANCEMENT-P
3639 non-nil) just submit an enhancement request."
3640   (interactive
3641    (list (not (y-or-n-p
3642 	       "Is this a bug report (hit `n' to send other comments)? "))))
3643   (let ((reporter-prompt-for-summary-p (if enhancement-p
3644 					   "(Very) brief summary: "
3645 					 t)))
3646     (require 'reporter)
3647     (reporter-submit-bug-report
3648      py-help-address			;address
3649      (concat "python-mode " py-version)	;pkgname
3650      ;; varlist
3651      (if enhancement-p nil
3652        '(py-python-command
3653 	 py-indent-offset
3654 	 py-block-comment-prefix
3655 	 py-temp-directory
3656 	 py-beep-if-tab-change))
3657      nil				;pre-hooks
3658      nil				;post-hooks
3659      "Dear Barry,")			;salutation
3660     (if enhancement-p nil
3661       (set-mark (point))
3662       (insert 
3663 "Please replace this text with a sufficiently large code sample\n\
3664 and an exact recipe so that I can reproduce your problem.  Failure\n\
3665 to do so may mean a greater delay in fixing your bug.\n\n")
3666       (exchange-point-and-mark)
3667       (py-keep-region-active))))
3668 
3669 
3670 (defun py-kill-emacs-hook ()
3671   "Delete files in `py-file-queue'.
3672 These are Python temporary files awaiting execution."
3673   (mapcar #'(lambda (filename)
3674 	      (py-safe (delete-file filename)))
3675 	  py-file-queue))
3676 
3677 ;; arrange to kill temp files when Emacs exists
3678 (add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
3679 (add-hook 'comint-output-filter-functions 'py-pdbtrack-track-stack-file)
3680 
3681 ;; Add a designator to the minor mode strings
3682 (or (assq 'py-pdbtrack-is-tracking-p minor-mode-alist)
3683     (push '(py-pdbtrack-is-tracking-p py-pdbtrack-minor-mode-string)
3684 	  minor-mode-alist))
3685 
3686 
3687 
3688 ;;; paragraph and string filling code from Bernhard Herzog
3689 ;;; see http://mail.python.org/pipermail/python-list/2002-May/103189.html
3690 
3691 (defun py-fill-comment (&optional justify)
3692   "Fill the comment paragraph around point"
3693   (let (;; Non-nil if the current line contains a comment.
3694 	has-comment
3695 
3696 	;; If has-comment, the appropriate fill-prefix for the comment.
3697 	comment-fill-prefix)
3698 
3699     ;; Figure out what kind of comment we are looking at.
3700     (save-excursion
3701       (beginning-of-line)
3702       (cond
3703        ;; A line with nothing but a comment on it?
3704        ((looking-at "[ \t]*#[# \t]*")
3705 	(setq has-comment t
3706 	      comment-fill-prefix (buffer-substring (match-beginning 0)
3707 						    (match-end 0))))
3708 
3709        ;; A line with some code, followed by a comment? Remember that the hash
3710        ;; which starts the comment shouldn't be part of a string or character.
3711        ((progn
3712 	  (while (not (looking-at "#\\|$"))
3713 	    (skip-chars-forward "^#\n\"'\\")
3714 	    (cond
3715 	     ((eq (char-after (point)) ?\\) (forward-char 2))
3716 	     ((memq (char-after (point)) '(?\" ?')) (forward-sexp 1))))
3717 	  (looking-at "#+[\t ]*"))
3718 	(setq has-comment t)
3719 	(setq comment-fill-prefix
3720 	      (concat (make-string (current-column) ? )
3721 		      (buffer-substring (match-beginning 0) (match-end 0)))))))
3722 
3723     (if (not has-comment)
3724 	(fill-paragraph justify)
3725 
3726       ;; Narrow to include only the comment, and then fill the region.
3727       (save-restriction
3728 	(narrow-to-region
3729 
3730 	 ;; Find the first line we should include in the region to fill.
3731 	 (save-excursion
3732 	   (while (and (zerop (forward-line -1))
3733 		       (looking-at "^[ \t]*#")))
3734 
3735 	   ;; We may have gone to far.  Go forward again.
3736 	   (or (looking-at "^[ \t]*#")
3737 	       (forward-line 1))
3738 	   (point))
3739 
3740 	 ;; Find the beginning of the first line past the region to fill.
3741 	 (save-excursion
3742 	   (while (progn (forward-line 1)
3743 			 (looking-at "^[ \t]*#")))
3744 	   (point)))
3745 
3746 	;; Lines with only hashes on them can be paragraph boundaries.
3747 	(let ((paragraph-start (concat paragraph-start "\\|[ \t#]*$"))
3748 	      (paragraph-separate (concat paragraph-separate "\\|[ \t#]*$"))
3749 	      (fill-prefix comment-fill-prefix))
3750 	  ;;(message "paragraph-start %S paragraph-separate %S"
3751 	  ;;paragraph-start paragraph-separate)
3752 	  (fill-paragraph justify))))
3753     t))
3754 
3755 
3756 (defun py-fill-string (start &optional justify)
3757   "Fill the paragraph around (point) in the string starting at start"
3758   ;; basic strategy: narrow to the string and call the default
3759   ;; implementation
3760   (let (;; the start of the string's contents
3761 	string-start
3762 	;; the end of the string's contents
3763 	string-end
3764 	;; length of the string's delimiter
3765 	delim-length
3766 	;; The string delimiter
3767 	delim
3768 	)
3769 
3770     (save-excursion
3771       (goto-char start)
3772       (if (looking-at "\\('''\\|\"\"\"\\|'\\|\"\\)\\\\?\n?")
3773 	  (setq string-start (match-end 0)
3774 		delim-length (- (match-end 1) (match-beginning 1))
3775 		delim (buffer-substring-no-properties (match-beginning 1)
3776 						      (match-end 1)))
3777 	(error "The parameter start is not the beginning of a python string"))
3778 
3779       ;; if the string is the first token on a line and doesn't start with
3780       ;; a newline, fill as if the string starts at the beginning of the
3781       ;; line. this helps with one line docstrings
3782       (save-excursion
3783 	(beginning-of-line)
3784 	(and (/= (char-before string-start) ?\n)
3785 	     (looking-at (concat "[ \t]*" delim))
3786 	     (setq string-start (point))))
3787 
3788       (forward-sexp (if (= delim-length 3) 2 1))
3789 
3790       ;; with both triple quoted strings and single/double quoted strings
3791       ;; we're now directly behind the first char of the end delimiter
3792       ;; (this doesn't work correctly when the triple quoted string
3793       ;; contains the quote mark itself). The end of the string's contents
3794       ;; is one less than point
3795       (setq string-end (1- (point))))
3796 
3797     ;; Narrow to the string's contents and fill the current paragraph
3798     (save-restriction
3799       (narrow-to-region string-start string-end)
3800       (let ((ends-with-newline (= (char-before (point-max)) ?\n)))
3801 	(fill-paragraph justify)
3802 	(if (and (not ends-with-newline)
3803 		 (= (char-before (point-max)) ?\n))
3804 	    ;; the default fill-paragraph implementation has inserted a
3805 	    ;; newline at the end. Remove it again.
3806 	    (save-excursion
3807 	      (goto-char (point-max))
3808 	      (delete-char -1)))))
3809 
3810     ;; return t to indicate that we've done our work
3811     t))
3812 
3813 (defun py-fill-paragraph (&optional justify)
3814   "Like \\[fill-paragraph], but handle Python comments and strings.
3815 If any of the current line is a comment, fill the comment or the
3816 paragraph of it that point is in, preserving the comment's indentation
3817 and initial `#'s.
3818 If point is inside a string, narrow to that string and fill.
3819 "
3820   (interactive "P")
3821   (let* ((bod (py-point 'bod))
3822 	 (pps (parse-partial-sexp bod (point))))
3823     (cond
3824      ;; are we inside a comment or on a line with only whitespace before
3825      ;; the comment start?
3826      ((or (nth 4 pps)
3827 	  (save-excursion (beginning-of-line) (looking-at "[ \t]*#")))
3828       (py-fill-comment justify))
3829      ;; are we inside a string?
3830      ((nth 3 pps)
3831       (py-fill-string (nth 8 pps)))
3832      ;; are we at the opening quote of a string, or in the indentation?
3833      ((save-excursion
3834 	(forward-word 1)
3835 	(eq (py-in-literal) 'string))
3836       (save-excursion
3837 	(py-fill-string (py-point 'boi))))
3838      ;; are we at or after the closing quote of a string?
3839      ((save-excursion
3840 	(backward-word 1)
3841 	(eq (py-in-literal) 'string))
3842       (save-excursion
3843 	(py-fill-string (py-point 'boi))))
3844      ;; otherwise use the default
3845      (t
3846       (fill-paragraph justify)))))
3847 
3848 
3849 
3850 (provide 'python-mode)
3851 ;;; python-mode.el ends here

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2008-06-29 22:37:57, 34.1 KB) [[attachment:doctest-mode.el]]
  • [get | view] (2008-06-29 22:38:05, 0.4 KB) [[attachment:pyrex-mode.el]]
  • [get | view] (2008-06-29 22:38:13, 2.4 KB) [[attachment:pyrex-mode.elc]]
  • [get | view] (2008-06-29 22:41:02, 141.2 KB) [[attachment:python-mode.el]]
  • [get | view] (2008-06-29 22:41:13, 108.9 KB) [[attachment:python-mode.elc]]
  • [get | view] (2008-06-29 22:38:19, 15.3 KB) [[attachment:sage.el]]
  • [get | view] (2008-06-29 22:38:23, 7.9 KB) [[attachment:sage.elc]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.