annotate lisp/progmodes/python.el @ 81548:334bfc4cc092

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