By Developers, For Developers

Historical errata for The VimL Primer

PDF PgPaper PgTypeDescriptionFixed onComments
19.0 TYPO

​function​! EchoQuote(): the epub version shows a space between the word function and the bang. If you type it in as it appears, it wont work; however, the downloaded code does work.

Not sure what I can do about this; the code in the book is pulled directly from the downloadable code. This sounds like an EPUB rendering error.
epubSUGGEST

Chapter 1, section “Functions, Types, and Variables”:

No explanation is given on how to call the function after sourcing the file. Before the output from the first version of EchoQuote, show:
:call EchoQuote()

2015-01-28
epubSUGGEST

Chapter 1, section “Functions, Types, and Variables”
paragraph starting: “In the last line, we use the :call”

First sentance should go above, where first appearance of “call
EchoQuote” appears. As written, the second sentence (“At the end of the
function…”) is out of order, since it refers to the third-before-last
line.

2015-01-28
epubERROR

Chapter 1, section “Funcref”

If someone has been following along implementing the functions in order, this example will not work (the current version of the function requires arguments). A correct example would be:
:call Example(“Need MOAR editing!”, 2015, ‘A. Reader’)

The downloadable source is also incorrect, but could be fixed by redefining the proper version of the function at the start of the file.

2015-01-28
epubERROR

Chapter 1, section “funcref”, 2nd example

Text (and source file) defines variable ‘q’, but invokes function using variable ‘quote’ as the argument.

2015-01-28
49ERROR

mpc#Decode is never called anywhere and so the syntax markers when running call mpc#OpenMPC() are visible.

Good catch. On the off chance that you’re using the system Vim in OS X Yosemite, though, I think that could have something to do with the characters being displayed; I am, and the conceal system is also broken for me in the help files.
6TYPO

Mid-page, “function(‘EchoQuote’)”, the output of “echo string(Example)”, should be preceded with the double quote/arrow construct.

2015-01-28
9ERROR

In “Iterating with for” section, it states that a for loop can be used with a List or Dictionary. However, a for loop can only be used with a List or a List of Lists.

2015-01-28
11TYPO

On the fifth line from the top, =? should be ?.

2015-01-28
17TYPO

The line starting “E730: …” should be preceded with a double quote/arrow.

2015-01-28
24TYPO

On the 16th line, “…following hte path…” should be “…following the path…”.

2015-01-28
34TYPO

On line 19, “))” should be “)”.

2015-01-28
epubTYPO

Section ’Specifying a New Syntax“, towards the end (page 124 of 164 on iPad mini). ”Open the syntax file we created earlier, syntax/mpdv.vim…the code for the script does not have a contains clause as suggested in “… and then the key bit in these commands is the argument contains.”

2015-01-28
epubERROR

At the end of section “Our Project: An Interface for mpc”, (p 43 of 164), the final command is incorrect. “After doing that, run this:
mpc ls | mpc add”
Actually, you have to run
mpc update
before mpc ls will give you anything.

2015-01-28
11SUGGEST

The multi-sentences here are a bit clumsy.

… using function and endfunction, but our functions must begin with uppercase letters in order to distinguish them from built-in functions.

2015-01-28
12ERROR

“When we call the command, Vim prints the function’s output as a message:”

No it doesn’t. A) There’s no command (did you have one previously but deleted it?) and B) sourcing the file wouldn’t run it anyway. I think what you may have intended was:

:w | source % | call EchoQuote()

2015-01-28
13TYPO

Your comma is in the wrong spot. Should be after ‘written’

What we saw in our last function, where our variables were written a:quote, was the argument scope

2015-01-28
13TYPO

I wouldn’t call an l:scope variable a ‘function scoped’ variable. It’s confusing… the ‘l’ means local and will be easier to remember if you call it that. If you want, I would say, “A variable scoped locally to the function”.

2015-01-28
14SUGGEST

Before moving on from scopes, you might want to mention the other scopes, b: s: w: t: and v:. At least give them :help internal-variables. I use s: a fair bit, and v: is something they’re going to encounter at some point.

2015-01-28
16TYPO

