Skip to content

Commit 5e29af0

Browse files
committedJul 21, 2019
Move functions in autoload dir
According to the doc the better approach is to have all the plugin involved functions in the autoload dir to defer the load phase. Autoload let us delay loading code until it's actually needed. Signed-off-by: fmount <[email protected]>
1 parent dc420d0 commit 5e29af0

File tree

3 files changed

+118
-111
lines changed

3 files changed

+118
-111
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tags

‎autoload/notes.vim

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
2+
function notes#version()
3+
echom "version " . g:notes_version
4+
endfunction
5+
6+
7+
function notes#edit(filename)
8+
let l:dir = expand(g:notes_folder)
9+
let l:tmpfilename = a:filename
10+
11+
"Check for file extention"
12+
let l:fileext = matchstr(l:tmpfilename, '\.\w*$')
13+
14+
if (empty(l:fileext))
15+
let l:newext = '.note'
16+
else
17+
let l:newext = ''
18+
endif
19+
20+
" Create l:dir if not existing
21+
if !isdirectory(l:dir)
22+
exe "silent !mkdir " . l:dir
23+
endif
24+
25+
" This is used when we don't pass any log
26+
if(fnamemodify(expand(a:filename, '/'), ':h') == "")
27+
exe "edit " . l:dir . "/" . a:filename . l:newext
28+
" This is used when we pass an absolute path
29+
elseif(expand(g:notes_folder) != fnamemodify(expand(a:filename, '/'), ':h'))
30+
let l:filename = fnamemodify(expand(a:filename, '/'), ':t')
31+
exe "edit " . l:dir . "/" . l:filename . l:newext
32+
else
33+
exe "edit " . a:filename . l:newext
34+
endif
35+
endfunction
36+
37+
function notes#list()
38+
let l:dir = expand(g:notes_folder)
39+
execute ":Explore " . l:dir
40+
endfunction
41+
42+
function notes#navigate(A,L,P)
43+
return split(globpath(g:notes_folder, "*"), "\n")
44+
endfunction
45+
46+
47+
function notes#delete(...)
48+
if(exists('a:1'))
49+
let note = a:1
50+
51+
"Check for the working directory
52+
if(expand(g:notes_folder) != fnamemodify(expand(note, '/'), ':h'))
53+
echomsg "Working directory doesn't match"
54+
return -1
55+
endif
56+
elseif ( &ft == 'help' )
57+
echohl Error
58+
echo "Cannot delete a help buffer!"
59+
echohl None
60+
return -1
61+
else
62+
let note = expand('%:p')
63+
endif
64+
65+
let delStatus = delete(note)
66+
67+
if(delStatus == 0)
68+
echo "Deleted " . note
69+
else
70+
echohl WarningMsg
71+
echo "Failed to delete " . note
72+
echohl None
73+
endif
74+
75+
return delStatus
76+
endfunction
77+
78+
79+
" Autosave for buffered notes
80+
function notes#update_buffer()
81+
"echomsg "Time elapsed: ".(localtime()-b:notes_start_time) "DEBUG TIME
82+
let l:note_time_elapsed = localtime() - b:notes_start_time
83+
84+
" check for the time elapsed, the value of the autosave variable and
85+
" the current file type (through the .note extension)
86+
if(matchstr(expand('%.t'),'\^*.note$') != "" && g:notes_autosave >= 1
87+
\&& l:note_time_elapsed >= g:notes_autosave_time)
88+
89+
"Try to update the buffer if modified
90+
let was_modified = &modified
91+
silent! w
92+
93+
if(was_modified && !&modified)
94+
echomsg "(AutoSaved at " . strftime("%H:%M:%S") . ")"
95+
let b:notes_start_time = localtime()
96+
endif
97+
98+
endif
99+
endfunction
100+
101+
" Enable and disable the autosave function for .note files
102+
function notes#autosave_toggle()
103+
if g:notes_autosave >= 1
104+
let g:notes_autosave = 0
105+
echo "Notes AutoSave disabled"
106+
else
107+
let g:notes_autosave = 1
108+
echo "Notes AutoSave enabled"
109+
endif
110+
endfunction
111+

‎plugin/notes.vim

+6-111
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" notes.vim - A vim plugin to take notes easily
2-
" Mantainer: bxor99
3-
" Version: 0.01-alpha
2+
" Mantainer: fmount
3+
" Version: 1.0
44
" AutoInstall: notes.vim
55
" ====== Enjoy ======
66
"
@@ -10,11 +10,12 @@ if exists('g:loaded_notes') || &cp
1010
endif
1111

1212
let g:loaded_notes = 0
13+
let g:notes_version = '1.0'
1314

