Skip to content

Commit

Permalink
Move common function/vars to autoload
Browse files Browse the repository at this point in the history
Functions needed by both systems and the default variables have been moved
into an autoload file, and the default variable setting has been moved to a
function and called at the beginning of both the syntax/ftplugin file.

This solves an issue where if the syntax file is loaded first (if you have
syntax on before filetype plugin on in your .vimrc), it doesn't have access to
the default variables and task state function, causing errors.
  • Loading branch information
mivok committed Oct 4, 2010
1 parent 0a653d9 commit a5eea43
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
36 changes: 36 additions & 0 deletions autoload/vimtodo.vim
@@ -0,0 +1,36 @@
" Vim filetype plugin for heirarchical TODO lists
" Maintainer: Mark Harrison <mark@mivok.net>
" License: ISC - See LICENSE file for details

" Utility functions
" TodoParseTaskState - Parse TODO(t) into state and shortcut key {{{1
function! vimtodo#TodoParseTaskState(state)
let state=matchstr(a:state, '^[A-Z]\+')
let key=matchstr(a:state, '\(^[A-Z]\+(\)\@<=[a-zA-Z0-9]\()\)\@=')
return { "state": state, "key": key }
endfunction
"1}}}

" Default settings
" Set - setup script variables {{{1
function! vimtodo#Set(varname, value)
if !exists(a:varname)
exec "let" a:varname "=" string(a:value)
endif
endfunction
"1}}}

" Default variables {{{1
function! vimtodo#SetDefaultVars()
call vimtodo#Set("g:todo_states",
\[["TODO(t)", "|", "DONE(d)", "CANCELLED(c)"],
\["WAITING(w)", "CLOSED(l)"]])
call vimtodo#Set("g:todo_state_colors", { "TODO" : "Blue", "DONE": "Green",
\ "CANCELLED" : "Red", "WAITING": "Yellow", "CLOSED": "Grey" })
call vimtodo#Set("g:todo_checkbox_states", [[" ", "X"], ["+", "-", "."],
\["Y", "N", "?"]])
call vimtodo#Set("g:todo_log_done", 1)
call vimtodo#Set("g:todo_log_into_drawer", "LOGBOOK")
call vimtodo#Set("g:todo_done_file", "done.txt")
endfunction
"1}}}
4 changes: 2 additions & 2 deletions doc/todo.txt
Expand Up @@ -16,8 +16,8 @@ system, taking several ideas from Emacs' org-mode (http://www.orgmode.org/).
INSTALLATION *vimtodo-installation*

Untar the archive to ~/.vim. If you wish, untar to a temporary directory, and
copy the contents of the doc/, ftplugin/ and syntax/ directories to the
respective directories in your ~/.vim directory.
copy the contents of the autoload/, doc/, ftplugin/ and syntax/ directories to
the respective directories in your ~/.vim directory.

Alternatively, if you have pathogen installed, untar the entire archive to
~/.vim/bundle/vimtodo/.
Expand Down
42 changes: 11 additions & 31 deletions ftplugin/todo.vim
Expand Up @@ -18,13 +18,6 @@ set cpo&vim
"1}}}

