[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
autoloading entire function
From: |
Phillip Lord |
Subject: |
autoloading entire function |
Date: |
Mon, 13 Jun 2016 16:43:08 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.0.95 (gnu/linux) |
I have written a function in my package which looks like this...
(defun lentic-script-hook (mode-hook init)
(add-to-list 'lentic-init-functions
init)
(add-hook mode-hook
(lambda ()
(unless lentic-init
(setq lentic-init init)))))
It then gets used like this:
(lentic-script-hook 'python-mode-hook
'lentic-python-script-init)
Essentially, it makes writing a set of stereotyped "add-hook" calls a
bit easier.
But I now have an autoload problem. If I want to do this:
;;;###autoload
(lentic-script-hook 'python-mode-hook
'lentic-python-script-init)
My code will now break since `lentic-script-hook' is not defined.
I could, of course, add an autoload cookie to `lentic-script-hook'. But
now, when the autoload file is loaded, lentic-script-hook is called, so
loading will be forced which defeats the point of autoloading.
So, I need to put the entirely of `lentic-script-hook' into the autoload
files. Of course, it will itself not be autoloaded, but that's not a
huge problem.
How to achieve this?
The only solutions I have so far are this:
;;;###autoload (defun lentic-script-hook (mode-hook init) (add-to-list
'lentic-init-functions init) (add-hook mode-hook (lambda nil (unless
lentic-init (setq lentic-init init))))))
putting everything on oneline does the trick. But it's ugly to read,
and, if I evaluate the source file manually (i.e. I don't use
autoloads), the form is commented so does not get evaluated.
Another options is not use defun but it's expansion and autoload this:
;;;###autoload
(defalias 'lentic-script-hook
#'(lambda
(mode-hook init)
(add-to-list 'lentic-init-functions init)
(add-hook mode-hook
(lambda nil
(unless lentic-init
(setq lentic-init init))))))
which works but, also, is not ideal. The defun macro is there for a
reason, and it would be nice to use it.
Any other options I have missed?
Phil
- autoloading entire function,
Phillip Lord <=