all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* autoloading entire function
@ 2016-06-13 15:43 Phillip Lord
  2016-06-13 17:36 ` Glenn Morris
  0 siblings, 1 reply; 3+ messages in thread
From: Phillip Lord @ 2016-06-13 15:43 UTC (permalink / raw)
  To: help-gnu-emacs


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



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: autoloading entire function
  2016-06-13 15:43 autoloading entire function Phillip Lord
@ 2016-06-13 17:36 ` Glenn Morris
  2016-06-13 20:07   ` Phillip Lord
  0 siblings, 1 reply; 3+ messages in thread
From: Glenn Morris @ 2016-06-13 17:36 UTC (permalink / raw)
  To: Phillip Lord; +Cc: help-gnu-emacs


Simply wrap your defun in a progn.
See eg the examples in Emacs sources such as cvs-dired-noselect
(simply search for ^(defun in loaddefs.el).



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: autoloading entire function
  2016-06-13 17:36 ` Glenn Morris
@ 2016-06-13 20:07   ` Phillip Lord
  0 siblings, 0 replies; 3+ messages in thread
From: Phillip Lord @ 2016-06-13 20:07 UTC (permalink / raw)
  To: Glenn Morris; +Cc: help-gnu-emacs

Glenn Morris <rgm@gnu.org> writes:

> Simply wrap your defun in a progn.
> See eg the examples in Emacs sources such as cvs-dired-noselect
> (simply search for ^(defun in loaddefs.el).

Ah, yes, hadn't thought of that.

Unfortunately, I have another issue. The source file is lexically bound,
and the function uses this feature (it returns closures). This breaks
in the autoload file.

I've tried using eval like so:

;;;###autoload
(eval
   (defun fname())
 t)

which solves both problems.

Any better way?

Phil



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-06-13 20:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-06-13 15:43 autoloading entire function Phillip Lord
2016-06-13 17:36 ` Glenn Morris
2016-06-13 20:07   ` Phillip Lord

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.