1

How can I apply M-; to comments out blocks in matlab-mode? Currently it only adds comments at the end of line.

Thanks!

3 Answers 3

0

I prefer to use a generic "comment/uncomment line or region DWIM" kind of function that works in any major mode.

Generic comment DWIM function

Here is the function I use:

(defun endless/comment-line-or-region (n)
  "Comment or uncomment current line and proceed to the next line.
With positive prefix, apply to N lines including current one.
With negative prefix, apply to -N lines above.
If region is active, apply to active region instead."
  (interactive "p")
  (if (use-region-p)
      (comment-or-uncomment-region
       (region-beginning) (region-end))
    (let ((range
           (list (line-beginning-position)
                 (goto-char (line-end-position n)))))
      (comment-or-uncomment-region
       (apply #'min range)
       (apply #'max range)))
    (forward-line 1)
    (back-to-indentation)))

Tweaking the bindings for matlab-mode

;; Unbind the M-; key from `matlab-mode-map'
;; `with-eval-after-load' will work in emacs 24.4 and newer versions
(with-eval-after-load 'matlab
  (define-key matlab-mode-map (kbd "M-;") nil)) 

;; For emacs 24.3 and older versions use `eval-after-load' instead
;; of `with-eval-after-load'. Note that `eval-after-load' accepts
;; only a single quoted form.
;; (eval-after-load 'matlab
;;   '(define-key matlab-mode-map (kbd "M-;") nil)) 

;; Define the M-; binding globally
;; This binding will be effective in any major-mode that does not
;; have that major-mode-specific M-; binding
(global-set-key (kbd "M-;") #'endless/comment-line-or-region) 

Source

4
  • Thank you for your answer. Unfortunately, this doesn't work. It behaves the same. I also tried it in python files, and they behave the same. Nov 3, 2015 at 0:25
  • You need to save the above pieces of code somewhere in your emacs config and then restart emacs. If it doesn't work even after that, report on what you see on doing C-h k M-; while in the *scratch* buffer and while in a buffer with matlab-mode. Nov 3, 2015 at 0:34
  • I missed it before: When emacs loads it reports: Symbol's function definition is void: with-eval-after-load Nov 3, 2015 at 0:51
  • @user2476373 You are probably using emacs 24.3 or an older version. with-eval-after-load was introduced in emacs 24.4. See if you can upgrade the emacs version on your machine (latest as of today is version 24.5). If not, I have updated the solution with an alternative (commented out) to use eval-after-load instead of with-eval-after-load for emacs 24.3 or older versions. Nov 3, 2015 at 4:02
0

AFAIK, all you should need is:

(add-hook 'matlab-mode-hook (lamba () (local-set-key "\M-;" nil)))

so as to stop matlab-mode from overriding the default M-; binding. And please report this problem to the matlab-mode authors so they can fix it.

0

https://github.com/redguardtoo/evil-nerd-commenter

supports even emacs23.4, all you need is just one line setup:

(evilnc-default-hotkeys)

There many commands you can use, but if you want to comment out blocks separated by empty line, you just need M-x evilnc-comment-or-uncomment-paragraphs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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