Skip to content
Browse files

Move file type detection to ftdetect/notes.vim

  • Loading branch information...
1 parent 7171b3e commit 1099756cdede2528d695f989da0ddbf2cc970a4b @xolox committed
Showing with 98 additions and 104 deletions.
  1. +90 −8 autoload/xolox/notes.vim
  2. +8 −96 plugin/notes.vim
View
98 autoload/xolox/notes.vim
@@ -6,7 +6,70 @@
" 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.16.10'
+let g:xolox#notes#version = '0.16.11'
+let s:scriptdir = expand('<sfile>:p:h')
+
+function! xolox#notes#init() " {{{1
+ " Initialize the configuration of the notes plug-in. This is a bit tricky:
+ " We want to be compatible with Pathogen which installs plug-ins as
+ " "bundles" under ~/.vim/bundle/*/ so we use a relative path to make sure we
+ " 'stay inside the bundle'. However if the notes.vim plug-in is installed
+ " system wide the user probably won't have permission to write inside the
+ " installation directory, so we have to switch to $HOME then.
+ let systemdir = xolox#misc#path#absolute(s:scriptdir . '/../../misc/notes')
+ if filewritable(systemdir) == 2
+ let localdir = systemdir
+ elseif xolox#misc#os#is_win()
+ let localdir = xolox#misc#path#absolute('~/vimfiles/misc/notes')
+ else
+ let localdir = xolox#misc#path#absolute('~/.vim/misc/notes')
+ endif
+ " Define the default location where the user's notes are saved?
+ if !exists('g:notes_directory')
+ let g:notes_directory = xolox#misc#path#merge(localdir, 'user')
+ endif
+ " Define the default location of the shadow directory with predefined notes?
+ if !exists('g:notes_shadowdir')
+ let g:notes_shadowdir = xolox#misc#path#merge(systemdir, 'shadow')
+ endif
+ " Define the default location for the full text index.
+ if !exists('g:notes_indexfile')
+ let g:notes_indexfile = xolox#misc#path#merge(localdir, 'index.pickle')
+ endif
+ " Define the default location for the keyword scanner script.
+ if !exists('g:notes_indexscript')
+ let g:notes_indexscript = xolox#misc#path#merge(systemdir, 'search-notes.py')
+ endif
+ " Define the default suffix for note filenames.
+ if !exists('g:notes_suffix')
+ let g:notes_suffix = ''
+ endif
+ " Define the default location for the tag name index (used for completion).
+ if !exists('g:notes_tagsindex')
+ let g:notes_tagsindex = xolox#misc#path#merge(localdir, 'tags.txt')
+ endif
+ " Define the default action when a note's filename and title are out of sync.
+ if !exists('g:notes_title_sync')
+ " Valid values are "no", "change_title", "rename_file" and "prompt".
+ let g:notes_title_sync = 'prompt'
+ endif
+ " Smart quotes and such are enabled by default.
+ if !exists('g:notes_smart_quotes')
+ let g:notes_smart_quotes = 1
+ endif
+ " Text used for horizontal rulers.
+ if !exists('g:notes_ruler_text')
+ let g:notes_ruler_text = repeat(' ', ((&tw > 0 ? &tw : 79) - 5) / 2) . '* * *'
+ endif
+ " Symbols used to denote list items with increasing nesting levels.
+ if !exists('g:notes_list_bullets')
+ if xolox#notes#unicode_enabled()
+ let g:notes_list_bullets = ['•', '◦', '▸', '▹', '▪', '▫']
+ else
+ let g:notes_list_bullets = ['*', '-', '+']
+ endif
+ endif
+endfunction
function! xolox#notes#shortcut() " {{{1
" The "note:" pseudo protocol is just a shortcut for the :Note command.
@@ -372,13 +435,6 @@ function! s:match_any_keyword(keywords)
return '/' . escape(join(results, '\|'), '/') . '/'
endfunction
-function! xolox#notes#swaphack() " {{{1
- " Selectively ignore the dreaded E325 interactive prompt.
- if exists('s:swaphack_enabled')
- let v:swapchoice = 'o'
- endif
-endfunction
-
function! xolox#notes#related(bang) " {{{1
" Find all notes related to the current note or file.
let starttime = xolox#misc#timer#start()
@@ -478,6 +534,32 @@ endfunction
" Miscellaneous functions. {{{1
+function! xolox#notes#swaphack() " {{{2
+ " Selectively ignore the dreaded E325 interactive prompt.
+ if exists('s:swaphack_enabled')
+ let v:swapchoice = 'o'
+ endif
+endfunction
+
+function! xolox#notes#autocmd_pattern(directory) " {{{2
+ " Generate a normalized automatic command pattern. First we resolve the path
+ " to the directory with notes (eliminating any symbolic links) so that the
+ " automatic command also applies to symbolic links pointing to notes (Vim
+ " matches filename patterns in automatic commands after resolving
+ " filenames).
+ let directory = xolox#misc#path#absolute(a:directory)
+ " On Windows we have to replace backslashes with forward slashes, otherwise
+ " the automatic command will never trigger! This has to happen before we
+ " make the fnameescape() call.
+ if xolox#misc#os#is_win()
+ let directory = substitute(directory, '\\', '/', 'g')
+ endif
+ " Escape the directory but not the trailing "*".
+ let pattern = fnameescape(directory) . '/*'
+ " On Windows the pattern won't match if it contains repeating slashes.
+ return substitute(pattern, '/\+', '/', 'g')
+endfunction
+
function! xolox#notes#filetype_is_note(ft) " {{{2
" Check whether the given file type value refers to the notes.vim plug-in.
return index(split(a:ft, '\.'), 'notes') >= 0
View
104 plugin/notes.vim
@@ -11,74 +11,8 @@ if &cp || exists('g:loaded_notes')
finish
endif
-" This is a bit tricky: We want to be compatible with Pathogen which installs
-" plug-ins as "bundles" under ~/.vim/bundle/*/ so we use a relative path to
-" make sure we 'stay inside the bundle'. However if the notes.vim plug-in is
-" installed system wide the user probably won't have permission to write
-" inside the installation directory, so we have to switch to $HOME then.
-let s:systemdir = xolox#misc#path#absolute(expand('<sfile>:p:h') . '/../misc/notes')
-if filewritable(s:systemdir) == 2
- let s:localdir = s:systemdir
-elseif xolox#misc#os#is_win()
- let s:localdir = xolox#misc#path#absolute('~/vimfiles/misc/notes')
-else
- let s:localdir = xolox#misc#path#absolute('~/.vim/misc/notes')
-endif
-
-" Define the default location where the user's notes are saved?
-if !exists('g:notes_directory')
- let g:notes_directory = xolox#misc#path#merge(s:localdir, 'user')
-endif
-
-" Define the default location of the shadow directory with predefined notes?
-if !exists('g:notes_shadowdir')
- let g:notes_shadowdir = xolox#misc#path#merge(s:systemdir, 'shadow')
-endif
-
-" Define the default location for the full text index.
-if !exists('g:notes_indexfile')
- let g:notes_indexfile = xolox#misc#path#merge(s:localdir, 'index.pickle')
-endif
-
-" Define the default location for the keyword scanner script.
-if !exists('g:notes_indexscript')
- let g:notes_indexscript = xolox#misc#path#merge(s:systemdir, 'search-notes.py')
-endif
-
-" Define the default suffix for note filenames.
-if !exists('g:notes_suffix')
- let g:notes_suffix = ''
-endif
-
-" Define the default location for the tag name index (used for completion).
-if !exists('g:notes_tagsindex')
- let g:notes_tagsindex = xolox#misc#path#merge(s:localdir, 'tags.txt')
-endif
-
-" Define the default action when a note's filename and title are out of sync.
-if !exists('g:notes_title_sync')
- " Valid values are "no", "change_title", "rename_file" and "prompt".
- let g:notes_title_sync = 'prompt'
-endif
-
-" Smart quotes and such are enabled by default.
-if !exists('g:notes_smart_quotes')
- let g:notes_smart_quotes = 1
-endif
-
-" Text used for horizontal rulers.
-if !exists('g:notes_ruler_text')
- let g:notes_ruler_text = repeat(' ', ((&tw > 0 ? &tw : 79) - 5) / 2) . '* * *'
-endif
-
-" Symbols used to denote list items with increasing nesting levels.
-if !exists('g:notes_list_bullets')
- if xolox#notes#unicode_enabled()
- let g:notes_list_bullets = ['•', '◦', '▸', '▹', '▪', '▫']
- else
- let g:notes_list_bullets = ['*', '-', '+']
- endif
-endif
+" Initialize the configuration defaults.
+call xolox#notes#init()
" User commands to create, delete and search notes.
command! -bar -bang -nargs=? -complete=customlist,xolox#notes#cmd_complete Note call xolox#notes#edit(<q-bang>, <q-args>)
@@ -93,43 +27,21 @@ command! -bar IndexTaggedNotes call xolox#notes#tags#create_index()
" Automatic commands to enable the :edit note:… shortcut and load the notes file type.
-function! s:DAC(events, directory, command)
- " Define automatic command for {events} in {directory} with {command}.
- " Resolve the path to the directory with notes so that the automatic command
- " also applies to symbolic links pointing to notes (Vim matches filename
- " patterns in automatic commands after resolving filenames).
- let directory = xolox#misc#path#absolute(a:directory)
- " On Windows we have to replace backslashes with forward slashes.
- if xolox#misc#os#is_win()
- let directory = substitute(directory, '\\', '/', 'g')
- endif
- let pattern = fnameescape(directory) . '/*'
- " On Windows the pattern won't match if it contains repeating slashes.
- let pattern = substitute(pattern, '/\+', '/', 'g')
- execute 'autocmd' a:events pattern a:command
-endfunction
-
augroup PluginNotes
autocmd!
- " NB: "nested" is used here so that SwapExists automatic commands apply
- " to notes (which is IMHO better than always showing the E325 prompt).
- au BufReadCmd note:* nested call xolox#notes#shortcut()
- call s:DAC('BufReadCmd', g:notes_shadowdir, 'call xolox#notes#edit_shadow()')
- call s:DAC('BufWriteCmd', g:notes_directory, 'call xolox#notes#save()')
au SwapExists * call xolox#notes#swaphack()
au BufUnload * call xolox#notes#unload_from_cache()
au BufReadPost,BufWritePost * call xolox#notes#refresh_syntax()
au InsertEnter,InsertLeave * call xolox#notes#refresh_syntax()
au CursorHold,CursorHoldI * call xolox#notes#refresh_syntax()
+ " NB: "nested" is used here so that SwapExists automatic commands apply
+ " to notes (which is IMHO better than always showing the E325 prompt).
+ au BufReadCmd note:* nested call xolox#notes#shortcut()
+ " Automatic commands to read/write notes (used for automatic renaming).
+ exe 'au BufReadCmd' xolox#notes#autocmd_pattern(g:notes_shadowdir) 'call xolox#notes#edit_shadow()'
+ exe 'au BufWriteCmd' xolox#notes#autocmd_pattern(g:notes_directory) 'call xolox#notes#save()'
augroup END
-augroup filetypedetect
- call s:DAC('BufNewFile,BufRead', g:notes_directory, 'if &bt == "" | setl ft=notes | endif')
- call s:DAC('BufNewFile,BufRead', g:notes_shadowdir, 'if &bt == "" | setl ft=notes | endif')
-augroup END
-
-delfunction s:DAC
-
" Make sure the plug-in is only loaded once.
let g:loaded_notes = 1

0 comments on commit 1099756

Please sign in to comment.
Something went wrong with that request. Please try again.