Skip to content

Commit

Permalink
move todo state setting prsing a document function.
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMickler committed May 22, 2019
1 parent bb1569d commit 984df1a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 67 deletions.
76 changes: 73 additions & 3 deletions ftplugin/orgmode/liborgmode/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
except:
from UserList import UserList

from orgmode import settings

from orgmode.liborgmode.base import MultiPurposeList, flatten_list, Direction, get_domobj_range
from orgmode.liborgmode.headings import Heading, HeadingList

from orgmode.py3compat.encode_compatibility import *
from orgmode.py3compat.unicode_compatibility import *

import re
REGEX_LOGGING_MODIFIERS = re.compile(r"[!@/]")

class Document(object):
u"""
Representation of a whole org-mode document.
Expand Down Expand Up @@ -51,7 +56,8 @@ def __init__(self):
self._tag_column = 77

# TODO this doesn't differentiate between ACTIVE and FINISHED todo's
self.todo_states = [u'TODO', u'DONE']
self.todo_states_stripped = self.get_settings_todo_states(True)
self.todo_states = self.get_settings_todo_states(False)

def __unicode__(self):
if self.meta_information is None:
Expand All @@ -61,6 +67,65 @@ def __unicode__(self):
def __str__(self):
return u_encode(self.__unicode__())

def get_done_states(self, strip_access_key=True):
all_states = self.get_todo_states(strip_access_key)
done_states = list([ done_state for x in all_states for done_state in x[1]])

return done_states

def parse_todo_settings(self, setting, strip_access_key = True):
def parse_states(s, stop=0):
res = []
if not s:
return res
if type(s[0]) in (unicode, str):
r = []
for i in s:
_i = i
if type(_i) == str:
_i = u_decode(_i)
if type(_i) == unicode and _i:
if strip_access_key and u'(' in _i:
_i = _i[:_i.index(u'(')]
if _i:
r.append(_i)
else:
_i = REGEX_LOGGING_MODIFIERS.sub("", _i)
r.append(_i)
if not u'|' in r:
if not stop:
res.append((r[:-1], [r[-1]]))
else:
res = (r[:-1], [r[-1]])
else:
seperator_pos = r.index(u'|')
if not stop:
res.append((r[0:seperator_pos], r[seperator_pos + 1:]))
else:
res = (r[0:seperator_pos], r[seperator_pos + 1:])
elif type(s) in (list, tuple) and not stop:
for i in s:
r = parse_states(i, stop=1)
if r:
res.append(r)
return res
return parse_states(setting)


def get_settings_todo_states(self, strip_access_key=True):
u""" Returns a list containing a tuple of two lists of allowed todo
states split by todo and done states. Multiple todo-done state
sequences can be defined.
:returns: [([todo states], [done states]), ..]
"""
states = settings.get(u'org_todo_keywords', [])

if type(states) not in (list, tuple):
return []

return self.parse_todo_settings(states, strip_access_key)

def get_all_todo_states(self):
u""" Convenience function that returns all todo and done states and
sequences in one big list.
Expand All @@ -71,7 +136,7 @@ def get_all_todo_states(self):
# TODO This is not necessary remove
return flatten_list(self.get_todo_states())

def get_todo_states(self):
def get_todo_states(self, strip_access_key=True):
u""" Returns a list containing a tuple of two lists of allowed todo
states split by todo and done states. Multiple todo-done state
sequences can be defined.
Expand All @@ -82,7 +147,12 @@ def get_todo_states(self):
# TODO this should be made into property so todo states can be set like
# this too.. or there was also some todo property around... oh well..
# TODO there is the same method in vimbuffer
return self.todo_states

ret = self.todo_states
if strip_access_key:
ret = self.todo_states_stripped

return ret

@property
def tabstop(self):
Expand Down
2 changes: 1 addition & 1 deletion ftplugin/orgmode/plugins/Todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def init_org_todo(cls):

if all_states is None:
vim.command(u_encode(u'bw'))
echom(u'No todo states avaiable for buffer %s' % vim.current.buffer.name)
echom(u'No todo states available for buffer %s' % vim.current.buffer.name)

for idx, state in enumerate(all_states):
pairs = [split_access_key(x, sub=u' ') for x in it.chain(*state)]
Expand Down
64 changes: 1 addition & 63 deletions ftplugin/orgmode/vimbuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
is UTF-8.
"""


try:
from collections import UserList
except:
Expand All @@ -33,7 +34,6 @@
from orgmode.py3compat.encode_compatibility import *
from orgmode.py3compat.unicode_compatibility import *


class VimBuffer(Document):
def __init__(self, bufnr=0):
u"""
Expand Down Expand Up @@ -89,68 +89,6 @@ def changedtick(self):
def changedtick(self, value):
self._changedtick = value

def get_done_states(self, strip_access_key=True):
all_states = self.get_todo_states(strip_access_key)
done_states = list([ done_state for x in all_states for done_state in x[1]])

return done_states

def get_todo_states(self, strip_access_key=True):
u""" Returns a list containing a tuple of two lists of allowed todo
states split by todo and done states. Multiple todo-done state
sequences can be defined.
:returns: [([todo states], [done states]), ..]
"""
states = settings.get(u'org_todo_keywords', [])
# TODO this function gets called too many times when change of state of
# one todo is triggered, check with:
# print(states)
# this should be changed by saving todo states into some var and only
# if new states are set hook should be called to register them again
# into a property
# TODO move this to documents.py, it is all tangled up like this, no
# structure...
if type(states) not in (list, tuple):
return []

def parse_states(s, stop=0):
res = []
if not s:
return res
if type(s[0]) in (unicode, str):
r = []
for i in s:
_i = i
if type(_i) == str:
_i = u_decode(_i)
if type(_i) == unicode and _i:
if strip_access_key and u'(' in _i:
_i = _i[:_i.index(u'(')]
if _i:
r.append(_i)
else:
r.append(_i)
if not u'|' in r:
if not stop:
res.append((r[:-1], [r[-1]]))
else:
res = (r[:-1], [r[-1]])
else:
seperator_pos = r.index(u'|')
if not stop:
res.append((r[0:seperator_pos], r[seperator_pos + 1:]))
else:
res = (r[0:seperator_pos], r[seperator_pos + 1:])
elif type(s) in (list, tuple) and not stop:
for i in s:
r = parse_states(i, stop=1)
if r:
res.append(r)
return res

return parse_states(states)

def update_changedtick(self):
if self.bufnr == vim.current.buffer.number:
self._changedtick = int(vim.eval(u_encode(u'b:changedtick')))
Expand Down

0 comments on commit 984df1a

Please sign in to comment.