当前本地键盘映射的 Emacs 名称? [英] Emacs name of current local keymap?

查看:20
本文介绍了当前本地键盘映射的 Emacs 名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 elisp 函数,该函数将给定的键永久绑定到当前主要模式的键盘映射中的给定命令.例如,

I am writing an elisp function that permanently binds a given key to a given command in the current major mode's keymap. For example,

    (define-key python-mode-map [C-f1] 'python-describe-symbol)

命令和按键序列是从用户交互式收集的.但是,我无法生成与当前主要模式对应的 KEYMAP 的名称(例如python-mode-map").

The command and the key sequence are gathered interactively from the user. However, I am having trouble producing the name of the KEYMAP (e.g. 'python-mode-map') that corresponds to the current major mode.

我已经尝试过这个函数(current-local-map),但是这个函数返回的是keymap对象本身,而不是它的名字.

I have tried the function (current-local-map), but this function returns the keymap object itself, rather than its name.

我知道许多主要模式键映射是根据约定命名的''major-mode-name'-mode-map',但是,情况并非总是如此(例如,python-shell-map),所以我宁愿我的代码不依赖于这个约定.(我什至不确定如何访问当前主要模式的名称).

I understand that many major mode keymaps are named according to the convention ''major-mode-name'-mode-map', however, this is not always the case (for example, python-shell-map), so I would rather my code not rely on this convention. (I am not even sure how to access the name of the current major mode).

(define-key ...) 将被添加到 init 文件中,所以尽管

The (define-key ...) is to be added to an init file, so although

(define-key (current-local-map) 按键命令)

似乎有效,但它不能作为初始化文件上的代码.

seems to work, it does not work as code on an initialization file.

推荐答案

没有直接的方法可以找到当前本地键盘映射的名称——更准确地说,它的值绑定到的符号——因为键盘映射没有甚至必须绑定到一个符号.然而,模式键映射通常绑定到一个全局符号,并且可以通过遍历所有符号并在键映射对象的 eq 值处停止来找到它是哪一个.

There is no direct way to find the name of the current local keymap—more precisely, the symbol to which its value is bound—because the keymap doesn't even have to be bound to a symbol. However, mode keymaps typically are bound to a global symbol, and one can find which one it is by iterating through all symbols and stopping on the one whose value is eq to the keymap object.

这必须查看所有符号(尽管它对每个符号的工作量最少),但具有不依赖任何特定命名约定的优点.

This has to look at all the symbols (although it does minimum work with each), but has the advantage of not relying on any particular naming convention.

(defun keymap-symbol (keymap)
  "Return the symbol to which KEYMAP is bound, or nil if no such symbol exists."
  (catch 'gotit
    (mapatoms (lambda (sym)
                (and (boundp sym)
                     (eq (symbol-value sym) keymap)
                     (not (eq sym 'keymap))
                     (throw 'gotit sym))))))


;; in *scratch*:
(keymap-symbol (current-local-map))
==> lisp-interaction-mode-map

这篇关于当前本地键盘映射的 Emacs 名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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