4.5 Magic handlers

Similar to :mode and :interpreter, you can also use :magic and :magic-fallback to cause certain function to be run if the beginning of a file matches a given regular expression, as if these regular expressions were added to magic-mode-alist and magic-fallback-mode-alist (see Auto Major Mode in GNU Emacs Lisp Reference Manual). The difference between :magic and :magic-fallback, is that the latter has a lower priority than :mode.

Here is an example:

(use-package pdf-tools
  :magic ("%PDF" . pdf-view-mode)
  :config
  (pdf-tools-install :no-query))

This registers an autoloaded command for pdf-view-mode, defers loading of pdf-tools, and runs pdf-view-mode if the beginning of a buffer matches the string "%PDF".