1 |
;;; python.el --- silly walks for Python |
;;; python.el --- silly walks for Python |
2 |
|
|
3 |
;; Copyright (C) 2003, 04 Free Software Foundation, Inc. |
;; Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
4 |
|
|
5 |
;; Author: Dave Love <fx@gnu.org> |
;; Author: Dave Love <fx@gnu.org> |
6 |
;; Maintainer: FSF |
;; Maintainer: FSF |
103 |
(defconst python-font-lock-syntactic-keywords |
(defconst python-font-lock-syntactic-keywords |
104 |
;; Make outer chars of matching triple-quote sequences into generic |
;; Make outer chars of matching triple-quote sequences into generic |
105 |
;; string delimiters. Fixme: Is there a better way? |
;; string delimiters. Fixme: Is there a better way? |
106 |
`((,(rx (and (group (optional (any "uUrR"))) ; prefix gets syntax property |
`((,(rx (and (or line-start buffer-start (not (syntax escape))) ; avoid escaped |
107 |
|
; leading quote |
108 |
|
(group (optional (any "uUrR"))) ; prefix gets syntax property |
109 |
(optional (any "rR")) ; possible second prefix |
(optional (any "rR")) ; possible second prefix |
110 |
(group (syntax string-quote)) ; maybe gets property |
(group (syntax string-quote)) ; maybe gets property |
111 |
(backref 2) ; per first quote |
(backref 2) ; per first quote |
132 |
;; ur"""ar""" x='"' # """ |
;; ur"""ar""" x='"' # """ |
133 |
;; x = ''' """ ' a |
;; x = ''' """ ' a |
134 |
;; ''' |
;; ''' |
135 |
;; x '"""' x |
;; x '"""' x """ \"""" x |
136 |
(save-excursion |
(save-excursion |
137 |
(goto-char (match-beginning 0)) |
(goto-char (match-beginning 0)) |
138 |
(unless (eq ?\\ (char-before)) |
(cond |
139 |
(cond |
;; Consider property for the last char if in a fenced string. |
140 |
;; Consider property for the last char if in a fenced string. |
((= n 3) |
141 |
((= n 3) |
(let ((syntax (syntax-ppss))) |
142 |
(let ((syntax (syntax-ppss))) |
(when (eq t (nth 3 syntax)) ; after unclosed fence |
143 |
(when (eq t (nth 3 syntax)) ; after unclosed fence |
(goto-char (nth 8 syntax)) ; fence position |
144 |
(goto-char (nth 8 syntax)) ; fence position |
;; Skip any prefix. |
145 |
;; Skip any prefix. |
(if (memq (char-after) '(?u ?U ?R ?r)) |
146 |
(if (memq (char-after) '(?u ?U ?R ?r)) |
(skip-chars-forward "uUrR")) |
147 |
(skip-chars-forward "uUrR")) |
;; Is it a matching sequence? |
148 |
;; Is it a matching sequence? |
(if (eq (char-after) (char-after (match-beginning 2))) |
149 |
(if (eq (char-after) (char-after (match-beginning 2))) |
(eval-when-compile (string-to-syntax "|")))))) |
150 |
(eval-when-compile (string-to-syntax "|")))))) |
;; Consider property for initial char, accounting for prefixes. |
151 |
;; Consider property for initial char, accounting for prefixes. |
((or (and (= n 2) ; not prefix |
152 |
((or (and (= n 2) ; not prefix |
(= (match-beginning 1) (match-end 1))) ; prefix is null |
153 |
(= (match-beginning 1) (match-end 1))) ; prefix is null |
(and (= n 1) ; prefix |
154 |
(and (= n 1) ; prefix |
(/= (match-beginning 1) (match-end 1)))) ; non-empty |
155 |
(/= (match-beginning 1) (match-end 1)))) ; non-empty |
(unless (eq 'string (syntax-ppss-context (syntax-ppss))) |
156 |
(unless (eq 'string (syntax-ppss-context (syntax-ppss))) |
(eval-when-compile (string-to-syntax "|")))) |
157 |
(eval-when-compile (string-to-syntax "|"))))) |
;; Otherwise (we're in a non-matching string) the property is |
158 |
;; Otherwise (we're in a non-matching string) the property is |
;; nil, which is OK. |
159 |
;; nil, which is OK. |
))) |
|
))) |
|
160 |
|
|
161 |
;; This isn't currently in `font-lock-defaults' as probably not worth |
;; This isn't currently in `font-lock-defaults' as probably not worth |
162 |
;; it -- we basically only mess with a few normally-symbol characters. |
;; it -- we basically only mess with a few normally-symbol characters. |
1128 |
buffer automatically. |
buffer automatically. |
1129 |
If there is a process already running in `*Python*', switch to |
If there is a process already running in `*Python*', switch to |
1130 |
that buffer. Interactively, a prefix arg allows you to edit the initial |
that buffer. Interactively, a prefix arg allows you to edit the initial |
1131 |
command line (default is `python-command'); `-i' etc. args will be added |
command line (default is `python-command'); `-i' etc. args will be added |
1132 |
to this as appropriate. Runs the hook `inferior-python-mode-hook' |
to this as appropriate. Runs the hook `inferior-python-mode-hook' |
1133 |
\(after the `comint-mode-hook' is run). |
\(after the `comint-mode-hook' is run). |
1134 |
\(Type \\[describe-mode] in the process buffer for a list of commands.)" |
\(Type \\[describe-mode] in the process buffer for a list of commands.)" |
1144 |
(let* ((cmdlist (append (python-args-to-list cmd) '("-i"))) |
(let* ((cmdlist (append (python-args-to-list cmd) '("-i"))) |
1145 |
(path (getenv "PYTHONPATH")) |
(path (getenv "PYTHONPATH")) |
1146 |
(process-environment ; to import emacs.py |
(process-environment ; to import emacs.py |
1147 |
(push (concat "PYTHONPATH=" data-directory |
(cons (concat "PYTHONPATH=" data-directory |
1148 |
(if path (concat ":" path))) |
(if path (concat ":" path))) |
1149 |
process-environment))) |
process-environment))) |
1150 |
(set-buffer (apply 'make-comint "Python" (car cmdlist) nil |
(set-buffer (apply 'make-comint "Python" (car cmdlist) nil |
1151 |
(cdr cmdlist))) |
(cdr cmdlist))) |
1152 |
(setq python-buffer "*Python*")) |
(setq python-buffer (buffer-name))) |
1153 |
(inferior-python-mode) |
(inferior-python-mode) |
1154 |
;; Load function defintions we need. |
;; Load function defintions we need. |
1155 |
;; Before the preoutput function was used, this was done via -c in |
;; Before the preoutput function was used, this was done via -c in |
1205 |
(set-marker orig-start (line-beginning-position 0))) |
(set-marker orig-start (line-beginning-position 0))) |
1206 |
(write-region "if True:\n" nil f nil 'nomsg)) |
(write-region "if True:\n" nil f nil 'nomsg)) |
1207 |
(write-region start end f t 'nomsg) |
(write-region start end f t 'nomsg) |
1208 |
(let ((proc (python-proc))) ;Make sure we're running a process. |
(with-current-buffer (process-buffer (python-proc)) ;Runs python if needed. |
1209 |
(with-current-buffer python-buffer |
(python-send-command command) |
1210 |
(python-send-command command) |
;; Tell compile.el to redirect error locations in file `f' to |
1211 |
;; Tell compile.el to redirect error locations in file `f' to |
;; positions past marker `orig-start'. It has to be done *after* |
1212 |
;; positions past marker `orig-start'. It has to be done *after* |
;; python-send-command's call to compilation-forget-errors. |
1213 |
;; python-send-command's call to compilation-forget-errors. |
(compilation-fake-loc orig-start f)))) |
|
(compilation-fake-loc orig-start f))))) |
|
1214 |
|
|
1215 |
(defun python-send-string (string) |
(defun python-send-string (string) |
1216 |
"Evaluate STRING in inferior Python process." |
"Evaluate STRING in inferior Python process." |
1235 |
"Switch to the Python process buffer. |
"Switch to the Python process buffer. |
1236 |
With prefix arg, position cursor at end of buffer." |
With prefix arg, position cursor at end of buffer." |
1237 |
(interactive "P") |
(interactive "P") |
1238 |
(if (get-buffer python-buffer) |
(pop-to-buffer (process-buffer (python-proc))) ;Runs python if needed. |
|
(pop-to-buffer python-buffer) |
|
|
(error "No current process buffer. See variable `python-buffer'")) |
|
1239 |
(when eob-p |
(when eob-p |
1240 |
(push-mark) |
(push-mark) |
1241 |
(goto-char (point-max)))) |
(goto-char (point-max)))) |
1242 |
|
|
|
(add-to-list 'debug-ignored-errors "^No current process buffer.") |
|
|
|
|
1243 |
(defun python-send-region-and-go (start end) |
(defun python-send-region-and-go (start end) |
1244 |
"Send the region to the inferior Python process. |
"Send the region to the inferior Python process. |
1245 |
Then switch to the process buffer." |
Then switch to the process buffer." |
1271 |
(comint-check-source file-name) ; Check to see if buffer needs saving. |
(comint-check-source file-name) ; Check to see if buffer needs saving. |
1272 |
(setq python-prev-dir/file (cons (file-name-directory file-name) |
(setq python-prev-dir/file (cons (file-name-directory file-name) |
1273 |
(file-name-nondirectory file-name))) |
(file-name-nondirectory file-name))) |
1274 |
(let ((proc (python-proc))) ;Make sure we have a process. |
(with-current-buffer (process-buffer (python-proc)) ;Runs python if needed. |
1275 |
(with-current-buffer python-buffer |
;; Fixme: I'm not convinced by this logic from python-mode.el. |
1276 |
;; Fixme: I'm not convinced by this logic from python-mode.el. |
(python-send-command |
1277 |
(python-send-command |
(if (string-match "\\.py\\'" file-name) |
1278 |
(if (string-match "\\.py\\'" file-name) |
(let ((module (file-name-sans-extension |
1279 |
(let ((module (file-name-sans-extension |
(file-name-nondirectory file-name)))) |
1280 |
(file-name-nondirectory file-name)))) |
(format "emacs.eimport(%S,%S)" |
1281 |
(format "emacs.eimport(%S,%S)" |
module (file-name-directory file-name))) |
1282 |
module (file-name-directory file-name))) |
(format "execfile(%S)" file-name))) |
1283 |
(format "execfile(%S)" file-name))) |
(message "%s loaded" file-name))) |
|
(message "%s loaded" file-name)))) |
|
1284 |
|
|
1285 |
;; Fixme: If we need to start the process, wait until we've got the OK |
;; Fixme: If we need to start the process, wait until we've got the OK |
1286 |
;; from the startup. |
;; from the startup. |