Mercurial > emacs
annotate lisp/progmodes/python.el @ 90201:fbb2bea03df9
Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-69
Merge from emacs--cvs-trunk--0
Patches applied:
* emacs--cvs-trunk--0 (patch 474-484)
- Update from CVS
- Merge from gnus--rel--5.10
* gnus--rel--5.10 (patch 88-91)
- Merge from emacs--cvs-trunk--0
- Update FSF's address in GPL notices
- Update from CVS
author | Miles Bader <miles@gnu.org> |
---|---|
date | Thu, 14 Jul 2005 08:02:00 +0000 |
parents | f9a65d7ebd29 |
children | a3716f7538f2 |
rev | line source |
---|---|
54789 | 1 ;;; python.el --- silly walks for Python |
2 | |
60931
494d942e49fe
(python-close-block-statement-p)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59996
diff
changeset
|
3 ;; Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. |
54789 | 4 |
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 | 7 ;; Created: Nov 2003 |
8 ;; Keywords: languages | |
9 | |
10 ;; This file is part of GNU Emacs. | |
11 | |
12 ;; This file is free software; you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
14 ;; the Free Software Foundation; either version 2, or (at your option) | |
15 ;; any later version. | |
16 | |
17 ;; This file is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
64085 | 24 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 ;; Boston, MA 02110-1301, USA. | |
54789 | 26 |
27 ;;; Commentary: | |
28 | |
29 ;; Major mode for editing Python, with support for inferior processes. | |
30 | |
31 ;; There is another Python mode, python-mode.el, used by XEmacs and | |
32 ;; maintained with Python. That isn't covered by an FSF copyright | |
33 ;; assignment, unlike this code, and seems not to be well-maintained | |
34 ;; for Emacs (though I've submitted fixes). This mode is rather | |
35 ;; simpler and is, perhaps, better in other ways. In particular, | |
36 ;; using the syntax functions with text properties maintained by | |
37 ;; font-lock should make it more correct with arbitrary string and | |
38 ;; comment contents. | |
39 | |
40 ;; This doesn't implement all the facilities of python-mode.el. Some | |
41 ;; 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
|
42 ;; buffer (but see M-x pdb for debugging). [Actually, the use of |
54789 | 43 ;; `compilation-minor-mode' now is probably enough for that.] Others |
44 ;; don't seem appropriate. For instance, `forward-into-nomenclature' | |
45 ;; should be done separately, since it's not specific to Python, and | |
46 ;; I've installed a minor mode to do the job properly in Emacs 22. | |
47 ;; Other things seem more natural or canonical here, e.g. the | |
48 ;; {beginning,end}-of-defun implementation dealing with nested | |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
49 ;; definitions, and the inferior mode following `cmuscheme'. The |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
50 ;; inferior mode can find the source of errors from |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
51 ;; `python-send-region' & al via `compilation-minor-mode'. Successive |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
52 ;; TABs cycle between possible indentations for the line. There is |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
53 ;; symbol completion using lookup in Python. |
54789 | 54 |
55 ;; Even where it has similar facilities, this is incompatible with | |
56 ;; python-mode.el in various respects. For instance, various key | |
57 ;; bindings are changed to obey Emacs conventions, and things like | |
58 ;; marking blocks and `beginning-of-defun' behave differently. | |
59 | |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
60 ;; TODO: See various Fixmes below. |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
61 |
54789 | 62 ;;; Code: |
63 | |
64 ;; It's messy to autoload the relevant comint functions so that comint | |
65 ;; is only required when inferior Python is used. | |
66 (require 'comint) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
67 (eval-when-compile |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
68 (require 'compile) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
69 (autoload 'info-lookup-maybe-add-help "info-look")) |
54938
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
70 (autoload 'compilation-start "compile") |
54789 | 71 |
72 (defgroup python nil | |
64049
69990675200d
(python): Finish `defgroup' description with period.
Juanma Barranquero <lekktu@gmail.com>
parents:
63831
diff
changeset
|
73 "Silly walks in the Python language." |
54789 | 74 :group 'languages |
59996
aac0a33f5772
Change release version from 21.4 to 22.1 throughout.
Kim F. Storm <storm@cua.dk>
parents:
59250
diff
changeset
|
75 :version "22.1" |
54789 | 76 :link '(emacs-commentary-link "python")) |
77 | |
78 ;;;###autoload | |
79 (add-to-list 'interpreter-mode-alist '("jython" . jython-mode)) | |
80 ;;;###autoload | |
81 (add-to-list 'interpreter-mode-alist '("python" . python-mode)) | |
82 ;;;###autoload | |
83 (add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode)) | |
84 | |
85 ;;;; Font lock | |
86 | |
87 (defvar python-font-lock-keywords | |
88 `(,(rx (and word-start | |
89 ;; From v 2.3 reference. | |
90 ;; def and class dealt with separately below | |
91 (or "and" "assert" "break" "continue" "del" "elif" "else" | |
92 "except" "exec" "finally" "for" "from" "global" "if" | |
93 "import" "in" "is" "lambda" "not" "or" "pass" "print" | |
94 "raise" "return" "try" "while" "yield" | |
95 ;; Future keywords | |
96 "as" "None") | |
97 word-end)) | |
98 (,(rx (and word-start (group "class") (1+ space) (group (1+ word)))) | |
99 (1 font-lock-keyword-face) (2 font-lock-type-face)) | |
100 (,(rx (and word-start (group "def") (1+ space) (group (1+ word)))) | |
101 (1 font-lock-keyword-face) (2 font-lock-function-name-face)))) | |
102 | |
103 (defconst python-font-lock-syntactic-keywords | |
104 ;; Make outer chars of matching triple-quote sequences into generic | |
105 ;; string delimiters. Fixme: Is there a better way? | |
58739
2761697b0e2f
(python-font-lock-syntactic-keywords): Fix previous change.
Richard M. Stallman <rms@gnu.org>
parents:
58487
diff
changeset
|
106 `((,(rx (and (or line-start buffer-start (not (syntax escape))) ; avoid escaped |
58487
d0581f1eef46
(python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents:
58415
diff
changeset
|
107 ; leading quote |
d0581f1eef46
(python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents:
58415
diff
changeset
|
108 (group (optional (any "uUrR"))) ; prefix gets syntax property |
54789 | 109 (optional (any "rR")) ; possible second prefix |
110 (group (syntax string-quote)) ; maybe gets property | |
111 (backref 2) ; per first quote | |
112 (group (backref 2)))) ; maybe gets property | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
113 (1 (python-quote-syntax 1)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
114 (2 (python-quote-syntax 2)) |
54789 | 115 (3 (python-quote-syntax 3))) |
116 ;; This doesn't really help. | |
117 ;;; (,(rx (and ?\\ (group ?\n))) (1 " ")) | |
118 )) | |
119 | |
120 (defun python-quote-syntax (n) | |
121 "Put `syntax-table' property correctly on triple quote. | |
122 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
|
123 ;; 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
|
124 ;; 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
|
125 ;; 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
|
126 ;; 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
|
127 ;; 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
|
128 ;; 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
|
129 ;; 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
|
130 |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
131 ;; Test cases: |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
132 ;; ur"""ar""" x='"' # """ |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
133 ;; x = ''' """ ' a |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
134 ;; ''' |
58487
d0581f1eef46
(python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents:
58415
diff
changeset
|
135 ;; x '"""' x """ \"""" x |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
136 (save-excursion |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
137 (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
|
138 (cond |
d0581f1eef46
(python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents:
58415
diff
changeset
|
139 ;; 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
|
140 ((= n 3) |
d0581f1eef46
(python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents:
58415
diff
changeset
|
141 (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
|
142 (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
|
143 (goto-char (nth 8 syntax)) ; fence position |
d0581f1eef46
(python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents:
58415
diff
changeset
|
144 ;; Skip any prefix. |
d0581f1eef46
(python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents:
58415
diff
changeset
|
145 (if (memq (char-after) '(?u ?U ?R ?r)) |
d0581f1eef46
(python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents:
58415
diff
changeset
|
146 (skip-chars-forward "uUrR")) |
d0581f1eef46
(python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents:
58415
diff
changeset
|
147 ;; 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
|
148 (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
|
149 (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
|
150 ;; Consider property for initial char, accounting for prefixes. |
d0581f1eef46
(python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents:
58415
diff
changeset
|
151 ((or (and (= n 2) ; not prefix |
d0581f1eef46
(python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents:
58415
diff
changeset
|
152 (= (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
|
153 (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
|
154 (/= (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
|
155 (unless (eq 'string (syntax-ppss-context (syntax-ppss))) |
d0581f1eef46
(python-font-lock-syntactic-keywords): Check for escapes in the regexp.
Richard M. Stallman <rms@gnu.org>
parents:
58415
diff
changeset
|
156 (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
|
157 ;; 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
|
158 ;; 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
|
159 ))) |
54789 | 160 |
161 ;; This isn't currently in `font-lock-defaults' as probably not worth | |
162 ;; it -- we basically only mess with a few normally-symbol characters. | |
163 | |
164 ;; (defun python-font-lock-syntactic-face-function (state) | |
165 ;; "`font-lock-syntactic-face-function' for Python mode. | |
166 ;; Returns the string or comment face as usual, with side effect of putting | |
167 ;; a `syntax-table' property on the inside of the string or comment which is | |
168 ;; the standard syntax table." | |
169 ;; (if (nth 3 state) | |
170 ;; (save-excursion | |
171 ;; (goto-char (nth 8 state)) | |
172 ;; (condition-case nil | |
173 ;; (forward-sexp) | |
174 ;; (error nil)) | |
175 ;; (put-text-property (1+ (nth 8 state)) (1- (point)) | |
176 ;; 'syntax-table (standard-syntax-table)) | |
177 ;; 'font-lock-string-face) | |
178 ;; (put-text-property (1+ (nth 8 state)) (line-end-position) | |
179 ;; 'syntax-table (standard-syntax-table)) | |
180 ;; 'font-lock-comment-face)) | |
181 | |
182 ;;;; Keymap and syntax | |
183 | |
184 (defvar python-mode-map | |
185 (let ((map (make-sparse-keymap))) | |
186 ;; Mostly taken from python-mode.el. | |
187 (define-key map ":" 'python-electric-colon) | |
188 (define-key map "\177" 'python-backspace) | |
189 (define-key map "\C-c<" 'python-shift-left) | |
190 (define-key map "\C-c>" 'python-shift-right) | |
191 (define-key map "\C-c\C-k" 'python-mark-block) | |
192 (define-key map "\C-c\C-n" 'python-next-statement) | |
193 (define-key map "\C-c\C-p" 'python-previous-statement) | |
194 (define-key map "\C-c\C-u" 'python-beginning-of-block) | |
195 (define-key map "\C-c\C-f" 'python-describe-symbol) | |
196 (define-key map "\C-c\C-w" 'python-check) | |
197 (define-key map "\C-c\C-v" 'python-check) ; a la sgml-mode | |
198 (define-key map "\C-c\C-s" 'python-send-string) | |
199 (define-key map [?\C-\M-x] 'python-send-defun) | |
200 (define-key map "\C-c\C-r" 'python-send-region) | |
201 (define-key map "\C-c\M-r" 'python-send-region-and-go) | |
202 (define-key map "\C-c\C-c" 'python-send-buffer) | |
203 (define-key map "\C-c\C-z" 'python-switch-to-python) | |
204 (define-key map "\C-c\C-m" 'python-load-file) | |
205 (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
|
206 (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
|
207 map global-map) |
54789 | 208 ;; Fixme: Add :help to menu. |
209 (easy-menu-define python-menu map "Python Mode menu" | |
210 '("Python" | |
211 ["Shift region left" python-shift-left :active mark-active] | |
212 ["Shift region right" python-shift-right :active mark-active] | |
213 "-" | |
214 ["Mark block" python-mark-block] | |
215 ["Mark def/class" mark-defun | |
216 :help "Mark innermost definition around point"] | |
217 "-" | |
218 ["Start of block" python-beginning-of-block] | |
219 ["End of block" python-end-of-block] | |
220 ["Start of def/class" beginning-of-defun | |
221 :help "Go to start of innermost definition around point"] | |
222 ["End of def/class" end-of-defun | |
223 :help "Go to end of innermost definition around point"] | |
224 "-" | |
225 ["Start interpreter" run-python | |
226 :help "Run `inferior' Python in separate buffer"] | |
227 ["Import/reload file" python-load-file | |
228 :help "Load into inferior Python session"] | |
229 ["Eval buffer" python-send-buffer | |
230 :help "Evaluate buffer en bloc in inferior Python session"] | |
231 ["Eval region" python-send-region :active mark-active | |
232 :help "Evaluate region en bloc in inferior Python session"] | |
233 ["Eval def/class" python-send-defun | |
234 :help "Evaluate current definition in inferior Python session"] | |
235 ["Switch to interpreter" python-switch-to-python | |
236 :help "Switch to inferior Python buffer"] | |
237 ["Check file" python-check :help "Run pychecker"] | |
238 ["Debugger" pdb :help "Run pdb under GUD"] | |
239 "-" | |
240 ["Help on symbol" python-describe-symbol | |
241 :help "Use pydoc on symbol at point"])) | |
242 map)) | |
243 | |
244 (defvar python-mode-syntax-table | |
245 (let ((table (make-syntax-table))) | |
246 ;; Give punctuation syntax to ASCII that normally has symbol | |
247 ;; syntax or has word syntax and isn't a letter. | |
248 (let ((symbol (string-to-syntax "_")) | |
249 (sst (standard-syntax-table))) | |
250 (dotimes (i 128) | |
251 (unless (= i ?_) | |
252 (if (equal symbol (aref sst i)) | |
253 (modify-syntax-entry i "." table))))) | |
254 (modify-syntax-entry ?$ "." table) | |
255 (modify-syntax-entry ?% "." table) | |
256 ;; exceptions | |
257 (modify-syntax-entry ?# "<" table) | |
258 (modify-syntax-entry ?\n ">" table) | |
259 (modify-syntax-entry ?' "\"" table) | |
260 (modify-syntax-entry ?` "$" table) | |
261 table)) | |
262 | |
263 ;;;; Utility stuff | |
264 | |
265 (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
|
266 "Return non-nil if point is in a Python literal (a comment or string)." |
54789 | 267 (syntax-ppss-context (syntax-ppss))) |
268 | |
269 (defconst python-space-backslash-table | |
270 (let ((table (copy-syntax-table python-mode-syntax-table))) | |
271 (modify-syntax-entry ?\\ " " table) | |
272 table) | |
273 "`python-mode-syntax-table' with backslash given whitespace syntax.") | |
274 | |
275 (defun python-skip-comments/blanks (&optional backward) | |
276 "Skip comments and blank lines. | |
277 BACKWARD non-nil means go backwards, otherwise go forwards. Backslash is | |
278 treated as whitespace so that continued blank lines are skipped. | |
279 Doesn't move out of comments -- should be outside or at end of line." | |
280 (with-syntax-table python-space-backslash-table | |
281 (forward-comment (if backward | |
282 most-negative-fixnum | |
283 most-positive-fixnum)))) | |
284 | |
285 (defun python-backslash-continuation-line-p () | |
286 "Non-nil if preceding line ends with backslash that is not in a comment." | |
287 (and (eq ?\\ (char-before (line-end-position 0))) | |
288 (not (syntax-ppss-context (syntax-ppss))))) | |
289 | |
290 (defun python-continuation-line-p () | |
291 "Return non-nil if current line continues a previous one. | |
292 The criteria are that the previous line ends in a backslash outside | |
293 comments and strings, or that the bracket/paren nesting depth is nonzero." | |
294 (or (and (eq ?\\ (char-before (line-end-position 0))) | |
295 (not (syntax-ppss-context (syntax-ppss)))) | |
296 (/= 0 (syntax-ppss-depth | |
297 (save-excursion ; syntax-ppss with arg changes point | |
298 (syntax-ppss (line-beginning-position))))))) | |
299 | |
300 (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
|
301 "Return non-nil iff current line has only a comment." |
54789 | 302 (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
|
303 (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
|
304 (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
|
305 (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
|
306 (looking-at (rx (or (syntax comment-start) line-end)))))) |
54789 | 307 |
308 (defun python-beginning-of-string () | |
309 "Go to beginning of string around point. | |
310 Do nothing if not in string." | |
311 (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
|
312 (when (eq 'string (syntax-ppss-context state)) |
54789 | 313 (goto-char (nth 8 state))))) |
314 | |
315 (defun python-open-block-statement-p (&optional bos) | |
316 "Return non-nil if statement at point opens a block. | |
317 BOS non-nil means point is known to be at beginning of statement." | |
318 (save-excursion | |
319 (unless bos (python-beginning-of-statement)) | |
320 (and (not (python-comment-line-p)) | |
321 (re-search-forward (rx (and ?: (0+ space) | |
322 (optional (and (syntax comment-start) | |
323 (0+ not-newline))) | |
324 line-end)) | |
325 (save-excursion (python-end-of-statement)) | |
326 t) | |
56051
0cecb3d4d566
(python-open-block-statement-p): Fix
Eli Zaretskii <eliz@gnu.org>
parents:
55511
diff
changeset
|
327 (not (progn (goto-char (match-beginning 0)) |
0cecb3d4d566
(python-open-block-statement-p): Fix
Eli Zaretskii <eliz@gnu.org>
parents:
55511
diff
changeset
|
328 (python-in-string/comment)))))) |
54789 | 329 |
330 (defun python-close-block-statement-p (&optional bos) | |
331 "Return non-nil if current line is a statement closing a block. | |
332 BOS non-nil means point is at beginning of statement. | |
333 The criteria are that the line isn't a comment or in string and starts with | |
334 keyword `raise', `break', `continue' or `pass'." | |
335 (save-excursion | |
336 (unless bos (python-beginning-of-statement)) | |
337 (back-to-indentation) | |
338 (looking-at (rx (and (or "return" "raise" "break" "continue" "pass") | |
60931
494d942e49fe
(python-close-block-statement-p)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59996
diff
changeset
|
339 symbol-end))))) |
54789 | 340 |
341 (defun python-outdent-p () | |
342 "Return non-nil if current line should outdent a level." | |
343 (save-excursion | |
344 (back-to-indentation) | |
60931
494d942e49fe
(python-close-block-statement-p)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59996
diff
changeset
|
345 (and (looking-at (rx (and (or (and (or "else" "finally") symbol-end) |
494d942e49fe
(python-close-block-statement-p)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59996
diff
changeset
|
346 (and (or "except" "elif") symbol-end |
54789 | 347 (1+ (not (any ?:))))) |
348 (optional space) ":" (optional space) | |
349 (or (syntax comment-start) line-end)))) | |
350 (progn (end-of-line) | |
351 (not (python-in-string/comment))) | |
352 ;; Ensure there's a previous statement and move to it. | |
353 (zerop (python-previous-statement)) | |
354 (not (python-close-block-statement-p t)) | |
355 ;; Fixme: check this | |
356 (not (looking-at (rx (and (or (and (or "if" "elif" "except" | |
357 "for" "while") | |
60931
494d942e49fe
(python-close-block-statement-p)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59996
diff
changeset
|
358 symbol-end (1+ (not (any ?:)))) |
494d942e49fe
(python-close-block-statement-p)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59996
diff
changeset
|
359 (and "try" symbol-end)) |
54789 | 360 (optional space) ":" (optional space) |
361 (or (syntax comment-start) line-end))))) | |
362 (progn (end-of-line) | |
363 (not (python-in-string/comment)))))) | |
364 | |
365 ;;;; Indentation. | |
366 | |
367 (defcustom python-indent 4 | |
368 "*Number of columns for a unit of indentation in Python mode. | |
369 See also `\\[python-guess-indent]'" | |
370 :group 'python | |
371 :type 'integer) | |
372 | |
373 (defcustom python-guess-indent t | |
374 "*Non-nil means Python mode guesses `python-indent' for the buffer." | |
375 :type 'boolean | |
376 :group 'python) | |
377 | |
378 (defcustom python-indent-string-contents t | |
379 "*Non-nil means indent contents of multi-line strings together. | |
380 This means indent them the same as the preceding non-blank line. | |
381 Otherwise indent them to column zero." | |
382 :type '(choice (const :tag "Align with preceding" t) | |
383 (const :tag "Indent to column 0" nil)) | |
384 :group 'python) | |
385 | |
386 (defcustom python-honour-comment-indentation nil | |
387 "Non-nil means indent relative to preceding comment line. | |
388 Only do this for comments where the leading comment character is followed | |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
389 by space. This doesn't apply to comment lines, which are always indented |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
390 in lines with preceding comments." |
54789 | 391 :type 'boolean |
392 :group 'python) | |
393 | |
394 (defcustom python-continuation-offset 4 | |
395 "*Number of columns of additional indentation for continuation lines. | |
396 Continuation lines follow a backslash-terminated line starting a statement." | |
397 :group 'python | |
398 :type 'integer) | |
399 | |
400 (defun python-guess-indent () | |
401 "Guess step for indentation of current buffer. | |
402 Set `python-indent' locally to the value guessed." | |
403 (interactive) | |
404 (save-excursion | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
405 (save-restriction |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
406 (widen) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
407 (goto-char (point-min)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
408 (let (done indent) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
409 (while (and (not done) (not (eobp))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
410 (when (and (re-search-forward (rx (and ?: (0+ space) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
411 (or (syntax comment-start) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
412 line-end))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
413 nil 'move) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
414 (python-open-block-statement-p)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
415 (save-excursion |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
416 (python-beginning-of-statement) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
417 (let ((initial (current-indentation))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
418 (if (zerop (python-next-statement)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
419 (setq indent (- (current-indentation) initial))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
420 (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
|
421 (setq done t)))))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
422 (when done |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
423 (when (/= indent (default-value 'python-indent)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
424 (set (make-local-variable 'python-indent) indent) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
425 (unless (= tab-width python-indent) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
426 (setq indent-tabs-mode nil))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
427 indent))))) |
54789 | 428 |
429 (defun python-calculate-indentation () | |
430 "Calculate Python indentation for line at point." | |
431 (save-excursion | |
432 (beginning-of-line) | |
433 (let ((syntax (syntax-ppss)) | |
434 start) | |
435 (cond | |
436 ((eq 'string (syntax-ppss-context syntax)) ; multi-line string | |
437 (if (not python-indent-string-contents) | |
438 0 | |
439 (save-excursion | |
440 ;; Find indentation of preceding non-blank line within string. | |
441 (setq start (nth 8 syntax)) | |
442 (forward-line -1) | |
443 (while (and (< start (point)) (looking-at "\\s-*$")) | |
444 (forward-line -1)) | |
445 (current-indentation)))) | |
446 ((python-continuation-line-p) | |
447 (let ((point (point)) | |
448 (open-start (cadr syntax))) | |
449 (if open-start | |
450 ;; Inside bracketed expression. | |
451 (progn | |
452 (goto-char (1+ open-start)) | |
453 ;; Look for first item in list (preceding point) and | |
454 ;; align with it, if found. | |
455 (if (with-syntax-table python-space-backslash-table | |
456 (let ((parse-sexp-ignore-comments t)) | |
457 (condition-case () | |
458 (progn (forward-sexp) | |
459 (backward-sexp) | |
460 (< (point) point)) | |
461 (error nil)))) | |
462 (current-column) | |
463 ;; Otherwise indent relative to statement start, one | |
464 ;; level per bracketing level. | |
465 (goto-char (1+ open-start)) | |
466 (python-beginning-of-statement) | |
467 (+ (current-indentation) (* (car syntax) python-indent)))) | |
468 ;; Otherwise backslash-continued. | |
469 (forward-line -1) | |
470 (if (python-continuation-line-p) | |
471 ;; We're past first continuation line. Align with | |
472 ;; previous line. | |
473 (current-indentation) | |
474 ;; First continuation line. Indent one step, with an | |
475 ;; extra one if statement opens a block. | |
476 (save-excursion | |
477 (python-beginning-of-statement) | |
478 (+ (current-indentation) python-continuation-offset | |
479 (if (python-open-block-statement-p t) | |
480 python-indent | |
481 0))))))) | |
482 ((bobp) 0) | |
483 ;; Fixme: Like python-mode.el; not convinced by this. | |
484 ((looking-at (rx (and (0+ space) (syntax comment-start) | |
485 (not (any " \t\n"))))) ; non-indentable comment | |
486 (current-indentation)) | |
487 (t (let ((point (point))) | |
488 (if python-honour-comment-indentation | |
489 ;; Back over whitespace, newlines, non-indentable comments. | |
490 (catch 'done | |
491 (while t | |
492 (if (cond ((bobp)) | |
493 ;; not at comment start | |
494 ((not (forward-comment -1)) | |
495 (python-beginning-of-statement) | |
496 t) | |
497 ;; trailing comment | |
498 ((/= (current-column) (current-indentation)) | |
499 (python-beginning-of-statement) | |
500 t) | |
501 ;; indentable comment like python-mode.el | |
502 ((and (looking-at (rx (and (syntax comment-start) | |
503 (or space line-end)))) | |
504 (/= 0 (current-column))))) | |
505 (throw 'done t)))) | |
506 ;; Else back over all comments. | |
507 (python-skip-comments/blanks t) | |
508 (python-beginning-of-statement)) | |
509 ;; don't lose on bogus outdent | |
510 (max 0 (+ (current-indentation) | |
511 (or (cond ((python-open-block-statement-p t) | |
512 python-indent) | |
513 ((python-close-block-statement-p t) | |
514 (- python-indent))) | |
515 (progn (goto-char point) | |
516 (if (python-outdent-p) | |
517 (- python-indent))) | |
518 0))))))))) | |
519 | |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
520 (defun python-comment-indent () |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
521 "`comment-indent-function' for Python." |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
522 ;; If previous non-blank line was a comment, use its indentation. |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
523 ;; FIXME: This seems unnecessary since the default code delegates to |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
524 ;; indent-according-to-mode. --Stef |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
525 (unless (bobp) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
526 (save-excursion |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
527 (forward-comment -1) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
528 (if (eq ?# (char-after)) (current-column))))) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
529 |
54789 | 530 ;;;; Cycling through the possible indentations with successive TABs. |
531 | |
532 ;; These don't need to be buffer-local since they're only relevant | |
533 ;; during a cycle. | |
534 | |
535 ;; Alist of possible indentations and start of statement they would close. | |
536 (defvar python-indent-list nil | |
537 "Internal use.") | |
538 ;; Length of the above | |
539 (defvar python-indent-list-length nil | |
540 "Internal use.") | |
541 ;; Current index into the alist. | |
542 (defvar python-indent-index nil | |
543 "Internal use.") | |
544 | |
545 (defun python-initial-text () | |
546 "Text of line following indentation and ignoring any trailing comment." | |
547 (buffer-substring (+ (line-beginning-position) (current-indentation)) | |
548 (save-excursion | |
549 (end-of-line) | |
550 (forward-comment -1) | |
551 (point)))) | |
552 | |
553 (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
|
554 "Return a list of possible indentations for this line. |
54789 | 555 Includes the default indentation and those which would close all |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
556 enclosing blocks. Assumes the line has already been indented per |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
557 `python-indent-line'. Elements of the list are actually pairs: |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
558 \(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
|
559 corresponding block opening (or nil)." |
54789 | 560 (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
|
561 (let ((levels (list (cons (current-indentation) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
562 (save-excursion |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
563 (if (python-beginning-of-block) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
564 (python-initial-text))))))) |
54789 | 565 ;; Only one possibility if we immediately follow a block open or |
566 ;; are in a continuation line. | |
567 (unless (or (python-continuation-line-p) | |
568 (save-excursion (and (python-previous-statement) | |
569 (python-open-block-statement-p t)))) | |
570 (while (python-beginning-of-block) | |
571 (push (cons (current-indentation) (python-initial-text)) | |
572 levels))) | |
573 levels))) | |
574 | |
575 ;; This is basically what `python-indent-line' would be if we didn't | |
576 ;; do the cycling. | |
577 (defun python-indent-line-1 () | |
578 "Subroutine of `python-indent-line'." | |
579 (let ((target (python-calculate-indentation)) | |
580 (pos (- (point-max) (point)))) | |
581 (if (= target (current-indentation)) | |
582 (if (< (current-column) (current-indentation)) | |
583 (back-to-indentation)) | |
584 (beginning-of-line) | |
585 (delete-horizontal-space) | |
586 (indent-to target) | |
587 (if (> (- (point-max) pos) (point)) | |
588 (goto-char (- (point-max) pos)))))) | |
589 | |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
590 (defun python-indent-line () |
54789 | 591 "Indent current line as Python code. |
592 When invoked via `indent-for-tab-command', cycle through possible | |
593 indentations for current line. The cycle is broken by a command different | |
594 from `indent-for-tab-command', i.e. successive TABs do the cycling." | |
595 (interactive) | |
596 ;; Don't do extra work if invoked via `indent-region', for instance. | |
597 (if (not (eq this-command 'indent-for-tab-command)) | |
598 (python-indent-line-1) | |
599 (if (eq last-command this-command) | |
600 (if (= 1 python-indent-list-length) | |
601 (message "Sole indentation") | |
602 (progn (setq python-indent-index (% (1+ python-indent-index) | |
603 python-indent-list-length)) | |
604 (beginning-of-line) | |
605 (delete-horizontal-space) | |
606 (indent-to (car (nth python-indent-index 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
|
607 (if (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
|
608 (let ((text (cdr (nth python-indent-index |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
609 python-indent-list)))) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
610 (if text |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
611 (message "Closes: %s" text)))))) |
54789 | 612 (python-indent-line-1) |
613 (setq python-indent-list (python-indentation-levels) | |
614 python-indent-list-length (length python-indent-list) | |
615 python-indent-index (1- python-indent-list-length))))) | |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
616 |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
617 (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
|
618 "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
|
619 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
|
620 (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
|
621 (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
|
622 (< (current-indentation) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
623 (save-excursion |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
624 (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
|
625 (current-indentation)))))) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
626 |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
627 ;; Fixme: Define an indent-region-function. It should probably leave |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
628 ;; lines alone if the indentation is already at one of the allowed |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
629 ;; levels. Otherwise, M-C-\ typically keeps indenting more deeply |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
630 ;; down a function. |
54789 | 631 |
632 ;;;; Movement. | |
633 | |
634 (defun python-beginning-of-defun () | |
635 "`beginning-of-defun-function' for Python. | |
636 Finds beginning of innermost nested class or method definition. | |
637 Returns the name of the definition found at the end, or nil if reached | |
638 start of buffer." | |
639 (let ((ci (current-indentation)) | |
640 (def-re (rx (and line-start (0+ space) (or "def" "class") | |
641 (1+ space) | |
642 (group (1+ (or word (syntax symbol))))))) | |
54888
89ed55db7532
(python-orig-start-line, python-orig-file): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54886
diff
changeset
|
643 found lep def-line) |
54789 | 644 (if (python-comment-line-p) |
645 (setq ci most-positive-fixnum)) | |
646 (while (and (not (bobp)) (not found)) | |
647 ;; Treat bol at beginning of function as outside function so | |
648 ;; that successive C-M-a makes progress backwards. | |
649 (setq def-line (looking-at def-re)) | |
650 (unless (bolp) (end-of-line)) | |
651 (setq lep (line-end-position)) | |
652 (if (and (re-search-backward def-re nil 'move) | |
653 ;; Must be less indented or matching top level, or | |
654 ;; equally indented if we started on a definition line. | |
655 (let ((in (current-indentation))) | |
656 (or (and (zerop ci) (zerop in)) | |
657 (= lep (line-end-position)) ; on initial line | |
658 (and def-line (= in ci)) | |
659 (< in ci))) | |
660 (not (python-in-string/comment))) | |
661 (setq found t))))) | |
662 | |
663 (defun python-end-of-defun () | |
664 "`end-of-defun-function' for Python. | |
665 Finds end of innermost nested class or method definition." | |
666 (let ((orig (point)) | |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
667 (pattern (rx (and line-start (0+ space) (or "def" "class") space)))) |
54789 | 668 ;; Go to start of current block and check whether it's at top |
669 ;; level. If it is, and not a block start, look forward for | |
670 ;; definition statement. | |
671 (when (python-comment-line-p) | |
672 (end-of-line) | |
673 (forward-comment most-positive-fixnum)) | |
674 (if (not (python-open-block-statement-p)) | |
675 (python-beginning-of-block)) | |
676 (if (zerop (current-indentation)) | |
677 (unless (python-open-block-statement-p) | |
678 (while (and (re-search-forward pattern nil 'move) | |
679 (python-in-string/comment))) ; just loop | |
680 (unless (eobp) | |
681 (beginning-of-line))) | |
682 ;; Don't move before top-level statement that would end defun. | |
683 (end-of-line) | |
684 (python-beginning-of-defun)) | |
685 ;; If we got to the start of buffer, look forward for | |
686 ;; definition statement. | |
687 (if (and (bobp) (not (looking-at "def\\|class"))) | |
688 (while (and (not (eobp)) | |
689 (re-search-forward pattern nil 'move) | |
690 (python-in-string/comment)))) ; just loop | |
691 ;; We're at a definition statement (or end-of-buffer). | |
692 (unless (eobp) | |
693 (python-end-of-block) | |
694 ;; Count trailing space in defun (but not trailing comments). | |
695 (skip-syntax-forward " >") | |
696 (beginning-of-line)) | |
697 ;; Catch pathological case like this, where the beginning-of-defun | |
698 ;; skips to a definition we're not in: | |
699 ;; if ...: | |
700 ;; ... | |
701 ;; else: | |
702 ;; ... # point here | |
703 ;; ... | |
704 ;; def ... | |
705 (if (< (point) orig) | |
706 (goto-char (point-max))))) | |
707 | |
708 (defun python-beginning-of-statement () | |
709 "Go to start of current statement. | |
710 Accounts for continuation lines, multi-line strings, and multi-line bracketed | |
711 expressions." | |
712 (beginning-of-line) | |
713 (python-beginning-of-string) | |
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
|
714 (catch 'foo |
e6bf7376c962
(python-beginning-of-statement): Exit the loop if backward-up-list gets error.
Richard M. Stallman <rms@gnu.org>
parents:
56051
diff
changeset
|
715 (while (python-continuation-line-p) |
e6bf7376c962
(python-beginning-of-statement): Exit the loop if backward-up-list gets error.
Richard M. Stallman <rms@gnu.org>
parents:
56051
diff
changeset
|
716 (beginning-of-line) |
e6bf7376c962
(python-beginning-of-statement): Exit the loop if backward-up-list gets error.
Richard M. Stallman <rms@gnu.org>
parents:
56051
diff
changeset
|
717 (if (python-backslash-continuation-line-p) |
e6bf7376c962
(python-beginning-of-statement): Exit the loop if backward-up-list gets error.
Richard M. Stallman <rms@gnu.org>
parents:
56051
diff
changeset
|
718 (while (python-backslash-continuation-line-p) |
e6bf7376c962
(python-beginning-of-statement): Exit the loop if backward-up-list gets error.
Richard M. Stallman <rms@gnu.org>
parents:
56051
diff
changeset
|
719 (forward-line -1)) |
e6bf7376c962
(python-beginning-of-statement): Exit the loop if backward-up-list gets error.
Richard M. Stallman <rms@gnu.org>
parents:
56051
diff
changeset
|
720 (python-beginning-of-string) |
e6bf7376c962
(python-beginning-of-statement): Exit the loop if backward-up-list gets error.
Richard M. Stallman <rms@gnu.org>
parents:
56051
diff
changeset
|
721 ;; Skip forward out of nested brackets. |
e6bf7376c962
(python-beginning-of-statement): Exit the loop if backward-up-list gets error.
Richard M. Stallman <rms@gnu.org>
parents:
56051
diff
changeset
|
722 (condition-case () ; beware invalid syntax |
e6bf7376c962
(python-beginning-of-statement): Exit the loop if backward-up-list gets error.
Richard M. Stallman <rms@gnu.org>
parents:
56051
diff
changeset
|
723 (progn (backward-up-list (syntax-ppss-depth (syntax-ppss))) t) |
e6bf7376c962
(python-beginning-of-statement): Exit the loop if backward-up-list gets error.
Richard M. Stallman <rms@gnu.org>
parents:
56051
diff
changeset
|
724 (error (throw 'foo nil)))))) |
54789 | 725 (back-to-indentation)) |
726 | |
727 (defun python-end-of-statement () | |
728 "Go to the end of the current statement and return point. | |
729 Usually this is the start of the next line, but if this is a | |
730 multi-line statement we need to skip over the continuation lines. | |
731 On a comment line, go to end of line." | |
732 (end-of-line) | |
733 (while (let (comment) | |
734 ;; Move past any enclosing strings and sexps, or stop if | |
735 ;; we're in a comment. | |
736 (while (let ((s (syntax-ppss))) | |
737 (cond ((eq 'comment (syntax-ppss-context s)) | |
738 (setq comment t) | |
739 nil) | |
740 ((eq 'string (syntax-ppss-context s)) | |
741 ;; Go to start of string and skip it. | |
742 (goto-char (nth 8 s)) | |
743 (condition-case () ; beware invalid syntax | |
744 (progn (forward-sexp) t) | |
745 (error (end-of-line)))) | |
746 ((> (syntax-ppss-depth s) 0) | |
747 ;; Skip forward out of nested brackets. | |
748 (condition-case () ; beware invalid syntax | |
749 (progn (backward-up-list | |
750 (- (syntax-ppss-depth s))) | |
751 t) | |
752 (error (end-of-line)))))) | |
753 (end-of-line)) | |
754 (unless comment | |
755 (eq ?\\ (char-before)))) ; Line continued? | |
756 (end-of-line 2)) ; Try next line. | |
757 (point)) | |
758 | |
759 (defun python-previous-statement (&optional count) | |
760 "Go to start of previous statement. | |
761 With argument COUNT, do it COUNT times. Stop at beginning of buffer. | |
762 Return count of statements left to move." | |
763 (interactive "p") | |
764 (unless count (setq count 1)) | |
765 (if (< count 0) | |
766 (python-next-statement (- count)) | |
767 (python-beginning-of-statement) | |
768 (while (and (> count 0) (not (bobp))) | |
769 (python-skip-comments/blanks t) | |
770 (python-beginning-of-statement) | |
771 (unless (bobp) (setq count (1- count)))) | |
772 count)) | |
773 | |
774 (defun python-next-statement (&optional count) | |
775 "Go to start of next statement. | |
776 With argument COUNT, do it COUNT times. Stop at end of buffer. | |
777 Return count of statements left to move." | |
778 (interactive "p") | |
779 (unless count (setq count 1)) | |
780 (if (< count 0) | |
781 (python-previous-statement (- count)) | |
782 (beginning-of-line) | |
783 (while (and (> count 0) (not (eobp))) | |
784 (python-end-of-statement) | |
785 (python-skip-comments/blanks) | |
786 (setq count (1- count))) | |
787 count)) | |
788 | |
789 (defun python-beginning-of-block (&optional arg) | |
790 "Go to start of current block. | |
791 With numeric arg, do it that many times. If ARG is negative, call | |
792 `python-end-of-block' instead. | |
793 If point is on the first line of a block, use its outer block. | |
794 If current statement is in column zero, don't move and return nil. | |
795 Otherwise return non-nil." | |
796 (interactive "p") | |
797 (unless arg (setq arg 1)) | |
798 (cond | |
799 ((zerop arg)) | |
800 ((< arg 0) (python-end-of-block (- arg))) | |
801 (t | |
802 (let ((point (point))) | |
803 (if (python-comment-line-p) | |
804 (python-skip-comments/blanks t)) | |
805 (python-beginning-of-statement) | |
806 (let ((ci (current-indentation))) | |
807 (if (zerop ci) | |
808 (not (goto-char point)) ; return nil | |
809 ;; Look upwards for less indented statement. | |
810 (if (catch 'done | |
811 ;;; This is slower than the below. | |
812 ;;; (while (zerop (python-previous-statement)) | |
813 ;;; (when (and (< (current-indentation) ci) | |
814 ;;; (python-open-block-statement-p t)) | |
815 ;;; (beginning-of-line) | |
816 ;;; (throw 'done t))) | |
817 (while (and (zerop (forward-line -1))) | |
818 (when (and (< (current-indentation) ci) | |
819 (not (python-comment-line-p)) | |
820 ;; Move to beginning to save effort in case | |
821 ;; this is in string. | |
822 (progn (python-beginning-of-statement) t) | |
823 (python-open-block-statement-p t)) | |
824 (beginning-of-line) | |
825 (throw 'done t))) | |
826 (not (goto-char point))) ; Failed -- return nil | |
827 (python-beginning-of-block (1- arg))))))))) | |
828 | |
829 (defun python-end-of-block (&optional arg) | |
830 "Go to end of current block. | |
831 With numeric arg, do it that many times. If ARG is negative, call | |
832 `python-beginning-of-block' instead. | |
833 If current statement is in column zero and doesn't open a block, don't | |
834 move and return nil. Otherwise return t." | |
835 (interactive "p") | |
836 (unless arg (setq arg 1)) | |
837 (if (< arg 0) | |
838 (python-beginning-of-block (- arg))) | |
839 (while (and (> arg 0) | |
840 (let* ((point (point)) | |
841 (_ (if (python-comment-line-p) | |
842 (python-skip-comments/blanks t))) | |
843 (ci (current-indentation)) | |
844 (open (python-open-block-statement-p))) | |
845 (if (and (zerop ci) (not open)) | |
846 (not (goto-char point)) | |
847 (catch 'done | |
848 (while (zerop (python-next-statement)) | |
849 (when (or (and open (<= (current-indentation) ci)) | |
850 (< (current-indentation) ci)) | |
851 (python-skip-comments/blanks t) | |
852 (beginning-of-line 2) | |
853 (throw 'done t))) | |
854 (not (goto-char point)))))) | |
855 (setq arg (1- arg))) | |
856 (zerop arg)) | |
857 | |
858 ;;;; Imenu. | |
859 | |
54888
89ed55db7532
(python-orig-start-line, python-orig-file): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54886
diff
changeset
|
860 (defvar python-recursing) |
54789 | 861 (defun python-imenu-create-index () |
862 "`imenu-create-index-function' for Python. | |
863 | |
864 Makes nested Imenu menus from nested `class' and `def' statements. | |
865 The nested menus are headed by an item referencing the outer | |
866 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
|
867 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
|
868 precede it)." |
54888
89ed55db7532
(python-orig-start-line, python-orig-file): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54886
diff
changeset
|
869 (unless (boundp 'python-recursing) ; dynamically bound below |
54789 | 870 (goto-char (point-min))) ; normal call from Imenu |
871 (let (index-alist ; accumulated value to return | |
54888
89ed55db7532
(python-orig-start-line, python-orig-file): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54886
diff
changeset
|
872 name) |
54789 | 873 (while (re-search-forward |
874 (rx (and line-start (0+ space) ; leading space | |
875 (or (group "def") (group "class")) ; type | |
876 (1+ space) (group (1+ (or word ?_))))) ; name | |
877 nil t) | |
878 (unless (python-in-string/comment) | |
879 (let ((pos (match-beginning 0)) | |
880 (name (match-string-no-properties 3))) | |
881 (if (match-beginning 2) ; def or class? | |
882 (setq name (concat "class " name))) | |
883 (save-restriction | |
884 (narrow-to-defun) | |
54888
89ed55db7532
(python-orig-start-line, python-orig-file): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54886
diff
changeset
|
885 (let* ((python-recursing t) |
54789 | 886 (sublist (python-imenu-create-index))) |
887 (if sublist | |
888 (progn (push (cons (concat " " name) pos) sublist) | |
889 (push (cons name sublist) index-alist)) | |
890 (push (cons name pos) index-alist))))))) | |
891 (nreverse index-alist))) | |
892 | |
893 ;;;; `Electric' commands. | |
894 | |
895 (defun python-electric-colon (arg) | |
896 "Insert a colon and maybe outdent the line if it is a statement like `else'. | |
897 With numeric ARG, just insert that many colons. With \\[universal-argument], | |
898 just insert a single colon." | |
899 (interactive "*P") | |
900 (self-insert-command (if (not (integerp arg)) 1 arg)) | |
901 (and (not arg) | |
902 (eolp) | |
903 (python-outdent-p) | |
904 (not (python-in-string/comment)) | |
905 (> (current-indentation) (python-calculate-indentation)) | |
906 (python-indent-line))) ; OK, do it | |
907 (put 'python-electric-colon 'delete-selection t) | |
908 | |
909 (defun python-backspace (arg) | |
910 "Maybe delete a level of indentation on the current line. | |
911 If not at the end of line's indentation, or on a comment line, just call | |
912 `backward-delete-char-untabify'. With ARG, repeat that many times." | |
913 (interactive "*p") | |
914 (if (or (/= (current-indentation) (current-column)) | |
915 (bolp) | |
916 (python-continuation-line-p)) | |
917 (backward-delete-char-untabify arg) | |
918 (let ((indent 0)) | |
919 (save-excursion | |
920 (while (and (> arg 0) (python-beginning-of-block)) | |
921 (setq arg (1- arg))) | |
922 (when (zerop arg) | |
923 (setq indent (current-indentation)) | |
924 (message "Closes %s" (python-initial-text)))) | |
925 (delete-horizontal-space) | |
926 (indent-to indent)))) | |
927 (put 'python-backspace 'delete-selection 'supersede) | |
928 | |
929 ;;;; pychecker | |
930 | |
931 (defcustom python-check-command "pychecker --stdlib" | |
932 "*Command used to check a Python file." | |
933 :type 'string | |
934 :group 'python) | |
935 | |
936 (defvar python-saved-check-command nil | |
937 "Internal use.") | |
938 | |
939 ;; After `sgml-validate-command'. | |
940 (defun python-check (command) | |
941 "Check a Python file (default current buffer's file). | |
942 Runs COMMAND, a shell command, as if by `compile'. | |
943 See `python-check-command' for the default." | |
944 (interactive | |
945 (list (read-string "Checker command: " | |
946 (or python-saved-check-command | |
947 (concat python-check-command " " | |
948 (let ((name (buffer-file-name))) | |
949 (if name | |
950 (file-name-nondirectory name)))))))) | |
951 (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
|
952 (require 'compile) ;To define compilation-* variables. |
54789 | 953 (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
|
954 (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
|
955 (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
|
956 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
|
957 (compilation-start command))) |
54789 | 958 |
959 ;;;; Inferior mode stuff (following cmuscheme). | |
960 | |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
961 ;; 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
|
962 |
54789 | 963 (defcustom python-python-command "python" |
964 "*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
|
965 Any arguments can't contain whitespace. |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
966 Note that IPython may not work properly; it must at least be used with the |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
967 `-cl' flag, i.e. use `ipython -cl'." |
54789 | 968 :group 'python |
969 :type 'string) | |
970 | |
971 (defcustom python-jython-command "jython" | |
972 "*Shell command to run Jython interpreter. | |
973 Any arguments can't contain whitespace." | |
974 :group 'python | |
975 :type 'string) | |
976 | |
977 (defvar python-command python-python-command | |
978 "Actual command used to run Python. | |
979 May be `python-python-command' or `python-jython-command'. | |
980 Additional arguments are added when the command is used by `run-python' | |
981 et al.") | |
982 | |
983 (defvar python-buffer nil | |
54938
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
984 "The current python process buffer." |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
985 ;; Fixme: a single process is currently assumed, so that this doc |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
986 ;; is misleading. |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
987 |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
988 ;; "*The current python process buffer. |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
989 ;; To run multiple Python processes, start the first with \\[run-python]. |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
990 ;; It will be in a buffer named *Python*. Rename that with |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
991 ;; \\[rename-buffer]. Now start a new process with \\[run-python]. It |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
992 ;; will be in a new buffer, named *Python*. Switch between the different |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
993 ;; process buffers with \\[switch-to-buffer]. |
54789 | 994 |
54938
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
995 ;; Commands that send text from source buffers to Python processes have |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
996 ;; to choose a process to send to. This is determined by global variable |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
997 ;; `python-buffer'. Suppose you have three inferior Pythons running: |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
998 ;; Buffer Process |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
999 ;; foo python |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1000 ;; bar python<2> |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1001 ;; *Python* python<3> |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1002 ;; If you do a \\[python-send-region-and-go] command on some Python source |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1003 ;; code, what process does it go to? |
54789 | 1004 |
54938
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1005 ;; - In a process buffer (foo, bar, or *Python*), send it to that process. |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1006 ;; - In some other buffer (e.g. a source file), send it to the process |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1007 ;; attached to `python-buffer'. |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1008 ;; Process selection is done by function `python-proc'. |
54789 | 1009 |
54938
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1010 ;; Whenever \\[run-python] starts a new process, it resets `python-buffer' |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1011 ;; to be the new process's buffer. If you only run one process, this will |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1012 ;; do the right thing. If you run multiple processes, you can change |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1013 ;; `python-buffer' to another process buffer with \\[set-variable]." |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1014 ) |
54789 | 1015 |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1016 (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
|
1017 ;; FIXME: maybe these should move to compilation-error-regexp-alist-alist. |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1018 `((,(rx (and line-start (1+ (any " \t")) "File \"" |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1019 (group (1+ (not (any "\"<")))) ; avoid `<stdin>' &c |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1020 "\", 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
|
1021 1 2) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1022 (,(rx (and " in file " (group (1+ not-newline)) " on line " |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1023 (group (1+ digit)))) |
55300
2db456741f80
(python-compilation-line-number): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55240
diff
changeset
|
1024 1 2)) |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1025 "`compilation-error-regexp-alist' for inferior Python.") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1026 |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1027 (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
|
1028 (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
|
1029 ;; 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
|
1030 (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
|
1031 (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
|
1032 ;; 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
|
1033 ;; 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
|
1034 ;; 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
|
1035 ;; 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
|
1036 |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1037 ;; (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
|
1038 ;; (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
|
1039 map)) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1040 |
54938
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1041 ;; Fixme: This should inherit some stuff from python-mode, but I'm not |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1042 ;; sure how much: at least some keybindings, like C-c C-f; syntax?; |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1043 ;; font-locking, e.g. for triple-quoted strings? |
54789 | 1044 (define-derived-mode inferior-python-mode comint-mode "Inferior Python" |
1045 "Major mode for interacting with an inferior Python process. | |
1046 A Python process can be started with \\[run-python]. | |
1047 | |
1048 Hooks `comint-mode-hook' and `inferior-python-mode-hook' are run in | |
1049 that order. | |
1050 | |
1051 You can send text to the inferior Python process from other buffers containing | |
1052 Python source. | |
1053 * `python-switch-to-python' switches the current buffer to the Python | |
1054 process buffer. | |
1055 * `python-send-region' sends the current region to the Python process. | |
1056 * `python-send-region-and-go' switches to the Python process buffer | |
1057 after sending the text. | |
1058 For running multiple processes in multiple buffers, see `python-buffer'. | |
1059 | |
1060 \\{inferior-python-mode-map}" | |
1061 :group 'python | |
1062 (set-syntax-table python-mode-syntax-table) | |
1063 (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
|
1064 (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
|
1065 (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
|
1066 nil t) |
54938
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1067 ;; 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
|
1068 ;; (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
|
1069 (set (make-local-variable 'comint-prompt-regexp) |
64049
69990675200d
(python): Finish `defgroup' description with period.
Juanma Barranquero <lekktu@gmail.com>
parents:
63831
diff
changeset
|
1070 (rx (and line-start (1+ (and (repeat 3 (any ">.")) ?\s))))) |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1071 (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
|
1072 python-compilation-regexp-alist) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1073 (compilation-shell-minor-mode 1)) |
54789 | 1074 |
1075 (defcustom inferior-python-filter-regexp "\\`\\s-*\\S-?\\S-?\\s-*\\'" | |
1076 "*Input matching this regexp is not saved on the history list. | |
1077 Default ignores all inputs of 0, 1, or 2 non-blank characters." | |
1078 :type 'regexp | |
1079 :group 'python) | |
1080 | |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1081 (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
|
1082 "`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
|
1083 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
|
1084 (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
|
1085 |
54789 | 1086 ;; Fixme: Loses with quoted whitespace. |
1087 (defun python-args-to-list (string) | |
1088 (let ((where (string-match "[ \t]" string))) | |
1089 (cond ((null where) (list string)) | |
1090 ((not (= where 0)) | |
1091 (cons (substring string 0 where) | |
1092 (python-args-to-list (substring string (+ 1 where))))) | |
1093 (t (let ((pos (string-match "[^ \t]" string))) | |
1094 (if pos (python-args-to-list (substring string pos)))))))) | |
1095 | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1096 (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
|
1097 "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
|
1098 |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1099 (defvar python-preoutput-continuation nil |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1100 "If non-nil, funcall this when `python-preoutput-filter' sees `_emacs_ok'.") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1101 |
61037
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1102 (defvar python-preoutput-leftover nil) |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1103 |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1104 ;; 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
|
1105 ;; >>> ... ... >>> |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1106 ;; Also look for (and delete) an `_emacs_ok' string and call |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1107 ;; `python-preoutput-continuation' if we get it. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1108 (defun python-preoutput-filter (s) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1109 "`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
|
1110 (when python-preoutput-leftover |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1111 (setq s (concat python-preoutput-leftover s)) |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1112 (setq python-preoutput-leftover nil)) |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1113 (cond ((and (string-match (rx (and string-start (repeat 3 (any ".>")) |
61037
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1114 " " string-end)) |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1115 s) |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1116 (/= (let ((inhibit-field-text-motion t)) |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1117 (line-beginning-position)) |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1118 (point))) |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1119 "") |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1120 ((string= s "_emacs_ok\n") |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1121 (when python-preoutput-continuation |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1122 (funcall python-preoutput-continuation) |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1123 (setq python-preoutput-continuation nil)) |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1124 "") |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1125 ((string-match "_emacs_out \\(.*\\)\n" s) |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1126 (setq python-preoutput-result (match-string 1 s)) |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1127 "") |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1128 ((string-match ".*\n" s) |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1129 s) |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1130 ((or (eq t (compare-strings s nil nil "_emacs_ok\n" nil (length s))) |
61086
bf4917ef8f38
(python-preoutput-filter): Fix last change.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
61037
diff
changeset
|
1131 (let ((end (min (length "_emacs_out ") (length s)))) |
bf4917ef8f38
(python-preoutput-filter): Fix last change.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
61037
diff
changeset
|
1132 (eq t (compare-strings s nil end "_emacs_out " nil end)))) |
61037
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1133 (setq python-preoutput-leftover s) |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1134 "") |
61037
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1135 (t s))) |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1136 |
54789 | 1137 ;;;###autoload |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1138 (defun run-python (&optional cmd noshow) |
54789 | 1139 "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
|
1140 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
|
1141 buffer automatically. |
54789 | 1142 If there is a process already running in `*Python*', switch to |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1143 that buffer. Interactively, a prefix arg allows you to edit the initial |
58415
86ead4686506
(run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
58358
diff
changeset
|
1144 command line (default is `python-command'); `-i' etc. args will be added |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1145 to this as appropriate. Runs the hook `inferior-python-mode-hook' |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1146 \(after the `comint-mode-hook' is run). |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1147 \(Type \\[describe-mode] in the process buffer for a list of commands.)" |
54789 | 1148 (interactive (list (if current-prefix-arg |
1149 (read-string "Run Python: " python-command) | |
1150 python-command))) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1151 (unless cmd (setq cmd python-python-command)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1152 (setq python-command cmd) |
54789 | 1153 ;; Fixme: Consider making `python-buffer' buffer-local as a buffer |
1154 ;; (not a name) in Python buffers from which `run-python' &c is | |
1155 ;; invoked. Would support multiple processes better. | |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1156 (unless (comint-check-proc python-buffer) |
55511
5dbde1bf6cad
(help-buffer): Autoload when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55495
diff
changeset
|
1157 (let* ((cmdlist (append (python-args-to-list cmd) '("-i"))) |
5dbde1bf6cad
(help-buffer): Autoload when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55495
diff
changeset
|
1158 (path (getenv "PYTHONPATH")) |
5dbde1bf6cad
(help-buffer): Autoload when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55495
diff
changeset
|
1159 (process-environment ; to import emacs.py |
58415
86ead4686506
(run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
58358
diff
changeset
|
1160 (cons (concat "PYTHONPATH=" data-directory |
55511
5dbde1bf6cad
(help-buffer): Autoload when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55495
diff
changeset
|
1161 (if path (concat ":" path))) |
5dbde1bf6cad
(help-buffer): Autoload when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55495
diff
changeset
|
1162 process-environment))) |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1163 (set-buffer (apply 'make-comint "Python" (car cmdlist) nil |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1164 (cdr cmdlist))) |
58415
86ead4686506
(run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
58358
diff
changeset
|
1165 (setq python-buffer (buffer-name))) |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1166 (inferior-python-mode) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1167 ;; Load function defintions we need. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1168 ;; Before the preoutput function was used, this was done via -c in |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1169 ;; cmdlist, but that loses the banner and doesn't run the startup |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1170 ;; file. The code might be inline here, but there's enough that it |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1171 ;; seems worth putting in a separate file, and it's probably cleaner |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1172 ;; to put it in a module. |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1173 (python-send-string "import emacs")) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1174 (unless noshow (pop-to-buffer python-buffer))) |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1175 |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1176 ;; 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
|
1177 ;; e.g. prompt is `help> '. Probably raise an error if the form of |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1178 ;; the prompt is unexpected; actually, it needs to be `>>> ', not |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1179 ;; `... ', i.e. we're not inputting a block &c. However, this may not |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1180 ;; be the place to do it, e.g. we might actually want to send commands |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1181 ;; having set up such a state. |
54789 | 1182 |
55240
780b94f913fe
(python-send-command): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55054
diff
changeset
|
1183 (defun python-send-command (command) |
780b94f913fe
(python-send-command): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55054
diff
changeset
|
1184 "Like `python-send-string' but resets `compilation-minor-mode'." |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1185 (goto-char (point-max)) |
55240
780b94f913fe
(python-send-command): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55054
diff
changeset
|
1186 (let ((end (marker-position (process-mark (python-proc))))) |
780b94f913fe
(python-send-command): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55054
diff
changeset
|
1187 (compilation-forget-errors) |
780b94f913fe
(python-send-command): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55054
diff
changeset
|
1188 (python-send-string command) |
780b94f913fe
(python-send-command): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55054
diff
changeset
|
1189 (set-marker compilation-parsing-end end) |
780b94f913fe
(python-send-command): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55054
diff
changeset
|
1190 (setq compilation-last-buffer (current-buffer)))) |
780b94f913fe
(python-send-command): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55054
diff
changeset
|
1191 |
54789 | 1192 (defun python-send-region (start end) |
1193 "Send the region to the inferior Python process." | |
1194 ;; The region is evaluated from a temporary file. This avoids | |
1195 ;; problems with blank lines, which have different semantics | |
1196 ;; 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
|
1197 ;; 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
|
1198 ;; 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
|
1199 ;; (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
|
1200 ;; 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
|
1201 ;; tracebacks not to mention the frame of the function itself. |
54789 | 1202 ;; |
1203 ;; The compilation-minor-mode parsing takes care of relating the | |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1204 ;; reference to the temporary file to the source. |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1205 ;; |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1206 ;; 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
|
1207 ;; non-ASCII. |
54789 | 1208 (interactive "r") |
1209 (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
|
1210 (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
|
1211 (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
|
1212 (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
|
1213 (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
|
1214 (/= 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
|
1215 (save-excursion |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1216 (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
|
1217 ;; 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
|
1218 (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
|
1219 (write-region "if True:\n" nil f nil 'nomsg)) |
54789 | 1220 (write-region start end f t 'nomsg) |
58415
86ead4686506
(run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
58358
diff
changeset
|
1221 (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
|
1222 (python-send-command command) |
86ead4686506
(run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
58358
diff
changeset
|
1223 ;; 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
|
1224 ;; positions past marker `orig-start'. It has to be done *after* |
86ead4686506
(run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
58358
diff
changeset
|
1225 ;; python-send-command's call to compilation-forget-errors. |
86ead4686506
(run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
58358
diff
changeset
|
1226 (compilation-fake-loc orig-start f)))) |
54789 | 1227 |
1228 (defun python-send-string (string) | |
1229 "Evaluate STRING in inferior Python process." | |
1230 (interactive "sPython command: ") | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1231 (comint-send-string (python-proc) string) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1232 (comint-send-string (python-proc) "\n\n")) |
54789 | 1233 |
1234 (defun python-send-buffer () | |
1235 "Send the current buffer to the inferior Python process." | |
1236 (interactive) | |
1237 (python-send-region (point-min) (point-max))) | |
1238 | |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1239 ;; 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
|
1240 ;; module, not just at top level. |
54789 | 1241 (defun python-send-defun () |
1242 "Send the current defun (class or method) to the inferior Python process." | |
1243 (interactive) | |
1244 (save-excursion (python-send-region (progn (beginning-of-defun) (point)) | |
1245 (progn (end-of-defun) (point))))) | |
1246 | |
1247 (defun python-switch-to-python (eob-p) | |
1248 "Switch to the Python process buffer. | |
1249 With prefix arg, position cursor at end of buffer." | |
1250 (interactive "P") | |
58415
86ead4686506
(run-python): Don't hard code *Python*. Don't modify process-environment.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
58358
diff
changeset
|
1251 (pop-to-buffer (process-buffer (python-proc))) ;Runs python if needed. |
54789 | 1252 (when eob-p |
1253 (push-mark) | |
1254 (goto-char (point-max)))) | |
1255 | |
1256 (defun python-send-region-and-go (start end) | |
1257 "Send the region to the inferior Python process. | |
1258 Then switch to the process buffer." | |
1259 (interactive "r") | |
1260 (python-send-region start end) | |
1261 (python-switch-to-python t)) | |
1262 | |
1263 (defcustom python-source-modes '(python-mode jython-mode) | |
1264 "*Used to determine if a buffer contains Python source code. | |
1265 If it's loaded into a buffer that is in one of these major modes, it's | |
1266 considered a Python source file by `python-load-file'. | |
1267 Used by these commands to determine defaults." | |
1268 :type '(repeat function) | |
1269 :group 'python) | |
1270 | |
1271 (defvar python-prev-dir/file nil | |
1272 "Caches (directory . file) pair used in the last `python-load-file' command. | |
1273 Used for determining the default in the next one.") | |
1274 | |
1275 (defun python-load-file (file-name) | |
1276 "Load a Python file FILE-NAME into the inferior Python process. | |
1277 If the file has extension `.py' import or reload it as a module. | |
1278 Treating it as a module keeps the global namespace clean, provides | |
1279 function location information for debugging, and supports users of | |
1280 module-qualified names." | |
1281 (interactive (comint-get-source "Load Python file: " python-prev-dir/file | |
1282 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
|
1283 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
|
1284 (comint-check-source file-name) ; Check to see if buffer needs saving. |
54789 | 1285 (setq python-prev-dir/file (cons (file-name-directory file-name) |
1286 (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
|
1287 (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
|
1288 ;; 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
|
1289 (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
|
1290 (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
|
1291 (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
|
1292 (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
|
1293 (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
|
1294 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
|
1295 (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
|
1296 (message "%s loaded" file-name))) |
54789 | 1297 |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1298 ;; Fixme: If we need to start the process, wait until we've got the OK |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1299 ;; from the startup. |
54789 | 1300 (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
|
1301 "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
|
1302 See variable `python-buffer'. Starts a new process if necessary." |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1303 (or (if python-buffer |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1304 (get-buffer-process (if (eq major-mode 'inferior-python-mode) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1305 (current-buffer) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1306 python-buffer))) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1307 (progn (run-python nil t) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1308 (python-proc)))) |
54789 | 1309 |
1310 ;;;; Context-sensitive help. | |
1311 | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1312 (defconst python-dotty-syntax-table |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1313 (let ((table (make-syntax-table))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1314 (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
|
1315 (modify-syntax-entry ?. "_" table) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1316 table) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1317 "Syntax table giving `.' symbol syntax. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1318 Otherwise inherits from `python-mode-syntax-table'.") |
54789 | 1319 |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1320 (defvar view-return-to-alist) |
55511
5dbde1bf6cad
(help-buffer): Autoload when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55495
diff
changeset
|
1321 (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
|
1322 |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1323 ;; Fixme: Should this actually be used instead of info-look, i.e. be |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1324 ;; bound to C-h S? Can we use other pydoc stuff before python 2.2? |
54789 | 1325 (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
|
1326 "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
|
1327 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
|
1328 |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1329 Symbol may be anything recognized by the interpreter's `help' command -- |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1330 e.g. `CALLS' -- not just variables in scope. |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1331 This only works for Python version 2.2 or newer since earlier interpreters |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1332 don't support `help'." |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1333 ;; 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
|
1334 ;; ensure the environment is appropriate. |
54789 | 1335 (interactive |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1336 (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
|
1337 (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
|
1338 (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
|
1339 (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
|
1340 (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
|
1341 "Describe symbol: ") |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1342 nil nil symbol)))) |
54789 | 1343 (if (equal symbol "") (error "No symbol")) |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1344 (let* ((func `(lambda () |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1345 (comint-redirect-send-command (format "emacs.ehelp(%S)\n" |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1346 ,symbol) |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1347 "*Help*" nil)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1348 ;; Ensure we have a suitable help buffer. |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1349 ;; Fixme: Maybe process `Related help topics' a la help xrefs and |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1350 ;; allow C-c C-f in help buffer. |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1351 (let ((temp-buffer-show-hook ; avoid xref stuff |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1352 (lambda () |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1353 (toggle-read-only 1) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1354 (setq view-return-to-alist |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1355 (list (cons (selected-window) help-return-method)))))) |
55447
f1b7359315f0
(python-describe-symbol): Pass INTERACTIVE-P argument to `help-setup-xref'.
Juanma Barranquero <lekktu@gmail.com>
parents:
55397
diff
changeset
|
1356 (help-setup-xref (list 'python-describe-symbol symbol) (interactive-p)) |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1357 (with-output-to-temp-buffer (help-buffer) |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1358 (with-current-buffer standard-output |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1359 (set (make-local-variable 'comint-redirect-subvert-readonly) t) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1360 (print-help-return-message)))) |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1361 (if (and python-buffer (get-buffer python-buffer)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1362 (with-current-buffer python-buffer |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1363 (funcall func)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1364 (setq python-preoutput-continuation func) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1365 (run-python nil t)))) |
54789 | 1366 |
1367 (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
|
1368 |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1369 (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
|
1370 "Send STRING to inferior Python (if any) and return result. |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1371 The result is what follows `_emacs_out' in the output (or nil)." |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1372 (let ((proc (python-proc))) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1373 (python-send-string string) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1374 (setq python-preoutput-result nil) |
61037
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1375 (while (progn |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1376 (accept-process-output proc 5) |
758ff54158e2
(python-preoutput-leftover): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
60931
diff
changeset
|
1377 python-preoutput-leftover)) |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1378 python-preoutput-result)) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1379 |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1380 ;; Fixme: try to make it work with point in the arglist. Also, is |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1381 ;; there anything reasonable we can do with random methods? |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1382 ;; (Currently only works with functions.) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1383 (defun python-eldoc-function () |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1384 "`eldoc-print-current-symbol-info' for Python. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1385 Only works when point is in a function name, not its arglist, for instance. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1386 Assumes an inferior Python is running." |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1387 (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
|
1388 (current-word)))) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1389 (when symbol |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1390 (python-send-receive (format "emacs.eargs(%S)" symbol))))) |
54789 | 1391 |
1392 ;;;; Info-look functionality. | |
1393 | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1394 (defun python-after-info-look () |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1395 "Set up info-look for Python. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1396 Used with `eval-after-load'." |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1397 (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
|
1398 " -V")))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1399 (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
|
1400 (match-string 1 s))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1401 ;; 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
|
1402 (versioned |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1403 (with-temp-buffer |
54938
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1404 (with-no-warnings (Info-mode)) |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1405 (condition-case () |
54888
89ed55db7532
(python-orig-start-line, python-orig-file): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54886
diff
changeset
|
1406 ;; 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
|
1407 (with-no-warnings |
109b2bf180dd
(python-after-info-look): Use with-no-warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54888
diff
changeset
|
1408 (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
|
1409 version)) |
5dbde1bf6cad
(help-buffer): Autoload when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55495
diff
changeset
|
1410 t) |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1411 (error nil))))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1412 (info-lookup-maybe-add-help |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1413 :mode 'python-mode |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1414 :regexp "[[:alnum:]_]+" |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1415 :doc-spec |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1416 ;; 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
|
1417 ;; 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
|
1418 ;; (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
|
1419 ;; instance.) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1420 (if versioned |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1421 ;; 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
|
1422 `((,(concat "(python" version "-ref)Miscellaneous Index") nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1423 (,(concat "(python" version "-ref)Module Index" nil "")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1424 (,(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
|
1425 nil "")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1426 (,(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
|
1427 nil "")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1428 (,(concat "(python" version "-lib)Module Index" nil "")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1429 (,(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
|
1430 nil "")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1431 (,(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
|
1432 nil "")) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1433 (,(concat "(python" version "-lib)Miscellaneous Index" nil ""))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1434 '(("(python-ref)Miscellaneous Index" nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1435 ("(python-ref)Module Index" nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1436 ("(python-ref)Function-Method-Variable Index" nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1437 ("(python-ref)Class-Exception-Object Index" nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1438 ("(python-lib)Module Index" nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1439 ("(python-lib)Class-Exception-Object Index" nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1440 ("(python-lib)Function-Method-Variable Index" nil "") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1441 ("(python-lib)Miscellaneous Index" nil "")))))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1442 (eval-after-load "info-look" '(python-after-info-look)) |
54789 | 1443 |
1444 ;;;; Miscellancy. | |
1445 | |
1446 (defcustom python-jython-packages '("java" "javax" "org" "com") | |
1447 "Packages implying `jython-mode'. | |
1448 If these are imported near the beginning of the buffer, `python-mode' | |
1449 actually punts to `jython-mode'." | |
1450 :type '(repeat string) | |
1451 :group 'python) | |
1452 | |
1453 ;; Called from `python-mode', this causes a recursive call of the | |
1454 ;; mode. See logic there to break out of the recursion. | |
1455 (defun python-maybe-jython () | |
1456 "Invoke `jython-mode' if the buffer appears to contain Jython code. | |
1457 The criterion is either a match for `jython-mode' via | |
1458 `interpreter-mode-alist' or an import of a module from the list | |
1459 `python-jython-packages'." | |
1460 ;; The logic is taken from python-mode.el. | |
1461 (save-excursion | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1462 (save-restriction |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1463 (widen) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1464 (goto-char (point-min)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1465 (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
|
1466 (match-string 2)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1467 (if (and interpreter (eq 'jython-mode |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1468 (cdr (assoc (file-name-nondirectory |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1469 interpreter) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1470 interpreter-mode-alist)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1471 (jython-mode) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1472 (if (catch 'done |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1473 (while (re-search-forward |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1474 (rx (and line-start (or "import" "from") (1+ space) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1475 (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
|
1476 (+ (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
|
1477 t) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1478 (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
|
1479 (throw 'done t)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1480 (jython-mode))))))) |
54789 | 1481 |
1482 (defun python-fill-paragraph (&optional justify) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1483 "`fill-paragraph-function' handling comments and multi-line strings. |
54789 | 1484 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
|
1485 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
|
1486 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
|
1487 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
|
1488 Otherwise, do nothing." |
54789 | 1489 (interactive "P") |
1490 (or (fill-comment-paragraph justify) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1491 ;; The `paragraph-start' and `paragraph-separate' variables |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1492 ;; 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
|
1493 ;; 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
|
1494 ;; (the end of) the current line. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1495 (save-excursion |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1496 (end-of-line) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1497 (let* ((syntax (syntax-ppss)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1498 (orig (point)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1499 (start (nth 8 syntax)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1500 end) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1501 (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
|
1502 (goto-char (nth 8 syntax)) ; string start |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1503 (condition-case () ; for unbalanced quotes |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1504 (progn (forward-sexp) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1505 (setq end (point))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1506 (error (setq end (point-max))))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1507 ((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
|
1508 ; string |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1509 (forward-char) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1510 (setq end (point)) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1511 (condition-case () |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1512 (progn (backward-sexp) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1513 (setq start (point))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1514 (error nil)))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1515 (when end |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1516 (save-restriction |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1517 (narrow-to-region start end) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1518 (goto-char orig) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1519 (fill-paragraph justify)))))) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1520 t) |
54789 | 1521 |
1522 (defun python-shift-left (start end &optional count) | |
1523 "Shift lines in region COUNT (the prefix arg) columns to the left. | |
1524 COUNT defaults to `python-indent'. If region isn't active, just shift | |
1525 current line. The region shifted includes the lines in which START and | |
1526 END lie. It is an error if any lines in the region are indented less than | |
1527 COUNT columns." | |
1528 (interactive (if mark-active | |
1529 (list (region-beginning) (region-end) current-prefix-arg) | |
1530 (list (point) (point) current-prefix-arg))) | |
1531 (if count | |
1532 (setq count (prefix-numeric-value count)) | |
1533 (setq count python-indent)) | |
1534 (when (> count 0) | |
1535 (save-excursion | |
1536 (goto-char start) | |
1537 (while (< (point) end) | |
1538 (if (and (< (current-indentation) count) | |
1539 (not (looking-at "[ \t]*$"))) | |
1540 (error "Can't shift all lines enough")) | |
1541 (forward-line)) | |
1542 (indent-rigidly start end (- count))))) | |
1543 | |
1544 (add-to-list 'debug-ignored-errors "^Can't shift all lines enough") | |
1545 | |
1546 (defun python-shift-right (start end &optional count) | |
1547 "Shift lines in region COUNT (the prefix arg) columns to the right. | |
1548 COUNT defaults to `python-indent'. If region isn't active, just shift | |
1549 current line. The region shifted includes the lines in which START and | |
1550 END lie." | |
1551 (interactive (if mark-active | |
1552 (list (region-beginning) (region-end) current-prefix-arg) | |
1553 (list (point) (point) current-prefix-arg))) | |
1554 (if count | |
1555 (setq count (prefix-numeric-value count)) | |
1556 (setq count python-indent)) | |
1557 (indent-rigidly start end count)) | |
1558 | |
1559 (defun python-outline-level () | |
1560 "`outline-level' function for Python mode. | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1561 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
|
1562 of current line." |
54789 | 1563 (/ (current-indentation) python-indent)) |
1564 | |
1565 ;; Fixme: Consider top-level assignments, imports, &c. | |
1566 (defun python-current-defun () | |
1567 "`add-log-current-defun-function' for Python." | |
1568 (save-excursion | |
1569 ;; Move up the tree of nested `class' and `def' blocks until we | |
1570 ;; get to zero indentation, accumulating the defined names. | |
1571 (let ((start t) | |
1572 accum) | |
1573 (while (or start (> (current-indentation) 0)) | |
1574 (setq start nil) | |
1575 (python-beginning-of-block) | |
1576 (end-of-line) | |
1577 (beginning-of-defun) | |
1578 (if (looking-at (rx (and (0+ space) (or "def" "class") (1+ space) | |
1579 (group (1+ (or word (syntax symbol)))) | |
60931
494d942e49fe
(python-close-block-statement-p)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59996
diff
changeset
|
1580 ;; Greediness makes this unnecessary? --Stef |
494d942e49fe
(python-close-block-statement-p)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59996
diff
changeset
|
1581 symbol-end))) |
54789 | 1582 (push (match-string 1) accum))) |
1583 (if accum (mapconcat 'identity accum "."))))) | |
1584 | |
1585 (defun python-mark-block () | |
1586 "Mark the block around point. | |
1587 Uses `python-beginning-of-block', `python-end-of-block'." | |
1588 (interactive) | |
1589 (push-mark) | |
1590 (python-beginning-of-block) | |
1591 (push-mark (point) nil t) | |
1592 (python-end-of-block) | |
1593 (exchange-point-and-mark)) | |
1594 | |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1595 ;;;; Completion. |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1596 |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1597 (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
|
1598 "Return a list of completions of the string SYMBOL from Python process. |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1599 The list is sorted." |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1600 (when symbol |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1601 (let ((completions |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1602 (condition-case () |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1603 (car (read-from-string (python-send-receive |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1604 (format "emacs.complete(%S)" symbol)))) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1605 (error nil)))) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1606 (sort |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1607 ;; 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
|
1608 (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
|
1609 #'string<)))) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1610 |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1611 (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
|
1612 "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
|
1613 (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
|
1614 (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
|
1615 (and (re-search-backward |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1616 (rx (and (or buffer-start (regexp "[^[:alnum:]._]")) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1617 (group (1+ (regexp "[[:alnum:]._]"))) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1618 point)) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1619 nil t) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1620 (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
|
1621 (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
|
1622 |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1623 ;; Fixme: We should have an abstraction of this sort of thing in the |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1624 ;; core. |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1625 (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
|
1626 "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
|
1627 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
|
1628 (interactive) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1629 (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
|
1630 (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
|
1631 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
|
1632 (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
|
1633 (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
|
1634 (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
|
1635 (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
|
1636 (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
|
1637 (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
|
1638 (scroll-up)))) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1639 ;; Do completion. |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1640 (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
|
1641 (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
|
1642 (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
|
1643 (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
|
1644 (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
|
1645 (when symbol |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1646 (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
|
1647 ((null completion) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1648 (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
|
1649 (ding)) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1650 ((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
|
1651 (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
|
1652 (insert completion)) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1653 (t |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1654 (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
|
1655 (with-output-to-temp-buffer "*Completions*" |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1656 (display-completion-list completions)) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1657 (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
|
1658 |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1659 (eval-when-compile (require 'hippie-exp)) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1660 |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1661 (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
|
1662 "Completion function for Python for use with `hippie-expand'." |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1663 (when (eq major-mode 'python-mode) ; though we only add it locally |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1664 (unless old |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1665 (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
|
1666 (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
|
1667 (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
|
1668 (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
|
1669 (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
|
1670 (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
|
1671 (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
|
1672 (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
|
1673 (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
|
1674 (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
|
1675 (progn |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1676 (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
|
1677 t) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1678 (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
|
1679 nil))) |
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1680 |
54789 | 1681 ;;;; Modes. |
1682 | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1683 (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
|
1684 (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
|
1685 |
54789 | 1686 ;;;###autoload |
1687 (define-derived-mode python-mode fundamental-mode "Python" | |
1688 "Major mode for editing Python files. | |
1689 Turns on Font Lock mode unconditionally since it is required for correct | |
1690 parsing of the source. | |
1691 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
|
1692 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
|
1693 commands for running Python under Emacs. |
54789 | 1694 |
1695 The Emacs commands which work with `defun's, e.g. \\[beginning-of-defun], deal | |
1696 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
|
1697 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
|
1698 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
|
1699 the end of definitions at that level, when they move up a level. |
54789 | 1700 \\<python-mode-map> |
1701 Colon is electric: it outdents the line if appropriate, e.g. for | |
1702 an else statement. \\[python-backspace] at the beginning of an indented statement | |
1703 deletes a level of indentation to close the current block; otherwise it | |
1704 deletes a charcter backward. TAB indents the current line relative to | |
1705 the preceding code. Successive TABs, with no intervening command, cycle | |
1706 through the possibilities for indentation on the basis of enclosing blocks. | |
1707 | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1708 \\[fill-paragraph] fills comments and multiline strings appropriately, but has no |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1709 effect outside them. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1710 |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1711 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
|
1712 Info-Look and Imenu. In Outline minor mode, `class' and `def' |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1713 lines count as headers. |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1714 |
54789 | 1715 \\{python-mode-map}" |
1716 :group 'python | |
1717 (set (make-local-variable 'font-lock-defaults) | |
1718 '(python-font-lock-keywords nil nil ((?_ . "w")) nil | |
1719 (font-lock-syntactic-keywords | |
1720 . python-font-lock-syntactic-keywords) | |
60931
494d942e49fe
(python-close-block-statement-p)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59996
diff
changeset
|
1721 ;; This probably isn't worth it. |
494d942e49fe
(python-close-block-statement-p)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59996
diff
changeset
|
1722 ;; (font-lock-syntactic-face-function |
494d942e49fe
(python-close-block-statement-p)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
59996
diff
changeset
|
1723 ;; . python-font-lock-syntactic-face-function) |
54789 | 1724 )) |
1725 (set (make-local-variable 'parse-sexp-lookup-properties) t) | |
1726 (set (make-local-variable 'comment-start) "# ") | |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1727 (set (make-local-variable 'comment-indent-function) #'python-comment-indent) |
54789 | 1728 (set (make-local-variable 'indent-line-function) #'python-indent-line) |
1729 (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
|
1730 (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
|
1731 (set (make-local-variable 'require-final-newline) mode-require-final-newline) |
54789 | 1732 (set (make-local-variable 'add-log-current-defun-function) |
1733 #'python-current-defun) | |
1734 ;; Fixme: Generalize to do all blocks? | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1735 (set (make-local-variable 'outline-regexp) "\\s-*\\(def\\|class\\)\\>") |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1736 (set (make-local-variable 'outline-heading-end-regexp) ":\\s-*\n") |
54789 | 1737 (set (make-local-variable 'outline-level) #'python-outline-level) |
1738 (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil) | |
1739 (make-local-variable 'python-saved-check-command) | |
1740 (set (make-local-variable 'beginning-of-defun-function) | |
1741 'python-beginning-of-defun) | |
1742 (set (make-local-variable 'end-of-defun-function) 'python-end-of-defun) | |
1743 (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
|
1744 (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
|
1745 #'python-eldoc-function) |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1746 (add-hook 'eldoc-mode-hook |
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1747 '(lambda () (run-python 0 t)) nil t) ; need it running |
55397
a828ab1b3079
Changes largely merged in from Dave Love's code. Doc fixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55300
diff
changeset
|
1748 (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
|
1749 (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
|
1750 (cons 'python-try-complete hippie-expand-try-functions-list))) |
54789 | 1751 (unless font-lock-mode (font-lock-mode 1)) |
1752 (when python-guess-indent (python-guess-indent)) | |
1753 (set (make-local-variable 'python-command) python-python-command) | |
1754 (unless (boundp 'python-mode-running) ; kill the recursion from jython-mode | |
1755 (let ((python-mode-running t)) | |
1756 (python-maybe-jython)))) | |
1757 | |
1758 (custom-add-option 'python-mode-hook 'imenu-add-menubar-index) | |
1759 (custom-add-option 'python-mode-hook | |
1760 '(lambda () | |
1761 "Turn on Indent Tabs mode." | |
1762 (set (make-local-variable 'indent-tabs-mode) t))) | |
54840
3768540a819c
Doc fixes. Changes for compiler warnings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
54838
diff
changeset
|
1763 (custom-add-option 'python-mode-hook 'turn-on-eldoc-mode) |
54789 | 1764 |
1765 ;;;###autoload | |
1766 (define-derived-mode jython-mode python-mode "Jython" | |
1767 "Major mode for editing Jython files. | |
1768 Like `python-mode', but sets up parameters for Jython subprocesses. | |
1769 Runs `jython-mode-hook' after `python-mode-hook'." | |
1770 :group 'python | |
1771 (set (make-local-variable 'python-command) python-jython-command)) | |
1772 | |
1773 (provide 'python) | |
1774 ;; arch-tag: 6fce1d99-a704-4de9-ba19-c6e4912b0554 | |
1775 ;;; python.el ends here |