Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow custom mappings #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions ftplugin/timesheet.vim
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
" Create a new entry.
" followed_by: string to append after the timestamp
" optional parameter: set to false (e.g. 0) to _not_ go into insert mode
" afterwards
function! s:NewTimesheetEntry(followed_by, ...)
let l:insertmode = get(a:, 1, 1)
" Create a new timestamp entry at the end of the file.
function! s:NewTimesheetEntry()
" Go to the end of the file, because that's the only place where
" inserting a new timestamped item makes sense.
normal G
call cursor(line('$'), 0)
" Look backwards for a date.
let l:dateline = search('\v^\d{4}-\d{2}-\d{2}:$', 'bcnw')
" If there's none or it's not the current day, create one.
if !l:dateline || getline(l:dateline) != strftime("%Y-%m-%d:")
execute "normal o\n" . strftime("%Y-%m-%d:")
call append(line('$'), '')
call append(line('$'), strftime("%Y-%m-%d:"))
endif
" Insert the timestamp and start insert mode.
execute "normal o" . strftime("%H%M") . a:followed_by
if l:insertmode
startinsert!
endif
call append(line('$'), strftime("%H%M"))
endfunction

nnoremap <Plug>(Timesheet) :call <SID>NewTimesheetEntry()<CR>

" Some mappings to insert a timestamped new line.
nnoremap <buffer> <LocalLeader>n :call <SID>NewTimesheetEntry(' ')<CR>
nnoremap <buffer> <LocalLeader>s :call <SID>NewTimesheetEntry('.', 0)<CR>
nnoremap <buffer> <LocalLeader>c :call <SID>NewTimesheetEntry('^', 0)<CR>
if !hasmapto('<Plug>(Timesheet)')
nmap <buffer> <LocalLeader>n <Plug>(Timesheet)GA<Space><Space>
nmap <buffer> <LocalLeader>s <Plug>(Timesheet)GA.<C-C>
nmap <buffer> <LocalLeader>c <Plug>(Timesheet)GA^<C-C>
endif