comparison lisp/progmodes/python.el @ 90054:f2ebccfa87d4

Revision: miles@gnu.org--gnu-2004/emacs--unicode--0--patch-74 Merge from emacs--cvs-trunk--0 Patches applied: * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-709 Update from CVS: src/indent.c (Fvertical_motion): Fix last change. * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-710 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-715 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-716 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-74 Update from CVS
author Miles Bader <miles@gnu.org>
date Wed, 08 Dec 2004 05:02:30 +0000
parents 6f6e9fe4658b 2761697b0e2f
children 95879cc1ed20
comparison
equal deleted inserted replaced
90053:fff5f1a61d92 90054:f2ebccfa87d4
1 ;;; python.el --- silly walks for Python 1 ;;; python.el --- silly walks for Python
2 2
3 ;; Copyright (C) 2003, 04 Free Software Foundation, Inc. 3 ;; Copyright (C) 2003, 2004 Free Software Foundation, Inc.
4 4
5 ;; Author: Dave Love <fx@gnu.org> 5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Maintainer: FSF 6 ;; Maintainer: FSF
7 ;; Created: Nov 2003 7 ;; Created: Nov 2003
8 ;; Keywords: languages 8 ;; Keywords: languages
101 (1 font-lock-keyword-face) (2 font-lock-function-name-face)))) 101 (1 font-lock-keyword-face) (2 font-lock-function-name-face))))
102 102
103 (defconst python-font-lock-syntactic-keywords 103 (defconst python-font-lock-syntactic-keywords
104 ;; Make outer chars of matching triple-quote sequences into generic 104 ;; Make outer chars of matching triple-quote sequences into generic
105 ;; string delimiters. Fixme: Is there a better way? 105 ;; string delimiters. Fixme: Is there a better way?
106 `((,(rx (and (group (optional (any "uUrR"))) ; prefix gets syntax property 106 `((,(rx (and (or line-start buffer-start (not (syntax escape))) ; avoid escaped
107 ; leading quote
108 (group (optional (any "uUrR"))) ; prefix gets syntax property
107 (optional (any "rR")) ; possible second prefix 109 (optional (any "rR")) ; possible second prefix
108 (group (syntax string-quote)) ; maybe gets property 110 (group (syntax string-quote)) ; maybe gets property
109 (backref 2) ; per first quote 111 (backref 2) ; per first quote
110 (group (backref 2)))) ; maybe gets property 112 (group (backref 2)))) ; maybe gets property
111 (1 (python-quote-syntax 1)) 113 (1 (python-quote-syntax 1))
128 130
129 ;; Test cases: 131 ;; Test cases:
130 ;; ur"""ar""" x='"' # """ 132 ;; ur"""ar""" x='"' # """
131 ;; x = ''' """ ' a 133 ;; x = ''' """ ' a
132 ;; ''' 134 ;; '''
133 ;; x '"""' x 135 ;; x '"""' x """ \"""" x
134 (save-excursion 136 (save-excursion
135 (goto-char (match-beginning 0)) 137 (goto-char (match-beginning 0))
136 (unless (eq ?\\ (char-before)) 138 (cond
137 (cond 139 ;; Consider property for the last char if in a fenced string.
138 ;; Consider property for the last char if in a fenced string. 140 ((= n 3)
139 ((= n 3) 141 (let ((syntax (syntax-ppss)))
140 (let ((syntax (syntax-ppss))) 142 (when (eq t (nth 3 syntax)) ; after unclosed fence
141 (when (eq t (nth 3 syntax)) ; after unclosed fence 143 (goto-char (nth 8 syntax)) ; fence position
142 (goto-char (nth 8 syntax)) ; fence position 144 ;; Skip any prefix.
143 ;; Skip any prefix. 145 (if (memq (char-after) '(?u ?U ?R ?r))
144 (if (memq (char-after) '(?u ?U ?R ?r)) 146 (skip-chars-forward "uUrR"))
145 (skip-chars-forward "uUrR")) 147 ;; Is it a matching sequence?
146 ;; Is it a matching sequence? 148 (if (eq (char-after) (char-after (match-beginning 2)))
147 (if (eq (char-after) (char-after (match-beginning 2))) 149 (eval-when-compile (string-to-syntax "|"))))))
148 (eval-when-compile (string-to-syntax "|")))))) 150 ;; Consider property for initial char, accounting for prefixes.
149 ;; Consider property for initial char, accounting for prefixes. 151 ((or (and (= n 2) ; not prefix
150 ((or (and (= n 2) ; not prefix 152 (= (match-beginning 1) (match-end 1))) ; prefix is null
151 (= (match-beginning 1) (match-end 1))) ; prefix is null 153 (and (= n 1) ; prefix
152 (and (= n 1) ; prefix 154 (/= (match-beginning 1) (match-end 1)))) ; non-empty
153 (/= (match-beginning 1) (match-end 1)))) ; non-empty 155 (unless (eq 'string (syntax-ppss-context (syntax-ppss)))
154 (unless (eq 'string (syntax-ppss-context (syntax-ppss))) 156 (eval-when-compile (string-to-syntax "|"))))
155 (eval-when-compile (string-to-syntax "|"))))) 157 ;; Otherwise (we're in a non-matching string) the property is
156 ;; Otherwise (we're in a non-matching string) the property is 158 ;; nil, which is OK.
157 ;; nil, which is OK. 159 )))
158 )))
159 160
160 ;; This isn't currently in `font-lock-defaults' as probably not worth 161 ;; This isn't currently in `font-lock-defaults' as probably not worth
161 ;; it -- we basically only mess with a few normally-symbol characters. 162 ;; it -- we basically only mess with a few normally-symbol characters.
162 163
163 ;; (defun python-font-lock-syntactic-face-function (state) 164 ;; (defun python-font-lock-syntactic-face-function (state)
1125 "Run an inferior Python process, input and output via buffer *Python*. 1126 "Run an inferior Python process, input and output via buffer *Python*.
1126 CMD is the Python command to run. NOSHOW non-nil means don't show the 1127 CMD is the Python command to run. NOSHOW non-nil means don't show the
1127 buffer automatically. 1128 buffer automatically.
1128 If there is a process already running in `*Python*', switch to 1129 If there is a process already running in `*Python*', switch to
1129 that buffer. Interactively, a prefix arg allows you to edit the initial 1130 that buffer. Interactively, a prefix arg allows you to edit the initial
1130 command line (default is `python-command'); `-i' etc. args will be added 1131 command line (default is `python-command'); `-i' etc. args will be added
1131 to this as appropriate. Runs the hook `inferior-python-mode-hook' 1132 to this as appropriate. Runs the hook `inferior-python-mode-hook'
1132 \(after the `comint-mode-hook' is run). 1133 \(after the `comint-mode-hook' is run).
1133 \(Type \\[describe-mode] in the process buffer for a list of commands.)" 1134 \(Type \\[describe-mode] in the process buffer for a list of commands.)"
1134 (interactive (list (if current-prefix-arg 1135 (interactive (list (if current-prefix-arg
1135 (read-string "Run Python: " python-command) 1136 (read-string "Run Python: " python-command)
1141 ;; invoked. Would support multiple processes better. 1142 ;; invoked. Would support multiple processes better.
1142 (unless (comint-check-proc python-buffer) 1143 (unless (comint-check-proc python-buffer)
1143 (let* ((cmdlist (append (python-args-to-list cmd) '("-i"))) 1144 (let* ((cmdlist (append (python-args-to-list cmd) '("-i")))
1144 (path (getenv "PYTHONPATH")) 1145 (path (getenv "PYTHONPATH"))
1145 (process-environment ; to import emacs.py 1146 (process-environment ; to import emacs.py
1146 (push (concat "PYTHONPATH=" data-directory 1147 (cons (concat "PYTHONPATH=" data-directory
1147 (if path (concat ":" path))) 1148 (if path (concat ":" path)))
1148 process-environment))) 1149 process-environment)))
1149 (set-buffer (apply 'make-comint "Python" (car cmdlist) nil 1150 (set-buffer (apply 'make-comint "Python" (car cmdlist) nil
1150 (cdr cmdlist))) 1151 (cdr cmdlist)))
1151 (setq python-buffer "*Python*")) 1152 (setq python-buffer (buffer-name)))
1152 (inferior-python-mode) 1153 (inferior-python-mode)
1153 ;; Load function defintions we need. 1154 ;; Load function defintions we need.
1154 ;; Before the preoutput function was used, this was done via -c in 1155 ;; Before the preoutput function was used, this was done via -c in
1155 ;; cmdlist, but that loses the banner and doesn't run the startup 1156 ;; cmdlist, but that loses the banner and doesn't run the startup
1156 ;; file. The code might be inline here, but there's enough that it 1157 ;; file. The code might be inline here, but there's enough that it
1202 (goto-char orig-start) 1203 (goto-char orig-start)
1203 ;; Wrong if we had indented code at buffer start. 1204 ;; Wrong if we had indented code at buffer start.
1204 (set-marker orig-start (line-beginning-position 0))) 1205 (set-marker orig-start (line-beginning-position 0)))
1205 (write-region "if True:\n" nil f nil 'nomsg)) 1206 (write-region "if True:\n" nil f nil 'nomsg))
1206 (write-region start end f t 'nomsg) 1207 (write-region start end f t 'nomsg)
1207 (let ((proc (python-proc))) ;Make sure we're running a process. 1208 (with-current-buffer (process-buffer (python-proc)) ;Runs python if needed.
1208 (with-current-buffer python-buffer 1209 (python-send-command command)
1209 (python-send-command command) 1210 ;; Tell compile.el to redirect error locations in file `f' to
1210 ;; Tell compile.el to redirect error locations in file `f' to 1211 ;; positions past marker `orig-start'. It has to be done *after*
1211 ;; positions past marker `orig-start'. It has to be done *after* 1212 ;; python-send-command's call to compilation-forget-errors.
1212 ;; python-send-command's call to compilation-forget-errors. 1213 (compilation-fake-loc orig-start f))))
1213 (compilation-fake-loc orig-start f)))))
1214 1214
1215 (defun python-send-string (string) 1215 (defun python-send-string (string)
1216 "Evaluate STRING in inferior Python process." 1216 "Evaluate STRING in inferior Python process."
1217 (interactive "sPython command: ") 1217 (interactive "sPython command: ")
1218 (comint-send-string (python-proc) string) 1218 (comint-send-string (python-proc) string)
1233 1233
1234 (defun python-switch-to-python (eob-p) 1234 (defun python-switch-to-python (eob-p)
1235 "Switch to the Python process buffer. 1235 "Switch to the Python process buffer.
1236 With prefix arg, position cursor at end of buffer." 1236 With prefix arg, position cursor at end of buffer."
1237 (interactive "P") 1237 (interactive "P")
1238 (if (get-buffer python-buffer) 1238 (pop-to-buffer (process-buffer (python-proc))) ;Runs python if needed.
1239 (pop-to-buffer python-buffer)
1240 (error "No current process buffer. See variable `python-buffer'"))
1241 (when eob-p 1239 (when eob-p
1242 (push-mark) 1240 (push-mark)
1243 (goto-char (point-max)))) 1241 (goto-char (point-max))))
1244
1245 (add-to-list 'debug-ignored-errors "^No current process buffer.")
1246 1242
1247 (defun python-send-region-and-go (start end) 1243 (defun python-send-region-and-go (start end)
1248 "Send the region to the inferior Python process. 1244 "Send the region to the inferior Python process.
1249 Then switch to the process buffer." 1245 Then switch to the process buffer."
1250 (interactive "r") 1246 (interactive "r")
1273 python-source-modes 1269 python-source-modes
1274 t)) ; because execfile needs exact name 1270 t)) ; because execfile needs exact name
1275 (comint-check-source file-name) ; Check to see if buffer needs saving. 1271 (comint-check-source file-name) ; Check to see if buffer needs saving.
1276 (setq python-prev-dir/file (cons (file-name-directory file-name) 1272 (setq python-prev-dir/file (cons (file-name-directory file-name)
1277 (file-name-nondirectory file-name))) 1273 (file-name-nondirectory file-name)))
1278 (let ((proc (python-proc))) ;Make sure we have a process. 1274 (with-current-buffer (process-buffer (python-proc)) ;Runs python if needed.
1279 (with-current-buffer python-buffer 1275 ;; Fixme: I'm not convinced by this logic from python-mode.el.
1280 ;; Fixme: I'm not convinced by this logic from python-mode.el. 1276 (python-send-command
1281 (python-send-command 1277 (if (string-match "\\.py\\'" file-name)
1282 (if (string-match "\\.py\\'" file-name) 1278 (let ((module (file-name-sans-extension
1283 (let ((module (file-name-sans-extension 1279 (file-name-nondirectory file-name))))
1284 (file-name-nondirectory file-name)))) 1280 (format "emacs.eimport(%S,%S)"
1285 (format "emacs.eimport(%S,%S)" 1281 module (file-name-directory file-name)))
1286 module (file-name-directory file-name))) 1282 (format "execfile(%S)" file-name)))
1287 (format "execfile(%S)" file-name))) 1283 (message "%s loaded" file-name)))
1288 (message "%s loaded" file-name))))
1289 1284
1290 ;; Fixme: If we need to start the process, wait until we've got the OK 1285 ;; Fixme: If we need to start the process, wait until we've got the OK
1291 ;; from the startup. 1286 ;; from the startup.
1292 (defun python-proc () 1287 (defun python-proc ()
1293 "Return the current Python process. 1288 "Return the current Python process.