help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Hook doesn't run as expected, if buffer mode is set from major-mode


From: Rolf Ade
Subject: Re: Hook doesn't run as expected, if buffer mode is set from major-mode
Date: Sat, 09 Jan 2016 01:36:56 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

Michael Heerdegen <address@hidden> writes:
> Rolf Ade <address@hidden> writes:
>> Is it me, doing something wrong or stupid? If not, what's the
>> rationale behind this behavior?
>
> Though it's better to set `default-major-mode' and use (goto-char
> (point-max)) instead of `end-of-buffer' (which sets the mark) - it
> should work as you expect.  But it doesn't.

Thanks for confirming.

The variable `default-major-mode' was, what I used since ages in my
init.el just to learn this evening, while analyzing this problem, that
this variable is obsolete since 23.2, see C-h v default-major-mode RET.

The documentation of `default-major-mode' points to major-mode. The
behavior is the same, regardless which of the two variables I use.

Also true is your note about (goto-char (point-max)). I tested both with
the same behavior. Picked just 'end-of-buffer to simplify the
reproducing recipe.

Diging further I realize, that what the documentation say explains, what
I see. C-h v major#-mode RET say:

    When a mode is used by default, `find-file' switches to it before it
    reads the contents into the buffer and before it finishes setting up
    the buffer. Thus, the mode and its hooks should not expect [...]

If the mode-hooks run, before the content is read into the buffer it
isn't surprising that (end-of-buffer) doesn't set the point at the end
of the (up to know not read into) content.

But this documentation doesn't tell the truth.

- Prepare a file with a suffix not included in auto-mode-alist (e.g.
  test.abc) with some text in it. Create a copy of that file with the
  name test.txt

- Start emacs -Q

- In the scratch buffer, evaluate:

(setq-default major-mode 'text-mode)
(add-hook 'text-mode-hook (lambda () (message "End of buffer: %s" (point-max))))

- find-file the above prepared file test.abc

If your file test.abc contains some text, as requested, something other
than 1 is show in the message line. If the content isn't read into the
buffer at the moment, the hook runs, how could it know the (point-max)
of the up to now not known data?

rolf


reply via email to

[Prev in Thread] Current Thread [Next in Thread]