Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indenting of curly braces #114

Open
midsbie opened this issue Jul 12, 2013 · 12 comments
Open

Indenting of curly braces #114

midsbie opened this issue Jul 12, 2013 · 12 comments
Milestone

Comments

@midsbie
Copy link

midsbie commented Jul 12, 2013

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?

@ejmr
Copy link
Collaborator

ejmr commented Jul 12, 2013

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.

@midsbie
Copy link
Author

midsbie commented Jul 12, 2013

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
Copy link
Author

midsbie commented Jul 12, 2013

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

@ejmr
Copy link
Collaborator

ejmr commented Jul 12, 2013

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

@ejmr
Copy link
Collaborator

ejmr commented Jul 12, 2013

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.

@midsbie
Copy link
Author

midsbie commented Jul 13, 2013

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?

@ejmr
Copy link
Collaborator

ejmr commented Jul 13, 2013

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.

@midsbie
Copy link
Author

midsbie commented Jul 13, 2013

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

Many thanks for the assistance!

@midsbie
Copy link
Author

midsbie commented Jul 13, 2013

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.

@ejmr
Copy link
Collaborator

ejmr commented Jul 13, 2013

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.

@sxediastis
Copy link

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;
}

@ejmr
Copy link
Collaborator

ejmr commented Nov 22, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants