在gid窗口中突出显示搜索到的标记

时间:2014-02-27 01:24:46

标签: emacs elisp

我在.emacs文件中有以下代码段:

(defvar gid-command "gid" "The command run by the gid function.")
(defun gid-select (args)
  "Run gid, with user-specified ARGS, and collect output in a buffer.
While gid runs asynchronously, you can use the \\[next-error] command to
find the text that gid hits refer to. The command actually run is
defined by the gid-command variable."
  (interactive (list
        (read-input (concat "Run " gid-command " (with args): ") ;confirmation
        ;(word-around-point)
        ))
        )
  ;; Preserve the present compile-command
  (let (compile-command
    (gid-buffer  ;; if gid for each symbol use: compilation-buffer-name-function
     (lambda (mode) (concat "*gid " args "*"))))
    ;; For portability between v18 & v19, use compile rather than compile-internal
    (compile (concat gid-command " " args))))

如何在gid编辑窗口中突出显示我正在寻找的令牌。

我尝试使用Iqbal的解决方案突出显示igrep窗口中的正则表达式,但在gid中找不到类似的grep-setup-hook。

1 个答案:

答案 0 :(得分:1)

以下是我修改后的原始答案,适用于gid搜索

;;;; Code for highlighting the matches
(defface my-gid-highlight `((t :background "blue" :foreground "white")) "Face to highlight grep matches with")

(defvar my-current-id-regexp nil)

(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 my-current-id-regexp 'my-gid-highlight nil nil 1))

(defun my-gid-highlight-setup (buffer)
  ;; Setup a hook so that we are called back every time a line
  ;; is inserted in *compilation* buffer
  (add-hook 'compilation-filter-hook 'my-highlight-regexp t t))

;;;; Your original code with some modifications
(defvar gid-command "gid" "The command run by the gid function.")

(defun gid-select (args)
  "Run gid, with user-specified ARGS, and collect output in a buffer.
    While gid runs asynchronously, you can use the \\[next-error] command to
    find the text that gid hits refer to. The command actually run is
    defined by the gid-command variable."
  (interactive (list (read-input (concat "Run " gid-command " (with args): "))))
  ;; Dynamically set the compilation-start-hook to setup gid highlighting
  (let* ((compilation-start-hook '(my-gid-highlight-setup))
         ;; Assuming the token to be searched is at the end of
         ;; args
         (search-token (car (last (split-string args)))))
    ;; Set the regexp used to highlight the token (use in "my-highlight-regexp")
    (setq my-current-id-regexp (format ":[0-9]+:.*\\(%s\\)" search-token))
    ;; Start gid
    (compilation-start (concat gid-command " " args))))