unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: phillip.lord@russet.org.uk (Phillip Lord)
To: help-gnu-emacs@gnu.org
Subject: autoloading entire function
Date: Mon, 13 Jun 2016 16:43:08 +0100	[thread overview]
Message-ID: <87mvmpf62b.fsf@russet.org.uk> (raw)


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



             reply	other threads:[~2016-06-13 15:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-13 15:43 Phillip Lord [this message]
2016-06-13 17:36 ` autoloading entire function Glenn Morris
2016-06-13 20:07   ` Phillip Lord

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87mvmpf62b.fsf@russet.org.uk \
    --to=phillip.lord@russet.org.uk \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).