是否可以通过/向升华文本3添加全局X11绑定?

2024-05-16 20:06:16 发布

您现在位置:Python中文网/ 问答频道 /正文

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

  • 将焦点移动到ST3窗口并查看
  • 这还包括切换到正确的虚拟桌面
  • 调用升华命令(文本命令或其他命令)

这可能吗

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


Tags: text文本命令编码手动全局桌面焦点
1条回答
网友
1楼 · 发布于 2024-05-16 20:06:16

我不确定您所说的全局X11绑定到底是什么意思,但我想说的是,您希望通过Xlib和EWMH以及一个Sublime文本插件来实现什么,以聚焦所需的视图并调用ST命令

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

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

将此文件另存为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 configPackages目录树的某个地方,扩展名为.py。e、 g.~/.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)

相关问题 更多 >