1

When I try to comment a block of code using comment-region in a file type emacs doesn't know the proper comment type for, then it prompts me No comment syntax is defined. Use: so I can specify the comment character and it works.

However this only happens on modern versions of emacs. In older version that I have to use on our servers, e.g. 21.4.1 It doesn't prompt me for a character. It only says No comment syntax is defined.

Right now my solution to this is that when I see this message, I run my own function e.g.

M-x setcomchar RET 
# RET

and this sets the comment character to #, or whatever else I want (90% of the time it will be #, but could also be //, -- etc) My function is as follows:

(defun setcomchar (cc)
  "Sets the default comment characted in the current mode"
  (interactive "sCharacter::")
  (setq comment-start cc)
)

Once I've set this, I can run comment-region again and it works.

However, what I would like to achieve is effectively to replicate the behaviour seen in newer versions of emacs. Whereby if no comment is set or known about then it automatically prompts me for one. And in fact, once I enter it, it automatically does the commenting for me without a need for another M-x comment-region.

So

I do M-x comment-region, if there is a comment set then apply it. Otherwise prompt me for a comment, (it would be nice for this to be pre-filled with the "#" character as it will be this one 90% of the time), then apply it without any further M-x ing from me. Any thoughts on how to proceed with this one?

Also as a general point, how can I detect the status of a function in emacs, e.g. is there a concept of return code for something like comment-region. And since comment-region returns the string No comment syntax is defined, how can I get access to this string?

Thanks a lot

Joey

2

You can write a wrapper for comment-region to check for and existing comment-start and set it if there is none.

(defun my-comment-region (beg end arg)
  (interactive "r\nP")
  (unless comment-start
    (setq comment-start (read-string "Specify a comment starter: ")))
  (comment-region beg end arg))

I can't test this on emacs 21 but I assume the functions used have not changed very much.

Side note: you said "our servers" so I assume you have some control of them, the version you are running was released a decade ago, your best solution is to update emacs if possible.

  • 1
    Great, thanks! I slightly modified the read-string line making it read-string "Specify a comment starter: " nil nil "#") so it picks up # by default if I don't enter one. Thanks a lot :) – Joey O Mar 13 '15 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.