I got a situation here, that I couldn't get resolved with the maintainer of web-mode. So I'm asking here, if anyone knows a good suggestion.

Ok, what I have here: GNU Emacs GUI (version (x86_64-w64-mingw32) compiled from Harroogan Emacs.) Packages web-mode and header2.el. If you need more detail about header2, see right answer here on StackExchange. It's a great package, I really recommend it.


header2.el lets you define file headers for different types of file (C, shell, EmacsLisp, and so on). You can automatically insert a header when you open a new file buffer.

I ran into crashes, when I use web-mode in combination with header2.el. This is tested with an empty Emacs configuration (emacs -q).

When I use header2.el in combination with PHP-mode, JS-mode, and HTML-mode, I don't run into crashes and everything works fine.


First, you need to configure your emacs.d, in order to get the packages.

(setq user-emacs-directory "C:/Dropbox/Emacs/.emacs.d/")
(add-to-list 'load-path "C:\\Dropbox\\Emacs\\.emacs.d\\")

Then do load the packages Web-mode and header2.

(require 'header2)
(require 'web-mode) 

Then you need to configure the header, and set it up to be automatic created when create a new file:

(setq php-tag "<?php \n \n")
(setq html-close-tag " --> \n") 

(defsubst my/header-title ()
(if (eq major-mode 'php-mode)
" \n")
(concat comment-start  " File " (buffer-name))
(if (eq major-mode 'web-mode)
" \n")

(setq make-header-hook '(my/header-title))

(autoload 'auto-update-file-header "header2")
(add-hook 'write-file-hooks 'auto-update-file-header)

(autoload 'auto-make-header "header2")
(add-hook 'html-mode-hook 'auto-make-header) 
(add-hook 'web-mode-hook 'auto-make-header) 
(add-hook 'php-mode-hook 'auto-make-header) 
(add-hook 'js-mde-hook 'auto-make-header)  

Errornous behavior:

I create in Emacs a new file, called foobar.php. I get the header automatically inserted for me. And I can type around and press RET. Everything works fine. Same for foobar.js and foobar.html

Then I add (add-to-list 'auto-mode-alist '("\\.html\\'" . web-mode)) to my setup. This means that when I open a HTML file, Web-mode will be enabled as a major mode.

I restart Emacs. Then I create a new file, called foobar.html. The .html-filetype is hooked on web-mode. When typing some characters and press RET, this causes Emacs to freeze. Nothing is appearing in the *Messages*-buffer. This will not happen, when I don't add the .html extension to the Web-mode on auto-mode-alist, and get html-modeinstead.

Notice that autoheader is not a minor mode. It's a action hook, that insert the text when you enter a major mode. So I'm puzzled why it lets unrecoverrable crashing Emacs. It's a fairly simple package, after all.

Any suggestion would be appreciated.

  • So is Emacs crashing or is it freezing? And is there some problem that header2 solves that the built-in auto-insert-mode doesn't?
    – nanny
    Commented Jun 10, 2015 at 17:02
  • The Emacs is crashing, not freezing. And yes, header2 updates the modified date and version number automatically every time when I save the file. Auto-insert-mode doesn't offer that feature.
    – ReneFroger
    Commented Jun 10, 2015 at 17:59
  • Since you have not posted the actual code for html, which is not working, but the php, which is working, I'll make a wild guess here. Since html is a generic enough extension, web-mode requires you to specify an engine in the web-mode-engines-alist for html extension. Does your html config have an engine spec as required by web-mode? If it does, then please post current values of these variables: web-mode-engines-alist, web-mode-engines, and auto-mode-alist. It would also help if you load the exact config lines related to html. No need to repost the php lines.
    – Emacs User
    Commented Jun 15, 2015 at 19:18
  • Thanks for your reply. What do you mean with the actual code for html? Both code, for PHP and HTML are included in the header. As far as I know, there is no engine spec included in my configuration. There was no variable as web-mode-engines-alist available, but web-mode-engines was available: pastebin.com/s4BqGBMD and from auto-mode-alist : pastebin.com/20jwadsU But I don't get it why this could be helping.
    – ReneFroger
    Commented Jun 15, 2015 at 20:47
  • Thanks for the info. I don't see any obvious problems with html in those vars. But you certainly need a web-mode-engines-alist entry for html because web-mode requires you to associate for the html extension. There are two ways of doing this as shown on web-mode website. Since your code did not show it, I assumed you had it elsewhere in your config. Based on current valid engines as defined in web-mode.el, html extensions are probably being picked up by "go" or some other language that you have not installed. The association will force it to use a valid engine instead.
    – Emacs User
    Commented Jun 15, 2015 at 21:30

1 Answer 1


I couldn't reproduce the crash, instead my development emacs just froze. However, it froze exactly when you said yours crashed, so I'll assume the problem is the same.

This is an infinite loop (i.e. bug) in web-mode-markup-indentation-origin. Here's is the basic run-down on what's going on.

We have the following loop: (stripped of the code that doesn't matter)

(while continue
  (forward-line -1)
  (setq continue 
    (not (bobp))))

Your template has a leading space on the first line.

This means Emacs will back up to the first line, then:

  1. Try to backup another line (no-op)
  2. Go back-to-indentation (i.e. after the first space)
  3. Check if it's at the beginning of the buffer. (it's not, point = 2)

There are other termination conditions in the loop, but they never apply. This is pretty tricky since it's tied to whitespace, which part of why it's so hard to track down. (How many files do you know that start with whitespace?)

It sounds like you already have a bug open, so I've not opened one.

  • My Emacs freeze indeed. I have the same thing as you. So chrashing is a poor choice of words. I don't have a bug filled, because I was not sure if it was related with my setup. I'm so glad that you tracked it down. As it seems, it is related with web-mode-markup-indentation-origin, which explains that the bug didn't happen with HTML-mode. You seems to have more insight in this bug. Could you fill a bug report, please then? And is there any chance on a possible solution on this? I would like to validate your answer then, if you found a solution. I'm grateful for your help.
    – ReneFroger
    Commented Aug 8, 2015 at 23:32
  • I'll look into filing a bug. The solution for you is to delete the leading space in your template, if you can.
    – PythonNut
    Commented Aug 8, 2015 at 23:56
  • I made a temporilaly patch for it, until the maintainer of webmode (fxbois) found a solution: pastebin.com/1xCHbVfp and add my/patch-for-web-mode in your make-header-hook. It solved my problem. So I decided to give you the bounty, because it was a difficult bug to track it down. Thanks for your great job, PythonNut!
    – ReneFroger
    Commented Aug 9, 2015 at 0:21
  • @ReneFroger sure, you can track the bug here, I haven't personally verified the fix, but it should be done.
    – PythonNut
    Commented Aug 9, 2015 at 21:01
  • I updated it to the latest version of web-mode, and removed my patch. And I can confirm, everything is working again without any freezing. Thanks PythonNut for helping me out. If I could do somedoing back, let me know! I really appreciated your help!
    – ReneFroger
    Commented Aug 9, 2015 at 21:54

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.