Skip to content

Commit 1099756

Browse files
committed
Move file type detection to ftdetect/notes.vim
1 parent 7171b3e commit 1099756

File tree

2 files changed

+98
-104
lines changed

2 files changed

+98
-104
lines changed

autoload/xolox/notes.vim

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,70 @@
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.16.10'
9+
let g:xolox#notes#version = '0.16.11'
10+
let s:scriptdir = expand('<sfile>:p:h')
11+
12+
function! xolox#notes#init() " {{{1
13+
" Initialize the configuration of the notes plug-in. This is a bit tricky:
14+
" We want to be compatible with Pathogen which installs plug-ins as
15+
" "bundles" under ~/.vim/bundle/*/ so we use a relative path to make sure we
16+
" 'stay inside the bundle'. However if the notes.vim plug-in is installed
17+
" system wide the user probably won't have permission to write inside the
18+
" installation directory, so we have to switch to $HOME then.
19+
let systemdir = xolox#misc#path#absolute(s:scriptdir . '/../../misc/notes')
20+
if filewritable(systemdir) == 2
21+
let localdir = systemdir
22+
elseif xolox#misc#os#is_win()
23+
let localdir = xolox#misc#path#absolute('~/vimfiles/misc/notes')
24+
else
25+
let localdir = xolox#misc#path#absolute('~/.vim/misc/notes')
26+
endif
27+
" Define the default location where the user's notes are saved?
28+
if !exists('g:notes_directory')
29+
let g:notes_directory = xolox#misc#path#merge(localdir, 'user')
30+
endif
31+
" Define the default location of the shadow directory with predefined notes?
32+
if !exists('g:notes_shadowdir')
33+
let g:notes_shadowdir = xolox#misc#path#merge(systemdir, 'shadow')
34+
endif
35+
" Define the default location for the full text index.
36+
if !exists('g:notes_indexfile')
37+
let g:notes_indexfile = xolox#misc#path#merge(localdir, 'index.pickle')
38+
endif
39+
" Define the default location for the keyword scanner script.
40+
if !exists('g:notes_indexscript')
41+
let g:notes_indexscript = xolox#misc#path#merge(systemdir, 'search-notes.py')
42+
endif
43+
" Define the default suffix for note filenames.
44+
if !exists('g:notes_suffix')
45+
let g:notes_suffix = ''
46+
endif
47+
" Define the default location for the tag name index (used for completion).
48+
if !exists('g:notes_tagsindex')
49+
let g:notes_tagsindex = xolox#misc#path#merge(localdir, 'tags.txt')
50+
endif
51+
" Define the default action when a note's filename and title are out of sync.
52+
if !exists('g:notes_title_sync')
53+
" Valid values are "no", "change_title", "rename_file" and "prompt".
54+
let g:notes_title_sync = 'prompt'
55+
endif
56+
" Smart quotes and such are enabled by default.
57+
if !exists('g:notes_smart_quotes')
58+
let g:notes_smart_quotes = 1
59+
endif
60+
" Text used for horizontal rulers.
61+
if !exists('g:notes_ruler_text')
62+
let g:notes_ruler_text = repeat(' ', ((&tw > 0 ? &tw : 79) - 5) / 2) . '* * *'
63+
endif
64+
" Symbols used to denote list items with increasing nesting levels.
65+
if !exists('g:notes_list_bullets')
66+
if xolox#notes#unicode_enabled()
67+
let g:notes_list_bullets = ['', '', '', '', '', '']
68+
else
69+
let g:notes_list_bullets = ['*', '-', '+']
70+
endif
71+
endif
72+
endfunction
1073

1174
function! xolox#notes#shortcut() " {{{1
1275
" The "note:" pseudo protocol is just a shortcut for the :Note command.
@@ -372,13 +435,6 @@ function! s:match_any_keyword(keywords)
372435
return '/' . escape(join(results, '\|'), '/') . '/'
373436
endfunction
374437

