How to configure firefox to run emacsclientw on specific links?

I have a Perl script that lays down a bunch of log files, looking for "interesting" lines, for some definition of interesting. It generates an HTML file, which consists of a table whose columns are a timestamp, a link to the file name / linenum and an “interesting” bit. I would really like the file name / linenum to be the actual link that will display this file using the cursor located on this line number in emacs.

emacsclientw will allow such a thing (for example, emacsclientw +60 foo.log), but I don’t know what URL / URI should be built, which will allow FireFox to call emacsclientw. The source HTML file will be local, so there are no problems there.

Should I define my own MIME type and connect in this way?

Firefox version is 3.5 and I am running Windows if that matters. Thank!

+3
html firefox emacs
Sep 09 '09 at 20:02
source share
3 answers

Thanks for the pointer, p4bl0. Unfortunately, this only works on a real OS; Windows uses a completely different method. See http://kb.mozillazine.org/Register_protocol for more details.

But, of course, you provided me with the beginning I needed, so thank you so much!

Here is the solution for Windows:

First you need to configure the registry correctly to handle this new type of URL. To do this, save the following in a file, edit it according to your environment, save it and double-click on it:

Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\emacs] @="URL:Emacs Protocol" "URL Protocol"="" [HKEY_CLASSES_ROOT\emacs\shell] [HKEY_CLASSES_ROOT\emacs\shell\open] [HKEY_CLASSES_ROOT\emacs\shell\open\command] @="\"c:\\product\\emacs\\bin\\emacsclientw.exe\" --no-wait -e \"(emacs-uri-handler \\\"%1\\\")\"" 

This is not as reliable as the p4bl0 shell script, because it does not guarantee that Emacs will work in the first place. Then add the following to the .emacs file:

 (defun emacs-uri-handler (uri) "Handles emacs URIs in the form: emacs:///path/to/file/LINENUM" (save-match-data (if (string-match "emacs://\\(.*\\)/\\([0-9]+\\)$" uri) (let ((filename (match-string 1 uri)) (linenum (match-string 2 uri))) (with-current-buffer (find-file filename) (goto-line (string-to-number linenum)))) (beep) (message "Unable to parse the URI <%s>" uri)))) 

The above code will not check to make sure that the file exists, and error handling is rudimentary at best. But it works!

Then create an HTML file with the following lines:

 <a href="emacs://c:/temp/my.log/60">file: c:/temp/my.log, line: 60</a> 

and then click the link.

Post Script:

I recently switched to Linux (Ubuntu 9.10), and here is what I did for this OS:

 $ gconftool -s /desktop/gnome/url-handlers/emacs/command '/usr/bin/emacsclient --no-wait -e "(emacs-uri-handler \"%s\")"' --type String $ gconftool -s /desktop/gnome/url-handlers/emacs/enabled --type Boolean true 

Using the same emacs-uri-handler on top.

+3
Sep 10 '09 at 14:38
source share

Go to the about:config page in Firefox. Add a new line:

 network.protocol-handler.app.emacs 

value: path to a script that parses the URL without a protocol (which is after emacs:// ), and then calls emacsclient with the correct argument.

You cannot just put the emacsclient path because everything after the protocol is passed as one argument to the executable, so your +60 foo.log will be a new file with that name.

But you can easily imagine something like emacs:///path/to/your/file/LINENUM and a little script that remove the final / and number and call emacsclient with the number and file :-)

EDIT: I could do this in bash if you want, but I don't know how to do this with shell windows or whatever is being called.

EDIT2: I am mistaken in something, the protocol is passed in the arg line for! Here is a little bash script that I just made for me, BTW thanks for the idea: -D

 #!/bin/bash ARG=${1##emacs://} LINE=${ARG##*/} FILE=${ARG%/*} if wmctrl -l | grep emacs@romuald &>/dev/null; then # if there already an emacs frame ARG="" # then just open the file in the existing emacs frame else ARG="-c" # else create a new frame fi emacsclient $ARG -n +$LINE "$FILE" exit $? 

and my network.protocol-handler.app.emacs in my iceweasel (firefox) /home/p4bl0/bin/ffemacsclient . It works great!

And yes, my laptop name is romuald ^^.

+8
09 Sep '09 at 20:16
source share

Perhaps this is a great reason to write your first FF plugin;)

0
Sep 09 '09 at 20:17
source share



All Articles