-
Notifications
You must be signed in to change notification settings - Fork 2
/
ftplugin.vim
518 lines (465 loc) · 17.1 KB
/
ftplugin.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
" @Author: Tom Link (mailto:micathom AT gmail com?subject=[vim])
" @Website: https://github.com/tomtom
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Last Change: 2018-04-04
" @Revision: 434
if !exists('g:ttodo#ftplugin#notefmt')
if exists('g:ttodo#ftplugin#notef') " && g:ttodo#ftplugin#notef !=# 'notes/%s/%s-%s.md'
let g:ttodo#ftplugin#notefmt = printf(g:ttodo#ftplugin#notef, '${name}', '${date}', '${index}')
else
let g:ttodo#ftplugin#notefmt = 'notes/${year}/${name}/${date}-${index}.md' "{{{2
endif
endif
if !exists('g:ttodo#ftplugin#note_prefix')
" Prefix for references to notes.
"
" Possible (potentially useful) values:
" - todo:// (opens the file in SimpleTask)
" - root:// (SimpleTask: path relative to todo.txt file)
" - file:// (less useful since it would require an absolute path)
let g:ttodo#ftplugin#note_prefix = 'root://' "{{{2
endif
if !exists('g:ttodo#ftplugin#edit_note')
" If non-empty, edit a newly added reference to a note right away.
"
" Possible (potentially useful) values:
" - split
" - hide edit
" - tabedit
let g:ttodo#ftplugin#edit_note = '' "{{{2
endif
if !exists('g:ttodo#ftplugin#add_at_eof')
" If true, the <cr> or <c-cr> map will make ttodo add a new task at
" the end of the file. Otherwise the task will be added below the
" current line.
" Subtasks will always be added below the current line.
let g:ttodo#ftplugin#add_at_eof = 1 "{{{2
endif
if !exists('g:ttodo#ftplugin#rec_copy')
" If true, marking a recurring task as "done" will mark the old task
" as completed and will then create a new updated task.
let g:ttodo#ftplugin#rec_copy = 1 "{{{2
endif
if !exists('g:ttodo#ftplugin#new_subtask_copy_pri')
" If true, copy the parent task's priority when creating subtasks.
let g:ttodo#ftplugin#new_subtask_copy_pri = 0 "{{{2
endif
function! ttodo#ftplugin#Archive(filename) abort "{{{3
let basename = fnamemodify(a:filename, ':t')
let arc = fnamemodify(a:filename, ':p:h') .'/done.txt'
if filereadable(arc) && !filewritable(arc)
throw 'TTodo: Cannot write: '. arc
endif
if !filewritable(a:filename)
throw 'TTodo: Cannot write: '. a:filename
endif
let done = []
let undone = []
let today = strftime(g:tlib#date#date_format)
let filetasks = ttodo#GetFileTasks({}, a:filename, {})
let lines = readfile(a:filename)
for lnum0 in range(len(lines))
let task = filetasks.GetQfeByLnum(lnum0 + 1).task
" let line = lines[lnum0]
let line = task.text
if line != lines[lnum0]
throw 'Ttodo: Internal error: Lines differ: '. string(line) .' != '. string(lines[lnum0])
endif
if get(task, 'done', 0) && !get(task, 'has_subtasks', 0) && !get(task, 'pending', 0)
let dline = tlib#string#TrimLeft(line)
let dline .= ' archive:'. today
let dline .= ' source:'. basename
call add(done, dline)
else
call add(undone, line)
endif
endfor
if !empty(done)
if filereadable(arc)
let done = readfile(arc) + done
endif
call writefile(done, arc)
call writefile(undone, a:filename)
endif
endf
function! ttodo#ftplugin#ArchiveCurrentBuffer() abort "{{{3
update
call ttodo#ftplugin#Archive(expand('%:p'))
edit
endf
function! ttodo#ftplugin#Note() abort "{{{3
let line = getline('.')
let task = ttodo#ParseTask(line, expand('%:p'))
Tlibtrace 'ttodo', task
let nname = get(task.lists, 0, 'misc')
let nname = substitute(nname, '\W', '_', 'g')
let date = strftime('%Y%m%d')
let year = strftime('%Y')
let dir = expand('%:p:h')
Tlibtrace 'ttodo', nname, dir
let fargs = {'name': nname, 'date': date, 'year': year, 'index': 0}
while 1
let shortname = tlib#string#Format(g:ttodo#ftplugin#notefmt, fargs, '$')
let filename = tlib#file#Join([dir, shortname])
if filereadable(filename)
let fargs.n += 1
else
let notename = g:ttodo#ftplugin#note_prefix . shortname
break
endif
endwh
Tlibtrace 'ttodo', filename
call setline('.', join([line, notename]))
call tlib#dir#Ensure(fnamemodify(filename, ':p:h'))
if !empty(g:ttodo#ftplugin#edit_note)
exec g:ttodo#ftplugin#edit_note fnameescape(filename)
endif
endf
" Values for copytags:
" 1 ... Copy tags
" 2 ... Copy tags & mark as dependency
function! ttodo#ftplugin#New(move, copytags, mode, ...) abort "{{{3
Tlibtrace 'ttodo', a:move, a:copytags, a:mode, a:000
if a:mode == 'i'
let o = "\<c-m>"
else
let o = "o"
endif
let new = strftime(g:tlib#date#date_format)
if a:copytags == 2
let [isnew, id] = s:EnsureIdAtLine(line('.'))
if isnew
if a:mode == 'i'
let o = "\<End> id:". id . o
else
let o = 'A id:'. id ."\<Esc>". o
endif
endif
let new .= ' dep:'. id
endif
let i0 = indent('.')
Tlibtrace 'ttodo', o, new, i0
" new item after indented line
if i0 > 0 && empty(a:move)
let move_down = ''
for pos1 in range(line('.') + 1, line('$'))
let i1 = indent(pos1)
Tlibtrace 'ttodo', i1
if i1 > i0
let move_down .= 'j'
else
break
endif
endfor
let keys = o . new .' '
if !empty(move_down)
let keys .= "\<Esc>ddk" . move_down . 'pA'
endif
Tlibtrace 'ttodo', keys
return keys
else
let task = a:0 >= 1 ? a:1 : ttodo#ParseTask(getline('.'), expand('%:p'))
if a:move == '>'
let new = new .' '
if g:ttodo#ftplugin#new_subtask_copy_pri
Tlibtrace 'ttodo', g:ttodo#ftplugin#new_subtask_copy_pri, new
let new = s:MaybeCopyPriority(task, new)
endif
let prefix = matchstr(getline('.'), '^\s\+')
Tlibtrace 'ttodo', prefix
if g:ttodo#ftplugin#add_at_eof
for lnum in range(line('.'), line('$') - 1)
let i1 = indent(lnum + 1)
Tlibtrace 'ttodo', lnum, i0, i1
if i1 == 0 || i1 <= i0
let expr = lnum .'gg'. o ."\<Esc>I". prefix ."\<c-t>". new
Tlibtrace 'ttodo', expr
return expr
endif
endfor
endif
Tlibtrace 'ttodo', o, new
return o ."\<c-t>" . new
else
let o .= "\<home>"
if a:copytags > 0
let new = ttodo#MaybeAppend(new, ttodo#FormatTags('@', task.lists))
let new = ttodo#MaybeAppend(new, ttodo#FormatTags('+', task.tags))
let new = ttodo#MaybeAppend(new, join(task.notes))
endif
let new = s:MaybeCopyPriority(task, new)
let move = a:move
if empty(move) && i0 == 0
let move = g:ttodo#ftplugin#add_at_eof ? 'G' : ''
" else " TODO: find last line with same indent
endif
if a:mode == 'i' && !empty(move)
let move = "\<c-\>\<c-o>"
endif
Tlibtrace 'ttodo', move, o, new
return move . o . new .' '
endif
endif
endf
function! s:MaybeCopyPriority(task, text) abort "{{{3
if has_key(a:task, 'pri')
return ttodo#MaybeSetPriority(a:text, a:task.pri)
else
return a:text
endif
endf
function! s:IsDone(line) abort "{{{3
return a:line =~# '^\s*x\s'
endf
function! ttodo#ftplugin#MarkDone(count, ...) abort "{{{3
TVarArg ['ignore_rec', 0]
Tlibtrace 'ttodo', a:count, ignore_rec
let donedate = strftime(g:tlib#date#date_format)
let lnum0 = line('.')
for lnum in range(lnum0, lnum0 + a:count)
let line = getline(lnum)
Tlibtrace 'ttodo', lnum, line
let task = ttodo#ParseTask(line, expand('%:p'))
Tlibtrace 'ttodo', task
if !get(task, 'done', 0)
if !ignore_rec
let rec = get(task, 'rec', '')
if !empty(rec)
if get(task, 'subtask', 0)
echohl Error
echom "TTodo: Cannot complete subtasks with a 'rec:' tag:" line
continue
elseif get(task, 'has_subtask', 0)
" TODO: Must copy the whole tree
echohl Error
echom "TTodo: Cannot complete top tasks with a 'rec:' tag:" line
continue
else
if g:ttodo#ftplugin#rec_copy
call ttodo#ftplugin#MarkDone(0, 1)
let line = ttodo#SetCreateDate(line)
call append('$', line)
let lnum = line('$')
endif
let shift = matchstr(rec, '\d\+\a$')
let due = get(task, 'due', '')
if !empty(due)
let refdate = rec =~ '^+' && !empty(due) ? due : donedate
let ndue = empty(due) ? donedate : due
Tlibtrace 'ttodo', rec, due, shift, refdate, ndue
while 1
let ndue = tlib#date#Shift(ndue, shift)
Tlibtrace 'ttodo', ndue
if ndue > refdate
break
endif
endwh
exec lnum
call s:MarkDueDate(ndue)
endif
if has_key(task, 't') && task.t =~# g:tlib#date#date_rx
let t0 = task.t
let t1 = tlib#date#Shift(t0, shift)
Tlibtrace 'ttodo', t1
exec lnum
call s:SetTag('t', g:tlib#date#date_rx, t1)
endif
continue
endif
endif
endif
if get(task, 'next', 0)
if line =~ '@next\s\+'
let line = substitute(line, '@next\s\+', '', '')
else
let line = substitute(line, '\s\+@next\>', '', '')
endif
" call setline(lnum, line)
endif
endif
if get(task, 'subtask', 0) && !has_key(task, 'parent')
let parent = s:GetParentID(lnum)
if !empty(parent)
let line = line .' parent:'. parent
" call setline(lnum, line .' parent:'. parent)
endif
endif
let line = substitute(line, '^\s*\zs\C\%(x\s\+\%('. g:tlib#date#date_rx .'\s\+\)\?\)\?', 'x '. donedate .' ', '')
call setline(lnum, line)
" exec lnum .'s/^\s*\zs\C\%(x\s\+\%('. g:tlib#date#date_rx .'\s\+\)\?\)\?/x '. donedate .' /'
endfor
exec lnum0 + a:count
endf
function! s:GetIdAtLine(lnum) abort "{{{3
let line = getline(a:lnum)
let filename = expand('%:p')
let task = ttodo#ParseTask(line, filename)
if !has_key(task, 'id')
exec a:lnum
call ttodo#ftplugin#AddId(0)
let task = ttodo#ParseTask(getline(a:lnum), filename)
endif
return task.id
endf
function! s:GetParentID(lnum0) abort "{{{3
let indent0 = indent(a:lnum0)
" let filename = expand('%:p')
for lnum in range(a:lnum0 - 1, 1, -1)
let indent = indent(lnum)
if indent < indent0
return s:GetIdAtLine(lnum)
" let line = getline(lnum)
" let task = ttodo#ParseTask(line, filename)
" if !has_key(task, 'id')
" exec lnum
" call ttodo#ftplugin#AddId(0)
" let task = ttodo#ParseTask(getline(lnum), filename)
" endif
" return task.id
endif
endfor
return ''
endf
function! ttodo#ftplugin#MarkDue(unit, count) abort "{{{3
if a:count == -1
elseif s:IsDone(getline('.'))
echohl WarningMsg
throw 'Ttodo: Cannot change finshed task'
echohl NONE
else
if type(a:count) == 0
let n = a:count
elseif type(a:count) == 1
let n = ttodo#InputNumber(a:count)
if n < 0
return
endif
else
throw 'ttodo#ftplugin#MarkDue: count must be a number or a string'
endif
let today = strftime(g:tlib#date#date_format)
let due = tlib#date#Shift(today, n . a:unit)
call s:MarkDueDate(due)
endif
endf
function! s:MarkDueDate(date) abort "{{{3
call s:SetTag('due', '\%(\<today\>\|'. g:tlib#date#date_rx .'\)', a:date)
" exec 's/\C\%(\s\+due:'. g:tlib#date#date_rx .'\|$\)/ due:'. a:date .'/'
endf
function! s:SetTag(name, rx, value) abort "{{{3
exec 's/\C\%(\s\+'. a:name .':'. escape(a:rx, '/') .'\|$\)/ '. a:name .':'. escape(a:value, '/') .'/'
endf
function! ttodo#ftplugin#SetPriority(count, ...) abort "{{{3
" TLogVAR a:count
if s:IsDone(getline('.'))
echohl WarningMsg
throw 'Ttodo: Cannot change finshed task'
echohl NONE
else
let category = a:0 >= 1 ? a:1 : tlib#string#Input('New task category [A-Z]: ')
let category = toupper(category)
if category =~ '\C^[A-Z]$'
exec 's/^\s*\zs\C\%((\u)\s\+\)\?/('. category .') /'
else
echohl WarningMsg
echom 'Invalid category (must be A-Z):' category
echohl NONE
endif
endif
endf
function! ttodo#ftplugin#Agent(world, selected, fn, ...) abort "{{{3
" TLogVAR a:fn, a:000
let cmd = printf('call call(%s, %s)', string(a:fn), string(map(copy(a:000), 's:AgentEvalArg(v:val)')))
Tlibtrace 'ttodo', cmd
let world = tlib#qfl#RunCmdOnSelected(a:world, a:selected, cmd)
return world
endf
function! s:AgentEvalArg(arg) abort "{{{3
if type(a:arg) == 1 && a:arg =~ '^\\='
return eval(substitute(a:arg, '^\\=', '', ''))
elseif type(a:arg) == 1 && a:arg =~ '^".\{-}"$'
return matchstr(a:arg, '^"\zs.\{-}\ze"$')
else
return a:arg
endif
endf
function! s:EnsureIdAtLine(lnum, ...) abort "{{{3
let filename = a:0 >= 1 ? a:1 : expand('%:p')
let filetasks = a:0 >= 2 ? a:2 : ttodo#GetFileTasks({}, filename, {})
let fqfl = filetasks.qfl
let line = getline(a:lnum)
let task = ttodo#ParseTask(line, filename)
if !has_key(task, 'id')
let qfe = filetasks.GetQfeByLnum(a:lnum)
let ttask = string(empty(qfe) ? task : qfe)
if v:version < 800 || !has('+reltime')
let id = tlib#hash#Adler32(ttask)
else
" let id = 't'. tlib#number#ConvertBase(localtime(), 36)
let id = tlib#number#ConvertBase(str2nr(substitute(reltimestr(reltime()), '\.', '', '')), 36)
endif
Tlibtrace 'ttodo', id
return [1, id]
else
return [0, task.id]
endif
endf
function! ttodo#ftplugin#AddId(count) abort "{{{3
let filename = expand('%:p')
let filetasks = ttodo#GetFileTasks({}, filename, {})
let fqfl = filetasks.qfl
for lnum in range(line('.'), line('.') + a:count)
let [isnew, id] = s:EnsureIdAtLine(lnum, filename, filetasks)
if isnew
let line = getline(lnum) .' id:'. id
Tlibtrace 'ttodo', lnum, line
call setline(lnum, line)
endif
endfor
endf
function! ttodo#ftplugin#SyntaxDue() abort "{{{3
let overdue_rx = ttodo#GetOverdueRx({'bufname': '%'})
if !empty(overdue_rx)
exec 'syntax match TtodoOverdue /'. escape(overdue_rx, '/') .'/'
endif
exec 'syntax match TtodoDue /\<due:\%(today\|'. strftime(g:tlib#date#date_format) .'\)\>/'
endf
function! ttodo#ftplugin#AddDep() abort "{{{3
let filename = expand('%:p')
let filetasks = ttodo#GetFileTasks({}, filename, {})
" TLogVAR len(filetasks.qfl), len(filetasks.task_by_id)
let with_id = values(map(copy(filetasks.task_by_id), 'v:key ."\t". v:val.text'))
if empty(with_id)
echom 'ttodo#ftplugin#AddDep: No IDs'
else
let dep = tlib#input#List('s', 'Select dependency:', with_id)
if !empty(dep)
let id = matchstr(dep, '^[^\t]\+\ze\t')
call setline('.', getline('.') .' dep:'. id)
endif
endif
endf
function! ttodo#ftplugin#Duplicate(count, markdone) abort "{{{3
" let pos = getpos('.')
Tlibtrace 'ttodo', a:count, a:markdone
for lnum in range(line('.'), line('.') + a:count - 1)
Tlibtrace 'ttodo', lnum
exec lnum
if indent('.') > 0
norm! yy
if a:count > 0
exec lnum + a:count - 1
endif
norm! p
else
norm! yyGp
endif
if a:markdone
let p1 = getpos('.')
exec lnum
" call setpos('.', pos)
call ttodo#ftplugin#MarkDone(0)
call setpos('.', p1)
endif
endfor
endf