感谢 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。