Emacs 没有正确缩进 PHP,尽管 C/C++做?
我在运行 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()
请注意,两个注释和 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 表现得像这样。我做了典型的Cc Cs,在PHP中语法元素也是“语句”,那么为什么在PHP中,emacs使用else中的'e'作为偏移量计算的锚点,但在C/C++中,它使用右大括号 '}' 应该如此吗?我将不胜感激任何提示和/或指示。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题是 else 之后缺少 { 。 Emacs 正在缩进,就好像您在继续 else 一样。添加 { 就可以正常工作了
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