Permalink
Browse files

Keyword completion for :SearchNotes :-)

  • Loading branch information...
xolox committed Nov 22, 2011
1 parent 7f86001 commit 69939518a1b1e661dd6cb0b7d4f06291a62a5a20
Showing with 50 additions and 21 deletions.
  1. +25 −14 autoload/xolox/notes.vim
  2. +24 −6 misc/notes/search-notes.py
  3. +1 −1 plugin/notes.vim
View
@@ -6,7 +6,7 @@
" Note: This file is encoded in UTF-8 including a byte order mark so
" that Vim loads the script using the right encoding transparently.
-let g:xolox#notes#version = '0.14'
+let g:xolox#notes#version = '0.14.1'
function! xolox#notes#shortcut() " {{{1
" The "note:" pseudo protocol is just a shortcut for the :Note command.
@@ -543,24 +543,35 @@ endfunction
function! s:run_scanner(keywords, matches) " {{{2
" Try to run scanner.py script to find notes matching {keywords}.
- let scanner = xolox#misc#path#absolute(g:notes_indexscript)
- let python = 'python'
- if executable('python2')
- let python = 'python2'
+ let output = s:python_command(a:keywords)
+ if !empty(output)
+ call extend(a:matches, split(output, '\n'))
+ return 1
endif
- if !(executable(python) && filereadable(scanner))
- call xolox#misc#msg#debug("notes.vim %s: The %s script isn't executable.", g:xolox#notes#version, scanner)
+endfunction
+
+function! xolox#notes#keyword_complete(arglead, cmdline, cursorpos) " {{{2
+ return s:python_command('--list=' . a:arglead)
+endfunction
+
+function! s:python_command(...) " {{{2
+ let script = xolox#misc#path#absolute(g:notes_indexscript)
+ let python = executable('python2') ? 'python2' : 'python'
+ if !(executable(python) && filereadable(script))
+ call xolox#misc#msg#debug("notes.vim %s: The %s script isn't executable.", g:xolox#notes#version, script)
else
- let arguments = [scanner, '--database', g:notes_indexfile, '--notes', g:notes_directory, a:keywords]
- call map(arguments, 'xolox#misc#escape#shell(v:val)')
- let output = xolox#misc#str#trim(system(join([python] + arguments)))
- if !v:shell_error
- call extend(a:matches, split(output, '\n'))
- return 1
+ let options = ['--database', g:notes_indexfile, '--notes', g:notes_directory]
+ let arguments = map([script] + options + a:000, 'xolox#misc#escape#shell(v:val)')
+ let command = join([python] + arguments)
+ call xolox#misc#msg#debug("notes.vim %s: Executing external command %s", g:xolox#notes#version, command)
+ let output = xolox#misc#str#trim(system(command))
+ if v:shell_error
+ call xolox#misc#msg#warn("notes.vim %s: Search script failed with output: %s", g:xolox#notes#version, output)
else
- call xolox#misc#msg#warn("notes.vim %s: scanner.py failed with output: %s", g:xolox#notes#version, output)
+ return output
endif
endif
+ return ''
endfunction
" Getters for filenames & titles of existing notes. {{{2
View
@@ -29,14 +29,17 @@ def __init__(self):
self.update_index()
if self.dirty:
self.save_index()
- matches = self.search_index(keywords)
- print '\n'.join(sorted(matches))
+ if self.keyword_filter is not None:
+ self.list_keywords(self.keyword_filter)
+ else:
+ matches = self.search_index(keywords)
+ print '\n'.join(sorted(matches))
def parse_args(self):
''' Parse the command line arguments. '''
try:
- opts, keywords = getopt.getopt(sys.argv[1:], 'd:n:e:h',
- ['database=', 'notes=', 'encoding=', 'help'])
+ opts, keywords = getopt.getopt(sys.argv[1:], 'l:d:n:e:h',
+ ['list=', 'database=', 'notes=', 'encoding=', 'help'])
except getopt.GetoptError, error:
print str(error)
self.usage()
@@ -45,9 +48,12 @@ def parse_args(self):
self.database_file = '~/.vim/misc/notes/index.pickle'
self.user_directory = '~/.vim/misc/notes/user/'
self.character_encoding = 'UTF-8'
+ self.keyword_filter = None
# Map command line options to variables.
for opt, arg in opts:
- if opt in ('-d', '--database'):
+ if opt in ('-l', '--list'):
+ self.keyword_filter = arg.strip().lower()
+ elif opt in ('-d', '--database'):
self.database_file = arg
elif opt in ('-n', '--notes'):
self.user_directory = arg
@@ -72,7 +78,7 @@ def load_index(self):
try:
with open(self.database_file) as handle:
self.index = pickle.load(handle)
- assert self.index['version'] >= 1
+ assert self.index['version'] == 1
self.first_use = False
self.dirty = False
except:
@@ -141,6 +147,17 @@ def search_index(self, keywords):
matches &= set(filenames)
return list(matches) if matches else []
+ def list_keywords(self, substring, limit=100):
+ ''' Print all (matching) keywords to standard output. '''
+ i = 0
+ for kw in self.index['keywords']:
+ if substring in kw.lower():
+ print kw
+ if i < limit:
+ i += 1
+ else:
+ break
+
def tokenize(self, text):
''' Tokenize a string into a list of normalized, unique keywords. '''
words = set()
@@ -164,6 +181,7 @@ def usage(self):
Valid options include:
+ -l, --list=SUBSTR list keywords matching substring
-d, --database=FILE set path to keywords index file
-n, --notes=DIR set directory with user notes
-e, --encoding=NAME set character encoding of notes
View
@@ -59,7 +59,7 @@ endif
command! -bar -bang -nargs=? -complete=customlist,xolox#notes#cmd_complete Note call xolox#notes#edit(<q-bang>, <q-args>)
command! -bar -bang -range NoteFromSelectedText call xolox#notes#from_selection(<q-bang>)
command! -bar -bang -nargs=? -complete=customlist,xolox#notes#cmd_complete DeleteNote call xolox#notes#delete(<q-bang>, <q-args>)
-command! -bang -nargs=? SearchNotes call xolox#notes#search(<q-bang>, <q-args>)
+command! -bang -nargs=? -complete=custom,xolox#notes#keyword_complete SearchNotes call xolox#notes#search(<q-bang>, <q-args>)
command! -bar -bang RelatedNotes call xolox#notes#related(<q-bang>)
command! -bar -bang -nargs=? RecentNotes call xolox#notes#recent(<q-bang>, <q-args>)
command! -bar -count=1 ShowTaggedNotes call xolox#notes#tags#show_tags(<count>)

0 comments on commit 6993951

Please sign in to comment.