Merge lp:~tcaswell-gmail/python-mode/window_fix into lp:python-mode

Proposed by Thomas Caswell
Status: Merged
Approved by: Andreas Roehler
Approved revision: 1165
Merged at revision: 1165
Proposed branch: lp:~tcaswell-gmail/python-mode/window_fix
Merge into: lp:python-mode
Diff against target: 145 lines (+77/-36)
1 file modified
python-mode.el (+77/-36)
To merge this branch: bzr merge lp:~tcaswell-gmail/python-mode/window_fix
Reviewer Review Type Date Requested Status
Andreas Roehler Pending
Review via email: mp+141166@code.launchpad.net

Description of the change

related to bug #1018996

To post a comment you must log in.
Revision history for this message
Andreas Roehler (a-roehler) wrote :

Looks like you spotted the bug with changes of `py-execute-buffer-file' and `py-execute-buffer-finally', congratulations!

As for the remaining stuff, IMO we need some distance to go, it's not at the bottom yet.
Please do me the favor and restrict your patch at the changes mentioned above, so I may merge this branch.

If interested, we may dig a little big deeper afterward into the architecture of this section.
Would welcome if may take it in charge somehow. As a starting-point we may take the pseudo-code mailed by Yaroslav lately. Addressing this and some more rationales might help to choose solutions if issues rise up.

Revision history for this message
Thomas Caswell (tcaswell-gmail) wrote :

On Mon, Dec 24, 2012 at 7:15 AM, Andreas Roehler
<email address hidden> wrote:
> with changes of `py-execute-buffer-file' and `py-execute-buffer-finally'

Here is a patch of just those two lines.

--
Thomas Caswell
<email address hidden>

