From a5eea434655a2c704db0b324faf92d004d511aa8 Mon Sep 17 00:00:00 2001 From: Mark Harrison Date: Mon, 4 Oct 2010 15:30:16 -0400 Subject: [PATCH] Move common function/vars to autoload 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. --- autoload/vimtodo.vim | 36 ++++++++++++++++++++++++++++++++++++ doc/todo.txt | 4 ++-- ftplugin/todo.vim | 42 +++++++++++------------------------------- syntax/todo.vim | 5 ++++- test/setup_tests.inc | 3 +++ 5 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 autoload/vimtodo.vim diff --git a/autoload/vimtodo.vim b/autoload/vimtodo.vim new file mode 100644 index 0000000..0380b5e --- /dev/null +++ b/autoload/vimtodo.vim @@ -0,0 +1,36 @@ +" Vim filetype plugin for heirarchical TODO lists +" Maintainer: Mark Harrison +" 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}}} diff --git a/doc/todo.txt b/doc/todo.txt index 5d4dc33..7525312 100644 --- a/doc/todo.txt +++ b/doc/todo.txt @@ -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/. diff --git a/ftplugin/todo.vim b/ftplugin/todo.vim index f459ce3..609780c 100644 --- a/ftplugin/todo.vim +++ b/ftplugin/todo.vim @@ -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('Todo'.a:funcname) @@ -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) @@ -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 @@ -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 @@ -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 = { @@ -293,8 +271,8 @@ call s:Map("ca", "ArchiveDone") " ds - Datestamp {{{1 iab ds =strftime("%Y-%m-%d") " 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"]. \' =strftime("%Y-%m-%d")' "1}}} @@ -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 @@ -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"] diff --git a/syntax/todo.vim b/syntax/todo.vim index f1efc6b..de5c38e 100644 --- a/syntax/todo.vim +++ b/syntax/todo.vim @@ -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])/ @@ -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\"". diff --git a/test/setup_tests.inc b/test/setup_tests.inc index 1a429ac..3aa72d9 100644 --- a/test/setup_tests.inc +++ b/test/setup_tests.inc @@ -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