Python mode - `C-c |' feature suggestion
François Pinard
pinard at iro.umontreal.ca
Tue Feb 22 14:39:26 EST 2000
python-mode at python.org writes:
> <pinard at iro.umontreal.ca> writes:
> > My suggestion is that `C-c |' be made clever enough to discover
> > the left margin of the region under consideration, and that it
> > removes that margin while transmitting the region to the Python
> > interpreter. That would allow for using that command in a much
> > wider variety of (smaller :-) contexts.
> I've also wanted this for a while, but never had the time to add it.
> I'd accept a patch that implemented this feature.
OK, here is a cut at it. Quick testing seems to show that it works,
but I did not drive extensive tests.
I also took this opportunity for another little change. With it, a
temporary file is not created when it is not going to be needed.
--- python-mode.el 2000/02/22 17:49:11 1.1
+++ python-mode.el 2000/02/22 19:35:02
@@ -1278,11 +1278,34 @@
(format "python-%d-%d" sn pid)
(format "python-%d" sn)))
(make-temp-name "python-")))
- (file (expand-file-name temp py-temp-directory)))
- (write-region start end file nil 'nomsg)
+ (file (expand-file-name temp py-temp-directory))
+ input)
+ (save-excursion
+ (let ((margin -1))
+ (goto-char start)
+ (while (and (not (zerop margin)) (< (point) end))
+ (skip-chars-forward " \t")
+ (let ((column (current-column)))
+ (and (not (= (following-char) ?\n))
+ (or (< margin 0) (< column margin))
+ (setq margin column)))
+ (forward-line 1))
+ (if (> margin 0)
+ (let ((buffer (current-buffer)))
+ (setq input (get-buffer-create
+ (generate-new-buffer-name " *Python Input*")))
+ (set-buffer input)
+ (insert-buffer-substring buffer start end)
+ (indent-rigidly (point-min) (point-max) (- margin))))))
(cond
;; always run the code in its own asynchronous subprocess
(async
+ (if (not input)
+ (write-region start end file nil 'nomsg)
+ (save-excursion
+ (set-buffer input)
+ (write-region (point-min) (point-max) file nil 'nomsg))
+ (kill-buffer input))
(let* ((buf (generate-new-buffer-name py-output-buffer))
;; TBD: a horrible hack, but why create new Custom variables?
(arg (if (string-equal py-which-bufname "Python")
@@ -1295,18 +1318,30 @@
;; execution there.
(proc
;; use the existing python shell
+ (if (not input)
+ (write-region start end file nil 'nomsg)
+ (save-excursion
+ (set-buffer input)
+ (write-region (point-min) (point-max) file nil 'nomsg))
+ (kill-buffer input))
(if (not py-file-queue)
(py-execute-file proc file)
(message "File %s queued for execution" file))
(setq py-file-queue (append py-file-queue (list file)))
(setq py-exception-buffer (cons file (current-buffer))))
(t
- ;; TBD: a horrible hack, buy why create new Custom variables?
+ ;; TBD: a horrible hack, but why create new Custom variables?
(let ((cmd (concat py-which-shell
(if (string-equal py-which-bufname "JPython")
" -" ""))))
;; otherwise either run it synchronously in a subprocess
+ (if (not input)
(shell-command-on-region start end cmd py-output-buffer)
+ (save-excursion
+ (set-buffer input)
+ (shell-command-on-region (point-min) (point-max) cmd
+ py-output-buffer))
+ (kill-buffer input))
;; shell-command-on-region kills the output buffer if it never
;; existed and there's no output from the command
(if (not (get-buffer py-output-buffer))
--
François Pinard http://www.iro.umontreal.ca/~pinard
More information about the Python-list
mailing list