0From 8ed76840779139ba64d61e6c51f81b8a13e72acc Mon Sep 17 00:00:00 20010From 8ed76840779139ba64d61e6c51f81b8a13e72acc Mon Sep 17 00:00:00 2001
1From: Thomas A Caswell <tcaswell@uchicago.edu>1From: Thomas A Caswell <tcaswell@uchicago.edu>
2Date: Mon, 24 Dec 2012 11:57:59 -06002Date: Mon, 24 Dec 2012 11:57:59 -0600
3Subject: [PATCH] fixed bug with changes of `py-execute-buffer-file' and3Subject: [PATCH] fixed bug with changes of `py-execute-buffer-file' and
4`py-execute-buffer-finally'4`py-execute-buffer-finally'
55
6--
7python-mode.el | 2 --6python-mode.el | 2 --
81 file changed, 2 deletions(-)71 file changed, 2 deletions(-)
98
diff --git a/python-mode.el b/python-mode.el
index 56cca75..82c3f08 100644
--- a/python-mode.el
+++ b/python-mode.el
@@ -8725,7 +8725,6 @@ When called from a programm, it accepts a string specifying a shell which will b
8725 (kill-buffer localname)))8725 (kill-buffer localname)))
87268726
8727(defun py-execute-buffer-file (start end pyshellname dedicated switch nostars sepchar split file)8727(defun py-execute-buffer-file (start end pyshellname dedicated switch nostars sepchar split file)
8728 (delete-other-windows)
8729 (let* ((oldbuf (current-buffer))8728 (let* ((oldbuf (current-buffer))
8730 (py-exception-buffer oldbuf)8729 (py-exception-buffer oldbuf)
8731 (pyshellname (or pyshellname (py-choose-shell)))8730 (pyshellname (or pyshellname (py-choose-shell)))
@@ -8772,7 +8771,6 @@ When called from a programm, it accepts a string specifying a shell which will b
8772 erg))8771 erg))
87738772
8774(defun py-execute-buffer-finally (start end &optional pyshellname dedicated switch nostars sepchar split)8773(defun py-execute-buffer-finally (start end &optional pyshellname dedicated switch nostars sepchar split)
8775 (delete-other-windows)
8776 (let* ((oldbuf (current-buffer))8774 (let* ((oldbuf (current-buffer))
8777 (pyshellname (or pyshellname (py-choose-shell)))8775 (pyshellname (or pyshellname (py-choose-shell)))
8778 (execute-directory8776 (execute-directory
8779-
87801.8.0.287771.8.0.2
Revision history for this message
Andreas Roehler (a-roehler) wrote :

Am 24.12.2012 19:04, schrieb Thomas Caswell:
> On Mon, Dec 24, 2012 at 7:15 AM, Andreas Roehler
> <email address hidden> wrote:
>> with changes of `py-execute-buffer-file' and `py-execute-buffer-finally'
>
>
> Here is a patch of just those two lines.
>
>
> --
> Thomas Caswell
> <email address hidden>
>

Thanks. May you still upload it? Otherwise your branch remains with a merge-proposal without merge...

Cheers,

Andreas

Revision history for this message
Andreas Roehler (a-roehler) wrote :

ah, okay, you did it already. So I'm going to merge.

Revision history for this message
Andreas Roehler (a-roehler) wrote :

hmmn, was just your patch displayed.

Revision history for this message
Thomas Caswell (tcaswell-gmail) wrote :
Download full text (3.6 KiB)

I would re-arrange the psudo-code of Yaroslav a bit to include the
limit on the number of windows that can be made. It also deals with
the case where switching and splitting are both off, and there is more
than one window, the user may want the execution buffer to be
displayed in one of the other windows.

Consider the case where switching is off, splitting is on, there are 2
windows neither of which are showing *Python*, and
py-max-split-windows = 2 (which I think is equivalent to splitting
being turned off). What we want is this case is to put *Python* in the
window that our code is not in. This generalizes to more than 2 windows
directly by selecting the least recently used window.

(I wrote the psudo-code like python because I assume everyone
interested in this mode can read python. )

if py-manage-buffers-on-execute-p:
    # we want to make sure the user can see the output
    if *Python* buffer is visible:
        # we can already see *Python*, thus we are done
        p_win = window *Python* is in
        pass
    else:
        # we need to try and make *Python* visible
        if py-split-windows-on-execute-p and count-windows < py-max-split-windows:
            # we can make a new window
            do the split and put *Python* in the new window
            p_win = new window
        else:
            if count-windows > 1:
                seleect some other window and put *Python* in it
                p_win = window *Python* was put in
            else:
                # this is the only branch where *Python* is not visible at the end
                p_win = None
                do nothing

    if py-switch-buffers-on-execute-p:
        if p_win:
            switch focus to p_win
        else:
            # this is the only branch that replaces the code window with the output
            switch the buffer displayed in the current window, replacing the code
else:
    # DO NOTHING
    pass

To deal with the use case where the user wants to always replace
the code window with the output (with what I think is a reasonable
exception of *Python* being already visible, in which case it behaves
like switching is turned on and splitting is turned off), we need to
add another flag. If this flag (py-replace-code-on-execute-p) is set,
the values of the split and switch flag are irrelevant.

This logic is not in the lisp yet.

if py-manage-buffers-on-execute-p:
    # we want to make sure the user can see the output
    if *Python* buffer is visible:
        # we can already see *Python*, thus we are done
        p_win = window *Python* is in
        pass
    else:
        if py-replace-code-on-execute-p:
            p_win = None
        # we need to try and make *Python* visible
        elif py-split-windows-on-execute-p and count-windows < py-max-split-windows:
            # we can make a new window
            do the split and put *Python* in the new window
            p_win = new window
        else:
            if count-windows > 1:
                seleect some other window and put *Python* in it
                p_win = window *Python* was put in
            else:
                # this is the only branch w...

Read more...

Revision history for this message
Andreas Roehler (a-roehler) wrote :
Download full text (4.8 KiB)

Am 30.12.2012 21:36, schrieb Thomas Caswell:
> I would re-arrange the psudo-code of Yaroslav a bit

Great, was meant just as a starting point.

  to include the
> limit on the number of windows that can be made. It also deals with
> the case where switching and splitting are both off, and there is more
> than one window, the user may want the execution buffer to be
> displayed in one of the other windows.
>
> Consider the case where switching is off, splitting is on, there are 2
> windows neither of which are showing *Python*, and
> py-max-split-windows = 2 (which I think is equivalent to splitting
> being turned off).

IMO that remark is misleading, as the way Emacs will create the final window configuration might be complex.
Temporary buffers and files might be created and so on. Think the final window state somehow independent from the
start configuration.

   What we want is this case is to put *Python* in the
> window that our code is not in. This generalizes to more than 2 windows
> directly by selecting the least recently used window.
>

See remark above. Such like "recently used" probably is not enough.

> (I wrote the psudo-code like python because I assume everyone
> interested in this mode can read python. )
>
>
> if py-manage-buffers-on-execute-p:

We don't need this boolean, because some management is needed in any case -
unless we want unpredicted results.

> # we want to make sure the user can see the output
> if *Python* buffer is visible:
> # we can already see *Python*, thus we are done

That's not sure user may want to see the output.
For example when running serialised stuff from a programm.

Basically the logic is:

Start window config
Unpredictable changes from point of the first
Desired final window config

Remains the question if 'switch and 'split are sufficient to describe the desired final config.

Probably not, also when thinking at use in org-babel.

> p_win = window *Python* is in
> pass
> else:
> # we need to try and make *Python* visible
> if py-split-windows-on-execute-p and count-windows < py-max-split-windows:
> # we can make a new window
> do the split and put *Python* in the new window
> p_win = new window
> else:
> if count-windows > 1:
> seleect some other window and put *Python* in it
> p_win = window *Python* was put in
> else:
> # this is the only branch where *Python* is not visible at the end
> p_win = None
> do nothing
>
> if py-switch-buffers-on-execute-p:
> if p_win:
> switch focus to p_win
> else:
> # this is the only branch that replaces the code window with the output
> switch the buffer displayed in the current window, replacing the code

the last is another possible option not implemented yet.

> else:
> # DO NOTHING
> pass
>
> To deal with the use case where the user wants to always replace
> the code window with the output (with what I think is a reasonable
> exception of *Python* being already visi...

Read more...

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'python-mode.el'
--- python-mode.el 2012-12-21 18:27:07 +0000
+++ python-mode.el 2012-12-22 21:11:20 +0000
@@ -710,6 +710,12 @@
710 :type 'boolean710 :type 'boolean
711 :group 'python-mode)711 :group 'python-mode)
712712
713(defcustom py-on-execute-buffer-management-p t
714 "If emacs should attempt to manage your buffers on executing python code."
715 :type 'boolean
716 :group 'python-mode
717)
718
713(defcustom py-switch-buffers-on-execute-p nil719(defcustom py-switch-buffers-on-execute-p nil
714 "When non-nil switch to the Python output buffer. "720 "When non-nil switch to the Python output buffer. "
715721
@@ -8757,7 +8763,6 @@
8757 (kill-buffer localname)))8763 (kill-buffer localname)))
87588764
8759(defun py-execute-buffer-file (start end pyshellname dedicated switch nostars sepchar split file)8765(defun py-execute-buffer-file (start end pyshellname dedicated switch nostars sepchar split file)
8760 (delete-other-windows)
8761 (let* ((oldbuf (current-buffer))8766 (let* ((oldbuf (current-buffer))
8762 (py-exception-buffer oldbuf)8767 (py-exception-buffer oldbuf)
8763 (pyshellname (or pyshellname (py-choose-shell)))8768 (pyshellname (or pyshellname (py-choose-shell)))
@@ -8804,7 +8809,6 @@
8804 erg))8809 erg))
88058810
8806(defun py-execute-buffer-finally (start end &optional pyshellname dedicated switch nostars sepchar split)8811(defun py-execute-buffer-finally (start end &optional pyshellname dedicated switch nostars sepchar split)
8807 (delete-other-windows)
8808 (let* ((oldbuf (current-buffer))8812 (let* ((oldbuf (current-buffer))
8809 (pyshellname (or pyshellname (py-choose-shell)))8813 (pyshellname (or pyshellname (py-choose-shell)))
8810 (execute-directory8814 (execute-directory
@@ -10492,41 +10496,78 @@
10492 "\*" ""10496 "\*" ""
10493 string)))10497 string)))
1049410498
10499(defun py-try-make-output-buffer-visible (split py-buffer-name)
10500 "A helper function that tries to make the output buffer is visible.
10501
10502If it is already visible it return the window it is in
10503
10504If it is not visible and splitting is allowed and there are less
10505that the maximum number of windows, then it will split the code window,
10506put the output buffer in the new window and return the new window
10507
10508If there is more than one window and splitting is not allowed, then
10509put the output buffer in the least recently used window and return that window
10510
10511if there is only one window and splitting in not allowed return nil
10512"
10513 (let ((cur-py-window (get-buffer-window py-buffer-name t)))
10514 (if cur-py-window
10515 ;; There is a currently visible window with the output buffer in it use that one
10516 cur-py-window
10517 ;; if the output buffer isn't already visible, try to make it so
10518
10519 ;;we have not explicitly said not to split, we are allowed to split, and
10520 ;; there are few enough windows
10521 (if (and (not (eq split 'nosplit))
10522 py-split-windows-on-execute-p
10523 (< (count-windows) py-max-split-windows)
10524 )
10525 (let ((new-py-wind (funcall py-split-windows-on-execute-function)))
10526 (set-window-buffer new-py-wind py-buffer-name)
10527 new-py-wind
10528 )
10529
10530 (if (eq 1 (count-windows))
10531 nil
10532 ;; else make the lru window show the output buffer and return it
10533 (let ((new-py-wind (get-lru-window)))
10534 (set-window-buffer new-py-wind py-buffer-name)
10535 new-py-wind
10536 )
10537 )
10538 )
10539 )
10540 )
10541 )
10542
10495(defun py-shell-manage-windows (switch split oldbuf py-buffer-name)10543(defun py-shell-manage-windows (switch split oldbuf py-buffer-name)
10496 (cond (;; split and switch10544 (if py-on-execute-buffer-management-p
10497 (and (not (eq split 'nosplit))10545 ;;if we are going to let python mode touch our buffers under any conditions
10498 py-split-windows-on-execute-p10546 (let ((cur-py-window (py-try-make-output-buffer-visible split py-buffer-name)))
10499 (not (eq switch 'noswitch))10547 ;; if we are switching
10500 (or (eq switch 'switch)10548 (when
10501 py-switch-buffers-on-execute-p))10549 ;;this logic seems overly twisted
10502 (when (< (count-windows) py-max-split-windows)10550 (and
10503 (funcall py-split-windows-on-execute-function))10551 (not (eq switch 'noswitch))
10504 (pop-to-buffer py-buffer-name)10552 (or (eq switch 'switch)
10505 (display-buffer oldbuf))10553 py-switch-buffers-on-execute-p))
10506 ;; split, not switch10554 (if cur-py-window
10507 ((and10555 ;; if we have a visible window with the output buffer, switch to it
10508 (not (eq split 'nosplit))10556 (select-window cur-py-window)
10509 py-split-windows-on-execute-p10557 ;; if we don't then make the current window display the output (case of 1 window, switch, nosplit)
10510 (or (eq switch 'noswitch)10558 (switch-to-buffer py-buffer-name)
10511 (not (eq switch 'switch))))10559 )
10512 (if (< (count-windows) py-max-split-windows)10560
10513 (progn10561 )
10514 (funcall py-split-windows-on-execute-function)10562 )
10515 (display-buffer py-buffer-name 'display-buffer-reuse-window))10563 ;;else, return it to the state it was in
10516 (display-buffer py-buffer-name 'display-buffer-reuse-window))10564 (let (pop-up-windows)
10517 (pop-to-buffer oldbuf))10565 (set-buffer oldbuf)
10518 ;; no split, switch10566 (switch-to-buffer (current-buffer))
10519 ((or (eq switch 'switch)10567 )
10520 (and (not (eq switch 'noswitch))10568 )
10521 py-switch-buffers-on-execute-p))10569 )
10522 (let (pop-up-windows)10570
10523 (pop-to-buffer py-buffer-name)))
10524 ;; no split, no switch
10525 ((or (eq switch 'noswitch)
10526 (not py-switch-buffers-on-execute-p))
10527 (let (pop-up-windows)
10528 (set-buffer oldbuf)
10529 (switch-to-buffer (current-buffer))))))
1053010571
10531(defun py-report-executable (py-buffer-name)10572(defun py-report-executable (py-buffer-name)
10532 (let ((erg (downcase (replace-regexp-in-string10573 (let ((erg (downcase (replace-regexp-in-string

Subscribers

People subscribed via source and target branches

to status/vote changes: