Fix obscure bug with sorting zk-index buffer by size #64
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The bug is rare, but if
zk-id-regexp
happens to match a portion of zk-index buffer that is not a zk-id, thenzk-index--current-id-list
will include it in its returned list, which in turn is used inzk-index--current-file-list
to return a list of files. But since it's not a zk-id,zk--parse-id
will return nil, resulting inzk-index--current-file-list
returning a list with at least one nil element. This becomes a problem inzk-index--sort-size
, since it uses>
(greater) function to compare file sizes, which signals an error if an argument is nil. However,file-attributes
(andfile-attribute-size
) can returnnil
if one is passed to them or if the file does not exist (see Emacs Lisp manual about "File Attributes").Additionally,
zk-index--format-candidates
does not expect nils in the list of the files it works with, which again causes an error whenstring-match
ends up being passed a nil rather than a string.The fix involves having 1)
zk-index--sort-size
ensure that it's always passing numbers to>
, but since that just propagates the nil inside the list of files returned byzk-index--sort
, we also make sure 2)zk-index--format-candidates
does not pass nil tostring-match
.Another possibility would be to have
zk-index--current-file-list
remove nils from the file list it returns (replacingfiles
with(delq nil files)
as the final line), but that results in a small performance loss becausedelq
needs to traverse the list looking for nils. I think my two-pronged fix is more robust, anyway, since it deals with the errors at the level where they occur (in the calls to>
andstring-match
). Of course, we can also wrap those calls inignore-errors
, but that might make debugging more difficult in the future.