简体   繁体   English

在 emacs lisp 中,有没有办法搜索匹配与当前点重叠的字符串的文本?

[英]In emacs lisp, is there a way to search text that will match a string that overlaps the current point?

I'm not exactly a lisp expert, so please forgive a fairly newbie question.我不是一个 lisp 专家,所以请原谅一个相当新手的问题。

I'm writing a fairly simple elisp function, trying to find a short string on the same line as the current cursor position.我正在写一个相当简单的 elisp function,试图在与当前 cursor position 相同的行上找到一个短字符串。 The relevant part of the code as written now is:现在编写的代码的相关部分是:

(let ((matchpos (search-forward myword (line-end-position) t)))
  (if matchpos
      (goto-char (- matchpos (length myword)))
    (setq matchpos (search-backward myword (line-beginning-position) t)))
  ...

This mostly works without trouble, but it fails to find a string containing the current cursor position.这大部分工作没有问题,但它无法找到包含当前 cursor position 的字符串。 Apparently, search-backward only looks for strings that end before point.显然,向后搜索只查找在点之前结束的字符串。 In truth, I can solve this particular problem by going to the end of the line before doing the search-backward, but I'm rather curious whether this behavior can be modified.事实上,我可以通过在向后搜索之前转到行尾来解决这个特殊问题,但我很好奇这种行为是否可以修改。

Is there any variant on search-backward that matches any string that starts before point?向后搜索是否有任何变体匹配点之前开始的任何字符串?

Edit: Perhaps an example is in order, since those answering don't seem to quite understand the question.编辑:也许可以举一个例子,因为那些回答的人似乎不太理解这个问题。 Suppose I am searching for the string "search this", and my line looks like this (point noted by the *):假设我正在搜索字符串“search this”,我的行看起来像这样(* 指出的点):

I want to search this string, I have code to search t*his string

Unfortunately, the code fragment shown above moves point to the first occurrence of "search this" in the line above.不幸的是,上面显示的代码片段指向上一行中“search this”的第一次出现。 I want point to end up at the beginning of the second "search this" string, the one currently containing point, since it's the nearest one.我希望点结束于第二个“搜索这个”字符串的开头,即当前包含点的字符串,因为它是最近的一个。

The second argument to search-forward bounds the search, so to search for a string on the current line, go to the start of the line, and use the end of the line as your bound: search-forward的第二个参数限制了搜索,因此要在当前行搜索字符串,go 到行首,并使用行尾作为边界:

(beginning-of-line)
(search-forward myword (line-end-position))

If you don't want to move point, then use save-excursion to preserve it:如果您不想移动点,请使用save-excursion来保留它:

(save-excursion
  (beginning-of-line)
  (search-forward myword (line-end-position)))

I would suggest that you move the cursor to the right the number of characters that corresponds to the length of the string you search for (possibly minus one, depending on if you would like to match something immediately to the right of the point).我建议您将 cursor 向右移动与您搜索的字符串长度相对应的字符数(可能减一,具体取决于您是否想立即匹配点右侧的内容)。

Something like:就像是:

(goto-char (min (end-of-line-position)
                (+ (point) (length myword))))
(setq matchpos (search-backward myword (line-beginning-position) t))

This will work, as you have moved the point out of the way for search-backword .这将起作用,因为您已将点移开search-backword

Another solution would be to go to the beginning of the line and repeatedly search forward until you have passed the point.另一种解决方案是将 go 到该行的开头并反复向前搜索,直到您通过该点。

the regex pattern "\=" matches the current point.正则表达式模式"\="匹配当前点。 if you work that into your expression, that should get you what you need.如果你把它融入你的表达中,那应该会得到你所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM