The mpc#DisplayPlaylist code that we just copied from the original OpenMPC opens a new window, and then loses track of it. The next time the function is called, it opens another window.
To see this in action, open the editor and then start the plugin with :call OpenMPC(). Run that command a few times: you’ll see a new window open every time the function calls mpc#DisplayPlaylist. The code doesn’t check for or switch to any window that’s already opened to our playlist.
Let’s remedy this. We’ll change mpc#DisplayPlaylist so that if we already have a playlist window open when we call OpenMPC, we switch to the open playlist from the current window. Because the job of mpc#DisplayPlaylist is only to display the playlist, we’ll also move the window-managing code back to OpenMPC.
Modify OpenMPC to look like this:
| function! OpenMPC() |
| if(bufexists('mpc.mpdv')) |
| let mpcwin = bufwinnr('mpc.mpdv') |
| if(mpcwin == -1) |
| execute "sbuffer " . bufnr('mpc.mpdv') |
| else |
| execute mpcwin . 'wincmd w' |
| return |
| endif |
| else |
| execute "new mpc.mpdv" |
| endif |
| call mpc#DisplayPlaylist() |
| endfunction |
We’ll go over this one piece at a time, but before we do, let’s try running :call OpenMPC() a few times again. After each call, switch to the window you were in before. Once you’ve called the function one time, you should see Vim switch focus back and forth, from that window to the playlist window it opened the first time.
Now we have a specific window for our function to use. Let’s look at our code again to see how we’re handling that window.
18.219.142.20