comparison lisp/progmodes/python.el @ 55240:780b94f913fe

(python-send-command): New fun. (python-send-region, python-load-file): Use it.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Thu, 29 Apr 2004 20:34:11 +0000
parents ee7e5daa7ffd
children 2db456741f80
comparison
equal deleted inserted replaced
55239:1af59a842b93 55240:780b94f913fe
1139 print '_emacs_out', func.__name__+inspect.formatargspec (args, varargs, varkw, defaults) 1139 print '_emacs_out', func.__name__+inspect.formatargspec (args, varargs, varkw, defaults)
1140 1140
1141 print '_emacs_ok'")) 1141 print '_emacs_ok'"))
1142 (unless noshow (pop-to-buffer (setq python-buffer "*Python*")))) 1142 (unless noshow (pop-to-buffer (setq python-buffer "*Python*"))))
1143 1143
1144 (defun python-send-command (command)
1145 "Like `python-send-string' but resets `compilation-minor-mode'."
1146 (let ((end (marker-position (process-mark (python-proc)))))
1147 (compilation-forget-errors)
1148 (python-send-string command)
1149 (set-marker compilation-parsing-end end)
1150 (setq compilation-last-buffer (current-buffer))))
1151
1144 (defun python-send-region (start end) 1152 (defun python-send-region (start end)
1145 "Send the region to the inferior Python process." 1153 "Send the region to the inferior Python process."
1146 ;; The region is evaluated from a temporary file. This avoids 1154 ;; The region is evaluated from a temporary file. This avoids
1147 ;; problems with blank lines, which have different semantics 1155 ;; problems with blank lines, which have different semantics
1148 ;; interactively and in files. It also saves the inferior process 1156 ;; interactively and in files. It also saves the inferior process
1168 (/= 0 (current-indentation))) ; need dummy block 1176 (/= 0 (current-indentation))) ; need dummy block
1169 (write-region "if True:\n" nil f nil 'nomsg)) 1177 (write-region "if True:\n" nil f nil 'nomsg))
1170 (write-region start end f t 'nomsg) 1178 (write-region start end f t 'nomsg)
1171 (when python-buffer 1179 (when python-buffer
1172 (with-current-buffer python-buffer 1180 (with-current-buffer python-buffer
1173 (let ((end (marker-position (process-mark (python-proc))))) 1181 (set (make-local-variable 'python-orig-start) orig-start)
1174 (set (make-local-variable 'python-orig-start) orig-start) 1182 (let ((comint-input-filter-functions
1175 (set (make-local-variable 'compilation-error-list) nil) 1183 ;; Don't reset python-orig-start.
1176 (let ((comint-input-filter-functions 1184 (remq 'python-input-filter comint-input-filter-functions)))
1177 (delete 'python-input-filter comint-input-filter-functions))) 1185 (python-send-command command))))))
1178 (python-send-string command))
1179 (set-marker compilation-parsing-end end)
1180 (setq compilation-last-buffer (current-buffer)))))))
1181 1186
1182 (defun python-send-string (string) 1187 (defun python-send-string (string)
1183 "Evaluate STRING in inferior Python process." 1188 "Evaluate STRING in inferior Python process."
1184 (interactive "sPython command: ") 1189 (interactive "sPython command: ")
1185 (comint-send-string (python-proc) string) 1190 (comint-send-string (python-proc) string)
1240 (comint-check-source file-name) ; Check to see if buffer needs saved. 1245 (comint-check-source file-name) ; Check to see if buffer needs saved.
1241 (setq python-prev-dir/file (cons (file-name-directory file-name) 1246 (setq python-prev-dir/file (cons (file-name-directory file-name)
1242 (file-name-nondirectory file-name))) 1247 (file-name-nondirectory file-name)))
1243 (when python-buffer 1248 (when python-buffer
1244 (with-current-buffer python-buffer 1249 (with-current-buffer python-buffer
1245 (let ((end (marker-position (process-mark (python-proc))))) 1250 ;; Fixme: I'm not convinced by this logic from python-mode.el.
1246 (set (make-local-variable 'compilation-error-list) nil) 1251 (python-send-command
1247 ;; (set (make-local-variable 'compilation-old-error-list) nil) 1252 (if (string-match "\\.py\\'" file-name)
1248 (let ((comint-input-filter-functions 1253 ;; Fixme: make sure the directory is in the path list
1249 (delete 'python-input-filter comint-input-filter-functions))) 1254 (let ((module (file-name-sans-extension
1250 (set (make-local-variable 'python-orig-start) nil) 1255 (file-name-nondirectory file-name))))
1251 ;; Fixme: I'm not convinced by this logic from python-mode.el. 1256 (format "\
1252 (python-send-string
1253 (if (string-match "\\.py\\'" file-name)
1254 ;; Fixme: make sure the directory is in the path list
1255 (let ((module (file-name-sans-extension
1256 (file-name-nondirectory file-name))))
1257 (format "\
1258 if globals().has_key(%S): reload(%s) 1257 if globals().has_key(%S): reload(%s)
1259 else: import %s 1258 else: import %s
1260 " module module module)) 1259 " module module module))
1261 (format "execfile('%s')" file-name)))) 1260 (format "execfile('%s')" file-name))))))
1262 (set-marker compilation-parsing-end end)
1263 (setq compilation-last-buffer (current-buffer))))))
1264 1261
1265 ;; Fixme: Should this start a process if there isn't one? (Unlike cmuscheme.) 1262 ;; Fixme: Should this start a process if there isn't one? (Unlike cmuscheme.)
1266 (defun python-proc () 1263 (defun python-proc ()
1267 "Return the current Python process. See variable `python-buffer'." 1264 "Return the current Python process. See variable `python-buffer'."
1268 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-python-mode) 1265 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-python-mode)