繁体   English   中英

是否可以通过/向 Sublime Text 3 添加全局 X11 绑定?

[英]Is it possible to add a global X11 binding via/to Sublime Text 3?

我想添加一个全局 X11 绑定,最好通过 ST3 配置,当激活时,将:

  • 将焦点移至 ST3 window 并查看,
  • 这还涉及切换到正确的虚拟桌面,
  • 调用一个崇高的命令(文本命令或其他)。

这可能吗?

我正在做一些 Xlib / EWMH 编码,我可以“手动”进行激活和桌面切换。 我只需要一个全局 X11 绑定,它会调用 Sublime Text 命令。 这种绑定可以通过 Sublime 配置实现吗? 如果没有,那么如何完成上述操作?

我不确定您所说的全局 X11 绑定到底是什么意思,但我想说您想要实现的目标可以通过 Xlib 和 EWMH 以及 Sublime Text 插件来聚焦所需的视图并调用 ST 命令。

下面是所需逻辑的完整工作基本模型,它使用 Bash 脚本来聚焦 window 并切换虚拟桌面,然后调用 Sublime Text 插件来更改视图并运行所需的命令。

该脚本使用wmctrl一个有用的实用程序,它向兼容 EWMH/NetWM 的 X Window 管理器发出命令。

将此文件另存为 Bash 脚本并赋予其可执行权限:

#!/bin/bash

# EWMH WM_CLASS for Sublime Text 3: "sublime_text.Sublime_text"
#
# EWMH WM_NAME examples: "~/Path/FileName.ext (Project Name) - Sublime Text"
#                        "~/Path/No Project.ext - Sublime Text"
#                        "untitled - Sublime Text"
#                        "~/Path/FileName.ext • (Project) - Sublime Text"
#
# Note: The project file name without the extension will be shown in brackets
# if there is a project file. '•' (see last example above) will be added if
# the file is unsaved and the suffix " - Sublime Text" is always added.

# Example: "~/Programming/Projects/MyProject.sublime-project" --> "MyProject"
target_project="MyProject"

# Example "wmctrl -lx" line:
# 0x02400003 0 sublime_text.Sublime_text host ~/File.ext (MyProject) - Sublime Text
# WinID   Desktop      WM_CLASS        hostname             WM_NAME
target_window=$(wmctrl -lx | grep ".*sublime_text.Sublime_text.*($target_project).*")
if [ "$target_window" = "" ]; then
    exit
fi

target_window_id=$(echo "$target_window" | grep -Eo '0x[0-9a-f]+')
if [ "$target_window_id" = "" ]; then
    exit
fi

# Switch focus to the window, wmctrl will switch virtual desktops if needed.
wmctrl -ia "$target_window_id"

# Call the Sublime Text window command:
# [This assumes Sublime Text is already running.]
subl --background --command "focus_view_invoke_cmd"

将此文件保存在 ST3 config Packages目录树中的某个位置,扩展名为.py 例如~/.config/sublime-text-3/Packages/User/FocusViewInvokeCommand.py

import os.path
import sublime
import sublime_plugin

# Command created by ST for this class: focus_view_invoke_cmd
# How to set or find the command name of a Sublime Text plugin
# see this answer: https://stackoverflow.com/a/63979147/2102457

class FocusViewInvokeCmdCommand(sublime_plugin.WindowCommand):

    def run(self):

        # view_name_to_match is a file or unsaved buffer
        # name that the focus should be switched to. e.g.
        view_name_to_match = "MyToDoList"

        for view in self.window.views():
            if view_name_to_match in self.view_name(view):
                self.window.focus_view(view)
                self.run_command(view)
                break


    def view_name(self, view):

        # Return the file name (not full path) if the buffer is saved.
        if view.file_name():
            path = view.file_name()
            return os.path.basename(path)

        # Return the buffer name; i.e. the name the user has assigned,
        # or the buffer's 1st line auto. assigned by ST, or "untitled".
        else:
            # "untitled" is the default displayed name shown by ST
            # for all buffers which are both unsaved and unnamed,
            # but view.name() returns "" for "untitled" buffers.
            return view.name() if view.name() else "untitled"


    def run_command(self, view):

        command_to_run = "insert"
        command_args = {"characters": "This is a test! :)"}

        # In each of the run_command() methods the
        # command_args parameter may be omitted.

        # Use to run a *text command* on the view.
        view.run_command(command_to_run, command_args)

        # Use to run a *window command*.
        # self.window.run_command(command_to_run, command_args)

        # Use to run an *application command*.
        # sublime.run_command(command_to_run, command_args)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM