From 94d027bf898ecc432fce1411d3ab20495b0267f5 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sat, 22 Dec 2012 14:39:51 -0600 Subject: [PATCH] fixed window management --- python-mode.el | 114 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 78 insertions(+), 36 deletions(-) diff --git a/python-mode.el b/python-mode.el index 56cca75..d5f2dc6 100644 --- a/python-mode.el +++ b/python-mode.el @@ -710,6 +710,12 @@ See also `py-execute-directory'" :type 'boolean :group 'python-mode) +(defcustom py-on-execute-buffer-management-p t + "If emacs should attempt to manage your buffers on executing python code." + :type 'boolean + :group 'python-mode +) + (defcustom py-switch-buffers-on-execute-p nil "When non-nil switch to the Python output buffer. " @@ -1275,6 +1281,7 @@ can write into: the value (if any) of the environment variable TMPDIR, (defvar python-mode-v5-behavior nil) + (defvar py-bol-forms-last-indent nil "For internal use. Stores indent from last py-end-of-FORM-bol command. When this-command is py-beginning-of-FORM-bol, last-command's indent will be considered in order to jump onto right beginning position.") @@ -8725,7 +8732,6 @@ When called from a programm, it accepts a string specifying a shell which will b (kill-buffer localname))) (defun py-execute-buffer-file (start end pyshellname dedicated switch nostars sepchar split file) - (delete-other-windows) (let* ((oldbuf (current-buffer)) (py-exception-buffer oldbuf) (pyshellname (or pyshellname (py-choose-shell))) @@ -8772,7 +8778,6 @@ When called from a programm, it accepts a string specifying a shell which will b erg)) (defun py-execute-buffer-finally (start end &optional pyshellname dedicated switch nostars sepchar split) - (delete-other-windows) (let* ((oldbuf (current-buffer)) (pyshellname (or pyshellname (py-choose-shell))) (execute-directory @@ -10460,41 +10465,78 @@ Needed when file-path names are contructed from maybe numbered buffer names like "\*" "" string))) +(defun py-try-make-output-buffer-visible (split py-buffer-name) + "A helper function that tries to make the output buffer is visible. + +If it is already visible it return the window it is in + +If it is not visible and splitting is allowed and there are less +that the maximum number of windows, then it will split the code window, +put the output buffer in the new window and return the new window + +If there is more than one window and splitting is not allowed, then +put the output buffer in the least recently used window and return that window + +if there is only one window and splitting in not allowed return nil +" + (let ((cur-py-window (get-buffer-window py-buffer-name t))) + (if cur-py-window + ;; There is a currently visible window with the output buffer in it use that one + cur-py-window + ;; if the output buffer isn't already visible, try to make it so + + ;;we have not explicitly said not to split, we are allowed to split, and + ;; there are few enough windows + (if (and (not (eq split 'nosplit)) + py-split-windows-on-execute-p + (< (count-windows) py-max-split-windows) + ) + (let ((new-py-wind (funcall py-split-windows-on-execute-function))) + (set-window-buffer new-py-wind py-buffer-name) + new-py-wind + ) + + (if (eq 1 (count-windows)) + nil + ;; else make the lru window show the output buffer and return it + (let ((new-py-wind (get-lru-window))) + (set-window-buffer new-py-wind py-buffer-name) + new-py-wind + ) + ) + ) + ) + ) + ) + (defun py-shell-manage-windows (switch split oldbuf py-buffer-name) - (cond (;; split and switch - (and (not (eq split 'nosplit)) - py-split-windows-on-execute-p - (not (eq switch 'noswitch)) - (or (eq switch 'switch) - py-switch-buffers-on-execute-p)) - (when (< (count-windows) py-max-split-windows) - (funcall py-split-windows-on-execute-function)) - (pop-to-buffer py-buffer-name) - (display-buffer oldbuf)) - ;; split, not switch - ((and - (not (eq split 'nosplit)) - py-split-windows-on-execute-p - (or (eq switch 'noswitch) - (not (eq switch 'switch)))) - (if (< (count-windows) py-max-split-windows) - (progn - (funcall py-split-windows-on-execute-function) - (display-buffer py-buffer-name 'display-buffer-reuse-window)) - (display-buffer py-buffer-name 'display-buffer-reuse-window)) - (pop-to-buffer oldbuf)) - ;; no split, switch - ((or (eq switch 'switch) - (and (not (eq switch 'noswitch)) - py-switch-buffers-on-execute-p)) - (let (pop-up-windows) - (pop-to-buffer py-buffer-name))) - ;; no split, no switch - ((or (eq switch 'noswitch) - (not py-switch-buffers-on-execute-p)) - (let (pop-up-windows) - (set-buffer oldbuf) - (switch-to-buffer (current-buffer)))))) + (if py-on-execute-buffer-management-p + ;;if we are going to let python mode touch our buffers under any conditions + (let ((cur-py-window (py-try-make-output-buffer-visible split py-buffer-name))) + ;; if we are switching + (when + ;;this logic seems overly twisted + (and + (not (eq switch 'noswitch)) + (or (eq switch 'switch) + py-switch-buffers-on-execute-p)) + (if cur-py-window + ;; if we have a visible window with the output buffer, switch to it + (select-window cur-py-window) + ;; if we don't then make the current window display the output (case of 1 window, switch, nosplit) + (switch-to-buffer py-buffer-name) + ) + + ) + ) + ;;else, return it to the state it was in + (let (pop-up-windows) + (set-buffer oldbuf) + (switch-to-buffer (current-buffer)) + ) + ) + ) + (defun py-report-executable (py-buffer-name) (let ((erg (downcase (replace-regexp-in-string -- 1.8.0.2