2009-10-02 2 views
0

저는 최신 패치로 우분투 9.04를 실행중인 HP 노트북에서 GNU Emacs 22.2.1을 실행하고 있습니다.이맥스는 C/C++에서 PHP를 올바르게 들여 쓰지 못하나요?

최근에는 일부 PHP 코드를 처리해야하기 때문에 우분투의 php-mode 패키지를 추가하여 .emacs에서 기본 사항을 다룰 수 있다고 생각했습니다. 음, 거의. 내가 원하는대로 두 의견과 인쇄 문 모두 6 개 공간보다는 4에 의해 상쇄되는 것을

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

주의 사항 : 다음의 짧은 조각은 증상을 보여줍니다. 내가 가장 광범위하게 사용하는 두 가지 언어, ++는 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++로, 이맥스 내 코드를 들여 쓰기 수 있다는 것이다 필요에 따라, 예를 들면 :

#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 모드 설명서를 검토하지만, 이맥스과 같이 행동하는 원인을 알아낼 수 없었다. 나는 전형적인 Cc Cs를 만들었고 syntatical 엘리먼트는 PHP에서도 "statement"이다. 그래서 PHP의 경우 emacs는 오프셋 계산을위한 앵커로 'e'를 사용하지만 C/C++에서는 이것을 사용한다. 닫는 중괄호 '}'로해야합니까? 나는 어떤 조언이나 조언을 주셔서 감사합니다.

+0

내 질문을 게시 한 후 한 시간 만에 문제를 해결할 수있는 방법을 찾았습니다. 0. apt-get purge php-mode 1. 최신 PHP 모드를 다운로드하여 설치하십시오. sf.net에서; 바이트 컴파일도. 2. .emacs에서 (c-set-offset 'substatement 2)를 추가하십시오. 우분투는 1.0.2를 제공합니다. 너무 오래되었습니다. 그래서, 그것은 제거되어야합니다 :) 나는 C-c C-s, C-c C-o로 더 많은 시련을 가졌습니다. 작동하는 것 같습니다. 그럼에도 불구하고, 다른 제안은 인정됩니다. – user183394

+0

잠시 동안 .emacs를 가지고 놀고 나면, 위의 "주변을 돌아 다니는"것은 잘못되었다고 생각합니다. 나는 이제 범인은 PHP 모드가 CC 모드에서 상속 받았으며, 'elseif'와 같은 구조는 없지만 'else if'라고하면, Cc Cs를 사용하여이를 발견하고 커서를 강조 표시하여 닻. 'elseif'를 'else if'로 수정하자마자 정상 들여 쓰기가 작동합니다. 이 문제를 제대로 처리하는 방법을 알고 싶습니다. – user183394

답변

0

문제는 다음에 누락되었습니다. Emacs는 당신이 else를 계속하는 것처럼 들여 쓰기를합니다. {그리고 그게 잘 작동 할거야

+0

실제로; 나쁜 스타일 + 나쁜 언어 = 나쁜 행동. – jrockway

+1

"누락 된"{} 쌍 문제에 대해 알고 있습니다. 그럼에도 불구하고, K & R 스타일을 엄격하게 따르는 경우, if-else if else 시퀀스의 단일 명령문은 절대로 {}로 묶이지 않습니다. p. "C 프로그래밍 언어"제 2 판. 예를 들어 페이지 중간의 예를 참조하십시오. 내가 알아 내려고 노력한 것은 elseif가 한 단어로 쓰여지더라도 php-mode가 PHP로 인식되도록 만드는 방법입니다. 그러나 답변 해 주셔서 감사합니다. – user183394

관련 문제