2013-04-20 15 views
7

これは、rgrepを実行したときのバッファの外観です。私は私の結果を見たいと思う - 他のすべての情報は私にとって無意味です。emacsのrgrep/grep出力からヘッダー情報を削除しますか?

-*- 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 
+0

代わりに、 '' '(アドオンフック「のgrepモードフック(私の-grepのモードフック()(setqの切り捨てラインt)をDEFUN):grepをバッファに隠されたテキストを切り替えます'my-grep-mode-hook) 'を使用すると、ヘッダー(および他の行)はラップしないので、目立たなくなります。ラップされた行を見たい場合は 'toggle-truncate-lines'を呼び出すことができますが、これを行う必要はほとんどありません。 – phils

答えて

10

ヘッダインサートはrgrep内深いcompilation-startへの呼び出し内に埋め込まれています。これらの関数がヘッダを挿入しないようにする簡単な方法はありません。しかし、アドバイスを定義することで、バッファの最初の4行をかなり簡単に隠すことができます。

(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)) 

後で W C-XNで再び、これらの行を明らかに広げることができます。

あなたもlgrepgrep-findと​​に助言したい場合、あなたはこのように助言のプログラムと上記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)) 
+0

私はこのパーティーに非常に遅れていますが、この答えのためにトンをありがとう! –

1

少量の添加マイケル・ホフマンさんの答え:私たちは小さなボタンを追加することができますが

(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) 
関連する問題