0

I am editing a Drupal module file which doesn't use either a .php or .module extension.

How can I configure emacs to recognize the file alone as a PHP file:

  1. regardless of the extension and
  2. only for the session

If the file was the only file with that extension in the session how would I set the mode by the extension?

1

How can I configure emacs to recognize the file alone as a PHP file:

  1. regardless of the extension and
  2. only for the session

As major modes are just functions, you can enable any major-mode, whenever you like, in whichever buffer you like, by simply invoking its corresponding command. In your case, assuming the desired mode is called php-mode, you would invoke M-x php-mode RET in the buffer in question. There is usually nothing persistent about changing major-mode (unless a particular mode writes data to disk when enabled or similar), so the file would not automatically be detected as PHP the next time you read it.

If the file was the only file with that extension in the session how would I set the mode by the extension?

This is normally achieved by modifying the variable auto-mode-alist, which maps filename patterns in the form of Elisp regular expressions to corresponding major modes. See the Emacs manual node on Choosing Modes and the variable's documentation (C-h v auto-mode-alist RET or M-x describe-variable RET auto-mode-alist RET) for more information.

For example, assuming the file's extension is .drupal and the desired major-mode is php-mode, you could write

(add-to-list 'auto-mode-alist '("\\.drupal\\'" . php-mode))

Note that in Elisp, file extensions are usually terminated by the pattern \' instead of the more mainstream $. Emacs interprets the former as "end of string" and the latter as "end of line", so using the former avoids the theoretical possibility of failing to match a filename containing newlines. See the Emacs manual node on Regexps for more information on their usage and syntax.

  • I have tested the M-x php-mode RET and it works, but I also want to set it permanently for this file in particular. I have tried this method using // -*- mode: php -*- the file but it doesn't work. Have I got the syntax wrong? // is a valid PHP comment prefix. – vfclists Jun 26 '17 at 15:41
  • 1
    @vfclists Nothing wrong with the syntax AFAICT. Is it on the first line in the file? Did you try rereading the file after adding the mode file-local variable? You can do this without killing the buffer via M-x revert-buffer RET yes RET. If you are not familiar with this command, I recommend you read its documentation (C-h f revert-buffer RET or M-x describe-function RET revert-buffer RET). – Basil Jun 26 '17 at 15:57
  • After putting it on the first line it works. Thanks – vfclists Jun 26 '17 at 16:05
  • @vfclists Glad it worked. See the Emacs Manual node on File Variables for more information on this. – Basil Jun 26 '17 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.