Permalink
Browse files

try tab indent

  • Loading branch information...
1 parent caafaad commit 164993f5e5a920adc8a5f08214cd641b73886438 @erinata committed Apr 12, 2012
View
119 Bullet.py
@@ -0,0 +1,119 @@
+import sublime
+import sublime_plugin
+import re
+
+# TODO: TAB or space on a empty bullet will indent it
+# TODO: Backspace on a indented empty bullet will unindent it
+# TODO: Backspace on a unindented empty bullet will delete it
+# TODO: Better multi line pasting behaviour (esp the bug that if you paste a 2 lines content, a * will be added to the end of the pasting )
+# Just check whether the line is a empty line before inserting
+
+class BulletIndent(sublime_plugin.TextCommand):
+ def run(self, edit):
+ self.view.window().run_command("indent")
+
+class BulletUnindent(sublime_plugin.TextCommand):
+ def run(self, edit):
+ self.view.window().run_command("unindent")
+
+class Bullet(sublime_plugin.EventListener):
+ Modifying = False
+ is_markdown = False
+ last_line = 0
+ last_pos = 0
+ selectors = []
+
+ def __init__(self):
+ s = sublime.load_settings("Bullet.sublime-settings")
+ Bullet.selectors = s.get("markdown_bullet_selectors", [])
+
+ def on_activated(self, view):
+ for x in range(len(Bullet.selectors)):
+ if (view.score_selector(0,Bullet.selectors[x]) > 0):
+ Bullet.is_markdown = True
+ Bullet.update_row(self, view)
+ return
+ else:
+ Bullet.is_markdown = False
+
+ def on_selection_modified(self, view):
+ if (Bullet.is_markdown):
+ Bullet.update_row(self, view)
+
+ def update_row(self, view):
+ loc = view.sel()[0]
+ Bullet.last_pos = loc.begin()
+ row, col = view.rowcol(Bullet.last_pos)
+ Bullet.last_line = row
+
+ def on_modified(self, view):
+ if Bullet.Modifying == False:
+ Bullet.Modifying = True
+ if Bullet.is_markdown == True:
+ loc = view.sel()[0]
+ row, col = view.rowcol(loc.begin())
+ point_last_row = view.text_point(row-1,0)
+
+ #if (row - Bullet.last_line == 0):
+ # current_line_region = view.line(loc.begin())
+ # current_line = view.substr(current_line_region)
+ # if current_line != "":
+ # match_pattern = re.search("^( *|\t*)(\*|\-|\>|\+|[0-9]+\.)(.*)$",current_line)
+ # if match_pattern != None:
+ # if match_pattern.group(3) == "":
+ # if Bullet.last_pos >= loc.begin():
+ # if match_pattern.group(1) != "":
+ # edit = view.begin_edit()
+ # view.insert(edit,current_line_region.end()," ")
+ # view.erase(edit,sublime.Region(current_line_region.begin(),current_line_region.begin()+1))
+ # view.end_edit(edit)
+ # else:
+ # edit = view.begin_edit()
+ # view.erase(edit,current_line_region)
+ # view.end_edit(edit)
+ # else:
+ # match_space = re.search("^(( )( )+|\t)$", match_pattern.group(3))
+ # if match_space != None:
+ # if match_pattern.group(1) == None:
+ # indented_part = ""
+ # else:
+ # indented_part = match_pattern.group(1)
+ # insertion_location = current_line_region.begin()
+ # edit = view.begin_edit()
+ # view.erase(edit,current_line_region)
+ # view.insert(edit, insertion_location , "\t" + indented_part + match_pattern.group(2) + " ")
+ # view.end_edit(edit)
+ #elif (row - Bullet.last_line == 1):
+ if (row - Bullet.last_line == 1):
+ previous_line = view.substr(view.line(Bullet.last_pos))
+ if row != 0 and previous_line != "":
+ match_pattern = re.search("^( *|\t*)(\*|\-|\>|\+|[0-9]+\.)(.*)",previous_line)
+ if match_pattern != None:
+ if match_pattern.group(3) == " " or match_pattern.group(3) == "":
+ reg_remove = view.find("(\*|\-|\>|\+|[0-9]+\.)(.*)",point_last_row-1)
+ edit = view.begin_edit()
+ view.erase(edit,reg_remove)
+ view.end_edit(edit)
+ else:
+ if match_pattern.group(2) == "*":
+ insertion = "* "
+ elif match_pattern.group(2) == "-":
+ insertion = "- "
+ elif match_pattern.group(2) == ">":
+ insertion = "> "
+ elif match_pattern.group(2) == "+":
+ insertion = "+ "
+ else:
+ match_number = re.search("[0-9]+",match_pattern.group(2))
+ if match_number != None:
+ last_number = int(match_number.group(0))
+ insertion = str(last_number+1) + ". "
+ else:
+ insertion = ""
+ edit = view.begin_edit()
+ view.insert(edit, loc.end(), insertion)
+ view.end_edit(edit)
+
+
+ Bullet.update_row(self, view)
+ Bullet.Modifying = False
View
@@ -0,0 +1,8 @@
+{
+ "markdown_bullet_selectors" : ["text.html.markdown"]
+
+ // You can customize which kind of files are using bullet
+ // For example if you would like to have the automatic bullet points behaviour in plain text files,
+ //
+ // "markdown_bullet_selectors" : ["text.html.markdown", "text.plain"]
+}
@@ -0,0 +1,14 @@
+[
+ //{
+ // "keys": ["tab"], "command": "indent", "context":
+ // [
+ // { "key": "preceding_text", "operator": "regex_match", "operand": "^( *|\\t*)(\\*|\\-|\\>|\\+|[0-9]+\\.)(.*)", "match_all": true }
+ // ]
+ //},
+ //{
+ // "keys": ["shift+tab"], "command": "unindent", "context":
+ // [
+ // { "key": "preceding_text", "operator": "regex_match", "operand": "^( +|\\t+)(\\*|\\-|\\>|\\+|[0-9]+\\.)(.*)", "match_all": true }
+ // ]
+ //}
+]
@@ -0,0 +1,14 @@
+[
+ //{
+ // "keys": ["tab"], "command": "indent", "context":
+ // [
+ // { "key": "preceding_text", "operator": "regex_match", "operand": "^( *|\\t*)(\\*|\\-|\\>|\\+|[0-9]+\\.)(.*)", "match_all": true }
+ // ]
+ //},
+ //{
+ // "keys": ["shift+tab"], "command": "unindent", "context":
+ // [
+ // { "key": "preceding_text", "operator": "regex_match", "operand": "^( +|\\t+)(\\*|\\-|\\>|\\+|[0-9]+\\.)(.*)", "match_all": true }
+ // ]
+ //}
+]
@@ -0,0 +1,17 @@
+[
+ //// TODO: use settings to refine or customize regex
+ //{
+ // "keys": ["tab"], "command": "bullet_indent", "context":
+ // [
+ // { "key": "preceding_text", "operator": "regex_match", "operand": "^( *|\\t*)(\\*|\\-|\\>|\\+|[0-9]+\\.)(.*)", "match_all": true },
+ // { "key": "auto_complete_visible", "operator": "equal", "operand": false},
+ // { "key": "last_command", "operator": "not_equal", "operand": "insert_best_completion" }
+ // ]
+ //},
+ //{
+ // "keys": ["shift+tab"], "command": "bullet_unindent", "context":
+ // [
+ // { "key": "preceding_text", "operator": "regex_match", "operand": "^( +|\\t+)(\\*|\\-|\\>|\\+|[0-9]+\\.)(.*)", "match_all": true }
+ // ]
+ //}
+]
View
@@ -0,0 +1,3 @@
+[
+
+]
@@ -1,8 +0,0 @@
-{
- "markdown_selectors" : ["text.html.markdown"]
-
- // You can customize which kind of files are viewed as markdown.
- // For example if you would like to have the automatic bullet points behaviour in plain text files,
- //
- // "markdown_selectors" : ["text.html.markdown", "text.plain"]
-}
View
@@ -1,111 +0,0 @@
-import sublime
-import sublime_plugin
-import re
-
-# TODO: TAB or space on a empty bullet will indent it
-# TODO: Backspace on a indented empty bullet will unindent it
-# TODO: Backspace on a unindented empty bullet will delete it
-# TODO: Better multi line pasting behaviour (esp the bug that if you paste a 2 lines content, a * will be added to the end of the pasting )
-# Just check whether the line is a empty line before inserting
-
-class MarkdownBullet(sublime_plugin.EventListener):
- Modifying = False
- is_markdown = False
- last_line = 0
- last_pos = 0
- selectors = []
-
- def __init__(self):
- s = sublime.load_settings("Markdown.sublime-settings")
- MarkdownBullet.selectors = s.get("markdown_selectors", [])
-
- def on_activated(self, view):
- for x in range(len(MarkdownBullet.selectors)):
- if (view.score_selector(0,MarkdownBullet.selectors[x]) > 0):
- MarkdownBullet.is_markdown = True
- MarkdownBullet.update_row(self, view)
- return
- else:
- MarkdownBullet.is_markdown = False
-
- def on_selection_modified(self, view):
- if (MarkdownBullet.is_markdown):
- MarkdownBullet.update_row(self, view)
-
- def update_row(self, view):
- loc = view.sel()[0]
- MarkdownBullet.last_pos = loc.begin()
- row, col = view.rowcol(MarkdownBullet.last_pos)
- MarkdownBullet.last_line = row
-
- def on_modified(self, view):
- if MarkdownBullet.Modifying == False:
- MarkdownBullet.Modifying = True
- if MarkdownBullet.is_markdown == True:
- loc = view.sel()[0]
- row, col = view.rowcol(loc.begin())
- point_last_row = view.text_point(row-1,0)
-
- if (row - MarkdownBullet.last_line == 0):
- current_line_region = view.line(loc.begin())
- current_line = view.substr(current_line_region)
- if current_line != "":
- match_pattern = re.search("^( *|\t*)(\*|\-|\>|\+|[0-9]+\.)(.*)$",current_line)
- if match_pattern != None:
- if match_pattern.group(3) == "":
- if MarkdownBullet.last_pos >= loc.begin():
- if match_pattern.group(1) != "":
- edit = view.begin_edit()
- view.insert(edit,current_line_region.end()," ")
- view.erase(edit,sublime.Region(current_line_region.begin(),current_line_region.begin()+1))
- view.end_edit(edit)
- else:
- edit = view.begin_edit()
- view.erase(edit,current_line_region)
- view.end_edit(edit)
- else:
- match_space = re.search("^(( )( )+|\t)$", match_pattern.group(3))
- if match_space != None:
- if match_pattern.group(1) == None:
- indented_part = ""
- else:
- indented_part = match_pattern.group(1)
- insertion_location = current_line_region.begin()
- edit = view.begin_edit()
- view.erase(edit,current_line_region)
- view.insert(edit, insertion_location , "\t" + indented_part + match_pattern.group(2) + " ")
- view.end_edit(edit)
-
- elif (row - MarkdownBullet.last_line == 1):
- previous_line = view.substr(view.line(MarkdownBullet.last_pos))
- if row != 0 and previous_line != "":
- match_pattern = re.search("^( *|\t*)(\*|\-|\>|\+|[0-9]+\.)(.*)",previous_line)
- if match_pattern != None:
- if match_pattern.group(3) == " " or match_pattern.group(3) == "":
- reg_remove = view.find("(\*|\-|\>|\+|[0-9]+\.)(.*)",point_last_row-1)
- edit = view.begin_edit()
- view.erase(edit,reg_remove)
- view.end_edit(edit)
- else:
- if match_pattern.group(2) == "*":
- insertion = "* "
- elif match_pattern.group(2) == "-":
- insertion = "- "
- elif match_pattern.group(2) == ">":
- insertion = "> "
- elif match_pattern.group(2) == "+":
- insertion = "+ "
- else:
- match_number = re.search("[0-9]+",match_pattern.group(2))
- if match_number != None:
- last_number = int(match_number.group(0))
- insertion = str(last_number+1) + ". "
- else:
- insertion = ""
- edit = view.begin_edit()
- view.insert(edit, loc.end(), insertion)
- view.end_edit(edit)
-
-
- MarkdownBullet.update_row(self, view)
- MarkdownBullet.Modifying = False
View
@@ -6,20 +6,20 @@ This is a Sublime Text plugin for markdown style Bullet points and Number lists.
Install using Package Control (Recommanded)
-1. I guess most Sublime Text 2 users are using Sublime Package Control. If not, pleast install in from here <http://wbond.net/sublime_packages/package_control>
+1. I guess most Sublime Text 2 users are using Sublime Package Control. If not, please install it from here <http://wbond.net/sublime_packages/package_control>
2. Go to Preference > Package Control, and Choose "Install Package"
3. Choose the package named "Bullet" to install it
Install manually
-1. Download the repo
+1. Download the repo
2. Copy everything in the repo to a folder named "Bullet" under the package folder of SublimeText 2 (create it if it doesn't exist)
## Usage
In markdown files, * , + and - are automatically added for unordered lists (aka bullet points). Similarly > are automatically added for multiline blockquote.
-For ordered lists, the new item will use a number next to the previous item. For example when you hit "enter" after typing "1. Item one", "2. " will be added automatically to the next line.
+For ordered lists, the new item will use a number next to the previous item. For example when you hit "enter" after typing "1. Item one", "2. " will be added automatically to the next line.
I may add other bullet point style later. For now it only support markdown style.
@@ -32,3 +32,4 @@ I only tested this on Windows, I hope that it works on OSX and Linux.
Copyright (C) 2012 Tom Lam. MIT License.
+
View
@@ -1,3 +1,4 @@
{
+ "0.3.0": "messages/0.3.0.txt",
"0.2.0": "messages/0.2.0.txt"
}
View
@@ -0,0 +1,5 @@
+SublimeMarkdown 0.3.0
+
+Changelog
+ - a major rewrite on the mechanism of indent and unindent.
+ - use tab and shift tab to indent and unindent

0 comments on commit 164993f

Please sign in to comment.