annotate lisp/progmodes/python.el @ 55176:6bce3db285b5

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