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

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

问题描述

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

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

从用户交互地收集命令和密钥序列。但是,产生与当前主要模式相对应的KEYMAP(例如python-mode-map)的名称有困难。



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



我了解到,许多主要模式的键盘映射是根据惯例主模式名称模式映射命名的,但是并不总是如此(例如,python-shell-map),所以我宁愿我的代码不依赖这个约定。 (我甚至不知道如何访问当前主要模式的名称)。



(define-key ...) code>被添加到一个init文件中,所以尽管



(define-key(current-local-map) )



似乎工作,它不起作用的初始化文件的代码。

解决方案

没有直接的方法可以找到当前本地键盘映射的名称 - 更确切地说,它的值被绑定到的符号 - 因为键映射甚至不必绑定到一个符号。然而,模式键映射通常绑定到全局符号,并且可以通过遍历所有符号来找到哪一个,并且将值停留在其值为 eq 的键映射对象。



这必须查看所有符号(尽管它与每个符号进行最小的工作),但具有不依赖于任何特定命名约定的优点。

 (defun keymap-symbol(keymap)
返回KEYMAP绑定到的符号,如果没有这样的符号,则为零存在
(catch'getit
(mapatoms(lambda(sym))
(和(boundp sym)
(eq(symbol-value sym)keymap)
(不是(eq sym'keymap))
(throw'getit sym))))))


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


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)

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.

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

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).

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

(define-key (current-local-map) key command)

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

解决方案

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天全站免登陆