Skip to content

Commit

Permalink
Adds TaskWikiOpen command
Browse files Browse the repository at this point in the history
Co-authored-by: Tomáš Janoušek <tomi@nomi.cz>
  • Loading branch information
nkakouros and liskin committed Dec 22, 2021
1 parent 925d8ed commit cbb47c6
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ RUN apk add --no-cache \
make \
patchelf \
tzdata \
xdg-utils \
xvfb-run
RUN ln -sf /usr/share/zoneinfo/Etc/UTC /etc/localtime

Expand Down
11 changes: 11 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,15 @@ selected tasks.

Supports |cmdline-completion|.

*:TaskWikiOpen*
Opens annotations in the selected task(s) containing links to local files,
urls, etc.

*:TaskWikiNote*
Adds a note to the task under cursor and opens it to be edited. This
command is compatible with the taskopen utility (available at
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().open()"
execute "command! -buffer -range TaskWikiNote :<line1>,<line2>" . g:taskwiki_py . "SelectedTasks().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
37 changes: 37 additions & 0 deletions taskwiki/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import six
import sys
import vim # pylint: disable=F0401
import subprocess

# Insert the taskwiki on the python path
BASE_DIR = vim.eval("s:plugin_path")
Expand Down Expand Up @@ -85,6 +86,14 @@ def __init__(self):
if not self.tasks:
print("No tasks selected.")

self.taskopen_notes_folder = (
util.get_var("taskwiki_taskopen_notes_folder", default="~/tasknotes")
)

self.taskopen_notes_regex = (
util.get_var("taskwiki_taskopen_notes_regex") or "Notes"
)

@classmethod
def save_action(cls, method, *args):
cls.last_action = {'method': method, 'args': args}
Expand Down Expand Up @@ -125,6 +134,34 @@ def info(self):
util.show_in_split(out, name='info', activate_cursorline=True)
break # Show only one task

@errors.pretty_exception_handler
def open(self):
compatible_annotation_found = False
for vimwikitask in self.tasks:
for annotation in vimwikitask.task["annotations"]:
annotation = annotation["description"]
proc = subprocess.Popen(["xdg-open", annotation])
try:
proc.wait(0.3)
except subprocess.TimeoutExpired:
compatible_annotation_found = True

if not compatible_annotation_found:
print("No compatible annotation found.")

@errors.pretty_exception_handler
def note(self):
for vimwikitask in self.tasks:
if self.taskopen_notes_regex not in [
a["description"] for a in vimwikitask.task["annotations"]
]:
self.annotate(self.taskopen_notes_regex)

note_path = os.path.join(self.taskopen_notes_folder,
vimwikitask.task["uuid"] + ".md")
vim.command("edit " + note_path)
break # Add not to only one task

@errors.pretty_exception_handler
def edit(self):
for vimwikitask in self.tasks:
Expand Down
94 changes: 94 additions & 0 deletions tests/test_selected.py
Original file line number Diff line number Diff line change
Expand Up @@ -1372,3 +1372,97 @@ def execute(self):

self.tasks[0].refresh()
assert self.tasks[0]['project'] == "Home"


class TestAddTaskNote(IntegrationTest):

viminput = """
* [ ] test task 1 #{uuid}
"""

vimoutput = """
* [ ] test task 1 #{uuid}
"""

tasks = [
dict(description="test task 1"),
]

def execute(self):
# Create a note
self.command("let g:taskwiki_taskopen_notes_folder='~'")
self.command(
"TaskWikiNote",
regex='Task "test task 1" annotated',
lines=3
)
self.command("w", silent=False)
self.command("bd", silent=False)

# Re-opening the note should not create a new one
self.command(
"TaskWikiNote",
regex=" 0L, 0C$"
)
self.command("w", silent=False)
self.command("bd", silent=False)

# Note should be listed in task annotations
self.command("TaskWikiInfo")

output = '\n'.join(self.read_buffer())

data = r"Annotation of 'Notes' added"

assert re.search(data, output, re.MULTILINE)

self.command("bd")


class TestTaskOpenWithNoAnnotation(IntegrationTest):

viminput = """
* [ ] test task 1 #{uuid}
"""

vimoutput = """
* [ ] test task 1 #{uuid}
"""

tasks = [
dict(description="test task 1"),
]

def execute(self):
self.command(
"TaskWikiOpen",
regex='No compatible annotation found.',
lines=1
)


class TestTaskOpenWithIncompatibleAnnotation(IntegrationTest):

viminput = """
* [ ] test task 1 #{uuid}
"""

vimoutput = """
* [ ] test task 1 #{uuid}
"""

tasks = [
dict(description="test task 1"),
]

def execute(self):
# Create an annotation
self.command(
"TaskWikiAnnotate what is this",
silent=False
)
self.command(
"TaskWikiOpen",
regex='No compatible annotation found.',
lines=1
)

0 comments on commit cbb47c6

Please sign in to comment.