375-
function! xolox#notes#swaphack() " {{{1
376-
" Selectively ignore the dreaded E325 interactive prompt.
377-
if exists('s:swaphack_enabled')
378-
let v:swapchoice = 'o'
379-
endif
380-
endfunction
381-
382438
function! xolox#notes#related(bang) " {{{1
383439
" Find all notes related to the current note or file.
384440
let starttime = xolox#misc#timer#start()
@@ -478,6 +534,32 @@ endfunction
478534

479535
" Miscellaneous functions. {{{1
480536

537+
function! xolox#notes#swaphack() " {{{2
538+
" Selectively ignore the dreaded E325 interactive prompt.
539+
if exists('s:swaphack_enabled')
540+
let v:swapchoice = 'o'
541+
endif
542+
endfunction
543+
544+
function! xolox#notes#autocmd_pattern(directory) " {{{2
545+
" Generate a normalized automatic command pattern. First we resolve the path
546+
" to the directory with notes (eliminating any symbolic links) so that the
547+
" automatic command also applies to symbolic links pointing to notes (Vim
548+
" matches filename patterns in automatic commands after resolving
549+
" filenames).
550+
let directory = xolox#misc#path#absolute(a:directory)
551+
" On Windows we have to replace backslashes with forward slashes, otherwise
552+
" the automatic command will never trigger! This has to happen before we
553+
" make the fnameescape() call.
554+
if xolox#misc#os#is_win()
555+
let directory = substitute(directory, '\\', '/', 'g')
556+
endif
557+
" Escape the directory but not the trailing "*".
558+
let pattern = fnameescape(directory) . '/*'
559+
" On Windows the pattern won't match if it contains repeating slashes.
560+
return substitute(pattern, '/\+', '/', 'g')
561+
endfunction
562+
481563
function! xolox#notes#filetype_is_note(ft) " {{{2
482564
" Check whether the given file type value refers to the notes.vim plug-in.
483565
return index(split(a:ft, '\.'), 'notes') >= 0

plugin/notes.vim

Lines changed: 8 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -11,74 +11,8 @@ if &cp || exists('g:loaded_notes')
1111
finish
1212
endif
1313

14-
" This is a bit tricky: We want to be compatible with Pathogen which installs
15-
" plug-ins as "bundles" under ~/.vim/bundle/*/ so we use a relative path to
16-
" make sure we 'stay inside the bundle'. However if the notes.vim plug-in is
17-
" installed system wide the user probably won't have permission to write
18-
" inside the installation directory, so we have to switch to $HOME then.
19-
let s:systemdir = xolox#misc#path#absolute(expand('<sfile>:p:h') . '/../misc/notes')
20-
if filewritable(s:systemdir) == 2
21-
let s:localdir = s:systemdir
22-
elseif xolox#misc#os#is_win()
23-
let s:localdir = xolox#misc#path#absolute('~/vimfiles/misc/notes')
24-
else
25-
let s:localdir = xolox#misc#path#absolute('~/.vim/misc/notes')
26-
endif
27-
28-
" Define the default location where the user's notes are saved?
29-
if !exists('g:notes_directory')
30-
let g:notes_directory = xolox#misc#path#merge(s:localdir, 'user')
31-
endif
32-
33-
" Define the default location of the shadow directory with predefined notes?
34-
if !exists('g:notes_shadowdir')
35-
let g:notes_shadowdir = xolox#misc#path#merge(s:systemdir, 'shadow')
36-
endif
37-
38-
" Define the default location for the full text index.
39-
if !exists('g:notes_indexfile')
40-
let g:notes_indexfile = xolox#misc#path#merge(s:localdir, 'index.pickle')
41-
endif
42-
43-
" Define the default location for the keyword scanner script.
44-
if !exists('g:notes_indexscript')
45-
let g:notes_indexscript = xolox#misc#path#merge(s:systemdir, 'search-notes.py')
46-
endif
47-
48-
" Define the default suffix for note filenames.
49-
if !exists('g:notes_suffix')
50-
let g:notes_suffix = ''
51-
endif
52-
53-
" Define the default location for the tag name index (used for completion).
54-
if !exists('g:notes_tagsindex')
55-
let g:notes_tagsindex = xolox#misc#path#merge(s:localdir, 'tags.txt')
56-
endif
57-
58-
" Define the default action when a note's filename and title are out of sync.
59-
if !exists('g:notes_title_sync')
60-
" Valid values are "no", "change_title", "rename_file" and "prompt".
61-
let g:notes_title_sync = 'prompt'
62-
endif
63-
64-
" Smart quotes and such are enabled by default.
65-
if !exists('g:notes_smart_quotes')
66-
let g:notes_smart_quotes = 1
67-
endif
68-
69-
" Text used for horizontal rulers.
70-
if !exists('g:notes_ruler_text')
71-
let g:notes_ruler_text = repeat(' ', ((&tw > 0 ? &tw : 79) - 5) / 2) . '* * *'
72-
endif
73-
74-
" Symbols used to denote list items with increasing nesting levels.
75-
if !exists('g:notes_list_bullets')
76-
if xolox#notes#unicode_enabled()
77-
let g:notes_list_bullets = ['', '', '', '', '', '']
78-
else
79-
let g:notes_list_bullets = ['*', '-', '+']
80-
endif
81-
endif
14+
" Initialize the configuration defaults.
15+
call xolox#notes#init()
8216

