代码之家  ›  专栏  ›  技术社区  ›  psprint

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

  •  1
  • psprint  · 技术社区  · 4 年前

    • 将焦点移到ST3窗口并查看,
    • 这还需要切换到正确的虚拟桌面,
    • 调用崇高的命令(文本命令或其他命令)。

    我正在做一些Xlib/EWMH编码,我可以做激活和桌面切换“手动”。我只需要一个 这将调用一个崇高的文本命令。通过崇高的配置,这样的绑定是可能的吗?如果没有,那么如何完成上述工作?

    0 回复  |  直到 4 年前
        1
  •  1
  •   mattst    4 年前

    我不太清楚你说的a是什么意思 但是我想说的是,通过Xlib和EWMH以及一个崇高的文本插件,您可以获得所需的视图并调用ST命令。

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

    wmctrl 向EWMH/NetWM兼容的X窗口管理器发出命令的实用程序。

    #!/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配置文件中的某个位置 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)
    
    推荐文章