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