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)
Dilna
  • 1,173
  • 3
  • 10
  • 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 Jun 21 '23 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. – Fran Burstall Jun 21 '23 at 22:44
  • 1
    Would you have a link to it ? – Dilna Jun 21 '23 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 Jun 22 '23 at 03:51
  • @FranBurstall: And *I didn't say you should delete your answer.* **Best** is to add it to the same question posed earlier, *if* there is one. But yeah, for that you might have to search a bit. Until someone does that, **second best** is to undelete your answer here, so at least the question is answered, even if it might be a dup. My comment was intended to hopefully help by suggesting that I think this question might be a dup - just a guess. (I've voted to undelete your answer.) – Drew Jun 22 '23 at 03:57
  • Does this answer your question? [Enabling a minor mode in all but some buffers](https://emacs.stackexchange.com/questions/50339/enabling-a-minor-mode-in-all-but-some-buffers) – Drew Jun 22 '23 at 04:02
  • FWIW: Searching for these 3 tags together shows several duplicate questions: `[major-mode] [minor-mode] [hooks]`. It would be great if Someone(TM) tried to consolidate some of them. – Drew Jun 22 '23 at 04:05
  • This question is not about how to make a hook, but about how to write and use a minor-mode in the context of `;;;###autoload`. – Dilna Jun 22 '23 at 06:26
  • 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 Jun 22 '23 at 09:14

1 Answers1

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.

db48x
  • 15,741
  • 1
  • 19
  • 23
  • 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 Jun 22 '23 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 Jun 22 '23 at 22:18
  • This also indicates that `(require 'tika)` does not actually load the file but scans it ? – Dilna Jun 22 '23 at 22:31
  • Where did you get *that* idea? Please read the doc string of `require`. – NickD Jun 23 '23 at 02: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 Jun 23 '23 at 07:20
  • Ok. Thus, loading a file does not mean that the code within the file is immediately available for use. Loading a file simply executes the code within it, but the availability of functions and variables defined in that file depends on the scoping rules and when they are evaluated. – Dilna Jun 23 '23 at 08:23
  • Technically true, but most of the time those complications are irrelevant. 99.99% of the time a file that you load is just a bunch of `defvar`s and `defun`s and everything is evaluated all in one go. If you have more questions about this, search the documentation, search the existing questions, and if you still can’t figure it out then create new questions here on StackExchange. In the long term this kind of back and forth in the comments is unhelpful to others. – db48x Jun 23 '23 at 08:41
  • I agree with you. Using `require` on a file makes the defun's and defvar's available even without `;;;###autoload`. – Dilna Jun 23 '23 at 10:06
  • Of course, because that’s a comment. Comments don’t change what happens when the file is loaded. – db48x Jun 23 '23 at 13:09