comparison lisp/modes/python-mode.el @ 70:131b0175ea99 r20-0b30

Import from CVS: tag r20-0b30
author cvs
date Mon, 13 Aug 2007 09:02:59 +0200
parents 441bb1e64a06
children 0d2f883870bc
comparison
equal deleted inserted replaced
69:804d1389bcd6 70:131b0175ea99
1 ;;; python-mode.el --- Major mode for editing Python programs 1 ;;; python-mode.el --- Major mode for editing Python programs
2 2
3 ;; Copyright (C) 1992,1993,1994 Tim Peters 3 ;; Copyright (C) 1992,1993,1994 Tim Peters
4 4
5 ;; Author: 1995-1997 Barry A. Warsaw 5 ;; Author: 1995-1996 Barry A. Warsaw
6 ;; 1992-1994 Tim Peters 6 ;; 1992-1994 Tim Peters
7 ;; Maintainer: python-mode@python.org 7 ;; Maintainer: python-mode@python.org
8 ;; Created: Feb 1992 8 ;; Created: Feb 1992
9 ;; Version: 2.90 9 ;; Version: 2.83
10 ;; Last Modified: 1997/02/24 03:37:22 10 ;; Last Modified: 1996/10/23 20:44:59
11 ;; Keywords: python languages oop 11 ;; Keywords: python languages oop
12 12
13 ;; This software is provided as-is, without express or implied 13 ;; This software is provided as-is, without express or implied
14 ;; warranty. Permission to use, copy, modify, distribute or sell this 14 ;; warranty. Permission to use, copy, modify, distribute or sell this
15 ;; software, without fee, for any purpose and by any individual or 15 ;; software, without fee, for any purpose and by any individual or
206 current value of (the general Emacs variable) `tab-width' does not 206 current value of (the general Emacs variable) `tab-width' does not
207 equal <number>, `tab-width' is set to <number>, a message saying so is 207 equal <number>, `tab-width' is set to <number>, a message saying so is
208 displayed in the echo area, and if `py-beep-if-tab-change' is non-nil 208 displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
209 the Emacs bell is also rung as a warning.") 209 the Emacs bell is also rung as a warning.")
210 210
211 (defvar python-font-lock-keywords 211 (defconst python-font-lock-keywords
212 (let* ((keywords '("and" "break" "class" 212 (let* ((keywords '("and" "break" "class"
213 "continue" "def" "del" "elif" 213 "continue" "def" "del" "elif"
214 "else:" "except" "except:" "exec" 214 "else:" "except" "except:" "exec"
215 "finally:" "for" "from" "global" 215 "finally:" "for" "from" "global"
216 "if" "import" "in" "is" 216 "if" "import" "in" "is"
232 ;; functions 232 ;; functions
233 '("\\bdef[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)" 233 '("\\bdef[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
234 1 font-lock-function-name-face) 234 1 font-lock-function-name-face)
235 )) 235 ))
236 "Additional expressions to highlight in Python mode.") 236 "Additional expressions to highlight in Python mode.")
237 (put 'python-mode 'font-lock-defaults '(python-font-lock-keywords))
238
239 237
240 (defvar imenu-example--python-show-method-args-p nil 238 (defvar imenu-example--python-show-method-args-p nil
241 "*Controls echoing of arguments of functions & methods in the imenu buffer. 239 "*Controls echoing of arguments of functions & methods in the imenu buffer.
242 When non-nil, arguments are printed.") 240 When non-nil, arguments are printed.")
243 241
274 272
275 ;; in previous version of python-mode.el, the hook was incorrectly 273 ;; in previous version of python-mode.el, the hook was incorrectly
276 ;; called py-mode-hook, and was not defvar'd. deprecate its use. 274 ;; called py-mode-hook, and was not defvar'd. deprecate its use.
277 (and (fboundp 'make-obsolete-variable) 275 (and (fboundp 'make-obsolete-variable)
278 (make-obsolete-variable 'py-mode-hook 'python-mode-hook)) 276 (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
279
280 (defvar py-delete-function 'backward-delete-char-untabify
281 "*Function called by `py-delete-char' when deleting characters.")
282 277
283 (defvar py-mode-map () 278 (defvar py-mode-map ()
284 "Keymap used in `python-mode' buffers.") 279 "Keymap used in `python-mode' buffers.")
285 280
286 (if py-mode-map 281 (if py-mode-map
410 "Regexp for finding a class name. 405 "Regexp for finding a class name.
411 406
412 If you change this, you probably have to change `py-current-defun' as well. 407 If you change this, you probably have to change `py-current-defun' as well.
413 This is only used by `py-current-defun' to find the name for add-log.el.") 408 This is only used by `py-current-defun' to find the name for add-log.el.")
414 409
415 ;; As of 30-Jan-1997, Emacs 19.34 works but XEmacs 19.15b90 and
416 ;; previous does not. It is suspected that Emacsen before 19.34 are
417 ;; also broken.
418 (defvar py-parse-partial-sexp-works-p
419 (let ((buf (get-buffer-create " ---*---pps---*---"))
420 state status)
421 (save-excursion
422 (set-buffer buf)
423 (erase-buffer)
424 (insert "(line1\n line2)\nline3")
425 (lisp-mode)
426 (goto-char (point-min))
427 (setq state (parse-partial-sexp (point) (save-excursion
428 (forward-line 1)
429 (point))))
430 (parse-partial-sexp (point) (point-max) 0 nil state)
431 (setq status (not (= (point) (point-max))))
432 (kill-buffer buf)
433 status))
434 "Does `parse-partial-sexp' work in this Emacs?")
435 410
436 411
437 ;; Menu definitions, only relevent if you have the easymenu.el package 412 ;; Menu definitions, only relevent if you have the easymenu.el package
438 ;; (standard in the latest Emacs 19 and XEmacs 19 distributions). 413 ;; (standard in the latest Emacs 19 and XEmacs 19 distributions).
439 (defvar py-menu nil 414 (defvar py-menu nil
447 (require 'easymenu) 422 (require 'easymenu)
448 (error nil)) 423 (error nil))
449 (easy-menu-define 424 (easy-menu-define
450 py-menu py-mode-map "Python Mode menu" 425 py-menu py-mode-map "Python Mode menu"
451 '("Python" 426 '("Python"
452 ["Comment Out Region" py-comment-region (mark)] 427 ["Comment Out Region" comment-region (mark)]
453 ["Uncomment Region" (py-comment-region (point) (mark) '(4)) (mark)] 428 ["Uncomment Region" (comment-region (point) (mark) '(4)) (mark)]
454 "-" 429 "-"
455 ["Mark current block" py-mark-block t] 430 ["Mark current block" py-mark-block t]
456 ["Mark current def" mark-python-def-or-class t] 431 ["Mark current def" mark-python-def-or-class t]
457 ["Mark current class" (mark-python-def-or-class t) t] 432 ["Mark current class" (mark-python-def-or-class t) t]
458 "-" 433 "-"
705 (make-local-variable 'font-lock-defaults) 680 (make-local-variable 'font-lock-defaults)
706 (make-local-variable 'paragraph-separate) 681 (make-local-variable 'paragraph-separate)
707 (make-local-variable 'paragraph-start) 682 (make-local-variable 'paragraph-start)
708 (make-local-variable 'require-final-newline) 683 (make-local-variable 'require-final-newline)
709 (make-local-variable 'comment-start) 684 (make-local-variable 'comment-start)
710 (make-local-variable 'comment-end)
711 (make-local-variable 'comment-start-skip) 685 (make-local-variable 'comment-start-skip)
712 (make-local-variable 'comment-column) 686 (make-local-variable 'comment-column)
713 (make-local-variable 'indent-region-function) 687 (make-local-variable 'indent-region-function)
714 (make-local-variable 'indent-line-function) 688 (make-local-variable 'indent-line-function)
715 (make-local-variable 'add-log-current-defun-function) 689 (make-local-variable 'add-log-current-defun-function)
716 ;; 690 ;;
717 (set-syntax-table py-mode-syntax-table) 691 (set-syntax-table py-mode-syntax-table)
718 (setq major-mode 'python-mode 692 (setq major-mode 'python-mode
719 mode-name "Python" 693 mode-name "Python"
720 local-abbrev-table python-mode-abbrev-table 694 local-abbrev-table python-mode-abbrev-table
695 font-lock-defaults '(python-font-lock-keywords)
721 paragraph-separate "^[ \t]*$" 696 paragraph-separate "^[ \t]*$"
722 paragraph-start "^[ \t]*$" 697 paragraph-start "^[ \t]*$"
723 require-final-newline t 698 require-final-newline t
724 comment-start "# " 699 comment-start "# "
725 comment-end ""
726 comment-start-skip "# *" 700 comment-start-skip "# *"
727 comment-column 40 701 comment-column 40
728 indent-region-function 'py-indent-region 702 indent-region-function 'py-indent-region
729 indent-line-function 'py-indent-line 703 indent-line-function 'py-indent-line
730 ;; tell add-log.el how to find the current function/method/variable 704 ;; tell add-log.el how to find the current function/method/variable
1014 988
1015 989
1016 ;; Functions for Python style indentation 990 ;; Functions for Python style indentation
1017 (defun py-delete-char (count) 991 (defun py-delete-char (count)
1018 "Reduce indentation or delete character. 992 "Reduce indentation or delete character.
1019
1020 If point is at the leftmost column, deletes the preceding newline. 993 If point is at the leftmost column, deletes the preceding newline.
1021 Deletion is performed by calling the function in `py-delete-function'
1022 with a single argument (the number of characters to delete).
1023 994
1024 Else if point is at the leftmost non-blank character of a line that is 995 Else if point is at the leftmost non-blank character of a line that is
1025 neither a continuation line nor a non-indenting comment line, or if 996 neither a continuation line nor a non-indenting comment line, or if
1026 point is at the end of a blank line, reduces the indentation to match 997 point is at the end of a blank line, reduces the indentation to match
1027 that of the line that opened the current block of code. The line that 998 that of the line that opened the current block of code. The line that
1036 (if (or (/= (current-indentation) (current-column)) 1007 (if (or (/= (current-indentation) (current-column))
1037 (bolp) 1008 (bolp)
1038 (py-continuation-line-p) 1009 (py-continuation-line-p)
1039 (not py-honor-comment-indentation) 1010 (not py-honor-comment-indentation)
1040 (looking-at "#[^ \t\n]")) ; non-indenting # 1011 (looking-at "#[^ \t\n]")) ; non-indenting #
1041 (funcall py-delete-function count) 1012 (backward-delete-char-untabify count)
1042 ;; else indent the same as the colon line that opened the block 1013 ;; else indent the same as the colon line that opened the block
1043 1014
1044 ;; force non-blank so py-goto-block-up doesn't ignore it 1015 ;; force non-blank so py-goto-block-up doesn't ignore it
1045 (insert-char ?* 1) 1016 (insert-char ?* 1)
1046 (backward-char) 1017 (backward-char)
2222 ;; if in nest, zip to the end of the nest 2193 ;; if in nest, zip to the end of the nest
2223 (setq state (py-parse-state)) 2194 (setq state (py-parse-state))
2224 (if (and (not (zerop (car state))) 2195 (if (and (not (zerop (car state)))
2225 (not (eobp))) 2196 (not (eobp)))
2226 (progn 2197 (progn
2227 (parse-partial-sexp (point) (point-max) 2198 ;; BUG ALERT: I could swear, from reading the docs, that
2228 (if py-parse-partial-sexp-works-p 2199 ;; the 3rd argument should be plain 0
2229 0 (- 0 (car state))) 2200 (parse-partial-sexp (point) (point-max) (- 0 (car state))
2230 nil state) 2201 nil state)
2231 (forward-line 1)))))) 2202 (forward-line 1))))))
2232 2203
2233 ;; t iff statement opens a block == iff it ends with a colon that's 2204 ;; t iff statement opens a block == iff it ends with a colon that's
2234 ;; not in a comment. point should be at the start of a statement 2205 ;; not in a comment. point should be at the start of a statement
2388 py-this-is-lucid-emacs-p)) 2359 py-this-is-lucid-emacs-p))
2389 (move-marker last-input-end (point))) ; muck w/ shell-mode 2360 (move-marker last-input-end (point))) ; muck w/ shell-mode
2390 (set-buffer cbuf)) 2361 (set-buffer cbuf))
2391 (sit-for 0)) 2362 (sit-for 0))
2392 2363
2393 ;; older Emacsen don't have this function
2394 (if (not (fboundp 'match-string))
2395 (defun match-string (n)
2396 (let ((beg (match-beginning n))
2397 (end (match-end n)))
2398 (if (and beg end)
2399 (buffer-substring beg end)
2400 nil))))
2401
2402 (defun py-current-defun () 2364 (defun py-current-defun ()
2403 ;; tell add-log.el how to find the current function/method/variable 2365 ;; tell add-log.el how to find the current function/method/variable
2404 (save-excursion 2366 (save-excursion
2405 (if (re-search-backward py-defun-start-re nil t) 2367 (if (re-search-backward py-defun-start-re nil t)
2406 (or (match-string 3) 2368 (or (match-string 3)
2410 (concat (match-string 1) "." method) 2372 (concat (match-string 1) "." method)
2411 method))) 2373 method)))
2412 nil))) 2374 nil)))
2413 2375
2414 2376
2415 (defconst py-version "2.90" 2377 (defconst py-version "2.83"
2416 "`python-mode' version number.") 2378 "`python-mode' version number.")
2417 (defconst py-help-address "python-mode@python.org" 2379 (defconst py-help-address "python-mode@python.org"
2418 "Address accepting submission of bug reports.") 2380 "Address accepting submission of bug reports.")
2419 2381
2420 (defun py-version () 2382 (defun py-version ()