【问题标题】:How to configure firefox to run emacsclientw on certain links?如何配置 firefox 在某些链接上运行 emacsclientw?
【发布时间】:2010-01-20 07:51:04
【问题描述】:

我有一个 Perl 脚本,它可以搜索一堆日志文件,寻找“有趣”的行,以获得一些有趣的定义。它生成一个 HTML 文件,该文件由一个表格组成,表格的列是时间戳、文件名/行号引用和“有趣”位。我想做的是让 filename/linenum 成为一个实际的链接,它将在 emacs 中将光标定位在该行号上的光标打开。

emacsclientw 将允许这样的事情(例如 emacsclientw +60 foo.log),但我不知道构建什么样的 URL/URI 可以让 FireFox 调用 emacsclientw。原始的 HTML 文件将是本地的,所以那里没有问题。

我应该以这种方式定义自己的 MIME 类型和挂钩吗?

Firefox 版本是 3.5,我正在运行 Windows,以防万一。谢谢!

【问题讨论】:

    标签: html firefox emacs


    【解决方案1】:

    在 Firefox 中转到 about:config 页面。添加一个新字符串:

    network.protocol-handler.app.emacs
    

    value:一个脚本的路径,该脚本在没有协议的情况下解析 url(emacs:// 之后的内容),然后使用正确的参数调用 emacsclient。

    您不能只放置 emacsclient 的路径,因为协议之后的所有内容都作为一个 arg 传递给可执行文件,因此您的 +60 foo.log 将是一个以这种方式命名的新文件。

    但是你可以很容易地想象像emacs:///path/to/your/file/LINENUM 这样的东西,并且有一个小脚本可以删除最后的 / 和数字并使用数字和文件调用 emacsclient :-)

    编辑:如果您愿意,我可以在 bash 中执行此操作,但我不知道如何使用 Windows“shell”或其他任何名称来执行此操作。

    EDIT2:我错了,协议在 arg 字符串中传递给 ! 这是我刚为我制作的一个小 bash 脚本,顺便说一句,谢谢你的想法:-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 ^^。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        可能是编写你的第一个 FF 插件的好理由;)

        【讨论】:

          猜你喜欢
          • 2011-08-26
          • 2021-09-15
          • 2016-01-24
          • 2016-08-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多