【问题标题】:Remove header information from rgrep/grep output in emacs?从 emacs 中的 rgrep/grep 输出中删除标头信息?
【发布时间】:2013-04-20 16:50:12
【问题描述】:

这就是我运行rgrep 时缓冲区的样子。我只想看看我的结果——所有其他信息对我来说毫无意义。

-*- mode: grep; default-directory: "~/.emacs/" -*-
Grep started at Sat Jan 20 12:42:21

grep  --exclude=.\#\* --exclude=\*.o --exclude=\*\~ --exclude=\*.bin --exclude=\*.lbin --exclude=\*.so --exclude=\*.a --exclude=\*.ln --exclude=\*.blg --exclude=\*.bbl --exclude=\*.elc --exclude=\*.lof --exclude=\*.glo --exclude=\*.idx --exclude=\*.lot --exclude=\*.fmt --exclude=\*.tfm --exclude=\*.class --exclude=\*.fas --exclude=\*.lib --exclude=\*.mem --exclude=\*.x86f --exclude=\*.sparcf --exclude=\*.dfsl --exclude=\*.pfsl --exclude=\*.d64fsl --exclude=\*.p64fsl --exclude=\*.lx64fsl --exclude=\*.lx32fsl --exclude=\*.dx64fsl --exclude=\*.dx32fsl --exclude=\*.fx64fsl --exclude=\*.fx32fsl --exclude=\*.sx64fsl --exclude=\*.sx32fsl --exclude=\*.wx64fsl --exclude=\*.wx32fsl --exclude=\*.fasl --exclude=\*.ufsl --exclude=\*.fsl --exclude=\*.dxl --exclude=\*.lo --exclude=\*.la --exclude=\*.gmo --exclude=\*.mo --exclude=\*.toc --exclude=\*.aux --exclude=\*.cp --exclude=\*.fn --exclude=\*.ky --exclude=\*.pg --exclude=\*.tp --exclude=\*.vr --exclude=\*.cps --exclude=\*.fns --exclude=\*.kys --exclude=\*.pgs --exclude=\*.tps --exclude=\*.vrs --exclude=\*.pyc --exclude=\*.pyo -i -nH -e grep *.el

    init.el:236:(global-set-key (kbd "s-f") 'rgrep)
    init.el:237:(global-set-key (kbd "C-f") 'lgrep)

Grep finished (matches found) at Sat Jan 20 12:42:21

【问题讨论】:

  • 或者,(defun my-grep-mode-hook () (setq truncate-lines t)) (add-hook 'grep-mode-hook 'my-grep-mode-hook) 确保标题(和其他行)不换行,使其不那么突兀。如果您想查看换行符,可以致电 toggle-truncate-lines,但我很少需要这样做,所以我发现这种方法非常合适。

标签: emacs grep elisp emacs24


【解决方案1】:

标题插入隐藏在rgrep 深处对compilation-start 的调用中。没有一种简单的方法可以阻止这些函数插入标题。但是你可以很容易地通过定义通知来隐藏缓冲区的前四行。

(defun delete-grep-header ()
  (save-excursion
    (with-current-buffer grep-last-buffer
      (goto-line 5)
      (narrow-to-region (point) (point-max)))))

(defadvice grep (after delete-grep-header activate) (delete-grep-header))
(defadvice rgrep (after delete-grep-header activate) (delete-grep-header))

您稍后可以使用 C-xnw 再次扩大以显示这些线条。

如果您还想建议lgrepgrep-findzrgrep,您可以将上面的defadvice 宏替换为这样的程序化建议:

(defvar delete-grep-header-advice
  (ad-make-advice
   'delete-grep-header nil t
   '(advice lambda () (delete-grep-header))))

(defun add-delete-grep-header-advice (function)
  (ad-add-advice function delete-grep-header-advice 'after 'first)
  (ad-activate function))

(mapc 'add-delete-grep-header-advice
      '(grep lgrep grep-find rgrep zrgrep))

【讨论】:

  • 我参加这个聚会很晚,但非常感谢您的回答!
【解决方案2】:

Michael Hoffman 的一个小补充:我们可以在 grep 缓冲区中添加一个小按钮来切换隐藏文本:

(defmacro with-grep-buffer-writable (&rest body)
  `(save-excursion
     (with-current-buffer grep-last-buffer
       (setq buffer-read-only nil)
       ,@body
       (setq buffer-read-only t))))

(defun hide-grep-header (&rest ignored)
  "Hides (by narrowing) the first few lines of a grep buffer leaving only 
the results. Additionally, a button is created to toggle the lines."
  (with-grep-buffer-writable
   ;;HACK: If we (goto-line 5), the button is inserted *after* the results.
   (goto-line 4)
   (end-of-line)
   (insert "\n")
   (narrow-to-region (point) (point-max))
   (insert-text-button "(...)"
                       'help-echo "Toggle display of grep invocation"
                       'action #'click-show-grep-button)))

(defun click-hide-grep-button (button)
  (with-grep-buffer-writable
   (button-put button 'action #'click-show-grep-button)
   (narrow-to-region (button-start button) (point-max))))

(defun click-show-grep-button (button)
  (with-grep-buffer-writable
   (button-put button 'action #'click-hide-grep-button)
   (widen))
  ; HACK: goto-line won't have any effect because of save-excursion
  (with-current-buffer grep-last-buffer
    (goto-line 1)))

(advice-add 'grep :after #'hide-grep-header)
(advice-add 'rgrep :after #'hide-grep-header)

【讨论】:

    【解决方案3】:
    (defun my/grep-fix ()
      (save-excursion
        (let ((inhibit-read-only t))
          (goto-line 4)
          (kill-whole-line))))
    
    (add-hook 'grep-setup-hook 'my/grep-fix)
    

    这个解决方案:

    • next-error 配合得很好(即它不会跳过第一场比赛);
    • 保留包含根目录的标头;
    • 可在所有 grep 变体中无缝运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      • 2012-09-21
      • 2010-10-08
      • 1970-01-01
      • 2019-02-18
      • 1970-01-01
      • 2014-01-15
      相关资源
      最近更新 更多