【问题标题】:Emacs - select text by regexp (and idealy with option to select text only if regexp match is surrounding caretEmacs - 通过正则表达式选择文本(理想情况下,仅当正则表达式匹配围绕插入符号时才选择文本
【发布时间】:2011-11-27 13:52:07
【问题描述】:

我需要使用正则表达式在 Emacs 中选择文本。如果我可以选择仅在匹配围绕插入符号时进行匹配,那将是最好的。

例子:

text.....

<start oftext I want to select> text.....
text....
text.... <caret> text....
text....
text.... <end of text I want to select>

some other text

编辑:很抱歉,我显然没有清楚地说明我的问题,所以这里澄清一下:

  • 插入符号是指当前放置光标的位置,而不是要匹配的文字文本
  • 要选择的文本的开头和结尾只是文档中没有任何空行的所有文本。

【问题讨论】:

  • 根本不清楚你想要什么。举个例子会有所帮助。
  • “选择”是什么意思?放置点和标记?你想以交互方式还是在 ELisp 中这样做?
  • 通过 elisp 选择开始和结束之间的文本。光标必须在块内才能匹配。

标签: emacs elisp


【解决方案1】:

在 elisp 中找到点周围的东西并不难。只需使用两个搜索,向前和向后。

(defun set-selection-around-parens()
  (interactive)
  (let ( (right-paren (save-excursion ; using save-excursion because
                                      ; we don't want to move the
                                      ; point.
                        (re-search-forward ")" nil t))) ; bound nil
                                                        ; no-error t
         (left-paren (save-excursion (re-search-backward "(" nil t))))
  (when (and right-paren left-paren)
    ;; this is actually a way to activate a mark
    ;; you have to move your point to one side
    (push-mark right-paren)
    (goto-char left-paren)
    (activate-mark))))

当您使用主要选择来选择周围的东西时,您无法保存当前点的位置(您将其命名为插入符号)。要保存当前位置并进行选择,您可以使用secondary selection

(require 'second-sel)
(global-set-key [(control meta ?y)]     'secondary-dwim)
(define-key esc-map "y"                 'yank-pop-commands)
(define-key isearch-mode-map "\C-\M-y"  'isearch-yank-secondary)

(defun secondary-selection-deactivate()
  (interactive)
  (x-set-selection 'SECONDARY nil)
  (move-overlay mouse-secondary-overlay (point-min) (point-min) (current-buffer)))

(defun secondary-selection-in-this-buffer-p()
  (and (x-get-selection 'SECONDARY) (overlayp mouse-secondary-overlay) (eq (current-buffer) (overlay-buffer mouse-secondary-overlay))))

(defun set-secondary-selection-around-parens()
  (interactive)
  (let ( (right-paren (save-excursion (re-search-forward ")" nil t)))
         (left-paren (save-excursion (re-search-backward "(" nil t))))
  (when (and right-paren left-paren)
    (primary-to-secondary left-paren right-paren)
    )))

【讨论】:

    【解决方案2】:

    您没有说明要选择的文本的开头和结尾是如何定义的。事实上,您的问题根本不清楚。如果您只想突出显示包含文字文本 &lt;carat&gt; 的所有文本,请执行以下操作:

    1. 使用 M-s hr 突出显示正则表达式。它会提示您输入正则表达式。

    2. 键入此正则表达式以匹配所有包含字符串 &lt;carat&gt;:

    3. 的文本

    \(.\|C-qC-j\)*<carat>\(.\|C-qC-j\)*
    

    (如果您指的是克拉字符 ^,而不是文字文本 &lt;carat&gt;,则将上面的文本替换为 \^。)

    C-q C-j 插入换行符。正则表达式. 匹配除换行符以外的任何字符,因此正则表达式\(.\|C-qC-j\) 匹配任何字符,包括换行符。

    【讨论】:

    • 根据您的新问题,试试这个:M-s h r \(.+C-qC-j?\)+,其中C-qC-j 是 Control-Q,后跟 Control-J(插入换行符。
    【解决方案3】:

    我知道这并不能直接回答您的问题,但根据您实际选择的内容,最好使用mark-defunmark-paragraph。对于您所处的特定模式,甚至可能有类似LaTeX-mark-environment 的东西。如果它是一种新类型文件的新模式,您可以自定义paragraph-startbeginning-of-defun 或类似的东西以获得所需的结果。当然,如果用户输入正则表达式,这将不是一个好的选择。

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多