" Utility Functions
" s:Set - setup script variables {{{1
function! s:Set(varname, value)
if !exists(a:varname)
exec "let" a:varname "=" string(a:value)
endif
endfunction
"1}}}
" s:Map - mapping helper function {{{1
function! s:Map(keys, funcname)
if !hasmapto('<Plug>Todo'.a:funcname)
Expand Down Expand Up @@ -61,13 +54,6 @@ function! s:NewScratchBuffer(name, split)
setlocal nowrap " This can be changed if needed
endfunction
"1}}}
" TodoParseTaskState - Parse TODO(t) into state and shortcut key {{{1
function! TodoParseTaskState(state)
let state=matchstr(a:state, '^[A-Z]\+')
let key=matchstr(a:state, '\(^[A-Z]\+(\)\@<=[a-zA-Z0-9]\()\)\@=')
return { "state": state, "key": key }
endfunction
"1}}}
" s:GetState - Gets the state for a line, and its index {{{1
function! s:GetState(line)
let line=getline(a:line)
Expand All @@ -89,7 +75,7 @@ function! s:IsDoneState(state)
" Note, having idx set to -1 (when there is no |) means we will be
" looking at the last item, which is the desired behavior.
for teststate in group[idx+0:]
if TodoParseTaskState(teststate)["state"] == a:state
if vimtodo#TodoParseTaskState(teststate)["state"] == a:state
return 1
endif
endfor
Expand All @@ -108,7 +94,7 @@ function! s:GetDoneStates()
let idx = idx + 1
endif
for state in group[idx :]
call add(states, TodoParseTaskState(state)['state'])
call add(states, vimtodo#TodoParseTaskState(state)['state'])
endfor
endfor
return states
Expand Down Expand Up @@ -192,16 +178,8 @@ endfunction
" 1}}}

" Settings
" Default variables {{{1
call s:Set("g:todo_states",
\[["TODO(t)", "|", "DONE(d)", "CANCELLED(c)"], ["WAITING(w)", "CLOSED(l)"]])
call s:Set("g:todo_state_colors", { "TODO" : "Blue", "DONE": "Green",
\ "CANCELLED" : "Red", "WAITING": "Yellow", "CLOSED": "Grey" })
call s:Set("g:todo_checkbox_states", [[" ", "X"], ["+", "-", "."],
\["Y", "N", "?"]])
call s:Set("g:todo_log_done", 1)
call s:Set("g:todo_log_into_drawer", "LOGBOOK")
call s:Set("g:todo_done_file", "done.txt")
" Load default variables {{{1
call vimtodo#SetDefaultVars()
"1}}}
" Per file variables {{{1
let s:PropertyVars = {
Expand Down Expand Up @@ -293,8 +271,8 @@ call s:Map("ca", "ArchiveDone")
" ds - Datestamp {{{1
iab ds <C-R>=strftime("%Y-%m-%d")<CR>
" cn, \cn - New todo entry {{{1
exe 'map \cn o'.TodoParseTaskState(g:todo_states[0][0])["state"].' ds '
exe 'iab cn '.TodoParseTaskState(g:todo_states[0][0])["state"].
exe 'map \cn o'.vimtodo#TodoParseTaskState(g:todo_states[0][0])["state"].' ds '
exe 'iab cn '.vimtodo#TodoParseTaskState(g:todo_states[0][0])["state"].
\' <C-R>=strftime("%Y-%m-%d")<CR>'
"1}}}

Expand Down Expand Up @@ -346,14 +324,16 @@ function! s:NextTaskState()
for group in g:todo_states
let stateidx = 0
while stateidx < len(group)
let teststate = TodoParseTaskState(group[stateidx])["state"]
let teststate = vimtodo#TodoParseTaskState(group[stateidx]
\)["state"]
if teststate == oldstate
let stateidx=(stateidx + 1) % len(group)
" Skip | separator
if group[stateidx] == "|"
let stateidx=(stateidx + 1) % len(group)
endif
let val=TodoParseTaskState(group[stateidx])["state"]
let val=vimtodo#TodoParseTaskState(
\group[stateidx])["state"]
call s:SetTaskState(val, oldstate, idx)
return
endif
Expand All @@ -375,7 +355,7 @@ function! s:PromptTaskState()
if statestr == "|"
continue
endif
let state = TodoParseTaskState(statestr)
let state = vimtodo#TodoParseTaskState(statestr)
if state["key"] != ""
call add(promptlist, state["state"]." (".state["key"].")")
let statekeys[state["key"]] = state["state"]
Expand Down
5 changes: 4 additions & 1 deletion syntax/todo.vim
Expand Up @@ -10,6 +10,9 @@ if exists("b:current_syntax")
finish
endif

" Load default variables if not already set
call vimtodo#SetDefaultVars()

syn match todoProject /+\S\+/
syn match todoContext /\s@\S\+/
syn match todoPriority /([A-Z])/
Expand Down Expand Up @@ -68,7 +71,7 @@ function! s:HighlightDone()
endif
let parsed = []
for state in group[idx+0:]
call add(parsed, TodoParseTaskState(state)["state"])
call add(parsed, vimtodo#TodoParseTaskState(state)["state"])
endfor
let match = join(parsed, "\\|")
exec "syn region todoDone start=\"^\\z(\\s*\\)\\%(".match."\\)\\s\"".
Expand Down
3 changes: 3 additions & 0 deletions test/setup_tests.inc
Expand Up @@ -2,6 +2,9 @@
" Use TAP
call vimtest#StartTap()

" Set the autoload path
set rtp+=..

" Load the todo plugin
" This should have the same effect as set ft=todo
source ../ftplugin/todo.vim
Expand Down

0 comments on commit a5eea43

Please sign in to comment.