封装 webjump,搜索 symbol-at-point 的内容

写配置看到了 webjump,可以定义一些搜索链接,选择预置的链接,输入关键字进行搜索(利用 brower-url)。

我觉得用来搜索 point 下的内容也挺好的,就糊了个简单函数,用来搜索 symbol-at-point 的内容,例如用 Kagi 搜索、Kagi 翻译、搜索 GitHub、MDN 等常用文档。

不知道有没有已经存在的包实现了类似功能,没怎么找,有的话也欢迎分享。


效果:

Kapture 2026-01-25 at 21.46.39


代码
(use-package webjump
  :bind (("C-M-?" . spike-leung/webjump-symbol-at-point))
  :config
  (setq webjump-sites '(("Kagi" . [simple-query "kagi.com" "kagi.com/search?q=" ""])
                        ("Kagi Translate" . [simple-query "translate.kagi.com" "translate.kagi.com/?from=auto&to=zh_cn&text=" ""])
                        ("Wikipedia" . [simple-query "wikipedia.org" "wikipedia.org/wiki/" ""])
                        ("GitHub" . [simple-query "github.com" "github.com/search?q=" ""])
                        ("MDN" . [simple-query "developer.mozilla.org" "developer.mozilla.org/en-US/search?q=" ""])))
  (defun spike-leung/webjump-symbol-at-point ()
    "获取光标下的 symbol 并通过 webjump 搜索。"
    (interactive)
    (let* ((completion-ignore-case t)
           (content (if (use-region-p)
                        (buffer-substring-no-properties (region-beginning) (region-end))
                      (thing-at-point 'symbol t)))
           (query (read-string (format "Webjump search (%s): " (or content ""))
                               nil nil content))
           (item (assoc-string
                  (completing-read "WebJump to site: " webjump-sites nil t)
                  webjump-sites t))
           (name (car item))
           (expr (cdr item))
           (query-prefix (aref expr 2))
           (query-suffix (aref expr 3))
           (fun (if webjump-use-internal-browser
                    (apply-partially #'browse-url-with-browser-kind 'internal)
                  #'browse-url)))
      (funcall fun (webjump-url-fix
                    (cond ((concat query-prefix (webjump-url-encode query) query-suffix))
                          (t (error "WebJump URL expression for \"%s\" invalid"
                                    name))))))))
1 个赞

很多包都有在线搜文档的功能,比如haskell-hoogle可以自动搜hoogle,slime-documentation-lookup可以自动搜hyperspec,也是可以自己配置上游链接的。但是这方面没有统一接口,因为它们可能会自己处理返回页面里的链接,实现文档buffer内部的链接跳转,这个是需要Emacs里面集成的东西。

你这种聚合搜索的模式我更倾向于做成一个独立的网络聚合搜索服务,因为这个功能完全不用集成到Emacs里面去。比如说,一个简单的实现:在Emacs里面调LLM。

顺带一提,如果你经常使用操作系统API编程,你更常用的可能是man和info,而不是web文档。Emacs里面搜这些东西的工作流有很多集成(比如ace-jump)。

谢谢分享 :slight_smile:

这个函数主要是看到了 webjump,加上我平时用搜索引擎比较多,所以就顺便封装一下。

我经常需要查询一下 Web 相关的语法,在 Emacs 里可以快速获取 point 下的相关语法直接查询就会方便一点。(这方面 Emacs 好像也可以集成 dash 查询文档,不过网站信息可能更新一些)

原来可能在 Emacs 里碰到一些东西想了解一下,可能需要复制,再搜索 (针对那些 Emacs 里面可能没有的文档或内容)。

糊个函数,就可以直接获取相关内容,调用对应的网站搜索,方便一下这个场景而已。

我觉得之前yibie分享的一篇关于基于超链接的桌面系统(好像不是这个名字,差不多这个意思)的文章说的挺好。