简体   繁体   English

elisp编译,在错误检测中添加一个正则表达式

[英]elisp compile, add a regexp to error detection

I am starting with emacs, and don't know much elisp. 我从emacs开始,对elisp不太了解。 Nearly nothing, really. 几乎没有,真的。

I want to use ack as a replacement of grep. 我想用ack代替grep。

These are the instructions I followed to use ack from within emacs: http://www.rooijan.za.net/?q=ack_el 这些是我在emacs中使用ack的说明: http ://www.rooijan.za.net/?q= ack_el

Now I don't like the output format that is used in this el file, I would like the output to be that of ack --group . 现在,我不喜欢此el文件中使用的输出格式,我希望输出为ack --group

So I changed: 所以我改变了:

(read-string "Ack arguments: " "-i" nil "-i" nil)

to: 至:

(read-string "Ack arguments: " "-i --group" nil "-i --group" nil)

So far so good. 到现在为止还挺好。 But this made me lose the ability to click-press_enter on the rows of the output buffer. 但是,这使我无法在输出缓冲区的行上单击click-press_enter。 In the original behaviour, compile-mode was used to be able to jump to the selected line. 在原始行为中,编译模式用于跳转到所选行。

I figured I should add a regexp to the ack-mode. 我认为我应该在ack模式下添加一个正则表达式。 The ack-mode is defined like this: ack模式的定义如下:

(define-compilation-mode ack-mode "Ack"
  "Specialization of compilation-mode for use with ack."
   nil)

and I want to add the regexp [0-9]+: to be detected as an error too, since it is what every row of the output bugger includes (line number). 并且我想添加正则表达式[0-9]+:将被检测为错误,因为这是输出Bugger的每一行都包含的内容(行号)。

I've tried to modify the define-compilation-mode above to add the regexp, but I failed miserably. 我试图修改上面的define-compilation-mode以添加正则表达式,但是我失败了。

How can I make the output buffer of ack let me click on its rows? 如何使ack的输出缓冲区让我单击其行?

--- EDIT, I tried also: --- ---编辑,我也尝试过:---

(defvar ack-regexp-alist 
    '(("[0-9]+:"
     2 3))
  "Alist that specifies how to match rows in ack output.")

(setq compilation-error-regexp-alist
      (append compilation-error-regexp-alist
          ack-regexp-alist))

I stole that somewhere and tried to adapt to my needs. 我在某个地方偷了东西,试图适应我的需求。 No luck. 没运气。

--- EDIT, result after Ivan's proposal --- ---编辑,经过伊凡的提议后的结果---

With ack.el updated to include: ack.el更新为包括:

(defvar ack-regexp-alist 
  '(("^[0-9]+:" ;; match the line number
     nil        ;; the file is not found on this line, so assume that it's the same as before
     0          ;; The line is the 0'th subexpression (the whole thing)
     )
    ("^[^: ]+$" ;; match a file -- this could be better
     0          ;; The file is the 0'th subexpression
     ))
  "Alist that specifies how to match rows in ack output.")

(setq compilation-error-regexp-alist
      (append compilation-error-regexp-alist
          ack-regexp-alist))


(define-compilation-mode ack-mode "Ack"
  "Specialization of compilation-mode for use with ack."
   nil) 

Then checking the compilation-error-regext-alist variable, I get the value: 然后检查compilation-error-regext-alist变量,我得到的值是:

(absoft ada aix ant bash borland caml comma edg-1 edg-2 epc ftnchek iar ibm irix java jikes-file jikes-line gnu gcc-include lcc makepp mips-1 mips-2 msft oracle perl rxp sparc-pascal-file sparc-pascal-line sparc-pascal-example sun sun-ada 4bsd gcov-file gcov-header gcov-nomark gcov-called-line gcov-never-called
        ("^[0-9]+:" nil 0)
        ("^[^: ]+$" 0))

I find the format of the variable very strange, isn't it? 我发现变量的格式很奇怪,不是吗? I don't know elisp (yet), so maybe it's correct that way. 我还不知道elisp,所以也许这是正确的。

Still no links or color in the *ack* buffer. * ack *缓冲区中仍然没有链接或颜色。

There is another full-ack package up on ELPA which I have used before and handles --group output. 我以前使用过ELPA上的另一个full-ack包软件包,用于处理--group输出。

That said, reading the documentation for compilation-error-regexp-alist you see that it has the form: 就是说,阅读compilation-error-regexp-alist文档后,您会发现它具有以下形式:

(REGEXP FILE [LINE COLUMN TYPE HYPERLINK HIGHLIGHT...])

In the case of --group output, you have to match file and line separately, so I think you want something like (untested) --group输出的情况下,您必须分别匹配文件和行,所以我认为您想要的是(未经测试的)

(defvar ack-regexp-alist
  '(("^\\S +$" ;; match a file -- this could be better
     0          ;; The file is the 1st subexpression
     )
    ("^[0-9]+:" ;; match the line number
     nil        ;; the file is not found on this line, so assume that it's the same as before
     0          ;; The line is the 0'th subexpression (the whole thing)
     ))
  "Alist that specifies how to match rows in ack output.")

-- Updated -- - 更新 -

The variable compilation-error-regext-alist is a list of symbols or elements like (REGEXP ...) . 变量compilation-error-regext-alist是符号或元素列表,例如(REGEXP ...) Symbols are looked up in compilation-error-regexp-alist-alist to find the corresponding elements. compilation-error-regexp-alist-alist中查找符号以找到相应的元素。 So yes, it is a little weird, but it's easier to see what's turned on and off without having to look at ugly regexes and guess what they do. 所以,是的,这有点奇怪,但是无需查看丑陋的正则表达式并猜测它们的作用,就可以更轻松地查看打开和关闭的内容。 If you were going to distribute this I would suggest adding the regex to compilation-error-regexp-alist-alist and then turning it on in compilation-error-regext-alist , but that is somewhat moot until you get it to work correctly. 如果您打算分发此文件,我建议将正则表达式添加到compilation-error-regexp-alist-alist ,然后在compilation-error-regext-alist其打开,但这在您使其正常工作之前compilation-error-regext-alist定论。

Looking more closely at ack.el, I notice that it uses 仔细观察ack.el,我注意到它使用了

(let (compile-command
      (compilation-error-regexp-alist grep-regexp-alist)
      ...)
  ...
  )

In other words it locally overwrites compilation-error-regexp-alist with grep-regexp-alist , so you need to add the regexes there instead. 换句话说,它会使用grep-regexp-alist局部覆盖compilation-error-regexp-alist grep-regexp-alist ,因此您需要在其中添加正则表达式。 Or even better might be to replace it with 甚至更好的可能是将其替换为

(let (compile-command
      (compilation-error-regexp-alist ack-regexp-alist)
      ...)
  ...
  )

In the end I still recommend full-ack since the filename regex does not seem to be working correctly. 最后,我仍然建议使用全确认,因为文件名regex似乎无法正常工作。 It seems more complete (though more complicated), and I have been happy with it. 它似乎更完整(尽管更复杂),我对此感到满意。

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

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