php-mode icon indicating copy to clipboard operation
php-mode copied to clipboard

Indenting of curly braces

Open midsbie opened this issue 12 years ago • 12 comments

I can't seem to get php-mode to indent curly braces correctly. For instance, if the intended result is:

if(expression)
{
  statement;
}

... I always get:

if(expression)
  {                        // badly indented
    statement;
  }                        // badly indented

This with c-default-style set to linux and c-basic-offset to 2.

I've had a look in the php-mode group searching for any customizations that might affect brace indentation but didn't find any.

This can't be a bug: what am I missing?

midsbie avatar Jul 12 '13 13:07 midsbie

You can customize php-mode-coding-style to affect indentation by choosing one of four styles specifically meant for PHP. However, you should not need to do this to achieve what you want. Personally I also use the linux style when writing PHP.

Would you mind please typing your example in a file and then pasting the full output of C-h v c-default-style here? Also php-mode-version-number and php-mode-modified please.

ejmr avatar Jul 12 '13 13:07 ejmr

Thanks for your reply, @ejmr.

Here's the requested output:

c-default-style = "linux" php-mode-version-number = "1.10" php-mode-modified = "2013-04-24"

Forgot to mention that I get exactly the same result when starting emacs without a site file (-Q option) so it doesn't seem to be a conflict with other components.

midsbie avatar Jul 12 '13 14:07 midsbie

Just tried it with the latest commit from your repository and get exactly the same result (build 2013-06-09).

midsbie avatar Jul 12 '13 14:07 midsbie

Thank you for the information. I am going to spend some time today trying to fix this.

ejmr avatar Jul 12 '13 14:07 ejmr

How about your Emacs version from C-h C-a?

I am using GNU Emacs 24.3.50.1 (i686-pc-linux-gnu, GTK+ Version 2.24.17). If I start it via emacs -Q and only load PHP Mode I get the desired indentation, although I do have to use C-c C-. to forcibly set the style to linux since that is something my dot-emacs file does for me. Without doing that the indentation uses the gnu style similar to your second example.

ejmr avatar Jul 12 '13 15:07 ejmr

Emacs version I'm using is:

GNU Emacs 23.4.1 (x86_64-pc-linux-gnu, GTK+ Version 2.24.12) of 2012-09-22 on allspice, modified by Debian

But your tip on using C-c . to forcibly set the coding style is what solved it for me. Once the intended style was set, indentation was no longer an issue - it worked fine.

What do you think php-mode is doing wrong?

midsbie avatar Jul 13 '13 10:07 midsbie

Off the top of my head I am not sure what PHP Mode may be doing wrong. We define various CC Mode styles specifically for PHP so I am going to double-check the code for them to make sure they are not stomping over user settings. In the mean time you could use this in your dot-emacs file:

(add-hook 'php-mode-hook (lambda () (c-set-style "linux")))

This is what I use and it's never caused me any problems. But I'm still going to double-check all of the PHP Mode changes related to c-set-style and friends.

ejmr avatar Jul 13 '13 10:07 ejmr

Ok, Eric. Wish I could help tracking down this bug.

Many thanks for the assistance!

midsbie avatar Jul 13 '13 10:07 midsbie

Oh. Just been hacking on php-mode.el and found what's causing the change in coding style: the value in php-mode-coding-style is what causes a style to be forcibly set.

In my case its value is set to pear so upon initialization of php-mode's derived minor mode, the following conditional block is executed and the current coding style overriden:

  (cond ((eq php-mode-coding-style 'pear)
         (php-enable-pear-coding-style)
         (run-hooks 'php-mode-pear-hook))
        ((eq php-mode-coding-style 'drupal)
         (php-enable-drupal-coding-style)
         (run-hooks 'php-mode-drupal-hook))
        ((eq php-mode-coding-style 'wordpress)
         (php-enable-wordpress-coding-style)
         (run-hooks 'php-mode-wordpress-hook))
        ((eq php-mode-coding-style 'symfony2)
         (php-enable-symfony2-coding-style)
         (run-hooks 'php-mode-symfony2-hook)))

Simply setting php-mode-coding-style to nil should prevent any style from being set when php-mode is initialized, which means it will use whatever settings are in effect.

midsbie avatar Jul 13 '13 10:07 midsbie

Nice find. That is the problem. I never noticed it since the hook I use from the post above always runs after that cond. I am going to have to spend a little time thinking about what would be the best way to correct this issue. At the very least I need to describe this situation in the README. I'll come back and close the issue once I decide what would be best to do.

Thanks for both finding the bug and the solution.

ejmr avatar Jul 13 '13 10:07 ejmr

Setting php-mode-coding-style to nil didn't work for me. My settings:


(setq c-default-style "linux"
      c-basic-offset 4)
(setq-default c-basic-indent 4)
(setq-default tab-width 4)
(setq standard-indent 4)
(setq-default indent-tabs-mode nil)
(require 'php-mode)
(setq-default php-mode-coding-style nil)

This

if (test)
{
    exit;
}

becomes this (i.e.no identation)

if (test)
{
exit;
}

sxediastis avatar Nov 19 '13 02:11 sxediastis

Thanks for the information. Sorry about the slow response, I have simply been busy with other things. But I still intend to have a new version of PHP Mode out soon and will work to fix this for that release.

ejmr avatar Nov 22 '13 04:11 ejmr