0

I have introduced the following hook function inside a minor mode. The intention is to activate the minor mode for a number of major modes. I would also need to activate outline-minor-mode once tika-minor-mode is enabled. And disable outline-minor-mode when tika-minor-mode is turned off.

;;;###autoload
(defun tika-addhook ()
  "Activate minor-mode' automatically for specific major-modes."
  (add-hook 'emacs-lisp-mode-hook #'tika-minor-mode)
  (add-hook 'sh-mode-hook         #'tika-minor-mode)
  (add-hook 'f90-mode-hook        #'tika-minor-mode)
  (add-hook 'fortran-mode-hook    #'tika-minor-mode)
  (add-hook 'latex-mode-hook      #'tika-minor-mode)
  (add-hook 'plain-tex-mode-hook  #'tika-minor-mode))

I have the following function for the task

Have seen the use of ;;;###autoload but I not sure whether it is required to call function after the require clause in my .emacs. From what I can understand, a minor mode needs some way to enable its functionality and do not think that tika-addhook would execute automatically if it not called after loading the minor mode functionality.

(add-to-list 'load-path "~/bin/tika")
(require 'tika)
(tika-addhook)
9
  • 1
    I'm almost certain this is a duplicate question, but I don't have time to look for it. Please do that, then delete this duplicate. People posting questions or answers should really check whether the question has already been posted. Thx.
    – Drew
    Commented Jun 21, 2023 at 22:36
  • 1
    @Drew Sorry: did you really say that you don't have time to find a dup but I should somehow find time myself? OK--I have deleted my answer but shall probably engage with this site a little less. Commented Jun 21, 2023 at 22:44
  • 1
    Would you have a link to it ?
    – Dilna
    Commented Jun 21, 2023 at 22:47
  • 1
    @FranBurstall: I'm just a user here, like you. Everyone can look for dup Qs. In particular it's helpful & polite for someone posting a Q to do it. Q&A is for everyone, not just for someone who writes a Q or A. Dup Qs are a bother, and unfair to others, including (1) people searching for As and (2) people who've contributed to the same, but earlier, Qs. It's better for a single Q to have N answers than for multiple Qs to split N answers. To have fewer dups everyone can help out, and yes, that takes time. (I search and vote to close lots of Qs here.)
    – Drew
    Commented Jun 22, 2023 at 3:51
  • 1
    @Randy: “but about how to write and use a minor-mode in the context of ;;;###autoload” --- So please edit your question (and perhaps the title) to clarify this point and illustrate what confuses you, making it look relatively less like a duplicate question.
    – shynur
    Commented Jun 22, 2023 at 9:14

1 Answer 1

0

Have seen the use of ;;;###autoload but I not sure whether it is required to call function after the require clause in my .emacs.

If this is your only real question, then I agree that this is a likely duplicate, though if so then you have included enough irrelevant information to make it harder to tell. Very likely the information you seek is in one of the answers to these questions:

You might also peruse the list of questions taked “autoload”: https://emacs.stackexchange.com/questions/tagged/autoload

That said, the answer is pretty simple. Adding autoloads is a way to defer the cost of loading a package until the user actually starts using it. Normally a package foo will have a foo.el which is as short and simple as possible, generally consisting just of calls to autoload that look something like this¹:

(autoload 'foo-start "foo-impl")

This tells Emacs that the function foo-start exists, and that if the user calls it then Emacs must first load a file called foo-impl.el which will define it. Then when the user puts (require 'foo) in their config file, it will not need to stop and load the whole package at once.

Since keeping that list of autoloads up to date is somewhat annoying, you can call update-file-autoloads (or make-directory-autoloads) to generate it. This would look for the ;;;###autoload cookies in your source code.

But a small package won’t benefit from this extra work, so just ignore it and put everything in foo.el.

See chapter 16.5 Autoload of the Emacs Lisp manual for more information.

¹: ELPA (and MEPLA) packages have a different convention; they automatically load foo-autoloads.el at startup so that the user doesn’t have to add anything to their init file.

9
  • Right. Meaning that autoload is just a declaration to indicate that a specific function exists. Because the actual file contents have not been loaded yet, without such declaration, the function call would generate an error because the required function has not been properly included in the code before attempting to call the function.
    – Dilna
    Commented Jun 22, 2023 at 21:53
  • That’s correct. All the autoload function does is stash away the information you give it so that if you ever try to call a function that doesn’t exist, it can double check to see if it needs to be autoloaded before throwing an error.
    – db48x
    Commented Jun 22, 2023 at 22:18
  • This also indicates that (require 'tika) does not actually load the file but scans it ?
    – Dilna
    Commented Jun 22, 2023 at 22:31
  • Where did you get that idea? Please read the doc string of require.
    – NickD
    Commented Jun 23, 2023 at 2:49
  • No, require just loads the file you specify, nothing else. If the file happens to contain calls to the autoload function, then those autoloads are registered. Otherwise, there are no autoloads. You can read the docstring of a function with C-h f.
    – db48x
    Commented Jun 23, 2023 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.