1415
"For security reasons..
1516
"if v:version < 702
16-
" echomsg 'notes: You need at least Vim 7.2'
17-
" finish
17+
" echomsg 'notes: You need at least Vim 7.2'
18+
" finish
1819
"endif
1920

2021
if !exists('g:notes_folder')
@@ -30,7 +31,7 @@ augroup bufferset
3031
autocmd!
3132
autocmd BufRead,BufNewFile *.note set filetype=markdown
3233
autocmd BufRead,BufNewFile *.note let b:notes_start_time=localtime()
33-
autocmd BufRead,BufNewFile *.* syntax on
34+
"autocmd BufRead,BufNewFile *.* syntax on
3435
"Autosave settings
3536
autocmd CursorHold,BufRead *.note call notes#update_buffer()
3637
autocmd BufWritePre *.note let b:notes_start_time = localtime()
@@ -41,109 +42,3 @@ command! -complete=customlist,notes#navigate -nargs=1 Note call notes#edit(<f-ar
4142
command! -complete=customlist, NoteList call notes#list()
4243
command! -complete=customlist,notes#navigate -nargs=1 NoteDelete call notes#delete(<f-args>) | bdelete!
4344
command! NoteAutoSaveToggle :call notes#autosave_toggle()
44-
45-
function notes#edit(filename)
46-
let l:dir = expand(g:notes_folder)
47-
let l:tmpfilename = a:filename
48-
49-
"Check for file extention"
50-
let l:fileext = matchstr(l:tmpfilename, '\.\w*$')
51-
52-
if (empty(l:fileext))
53-
let l:newext = '.note'
54-
else
55-
let l:newext = ''
56-
endif
57-
58-
" Create l:dir if not existing
59-
if !isdirectory(l:dir)
60-
exe "silent !mkdir " . l:dir
61-
endif
62-
63-
" This is used when we don't pass any log
64-
if(fnamemodify(expand(a:filename, '/'), ':h') == "")
65-
exe "edit " . l:dir . "/" . a:filename . l:newext
66-
" This is used when we pass an absolute path
67-
elseif(expand(g:notes_folder) != fnamemodify(expand(a:filename, '/'), ':h'))
68-
let l:filename = fnamemodify(expand(a:filename, '/'), ':t')
69-
exe "edit " . l:dir . "/" . l:filename . l:newext
70-
else
71-
exe "edit " . a:filename . l:newext
72-
endif
73-
endfunction
74-
75-
function notes#list()
76-
let l:dir = expand(g:notes_folder)
77-
execute ":Explore " . l:dir
78-
endfunction
79-
80-
function notes#navigate(A,L,P)
81-
return split(globpath(g:notes_folder, "*"), "\n")
82-
endfunction
83-
84-
85-
function notes#delete(...)
86-
if(exists('a:1'))
87-
let note = a:1
88-
89-
"Check for the working directory
90-
if(expand(g:notes_folder) != fnamemodify(expand(note, '/'), ':h'))
91-
echomsg "Working directory doesn't match"
92-
return -1
93-
endif
94-
elseif ( &ft == 'help' )
95-
echohl Error
96-
echo "Cannot delete a help buffer!"
97-
echohl None
98-
return -1
99-
else
100-
let note = expand('%:p')
101-
endif
102-
103-
let delStatus = delete(note)
104-
105-
if(delStatus == 0)
106-
echo "Deleted " . note
107-
else
108-
echohl WarningMsg
109-
echo "Failed to delete " . note
110-
echohl None
111-
endif
112-
113-
return delStatus
114-
endfunction
115-
116-
117-
" Autosave for buffered notes
118-
function notes#update_buffer()
119-
"echomsg "Time elapsed: ".(localtime()-b:notes_start_time) "DEBUG TIME
120-
let l:note_time_elapsed = localtime() - b:notes_start_time
121-
122-
" check for the time elapsed, the value of the autosave variable and
123-
" the current file type (through the .note extension)
124-
if(matchstr(expand('%.t'),'\^*.note$') != "" && g:notes_autosave >= 1
125-
\&& l:note_time_elapsed >= g:notes_autosave_time)
126-
127-
"Try to update the buffer if modified
128-
let was_modified = &modified
129-
silent! w
130-
131-
if(was_modified && !&modified)
132-
echomsg "(AutoSaved at " . strftime("%H:%M:%S") . ")"
133-
let b:notes_start_time = localtime()
134-
endif
135-
136-
endif
137-
endfunction
138-
139-
" Enable and disable the autosave function for .note files
140-
function notes#autosave_toggle()
141-
if g:notes_autosave >= 1
142-
let g:notes_autosave = 0
143-
echo "Notes AutoSave disabled"
144-
else
145-
let g:notes_autosave = 1
146-
echo "Notes AutoSave enabled"
147-
endif
148-
endfunction
149-

0 commit comments

Comments
 (0)
Please sign in to comment.