Emacs 按正则表达式缩小

Emacs narrow-by-regex

M-x narrow-to-lineM-x narrow-to-page等函数。哪些例程可以帮助我实现不存在的 M-x narrow-by-regex 的功能?

谢谢。

这似乎有效。将提示用户输入开始和结束正则表达式。 (没有彻底测试!):

(defun narrow-to-regex ()
  "narrow the buffer visibility to the section between two regexes the user provides"
  (interactive)
  (let* ((beginRegex (read-regexp "begin pattern"))
         (endRegex (read-regexp "end pattern"))
         (beg)
         (end))
    (goto-char (point-min)) ;; go to the start of the buffer
    (if (re-search-forward beginRegex nil t nil)
        (setq beg (- (point) (length beginRegex))))
    (if (re-search-forward endRegex nil t nil)
        (setq end (point)))
    (if (and beg end (> end beg))
        (narrow-to-region beg end)
      (message "did not find both instances of the regex, %s %s, no narrow" beg end))))

您也需要通过将其放入缓冲区(从头开始等)并依次按 CTRL+X 和 CTRL+E 来安装它