0

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 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Lovely. However, my request was to activate visual-line-mode for all .txt files, not all text-mode files. That is, I don't think I want it for .tex files, for instance, though judging by the behaviour resulting from your suggestion, .tex files are included in text-mode-hook.
2

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.

2 Comments

Thank you very much. That is useful. If I've tested it properly, though, this is affecting all files with .txt in the buffer name?? Rather than those which end in .txt?
I don't think so: the "\\.txt\\'" specifically matches a .txt at the end of the file name (as opposed to "\\.txt$" which can also match a .txt in the middle of the name, if it is followed by a newline).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.