0

I've had an annoying moment with emacs (GUI). I'm a newbie and now I'm trying to use emacs for writing, so I need spell checking. Well, the consensus is flyspell. In order to load flyspell when starting up emacs with Org mode I've added the next settings:

(dolist (hook '(text-mode-hook))
    (add-hook hook (lambda () (flyspell-mode 1))))

But when I restarted emacs, flyspell did not work. The problem was the order of the settings. Here is a part of my init file:

;;--------------------------
;; CONFIG
;;--------------------------

;;Open notes files
(find-file "/home/pcuser/notes/notes.org")

...

;; Flyspell
(dolist (hook '(text-mode-hook))
   (add-hook hook (lambda () (flyspell-mode 1))))

When I change the order to do the flyspell part first, and then (find-file "/home/pcuser/notes/notes.org") it all works.

The big questions is: Why? And... What's the right order of the settings in the init file? For me, the last question is very important, because in the future I will add many custom settings and I don't want to guess the order of settings: maybe there exists a format, consensus or documentation for writing the init file of emacs.

Soft Specs:

OS: Debian 10

Emacs: 26.1

1
  • The question is too broad. Please remove as much as possible from your init file, to produce a minimal file that reproduces the problem. Then state just how a change to the file makes the error go away, etc.
    – Drew
    Commented Nov 26, 2019 at 1:03

2 Answers 2

1

The big questions is: Why?

Your init file is a software program, written in Emacs Lisp (elisp). As with any software, the order of instructions is important (at least in general).

Which also means...

What's the right order of the settings on init file?

...that there's no specific answer to this question. The "right order" of instructions in your init file program is any order which causes things to happen in the desired sequence, where "the desired sequence" is up to you.


Here, you are saying "open notes.org, right now":

;;Open notes files
(find-file "/home/pcuser/notes/notes.org")

After that had happened, you were then saying "in future, when a buffer changes to text-mode (or some derivative thereof, which includes org-mode), enable flyspell-mode".

;; Flyspell
(dolist (hook '(text-mode-hook))
   (add-hook hook (lambda () (flyspell-mode 1))))

Naturally this has no effect on the existing "notes.org" buffer, for which the major mode had already been set.


Very tangentially, that second piece of code could be simply:

;; Flyspell
(add-hook 'text-mode-hook #'flyspell-mode)
1

There are no general guidelines (other than "do things in the right order" which is not helpful), but there is one thing that you probably should NOT do in the initialization file and that is to open the specific file that you will be working on: the (find-file ".../notes.org") should not be there. Instead, you should open the file interactively with C-x C-f, after initialization is complete. That way, the settings will all have been (ahem) set by the time that you open the file and you will have fewer surprises.

In this particular case, the hook setting

(add-hook 'text-mode-hook (lambda () (flyspell-mode 1)))

would be done as part of initialization, and it would be ready and waiting for you when you open the file to work on it. Note, BTW, that dolist is a looping construct and since you are only setting one hook here, you do not need a loop.

The why is a bit complicated and understanding it will come with experience. But the gist of it is that often you customize the behavior of emacs by setting hooks: these are variables whose values are lists of functions. A hook is run at a specific time in the processing of e.g. a file, and when it is run, it executes each function in the list. In this case, you are adding a function to the hook text-mode-hook. When that function is run, it turns on flyspell mode in the buffer that is visiting the file.

This particular hook is run whenever a text file is visited. There are many types of files that are considered text files by emacs: org files is one type of text file.

So when you open your org file, emacs creates a buffer that visits that file; it then determines that it is a text file and runs the text-mode-hook. When the hook is run, it executes the function that you added to it, which enables flyspell mode.

So you see that in order for that to happen, the hook has to be set BEFORE you open the file. If you do it in the other order, the hook is not set when you open the file (its value is nil, the empty list). Everything happens as before, but when emacs comes to run the hook, it finds NO functions added to it, so nothing happens.

As I said before, all of this will become clearer with experience (and by reading the emacs manuals): for many of us in this forum, emacs has been our work environment for many years - and we are still learning. So, don't be discouraged (or annoyed): it's a journey of a thousand miles and you are just taking the first step.

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.