Talespin_Kit
Talespin_Kit

Reputation: 21897

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

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 ) 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?

Upvotes: 0

Views: 245

Answers (1)

Numbra
Numbra

Reputation: 642

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 'auto-mode-alist pdf-tools-auto-mode-alist-entry)
(add-to-list 'magic-mode-alist pdf-tools-magic-mode-alist-entry)

the relevant variables pdf-tools-<auto/magic>-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.

Upvotes: 3

Related Questions