0
votes

I indicate my muse-mode files (usually named with .txt suffix) as being muse-mode by starting them with a "#title". To do this, I have

    

    ;; muse-mode on *.txt files, if a #title or sect. header is on top 4 lines
    (add-hook 'text-mode-hook
              (lambda ()
                (unless (or (eq major-mode 'muse-mode)
                (not (stringp buffer-file-truename)))
                  (when (equal (file-name-extension buffer-file-truename) "txt")
                    (save-excursion
                      (goto-line 5)
                      (if (re-search-backward "\* [A-Z][a-z]+.*\\|#title " 1 t)
                          (muse-mode)))))))

If I also have


    (add-to-list 'auto-mode-alist '("\\.txt$" . visual-line-mode)) 

in the .emacs (following the code above), then muse-mode no longer works. Though if I invoke visual-line-mode with Meta-x from within emacs on a muse file, it doesn't mess things up.

Ideally, I would like to have visual-line-mode working on all .txt files, but without messing up muse. Or else, I would like to start all .txt files in visual-line-mode except when they are muse files.

2

2 Answers

3
votes

The variable 'auto-mode-alist chooses the major mode.

visual-line-mode is a minor mode, and by adding it to the 'auto-mode-alist you're making it act like a major mode, which replaces the text-mode you were starting with.

Instead, add turn-on-visual-line-mode-in-txt to the text-mode-hook like so:

(add-hook `text-mode-hook 'turn-on-visual-line-mode)

(defun turn-on-visual-line-mode-in-txt ()
  (when (and (buffer-file-name)
             (string-match ".txt$" (buffer-file-name)))
    (turn-on-visual-line-mode)))

For more information on the differences, read the manual for major and minor modes.

2
votes

I think @treyJackson identified the problem, but here are some extra comments:

BTW, your use of a text-mode-hook to switch to muse-mode will misbehave in various circumstances (because you first switch to text-mode, then halfway through you activate muse-mode, after which the end of the text-mode activation (usually, not much left to do, but there could be more functions on the text-mode-hook to run) will still be performed). A more robust approach might be to do:

(add-to-list 'auto-mode-alist '("\\.txt\\'" . text-or-muse-mode))

(defun text-or-muse-mode ()
  (if (save-excursion
        (goto-line 5)
        (re-search-backward "\\* [A-Z][a-z]+.*\\|#title " 1 t))
      (muse-mode)
    (text-mode)))

Of course, you could also use a -*- muse -*- on the first line, or rely on magic-mode-alist instead.