Skip to content

Commit

Permalink
Enable default command when the cursor is on an English word
Browse files Browse the repository at this point in the history
  • Loading branch information
ASHIJANKEN committed Aug 18, 2020
1 parent 63ed454 commit 7042ba1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 41 deletions.
9 changes: 0 additions & 9 deletions Default (OSX).sublime-mousemap
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,17 @@
"button": "button1", "count": 2,
"press_command": "drag_select_jp",
"command": "released",
"context": [
{"key": "cursor_on_jp_chars", "operator": "equal", "operand": true}
]
},
{
"button": "button1", "count": 2, "modifiers": ["super"],
"press_command": "drag_select_jp",
"press_args": {"additive": true},
"command": "released",
"context": [
{ "key": "preceding_text", "operator": "regex_match", "operand": "[\\p{Katakana}\\p{Hiragana}\\p{Han}。、,.!? ・「」:”()ー゚゙]+" }
]
},
{
"button": "button1", "count": 2, "modifiers": ["alt"],
"press_command": "drag_select_jp",
"press_args": {"subtractive": true},
"command": "released",
"context": [
{ "key": "preceding_text", "operator": "regex_match", "operand": "[\\p{Katakana}\\p{Hiragana}\\p{Han}。、,.!? ・「」:”()ー゚゙]+" }
]
},
]
9 changes: 0 additions & 9 deletions Default (Windows).sublime-mousemap
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,17 @@
"button": "button1", "count": 2,
"press_command": "drag_select_jp",
"command": "released",
"context": [
{ "key": "preceding_text", "operator": "regex_match", "operand": "[\\p{Katakana}\\p{Hiragana}\\p{Han}。、,.!? ・「」:”()ー゚゙]$" }
]
},
{
"button": "button1", "count": 2, "modifiers": ["ctrl"],
"press_command": "drag_select_jp",
"press_args": {"additive": true},
"command": "released",
"context": [
{ "key": "preceding_text", "operator": "regex_contains", "operand": "[\\p{Katakana}\\p{Hiragana}\\p{Han}。、,.!? ・「」:”()ー゚゙]" }
]
},
{
"button": "button1", "count": 2, "modifiers": ["alt"],
"press_command": "drag_select_jp",
"press_args": {"subtractive": true},
"command": "released",
"context": [
{ "key": "preceding_text", "operator": "regex_contains", "operand": "[\\p{Katakana}\\p{Hiragana}\\p{Han}。、,.!? ・「」:”()ー゚゙]" }
]
},
]
43 changes: 20 additions & 23 deletions japanesewordseparator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<<<<<<< HEAD
# -*- coding: utf-8 -*-
import sublime
import sublime_plugin
Expand All @@ -12,23 +11,11 @@
editing_region = sublime.Region(0, 0)
backup_regions = None
point_seg = None
point_pos = None

jp_pattern = u'[一-龠々〆ヵヶぁ-んァ-ヴア-ン゙]'
reg = re.compile(jp_pattern)

class PluginEventListener(sublime_plugin.EventListener):
def on_query_context(self, view, key, operator, operand, match_all):
if key == 'cursor_on_jp_chars':
point = self.view.sel()[-1].b
# If character before cursor is Japanese
char = view.substr(sublime.Region(point - 1, point))
if reg.search(char):
print("cursor_on_jp_chars: True")
return True
else:
return False
print("cursor_on_jp_chars: False")


# This class detects mouse move while dragging.
# It finds the closest point in the view to the mouse location, then expands a region you selecting now.
Expand Down Expand Up @@ -67,16 +54,23 @@ def expand_region(self, view, point):
# Find a region
class DragSelectJp(sublime_plugin.TextCommand):
def run(self, edit, additive=False, subtractive=False):
global pressing, editing_region, firepoint, firepoint_seg, backup_regions
global pressing, editing_region, firepoint, firepoint_seg, backup_regions, point_pos
backup_regions = []

# safety check
if firepoint is None:
# print("firepoint is None! what's up?")
return

# suppress in Find Result
if self.view.name() == "Find Results" and (not additive) and (not subtractive):
self.view.run_command("double_click_at_caret")
return

# safety check
if firepoint is None:
# print("firepoint is None! what's up?")
# Suppress if the cursor is on an English word
char = self.view.substr(sublime.Region(firepoint - 1, firepoint))
if not reg.search(char):
self.view.run_command("drag_select", {"additive": additive, "subtractive": subtractive, "by": "words", "event": {"button": 1, "count": 2, "x": point_pos[0], "y": point_pos[1]}})
return

pressing = True
Expand All @@ -100,9 +94,11 @@ def run(self, edit, additive=False, subtractive=False):
# Keeping text point on command fired, to global variable "firepoint"
class LastCaretListener(sublime_plugin.EventListener):
def on_text_command(self, view, command_name, args):
global firepoint
global firepoint, point_pos
if command_name == "drag_select_jp":
firepoint = view.window_to_text((args["event"]["x"], args["event"]["y"]))
print("LastCaretListener")
point_pos = (args["event"]["x"], args["event"]["y"])
firepoint = view.window_to_text(point_pos)


# Find region, move cursor by arrow keys.
Expand Down Expand Up @@ -150,7 +146,7 @@ def run(self, edit, additive=False, subtractive=False, key='none'):
# This is called when a mouse button is released.
class Released(sublime_plugin.TextCommand):
def run(self, edit):
global pressing, firepoint, firepoint_seg, editing_region, backup_regions
global pressing, firepoint, firepoint_seg, editing_region, backup_regions, point_pos

if editing_region is not None:
self.view.sel().clear()
Expand All @@ -162,8 +158,8 @@ def run(self, edit):
firepoint = None
firepoint_seg = None
editing_region = None
backup_region = None

backup_regions = None
point_pos = None


# This method finds a segment which cursor position is within, and return it.
Expand Down Expand Up @@ -192,6 +188,7 @@ def find_seg_en_jp(view, canditate_region, point):


# Shooting a double-click at the caret position
# This command is called on "Find Results" page.
class DoubleClickAtCaretCommand(sublime_plugin.TextCommand):
def run(self, edit, **kwargs):
view = self.view
Expand Down

0 comments on commit 7042ba1

Please sign in to comment.