I would like that when I open a file of certain type (say *.abc
)
certain function (say my-function
) automatically executes at the
opening.
Edit. The function is a buffer-dependent function and should run at the buffer containing the file
*.abc
, i.e., it should run after loading the file
.abc
not before.
The function I would like to execute is
shr-render-buffer
(but it may arise for other functions). It
renders html codes. Note that the file may have an arbitrary suffix
not just .html
After the hints in the answer of Mark an using the links in my comment below it, I tried the following:
Attempt 1:
(require 'shr)
(add-hook 'find-file-hook 'my-function)
(defun my-function ()
(when (and (stringp buffer-file-name)
(string-match "\\.html\\'" buffer-file-name))
'(shr-render-buffer)))
Attempt 2:
(require 'shr)
(add-to-list 'auto-mode-alist
'("\\.abc\\'" . (lambda ()
'(shr-render-buffer))))
Attempt 3:
(require 'shr)
(add-hook 'find-file-hook 'my-function)
(defun my-function ()
(when (string= (file-name-extension buffer-file-name) "abc")
'(shr-render-buffer)
))
Then after the suggetion of Lidydancer, I tried to define a major mode for
.abc
types. For simplicity I considered them as html-mode
and I
used the following (already provided by one of the answers in the
below links):
Attempt 4:
(require 'shr)
(add-to-list 'auto-mode-alist '("\\.abc\\'" . html-mode))
(add-hook 'html-mode-hook 'shr-render-buffer)
All my attempts were unsuccessful.