为了我的特殊目的,我有高度定制的igrep.el。 搜索工作正常,但我总是得到:
Grep exited abnormally with code 1 at Wed Feb 5 18:18:09
最后。 我该如何调试代码?如何找出我修改的部分导致问题?我以前从未在emacs中调试过。
我的第二个问题是,有没有办法在igrep窗口中“突出显示”我正在igrepping的令牌?
我正在尝试将Iqbal的解决方案纳入grep命令中,如下所示:
;;;###autoload
(defun igrep (program regex files &optional options)
(interactive
(igrep-read-args))
(if (null program)
(setq program (or igrep-program "grep")))
(if (null options)
(setq options igrep-options))
(if (not (listp files)) ; (stringp files)
(setq files (list files)))
(if (and (member ?~ (mapcar 'string-to-char files))
(save-match-data
(string-match "\\`[rj]?sh\\(\\.exe\\)?\\'"
(file-name-nondirectory shell-file-name))))
;; (restricted, job-control, or standard) Bourne shell doesn't expand ~:
(setq files
(mapcar 'expand-file-name files)))
(let* ((use-zgrep (cond ((eq igrep-use-zgrep t))
(igrep-use-zgrep
(let ((files files)
(compressed-p nil))
(while (and files (not compressed-p))
(if (save-match-data
(string-match "\\.g?[zZ]\\'" (car files)))
(setq compressed-p t))
(setq files (cdr files)))
compressed-p))
(t nil)))
(command (format "%s -n %s %s %s %s %s"
(if (and use-zgrep
(save-match-data
(not (string-match "\\`z" program))))
(setq program (concat "z" program))
program)
(or options
(and igrep-case-fold-search
(equal regex (downcase regex))
"-i")
; "")
"-i") ;; R-Modified - default to ignore case
(or igrep-regex-option
(progn
(if (save-match-data
(string-match "\\`-" regex))
(setq regex (concat "\\" regex)))
""))
(shell-quote-argument regex)
(if igrep-find
(if igrep-find-use-xargs
""
(shell-quote-argument "{}"))
(mapconcat (lambda (file)
(let ((dir (file-name-directory file)))
(if dir
(expand-file-name
(file-name-nondirectory file)
(igrep-quote-file-name dir))
file)))
files " "))
igrep-null-device)))
(if igrep-find
(setq command
(igrep-format-find-command command files)))
(cond ((eq igrep-save-buffers t) (save-some-buffers t))
(igrep-save-buffers (save-some-buffers)))
(if (fboundp 'compilation-start) ; Emacs 22
(let ((compilation-process-setup-function 'grep-process-setup))
(or (fboundp 'igrep-mode)
(define-derived-mode igrep-mode grep-mode "Igrep"))
(compilation-start command
; 'igrep-mode
'grep-mode ;;Modified
nil
(cond ((eq compilation-highlight-regexp t))
(compilation-highlight-regexp
(if (eq program "fgrep")
(regexp-quote regex)
regex)))))
(compile-internal command (format "No more %s matches" program)
"Igrep" nil grep-regexp-alist)))
(with-current-buffer "*grep*"
;; Remove any previous highlights
(dolist (pattern hi-lock-interactive-patterns)
(unhighlight-regexp (car pattern)))
;; Highlight the current regex
(highlight-regexp regex))
)
答案 0 :(得分:1)
理想情况下,将grep-highlight-matches
设置为非零应突出显示grep缓冲区中的匹配项。文档说它使用--color
选项获取匹配的颜色信息,正如您所说(在注释中),操作系统上的grep
命令不支持--color
选项,这解释为什么匹配在grep缓冲区中没有突出显示。
我们仍然可以在grep
缓冲区中突出显示,方法是将igrep
命令包装在自定义函数中,然后使用Drew Adam优秀的highlight.el
包来突出显示匹配项,以下是这种功能的例子
;; Download highlight.el from [http://www.emacswiki.org/emacs-en/download/highlight.el]
;; and add it to you load-path
(require 'highlight)
(defface my-grep-highlight `((t :background "blue" :foreground "white")) "Face to highlight grep matches with")
;; You can make this buffer local in *igrep* buffer, I was too lazy
(defvar my-igrep-regex nil "Global var storing the value of last grep pattern")
;; Wrapper around igrep, reads the arguments, sets up the hooks for
;; highlighting matches then calls igrep with the read arguments
(defun my-igrep (program regex files &optional options)
(interactive
(igrep-read-args))
;; Set the current regexp
(setq my-igrep-regex regex)
;; my-grep-highlight-setup sets the compilation-filter-hook which calls
;; us back whenever it inserts some line in the *igrep* buffer this
;; gives us a chance to hightlight the matches
(let ((grep-setup-hook (cons 'my-grep-highlight-setup grep-setup-hook)))
;; Call `igrep'
(igrep program regex files options)))
(defun my-grep-highlight-setup ()
;; Setup a hook so that we are called back every time a line
;; is inserted in *igrep* buffer
(add-hook 'compilation-filter-hook 'my-highlight-regexp t t))
(defun my-highlight-regexp ()
;; compilation-filter-start is bound to the start of inserted line
(goto-char compilation-filter-start)
;; Highlight from start of line to end, this assumes the lines
;; output by grep are of the format <filename>:<line_no>:matches
;; Thanks a lot Drew Adams for this awesome function, it allows
;; us to highlight just a group of given regexp
(hlt-highlight-regexp-to-end (format ":[0-9]+:.*\\(%s\\)" my-igrep-regex)
'my-grep-highlight nil nil 1))