Skip to content

Commit

Permalink
Merge pull request #69 from kxtcd950/master
Browse files Browse the repository at this point in the history
Added tri-state checkboxes to conform to Emacs' org mode checkboxes
  • Loading branch information
danielmagnussons authored Feb 21, 2018
2 parents ab2b1c0 + 514814a commit 2f8f5ae
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
49 changes: 38 additions & 11 deletions orgmode.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,17 @@ def run(self, edit):
except ImportError:
view.show_at_center(found)

class CheckState:
Unchecked, Checked, Indeterminate, Error = range(1, 5);


class AbstractCheckboxCommand(sublime_plugin.TextCommand):

def __init__(self, *args, **kwargs):
super(AbstractCheckboxCommand, self).__init__(*args, **kwargs)
indent_pattern = r'^(\s*).*$'
summary_pattern = r'(\[\d*[/]\d*\])'
checkbox_pattern = r'(\[[X ]\])'
checkbox_pattern = r'(\[[X\- ]\])'
self.indent_regex = re.compile(indent_pattern)
self.summary_regex = re.compile(summary_pattern)
self.checkbox_regex = re.compile(checkbox_pattern)
Expand Down Expand Up @@ -292,8 +295,24 @@ def get_checkbox(self, line):
view.text_point(row, col_stop),
)

def is_checked(self, line):
return '[X]' in self.view.substr(line)
def get_check_state(self, line):
if '[-]' in self.view.substr(line):
return CheckState.Indeterminate
if '[ ]' in self.view.substr(line):
return CheckState.Unchecked
if '[X]' in self.view.substr(line):
return CheckState.Checked
return CheckState.Error

def get_check_char(self, check_state):
if check_state == CheckState.Unchecked:
return ' '
elif check_state == CheckState.Checked:
return 'X'
elif check_state == CheckState.Indeterminate:
return '-'
else:
return 'E'

def recalc_summary(self, region):
# print('recalc_summary')
Expand All @@ -303,7 +322,7 @@ def recalc_summary(self, region):
# print children
num_children = len(children)
checked_children = len(
[child for child in children if self.is_checked(child)])
[child for child in children if (self.get_check_state(child) == CheckState.Checked)])
# print ('checked_children: ' + str(checked_children) + ', num_children: ' + str(num_children))
return (num_children, checked_children)

Expand All @@ -314,9 +333,13 @@ def update_line(self, edit, region, parent_update=True):
return False
# update region checkbox
if checked_children == num_children:
self.toggle_checkbox(edit, region, True)
newstate = CheckState.Checked
else:
self.toggle_checkbox(edit, region, False)
if checked_children != 0:
newstate = CheckState.Indeterminate
else:
newstate = CheckState.Unchecked
self.toggle_checkbox(edit, region, newstate)
# update region summary
self.update_summary(edit, region, checked_children, num_children)

Expand Down Expand Up @@ -350,16 +373,20 @@ def toggle_checkbox(self, edit, region, checked=None, recurse_up=False, recurse_
checkbox = self.get_checkbox(region)
if not checkbox:
return False
# if checked is not specified, toggle checkbox
if checked is None:
checked = not self.is_checked(checkbox)
view.replace(edit, checkbox, '[%s]' % (
'X' if checked else ' '))
check_state = self.get_check_state(region)
if (check_state == CheckState.Unchecked) | (check_state == CheckState.Indeterminate):
check_state = CheckState.Checked
elif (check_state == CheckState.Checked):
check_state = CheckState.Unchecked
else:
check_state = checked
view.replace(edit, checkbox, '[%s]' % ( self.get_check_char(check_state)))
if recurse_down:
# all children should follow
children = self.find_children(region)
for child in children:
self.toggle_checkbox(edit, child, checked, recurse_down=True)
self.toggle_checkbox(edit, child, check_state, recurse_down=True)
if recurse_up:
# update parent
parent = self.find_parent(region)
Expand Down
2 changes: 1 addition & 1 deletion orgmode.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<key>name</key>
<string>orgmode.checkbox</string>
<key>match</key>
<string>(\[[xX ]\])\s?</string>
<string>(\[[xX\- ]\])\s?</string>
</dict>
<dict>
<key>name</key>
Expand Down

0 comments on commit 2f8f5ae

Please sign in to comment.