You either need ‘let quote = …’ or ‘echo call(Example, [q, …’

2015-01-28
24TYPO

…before the function’s name is either the directory under autoload or
the filename, and that the last name is the filename

Should be: … ‘last name is the function name’

26TYPO

I believe you mean “Make sure that mpd is running” instead of ‘mpc’

29TYPO

(if creative mapping)

should be

(if creative) mapping

38TYPO

“No, calling the” should be “Now, calling the”

40TYPO

“This one checks for a particular option” should be “This one sets a particular option”

54SUGGEST

Manual padding of spaces is a bit hacky; Vim has printf for this. Why not just create the format string by which you format every line?

As well, looping over the same data structure twice is verbose and inefficient. You could easily build up what you want with one iteration.

And finally, you don’t actually need a “sort” to find the largest length… just set a value using a ternary operator and set the new max if it’s actually bigger than the existing max. I get, though, that you get to show how to use higher order functions if you use sort, and that might be a good reason to keep it, but you might want to show that it’s not actually necessary.

Oh, and one other thing; my data from mpc doesn’t always give me a row for which a split(item, " @") actually yields 4 elements, which makes the `let [position, artist, album, title] = song` fail. I can fix that up, but because we iterate over it twice, I have to fix it twice.

54SUGGEST

Here’s what I’ve written up to replace the GetPlayList() function. I just like this better… but I was wrong about the single iteration; I needed two as well. But this is smaller, illustrates the awesome power of `map()`, and handles output from `mpc` that doesn’t have 4 elements.

function! FormatPlaylistEntry(dict, posSize, artistSize, albumSize)
let a:dict[‘position’] = printf(“%-” . a:posSize . “s ”, a:dict[‘position’])
let a:dict[‘artist’] = printf(“%-” . a:artistSize . “s ”, a:dict[‘artist’])
let a:dict[‘album’] = printf(“%-” . a:albumSize . “s ”, a:dict[‘album’])
return a:dict
endfunction

function! mpc#GetPlaylist()
let command = “/usr/local/bin/mpc —format ‘position %artist% album @title’ playlist”
let [results, playlist] = [split(system(command), ‘\
’), []]
let maxPos = 0
let maxArtist = 0
let maxAlbum = 0

for item in results
let song = split(item, " @")
if len(song) < 4
let songlen = len(song) + 1
for i in range(songlen, 4)
call add(song, ’‘)
endfor
endif
let [position, artist, album, title] = song
let maxPos = len(position) > maxPos ? len(position) : maxPos
let maxArtist = len(artist) > maxArtist ? len(artist) : maxArtist
let maxAlbum = len(album) > maxAlbum ? len(album) : maxAlbum
call add(playlist, {’position’: position, ‘artist’: artist, ‘album’: album, ‘title’: title})
endfor
call map(playlist, ‘FormatPlaylistEntry(v:val, ’ . maxPos . ‘, ’ . maxArtist . ‘, ’ . maxAlbum . ‘)’)

return playlist
endfunction

epubERROR

In “Detecting the Current Filetype”, the first iteration of the ftdetect/mpc.vim does not work for me. Instead of coding:
autocmd​ ​BufRead​,​BufNewFile​ *.mpdv
set​ ​filetype​=mpd
which did nothing, I had to code:
autocmd BufRead,BufNewFile *.mpdf
\\ set filetype=mpd

My guess is that’s just an ePub formatting issue, but it sure caused problems for me.

Plus, the file name given - “filetype/mpc/ftdetect/mpdv.vim” - should actually read “plugin/mpc/ftdetect/mpdv.vim”

codeSUGGEST

The code should be better labeled and organized. Usually, code for books like this is identify with a chapter number so that it’s easy to see what’s being done where. The lack of clear organization makes it more difficult to work with than it should be.

codeERROR

I followed the book through Chapter 5 and all was working well. However, when I restarted my machine and thus restarted vim, I was hit with a bunch or errors so bad that I had to move mpc out of the plugin directory. This happens repeatedly
Error detected while processing function GetMPCStatusLine:
line 8:
E684: list index out of range: 1
and also happens when I even start to navigate to the fiplugin/mpdv.vim directory/file. Quite a surprize since everything was working so well!

60TYPO

first line of the 3rd paragraph:
(-) If you just entered [w]
(+) If you just entered [v]

epubERROR

Section “Specifying a New Syntax”, the code given at vsyntax.1/mpc/syntax/mpdv.vim is missing the ‘contains xxx’ clause. However, the code in Figure 1 in that section does have the right code.

epubTYPO

Section “Writing user Commands”. Code in Figure 1 (return substitute(s:statusline…) does not match the published code or what’s in the book.

2SUGGEST

maybe a small imprecision that could be improved :

“Did you catch the ! (bang) at the end of the function’s first line?”

=> the bang is after the `function` keyword, not at the end of the function’s first line.

56ERROR

There is a bug in mpc#ToggleRandom. If repeat is on then the output of “mpc random” will look like this: “volume: n/a repeat: on random: …”. Note that there are FOUR spaces after “repeat: on” instead of three. So the value of status will be " random: off" or " random: on". It needs code like this to remove the leading space:

if (state[0:0] == ’ ’)
let state = state[1:]
endif

39ERROR

It is not practical to update the statusline using a function that calls system() to execute a somewhat long-running command, especially twice. See the discussion at h t t p s : / / groups.google.com/forum/#!topic/vim_use/4J0WGoinUCw
I had to put spaces in the URL get past the restriction on posting hyperlinks.

Categories: