简体   繁体   English

如何在Emacs中调试igrep

[英]How to debug igrep in emacs

I have highly customized igrep.el for my special purpose. 我为我的特殊目的高度定制了igrep.el。 The search works fine but I always get : 搜索工作正常,但我总是得到:

Grep exited abnormally with code 1 at Wed Feb  5 18:18:09

At the end. 在末尾。 How do I go about debugging the code? 我该如何调试代码? How to find out which part I modified is causing the problem? 如何找出我修改的哪一部分导致了该问题? I never debugged in emacs before. 我以前从未在emacs中调试过。

My second question, is there a way to "highlight" the token I am igrepping for in the igrep window? 我的第二个问题,是否有办法在igrep窗口中“突出显示”我要igrep的令牌?

I am trying Iqbal's solution by incorporating the highlight inside the grep comand like so: 我正在尝试通过在grep comand中加入突出显示来尝试Iqbal的解决方案,如下所示:

;;;###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))

)

Ideally setting grep-highlight-matches to non-nil should have highlighted the matches in the grep buffer. 理想情况下,将grep-highlight-matches设置为non-nil应该会在grep缓冲区中突出显示匹配项。 The documentation says that it uses --color option for getting color information for matches and as you say (in the comments) the grep command on your OS does not support the --color option, this explains why the matches are not already highlighted in the grep buffer. 该文档说,它使用--color选项来获取匹配项的颜色信息,并且正如您所说(在注释中),操作系统上的grep命令不支持--color选项,这说明了为什么未在其中突出显示匹配项grep缓冲区。

We can still get highlighting in the grep buffer by wrapping the igrep command in a custom function and then using Drew Adam's excellent highlight.el package to highlight the matches, the following is an example of such a function 我们仍然可以通过将igrep命令包装到自定义函数中,然后使用Drew Adam的出色的highlight.el包突出显示匹配项,来在grep缓冲区中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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM