0

我在运行带有最新补丁的 Ubuntu 9.04 的 HP 笔记本上运行 GNU Emacs 22.2.1。

最近,我需要处理一些 PHP 代码,所以我添加了 Ubuntu 的 php-mode 包,认为我的 .emacs 中的内容将涵盖基础知识。嗯,差不多。以下简短片段显示了症状:

<?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()

请注意,两条注释和打印语句都偏移了 6 个空格,而不是我想要的 4 个。我只有一个简单的 .emacs,下面是 C/C++,这是我使用最广泛的两种语言:

[...]
(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)
[...]

令我困惑的是,使用 C 或 C++,emacs 能够根据需要缩进我的代码,例如:

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

我再次查看了 CC 模式手册,但无法弄清楚是什么导致了 emacs 的行为。我做了典型的 Cc Cs 并且语法元素也是 PHP 中的“语句”,那么为什么在 PHP 的情况下,emacs 使用 else 中的 'e' 作为偏移量计算的锚点,但在 C/C++ 中,它使用右括号'}'应该是什么?我将不胜感激任何提示和/或指示。

4

1 回答 1

0

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

于 2009-10-03T15:46:43.687 回答