1

I'm currently using autoheader.

I have this configuration, which causes the <?php tag to be inserted in other modes:

(defsubst my/header-title ()
  "Insert buffer's first row."

  (insert "<?php \n \n" (concat comment-start  " Foo bar "  (and (= 1 (length comment-start)) header-prefix-string )
                                (if (buffer-file-name)
                                    (file-name-nondirectory (buffer-file-name))
                                  (buffer-name))
                                " " "\n"))
  )  

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

;; To have Emacs add a file header whenever you create a new file in some mode: 
(autoload 'auto-make-header "header2")
(add-hook 'php-mode-hook 'auto-make-header) 
(add-hook 'emacs-lisp-mode-hook 'auto-make-header)
(add-hook 'web-mode-hook 'auto-make-header)
(add-hook 'html-mode-hook 'auto-make-header)
(add-hook 'js-mode-hook 'auto-make-header) 

When I'm creating a new file with the extension .html or .lisp, then the <?php opening tag will be inserted too. I want the <?php opening tag inserted in the header only when the newly created buffer is in PHP-mode. When PHP-mode is not enabled, it should not insert the PHP tag.

I tried to solve this with the following modifications:

(defsubst my/header-title ()
   (insert 
   ;; comparison major mode with the PHP-mode
    (if equal major-mode "PHP-mode" 
    ;; If true, insert this:
      ("<?php \n \n") 
    ;; If false, insert empty string
  " \n")
   (concat[...] rest of the function)))

But it doesn't work. The comparison seems logical, following the Emacs manual. So I couldn't figure it out. So I'm wondering how I could detect if there is PHP-mode enabled, then insert the <?php tag, or otherwise do nothing?

Any answer would be greatly appreciated.

  • First of all, Check your last snippet more carefully, it's full of unbalanced parentheses. – Malabarba May 20 '15 at 21:58
  • Thanks for the mention, I added two parentheses. Still missing an intelligent parentheses balancer, that adds the parentheses for you. Smartparens and Paredit didn't help. – ReneFroger May 20 '15 at 22:15
3

Replace this part

equal major-mode "PHP-mode"

With this expression

(equal major-mode 'php-mode)
  • I would love to try your comment out. I configured it on your way: pastebin.com/V8vDjGEE but I got an error that the function php-tag or empty-tag is void. Any suggestion? – ReneFroger May 21 '15 at 12:53
  • @ReneFrogertjuh yes, replace (php-tag) with php-tag. And replace (" \n") with " \n". – Malabarba May 21 '15 at 13:25
  • It worked now. I always thought you need to pack everything in parenthesese, except strings. Didn't see clearly that php-tag was a variable. Thanks Malabarba, I marked your reply as the answer. – ReneFroger May 22 '15 at 11:14
0

Since auto-make-header contains php related header, this hook must be added only to php as, (add-hook 'php-mode-hook 'auto-make-header). You can remove all the other add-hook statements.

  • I don't get it? I have a custom header. So only the custom header must be activated, not the default auto-header. And the same custom header will be applied on many different modes. – ReneFroger May 20 '15 at 22:17
  • you say, your custom header that adds --- <?php \n \n must be added for many different modes – Madhavan May 20 '15 at 22:20
  • I couldn't figure your answer out. I will read the documentation again, in order if your answer is the helpful one. – ReneFroger May 20 '15 at 22:23

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.