【问题标题】:Emacs not indenting PHP correctly, although C/C++ do?Emacs 没有正确缩进 PHP,尽管 C/C++ 可以?
【发布时间】:2009-10-02 22:36:03
【问题描述】:

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

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

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

请注意,两个 cmets 和 print 语句都偏移了 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 如此行为的原因。我做了典型的 C-c C-s 并且语法元素也是 PHP 中的“语句”,那么为什么在 PHP 的情况下,emacs 使用 else 中的 'e' 作为偏移量计算的锚点,但在 C/C++ 中,它使用右括号'}'应该是什么?我将不胜感激任何提示和/或指示。

【问题讨论】:

  • 我在发布我的问题一小时后找到了解决此问题的方法: 0. apt-get purge php-mode 1. 从 sf.net 下载并安装最新的 php-mode;字节编译的东西。 2.在.emacs中,添加(c-set-offset 'substatement 2) Ubuntu提供1.0.2,太旧了。所以,它应该被清除 :) 我用 C-c C-s、C-c C-o 做了更多的试验,然后想出了上面的解决方法。它似乎工作。尽管如此,任何其他建议都值得赞赏。
  • 玩了一段时间我的.emacs后,我认为上面的“绕过”是不正确的。我现在认为罪魁祸首是 php-mode 继承自 CC 模式,其中没有这样的构造“elseif”,而是“else if”,我通过使用 C-c C-s 发现了这一点,并观察光标突出显示锚。一旦我将“elseif”修改为“else if”,正常的缩进就起作用了。我想知道一种妥善处理此问题的方法。

标签: php emacs indentation


【解决方案1】:

您的问题是在 else 之后缺少 {。 Emacs 正在缩进,就好像你在继续 else 一样。添加 { 就可以了

【讨论】:

  • 确实;糟糕的风格 + 糟糕的语言 = 糟糕的行为。
  • 我知道所谓的“缺失”{} 对问题。然而,如果严格遵循 K&R 风格,那么 if-else if else 序列中的单个语句永远不会包含在 {} 中。见第“C 编程语言”第 2 版第 56 卷。例如,请参见页面中间的示例。我一直试图弄清楚的是如何使用 PHP 让 php-mode 意识到这一点,即使 elseif 是用一个单词编写的,而不是 else if(即两个单词)。不过感谢您的回答。
猜你喜欢
  • 2010-10-14
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
相关资源
最近更新 更多