annotate lisp/progmodes/python.el @ 72676:c8301fe78264

Task Done: ** Image scrolling problems reported by David Kastrup.
author Kim F. Storm <storm@cua.dk>
date Wed, 06 Sep 2006 10:25:42 +0000
parents a9874901ba07
children a15622a85c05 c358d0861b16
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1 ;;; python.el --- silly walks for Python
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2
68773
dc49655f57ae Update copyright for 2006.
Nick Roberts <nickrob@snap.net.nz>
parents: 68311
diff changeset
3 ;; Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
4
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
5 ;; Author: Dave Love <fx@gnu.org>
54943
07e279030b6f (python-compilation-line-number): Fix braindamage.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54938
diff changeset
6 ;; Maintainer: FSF
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
7 ;; Created: Nov 2003
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
8 ;; Keywords: languages
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
9
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
10 ;; This file is part of GNU Emacs.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
11
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
13 ;; it under the terms of the GNU General Public License as published by
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
15 ;; any later version.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
16
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
17 ;; GNU Emacs is distributed in the hope that it will be useful,
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
20 ;; GNU General Public License for more details.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
21
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
64085
18a818a2ee7c Update FSF's address.
Lute Kamstra <lute@gnu.org>
parents: 64049
diff changeset
25 ;; Boston, MA 02110-1301, USA.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
26
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
27 ;;; Commentary:
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
28
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
29 ;; Major mode for editing Python, with support for inferior processes.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
30
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
31 ;; There is another Python mode, python-mode.el, used by XEmacs and
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
32 ;; maintained with Python. That isn't covered by an FSF copyright
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
33 ;; assignment, unlike this code, and seems not to be well-maintained
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
34 ;; for Emacs (though I've submitted fixes). This mode is rather
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
35 ;; simpler and is better in other ways. In particular, using the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
36 ;; syntax functions with text properties maintained by font-lock makes
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
37 ;; it more correct with arbitrary string and comment contents.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
38
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
39 ;; This doesn't implement all the facilities of python-mode.el. Some
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
40 ;; just need doing, e.g. catching exceptions in the inferior Python
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
41 ;; buffer (but see M-x pdb for debugging). [Actually, the use of
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
42 ;; `compilation-shell-minor-mode' now is probably enough for that.]
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
43 ;; Others don't seem appropriate. For instance,
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
44 ;; `forward-into-nomenclature' should be done separately, since it's
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
45 ;; not specific to Python, and I've installed a minor mode to do the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
46 ;; job properly in Emacs 23. [CC mode 5.31 contains an incompatible
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
47 ;; feature, `c-subword-mode' which is intended to have a similar
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
48 ;; effect, but actually only affects word-oriented keybindings.]
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
49
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
50 ;; Other things seem more natural or canonical here, e.g. the
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
51 ;; {beginning,end}-of-defun implementation dealing with nested
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
52 ;; definitions, and the inferior mode following `cmuscheme'. (The
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
53 ;; inferior mode can find the source of errors from
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
54 ;; `python-send-region' & al via `compilation-shell-minor-mode'.)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
55 ;; There is (limited) symbol completion using lookup in Python and
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
56 ;; Eldoc support also using the inferior process. Successive TABs
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
57 ;; cycle between possible indentations for the line.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
58
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
59 ;; Even where it has similar facilities, this mode is incompatible
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
60 ;; with python-mode.el in some respects. For instance, various key
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
61 ;; bindings are changed to obey Emacs conventions.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
62
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
63 ;; TODO: See various Fixmes below.
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
64
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
65 ;;; Code:
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
66
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
67 (eval-when-compile
72527
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
68 (require 'cl)
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
69 (require 'compile)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
70 (require 'comint))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
71
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
72 (autoload 'comint-mode "comint")
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
73
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
74 (defgroup python nil
64049
69990675200d (python): Finish `defgroup' description with period.
Juanma Barranquero <lekktu@gmail.com>
parents: 63831
diff changeset
75 "Silly walks in the Python language."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
76 :group 'languages
59996
aac0a33f5772 Change release version from 21.4 to 22.1 throughout.
Kim F. Storm <storm@cua.dk>
parents: 59250
diff changeset
77 :version "22.1"
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
78 :link '(emacs-commentary-link "python"))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
79
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
80 ;;;###autoload
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
81 (add-to-list 'interpreter-mode-alist '("jython" . jython-mode))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
82 ;;;###autoload
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
83 (add-to-list 'interpreter-mode-alist '("python" . python-mode))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
84 ;;;###autoload
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
85 (add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
86
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
87 ;;;; Font lock
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
88
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
89 (defvar python-font-lock-keywords
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
90 `(,(rx symbol-start
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
91 ;; From v 2.4 reference.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
92 ;; def and class dealt with separately below
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
93 (or "and" "assert" "break" "continue" "del" "elif" "else"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
94 "except" "exec" "finally" "for" "from" "global" "if"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
95 "import" "in" "is" "lambda" "not" "or" "pass" "print"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
96 "raise" "return" "try" "while" "yield"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
97 ;; Future keywords
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
98 "as" "None")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
99 symbol-end)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
100 ;; Definitions
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
101 (,(rx symbol-start (group "class") (1+ space) (group (1+ (or word ?_))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
102 (1 font-lock-keyword-face) (2 font-lock-type-face))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
103 (,(rx symbol-start (group "def") (1+ space) (group (1+ (or word ?_))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
104 (1 font-lock-keyword-face) (2 font-lock-function-name-face))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
105 ;; Top-level assignments are worth highlighting.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
106 (,(rx line-start (group (1+ (or word ?_))) (0+ space) "=")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
107 (1 font-lock-variable-name-face))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
108 (,(rx "@" (1+ (or word ?_))) ; decorators
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
109 (0 font-lock-preprocessor-face))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
110
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
111 (defconst python-font-lock-syntactic-keywords
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
112 ;; Make outer chars of matching triple-quote sequences into generic
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
113 ;; string delimiters. Fixme: Is there a better way?
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
114 `((,(rx (or line-start buffer-start
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
115 (not (syntax escape))) ; avoid escaped leading quote
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
116 (group (optional (any "uUrR"))) ; prefix gets syntax property
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
117 (optional (any "rR")) ; possible second prefix
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
118 (group (syntax string-quote)) ; maybe gets property
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
119 (backref 2) ; per first quote
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
120 (group (backref 2))) ; maybe gets property
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
121 (1 (python-quote-syntax 1))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
122 (2 (python-quote-syntax 2))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
123 (3 (python-quote-syntax 3)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
124 ;; This doesn't really help.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
125 ;;; (,(rx (and ?\\ (group ?\n))) (1 " "))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
126 ))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
127
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
128 (defun python-quote-syntax (n)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
129 "Put `syntax-table' property correctly on triple quote.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
130 Used for syntactic keywords. N is the match number (1, 2 or 3)."
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
131 ;; Given a triple quote, we have to check the context to know
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
132 ;; whether this is an opening or closing triple or whether it's
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
133 ;; quoted anyhow, and should be ignored. (For that we need to do
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
134 ;; the same job as `syntax-ppss' to be correct and it seems to be OK
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
135 ;; to use it here despite initial worries.) We also have to sort
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
136 ;; out a possible prefix -- well, we don't _have_ to, but I think it
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
137 ;; should be treated as part of the string.
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
138
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
139 ;; Test cases:
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
140 ;; ur"""ar""" x='"' # """
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
141 ;; x = ''' """ ' a
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
142 ;; '''
58487
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
143 ;; x '"""' x """ \"""" x
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
144 ;; Fixme: """""" goes wrong (due to syntax-ppss not getting the string
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
145 ;; fence context).
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
146 (save-excursion
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
147 (goto-char (match-beginning 0))
58487
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
148 (cond
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
149 ;; Consider property for the last char if in a fenced string.
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
150 ((= n 3)
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
151 (let ((syntax (syntax-ppss)))
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
152 (when (eq t (nth 3 syntax)) ; after unclosed fence
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
153 (goto-char (nth 8 syntax)) ; fence position
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
154 (skip-chars-forward "uUrR") ; skip any prefix
58487
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
155 ;; Is it a matching sequence?
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
156 (if (eq (char-after) (char-after (match-beginning 2)))
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
157 (eval-when-compile (string-to-syntax "|"))))))
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
158 ;; Consider property for initial char, accounting for prefixes.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
159 ((or (and (= n 2) ; leading quote (not prefix)
58487
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
160 (= (match-beginning 1) (match-end 1))) ; prefix is null
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
161 (and (= n 1) ; prefix
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
162 (/= (match-beginning 1) (match-end 1)))) ; non-empty
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
163 (unless (eq 'string (syntax-ppss-context (syntax-ppss)))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
164 (eval-when-compile (string-to-syntax "|"))))
58487
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
165 ;; Otherwise (we're in a non-matching string) the property is
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
166 ;; nil, which is OK.
d0581f1eef46 (python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents: 58415
diff changeset
167 )))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
168
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
169 ;; This isn't currently in `font-lock-defaults' as probably not worth
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
170 ;; it -- we basically only mess with a few normally-symbol characters.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
171
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
172 ;; (defun python-font-lock-syntactic-face-function (state)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
173 ;; "`font-lock-syntactic-face-function' for Python mode.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
174 ;; Returns the string or comment face as usual, with side effect of putting
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
175 ;; a `syntax-table' property on the inside of the string or comment which is
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
176 ;; the standard syntax table."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
177 ;; (if (nth 3 state)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
178 ;; (save-excursion
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
179 ;; (goto-char (nth 8 state))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
180 ;; (condition-case nil
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
181 ;; (forward-sexp)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
182 ;; (error nil))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
183 ;; (put-text-property (1+ (nth 8 state)) (1- (point))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
184 ;; 'syntax-table (standard-syntax-table))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
185 ;; 'font-lock-string-face)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
186 ;; (put-text-property (1+ (nth 8 state)) (line-end-position)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
187 ;; 'syntax-table (standard-syntax-table))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
188 ;; 'font-lock-comment-face))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
189
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
190 ;;;; Keymap and syntax
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
191
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
192 (defvar python-mode-map
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
193 (let ((map (make-sparse-keymap)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
194 ;; Mostly taken from python-mode.el.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
195 (define-key map ":" 'python-electric-colon)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
196 (define-key map "\177" 'python-backspace)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
197 (define-key map "\C-c<" 'python-shift-left)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
198 (define-key map "\C-c>" 'python-shift-right)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
199 (define-key map "\C-c\C-k" 'python-mark-block)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
200 (define-key map "\C-c\C-n" 'python-next-statement)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
201 (define-key map "\C-c\C-p" 'python-previous-statement)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
202 (define-key map "\C-c\C-u" 'python-beginning-of-block)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
203 (define-key map "\C-c\C-f" 'python-describe-symbol)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
204 (define-key map "\C-c\C-w" 'python-check)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
205 (define-key map "\C-c\C-v" 'python-check) ; a la sgml-mode
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
206 (define-key map "\C-c\C-s" 'python-send-string)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
207 (define-key map [?\C-\M-x] 'python-send-defun)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
208 (define-key map "\C-c\C-r" 'python-send-region)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
209 (define-key map "\C-c\M-r" 'python-send-region-and-go)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
210 (define-key map "\C-c\C-c" 'python-send-buffer)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
211 (define-key map "\C-c\C-z" 'python-switch-to-python)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
212 (define-key map "\C-c\C-m" 'python-load-file)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
213 (define-key map "\C-c\C-l" 'python-load-file) ; a la cmuscheme
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
214 (substitute-key-definition 'complete-symbol 'python-complete-symbol
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
215 map global-map)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
216 (define-key map "\C-c\C-i" 'python-find-imports)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
217 (define-key map "\C-c\C-t" 'python-expand-template)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
218 (easy-menu-define python-menu map "Python Mode menu"
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
219 `("Python"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
220 :help "Python-specific Features"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
221 ["Shift region left" python-shift-left :active mark-active
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
222 :help "Shift by a single indentation step"]
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
223 ["Shift region right" python-shift-right :active mark-active
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
224 :help "Shift by a single indentation step"]
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
225 "-"
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
226 ["Mark block" python-mark-block
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
227 :help "Mark innermost block around point"]
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
228 ["Mark def/class" mark-defun
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
229 :help "Mark innermost definition around point"]
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
230 "-"
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
231 ["Start of block" python-beginning-of-block
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
232 :help "Go to start of innermost definition around point"]
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
233 ["End of block" python-end-of-block
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
234 :help "Go to end of innermost definition around point"]
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
235 ["Start of def/class" beginning-of-defun
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
236 :help "Go to start of innermost definition around point"]
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
237 ["End of def/class" end-of-defun
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
238 :help "Go to end of innermost definition around point"]
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
239 "-"
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
240 ("Templates..."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
241 :help "Expand templates for compound statements"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
242 :filter (lambda (&rest junk)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
243 (mapcar (lambda (elt)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
244 (vector (car elt) (cdr elt) t))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
245 python-skeletons))) ; defined later
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
246 "-"
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
247 ["Start interpreter" run-python
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
248 :help "Run `inferior' Python in separate buffer"]
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
249 ["Import/reload file" python-load-file
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
250 :help "Load into inferior Python session"]
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
251 ["Eval buffer" python-send-buffer
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
252 :help "Evaluate buffer en bloc in inferior Python session"]
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
253 ["Eval region" python-send-region :active mark-active
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
254 :help "Evaluate region en bloc in inferior Python session"]
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
255 ["Eval def/class" python-send-defun
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
256 :help "Evaluate current definition in inferior Python session"]
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
257 ["Switch to interpreter" python-switch-to-python
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
258 :help "Switch to inferior Python buffer"]
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
259 ["Set default process" python-set-proc
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
260 :help "Make buffer's inferior process the default"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
261 :active (buffer-live-p python-buffer)]
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
262 ["Check file" python-check :help "Run pychecker"]
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
263 ["Debugger" pdb :help "Run pdb under GUD"]
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
264 "-"
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
265 ["Help on symbol" python-describe-symbol
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
266 :help "Use pydoc on symbol at point"]
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
267 ["Complete symbol" python-complete-symbol
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
268 :help "Complete (qualified) symbol before point"]
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
269 ["Update imports" python-find-imports
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
270 :help "Update list of top-level imports for completion"]))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
271 map))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
272 ;; Fixme: add toolbar stuff for useful things like symbol help, send
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
273 ;; region, at least. (Shouldn't be specific to Python, obviously.)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
274 ;; eric has items including: (un)indent, (un)comment, restart script,
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
275 ;; run script, debug script; also things for profiling, unit testing.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
276
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
277 (defvar python-mode-syntax-table
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
278 (let ((table (make-syntax-table)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
279 ;; Give punctuation syntax to ASCII that normally has symbol
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
280 ;; syntax or has word syntax and isn't a letter.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
281 (let ((symbol (string-to-syntax "_"))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
282 (sst (standard-syntax-table)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
283 (dotimes (i 128)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
284 (unless (= i ?_)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
285 (if (equal symbol (aref sst i))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
286 (modify-syntax-entry i "." table)))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
287 (modify-syntax-entry ?$ "." table)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
288 (modify-syntax-entry ?% "." table)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
289 ;; exceptions
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
290 (modify-syntax-entry ?# "<" table)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
291 (modify-syntax-entry ?\n ">" table)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
292 (modify-syntax-entry ?' "\"" table)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
293 (modify-syntax-entry ?` "$" table)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
294 table))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
295
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
296 ;;;; Utility stuff
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
297
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
298 (defsubst python-in-string/comment ()
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
299 "Return non-nil if point is in a Python literal (a comment or string)."
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
300 ;; We don't need to save the match data.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
301 (nth 8 (syntax-ppss)))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
302
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
303 (defconst python-space-backslash-table
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
304 (let ((table (copy-syntax-table python-mode-syntax-table)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
305 (modify-syntax-entry ?\\ " " table)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
306 table)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
307 "`python-mode-syntax-table' with backslash given whitespace syntax.")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
308
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
309 (defun python-skip-comments/blanks (&optional backward)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
310 "Skip comments and blank lines.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
311 BACKWARD non-nil means go backwards, otherwise go forwards.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
312 Backslash is treated as whitespace so that continued blank lines
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
313 are skipped. Doesn't move out of comments -- should be outside
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
314 or at end of line."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
315 (let ((arg (if backward
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
316 ;; If we're in a comment (including on the trailing
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
317 ;; newline), forward-comment doesn't move backwards out
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
318 ;; of it. Don't set the syntax table round this bit!
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
319 (let ((syntax (syntax-ppss)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
320 (if (nth 4 syntax)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
321 (goto-char (nth 8 syntax)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
322 (- (point-max)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
323 (point-max))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
324 (with-syntax-table python-space-backslash-table
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
325 (forward-comment arg))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
326
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
327 (defun python-backslash-continuation-line-p ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
328 "Non-nil if preceding line ends with backslash that is not in a comment."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
329 (and (eq ?\\ (char-before (line-end-position 0)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
330 (not (syntax-ppss-context (syntax-ppss)))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
331
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
332 (defun python-continuation-line-p ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
333 "Return non-nil if current line continues a previous one.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
334 The criteria are that the previous line ends in a backslash outside
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
335 comments and strings, or that point is within brackets/parens."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
336 (or (python-backslash-continuation-line-p)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
337 (let ((depth (syntax-ppss-depth
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
338 (save-excursion ; syntax-ppss with arg changes point
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
339 (syntax-ppss (line-beginning-position))))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
340 (or (> depth 0)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
341 (if (< depth 0) ; Unbalanced brackets -- act locally
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
342 (save-excursion
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
343 (condition-case ()
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
344 (progn (backward-up-list) t) ; actually within brackets
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
345 (error nil))))))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
346
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
347 (defun python-comment-line-p ()
55495
4e81c5df6c36 (inferior-python-mode-map): Remove erroneous C-c C-z binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55447
diff changeset
348 "Return non-nil iff current line has only a comment."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
349 (save-excursion
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
350 (end-of-line)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
351 (when (eq 'comment (syntax-ppss-context (syntax-ppss)))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
352 (back-to-indentation)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
353 (looking-at (rx (or (syntax comment-start) line-end))))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
354
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
355 (defun python-blank-line-p ()
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
356 "Return non-nil iff current line is blank."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
357 (save-excursion
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
358 (beginning-of-line)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
359 (looking-at "\\s-*$")))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
360
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
361 (defun python-beginning-of-string ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
362 "Go to beginning of string around point.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
363 Do nothing if not in string."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
364 (let ((state (syntax-ppss)))
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
365 (when (eq 'string (syntax-ppss-context state))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
366 (goto-char (nth 8 state)))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
367
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
368 (defun python-open-block-statement-p (&optional bos)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
369 "Return non-nil if statement at point opens a block.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
370 BOS non-nil means point is known to be at beginning of statement."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
371 (save-excursion
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
372 (unless bos (python-beginning-of-statement))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
373 (looking-at (rx (and (or "if" "else" "elif" "while" "for" "def"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
374 "class" "try" "except" "finally")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
375 symbol-end)))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
376
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
377 (defun python-close-block-statement-p (&optional bos)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
378 "Return non-nil if current line is a statement closing a block.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
379 BOS non-nil means point is at beginning of statement.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
380 The criteria are that the line isn't a comment or in string and
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
381 starts with keyword `raise', `break', `continue' or `pass'."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
382 (save-excursion
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
383 (unless bos (python-beginning-of-statement))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
384 (back-to-indentation)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
385 (looking-at (rx (or "return" "raise" "break" "continue" "pass")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
386 symbol-end))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
387
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
388 (defun python-outdent-p ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
389 "Return non-nil if current line should outdent a level."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
390 (save-excursion
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
391 (back-to-indentation)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
392 (and (looking-at (rx (and (or "else" "finally" "except" "elif")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
393 symbol-end)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
394 (not (python-in-string/comment))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
395 ;; Ensure there's a previous statement and move to it.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
396 (zerop (python-previous-statement))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
397 (not (python-close-block-statement-p t))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
398 ;; Fixme: check this
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
399 (not (python-open-block-statement-p)))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
400
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
401 ;;;; Indentation.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
402
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
403 (defcustom python-indent 4
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
404 "Number of columns for a unit of indentation in Python mode.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
405 See also `\\[python-guess-indent]'"
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
406 :group 'python
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
407 :type 'integer)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
408
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
409 (defcustom python-guess-indent t
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
410 "Non-nil means Python mode guesses `python-indent' for the buffer."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
411 :type 'boolean
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
412 :group 'python)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
413
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
414 (defcustom python-indent-string-contents t
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
415 "Non-nil means indent contents of multi-line strings together.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
416 This means indent them the same as the preceding non-blank line.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
417 Otherwise preserve their indentation.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
418
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
419 This only applies to `doc' strings, i.e. those that form statements;
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
420 the indentation is preserved in others."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
421 :type '(choice (const :tag "Align with preceding" t)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
422 (const :tag "Preserve indentation" nil))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
423 :group 'python)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
424
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
425 (defcustom python-honour-comment-indentation nil
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
426 "Non-nil means indent relative to preceding comment line.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
427 Only do this for comments where the leading comment character is
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
428 followed by space. This doesn't apply to comment lines, which
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
429 are always indented in lines with preceding comments."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
430 :type 'boolean
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
431 :group 'python)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
432
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
433 (defcustom python-continuation-offset 4
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
434 "Number of columns of additional indentation for continuation lines.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
435 Continuation lines follow a backslash-terminated line starting a
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
436 statement."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
437 :group 'python
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
438 :type 'integer)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
439
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
440 (defun python-guess-indent ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
441 "Guess step for indentation of current buffer.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
442 Set `python-indent' locally to the value guessed."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
443 (interactive)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
444 (save-excursion
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
445 (save-restriction
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
446 (widen)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
447 (goto-char (point-min))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
448 (let (done indent)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
449 (while (and (not done) (not (eobp)))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
450 (when (and (re-search-forward (rx ?: (0+ space)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
451 (or (syntax comment-start)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
452 line-end))
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
453 nil 'move)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
454 (python-open-block-statement-p))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
455 (save-excursion
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
456 (python-beginning-of-statement)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
457 (let ((initial (current-indentation)))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
458 (if (zerop (python-next-statement))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
459 (setq indent (- (current-indentation) initial)))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
460 (if (and (>= indent 2) (<= indent 8)) ; sanity check
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
461 (setq done t))))))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
462 (when done
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
463 (when (/= indent (default-value 'python-indent))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
464 (set (make-local-variable 'python-indent) indent)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
465 (unless (= tab-width python-indent)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
466 (setq indent-tabs-mode nil)))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
467 indent)))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
468
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
469 ;; Alist of possible indentations and start of statement they would
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
470 ;; close. Used in indentation cycling (below).
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
471 (defvar python-indent-list nil
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
472 "Internal use.")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
473 ;; Length of the above
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
474 (defvar python-indent-list-length nil
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
475 "Internal use.")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
476 ;; Current index into the alist.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
477 (defvar python-indent-index nil
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
478 "Internal use.")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
479
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
480 (defun python-calculate-indentation ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
481 "Calculate Python indentation for line at point."
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
482 (setq python-indent-list nil
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
483 python-indent-list-length 1)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
484 (save-excursion
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
485 (beginning-of-line)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
486 (let ((syntax (syntax-ppss))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
487 start)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
488 (cond
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
489 ((eq 'string (syntax-ppss-context syntax)) ; multi-line string
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
490 (if (not python-indent-string-contents)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
491 (current-indentation)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
492 ;; Only respect `python-indent-string-contents' in doc
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
493 ;; strings (defined as those which form statements).
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
494 (if (not (save-excursion
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
495 (python-beginning-of-statement)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
496 (looking-at (rx (or (syntax string-delimiter)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
497 (syntax string-quote))))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
498 (current-indentation)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
499 ;; Find indentation of preceding non-blank line within string.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
500 (setq start (nth 8 syntax))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
501 (forward-line -1)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
502 (while (and (< start (point)) (looking-at "\\s-*$"))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
503 (forward-line -1))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
504 (current-indentation))))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
505 ((python-continuation-line-p) ; after backslash, or bracketed
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
506 (let ((point (point))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
507 (open-start (cadr syntax))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
508 (backslash (python-backslash-continuation-line-p))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
509 (colon (eq ?: (char-before (1- (line-beginning-position))))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
510 (if open-start
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
511 ;; Inside bracketed expression.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
512 (progn
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
513 (goto-char (1+ open-start))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
514 ;; Look for first item in list (preceding point) and
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
515 ;; align with it, if found.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
516 (if (with-syntax-table python-space-backslash-table
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
517 (let ((parse-sexp-ignore-comments t))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
518 (condition-case ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
519 (progn (forward-sexp)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
520 (backward-sexp)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
521 (< (point) point))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
522 (error nil))))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
523 ;; Extra level if we're backslash-continued or
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
524 ;; following a key.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
525 (if (or backslash colon)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
526 (+ python-indent (current-column))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
527 (current-column))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
528 ;; Otherwise indent relative to statement start, one
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
529 ;; level per bracketing level.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
530 (goto-char (1+ open-start))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
531 (python-beginning-of-statement)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
532 (+ (current-indentation) (* (car syntax) python-indent))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
533 ;; Otherwise backslash-continued.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
534 (forward-line -1)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
535 (if (python-continuation-line-p)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
536 ;; We're past first continuation line. Align with
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
537 ;; previous line.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
538 (current-indentation)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
539 ;; First continuation line. Indent one step, with an
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
540 ;; extra one if statement opens a block.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
541 (python-beginning-of-statement)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
542 (+ (current-indentation) python-continuation-offset
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
543 (if (python-open-block-statement-p t)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
544 python-indent
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
545 0))))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
546 ((bobp) 0)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
547 ;; Fixme: Like python-mode.el; not convinced by this.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
548 ((looking-at (rx (0+ space) (syntax comment-start)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
549 (not (any " \t\n")))) ; non-indentable comment
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
550 (current-indentation))
72527
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
551 (t (if python-honour-comment-indentation
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
552 ;; Back over whitespace, newlines, non-indentable comments.
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
553 (catch 'done
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
554 (while t
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
555 (if (cond ((bobp))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
556 ;; not at comment start
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
557 ((not (forward-comment -1))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
558 (python-beginning-of-statement)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
559 t)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
560 ;; trailing comment
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
561 ((/= (current-column) (current-indentation))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
562 (python-beginning-of-statement)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
563 t)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
564 ;; indentable comment like python-mode.el
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
565 ((and (looking-at (rx (syntax comment-start)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
566 (or space line-end)))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
567 (/= 0 (current-column)))))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
568 (throw 'done t)))))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
569 (python-indentation-levels)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
570 ;; Prefer to indent comments with an immediately-following
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
571 ;; statement, e.g.
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
572 ;; ...
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
573 ;; # ...
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
574 ;; def ...
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
575 (when (and (> python-indent-list-length 1)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
576 (python-comment-line-p))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
577 (forward-line)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
578 (unless (python-comment-line-p)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
579 (let ((elt (assq (current-indentation) python-indent-list)))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
580 (setq python-indent-list
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
581 (nconc (delete elt python-indent-list)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
582 (list elt))))))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
583 (caar (last python-indent-list)))))))
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
584
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
585 ;;;; Cycling through the possible indentations with successive TABs.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
586
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
587 ;; These don't need to be buffer-local since they're only relevant
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
588 ;; during a cycle.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
589
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
590 (defun python-initial-text ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
591 "Text of line following indentation and ignoring any trailing comment."
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
592 (save-excursion
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
593 (buffer-substring (progn
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
594 (back-to-indentation)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
595 (point))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
596 (progn
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
597 (end-of-line)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
598 (forward-comment -1)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
599 (point)))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
600
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
601 (defconst python-block-pairs
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
602 '(("else" "if" "elif" "while" "for" "try" "except")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
603 ("elif" "if" "elif")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
604 ("except" "try" "except")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
605 ("finally" "try"))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
606 "Alist of keyword matches.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
607 The car of an element is a keyword introducing a statement which
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
608 can close a block opened by a keyword in the cdr.")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
609
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
610 (defun python-first-word ()
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
611 "Return first word (actually symbol) on the line."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
612 (save-excursion
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
613 (back-to-indentation)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
614 (current-word t)))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
615
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
616 (defun python-indentation-levels ()
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
617 "Return a list of possible indentations for this line.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
618 It is assumed not to be a continuation line or in a multi-line string.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
619 Includes the default indentation and those which would close all
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
620 enclosing blocks. Elements of the list are actually pairs:
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
621 \(INDENTATION . TEXT), where TEXT is the initial text of the
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
622 corresponding block opening (or nil)."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
623 (save-excursion
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
624 (let ((initial "")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
625 levels indent)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
626 ;; Only one possibility immediately following a block open
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
627 ;; statement, assuming it doesn't have a `suite' on the same line.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
628 (cond
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
629 ((save-excursion (and (python-previous-statement)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
630 (python-open-block-statement-p t)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
631 (setq indent (current-indentation))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
632 ;; Check we don't have something like:
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
633 ;; if ...: ...
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
634 (if (progn (python-end-of-statement)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
635 (python-skip-comments/blanks t)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
636 (eq ?: (char-before)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
637 (setq indent (+ python-indent indent)))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
638 (push (cons indent initial) levels))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
639 ;; Only one possibility for comment line immediately following
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
640 ;; another.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
641 ((save-excursion
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
642 (when (python-comment-line-p)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
643 (forward-line -1)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
644 (if (python-comment-line-p)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
645 (push (cons (current-indentation) initial) levels)))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
646 ;; Fixme: Maybe have a case here which indents (only) first
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
647 ;; line after a lambda.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
648 (t
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
649 (let ((start (car (assoc (python-first-word) python-block-pairs))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
650 (python-previous-statement)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
651 ;; Is this a valid indentation for the line of interest?
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
652 (unless (or (if start ; potentially only outdentable
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
653 ;; Check for things like:
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
654 ;; if ...: ...
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
655 ;; else ...:
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
656 ;; where the second line need not be outdented.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
657 (not (member (python-first-word)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
658 (cdr (assoc start
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
659 python-block-pairs)))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
660 ;; Not sensible to indent to the same level as
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
661 ;; previous `return' &c.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
662 (python-close-block-statement-p))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
663 (push (cons (current-indentation) (python-initial-text))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
664 levels))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
665 (while (python-beginning-of-block)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
666 (when (or (not start)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
667 (member (python-first-word)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
668 (cdr (assoc start python-block-pairs))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
669 (push (cons (current-indentation) (python-initial-text))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
670 levels))))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
671 (prog1 (or levels (setq levels '((0 . ""))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
672 (setq python-indent-list levels
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
673 python-indent-list-length (length python-indent-list))))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
674
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
675 ;; This is basically what `python-indent-line' would be if we didn't
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
676 ;; do the cycling.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
677 (defun python-indent-line-1 (&optional leave)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
678 "Subroutine of `python-indent-line'.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
679 Does non-repeated indentation. LEAVE non-nil means leave
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
680 indentation if it is valid, i.e. one of the positions returned by
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
681 `python-calculate-indentation'."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
682 (let ((target (python-calculate-indentation))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
683 (pos (- (point-max) (point))))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
684 (if (or (= target (current-indentation))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
685 ;; Maybe keep a valid indentation.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
686 (and leave python-indent-list
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
687 (assq (current-indentation) python-indent-list)))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
688 (if (< (current-column) (current-indentation))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
689 (back-to-indentation))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
690 (beginning-of-line)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
691 (delete-horizontal-space)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
692 (indent-to target)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
693 (if (> (- (point-max) pos) (point))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
694 (goto-char (- (point-max) pos))))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
695
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
696 (defun python-indent-line ()
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
697 "Indent current line as Python code.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
698 When invoked via `indent-for-tab-command', cycle through possible
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
699 indentations for current line. The cycle is broken by a command
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
700 different from `indent-for-tab-command', i.e. successive TABs do
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
701 the cycling."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
702 (interactive)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
703 (if (and (eq this-command 'indent-for-tab-command)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
704 (eq last-command this-command))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
705 (if (= 1 python-indent-list-length)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
706 (message "Sole indentation")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
707 (progn (setq python-indent-index
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
708 (% (1+ python-indent-index) python-indent-list-length))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
709 (beginning-of-line)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
710 (delete-horizontal-space)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
711 (indent-to (car (nth python-indent-index python-indent-list)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
712 (if (python-block-end-p)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
713 (let ((text (cdr (nth python-indent-index
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
714 python-indent-list))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
715 (if text
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
716 (message "Closes: %s" text))))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
717 (python-indent-line-1)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
718 (setq python-indent-index (1- python-indent-list-length))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
719
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
720 (defun python-indent-region (start end)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
721 "`indent-region-function' for Python.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
722 Leaves validly-indented lines alone, i.e. doesn't indent to
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
723 another valid position."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
724 (save-excursion
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
725 (goto-char end)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
726 (setq end (point-marker))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
727 (goto-char start)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
728 (or (bolp) (forward-line 1))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
729 (while (< (point) end)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
730 (or (and (bolp) (eolp))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
731 (python-indent-line-1 t))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
732 (forward-line 1))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
733 (move-marker end nil)))
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
734
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
735 (defun python-block-end-p ()
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
736 "Non-nil if this is a line in a statement closing a block,
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
737 or a blank line indented to where it would close a block."
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
738 (and (not (python-comment-line-p))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
739 (or (python-close-block-statement-p t)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
740 (< (current-indentation)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
741 (save-excursion
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
742 (python-previous-statement)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
743 (current-indentation))))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
744
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
745 ;;;; Movement.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
746
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
747 ;; Fixme: Define {for,back}ward-sexp-function? Maybe skip units like
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
748 ;; block, statement, depending on context.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
749
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
750 (defun python-beginning-of-defun ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
751 "`beginning-of-defun-function' for Python.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
752 Finds beginning of innermost nested class or method definition.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
753 Returns the name of the definition found at the end, or nil if
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
754 reached start of buffer."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
755 (let ((ci (current-indentation))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
756 (def-re (rx line-start (0+ space) (or "def" "class") (1+ space)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
757 (group (1+ (or word (syntax symbol))))))
72527
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
758 found lep) ;; def-line
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
759 (if (python-comment-line-p)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
760 (setq ci most-positive-fixnum))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
761 (while (and (not (bobp)) (not found))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
762 ;; Treat bol at beginning of function as outside function so
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
763 ;; that successive C-M-a makes progress backwards.
72527
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
764 ;;(setq def-line (looking-at def-re))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
765 (unless (bolp) (end-of-line))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
766 (setq lep (line-end-position))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
767 (if (and (re-search-backward def-re nil 'move)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
768 ;; Must be less indented or matching top level, or
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
769 ;; equally indented if we started on a definition line.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
770 (let ((in (current-indentation)))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
771 (or (and (zerop ci) (zerop in))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
772 (= lep (line-end-position)) ; on initial line
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
773 ;; Not sure why it was like this -- fails in case of
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
774 ;; last internal function followed by first
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
775 ;; non-def statement of the main body.
72527
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
776 ;;(and def-line (= in ci))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
777 (= in ci)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
778 (< in ci)))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
779 (not (python-in-string/comment)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
780 (setq found t)))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
781
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
782 (defun python-end-of-defun ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
783 "`end-of-defun-function' for Python.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
784 Finds end of innermost nested class or method definition."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
785 (let ((orig (point))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
786 (pattern (rx line-start (0+ space) (or "def" "class") space)))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
787 ;; Go to start of current block and check whether it's at top
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
788 ;; level. If it is, and not a block start, look forward for
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
789 ;; definition statement.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
790 (when (python-comment-line-p)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
791 (end-of-line)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
792 (forward-comment most-positive-fixnum))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
793 (if (not (python-open-block-statement-p))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
794 (python-beginning-of-block))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
795 (if (zerop (current-indentation))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
796 (unless (python-open-block-statement-p)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
797 (while (and (re-search-forward pattern nil 'move)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
798 (python-in-string/comment))) ; just loop
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
799 (unless (eobp)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
800 (beginning-of-line)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
801 ;; Don't move before top-level statement that would end defun.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
802 (end-of-line)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
803 (python-beginning-of-defun))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
804 ;; If we got to the start of buffer, look forward for
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
805 ;; definition statement.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
806 (if (and (bobp) (not (looking-at "def\\|class")))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
807 (while (and (not (eobp))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
808 (re-search-forward pattern nil 'move)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
809 (python-in-string/comment)))) ; just loop
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
810 ;; We're at a definition statement (or end-of-buffer).
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
811 (unless (eobp)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
812 (python-end-of-block)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
813 ;; Count trailing space in defun (but not trailing comments).
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
814 (skip-syntax-forward " >")
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
815 (unless (eobp) ; e.g. missing final newline
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
816 (beginning-of-line)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
817 ;; Catch pathological cases like this, where the beginning-of-defun
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
818 ;; skips to a definition we're not in:
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
819 ;; if ...:
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
820 ;; ...
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
821 ;; else:
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
822 ;; ... # point here
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
823 ;; ...
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
824 ;; def ...
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
825 (if (< (point) orig)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
826 (goto-char (point-max)))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
827
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
828 (defun python-beginning-of-statement ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
829 "Go to start of current statement.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
830 Accounts for continuation lines, multi-line strings, and
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
831 multi-line bracketed expressions."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
832 (beginning-of-line)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
833 (python-beginning-of-string)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
834 (while (python-continuation-line-p)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
835 (beginning-of-line)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
836 (if (python-backslash-continuation-line-p)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
837 (progn
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
838 (forward-line -1)
56324
e6bf7376c962 (python-beginning-of-statement): Exit the loop if backward-up-list gets error.
Richard M. Stallman <rms@gnu.org>
parents: 56051
diff changeset
839 (while (python-backslash-continuation-line-p)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
840 (forward-line -1)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
841 (python-beginning-of-string)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
842 (python-skip-out)))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
843 (back-to-indentation))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
844
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
845 (defun python-skip-out (&optional forward syntax)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
846 "Skip out of any nested brackets.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
847 Skip forward if FORWARD is non-nil, else backward.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
848 If SYNTAX is non-nil it is the state returned by `syntax-ppss' at point.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
849 Return non-nil iff skipping was done."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
850 (let ((depth (syntax-ppss-depth (or syntax (syntax-ppss))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
851 (forward (if forward -1 1)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
852 (unless (zerop depth)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
853 (if (> depth 0)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
854 ;; Skip forward out of nested brackets.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
855 (condition-case () ; beware invalid syntax
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
856 (progn (backward-up-list (* forward depth)) t)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
857 (error nil))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
858 ;; Invalid syntax (too many closed brackets).
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
859 ;; Skip out of as many as possible.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
860 (let (done)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
861 (while (condition-case ()
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
862 (progn (backward-up-list forward)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
863 (setq done t))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
864 (error nil)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
865 done)))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
866
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
867 (defun python-end-of-statement ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
868 "Go to the end of the current statement and return point.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
869 Usually this is the start of the next line, but if this is a
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
870 multi-line statement we need to skip over the continuation lines.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
871 On a comment line, go to end of line."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
872 (end-of-line)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
873 (while (let (comment)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
874 ;; Move past any enclosing strings and sexps, or stop if
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
875 ;; we're in a comment.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
876 (while (let ((s (syntax-ppss)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
877 (cond ((eq 'comment (syntax-ppss-context s))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
878 (setq comment t)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
879 nil)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
880 ((eq 'string (syntax-ppss-context s))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
881 ;; Go to start of string and skip it.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
882 (goto-char (nth 8 s))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
883 (condition-case () ; beware invalid syntax
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
884 (progn (forward-sexp) t)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
885 (error (end-of-line))))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
886 ((python-skip-out t s))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
887 (end-of-line))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
888 (unless comment
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
889 (eq ?\\ (char-before)))) ; Line continued?
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
890 (end-of-line 2)) ; Try next line.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
891 (point))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
892
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
893 (defun python-previous-statement (&optional count)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
894 "Go to start of previous statement.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
895 With argument COUNT, do it COUNT times. Stop at beginning of buffer.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
896 Return count of statements left to move."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
897 (interactive "p")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
898 (unless count (setq count 1))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
899 (if (< count 0)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
900 (python-next-statement (- count))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
901 (python-beginning-of-statement)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
902 (while (and (> count 0) (not (bobp)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
903 (python-skip-comments/blanks t)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
904 (python-beginning-of-statement)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
905 (unless (bobp) (setq count (1- count))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
906 count))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
907
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
908 (defun python-next-statement (&optional count)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
909 "Go to start of next statement.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
910 With argument COUNT, do it COUNT times. Stop at end of buffer.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
911 Return count of statements left to move."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
912 (interactive "p")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
913 (unless count (setq count 1))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
914 (if (< count 0)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
915 (python-previous-statement (- count))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
916 (beginning-of-line)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
917 (while (and (> count 0) (not (eobp)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
918 (python-end-of-statement)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
919 (python-skip-comments/blanks)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
920 (unless (eobp)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
921 (setq count (1- count))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
922 count))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
923
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
924 (defun python-beginning-of-block (&optional arg)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
925 "Go to start of current block.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
926 With numeric arg, do it that many times. If ARG is negative, call
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
927 `python-end-of-block' instead.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
928 If point is on the first line of a block, use its outer block.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
929 If current statement is in column zero, don't move and return nil.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
930 Otherwise return non-nil."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
931 (interactive "p")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
932 (unless arg (setq arg 1))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
933 (cond
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
934 ((zerop arg))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
935 ((< arg 0) (python-end-of-block (- arg)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
936 (t
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
937 (let ((point (point)))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
938 (if (or (python-comment-line-p)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
939 (python-blank-line-p))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
940 (python-skip-comments/blanks t))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
941 (python-beginning-of-statement)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
942 (let ((ci (current-indentation)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
943 (if (zerop ci)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
944 (not (goto-char point)) ; return nil
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
945 ;; Look upwards for less indented statement.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
946 (if (catch 'done
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
947 ;;; This is slower than the below.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
948 ;;; (while (zerop (python-previous-statement))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
949 ;;; (when (and (< (current-indentation) ci)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
950 ;;; (python-open-block-statement-p t))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
951 ;;; (beginning-of-line)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
952 ;;; (throw 'done t)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
953 (while (and (zerop (forward-line -1)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
954 (when (and (< (current-indentation) ci)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
955 (not (python-comment-line-p))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
956 ;; Move to beginning to save effort in case
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
957 ;; this is in string.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
958 (progn (python-beginning-of-statement) t)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
959 (python-open-block-statement-p t))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
960 (beginning-of-line)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
961 (throw 'done t)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
962 (not (goto-char point))) ; Failed -- return nil
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
963 (python-beginning-of-block (1- arg)))))))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
964
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
965 (defun python-end-of-block (&optional arg)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
966 "Go to end of current block.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
967 With numeric arg, do it that many times. If ARG is negative,
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
968 call `python-beginning-of-block' instead.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
969 If current statement is in column zero and doesn't open a block,
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
970 don't move and return nil. Otherwise return t."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
971 (interactive "p")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
972 (unless arg (setq arg 1))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
973 (if (< arg 0)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
974 (python-beginning-of-block (- arg))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
975 (while (and (> arg 0)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
976 (let* ((point (point))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
977 (_ (if (python-comment-line-p)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
978 (python-skip-comments/blanks t)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
979 (ci (current-indentation))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
980 (open (python-open-block-statement-p)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
981 (if (and (zerop ci) (not open))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
982 (not (goto-char point))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
983 (catch 'done
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
984 (while (zerop (python-next-statement))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
985 (when (or (and open (<= (current-indentation) ci))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
986 (< (current-indentation) ci))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
987 (python-skip-comments/blanks t)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
988 (beginning-of-line 2)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
989 (throw 'done t)))))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
990 (setq arg (1- arg)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
991 (zerop arg)))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
992
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
993 ;;;; Imenu.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
994
54888
89ed55db7532 (python-orig-start-line, python-orig-file): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54886
diff changeset
995 (defvar python-recursing)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
996 (defun python-imenu-create-index ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
997 "`imenu-create-index-function' for Python.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
998
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
999 Makes nested Imenu menus from nested `class' and `def' statements.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1000 The nested menus are headed by an item referencing the outer
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1001 definition; it has a space prepended to the name so that it sorts
54938
109b2bf180dd (python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54888
diff changeset
1002 first with `imenu--sort-by-name' (though, unfortunately, sub-menus
109b2bf180dd (python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54888
diff changeset
1003 precede it)."
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1004 (unless (boundp 'python-recursing) ; dynamically bound below
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1005 ;; Normal call from Imenu.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1006 (goto-char (point-min))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1007 ;; Without this, we can get an infloop if the buffer isn't all
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1008 ;; fontified. I guess this is really a bug in syntax.el. OTOH,
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1009 ;; _with_ this, imenu doesn't immediately work; I can't figure out
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1010 ;; what's going on, but it must be something to do with timers in
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1011 ;; font-lock.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1012 ;; This can't be right, especially not when jit-lock is not used. --Stef
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1013 ;; (unless (get-text-property (1- (point-max)) 'fontified)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1014 ;; (font-lock-fontify-region (point-min) (point-max)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1015 )
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1016 (let (index-alist) ; accumulated value to return
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1017 (while (re-search-forward
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1018 (rx line-start (0+ space) ; leading space
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1019 (or (group "def") (group "class")) ; type
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1020 (1+ space) (group (1+ (or word ?_)))) ; name
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1021 nil t)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1022 (unless (python-in-string/comment)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1023 (let ((pos (match-beginning 0))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1024 (name (match-string-no-properties 3)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1025 (if (match-beginning 2) ; def or class?
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1026 (setq name (concat "class " name)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1027 (save-restriction
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1028 (narrow-to-defun)
54888
89ed55db7532 (python-orig-start-line, python-orig-file): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54886
diff changeset
1029 (let* ((python-recursing t)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1030 (sublist (python-imenu-create-index)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1031 (if sublist
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1032 (progn (push (cons (concat " " name) pos) sublist)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1033 (push (cons name sublist) index-alist))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1034 (push (cons name pos) index-alist)))))))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1035 (unless (boundp 'python-recursing)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1036 ;; Look for module variables.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1037 (let (vars)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1038 (goto-char (point-min))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1039 (while (re-search-forward
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1040 (rx line-start (group (1+ (or word ?_))) (0+ space) "=")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1041 nil t)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1042 (unless (python-in-string/comment)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1043 (push (cons (match-string 1) (match-beginning 1))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1044 vars)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1045 (setq index-alist (nreverse index-alist))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1046 (if vars
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1047 (push (cons "Module variables"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1048 (nreverse vars))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1049 index-alist))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1050 index-alist))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1051
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1052 ;;;; `Electric' commands.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1053
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1054 (defun python-electric-colon (arg)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1055 "Insert a colon and maybe outdent the line if it is a statement like `else'.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1056 With numeric ARG, just insert that many colons. With \\[universal-argument],
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1057 just insert a single colon."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1058 (interactive "*P")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1059 (self-insert-command (if (not (integerp arg)) 1 arg))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1060 (and (not arg)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1061 (eolp)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1062 (python-outdent-p)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1063 (not (python-in-string/comment))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1064 (> (current-indentation) (python-calculate-indentation))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1065 (python-indent-line))) ; OK, do it
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1066 (put 'python-electric-colon 'delete-selection t)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1067
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1068 (defun python-backspace (arg)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1069 "Maybe delete a level of indentation on the current line.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1070 Do so if point is at the end of the line's indentation.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1071 Otherwise just call `backward-delete-char-untabify'.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1072 Repeat ARG times."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1073 (interactive "*p")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1074 (if (or (/= (current-indentation) (current-column))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1075 (bolp)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1076 (python-continuation-line-p))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1077 (backward-delete-char-untabify arg)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1078 ;; Look for the largest valid indentation which is smaller than
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1079 ;; the current indentation.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1080 (let ((indent 0)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1081 (ci (current-indentation))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1082 (indents (python-indentation-levels))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1083 initial)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1084 (dolist (x indents)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1085 (if (< (car x) ci)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1086 (setq indent (max indent (car x)))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1087 (setq initial (cdr (assq indent indents)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1088 (if (> (length initial) 0)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1089 (message "Closes %s" initial))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1090 (delete-horizontal-space)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1091 (indent-to indent))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1092 (put 'python-backspace 'delete-selection 'supersede)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1093
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1094 ;;;; pychecker
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1095
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1096 (defcustom python-check-command "pychecker --stdlib"
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
1097 "Command used to check a Python file."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1098 :type 'string
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1099 :group 'python)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1100
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1101 (defvar python-saved-check-command nil
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1102 "Internal use.")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1103
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1104 ;; After `sgml-validate-command'.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1105 (defun python-check (command)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1106 "Check a Python file (default current buffer's file).
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1107 Runs COMMAND, a shell command, as if by `compile'.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1108 See `python-check-command' for the default."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1109 (interactive
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1110 (list (read-string "Checker command: "
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1111 (or python-saved-check-command
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1112 (concat python-check-command " "
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1113 (let ((name (buffer-file-name)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1114 (if name
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1115 (file-name-nondirectory name))))))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1116 (setq python-saved-check-command command)
63831
1962e8146bf4 (python-check): Require `compile' before modifying its variables.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 61857
diff changeset
1117 (require 'compile) ;To define compilation-* variables.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1118 (save-some-buffers (not compilation-ask-about-save) nil)
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1119 (let ((compilation-error-regexp-alist
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1120 (cons '("(\\([^,]+\\), line \\([0-9]+\\))" 1 2)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1121 compilation-error-regexp-alist)))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1122 (compilation-start command)))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1123
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1124 ;;;; Inferior mode stuff (following cmuscheme).
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1125
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1126 ;; Fixme: Make sure we can work with IPython.
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1127
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1128 (defcustom python-python-command "python"
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
1129 "Shell command to run Python interpreter.
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1130 Any arguments can't contain whitespace.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1131 Note that IPython may not work properly; it must at least be used
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1132 with the `-cl' flag, i.e. use `ipython -cl'."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1133 :group 'python
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1134 :type 'string)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1135
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1136 (defcustom python-jython-command "jython"
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
1137 "Shell command to run Jython interpreter.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1138 Any arguments can't contain whitespace."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1139 :group 'python
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1140 :type 'string)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1141
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1142 (defvar python-command python-python-command
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1143 "Actual command used to run Python.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1144 May be `python-python-command' or `python-jython-command', possibly
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1145 modified by the user. Additional arguments are added when the command
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1146 is used by `run-python' et al.")
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1147
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1148 (defvar python-buffer nil
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1149 "*The current python process buffer.
54938
109b2bf180dd (python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54888
diff changeset
1150
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1151 Commands that send text from source buffers to Python processes have
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1152 to choose a process to send to. This is determined by buffer-local
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1153 value of `python-buffer'. If its value in the current buffer,
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1154 i.e. both any local value and the default one, is nil, `run-python'
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1155 and commands that send to the Python process will start a new process.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1156
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1157 Whenever \\[run-python] starts a new process, it resets the default
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1158 value of `python-buffer' to be the new process's buffer and sets the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1159 buffer-local value similarly if the current buffer is in Python mode
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1160 or Inferior Python mode, so that source buffer stays associated with a
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1161 specific sub-process.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1162
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1163 Use \\[python-set-proc] to set the default value from a buffer with a
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1164 local value.")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1165 (make-variable-buffer-local 'python-buffer)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1166
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1167 (defconst python-compilation-regexp-alist
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1168 ;; FIXME: maybe these should move to compilation-error-regexp-alist-alist.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1169 ;; The first already is (for CAML), but the second isn't. Anyhow,
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1170 ;; these are specific to the inferior buffer. -- fx
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1171 `((,(rx line-start (1+ (any " \t")) "File \""
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1172 (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1173 "\", line " (group (1+ digit)))
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1174 1 2)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1175 (,(rx " in file " (group (1+ not-newline)) " on line "
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1176 (group (1+ digit)))
55300
2db456741f80 (python-compilation-line-number): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55240
diff changeset
1177 1 2))
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1178 "`compilation-error-regexp-alist' for inferior Python.")
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1179
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1180 (defvar inferior-python-mode-map
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1181 (let ((map (make-sparse-keymap)))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1182 ;; This will inherit from comint-mode-map.
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1183 (define-key map "\C-c\C-l" 'python-load-file)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1184 (define-key map "\C-c\C-v" 'python-check)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1185 ;; Note that we _can_ still use these commands which send to the
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1186 ;; Python process even at the prompt iff we have a normal prompt,
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1187 ;; i.e. '>>> ' and not '... '. See the comment before
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1188 ;; python-send-region. Fixme: uncomment these if we address that.
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1189
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1190 ;; (define-key map [(meta ?\t)] 'python-complete-symbol)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1191 ;; (define-key map "\C-c\C-f" 'python-describe-symbol)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1192 map))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1193
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1194 ;; Fixme: This should inherit some stuff from `python-mode', but I'm
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1195 ;; not sure how much: at least some keybindings, like C-c C-f;
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1196 ;; syntax?; font-locking, e.g. for triple-quoted strings?
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1197 (define-derived-mode inferior-python-mode comint-mode "Inferior Python"
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1198 "Major mode for interacting with an inferior Python process.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1199 A Python process can be started with \\[run-python].
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1200
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1201 Hooks `comint-mode-hook' and `inferior-python-mode-hook' are run in
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1202 that order.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1203
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1204 You can send text to the inferior Python process from other buffers
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1205 containing Python source.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1206 * \\[python-switch-to-python] switches the current buffer to the Python
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1207 process buffer.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1208 * \\[python-send-region] sends the current region to the Python process.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1209 * \\[python-send-region-and-go] switches to the Python process buffer
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1210 after sending the text.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1211 For running multiple processes in multiple buffers, see `run-python' and
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1212 `python-buffer'.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1213
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1214 \\{inferior-python-mode-map}"
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1215 :group 'python
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1216 (set-syntax-table python-mode-syntax-table)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1217 (setq mode-line-process '(":%s"))
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1218 (set (make-local-variable 'comint-input-filter) 'python-input-filter)
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1219 (add-hook 'comint-preoutput-filter-functions #'python-preoutput-filter
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1220 nil t)
54938
109b2bf180dd (python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54888
diff changeset
1221 ;; Still required by `comint-redirect-send-command', for instance
109b2bf180dd (python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54888
diff changeset
1222 ;; (and we need to match things like `>>> ... >>> '):
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1223 (set (make-local-variable 'comint-prompt-regexp)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1224 (rx line-start (1+ (and (repeat 3 (any ">.")) " "))))
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1225 (set (make-local-variable 'compilation-error-regexp-alist)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1226 python-compilation-regexp-alist)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1227 (compilation-shell-minor-mode 1))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1228
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1229 (defcustom inferior-python-filter-regexp "\\`\\s-*\\S-?\\S-?\\s-*\\'"
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
1230 "Input matching this regexp is not saved on the history list.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1231 Default ignores all inputs of 0, 1, or 2 non-blank characters."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1232 :type 'regexp
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1233 :group 'python)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1234
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1235 (defun python-input-filter (str)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1236 "`comint-input-filter' function for inferior Python.
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1237 Don't save anything for STR matching `inferior-python-filter-regexp'."
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1238 (not (string-match inferior-python-filter-regexp str)))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1239
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1240 ;; Fixme: Loses with quoted whitespace.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1241 (defun python-args-to-list (string)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1242 (let ((where (string-match "[ \t]" string)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1243 (cond ((null where) (list string))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1244 ((not (= where 0))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1245 (cons (substring string 0 where)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1246 (python-args-to-list (substring string (+ 1 where)))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1247 (t (let ((pos (string-match "[^ \t]" string)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1248 (if pos (python-args-to-list (substring string pos))))))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1249
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1250 (defvar python-preoutput-result nil
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1251 "Data from last `_emacs_out' line seen by the preoutput filter.")
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1252
61037
758ff54158e2 (python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 60931
diff changeset
1253 (defvar python-preoutput-leftover nil)
72527
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1254 (defvar python-preoutput-skip-next-prompt nil)
61037
758ff54158e2 (python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 60931
diff changeset
1255
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1256 ;; Using this stops us getting lines in the buffer like
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1257 ;; >>> ... ... >>>
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1258 (defun python-preoutput-filter (s)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1259 "`comint-preoutput-filter-functions' function: ignore prompts not at bol."
61037
758ff54158e2 (python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 60931
diff changeset
1260 (when python-preoutput-leftover
758ff54158e2 (python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 60931
diff changeset
1261 (setq s (concat python-preoutput-leftover s))
758ff54158e2 (python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 60931
diff changeset
1262 (setq python-preoutput-leftover nil))
72527
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1263 (let ((start 0)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1264 (res ""))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1265 ;; First process whole lines.
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1266 (while (string-match "\n" s start)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1267 (let ((line (substring s start (setq start (match-end 0)))))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1268 ;; Skip prompt if needed.
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1269 (when (and python-preoutput-skip-next-prompt
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1270 (string-match comint-prompt-regexp line))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1271 (setq python-preoutput-skip-next-prompt nil)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1272 (setq line (substring line (match-end 0))))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1273 ;; Recognize special _emacs_out lines.
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1274 (if (and (string-match "\\`_emacs_out \\(.*\\)\n\\'" line)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1275 (local-variable-p 'python-preoutput-result))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1276 (progn
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1277 (setq python-preoutput-result (match-string 1 line))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1278 (set (make-local-variable 'python-preoutput-skip-next-prompt) t))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1279 (setq res (concat res line)))))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1280 ;; Then process the remaining partial line.
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1281 (unless (zerop start) (setq s (substring s start)))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1282 (cond ((and (string-match comint-prompt-regexp s)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1283 ;; Drop this prompt if it follows an _emacs_out...
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1284 (or python-preoutput-skip-next-prompt
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1285 ;; ... or if it's not gonna be inserted at BOL.
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1286 ;; Maybe we could be more selective here.
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1287 (if (zerop (length res))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1288 (not (bolp))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1289 (string-match res ".\\'"))))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1290 ;; The need for this seems to be system-dependent:
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1291 ;; What is this all about, exactly? --Stef
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1292 ;; (if (and (eq ?. (aref s 0)))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1293 ;; (accept-process-output (get-buffer-process (current-buffer)) 1))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1294 (setq python-preoutput-skip-next-prompt nil)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1295 res)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1296 ((let ((end (min (length "_emacs_out ") (length s))))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1297 (eq t (compare-strings s nil end "_emacs_out " nil end)))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1298 ;; The leftover string is a prefix of _emacs_out so we don't know
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1299 ;; yet whether it's an _emacs_out or something else: wait until we
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1300 ;; get more output so we can resolve this ambiguity.
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1301 (set (make-local-variable 'python-preoutput-leftover) s)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1302 res)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1303 (t (concat res s)))))
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1304
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1305 (autoload 'comint-check-proc "comint")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1306
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1307 ;;;###autoload
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1308 (defun run-python (&optional cmd noshow new)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1309 "Run an inferior Python process, input and output via buffer *Python*.
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1310 CMD is the Python command to run. NOSHOW non-nil means don't show the
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1311 buffer automatically.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1312
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1313 Normally, if there is a process already running in `python-buffer',
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1314 switch to that buffer. Interactively, a prefix arg allows you to edit
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1315 the initial command line (default is `python-command'); `-i' etc. args
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1316 will be added to this as appropriate. A new process is started if:
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1317 one isn't running attached to `python-buffer', or interactively the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1318 default `python-command', or argument NEW is non-nil. See also the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1319 documentation for `python-buffer'.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1320
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1321 Runs the hook `inferior-python-mode-hook' \(after the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1322 `comint-mode-hook' is run). \(Type \\[describe-mode] in the process
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1323 buffer for a list of commands.)"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1324 (interactive (if current-prefix-arg
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1325 (list (read-string "Run Python: " python-command) nil t)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1326 (list python-command)))
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1327 (unless cmd (setq cmd python-python-command))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1328 (setq python-command cmd)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1329 ;; Fixme: Consider making `python-buffer' buffer-local as a buffer
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1330 ;; (not a name) in Python buffers from which `run-python' &c is
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1331 ;; invoked. Would support multiple processes better.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1332 (when (or new (not (comint-check-proc python-buffer)))
72567
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1333 (with-current-buffer
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1334 (let* ((cmdlist (append (python-args-to-list cmd) '("-i")))
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1335 (path (getenv "PYTHONPATH"))
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1336 (process-environment ; to import emacs.py
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1337 (cons (concat "PYTHONPATH=" data-directory
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1338 (if path (concat ":" path)))
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1339 process-environment)))
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1340 (apply 'make-comint-in-buffer "Python"
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1341 (if new (generate-new-buffer "*Python*") "*Python*")
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1342 (car cmdlist) nil (cdr cmdlist)))
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1343 (setq-default python-buffer (current-buffer))
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1344 (setq python-buffer (current-buffer))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1345 (accept-process-output (get-buffer-process python-buffer) 5)
72567
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1346 (inferior-python-mode)
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1347 ;; Load function definitions we need.
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1348 ;; Before the preoutput function was used, this was done via -c in
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1349 ;; cmdlist, but that loses the banner and doesn't run the startup
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1350 ;; file. The code might be inline here, but there's enough that it
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1351 ;; seems worth putting in a separate file, and it's probably cleaner
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1352 ;; to put it in a module.
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1353 ;; Ensure we're at a prompt before doing anything else.
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1354 (python-send-receive "import emacs; print '_emacs_out ()'")))
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
1355 (if (derived-mode-p 'python-mode)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1356 (setq python-buffer (default-value 'python-buffer))) ; buffer-local
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1357 ;; Without this, help output goes into the inferior python buffer if
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1358 ;; the process isn't already running.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1359 (sit-for 1 t) ;Should we use accept-process-output instead? --Stef
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1360 (unless noshow (pop-to-buffer python-buffer t)))
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1361
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1362 ;; Fixme: We typically lose if the inferior isn't in the normal REPL,
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1363 ;; e.g. prompt is `help> '. Probably raise an error if the form of
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1364 ;; the prompt is unexpected. Actually, it needs to be `>>> ', not
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1365 ;; `... ', i.e. we're not inputting a block &c. However, this may not
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1366 ;; be the place to check it, e.g. we might actually want to send
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1367 ;; commands having set up such a state.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1368
55240
780b94f913fe (python-send-command): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55054
diff changeset
1369 (defun python-send-command (command)
72527
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1370 "Like `python-send-string' but resets `compilation-shell-minor-mode'.
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1371 COMMAND should be a single statement."
72567
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1372 ;; (assert (not (string-match "\n" command)))
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1373 ;; (let ((end (marker-position (process-mark (python-proc)))))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1374 (with-current-buffer python-buffer (goto-char (point-max)))
55240
780b94f913fe (python-send-command): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55054
diff changeset
1375 (compilation-forget-errors)
72566
41f22ed02e75 (python-send-command): Don't wait for the command
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72535
diff changeset
1376 (python-send-string command)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1377 (with-current-buffer python-buffer
72566
41f22ed02e75 (python-send-command): Don't wait for the command
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72535
diff changeset
1378 (setq compilation-last-buffer (current-buffer)))
41f22ed02e75 (python-send-command): Don't wait for the command
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72535
diff changeset
1379 ;; No idea what this is for but it breaks the call to
41f22ed02e75 (python-send-command): Don't wait for the command
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72535
diff changeset
1380 ;; compilation-fake-loc in python-send-region. -- Stef
41f22ed02e75 (python-send-command): Don't wait for the command
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72535
diff changeset
1381 ;; Must wait until this has completed before re-setting variables below.
41f22ed02e75 (python-send-command): Don't wait for the command
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72535
diff changeset
1382 ;; (python-send-receive "print '_emacs_out ()'")
41f22ed02e75 (python-send-command): Don't wait for the command
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72535
diff changeset
1383 ;; (with-current-buffer python-buffer
41f22ed02e75 (python-send-command): Don't wait for the command
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72535
diff changeset
1384 ;; (set-marker compilation-parsing-end end))
72567
b562a18db094 (python-send-command): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72566
diff changeset
1385 ) ;;)
55240
780b94f913fe (python-send-command): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55054
diff changeset
1386
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1387 (defun python-send-region (start end)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1388 "Send the region to the inferior Python process."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1389 ;; The region is evaluated from a temporary file. This avoids
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1390 ;; problems with blank lines, which have different semantics
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1391 ;; interactively and in files. It also saves the inferior process
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1392 ;; buffer filling up with interpreter prompts. We need a Python
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1393 ;; function to remove the temporary file when it has been evaluated
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1394 ;; (though we could probably do it in Lisp with a Comint output
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1395 ;; filter). This function also catches exceptions and truncates
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1396 ;; tracebacks not to mention the frame of the function itself.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1397 ;;
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1398 ;; The `compilation-shell-minor-mode' parsing takes care of relating
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1399 ;; the reference to the temporary file to the source.
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1400 ;;
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1401 ;; Fixme: Write a `coding' header to the temp file if the region is
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1402 ;; non-ASCII.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1403 (interactive "r")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1404 (let* ((f (make-temp-file "py"))
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1405 (command (format "emacs.eexecfile(%S)" f))
54888
89ed55db7532 (python-orig-start-line, python-orig-file): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54886
diff changeset
1406 (orig-start (copy-marker start)))
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1407 (when (save-excursion
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1408 (goto-char start)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1409 (/= 0 (current-indentation))) ; need dummy block
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1410 (save-excursion
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1411 (goto-char orig-start)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1412 ;; Wrong if we had indented code at buffer start.
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1413 (set-marker orig-start (line-beginning-position 0)))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1414 (write-region "if True:\n" nil f nil 'nomsg))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1415 (write-region start end f t 'nomsg)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1416 (python-send-command command)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1417 (with-current-buffer (process-buffer (python-proc))
58415
86ead4686506 (run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 58358
diff changeset
1418 ;; Tell compile.el to redirect error locations in file `f' to
86ead4686506 (run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 58358
diff changeset
1419 ;; positions past marker `orig-start'. It has to be done *after*
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1420 ;; `python-send-command''s call to `compilation-forget-errors'.
58415
86ead4686506 (run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 58358
diff changeset
1421 (compilation-fake-loc orig-start f))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1422
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1423 (defun python-send-string (string)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1424 "Evaluate STRING in inferior Python process."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1425 (interactive "sPython command: ")
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1426 (comint-send-string (python-proc) string)
72527
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1427 (comint-send-string (python-proc)
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1428 ;; If the string is single-line or if it ends with \n,
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1429 ;; only add a single \n, otherwise add 2, so as to
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1430 ;; make sure we terminate the multiline instruction.
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1431 (if (string-match "\n.+\\'" string) "\n\n" "\n")))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1432
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1433 (defun python-send-buffer ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1434 "Send the current buffer to the inferior Python process."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1435 (interactive)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1436 (python-send-region (point-min) (point-max)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1437
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1438 ;; Fixme: Try to define the function or class within the relevant
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1439 ;; module, not just at top level.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1440 (defun python-send-defun ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1441 "Send the current defun (class or method) to the inferior Python process."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1442 (interactive)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1443 (save-excursion (python-send-region (progn (beginning-of-defun) (point))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1444 (progn (end-of-defun) (point)))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1445
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1446 (defun python-switch-to-python (eob-p)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1447 "Switch to the Python process buffer, maybe starting new process.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1448 With prefix arg, position cursor at end of buffer."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1449 (interactive "P")
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1450 (pop-to-buffer (process-buffer (python-proc)) t) ;Runs python if needed.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1451 (when eob-p
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1452 (push-mark)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1453 (goto-char (point-max))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1454
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1455 (defun python-send-region-and-go (start end)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1456 "Send the region to the inferior Python process.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1457 Then switch to the process buffer."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1458 (interactive "r")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1459 (python-send-region start end)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1460 (python-switch-to-python t))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1461
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1462 (defcustom python-source-modes '(python-mode jython-mode)
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
1463 "Used to determine if a buffer contains Python source code.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1464 If a file is loaded into a buffer that is in one of these major modes,
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1465 it is considered Python source by `python-load-file', which uses the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1466 value to determine defaults."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1467 :type '(repeat function)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1468 :group 'python)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1469
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1470 (defvar python-prev-dir/file nil
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1471 "Caches (directory . file) pair used in the last `python-load-file' command.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1472 Used for determining the default in the next one.")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1473
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1474 (autoload 'comint-get-source "comint")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1475
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1476 (defun python-load-file (file-name)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1477 "Load a Python file FILE-NAME into the inferior Python process.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1478 If the file has extension `.py' import or reload it as a module.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1479 Treating it as a module keeps the global namespace clean, provides
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1480 function location information for debugging, and supports users of
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1481 module-qualified names."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1482 (interactive (comint-get-source "Load Python file: " python-prev-dir/file
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1483 python-source-modes
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1484 t)) ; because execfile needs exact name
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1485 (comint-check-source file-name) ; Check to see if buffer needs saving.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1486 (setq python-prev-dir/file (cons (file-name-directory file-name)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1487 (file-name-nondirectory file-name)))
58415
86ead4686506 (run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 58358
diff changeset
1488 (with-current-buffer (process-buffer (python-proc)) ;Runs python if needed.
86ead4686506 (run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 58358
diff changeset
1489 ;; Fixme: I'm not convinced by this logic from python-mode.el.
86ead4686506 (run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 58358
diff changeset
1490 (python-send-command
86ead4686506 (run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 58358
diff changeset
1491 (if (string-match "\\.py\\'" file-name)
86ead4686506 (run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 58358
diff changeset
1492 (let ((module (file-name-sans-extension
86ead4686506 (run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 58358
diff changeset
1493 (file-name-nondirectory file-name))))
86ead4686506 (run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 58358
diff changeset
1494 (format "emacs.eimport(%S,%S)"
86ead4686506 (run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 58358
diff changeset
1495 module (file-name-directory file-name)))
86ead4686506 (run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 58358
diff changeset
1496 (format "execfile(%S)" file-name)))
86ead4686506 (run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 58358
diff changeset
1497 (message "%s loaded" file-name)))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1498
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1499 (defun python-proc ()
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1500 "Return the current Python process.
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1501 See variable `python-buffer'. Starts a new process if necessary."
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1502 ;; Fixme: Maybe should look for another active process if there
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1503 ;; isn't one for `python-buffer'.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1504 (unless (comint-check-proc python-buffer)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1505 (run-python nil t))
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
1506 (get-buffer-process (or (if (derived-mode-p 'inferior-python-mode)
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
1507 (current-buffer)
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
1508 python-buffer))))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1509
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1510 (defun python-set-proc ()
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1511 "Set the default value of `python-buffer' to correspond to this buffer.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1512 If the current buffer has a local value of `python-buffer', set the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1513 default (global) value to that. The associated Python process is
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1514 the one that gets input from \\[python-send-region] et al when used
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1515 in a buffer that doesn't have a local value of `python-buffer'."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1516 (interactive)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1517 (if (local-variable-p 'python-buffer)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1518 (setq-default python-buffer python-buffer)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1519 (error "No local value of `python-buffer'")))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1520
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1521 ;;;; Context-sensitive help.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1522
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1523 (defconst python-dotty-syntax-table
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1524 (let ((table (make-syntax-table)))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1525 (set-char-table-parent table python-mode-syntax-table)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1526 (modify-syntax-entry ?. "_" table)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1527 table)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1528 "Syntax table giving `.' symbol syntax.
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1529 Otherwise inherits from `python-mode-syntax-table'.")
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1530
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1531 (defvar view-return-to-alist)
55511
5dbde1bf6cad (help-buffer): Autoload when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55495
diff changeset
1532 (eval-when-compile (autoload 'help-buffer "help-fns"))
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1533
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1534 (defvar python-imports) ; forward declaration
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1535
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1536 ;; Fixme: Should this actually be used instead of info-look, i.e. be
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1537 ;; bound to C-h S? [Probably not, since info-look may work in cases
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1538 ;; where this doesn't.]
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1539 (defun python-describe-symbol (symbol)
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1540 "Get help on SYMBOL using `help'.
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1541 Interactively, prompt for symbol.
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1542
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1543 Symbol may be anything recognized by the interpreter's `help'
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1544 command -- e.g. `CALLS' -- not just variables in scope in the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1545 interpreter. This only works for Python version 2.2 or newer
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1546 since earlier interpreters don't support `help'.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1547
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1548 In some cases where this doesn't find documentation, \\[info-lookup-symbol]
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1549 will."
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1550 ;; Note that we do this in the inferior process, not a separate one, to
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1551 ;; ensure the environment is appropriate.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1552 (interactive
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1553 (let ((symbol (with-syntax-table python-dotty-syntax-table
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1554 (current-word)))
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1555 (enable-recursive-minibuffers t))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1556 (list (read-string (if symbol
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1557 (format "Describe symbol (default %s): " symbol)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1558 "Describe symbol: ")
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1559 nil nil symbol))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1560 (if (equal symbol "") (error "No symbol"))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1561 ;; Ensure we have a suitable help buffer.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1562 ;; Fixme: Maybe process `Related help topics' a la help xrefs and
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1563 ;; allow C-c C-f in help buffer.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1564 (let ((temp-buffer-show-hook ; avoid xref stuff
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1565 (lambda ()
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1566 (toggle-read-only 1)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1567 (setq view-return-to-alist
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1568 (list (cons (selected-window) help-return-method))))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1569 (with-output-to-temp-buffer (help-buffer)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1570 (with-current-buffer standard-output
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1571 ;; Fixme: Is this actually useful?
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1572 (help-setup-xref (list 'python-describe-symbol symbol) (interactive-p))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1573 (set (make-local-variable 'comint-redirect-subvert-readonly) t)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1574 (print-help-return-message))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1575 (comint-redirect-send-command-to-process (format "emacs.ehelp(%S, %s)"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1576 symbol python-imports)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1577 "*Help*" (python-proc) nil nil))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1578
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1579 (add-to-list 'debug-ignored-errors "^No symbol")
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1580
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1581 (defun python-send-receive (string)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1582 "Send STRING to inferior Python (if any) and return result.
72527
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
1583 The result is what follows `_emacs_out' in the output."
72535
e7ed98d0f919 (python-send-receive): Wait in the process's buffer so as to check the right
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72527
diff changeset
1584 (python-send-string string)
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1585 (let ((proc (python-proc)))
72535
e7ed98d0f919 (python-send-receive): Wait in the process's buffer so as to check the right
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72527
diff changeset
1586 (with-current-buffer (process-buffer proc)
e7ed98d0f919 (python-send-receive): Wait in the process's buffer so as to check the right
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72527
diff changeset
1587 (set (make-local-variable 'python-preoutput-result) nil)
e7ed98d0f919 (python-send-receive): Wait in the process's buffer so as to check the right
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72527
diff changeset
1588 (while (progn
e7ed98d0f919 (python-send-receive): Wait in the process's buffer so as to check the right
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72527
diff changeset
1589 (accept-process-output proc 5)
e7ed98d0f919 (python-send-receive): Wait in the process's buffer so as to check the right
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72527
diff changeset
1590 (null python-preoutput-result)))
e7ed98d0f919 (python-send-receive): Wait in the process's buffer so as to check the right
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72527
diff changeset
1591 (prog1 python-preoutput-result
e7ed98d0f919 (python-send-receive): Wait in the process's buffer so as to check the right
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72527
diff changeset
1592 (kill-local-variable 'python-preoutput-result)))))
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1593
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1594 ;; Fixme: Is there anything reasonable we can do with random methods?
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1595 ;; (Currently only works with functions.)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1596 (defun python-eldoc-function ()
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1597 "`eldoc-print-current-symbol-info' for Python.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1598 Only works when point is in a function name, not its arg list, for
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1599 instance. Assumes an inferior Python is running."
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1600 (let ((symbol (with-syntax-table python-dotty-syntax-table
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1601 (current-word))))
72578
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1602 ;; This is run from timers, so inhibit-quit tends to be set.
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1603 (with-local-quit
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1604 ;; First try the symbol we're on.
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1605 (or (and symbol
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1606 (python-send-receive (format "emacs.eargs(%S, %s)"
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1607 symbol python-imports)))
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1608 ;; Try moving to symbol before enclosing parens.
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1609 (let ((s (syntax-ppss)))
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1610 (unless (zerop (car s))
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1611 (when (eq ?\( (char-after (nth 1 s)))
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1612 (save-excursion
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1613 (goto-char (nth 1 s))
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1614 (skip-syntax-backward "-")
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1615 (let ((point (point)))
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1616 (skip-chars-backward "a-zA-Z._")
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1617 (if (< (point) point)
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1618 (python-send-receive
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1619 (format "emacs.eargs(%S, %s)"
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1620 (buffer-substring-no-properties (point) point)
a9874901ba07 (python-eldoc-function): Re-enable quit while waiting for process.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72567
diff changeset
1621 python-imports))))))))))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1622
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1623 ;;;; Info-look functionality.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1624
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1625 (defun python-after-info-look ()
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1626 "Set up info-look for Python.
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1627 Used with `eval-after-load'."
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1628 (let* ((version (let ((s (shell-command-to-string (concat python-command
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1629 " -V"))))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1630 (string-match "^Python \\([0-9]+\\.[0-9]+\\>\\)" s)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1631 (match-string 1 s)))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1632 ;; Whether info files have a Python version suffix, e.g. in Debian.
55447
f1b7359315f0 (python-describe-symbol): Pass INTERACTIVE-P argument to `help-setup-xref'.
Juanma Barranquero <lekktu@gmail.com>
parents: 55397
diff changeset
1633 (versioned
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1634 (with-temp-buffer
54938
109b2bf180dd (python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54888
diff changeset
1635 (with-no-warnings (Info-mode))
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1636 (condition-case ()
54888
89ed55db7532 (python-orig-start-line, python-orig-file): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54886
diff changeset
1637 ;; Don't use `info' because it would pop-up a *info* buffer.
54938
109b2bf180dd (python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54888
diff changeset
1638 (with-no-warnings
109b2bf180dd (python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54888
diff changeset
1639 (Info-goto-node (format "(python%s-lib)Miscellaneous Index"
55511
5dbde1bf6cad (help-buffer): Autoload when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55495
diff changeset
1640 version))
5dbde1bf6cad (help-buffer): Autoload when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55495
diff changeset
1641 t)
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1642 (error nil)))))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1643 (info-lookup-maybe-add-help
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1644 :mode 'python-mode
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1645 :regexp "[[:alnum:]_]+"
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1646 :doc-spec
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1647 ;; Fixme: Can this reasonably be made specific to indices with
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1648 ;; different rules? Is the order of indices optimal?
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1649 ;; (Miscellaneous in -ref first prefers lookup of keywords, for
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1650 ;; instance.)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1651 (if versioned
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1652 ;; The empty prefix just gets us highlighted terms.
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1653 `((,(concat "(python" version "-ref)Miscellaneous Index") nil "")
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1654 (,(concat "(python" version "-ref)Module Index" nil ""))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1655 (,(concat "(python" version "-ref)Function-Method-Variable Index"
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1656 nil ""))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1657 (,(concat "(python" version "-ref)Class-Exception-Object Index"
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1658 nil ""))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1659 (,(concat "(python" version "-lib)Module Index" nil ""))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1660 (,(concat "(python" version "-lib)Class-Exception-Object Index"
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1661 nil ""))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1662 (,(concat "(python" version "-lib)Function-Method-Variable Index"
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1663 nil ""))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1664 (,(concat "(python" version "-lib)Miscellaneous Index" nil "")))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1665 '(("(python-ref)Miscellaneous Index" nil "")
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1666 ("(python-ref)Module Index" nil "")
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1667 ("(python-ref)Function-Method-Variable Index" nil "")
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1668 ("(python-ref)Class-Exception-Object Index" nil "")
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1669 ("(python-lib)Module Index" nil "")
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1670 ("(python-lib)Class-Exception-Object Index" nil "")
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1671 ("(python-lib)Function-Method-Variable Index" nil "")
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1672 ("(python-lib)Miscellaneous Index" nil ""))))))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1673 (eval-after-load "info-look" '(python-after-info-look))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1674
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1675 ;;;; Miscellany.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1676
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1677 (defcustom python-jython-packages '("java" "javax" "org" "com")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1678 "Packages implying `jython-mode'.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1679 If these are imported near the beginning of the buffer, `python-mode'
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1680 actually punts to `jython-mode'."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1681 :type '(repeat string)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1682 :group 'python)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1683
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1684 ;; Called from `python-mode', this causes a recursive call of the
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1685 ;; mode. See logic there to break out of the recursion.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1686 (defun python-maybe-jython ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1687 "Invoke `jython-mode' if the buffer appears to contain Jython code.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1688 The criterion is either a match for `jython-mode' via
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1689 `interpreter-mode-alist' or an import of a module from the list
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1690 `python-jython-packages'."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1691 ;; The logic is taken from python-mode.el.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1692 (save-excursion
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1693 (save-restriction
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1694 (widen)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1695 (goto-char (point-min))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1696 (let ((interpreter (if (looking-at auto-mode-interpreter-regexp)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1697 (match-string 2))))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1698 (if (and interpreter (eq 'jython-mode
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1699 (cdr (assoc (file-name-nondirectory
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1700 interpreter)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1701 interpreter-mode-alist))))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1702 (jython-mode)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1703 (if (catch 'done
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1704 (while (re-search-forward
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1705 (rx line-start (or "import" "from") (1+ space)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1706 (group (1+ (not (any " \t\n.")))))
55054
ee7e5daa7ffd (python-maybe-jython): Don't assume point-min==1.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54943
diff changeset
1707 (+ (point-min) 10000) ; Probably not worth customizing.
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1708 t)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1709 (if (member (match-string 1) python-jython-packages)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1710 (throw 'done t))))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1711 (jython-mode)))))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1712
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1713 (defun python-fill-paragraph (&optional justify)
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1714 "`fill-paragraph-function' handling comments and multi-line strings.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1715 If any of the current line is a comment, fill the comment or the
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1716 paragraph of it that point is in, preserving the comment's
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1717 indentation and initial comment characters. Similarly if the end
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1718 of the current line is in or at the end of a multi-line string.
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1719 Otherwise, do nothing."
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1720 (interactive "P")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1721 (or (fill-comment-paragraph justify)
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1722 ;; The `paragraph-start' and `paragraph-separate' variables
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1723 ;; don't allow us to delimit the last paragraph in a multi-line
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1724 ;; string properly, so narrow to the string and then fill around
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1725 ;; (the end of) the current line.
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1726 (save-excursion
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1727 (end-of-line)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1728 (let* ((syntax (syntax-ppss))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1729 (orig (point))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1730 (start (nth 8 syntax))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1731 end)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1732 (cond ((eq t (nth 3 syntax)) ; in fenced string
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1733 (goto-char (nth 8 syntax)) ; string start
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1734 (condition-case () ; for unbalanced quotes
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1735 (progn (forward-sexp)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1736 (setq end (point)))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1737 (error (setq end (point-max)))))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1738 ((re-search-backward "\\s|\\s-*\\=" nil t) ; end of fenced
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1739 ; string
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1740 (forward-char)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1741 (setq end (point))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1742 (condition-case ()
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1743 (progn (backward-sexp)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1744 (setq start (point)))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1745 (error nil))))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1746 (when end
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1747 (save-restriction
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1748 (narrow-to-region start end)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1749 (goto-char orig)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1750 (fill-paragraph justify))))))
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1751 t)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1752
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1753 (defun python-shift-left (start end &optional count)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1754 "Shift lines in region COUNT (the prefix arg) columns to the left.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1755 COUNT defaults to `python-indent'. If region isn't active, just shift
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1756 current line. The region shifted includes the lines in which START and
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1757 END lie. It is an error if any lines in the region are indented less than
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1758 COUNT columns."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1759 (interactive (if mark-active
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1760 (list (region-beginning) (region-end) current-prefix-arg)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1761 (list (point) (point) current-prefix-arg)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1762 (if count
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1763 (setq count (prefix-numeric-value count))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1764 (setq count python-indent))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1765 (when (> count 0)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1766 (save-excursion
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1767 (goto-char start)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1768 (while (< (point) end)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1769 (if (and (< (current-indentation) count)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1770 (not (looking-at "[ \t]*$")))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1771 (error "Can't shift all lines enough"))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1772 (forward-line))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1773 (indent-rigidly start end (- count)))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1774
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1775 (add-to-list 'debug-ignored-errors "^Can't shift all lines enough")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1776
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1777 (defun python-shift-right (start end &optional count)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1778 "Shift lines in region COUNT (the prefix arg) columns to the right.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1779 COUNT defaults to `python-indent'. If region isn't active, just shift
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1780 current line. The region shifted includes the lines in which START and
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1781 END lie."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1782 (interactive (if mark-active
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1783 (list (region-beginning) (region-end) current-prefix-arg)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1784 (list (point) (point) current-prefix-arg)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1785 (if count
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1786 (setq count (prefix-numeric-value count))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1787 (setq count python-indent))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1788 (indent-rigidly start end count))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1790 (defun python-outline-level ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1791 "`outline-level' function for Python mode.
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1792 The level is the number of `python-indent' steps of indentation
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
1793 of current line."
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1794 (1+ (/ (current-indentation) python-indent)))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1795
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1796 ;; Fixme: Consider top-level assignments, imports, &c.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1797 (defun python-current-defun ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1798 "`add-log-current-defun-function' for Python."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1799 (save-excursion
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1800 ;; Move up the tree of nested `class' and `def' blocks until we
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1801 ;; get to zero indentation, accumulating the defined names.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1802 (let ((start t)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1803 accum)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1804 (while (or start (> (current-indentation) 0))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1805 (setq start nil)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1806 (python-beginning-of-block)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1807 (end-of-line)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1808 (beginning-of-defun)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1809 (if (looking-at (rx (0+ space) (or "def" "class") (1+ space)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1810 (group (1+ (or word (syntax symbol))))))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1811 (push (match-string 1) accum)))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1812 (if accum (mapconcat 'identity accum ".")))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1813
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1814 (defun python-mark-block ()
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1815 "Mark the block around point.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1816 Uses `python-beginning-of-block', `python-end-of-block'."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1817 (interactive)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1818 (push-mark)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1819 (python-beginning-of-block)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1820 (push-mark (point) nil t)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1821 (python-end-of-block)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1822 (exchange-point-and-mark))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1823
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1824 ;; Fixme: Provide a find-function-like command to find source of a
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1825 ;; definition (separate from BicycleRepairMan). Complicated by
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1826 ;; finding the right qualified name.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1827
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1828 ;;;; Completion.
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1829
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1830 (defvar python-imports nil
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1831 "String of top-level import statements updated by `python-find-imports'.")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1832 (make-variable-buffer-local 'python-imports)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1833
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1834 ;; Fixme: Should font-lock try to run this when it deals with an import?
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1835 ;; Maybe not a good idea if it gets run multiple times when the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1836 ;; statement is being edited, and is more likely to end up with
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1837 ;; something syntactically incorrect.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1838 ;; However, what we should do is to trundle up the block tree from point
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1839 ;; to extract imports that appear to be in scope, and add those.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1840 (defun python-find-imports ()
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1841 "Find top-level imports, updating `python-imports'."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1842 (interactive)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1843 (save-excursion
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1844 (let (lines)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1845 (goto-char (point-min))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1846 (while (re-search-forward "^import\\>\\|^from\\>" nil t)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1847 (unless (syntax-ppss-context (syntax-ppss))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1848 (push (buffer-substring (line-beginning-position)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1849 (line-beginning-position 2))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1850 lines)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1851 (setq python-imports
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1852 (if lines
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1853 (apply #'concat
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1854 ;; This is probably best left out since you're unlikely to need the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1855 ;; doc for a function in the buffer and the import will lose if the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1856 ;; Python sub-process' working directory isn't the same as the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1857 ;; buffer's.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1858 ;; (if buffer-file-name
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1859 ;; (concat
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1860 ;; "import "
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1861 ;; (file-name-sans-extension
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1862 ;; (file-name-nondirectory buffer-file-name))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1863 (nreverse lines))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1864 "None"))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1865 (when lines
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1866 (set-text-properties 0 (length python-imports) nil python-imports)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1867 ;; The output ends up in the wrong place if the string we
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1868 ;; send contains newlines (from the imports).
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1869 (setq python-imports
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1870 (replace-regexp-in-string "\n" "\\n"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1871 (format "%S" python-imports) t t))))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1872
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1873 ;; Fixme: This fails the first time if the sub-process isn't already
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1874 ;; running. Presumably a timing issue with i/o to the process.
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1875 (defun python-symbol-completions (symbol)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1876 "Return a list of completions of the string SYMBOL from Python process.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1877 The list is sorted.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1878 Uses `python-imports' to load modules against which to complete."
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1879 (when symbol
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1880 (let ((completions
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1881 (condition-case ()
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1882 (car (read-from-string
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1883 (python-send-receive
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1884 (format "emacs.complete(%S,%s)" symbol python-imports))))
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1885 (error nil))))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1886 (sort
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1887 ;; We can get duplicates from the above -- don't know why.
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1888 (delete-dups completions)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1889 #'string<))))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1890
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1891 (defun python-partial-symbol ()
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1892 "Return the partial symbol before point (for completion)."
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1893 (let ((end (point))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1894 (start (save-excursion
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1895 (and (re-search-backward
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1896 (rx (or buffer-start (regexp "[^[:alnum:]._]"))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1897 (group (1+ (regexp "[[:alnum:]._]"))) point)
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1898 nil t)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1899 (match-beginning 1)))))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1900 (if start (buffer-substring-no-properties start end))))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1901
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1902 (defun python-complete-symbol ()
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1903 "Perform completion on the Python symbol preceding point.
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1904 Repeating the command scrolls the completion window."
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1905 (interactive)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1906 (let ((window (get-buffer-window "*Completions*")))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1907 (if (and (eq last-command this-command)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1908 window (window-live-p window) (window-buffer window)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1909 (buffer-name (window-buffer window)))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1910 (with-current-buffer (window-buffer window)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1911 (if (pos-visible-in-window-p (point-max) window)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1912 (set-window-start window (point-min))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1913 (save-selected-window
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1914 (select-window window)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1915 (scroll-up))))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1916 ;; Do completion.
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1917 (let* ((end (point))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1918 (symbol (python-partial-symbol))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1919 (completions (python-symbol-completions symbol))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1920 (completion (if completions
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1921 (try-completion symbol completions))))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1922 (when symbol
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1923 (cond ((eq completion t))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1924 ((null completion)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1925 (message "Can't find completion for \"%s\"" symbol)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1926 (ding))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1927 ((not (string= symbol completion))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1928 (delete-region (- end (length symbol)) end)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1929 (insert completion))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1930 (t
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1931 (message "Making completion list...")
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1932 (with-output-to-temp-buffer "*Completions*"
66178
6c46e0e1ba7b Install to the CVS repository what I forgot to install in my
Masatake YAMATO <jet@gyve.org>
parents: 65672
diff changeset
1933 (display-completion-list completions symbol))
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1934 (message "Making completion list...%s" "done"))))))))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1935
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1936 (defun python-try-complete (old)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1937 "Completion function for Python for use with `hippie-expand'."
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
1938 (when (derived-mode-p 'python-mode) ; though we only add it locally
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1939 (unless old
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1940 (let ((symbol (python-partial-symbol)))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1941 (he-init-string (- (point) (length symbol)) (point))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1942 (if (not (he-string-member he-search-string he-tried-table))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1943 (push he-search-string he-tried-table))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1944 (setq he-expand-list
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1945 (and symbol (python-symbol-completions symbol)))))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1946 (while (and he-expand-list
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1947 (he-string-member (car he-expand-list) he-tried-table))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1948 (pop he-expand-list))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1949 (if he-expand-list
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1950 (progn
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1951 (he-substitute-string (pop he-expand-list))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1952 t)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1953 (if old (he-reset-string))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1954 nil)))
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
1955
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1956 ;;;; FFAP support
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1957
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1958 (defun python-module-path (module)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1959 "Function for `ffap-alist' to return path to MODULE."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1960 (python-send-receive (format "emacs.modpath (%S)" module)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1961
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1962 (eval-after-load "ffap"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1963 '(push '(python-mode . python-module-path) ffap-alist))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1964
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1965 ;;;; Skeletons
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1966
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1967 (defvar python-skeletons nil
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1968 "Alist of named skeletons for Python mode.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1969 Elements are of the form (NAME . EXPANDER-FUNCTION).")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1970
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1971 (defvar python-mode-abbrev-table nil
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1972 "Abbrev table for Python mode.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1973 The default contents correspond to the elements of `python-skeletons'.")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1974 (define-abbrev-table 'python-mode-abbrev-table ())
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1975
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1976 (eval-when-compile
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1977 ;; Define a user-level skeleton and add it to `python-skeletons' and
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1978 ;; the abbrev table.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1979 (defmacro def-python-skeleton (name &rest elements)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1980 (let* ((name (symbol-name name))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1981 (function (intern (concat "python-insert-" name))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1982 `(progn
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1983 (add-to-list 'python-skeletons ',(cons name function))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1984 (define-abbrev python-mode-abbrev-table ,name "" ',function nil t)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1985 (define-skeleton ,function
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1986 ,(format "Insert Python \"%s\" template." name)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1987 ,@elements)))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1988 (put 'def-python-skeleton 'lisp-indent-function 2)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1989
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1990 ;; From `skeleton-further-elements':
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1991 ;; `<': outdent a level;
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1992 ;; `^': delete indentation on current line and also previous newline.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1993 ;; Not quote like `delete-indentation'. Assumes point is at
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1994 ;; beginning of indentation.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1995
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1996 (def-python-skeleton if
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1997 "Condition: "
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1998 "if " str ":" \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
1999 > _ \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2000 ("other condition, %s: "
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2001 < ; Avoid wrong indentation after block opening.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2002 "elif " str ":" \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2003 > _ \n nil)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2004 (python-else) | ^)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2005
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2006 (define-skeleton python-else
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2007 "Auxiliary skeleton."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2008 nil
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2009 (unless (eq ?y (read-char "Add `else' clause? (y for yes or RET for no) "))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2010 (signal 'quit t))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2011 < "else:" \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2012 > _ \n)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2013
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2014 (def-python-skeleton while
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2015 "Condition: "
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2016 "while " str ":" \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2017 > _ \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2018 (python-else) | ^)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2019
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2020 (def-python-skeleton for
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2021 "Target, %s: "
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2022 "for " str " in " (skeleton-read "Expression, %s: ") ":" \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2023 > _ \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2024 (python-else) | ^)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2025
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2026 (def-python-skeleton try/except
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2027 nil
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2028 "try:" \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2029 > _ \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2030 ("Exception, %s: "
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2031 < "except " str (python-target) ":" \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2032 > _ \n nil)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2033 < "except:" \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2034 > _ \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2035 (python-else) | ^)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2036
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2037 (define-skeleton python-target
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2038 "Auxiliary skeleton."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2039 "Target, %s: " ", " str | -2)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2040
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2041 (def-python-skeleton try/finally
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2042 nil
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2043 "try:" \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2044 > _ \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2045 < "finally:" \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2046 > _ \n)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2047
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2048 (def-python-skeleton def
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2049 "Name: "
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2050 "def " str " (" ("Parameter, %s: " (unless (equal ?\( (char-before)) ", ")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2051 str) "):" \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2052 "\"\"\"" @ " \"\"\"" \n ; Fixme: syntaxification wrong for """"""
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2053 > _ \n)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2054
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2055 (def-python-skeleton class
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2056 "Name: "
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2057 "class " str " (" ("Inheritance, %s: "
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2058 (unless (equal ?\( (char-before)) ", ")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2059 str)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2060 & ")" | -2 ; close list or remove opening
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2061 ":" \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2062 "\"\"\"" @ " \"\"\"" \n
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2063 > _ \n)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2064
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2065 (defvar python-default-template "if"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2066 "Default template to expand by `python-insert-template'.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2067 Updated on each expansion.")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2068
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2069 (defun python-expand-template (name)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2070 "Expand template named NAME.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2071 Interactively, prompt for the name with completion."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2072 (interactive
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2073 (list (completing-read (format "Template to expand (default %s): "
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2074 python-default-template)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2075 python-skeletons nil t)))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2076 (if (equal "" name)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2077 (setq name python-default-template)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2078 (setq python-default-template name))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2079 (let ((func (cdr (assoc name python-skeletons))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2080 (if func
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2081 (funcall func)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2082 (error "Undefined template: %s" name))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2083
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2084 ;;;; Bicycle Repair Man support
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2085
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2086 (autoload 'pymacs-load "pymacs" nil t)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2087 (autoload 'brm-init "bikemacs")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2088
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2089 ;; I'm not sure how useful BRM really is, and it's certainly dangerous
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2090 ;; the way it modifies files outside Emacs... Also note that the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2091 ;; current BRM loses with tabs used for indentation -- I submitted a
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2092 ;; fix <URL:http://www.loveshack.ukfsn.org/emacs/bikeemacs.py.diff>.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2093 (defun python-setup-brm ()
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2094 "Set up Bicycle Repair Man refactoring tool (if available).
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2095
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2096 Note that the `refactoring' features change files independently of
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2097 Emacs and may modify and save the contents of the current buffer
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2098 without confirmation."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2099 (interactive)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2100 (condition-case data
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2101 (unless (fboundp 'brm-rename)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2102 (pymacs-load "bikeemacs" "brm-") ; first line of normal recipe
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2103 (let ((py-mode-map (make-sparse-keymap)) ; it assumes this
72527
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2104 (features (cons 'python-mode features))) ; and requires this
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2105 (brm-init)) ; second line of normal recipe
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2106 (remove-hook 'python-mode-hook ; undo this from `brm-init'
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2107 '(lambda () (easy-menu-add brm-menu)))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2108 (easy-menu-define
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2109 python-brm-menu python-mode-map
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2110 "Bicycle Repair Man"
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2111 '("BicycleRepairMan"
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2112 :help "Interface to navigation and refactoring tool"
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2113 "Queries"
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2114 ["Find References" brm-find-references
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2115 :help "Find references to name at point in compilation buffer"]
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2116 ["Find Definition" brm-find-definition
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2117 :help "Find definition of name at point"]
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2118 "-"
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2119 "Refactoring"
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2120 ["Rename" brm-rename
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2121 :help "Replace name at point with a new name everywhere"]
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2122 ["Extract Method" brm-extract-method
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2123 :active (and mark-active (not buffer-read-only))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2124 :help "Replace statements in region with a method"]
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2125 ["Extract Local Variable" brm-extract-local-variable
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2126 :active (and mark-active (not buffer-read-only))
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2127 :help "Replace expression in region with an assignment"]
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2128 ["Inline Local Variable" brm-inline-local-variable
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2129 :help
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2130 "Substitute uses of variable at point with its definition"]
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2131 ;; Fixme: Should check for anything to revert.
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2132 ["Undo Last Refactoring" brm-undo :help ""])))
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2133 (error (error "Bicyclerepairman setup failed: %s" data))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2134
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2135 ;;;; Modes.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2136
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2137 (defvar outline-heading-end-regexp)
61857
7b4e7e55f61b (python-mode): Use new name eldoc-documentation-function.
Richard M. Stallman <rms@gnu.org>
parents: 61086
diff changeset
2138 (defvar eldoc-documentation-function)
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
2139
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2140 ;; Stuff to allow expanding abbrevs with non-word constituents.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2141 (defun python-abbrev-pc-hook ()
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2142 "Set the syntax table before possibly expanding abbrevs."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2143 (remove-hook 'post-command-hook 'python-abbrev-pc-hook t)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2144 (set-syntax-table python-mode-syntax-table))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2145
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2146 (defvar python-abbrev-syntax-table
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2147 (copy-syntax-table python-mode-syntax-table)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2148 "Syntax table used when expanding abbrevs.")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2149
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2150 (defun python-pea-hook ()
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2151 "Reset the syntax table after possibly expanding abbrevs."
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2152 (set-syntax-table python-abbrev-syntax-table)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2153 (add-hook 'post-command-hook 'python-abbrev-pc-hook nil t))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2154 (modify-syntax-entry ?/ "w" python-abbrev-syntax-table)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2155
72527
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2156 (defvar python-mode-running) ;Dynamically scoped var.
9d3a9b609c04 (python-preoutput-skip-next-prompt): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72461
diff changeset
2157
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2158 ;;;###autoload
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2159 (define-derived-mode python-mode fundamental-mode "Python"
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2160 "Major mode for editing Python files.
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2161 Font Lock mode is currently required for correct parsing of the source.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2162 See also `jython-mode', which is actually invoked if the buffer appears to
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2163 contain Jython code. See also `run-python' and associated Python mode
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2164 commands for running Python under Emacs.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2165
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2166 The Emacs commands which work with `defun's, e.g. \\[beginning-of-defun], deal
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2167 with nested `def' and `class' blocks. They take the innermost one as
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2168 current without distinguishing method and class definitions. Used multiple
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2169 times, they move over others at the same indentation level until they reach
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2170 the end of definitions at that level, when they move up a level.
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2171 \\<python-mode-map>
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2172 Colon is electric: it outdents the line if appropriate, e.g. for
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2173 an else statement. \\[python-backspace] at the beginning of an indented statement
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2174 deletes a level of indentation to close the current block; otherwise it
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2175 deletes a character backward. TAB indents the current line relative to
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2176 the preceding code. Successive TABs, with no intervening command, cycle
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2177 through the possibilities for indentation on the basis of enclosing blocks.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2178
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2179 \\[fill-paragraph] fills comments and multi-line strings appropriately, but has no
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2180 effect outside them.
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2181
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2182 Supports Eldoc mode (only for functions, using a Python process),
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2183 Info-Look and Imenu. In Outline minor mode, `class' and `def'
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2184 lines count as headers. Symbol completion is available in the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2185 same way as in the Python shell using the `rlcompleter' module
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2186 and this is added to the Hippie Expand functions locally if
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2187 Hippie Expand mode is turned on. Completion of symbols of the
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2188 form x.y only works if the components are literal
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2189 module/attribute names, not variables. An abbrev table is set up
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2190 with skeleton expansions for compound statement templates.
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2191
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2192 \\{python-mode-map}"
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2193 :group 'python
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2194 (set (make-local-variable 'font-lock-defaults)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2195 '(python-font-lock-keywords nil nil nil nil
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2196 (font-lock-syntactic-keywords
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2197 . python-font-lock-syntactic-keywords)
60931
494d942e49fe (python-close-block-statement-p)
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59996
diff changeset
2198 ;; This probably isn't worth it.
494d942e49fe (python-close-block-statement-p)
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59996
diff changeset
2199 ;; (font-lock-syntactic-face-function
494d942e49fe (python-close-block-statement-p)
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 59996
diff changeset
2200 ;; . python-font-lock-syntactic-face-function)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2201 ))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2202 (set (make-local-variable 'parse-sexp-lookup-properties) t)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2203 (set (make-local-variable 'comment-start) "# ")
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2204 (set (make-local-variable 'indent-line-function) #'python-indent-line)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2205 (set (make-local-variable 'indent-region-function) #'python-indent-region)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2206 (set (make-local-variable 'paragraph-start) "\\s-*$")
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
2207 (set (make-local-variable 'fill-paragraph-function) 'python-fill-paragraph)
59250
54d81a8baaa6 (python-mode): Use mode-require-final-newline.
Richard M. Stallman <rms@gnu.org>
parents: 58739
diff changeset
2208 (set (make-local-variable 'require-final-newline) mode-require-final-newline)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2209 (set (make-local-variable 'add-log-current-defun-function)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2210 #'python-current-defun)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2211 (set (make-local-variable 'outline-regexp)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2212 (rx (* space) (or "class" "def" "elif" "else" "except" "finally"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2213 "for" "if" "try" "while")
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2214 symbol-end))
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2215 (set (make-local-variable 'outline-heading-end-regexp) ":\\s-*\n")
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2216 (set (make-local-variable 'outline-level) #'python-outline-level)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2217 (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2218 (make-local-variable 'python-saved-check-command)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2219 (set (make-local-variable 'beginning-of-defun-function)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2220 'python-beginning-of-defun)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2221 (set (make-local-variable 'end-of-defun-function) 'python-end-of-defun)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2222 (setq imenu-create-index-function #'python-imenu-create-index)
61857
7b4e7e55f61b (python-mode): Use new name eldoc-documentation-function.
Richard M. Stallman <rms@gnu.org>
parents: 61086
diff changeset
2223 (set (make-local-variable 'eldoc-documentation-function)
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2224 #'python-eldoc-function)
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2225 (add-hook 'eldoc-mode-hook
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2226 (lambda () (run-python nil t)) ; need it running
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2227 nil t)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2228 ;; Fixme: should be in hideshow. This seems to be of limited use
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2229 ;; since it isn't (can't be) indentation-based. Also hide-level
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2230 ;; doesn't seem to work properly.
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2231 (add-to-list 'hs-special-modes-alist
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2232 `(python-mode "^\\s-*def\\>" nil "#"
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2233 ,(lambda (arg)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2234 (python-end-of-defun)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2235 (skip-chars-backward " \t\n"))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2236 nil))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2237 (set (make-local-variable 'skeleton-further-elements)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2238 '((< '(backward-delete-char-untabify (min python-indent
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2239 (current-column))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2240 (^ '(- (1+ (current-indentation))))))
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2241 (add-hook 'pre-abbrev-expand-hook 'python-pea-hook nil t)
55397
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
2242 (if (featurep 'hippie-exp)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
2243 (set (make-local-variable 'hippie-expand-try-functions-list)
a828ab1b3079 Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 55300
diff changeset
2244 (cons 'python-try-complete hippie-expand-try-functions-list)))
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
2245 ;; Python defines TABs as being 8-char wide.
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
2246 (set (make-local-variable 'tab-width) 8)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2247 (when python-guess-indent (python-guess-indent))
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
2248 ;; Let's make it harder for the user to shoot himself in the foot.
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
2249 (unless (= tab-width python-indent)
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
2250 (setq indent-tabs-mode nil))
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2251 (set (make-local-variable 'python-command) python-python-command)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2252 (python-find-imports)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2253 (unless (boundp 'python-mode-running) ; kill the recursion from jython-mode
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2254 (let ((python-mode-running t))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2255 (python-maybe-jython))))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2256
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2257 (custom-add-option 'python-mode-hook 'imenu-add-menubar-index)
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2258 (custom-add-option 'python-mode-hook
72461
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
2259 (lambda ()
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
2260 "Turn off Indent Tabs mode."
2fee33a062da Remove * in defcustom docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 72459
diff changeset
2261 (set (make-local-variable 'indent-tabs-mode) nil)))
54840
3768540a819c Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54838
diff changeset
2262 (custom-add-option 'python-mode-hook 'turn-on-eldoc-mode)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2263 (custom-add-option 'python-mode-hook 'abbrev-mode)
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2264 (custom-add-option 'python-mode-hook 'python-setup-brm)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2265
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2266 ;;;###autoload
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2267 (define-derived-mode jython-mode python-mode "Jython"
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2268 "Major mode for editing Jython files.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2269 Like `python-mode', but sets up parameters for Jython subprocesses.
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2270 Runs `jython-mode-hook' after `python-mode-hook'."
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2271 :group 'python
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2272 (set (make-local-variable 'python-command) python-jython-command))
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2273
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2274 (provide 'python)
72459
2657308de2f6 Update to Dave Love's latest version.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70219
diff changeset
2275 (provide 'python-21)
54789
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2276 ;; arch-tag: 6fce1d99-a704-4de9-ba19-c6e4912b0554
9d1d7f062a4f New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2277 ;;; python.el ends here