简体   繁体   English

emacs,auctex和查询替换字符串换行

[英]emacs, auctex and query-replace strings with newline

I use emacs+auctex and auto-fill-mode. 我使用emacs + auctex和自动填充模式。

Now sometimes I want to search (and replace) a string containing spaces like "test1 test2". 现在有时我想搜索(并替换)包含空格的字符串,如“test1 test2”。 The problem is, that auto-fill-mode replaces space-characters sometimes by newline characters. 问题是,自动填充模式有时会用换行符替换空格字符。 So search and replace of "test1 test2" does not find those occurrences of this string where auto-fill replaced the whitespace by a newline character. 因此,搜索和替换“test1 test2”时,找不到此字符串的出现位置,其中auto-fill用换行符替换空白。

Any idea how to solve this problem? 知道如何解决这个问题吗?

I text mode it works to use \\s- in query-replace-regexp, ie "test1\\s-test2" but this does not work in auctex-mode, I don't know why. 我在文本模式下使用\\ s-在query-replace-regexp中工作,即“test1 \\ s-test2”,但这在auctex模式下不起作用,我不知道为什么。

Using Cq Cj is very uncomfortable to use, because such cases as "test1 test2" occur very often especially because I want to get the newlines and the spaces in one run, so I have to do something like this: 使用Cq Cj非常不舒服,因为“test1 test2”这样的情况经常发生,特别是因为我想在一次运行中获取换行符和空格,所以我必须做这样的事情:

M-x query-replace-regexp RET

test1[ <-- one space

C-j C-q

]\s-*test2

The last \\s-* is because of possible indentations in auctex. 最后的\\ s- *是因为auctex中可能存在缩进。 This seems not to be very elegant. 这似乎不是很优雅。

By the way if you want to search and replace "test1 test2" is is very annoying to think every time of treating the newline cases specially... 顺便说一句,如果你想搜索和替换“test1 test2”是非常讨厌每次特别处理换行案例...

Emacs also has "categories", which are like syntax classes, but a little more flexible. Emacs也有“类别”,类似于语法类,但更灵活一些。 You can use \\cX in regular expressions to match a character in category X . 您可以在正则表达式中使用\\cX来匹配类别X中的字符。

Here's a function to define an "all-whitespace" category, which includes spaces, newlines, tabs, and form feeds, that you can cite in regular expressions as \\cs . 这是一个定义“全空白”类别的函数,其中包括空格,换行符,制表符和换页符,您可以在正则表达式中引用\\cs

(defun define-all-whitespace-category (table)
  "Define the 'all-whitespace' category, 's', in the category table TABLE."
  ;; First, clear out any existing definition for category 's'. Otherwise,
  ;; define-category throws an error if one calls this function more than once.
  (aset (char-table-extra-slot table 0) (- ?s ? ) nil)
  ;; Define the new category.
  (define-category ?s "all whitespace
All whitespace characters, including tab, form feed, and newline"
    table)
  ;; Add characters to it.
  (mapc (lambda (c) (modify-category-entry c ?s table))
        '(?  ?\n ?\f ?\t)))

(define-all-whitespace-category (standard-category-table))

It looks to me like auctex-mode uses the standard category table, so you should be able to use query-replace-regexp with foo\\cs+bar in that mode. 在我看来,像auctex-mode使用标准类别表,所以你应该能够在该模式下使用query-replace-regexp和foo\\cs+bar

To test it out, you can put point on some character of interest and say: 为了测试它,你可以指出一些感兴趣的字符并说:

M-: (looking-at "\\cs") RET

which will evaluate to t if the character under point is in the all-whitespace category. 如果点下的字符在全空白类别中,它将评估为t

The easiest way to patch around this would probably be to write a little elisp function. 修补此问题的最简单方法可能是编写一个小的elisp函数。

something like: 就像是:

(defun auctex-query-replace-regexp (first second replace)
  (interactive "Mfirst:\nMsecond:\nM:replace:")
  (while (re-search-forward (concat first "[ 
]*" second))
    (replace-match replace)))

Stick it in your .emacs, put point on the last ) , and evaluate with Cx Ce 将它贴在你的.emacs中,把点放在最后一个上) ,并用Cx Ce评估

Bind it to a key either globally with global-set-key , or mode-specific with add-hook . 使用global-set-key将其绑定到全局global-set-key ,或者使用add-hook其绑定到特定于模式的global-set-key

note that the character class consists of a literal space and then a literal newline , inserted with Co or Cq Cj 请注意,字符类包含一个文字space ,然后是一个用CoCq Cj插入的文字newline

This is a pretty simple example. 这是一个非常简单的例子。 Improving it is left as an excersize for the reader ;) 改进它是留给读者的一个例外;)

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

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