Emacs按正则表达式缩小

时间:2018-09-04 15:44:31

标签: emacs narrowing

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

谢谢。

1 个答案:

答案 0 :(得分:0)

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

(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来安装它