You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Emacs-Lisp中停用%格式说明符以识别Hoon符文?

解决Emacs-Lisp识别Hoon %开头符文的问题

核心问题原因

%在Emacs部分场景中会被识别为格式字符串起始符,导致thing-at-point或自定义函数处理时触发format string ends in middle of format specifier错误。你尝试的(put 'format 'disabled t)和(disable-command format)无效,是因为format是普通Lisp函数而非交互式命令,这两种方式仅对命令生效。

可行解决方案

1. 修改语法表,将%标记为普通单词字符

直接调整Emacs语法规则,把%归类为单词组成字符,让current-word、thing-at-point能正常识别以%开头的双字符符文:

;; 仅在当前Hoon模式buffer生效(推荐)
(add-hook 'hoon-mode-hook
          (lambda () (modify-syntax-entry ?% "w")))

;; 全局生效(不推荐,可能干扰其他模式的语法识别)
(modify-syntax-entry ?% "w")

修改后标准函数可直接获取%=、%<这类符文,不会触发格式错误。

2. 捕获错误并手动提取符文

若不想修改语法规则,可用condition-case捕获格式错误,报错时手动提取光标下的双字符符文:

(defun get-hoon-rune-at-point ()
  (condition-case err
      ;; 优先尝试标准函数获取
      (thing-at-point 'word)
    ;; 捕获格式错误时手动提取
    ((error)
     (let* ((pos (point))
            (prev-char (char-before pos))
            (next-char (char-after pos)))
       ;; 处理光标在符文第一个字符后的情况
       (if (and prev-char next-char)
           (string prev-char next-char)
         ;; 处理光标在符文第二个字符上的情况
         (when (>= pos 2)
           (string (char-before (1- pos)) prev-char)))))))

3. 手动实现精准的符文识别

由于Hoon符文均为固定双字符组合,可直接枚举所有可能的符文,精准匹配光标位置的字符:

(defvar hoon-runes '("?=" "/+" "$-" "%=" "%<" "%>" "%+" "%-" "%*" "%/"))

(defun get-hoon-rune-at-point ()
  (let* ((pos (point))
         ;; 检查光标在符文第一个字符后的情况
         (rune1 (when (and (char-before pos) (char-after pos))
                  (string (char-before pos) (char-after pos))))
         ;; 检查光标在符文第二个字符上的情况
         (rune2 (when (>= pos 2)
                  (string (char-before (1- pos)) (char-before pos)))))
    (cond ((member rune1 hoon-runes) rune1)
          ((member rune2 hoon-runes) rune2)
          (t nil))))

该方式精准度最高,不受其他语法规则影响,但需维护hoon-runes变量覆盖所有可能的符文。

内容的提问来源于stack exchange,提问作者Mortimer Cladwell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 05:27:18