如何获取窗口的唯一标识符? [英] How to get a unique identifier for a window?

查看:37
本文介绍了如何获取窗口的唯一标识符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为窗口获取某种唯一标识符,以便可以针对该窗口运行命令.

I am trying to get some sort of unique identifier for a window, so that commands can be run against that window.

即,如果我需要给那个窗口焦点..或者如果我需要看到那个窗口的大小..等等.问题是目前似乎窗口号被用作这个标识符,但这个数字可能每次引入新窗口时都会更改.. 似乎是从左到右、从上到下的索引计数.. 这让我很困惑为什么将其用作标识符.

Ie, if i need to give that window focus.. or if i need to see the size of that window.. etc. The problem is currently it seems like the window number is used as this identifier, but this number potentially changes any time a new window is introduced.. It seems like it is an index count from left to right and top to bottom.. which puzzles me as to why that would be used as an identifier.

看到我不知道用户可能对布局做什么..我如何确保当我为窗口分配缓冲区或获取有关窗口的信息时,我实际上正在获取有关该窗口的信息我想要吗?

Seeing as i have no idea what the user may do to a layout.. how can i assure that when i assign a window a buffer, or get information about a window, that the i am actually getting information about the window i want?

推荐答案

最近的 Vim 版本有 win_getid() 函数和 win_id2tabwin() 代替下面的 >s:FindWinID.也 win_gotoid() 只是转到具有给定标识符的窗口.标识符由 Vim 本身维护,所以即使打开窗口,例如noautocmd wincmd s 将无法创建没有标识符的窗口.

Recent Vim versions have win_getid() function and win_id2tabwin() in place of the below s:FindWinID. Also win_gotoid() to just go to window with given identifier. Identifiers are maintained by Vim itself, so even opening window with e.g. noautocmd wincmd s will not be able to create a window without an identifier.

对于旧版本,您可以使用窗口变量来获取此类标识符:

For older versions, you can use window variables to get such identifier:

" put unique window identifier into w:id variable
autocmd VimEnter,WinEnter * if !exists('w:id') | let w:id={expr_that_will_return_an_unique_identifier} | endif

这应该标记所有窗口.或者,最好只标记您想在创建窗口后立即使用的窗口.找到一个id为abc的窗口然后切换到它:

This should mark all windows. Or, it is maybe better to mark only that windows which you want to use just after window creation. To find a window with id abc and then switch to it:

function s:FindWinID(id)
    for tabnr in range(1, tabpagenr('$'))
        for winnr in range(1, tabpagewinnr(tabnr, '$'))
            if gettabwinvar(tabnr, winnr, 'id') is a:id
                return [tabnr, winnr]
            endif
        endfor
    endfor
    return [0, 0]
endfunction
<...>
let [tabnr, winnr]=s:FindWinID('abc')
execute "tabnext" tabnr
execute winnr."wincmd w"

这篇关于如何获取窗口的唯一标识符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