8317
" User commands to create, delete and search notes.
8418
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()
9327

9428
" Automatic commands to enable the :edit note:… shortcut and load the notes file type.
9529

96-
function! s:DAC(events, directory, command)
97-
" Define automatic command for {events} in {directory} with {command}.
98-
" Resolve the path to the directory with notes so that the automatic command
99-
" also applies to symbolic links pointing to notes (Vim matches filename
100-
" patterns in automatic commands after resolving filenames).
101-
let directory = xolox#misc#path#absolute(a:directory)
102-
" On Windows we have to replace backslashes with forward slashes.
103-
if xolox#misc#os#is_win()
104-
let directory = substitute(directory, '\\', '/', 'g')
105-
endif
106-
let pattern = fnameescape(directory) . '/*'
107-
" On Windows the pattern won't match if it contains repeating slashes.
108-
let pattern = substitute(pattern, '/\+', '/', 'g')
109-
execute 'autocmd' a:events pattern a:command
110-
endfunction
111-
11230
augroup PluginNotes
11331
autocmd!
114-
" NB: "nested" is used here so that SwapExists automatic commands apply
115-
" to notes (which is IMHO better than always showing the E325 prompt).
116-
au BufReadCmd note:* nested call xolox#notes#shortcut()
117-
call s:DAC('BufReadCmd', g:notes_shadowdir, 'call xolox#notes#edit_shadow()')
118-
call s:DAC('BufWriteCmd', g:notes_directory, 'call xolox#notes#save()')
11932
au SwapExists * call xolox#notes#swaphack()
12033
au BufUnload * call xolox#notes#unload_from_cache()
12134
au BufReadPost,BufWritePost * call xolox#notes#refresh_syntax()
12235
au InsertEnter,InsertLeave * call xolox#notes#refresh_syntax()
12336
au CursorHold,CursorHoldI * call xolox#notes#refresh_syntax()
37+
" NB: "nested" is used here so that SwapExists automatic commands apply
38+
" to notes (which is IMHO better than always showing the E325 prompt).
39+
au BufReadCmd note:* nested call xolox#notes#shortcut()
40+
" Automatic commands to read/write notes (used for automatic renaming).
41+
exe 'au BufReadCmd' xolox#notes#autocmd_pattern(g:notes_shadowdir) 'call xolox#notes#edit_shadow()'
42+
exe 'au BufWriteCmd' xolox#notes#autocmd_pattern(g:notes_directory) 'call xolox#notes#save()'
12443
augroup END
12544

126-
augroup filetypedetect
127-
call s:DAC('BufNewFile,BufRead', g:notes_directory, 'if &bt == "" | setl ft=notes | endif')
128-
call s:DAC('BufNewFile,BufRead', g:notes_shadowdir, 'if &bt == "" | setl ft=notes | endif')
129-
augroup END
130-
131-
delfunction s:DAC
132-
13345
" Make sure the plug-in is only loaded once.
13446
let g:loaded_notes = 1
13547

0 commit comments

Comments
 (0)