Skip to content

Commit 6723f90

Browse files
committed
:SearchNotes completion now sorts by Levenshtein distance
TODO Document this: sudo apt-get install python-levenshtein
1 parent 6993951 commit 6723f90

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

autoload/xolox/notes.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
" Vim auto-load script
22
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: November 22, 2011
3+
" Last Change: November 23, 2011
44
" URL: http://peterodding.com/code/vim/notes/
55

66
" Note: This file is encoded in UTF-8 including a byte order mark so
77
" that Vim loads the script using the right encoding transparently.
88

9-
let g:xolox#notes#version = '0.14.1'
9+
let g:xolox#notes#version = '0.14.2'
1010

1111
function! xolox#notes#shortcut() " {{{1
1212
" The "note:" pseudo protocol is just a shortcut for the :Note command.
@@ -551,7 +551,7 @@ function! s:run_scanner(keywords, matches) " {{{2
551551
endfunction
552552

553553
function! xolox#notes#keyword_complete(arglead, cmdline, cursorpos) " {{{2
554-
return s:python_command('--list=' . a:arglead)
554+
return split(s:python_command('--list=' . a:arglead), '\n')
555555
endfunction
556556

557557
function! s:python_command(...) " {{{2

misc/notes/search-notes.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
import re
2121
import sys
2222

23+
try:
24+
import Levenshtein
25+
levenshtein_supported = True
26+
except ImportError:
27+
levenshtein_supported = False
28+
2329
class NotesIndex:
2430

2531
def __init__(self):
@@ -33,7 +39,7 @@ def __init__(self):
3339
self.list_keywords(self.keyword_filter)
3440
else:
3541
matches = self.search_index(keywords)
36-
print '\n'.join(sorted(matches))
42+
print self.encode('\n'.join(sorted(matches)))
3743

3844
def parse_args(self):
3945
''' Parse the command line arguments. '''
@@ -64,6 +70,8 @@ def parse_args(self):
6470
sys.exit(0)
6571
else:
6672
assert False, "Unhandled option"
73+
if self.keyword_filter is not None:
74+
self.keyword_filter = self.decode(self.keyword_filter)
6775
# Canonicalize pathnames, check validity.
6876
self.database_file = self.munge_path(self.database_file)
6977
self.user_directory = self.munge_path(self.user_directory)
@@ -147,27 +155,37 @@ def search_index(self, keywords):
147155
matches &= set(filenames)
148156
return list(matches) if matches else []
149157

150-
def list_keywords(self, substring, limit=100):
158+
def list_keywords(self, substring, limit=25):
151159
''' Print all (matching) keywords to standard output. '''
152-
i = 0
153-
for kw in self.index['keywords']:
160+
decorated = []
161+
for kw, filenames in self.index['keywords'].iteritems():
154162
if substring in kw.lower():
155-
print kw
156-
if i < limit:
157-
i += 1
163+
if levenshtein_supported:
164+
decorated.append((Levenshtein.distance(kw.lower(), substring), -len(filenames), kw))
158165
else:
159-
break
166+
decorated.append((-len(filenames), kw))
167+
decorated.sort()
168+
selection = [d[-1] for d in decorated[:limit]]
169+
print self.encode('\n'.join(selection))
160170

161171
def tokenize(self, text):
162172
''' Tokenize a string into a list of normalized, unique keywords. '''
163173
words = set()
164-
text = text.decode(self.character_encoding, 'ignore')
165-
for word in re.findall(r'\w+', text.lower(), re.UNICODE):
174+
text = self.decode(text).lower()
175+
for word in re.findall(r'\w+', text, re.UNICODE):
166176
word = word.strip()
167177
if word != '' and not word.isspace():
168178
words.add(word)
169179
return words
170180

181+
def encode(self, text):
182+
''' Encode a string in the user's preferred character encoding. '''
183+
return text.encode(self.character_encoding, 'ignore')
184+
185+
def decode(self, text):
186+
''' Decode a string in the user's preferred character encoding. '''
187+
return text.decode(self.character_encoding, 'ignore')
188+
171189
def munge_path(self, path):
172190
''' Canonicalize user-defined path, making it absolute. '''
173191
return os.path.abspath(os.path.expanduser(path))

plugin/notes.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" Vim plug-in
22
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: November 22, 2011
3+
" Last Change: November 23, 2011
44
" URL: http://peterodding.com/code/vim/notes/
55

66
" Support for automatic update using the GLVS plug-in.
@@ -59,7 +59,7 @@ endif
5959
command! -bar -bang -nargs=? -complete=customlist,xolox#notes#cmd_complete Note call xolox#notes#edit(<q-bang>, <q-args>)
6060
command! -bar -bang -range NoteFromSelectedText call xolox#notes#from_selection(<q-bang>)
6161
command! -bar -bang -nargs=? -complete=customlist,xolox#notes#cmd_complete DeleteNote call xolox#notes#delete(<q-bang>, <q-args>)
62-
command! -bang -nargs=? -complete=custom,xolox#notes#keyword_complete SearchNotes call xolox#notes#search(<q-bang>, <q-args>)
62+
command! -bang -nargs=? -complete=customlist,xolox#notes#keyword_complete SearchNotes call xolox#notes#search(<q-bang>, <q-args>)
6363
command! -bar -bang RelatedNotes call xolox#notes#related(<q-bang>)
6464
command! -bar -bang -nargs=? RecentNotes call xolox#notes#recent(<q-bang>, <q-args>)
6565
command! -bar -count=1 ShowTaggedNotes call xolox#notes#tags#show_tags(<count>)

0 commit comments

Comments
 (0)