如何给expand-region添加新的support

想给lisp-mode下的expand-region添加支持,这是我的代码:


(defun er/lisp-mode ()
  (interactive)
  "Enhancement for lisp like #'() ,@()"
  (when (looking-at "\\(\\(`,?\\)\\|\\('?,?\\)\\|\\(,?@?\\)\\|\\(#?'?\\)\\)?(")
    (cond ((er/looking-back-on-line ",\\|@\\|?\\|#\\|'\\|`")
	   (backward-char 1)))
    (set-mark (point))
    (forward-list)
    (exchange-point-and-mark)))


(defun er/add-lisp-mode-expansions ()
  (make-variable-buffer-local 'er/try-expand-list)
  (setq er/try-expand-list (append
                            er/try-expand-list
                            '(er/lisp-mode))))

(add-hook 'lisp-mode-hook 'er/add-lisp-mode-expansions)

打开第一个.lisp的时候expand-region能正常工作

但是打开其他文本文件的时候expand-region出错了。

在后续打开的.lisp文件执行

eval-expression er/try-expand-list

会给出

(er/lisp-mode)

在其他文件则给出nil

请问应该怎么修改呢

1 个赞
  1. M-x toggle-debug-on-error
  2. 打开其他文本文件。
  3. 查看 *Backtrace* 缓冲。

按照顺序操作后并没有Backtrace

那是怎么个出错法?

错误的具体表现就是我上面说的那样

应该是哪里的逻辑出错误了

但是我找不出来

你上面有描述错误现象?是哪一句?

就是expand-region工作不正确了。

因为我上面的代码主要是改了er/try-expand-list的值,

所以我在后续打开文件时查看er/try-expand-list的值:

eval-expression er/try-expand-list

发现在lisp-mode里的值是

(er/lisp-mode)

在其他文件是

nil

buffer local variable 就是这个效果。而且你的函数设计目标也是针对 lisp mode,其它文本文件不起作用是对的。

那我应该怎么实现我想要的效果呢,就是在lisp-mode的时候在原有的值上添加新的值,其他mode照旧。

上面的代码我是看expan-region的github写的:

However, most languages also will benefit from some specially crafted expansions. For instance, expand-region comes with these extra expansions for html-mode:

er/mark-html-attribute
er/mark-inner-tag
er/mark-outer-tag

You can add your own expansions to the languages of your choice simply by creating a function that looks around point to see if it’s inside or looking at the construct you want to mark, and if so - mark it.

There’s plenty of examples to look at in these files.

After you make your function, add it to a buffer-local version of the er/try-expand-list .

Example:

Let’s say you want expand-region to also mark paragraphs and pages in text-mode. Incidentally Emacs already comes with mark-paragraph and mark-page . To add it to the try-list, do this:

(defun er/add-text-mode-expansions ()
  (make-variable-buffer-local 'er/try-expand-list)
  (setq er/try-expand-list (append
                            er/try-expand-list
                            '(mark-paragraph
                              mark-page))))

(add-hook 'text-mode-hook 'er/add-text-mode-expansions)

现在不就是这个效果吗?你又说是错(不符合预期)的。

我彻底糊涂了。

这里也许有什么误会。你可以试着套用以下 ISSUE 模板把问题重新描述一遍:

Summary*

<问题概述>

Actual Behavior*

<实际表现>

Expected Behavior*

<期望表现>

Steps to Reproduce*

<复现步骤>

Environment*

<环境&版本>

Error callstack

<错误堆栈>

我找到解决方法了

在运行我的代码之前要先

(require 'expand-region)

不然的话er/try-expand-list的初始值会是nil,导致运行我的代码之后er/try-expand-list的值只有(er/lisp-mode)

我不理解的是(make-variable-buffer-local 'er/try-expand-list)应该把er/try-expand-list的值限制到当前buffer,为什么其他buffer也会受影响