Retrieving the Text of a Line

We now have the playlist displayed in a window we can easily open. Let’s add the ability to play a specific song chosen by the user.

We’re going to create a new function, mpc#PlaySong, within our autoload directory. It will take the number of a song in the playlist—what mpc refers to as the song’s position—and send that number back to mpc using system. Add the following code to autoload/mpc.vim:

autoload.1/mpc/autoload/mpc.vim
Line 1 
function​! mpc#PlaySong(​no​)
let​ song = ​split​(getline(a:​no​), ​" "​)
let​ results = ​split​(system(​"mpc --format '%title% (%artist%)' play "
. song[0]), ​" "​)
let​ message = ​'[mpc] NOW PLAYING: '​ . results[0]
echomsg​ message
endfunction

There is a fair bit happening here. mpc#PlaySong takes one argument, no, which actually represents a line in the playlist buffer. Vim’s function getline takes a number and returns the contents of the line by that number. On line 2 we call getline on no and then split the resulting line contents into a List, with items delimited by spaces. We assign that List to the variable song.

To make this clearer, let’s look at an example from the playlist window we saw in the previous chapter. Here it is:

images/PlaylistView01.png

The last track in the window is number 10 in the playlist, so it has the position 10. If we were to call mpc#PlaySong(10), this would be the song we would get:

 
[​'10'​, ​'Wayne'​, ​'Watson'​, ​'/'​, ​'The'​, ​'Very'​, ​'Best'​, ​'/'​, ​'Home'​, ​'Free'​]

And as you can see, this also gets the item separators, /. No matter—the important thing is the first item in the List: the position (10).

So next, on line 3, we use system to call mpc play, passing it that first item in the song List, the position. We call split again, this time on the output we get from mpc when we play the song, and assign that—another List—to the variable results. So results is a List of mpc’s output, broken up by new lines. We finish on lines 5 and 6 by echoing a “now playing” message, which contains the first item (or line) of results, to the user.

Try it out. Open Vim and run :call OpenMPC(). Then run :call mpc#PlaySong(3). If MPD is running, you should hear the third track in the playlist starting up and see something like what’s shown in the figure below.

images/PlaySelected01.png

No, calling the function manually isn’t really user-friendly. In Chapter 6, Commands and Mappings, we’ll see how we can bind that function call to our own key mapping.

As we continue with our plugin project, you’ll find the autoload directory is a useful place to put most of the code. The next area of VimL you’ll learn about is filetypes, including the use of the ftdetect and ftplugin directories, which allow you to add support for filetypes Vim doesn’t support by default. In the next chapter we’ll see how we can have our plugin recognize and add behavior based on filetypes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.156.202