diff --git a/Default.sublime-keymap b/Default.sublime-keymap index 28a78b4..8d1301b 100644 --- a/Default.sublime-keymap +++ b/Default.sublime-keymap @@ -12,7 +12,7 @@ { "keys": ["enter"], "command": "smart_list", "context": [ { "key": "selector", "operator": "equal", "operand": "text.html.markdown" }, - { "key": "preceding_text", "operator": "regex_contains", "operand": "^\\s*([-+\\**]|\\d+\\.+)\\s+" } + { "key": "preceding_text", "operator": "regex_contains", "operand": "^\\s*([-+\\**\\>\\|%]+|[(]?\\d+[.)]+)\\s+" } ] }, { "keys": ["enter"], "command": "smart_list", "context": diff --git a/SmartMarkdown.sublime-settings b/SmartMarkdown.sublime-settings index 69d1fb2..2ab86e8 100644 --- a/SmartMarkdown.sublime-settings +++ b/SmartMarkdown.sublime-settings @@ -7,5 +7,9 @@ "pandoc_args": [], "pandoc_args_pdf": [], "pandoc_args_html": [], - "pandoc_args_docx": [] + "pandoc_args_docx": [], + + /* If true empty bullet will be converted into whitespaces when pressing enter, + if false empty bullet will be erased completely */ + "empty_list_bullet": false } \ No newline at end of file diff --git a/smart_list.py b/smart_list.py index c1e229f..ec82424 100644 --- a/smart_list.py +++ b/smart_list.py @@ -7,9 +7,10 @@ import sublime_plugin -ORDER_LIST_PATTERN = re.compile(r"(\s*)(\d+)(\.\s+)\S+") +ORDER_LIST_PATTERN = re.compile(r"(\s*[(]?)(\d+)([.)]\s+)\S+") UNORDER_LIST_PATTERN = re.compile(r"(\s*[-+\**]+)(\s+)\S+") -EMPTY_LIST_PATTERN = re.compile(r"(\s*([-+\**]|\d+\.+))\s+$") +EMPTY_LIST_PATTERN = re.compile(r"(\s*)([-+\**]|[(]?\d+[.)])(\s+)$") +NONLIST_PATTERN = re.compile(r"(\s*[>|%]+)(\s+)\S?") class SmartListCommand(sublime_plugin.TextCommand): @@ -32,7 +33,15 @@ def run(self, edit): match = EMPTY_LIST_PATTERN.match(before_point_content) if match: - self.view.erase(edit, before_point_region) + settings = sublime.load_settings("SmartMarkdown.sublime-settings") + if settings.get("empty_list_bullet"): + insert_text = match.group(1) + \ + re.sub(r'\S', ' ', str(match.group(2))) + \ + match.group(3) + self.view.erase(edit, before_point_region) + self.view.insert(edit, line_region.a, insert_text) + else: + self.view.erase(edit, before_point_region) break match = ORDER_LIST_PATTERN.match(before_point_content) @@ -49,6 +58,19 @@ def run(self, edit): self.view.insert(edit, region.a, "\n" + insert_text) break + match = NONLIST_PATTERN.match(before_point_content) + if match: + current_line = self.view.line(region.a) + prev_line = self.view.line(current_line.a - 1) + empty_nonlist = match.group(1) + match.group(2) + if (self.view.substr(prev_line) == empty_nonlist) and (self.view.substr(current_line) == empty_nonlist): + self.view.replace(edit, current_line, '') + self.view.replace(edit, prev_line, '') + else: + insert_text = match.group(1) + match.group(2) + self.view.insert(edit, region.a, "\n" + insert_text) + break + self.view.insert(edit, region.a, '\n') self.adjust_view()