Skip to content

Adds TaskWikiOpen command #270

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions doc/taskwiki.txt
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,8 @@ vn m |:TaskWikiMod|
n t |:TaskWikiTags|
vn + |:TaskWikiStart|
vn - |:TaskWikiStop|
n o |:TaskWikiOpen|
n n |:TaskWikiNote|

=============================================================================
7. COMMANDS *taskwiki-commands*
Expand Down Expand Up @@ -634,6 +636,16 @@ selected tasks.

Supports |cmdline-completion|.

*:TaskWikiOpen*
Opens annotations in the selected task(s) containing links to local files,
urls, etc. This command requires taskopen to be installed
(https://github.com/jschlatow/taskopen).

*:TaskWikiNote*
Adds a note to the task under cursor and opens it to be edited. This
command requires taskopen to be installed
(https://github.com/jschlatow/taskopen).

----------------------------------------------------------------------------
Interactive commands.

Expand Down
6 changes: 6 additions & 0 deletions ftplugin/vimwiki/taskwiki.vim
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ execute "command! -buffer -nargs=* TaskWikiTags :" . g:taskwiki_py .

" Commands that operate on tasks in the buffer
execute "command! -buffer -range TaskWikiInfo :<line1>,<line2>" . g:taskwiki_py . "SelectedTasks().info()"
execute "command! -buffer -range TaskWikiOpen :<line1>,<line2>" . g:taskwiki_py . "SelectedTasks().taskopen('open')"
execute "command! -buffer -range TaskWikiNote :<line1>,<line2>" . g:taskwiki_py . "SelectedTasks().taskopen('note')"
execute "command! -buffer -range TaskWikiEdit :<line1>,<line2>" . g:taskwiki_py . "SelectedTasks().edit()"
execute "command! -buffer -range TaskWikiLink :<line1>,<line2>" . g:taskwiki_py . "SelectedTasks().link()"
execute "command! -buffer -range TaskWikiGrid :<line1>,<line2>" . g:taskwiki_py . "SelectedTasks().grid()"
Expand Down Expand Up @@ -130,6 +132,8 @@ if !exists('g:taskwiki_suppress_mappings')
nnoremap <silent><buffer> <LocalLeader>hm :TaskWikiHistoryMonthly<CR>
nnoremap <silent><buffer> <LocalLeader>ha :TaskWikiHistoryAnnual<CR>
nnoremap <silent><buffer> <LocalLeader>i :TaskWikiInfo<CR>
nnoremap <silent><buffer> <LocalLeader>o :TaskWikiOpen<CR>
nnoremap <silent><buffer> <LocalLeader>n :TaskWikiNote<CR>
nnoremap <silent><buffer> <LocalLeader>l :TaskWikiLink<CR>
nnoremap <silent><buffer> <LocalLeader>m :TaskWikiMod<CR>
nnoremap <silent><buffer> <LocalLeader>p :TaskWikiProjects<CR>
Expand All @@ -149,6 +153,8 @@ if !exists('g:taskwiki_suppress_mappings')
vnoremap <silent><buffer> <LocalLeader>e :TaskWikiEdit<CR>
vnoremap <silent><buffer> <LocalLeader>g :TaskWikiGrid<CR>
vnoremap <silent><buffer> <LocalLeader>i :TaskWikiInfo<CR>
vnoremap <silent><buffer> <LocalLeader>o :TaskWikiOpen<CR>
vnoremap <silent><buffer> <LocalLeader>n :TaskWikiNote<CR>
vnoremap <silent><buffer> <LocalLeader>l :TaskWikiLink<CR>
vnoremap <silent><buffer> <LocalLeader>m :TaskWikiMod<CR>
vnoremap <silent><buffer> <LocalLeader>. :TaskWikiRedo<CR>
Expand Down
34 changes: 34 additions & 0 deletions taskwiki/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,40 @@ def info(self):
util.show_in_split(out, name='info', activate_cursorline=True)
break # Show only one task

@errors.pretty_exception_handler
def taskopen(self, mode):
if mode == "note":
args = "-n {task_id} -x 'echo'"
if mode == "open":
args = "-N {task_id}"

for vimwikitask in self.tasks:
output, exit_status = util.taskopen(args.format(task_id=vimwikitask["id"]))

if mode == "note":
if exit_status:
vim.command("echohl Error")
vim.command('echo "%s"' % output)
vim.command("echohl None")
# Using `echoerr` is too verbose and scary, using
# echohl+echo instead.
else:
vim.command("edit " + output)

if mode == "open":
if exit_status:
error_msg = "taskopen failed to open task " + str(
vimwikitask["id"]
)

vim.command('echo "%s"' % output)
vim.command("echohl Error")
vim.command('echo "%s"' % error_msg)
vim.command("echohl None")

else:
vim.command("echomsg 'Opened task %s'" % vimwikitask["id"])

@errors.pretty_exception_handler
def edit(self):
for vimwikitask in self.tasks:
Expand Down
48 changes: 48 additions & 0 deletions taskwiki/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import contextlib
import os
import pexpect
import random
import sys
import vim # pylint: disable=F0401
Expand Down Expand Up @@ -96,6 +97,8 @@ def tw_args_to_kwargs(args):
return output

def get_input(prompt="Enter: ", allow_empty=False, completion=None):
prompt = prompt.replace('"', '\\"')

if completion is not None:
value = vim.eval('input("%s", "", "%s")' % (prompt, completion))
else:
Expand Down Expand Up @@ -441,3 +444,48 @@ def is_midnight(dt):
"""

return dt.hour == 0 and dt.minute == 0 and dt.second == 0

def taskopen(args):
"""
Runs taskopen, handles its interactive prompts, returns final output.

Params:
args - arguments to be passed to the taskopen command
"""
try:
p = pexpect.spawn(
"taskopen " + args,
timeout=3,
echo=False,
)

prompts = [
"Type number\(s\): ",
"Config file.* does not exist. Do you want to create it? (Y/n)"
]

while not p.eof():
index = p.expect([pexpect.EOF, *prompts])

if index == 0:
last_lines = p.before.splitlines()
else:
prompt = p.before.splitlines()
prompt += [bytes(prompts[index-1].replace('.*', ''), "utf-8")]

input = get_input(b"\n".join(prompt).decode("utf-8"))

p.sendline(input)

p.close(force=True) # make sure the process is ended

except pexpect.exceptions.ExceptionPexpect as e:
if "command was not found" not in e.value:
return "taskopen is not installed", 127

if p.exitstatus == 3: # xdg-open: no method to open annotation
last_lines = [last_lines[-1]]

last_lines = [l.decode("utf-8").rstrip("\r\n") for l in last_lines]

return "\n".join(last_lines).strip(), p.exitstatus