Skip to content

Commit

Permalink
Basic support for @tags (suggested by Jonathan Reeve)
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Jun 3, 2011
1 parent 7a9e923 commit 677cd70
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -7,6 +7,7 @@ The notes.vim plug-in for the [Vim text editor] [vim] makes it easy to manage yo
* **Editing existing notes:** Execute `:Note anything` to edit a note containing `anything` in its title (if no notes are found a new one is created with its title set to `anything`)
* **Deleting notes:** The `:DeleteNote` command enables you to delete the current note
* **Searching notes:** `:SearchNotes keyword …` searches for keywords and `:SearchNotes /pattern/` searches for regular expressions
* **Smart defaults:** Without an argument `:SearchNotes` searches for the word under the cursor (if the word starts with `@` that character will be included in the search, this means you can easily search for *@tagged* notes)
* **Back-references:** The `:RelatedNotes` command find all notes referencing the current file
* A [Python 2] [python] script is included that accelerates keyword searches using an [SQLite] [sqlite] database
* **Navigating between notes:** The included file type plug-in redefines [gf] [gf] to jump between notes and the syntax script highlights note names as hyper links
Expand Down
32 changes: 25 additions & 7 deletions autoload/xolox/notes.vim
@@ -1,6 +1,6 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
" Last Change: May 22, 2011
" Last Change: June 4, 2011
" URL: http://peterodding.com/code/vim/notes/

" Note: This file is encoded in UTF-8 including a byte order mark so
Expand Down Expand Up @@ -169,17 +169,25 @@ function! xolox#notes#delete(bang) " {{{1
endfunction

function! xolox#notes#search(bang, input) " {{{1
" Search all notes for the pattern or keywords {input}.
if a:input =~ '^/.\+/$'
call s:internal_search(a:bang, a:input, '', '')
" Search all notes for the pattern or keywords {input} (current word if none given).
let input = a:input
if input == ''
let input = s:tag_under_cursor()
if input == ''
call xolox#misc#msg#warn("No string under cursor")
return
endif
endif
if input =~ '^/.\+/$'
call s:internal_search(a:bang, input, '', '')
if &buftype == 'quickfix'
let w:quickfix_title = 'Notes matching the pattern ' . a:input
let w:quickfix_title = 'Notes matching the pattern ' . input
endif
else
let keywords = split(a:input)
let keywords = split(input)
let all_keywords = s:match_all_keywords(keywords)
let any_keyword = s:match_any_keyword(keywords)
call s:internal_search(a:bang, all_keywords, a:input, any_keyword)
call s:internal_search(a:bang, all_keywords, input, any_keyword)
if &buftype == 'quickfix'
call map(keywords, '"`" . v:val . "''"')
let w:quickfix_title = printf('Notes containing the word%s %s', len(keywords) == 1 ? '' : 's',
Expand All @@ -188,6 +196,16 @@ function! xolox#notes#search(bang, input) " {{{1
endif
endfunction

function! s:tag_under_cursor() " {{{2
try
let isk_save = &isk
set iskeyword+=@-@
return expand('<cword>')
finally
let &isk = isk_save
endtry
endfunction

function! s:match_all_keywords(keywords) " {{{2
" Create a regex that matches when a file contains all {keywords}.
let results = copy(a:keywords)
Expand Down
4 changes: 4 additions & 0 deletions doc/notes.txt
Expand Up @@ -21,6 +21,10 @@ notes in Vim:
- Searching notes: ':SearchNotes keyword …' searches for keywords and
':SearchNotes /pattern/' searches for regular expressions

- Smart defaults: Without an argument ':SearchNotes' searches for the word
under the cursor (if the word starts with '@' that character will be
included in the search, this means you can easily search for @tagged notes)

- Back-references: The ':RelatedNotes' command find all notes referencing the
current file

Expand Down
24 changes: 24 additions & 0 deletions misc/notes/shadow/Note taking commands
Expand Up @@ -38,6 +38,30 @@ notes containing all of the given keywords:

:SearchNotes syntax highlighting

## :SearchNotes understands @tags

If you don’t pass any arguments to the :SearchNotes command it will search for
the word under the cursor. If the word under the cursor starts with ‘@’ this
character will be included in the search, which makes it possible to easily
add @tags to your @notes and then search for those tags. To make searching for
tags even easier you can create key mappings for the :SearchNotes command:
{{{vim
" Make the C-] combination search for @tags:
imap <C-]> <C-o>:SearchNotes<CR>
nmap <C-]> :SearchNotes<CR>

" Make double mouse click search for @tags. This is actually quite a lot of
" fun if you don’t use the mouse for text selections anyway; you can click
" between notes as if you’re in a web browser:
imap <2-LeftMouse> <C-o>:SearchNotes<CR>
nmap <2-LeftMouse> :SearchNotes<CR>
}}}
These mappings are currently not enabled by default because they conflict with
already useful key mappings, but if you have any suggestions for alternatives
feel free to contact me through GitHub or at peter@peterodding.com.

## Accelerated searching with Python and SQLite

After collecting a fair amount of notes (say >= 5 MB) you will probably start
to get annoyed at how long it takes Vim to search through all of your notes. To
make searching more scalable the notes plug-in includes a Python script which
Expand Down
6 changes: 3 additions & 3 deletions plugin/notes.vim
@@ -1,9 +1,9 @@
" Vim plug-in
" Author: Peter Odding <peter@peterodding.com>
" Last Change: May 22, 2011
" Last Change: June 4, 2011
" URL: http://peterodding.com/code/vim/notes/
" License: MIT
" Version: 0.8.7
" Version: 0.8.8

" Support for automatic update using the GLVS plug-in.
" GetLatestVimScripts: 3375 1 :AutoInstall: notes.zip
Expand Down Expand Up @@ -39,7 +39,7 @@ endif
" User commands to create, delete and search notes.
command! -bar -bang -nargs=? -complete=customlist,xolox#notes#complete Note call xolox#notes#edit(<q-bang>, <q-args>)
command! -bar -bang DeleteNote call xolox#notes#delete(<q-bang>)
command! -bar -bang -nargs=1 SearchNotes call xolox#notes#search(<q-bang>, <q-args>)
command! -bar -bang -nargs=? SearchNotes call xolox#notes#search(<q-bang>, <q-args>)
command! -bar -bang RelatedNotes call xolox#notes#related(<q-bang>)

" Automatic commands to enable the :edit note:… shortcut and load the notes file type.
Expand Down
6 changes: 5 additions & 1 deletion syntax/notes.vim
@@ -1,6 +1,6 @@
" Vim syntax script
" Author: Peter Odding <peter@peterodding.com>
" Last Change: May 22, 2011
" Last Change: June 4, 2011
" URL: http://peterodding.com/code/vim/notes/

" Note: This file is encoded in UTF-8 including a byte order mark so
Expand All @@ -27,6 +27,10 @@ call xolox#notes#highlight_names(1)
syntax cluster notesInline add=notesName
highlight def link notesName Underlined

" Highlight @tags as hyperlinks. {{{2
syntax match notesTagName /\(^\|\s\)\@<=@\w\+/
highlight def link notesTagName Underlined

" Highlight list bullets and numbers. {{{2
syntax match notesListBullet /^\s*\zs\(\|\*\)/
highlight def link notesListBullet Comment
Expand Down

0 comments on commit 677cd70

Please sign in to comment.