我有一个Perl脚本,它会查找一堆日志文件,寻找“有趣”的行,对于某些有趣的定义。它生成一个HTML文件,该文件由一个表组成,该表的列是时间戳,文件名/亚麻布参考和“有趣”位。我喜欢做的是将文件名/ linenum作为一个实际的链接,在emacs中将光标定位在该行号上。
emacsclientw会允许这样的事情(例如emacsclientw +60 foo.log),但我不知道要构建哪种URL / URI会让FireFox调用emacsclientw。原始HTML文件将是本地的,因此没有问题。
我应该以这种方式定义自己的MIME类型并挂钩吗?
Firefox版本为3.5,我正在运行Windows,以防任何重要事项。谢谢!
答案 0 :(得分:8)
转到firefox中的about:config
页面。添加一个新字符串:
network.protocol-handler.app.emacs
value:解析没有协议的url的脚本的路径(emacs://
之后的内容),然后使用正确的参数调用emacsclient。
您不能只放置emacsclient的路径,因为协议之后的所有内容都作为一个arg传递给可执行文件,因此您的+60 foo.log
将是一个以这种方式命名的新文件。
但你很容易想象像emacs:///path/to/your/file/LINENUM
这样的东西,并有一个小脚本删除最终的/和数字,并用数字和文件调用emacsclient: - )
EDIT2:我错了什么,协议是在arg字符串中传递给的! 这是我刚刚为我制作的一个小bash脚本,BTW感谢你的想法:-D
#!/bin/bash
ARG=${1##emacs://}
LINE=${ARG##*/}
FILE=${ARG%/*}
if wmctrl -l | grep emacs@romuald &>/dev/null; then # if there's 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 $?
我的iceweasel(firefox)中的network.protocol-handler.app.emacs
为/home/p4bl0/bin/ffemacsclient
。它运作得很好!
是的,我的笔记本电脑的名字是romuald ^^。
答案 1 :(得分:3)
感谢指针p4bl0。不幸的是,这只适用于真实的操作系统; Windows使用完全不同的方法。有关详细信息,请参阅http://kb.mozillazine.org/Register_protocol。
但是,你确实为我提供了我需要的开始,所以非常非常感谢你!
以下是Windows的解决方案:
首先,您需要正确设置注册表以处理此新URL类型。为此,请将以下内容保存到文件中,根据您的环境进行编辑,保存并双击它:
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\\\")\""
这不像p4bl0的shell脚本那样健壮,因为它不能确保Emacs先运行。然后将以下内容添加到.emacs文件中:
(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))))
以上代码不会检查以确保文件存在,并且错误处理最多也是基本的。但它有效!
然后创建一个包含以下行的HTML文件:
<a href="emacs://c:/temp/my.log/60">file: c:/temp/my.log, line: 60</a>
然后点击链接。
发布脚本:
我最近切换到Linux(Ubuntu 9.10),这就是我为该操作系统所做的事情:
$ 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
使用上面的相同emacs-uri-handler
。
答案 2 :(得分:0)
可能是编写第一个FF插件的一个很好的理由;)