tags:

views:

73

answers:

1

I have a emacs mode hook code

(defun php-mode-hook ()
  (setq tab-width 4
        c-basic-offset 4
        c-hanging-comment-ender-p nil
        indent-tabs-mode
          (not
            (and (string-match "/\\(PEAR\\|pear\\)/" (buffer-file-name))
              (string-match "\.php$" (buffer-file-name))))))

I need to ensure to call this function whenever i open a php file within emacs.. i have installed php-mode for emacs as well as added this code in .emacs file but it seems not to be working.. can anyone tell me how to add such customization code for emacs?

NOTE: I have recently migrated to emacs.. please be more descriptive while answering.. :)

Updated code1

(add-hook 'php-mode-hook
   '(lambda()
     (setq tab-width 4
      c-basic-offset 4
      c-hanging-comment-ender-p nil
      indent-tabs-mode
      (not
       (and (string-match "/\\(PEAR\\|pear\\)/" (buffer-file-name))
            (string-match "\.php$" (buffer-file-name)))))))
A: 

Adding hooks to places provided by various modes usually works using the add-hook function. You defined a function with the name of the hook you wanted to use. Instead, you should define a function with another name, add add-hook that to the php-mode-hook:

(defun my-php-settings ()
  ...)

(add-hook 'php-mode-hook 'my-php-settings)

In fact, you don't even need to create a named function at all:

(add-hook 'php-mode-hook
          (lambda ()
            (setq tab-width 4 ...)))
rafl
hi rafl! thanks for your reply! i have updated the code with the guidelines you told.. still it seems not to be working. am i going wrong somewhere? i am asked to add the above code to emacs so to follow PHP PEAR Guidelines, i have not coded it.. nor do i know lisp. :|
Idlecool
First off, there's no need to quote that lambda. Have a look at the example I gave you again. Also, how does it not work? Does it sit on the couch all day? Does it make faces at you? ;-) If you're not getting any error message you might first want to check if your hook is executed at all. You can do that by making it execute `(message "hook called")` and check for appearances of that in the `*Messages*` buffer.
rafl
yeah! hook is getting executed.. but i cannot see the expected behavior from the hook.. when i open a php file and issue tab then tab-width is 0 while it is expected to be 4
Idlecool
Although you needn't have a named function, it's a really good idea to have one while you are experimenting, as that way you only need to re-evaluate the function definition when you change your code. With the lambda expression, you would need to `remove-hook` on the original expression, then modify the code, and `add-hook` again with the new one (or otherwise risk potential issues by executing all previous revisions of your hook code as well).
phils