简体   繁体   中英

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

I have emacs 23.3.1, running on windows.

php-mode 1.5 from http://php-mode.sourceforge.net/ , Modified: 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.

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 .

I'll see if I can figure this out, but is there a setting I am missing in php-mode?

Anyone know?


MORE

I didn't put anything in my php-mode-hook function to change the comment-start and comment-end variables. When I debug comment-region I can see they get set to the mismatched pair of // and */ somehow. 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. 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. .

MORE2

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.

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. In the php buffer I was editing, the variable was set to nil. I'm guessing it was set by 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.

I should say that I only use C-style comments in php; I don't use the # .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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