“pdf-tools”包是如何覆盖“dired-find-file”方法的?

huangapple go评论43阅读模式
英文:

How does the "pdf-tools" package overrides "dired-find-file" method?

问题

安装了 pdf-tools 后,dired 模式将以 PDFView 模式作为主要模式打开 PDF 文件。

(use-package pdf-tools
  :ensure t
  :config
  (pdf-tools-install t))

pdf-tools 软件包是如何实现这一功能的呢?

dired 缓冲区中,按下 RET 键的帮助信息表明它绑定到 dired-find-file

"RET"(从<return>翻译而来)运行命令 dired-find-file(在 dired-mode-map 中找到),这是一个交互式的编译过的 Lisp 函数。

我在 pdf-tools 安装的 elisp 文件中搜索了 dired-find-file,但找不到任何 advice-add 的信息?

此外,请解释一下如何查找类似这样的任意键绑定?

英文:

After installing pdf-tools the dired mode opens the pdf file with PDFView mode as major mode.

(use-package pdf-tools
  :ensure t
  :config
  (pdf-tools-install t))

How does the pdf-tools package be able to accomplish this?

The Help for RET key in dried buffer says it is bound to dired-find-file

> RET (translated from <return>) runs the command dired-find-file (found
> in dired-mode-map), which is an interactive compiled Lisp function.

I searched for dired-find-file in pdf-tools installed elisp files and could not find any advice-add's?

Also please explain how can one go about finding arbitrary key bindings like this one?

答案1

得分: 3

它修改的不是与dired直接相关的内容,而是关于Emacs如何决定一般性打开文件的方式。负责这一部分的代码位于pdf-tools-install-noverify中,它本身被pdf-tools-install调用。函数的前两行是:

(add-to-list 'auto-mode-alist pdf-tools-auto-mode-alist-entry)
(add-to-list 'magic-mode-alist pdf-tools-magic-mode-alist-entry)

相关变量pdf-tools-auto-mode-alist-entrypdf-tools-magic-mode-alist-entry是在文件pdf-tools.el中提前定义的常量。

您可以查看关于auto-mode-alistmagic-mode-alist的相关文档,但总结一下,前者是从“文件名”(更精确地说,是文件模式,通常是与文件扩展名匹配的正则表达式)到主要模式的映射,后者是从“缓冲区开头”到主要模式的映射(也可以参考这个维基百科页面上的有关魔法数字/文件标识的信息)。

至于如何确定这一点:因为它直接与键绑定/建议/函数重新定义相关,唯一的“通用”选项是探索“调用堆栈”!该软件包要求您在初始化文件的某个地方放置(pdf-tools-install)以激活该软件包,因此您可以尝试查看此函数实际执行了什么操作——稍微深入一点,您会发现它本质上是围绕pdf-tools-install-noverify的包装器,后者完成了所有的设置工作。

英文:

What it modified is not directly related to dired, but to how Emacs decides to open files in general. The part of the code that is responsible for that is in pdf-tools-install-noverify, itself called by pdf-tools-install. The first two lines of the function are:

(add-to-list &#39;auto-mode-alist pdf-tools-auto-mode-alist-entry)
(add-to-list &#39;magic-mode-alist pdf-tools-magic-mode-alist-entry)

the relevant variables pdf-tools-&lt;auto/magic&gt;-mode-alist-entry being constants defined earlier in the file pdf-tools.el.

You can check the relevant documentation for auto-mode-alist and magic-mode-alist, but to sum up, the former is a mapping from "filenames" (or more precisely, file patterns -- typically, regexps matching file extensions) to major modes, and the latter is a mapping from "beginning of a buffer" to a major mode (see also this wikipedia page on magic numbers/file signatures).

As to how one can determine that: because it is not directly related to key bindings/advices/redefinition of functions, the only "general" option is to explore the "call stack" ! The package tells you to put (pdf-tools-install) somewhere in your init file to activate the package, so you can try to see what this function actually does -- and going a bit further, you see that it is essentially a wrapper around pdf-tools-install-noverify, which does the real job of setting everything up.

huangapple
  • 本文由 发表于 2023年2月6日 07:33:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定