4

I am using web-mode.el to edit my CSS and HTML files. I am 90% sure that I haven't touched it but, just in case I modified it in the past, you can find the actual web-mode.el file I'm using here.

I have mapped the shortcut C-cc to comment-region in my ~/.emacs:

(global-set-key "\C-cc" 'comment-region)

This works fine in all other modes but if I try to use it in web-mode I get No comment syntax is defined. Use:. So, presumably, I need to tell web-mode to use <!-- and --> for HTML and /* and */ for CSS. I checked some other mode files and I'm guessing I need something like this for CSS:

(let ((deactivate-mark nil) (comment-start "/*") (comment-end "*/"))

How should I go about editing my web-mode.el to get my comments to work? Is the above line correct and, if so, where should I add it? Presumably, I would need to add the above (or something like it) to a CSS section of the file and the equivalent to an HTML one.

In the meantime, I would also appreciate a pointer explaining how I can give /* and */ or <!-- and --> as answers to the No comment syntax is defined. Use: prompt.

I am aware of the M-; shortcut from Web mode but I would rather have the same shortcut since I am very used to it from the various other modes I use where it works. In any case, I want the comment-region function to work as it does in other modes.

terdon
  • 745
  • 6
  • 21
  • 2
    This is probably an issue with `web-mode` itself, which should support generic commenting commands. Perhaps you can shoot the devs a message, I know they're very active. – PythonNut Jan 12 '15 at 18:50
  • @PythonNut yeah, I may well do that. I kind of assumed it was specific to my case since it is such a basic function I'd be surprised no one else has had issues with it. I can't rule out that I've screwed it up myself somehow. – terdon Jan 12 '15 at 18:55
  • @PythonNut Does not look like an issue of `web-mode` itself. The error comes from an advice (indicator `ad-`). But, the only advice I found in https://raw.githubusercontent.com/fxbois/web-mode/master/web-mode.el is that one of `ac-start` (auto-complete) which does not have any arguments. Looks like this has something to do with some `develock-mode` which I do not know. Terdon should look in his init files for `develock-mode` and start without it. – Tobias Jun 05 '15 at 16:40
  • @Tobias `grep -R develock .emacs.d .emacs` returns nothing. It must be in the site-wide files. – terdon Jun 05 '15 at 16:46
  • Looks like the above code is from `defadvice indent-region` in http://www.jpl.org/ftp/pub/elisp/develock.el.gz. Maybe, you coud try `(ad-unadvice 'indent-region)` to test it. – Tobias Jun 05 '15 at 17:01
  • 1
    @Tobias but that would be for fixing the `M-;` error right? My main objective here is to get `M-x comment-region` and my associated shortcut to work. Also, the error seems to have gone away since the last time I tried this. The main issue is still being able to use `comment-region` in web-mode. Presumably by defining the open and close comment signs somewhere. – terdon Jun 05 '15 at 17:28
  • Yes, this is about the error at `M-;`. If this error is gone away you can ignore the comments. – Tobias Jun 05 '15 at 17:33
  • Have you tried binding your key (`C-c c`) to the command `web-mode` uses for commenting (`web-mode-comment-or-uncomment`)? You could just bind it in the keymap for `web-mode` so other modes work as expected. –  Jan 01 '19 at 12:27

1 Answers1

11

The web-mode shortcut M-; (which calls web-mode-comment-or-uncomment) works for me in HTML, CSS, and PHP files. For the first, this command puts <!-- --> around the region, for the second, /* */, and so on.

I also tried testing both comment-region and comment-dwim with web-mode enabled. Both functions had the same result: <!-- --> was placed around the region, no matter what kind of file was open. Thus, I suggest using the web-mode command.

To make the shortcut more consistent, you could do e.g.:

(defun my-web-mode-hook ()
    (local-set-key (kbd "C-c c") 'web-mode-comment-or-uncomment))

(add-hook 'web-mode-hook 'my-web-mode-hook)

My testing was done with various versions of Emacs and web-mode. (This includes testing with emacs -Q + loading web-mode.) I would also recommend updating to ensure you have a recent version of web-mode.

Also, if you do have changes you want to apply on top of web-mode for your personal use, I would recommend doing so directly from a copy of the Git repo. You can even fork the repo if you want to share your changes with others, and you can use that to open a pull request.

Edit: I have noticed a problem recently with the commenting in web-mode, which causes an empty comment to be added after the region if the point is after the mark. I've reported the issue here, and it has now been fixed in the latest version of web-mode.

Scott Weldon
  • 2,695
  • 1
  • 17
  • 31
  • 1
    Thanks, but I know that shortcut (see the last paragraph of my question). My problem is that i) that seems to work on emacs regions and not simple selections, or at least that's how it is in my setup. I want to be able to highlight a region with the mouse or `C-Space` and then comment it with a shortcut. Also, ii) I want to use the same shortcut as I do in all other modes. The global shortcut I have set works in Perl mode, C mode, shell, and pretty much all others. I see no reason why it shouldn't with web mode as well. – terdon Jan 12 '15 at 23:40
  • Ah, okay, that first part is what I misunderstood. Are you not running Emacs in GUI mode? When I do that, the `region` can be set by the mouse. – Scott Weldon Jan 12 '15 at 23:44
  • Updated my answer to address the latter concern. I meant to add that the first time around, but forgot to. – Scott Weldon Jan 12 '15 at 23:48
  • 1
    No, I'm running it in GUI mode. I tried your workaround (thanks, by the way) but get the same error message. That led me to try running `M-x comment-dwim` with a region selected and I got the "no comment syntax defined" error again! Which is particularly strange given that `M-;` results in an empty `/* */` appearing at the beginning of the line I have selected. Not around the selected text, just at the start of the line. Still, I don't get how the shortcut and manually launching the command can give different results. – terdon Jan 13 '15 at 00:06
  • That is very strange. Have you tried with `emacs -Q` and/or an updated version of `web-mode`? – Scott Weldon Jan 13 '15 at 00:10
  • 1
    Well, with `emacs -Q`, the `M-;` shortcut does indeed work as expected. However, that is not web mode but `html-mode` and `css-mode` and I can't load web mode when using `emacs -Q`. On the bright side, the updated web-mode.el lets me use `M-;` correctly(ish) without `-Q`. It throws an error that pops up and is annoying (see updated question) but at least it lets me comment selected text. – terdon Jan 13 '15 at 00:22
  • Note that to test `web-mode` with `emacs -Q`, you should be able to manually load `web-mode` by doing e.g. `M-x load-library RET /path/to/web-mode.el RET`. – Scott Weldon Jun 05 '15 at 15:52
  • Do you get that error with `emacs -Q`? I can't find such code in `web-mode`, so maybe that is something in your init file? – Scott Weldon Jun 05 '15 at 15:56
  • No I don't. With `emacs -Q` and after loading `webmode` as you suggested in your previous comment, `M-x comment-region` works fine. I also see that the current versions I'm using (I've updated since last time) no longer let me use `M-;` to comment in webmode. The selected region is ignored and a `<-- -->` is added to the start of the line. Just as described in the Q. – terdon Jun 05 '15 at 16:32
  • Does my edit help? That sounds *very* close to an issue that I've been having recently. – Scott Weldon Jun 05 '15 at 21:53