首页 > 解决方案 > 用 Elisp(输入法)编写一个简单的切换函数

问题描述

所以,我尝试了以下

(defun toggle-input-chinese ()
   (if (equal current-input-method 'chinese-py)
       (set-input-method chinese-sisheng)
     (set-input-method chinese-py)))

现在,基本上,我想写中文或拼音。我发现没有简单的方法在非标准输入之间使用切换。因此,我决定编写这个函数并绑定到一个键。

好的。我的问题是:它引发了错误(void-variable chinese-py)。我不知道如何将当前方法与列出的方法等同起来。我该怎么做?

- 编辑

这个版本是功能性的。可以将其他输入的列表放在条件中,您将在语言环中切换。最后,将其绑定到某个键。

这是一种比这里想象的更简单的方法: 是否可以在 Emacs 中交替使用两种输入法?

(defun toggle-input-chinese ()
  (interactive)
  (if (equal (car input-method-history) "chinese-py")
      (set-input-method 'chinese-py)
    (set-input-method 'chinese-sisheng)))

标签: variablesemacstoggleevaluation

解决方案


您将chinese-pychinese-sisheng作为变量传递给 function set-input-method。Lisp 在调用函数之前评估函数的参数。它试图评估该变量,但该符号没有作为变量的值。

相反,您要做的是传递符号 chinese-pyor chinese-sisheng,而不是将其值作为变量传递(它没有)。

尝试同时引用chinese-pyand chinese-sisheng

(defun toggle-input-chinese ()
   (interactive) ; If you want to use it as a command
   (if (equal (car input-method-history) "chinese-py")
       (set-input-method 'chinese-sisheng)
     (set-input-method 'chinese-py)))

这是一样的:

(defun toggle-input-chinese ()
   (interactive) ; If you want to use it as a command
   (set-input-method (if (equal (car input-method-history) "chinese-py")
                         'chinese-sisheng
                       'chinese-py)))

推荐阅读