1

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:

enter image description here

after:

enter image description here

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 C-u 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:

enter image description here

...which seems right to me.

Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • http://emacswiki.org/emacs/PhpMode has a patch for this bug. – tripleee May 25 '12 at 18:13
  • hmm, ok, that page is pretty large and I did not see anything in particular that looked like it described the problem I'm having here. Also did not see anything like an explicit patch on that page, though there are pointers to other derived modes that fix various things. But those various things are not my specific problem . In any case I found something that allows me to proceed, and that is just setting the `comment-use-syntax` variable. Might not work for everyone but it worked for me. http://stackoverflow.com/a/10759293/48082 – Cheeso May 25 '12 at 19:12
  • Did you, like, say, search for "patch" on that page, for example? "http://php-mode.sourceforge.net – maintained by AaronHawley that supports Emacs 22 (CcMode 5.31). By default, in GNU Emacs 23.2.1, // comments are not formatted correctly by M-q. The problem is a bug in c-mode, which php-mode 1.5.0 depends on. A patch is available and works, but it has to be applied manually: Line numbers don’t match." – tripleee May 25 '12 at 19:17
  • I saw that. But that's not the problem I described here. That one has to do with fill-paragraph on comments (`M-q`). The problem I described here is different. – Cheeso May 25 '12 at 19:20
  • Sorry about that. My apologies for jumping on this. – tripleee May 25 '12 at 19:22

1 Answers1

3

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 # .

Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • Thanks, it worked! I ran everything but the modify-syntax-entry and c-default-style and offset parts on emacs 24.3.1 with php-mode-20130920.1850 – sp3ctum Nov 07 '13 at 09:25