views:

231

answers:

1

I am running GNU Emacs 22.2.1 on a HP notebook running Ubuntu 9.04 with the latest patches.

Recently, I need to handle some PHP codes, so I added Ubuntu's php-mode package, thinking what I have in my .emacs would cover the basics. Well, almost. The following short snippet shows the symptom:

<?php
# main()
/*
 *
 *
 */
{
    $fpath="/tmp";
    // This is a comment
    if (!file_exists($fpath)) {
        // This is another comment
        print "$fpath doesn't exist on the system!\n";
        exit(1);
    } elseif (!is_dir($fpath)) {
        print "$fpath is not a directory\n";
        exit(2);
    } else
          // I don't know why it doesn't use the } as the anchor position
          // in PHP, but in C and C++?
          print "Found $fpath on the system. Good...\n";
} # end of main()

Notice that both the two comments and the print statement are offset by 6 spaces, rather than 4 as I desired. I have only a simple .emacs, with the following for C/C++, the two languages that I use most extensively:

[...]
(defconst my-c-style
  '((c-comment-only-line-offset . 0)
    (c-hanging-braces-alist     . ((substatement-open after)
                                   (brace-list-open)))
    (c-hanging-colons-alist     . ((member-init-intro before)
                                   (inher-intro)
                                   (case-label after)
                                   (label after)
                                   (access-label after)))
    (c-cleanup-list             . (scope-operator
                                   empty-defun-braces
                                   defun-close-semi))
    (c-offsets-alist            . ((arglist-close . c-lineup-arglist)
                                   (substatement-open . 0)
                                   (case-label        . 4)
                                   (block-open        . 0)
                                   (defun-block-intro . 0)
                                   (statement-block-intro . 4)
                                   (substatement . 4)
                                   (knr-argdecl-intro . -)))
    (c-echo-syntactic-information-p . t)
    )
  "My C Programming Style")

;; Customizations for all of c-mode, c++-mode, and objc-mode
(defun my-c-mode-common-hook ()
  ;; add my personal style and set it for the current buffer
  (c-add-style "PERSONAL" my-c-style t)
  ;; offset customizations not in my-c-style
  (c-set-offset 'defun-block-intro' +)
  ;; other customizations
  ;
  (setq-default indent-tabs-mode nil)

  ;; make sure that comments don't get moved when you do a //
  (c-set-offset 'comment-intro 0)
  ;; keybindings for all supported languages.  We can put these in
  ;; c-mode-base-map because c-mode-map, c++-mode-map, objc-mode-map,
  ;; java-mode-map, and idl-mode-map inherit from it.
  (define-key c-mode-base-map "\C-m" 'newline-and-indent)
  )

(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
(require 'php-mode)
[...]

What's puzzling to me is that with C or C++, emacs is able to indent my codes as desired, e.g:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    [...]
    if ((cond = strcmp(word, mid->word)) < 0) {
        high = mid;
        low = mid - 1;
    } else if (cond > 0) {
        low = mid + 1;
        high = mid + 2;
    } else
        return mid;
}

I reviewed the CC Mode manual again, but couldn't figure out what's causing emacs to behave like so. I did the typical C-c C-s and the syntatical element is "statement" in PHP as well, so why in the case of PHP, emacs uses the 'e' in else as the anchor for offset calculation, but in C/C++, it uses the closing brace '}' as it should? I would appreciate any tips and/or pointers.

A: 

Your problem is a missing { after the else. Emacs is indenting as if you were continuing the else. Add the { and it'll work fine

mabwi
Indeed; bad style + bad language = bad behavior.
jrockway