In sublimetext, I want to create a key binding that is valid only in Japanese using context.

Asked 2 years ago, Updated 2 years ago, 123 views

I am developing a sublimetext plug-in.
This is a plug-in that allows you to divide Japanese into word levels by double-clicking.
https://github.com/ASHIJANKEN/JapaneseWordSeparator

Currently, this plug-in is being registered with Package Control and has been interacting with the administrator.
https://github.com/wbond/package_control_channel/pull/6992#issuecomment-383399294
"In order to enable this function only for Japanese sentences, it is better to mask the key bindings using the following context in .sublime-keymap or .sublime_mousemap."

{
    "keys": ["ctrl+left",
    "command": "key_select_jp",
    "args": {"key": "left"},
    "context": [
        { "key": "preceding_text", "operator": "regex_contains", "operand": "\\p {Jpan}$"}
    ]
}

Based on this, I tried to create the following context, but it doesn't work well. I've been playing with operand a lot, but this plug-in will be enabled at any time (even in English sentences).

// Pattern 1
"context": [
  { "key": "preceding_text", "operator": "regex_contains", "operand": "[\\p {Katakana}\\p {Hiragana}\\p {Han}. , .?!? · "()":()"$"}
]

// Pattern 2
"context": [
  { "key": "preceding_text", "operator": "regex_contains", "operand": "[\\p {Katakana}\\p {Hiragana}\\p {Han}. , .?!? · "()":()"}
]

// Pattern 3
"context": [
  { "key": "preceding_text", "operator": "regex_contains", "operand": "[An-ahn 1- 。v., , !?!? · "()]}
]

// and so on. I also tried regex_match but it didn't work.

I think the regular expression method is bad, but I don't know how to specify it to solve it.
Does anyone have any tips?
Thank you for your cooperation.

By the way, regarding the context, I think the following site is easy to understand.
https://qiita.com/shibainurou/items/dc18f2dfc91e36adb208#%E3%82%B3%E3%83%B3%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%E3%81%8C%E3%81%82%E3%82%8B%E5%A0%B4%E5%90%88

regular-expression sublimetext

2022-09-30 21:30

1 Answers

I also made some Sublime Text packages and registered them in Package Control.

When I put the package you made in the Packages directory of the Sublime Text and tried pattern 1-3, pattern 12 doesn't work, but pattern 3 seems to work fine.

"Operating without problems" specifically refers to the following:

  • The key_select_jp command responds when the cursor is in Japanese in the line preceding the cursor
  • The set_motion command responds when there is no Japanese in the line before the cursor (=Sublime Text default movement)

Incidentally, some of the .sublime-keymap when I checked the operation are as follows:

{
        "keys": ["super+left",
        "command": "key_select_jp",
        "args": {"key": "left"},
        "context": [
            {
                "key": "preceding_text",
                "operator": "regex_contains",
                "operand": "[An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An-An
            }
        ]
    },

My environment is macOS.

Below are some comments.

  • There is no proof, but I think the expression \\p {Katakana} in pattern 12 is not available here.Please check it out.
  • As you know, there are not many context documents, so you should refer to other packages.If you have already installed many packages, you can search under the Packages directory for examples.

I hope you will find it helpful. :)


2022-09-30 21:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.