views:

152

answers:

2

I have a HTML page, with html-mode enabled. I call function sgml-validate to check for any markup errors. It's based on compilation-mode. I want to remove some warnings from the compilation output, so I wrote a function and hooked it to compilation-filter-hook (this variable is not documented, but compilation-filter invokes it). Everything works. My problem is that how can I ensure my filter function only gets called when I started the compilation process on a HTML page (via sgml-validate)?

I see two methods, but none of them worked:

  • First, I can check the value of major-mode. But it always returns compilation-mode, since that is enabled on the *compilation* buffer. (I found a filter function in the source code of grep+, and they did check the value of major-mode. I can't figure out how can it work correctly.)
  • The other idea was than to only hook my filter function to the HTML file's buffer, but for similar reasons it couldn't work as the output of compilation process goes to a seperate buffer.
+1  A: 

Meanwhile, I found that compilation-start accepts an optional argument mode, which will be the major mode for the compilation buffer. So I can create a major mode derived from compilation-mode, and define there my filter function now hooked to the proper buffer.

The only problem is now that sgml-validate does not allow me to set the mode argument on compilation-start, but that's another question.

(I don't consider this the ultimate solution, of course.)

Török Gábor
+1  A: 

It sounds like you can advise smgl-validate so that it performs the filtering before it performs all it's other operations. For example:

(defadvice sgml-validate (around fix-filtering command activate)
    (let ((return-value ad-do-it))
      (filter-function return-value))))
Nathaniel Flath
Thanks, I take a look at it. In my case I think I should use an around-advise, because I want to filter the generated output, and still need the compilation buffer name that is returned by `sgml-validate`.
Török Gábor
In that case, after advise may be what you need instead of around advise - unless you need to perform operations both before and after the call to sgml-validate.
Nathaniel Flath
But can I get the return value `of `sgml-validate` when using after-advise? It tells me the name of the compilation buffer, otherwise I'm not able to run the filter function on the output.
Török Gábor
Ah, that's true. Around-advise is probably what you want, then.
Nathaniel Flath
A last remark to this answer. In the filter function I had to set `inhibit-read-only` to `t`, because the compilation buffer gets read-only status after the compilation process was finished.
Török Gábor