将字符定义为字边界 [英] Define a character as a word boundary

查看:161
本文介绍了将字符定义为字边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了 \ 字符在乳胶模式中表现为单词成分,我对结果非常满意。唯一困扰我的是,像 \alpha\beta 这样的序列被视为一个单词(当然是预期的行为)。



有没有办法使emacs将特定字符解释为单词starter?这样一来,它总是被认为是它之后的单词的一部分,但是从来没有这个词的一部分。



为了清楚起见,这里有一个例子:

  \alpha\beta 
^ ^
1 2

如果点位于 1 ,我按 Md ,字符串\alpha应该被杀死。
如果点位于 2 ,我按 M-< backspace> ,字符串\\



如何实现?


您的要求非常类似于 subword-mode 为camelCase提供的



您不能自定义子字模式的行为 - 正则表达式是硬编码的 - 但您可以复制该库并为此目的进行修改。



Mx find-library RET code> RET



这可能是一个非常强大的解决方案。



编辑:从注释中更新,如下所示:


为了记录, code> [[:upper:]] [\\\\\ [:upper:]] 功能 subword-forward-internal an d subword-backward-internal within subword.el works great =)(只要\被定义为w语法)。


个人来说,我更倾向于制作图书馆的副本,而不是直接编辑图书馆,除非是为了使现有的图书馆一个更通用的方法,最简单的解决方案似乎是将这些正则表达式转化为变量 - 之后为这种目的而设置缓冲区本地修改版本是微不足道的。



编辑2:截至Emacs 24.3(目前为发布候选人),子字模式通过新的子字转发正则表达式 subword-backward-regexp 变量(简单修改)和子词向前函数子字 - 反向函数变量(用于更复杂的修改)。



通过使乳胶模式中的regexp变量缓冲区为变量,您可以使用子字模式可怕的ctly。


I've defined the \ character to behave as a word constituent in latex-mode, and I'm pretty happy with the results. The only thing bothering me is that a sequence like \alpha\beta gets treated as a single word (which is the expected behavior, of course).

Is there a way to make emacs interpret a specific character as a word "starter"? This way it would always be considered part of the word following it, but never part of the word preceding it.

For clarity, here's an example:

\alpha\beta
^          ^
1          2

If the point is at 1 and I press M-d, the string "\alpha" should be killed. If the point is at 2 and I press M-<backspace>, the string "\beta" should be killed.

How can I achieve this?

解决方案

Another thought:
Your requirement is very like what subword-mode provides for camelCase.

You can't customize subword-mode's behaviour -- the regexps are hard-coded -- but you could certainly copy that library and modify it for your purposes.

M-x find-library RET subword RET

That would presumably be a pretty robust solution.

Edit: updated from the comments, as suggested:

For the record, changing every instance of [[:upper:]] to [\\\\[:upper:]] in the functions subword-forward-internal and subword-backward-internal inside subword.el works great =) (as long as "\" is defined as "w" syntax).

Personally I would be more inclined to make a copy of the library than edit it directly, unless for the purpose of making the existing library a little more general-purpose, for which the simplest solution would seem to be to move those regexps into variables -- after which it would be trivial to have buffer-local modified versions for this kind of purpose.

Edit 2: As of Emacs 24.3 (currently a release candidate), subword-mode facilitates this with the new subword-forward-regexp and subword-backward-regexp variables (for simple modifications), and the subword-forward-function and subword-backward-function variables (for more complex modifications).

By making those regexp variables buffer-local in latex-mode with the desired values, you can just use subword-mode directly.

这篇关于将字符定义为字边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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