简体   繁体   English

如何配置emacs以正确注释PHP模式中的代码?

[英]How to configure emacs to properly comment code in php-mode?

I have emacs 23.3.1, running on windows. 我有emacs 23.3.1,在Windows上运行。

php-mode 1.5 from http://php-mode.sourceforge.net/ , Modified: 2008-11-04 php-mode 1.5来自http://php-mode.sourceforge.net/ ,修改时间:2008-11-04

I think this used to work, but now, when I try to comment out a block of code, using comment-region, which is an interactive compiled Lisp function in `newcomment.el', I get poor results. 认为这曾经有用,但是现在,当我尝试使用comment-region注释掉一段代码时,这是一个在`newcomment.el'中的交互式编译Lisp函数,我的结果很差。

Example: 例:

before: 之前:

在此输入图像描述

after: 后:

在此输入图像描述

You can see that each line in the commented block has the single-line comment start sequence // , and the multi-line comment-end sequence */ . 您可以看到注释块中的每一行都有单行注释开始序列//和多行注释结束序列*/

This is not a huge problem, though it is ugly. 这不是一个大问题,虽然它很难看。 The problem comes in when I try to uncomment the block. 当我尝试取消注释块时出现问题。 I get an error, "Can't find comment end". 我收到一个错误,“找不到评论结束”。 In other words, comment-region is not reversible with Cu comment-region . 换句话说, comment-regionCu comment-region不可逆。

I'll see if I can figure this out, but is there a setting I am missing in php-mode? 我会看看我是否可以解决这个问题,但有没有我在php模式中缺少的设置?

Anyone know? 谁知道?


MORE 更多

I didn't put anything in my php-mode-hook function to change the comment-start and comment-end variables. 我没有在php-mode-hook函数中添加任何内容来更改comment-startcomment-end变量。 When I debug comment-region I can see they get set to the mismatched pair of // and */ somehow. 当我调试comment-region我可以看到它们被设置为不匹配的//*/某种方式。 That explains the weird results of comment-region. 这解释了评论区域的奇怪结果。 I don't believe it's my code that does sets those variables like that. 我不相信这是我的代码确实设置那样的变量。

I tried setting them explicitly in my hook to // and (empty string). 我尝试在我的钩子中显式设置它们//和(空字符串)。 In that case, the comment-region looks prettier but it still does not uncomment. 在这种情况下, comment-region看起来更漂亮,但仍然没有取消评论。 I also tried the matched pair of /* and */ , but that gave the same results. 我也尝试了匹配的/**/ ,但结果相同。 Uncomment does not succeed; 取消注释不成功; the error is can't find comment end. 错误是can't find comment end. .

MORE2 更多2

I think my syntax table is correct. 我认为我的语法表是正确的。 It shows this: 它显示了这个:

在此输入图像描述

...which seems right to me. ......对我来说似乎是对的。

This solved it for me: 这解决了我:

(setq comment-use-syntax t)

I put that in my php-mode hook. 我把它放在我的php模式钩子里。

Not sure if this was necessary, but I als included statements to modify the syntax table. 不确定这是否必要,但我也包含了修改语法表的语句。 The entire hook looks like this: 整个钩子看起来像这样:

(defun cheeso-php-mode-fn ()
  "Function to run when php-mode is initialized for a buffer."
  (require 'flymake)
  (flymake-mode 1)

  (setq c-default-style "bsd"
      c-basic-offset 2)

  ;; not sure if necessary or not.
  (modify-syntax-entry ?/ ". 124b" php-mode-syntax-table)
  (modify-syntax-entry ?* ". 23" php-mode-syntax-table)
  (modify-syntax-entry ?\n "> b"  php-mode-syntax-table)
  (modify-syntax-entry ?\^m "> b" php-mode-syntax-table)

  (setq comment-multi-line nil ;; maybe
        comment-start "// "
        comment-end ""
        comment-style 'indent
        comment-use-syntax t))

The help statement for comment-use-syntax says that major modes should set it. comment-use-syntax的帮助声明说主要模式应该设置它。 In the php buffer I was editing, the variable was set to nil. 在我编辑的php缓冲区中,变量设置为nil。 I'm guessing it was set by php-mode; 我猜它是由php-mode设置的; in any case it was not set by me. 无论如何,它不是由我设定的。 Setting this to t and insuring that the syntax table had the appropriate values did the trick. 将其设置为t并确保语法表具有适当的值就可以了。

I should say that I only use C-style comments in php; 我应该说我只在PHP中使用C风格的评论; I don't use the # . 我不使用#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM