I want to implement the functionality, that i wrote about in the title. Since searching the web for existing solutions didn't give me anything useful, i figured i would just try and implement it myself.

I would want to just add a lambda or function to my init.el-File, that executes everytime, it looks at an opening multi-line-comment and inserts that matching closing chars. This is what i have so far:

(add-hook 'c-mode-hook
      (lambda ()
        (if (looking-at "/*")
            (insert "*/"))))

Probably not right at all. I just started with elisp a while ago, so help and explanation will be thankfully appreciated! If there is an existing solution, i also wouldn't hesitate to try that out. Is there an option to enable this in C-Mode, as well as in C++-Mode? Thanks in advance!

  • 1
    Not an answer, but why are you not using M-; to enter comments? – icarus Mar 27 '18 at 20:13
  • When commenting out a larger portion of Code, i would want to do that by adding // per line. Also i'm looking to improve my understanding of elisp-code and i thought developing something for my needs would be a good exercise. Well- but at the moment i'm stuck so i posted this question. – Tim Hilt Mar 27 '18 at 20:34

If you want to use an existing solution then you can look into smartparens which seems to be easily extensible, see the pair management section on their wiki.

As for your code, it's definitely on the right track, but looking-at looks at text after the point, so the insertion function should rather be something along the lines of

(defun close-c-comment ()
  (when (looking-back "/\\*" (- (point) 2))
    (insert "*/")))

(the first argument of looking-back is a regex, so the asterisk needs to be escaped). In addition, you probably want to add this function to the post-self-insert-hook in c-mode (to be automatically activated after insertions), so this part should be something like

(add-hook 'c-mode-hook
  (lambda () 
    (add-hook 'post-self-insert-hook #'close-c-comment)))

Note: The first version of this answer omitted the (optional) (- (point) 2) limit argument from the looking-back function call in the definition of close-c-comment, but -- as npostavs pointed out in his comments -- not specifying a limit for the backward search can seriously slow down the execution of the function, especially in the case of large buffers.

  • Very good, it worked as expected out of the Box! I Just added a space before the closing part and a wrapped it in a save-excursion! – Tim Hilt Mar 28 '18 at 9:25
  • You should give a limit looking-back: (looking-back "/\\*" (- (point) 2)) – npostavs Mar 28 '18 at 12:57
  • @npostavs: Thanks -- I was considering specifying a limit, but thought that since the regex here is just a simple string (which has to match exactly from point) there is probably no noticeable performance penalty if you omit it. Have you experienced performance problems in this type of scenario? – Simka Mar 28 '18 at 13:27
  • ... in general, as its documentation says, it's better to "avoid using looking-back wherever possible, since it is slow." – Simka Mar 28 '18 at 13:49
  • I don't think the complexity of the regex matters that much: Emacs would still search all the way back to the beginning of the buffer in the worst case. But, it's very likely that you will hit the string /* in a c-mode buffer very soon, so in practice you probably won't notice problems even in a massive buffer. – npostavs Mar 28 '18 at 19:02

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.