the title of this post is misleading … i only talk about commenting and uncommenting region.
this is a small tip for emacs users who want to edit matlab m-files and octave m-files without installing some major mode for editing matlab files. emacs already ships with a built in gnu octave mode and the true hacker will use free software only. but i m no where near being a true hacker thus i often have to write some matlab stuff. i associate all .m files with the octave mode and i have the following line in my .emacs file to achieve that
(setq auto-mode-alist (cons '("\\.m$" . octave-mode) auto-mode-alist))
the major annoyance is that matlab uses the ‘%‘ as the comment char while gnu octave uses ‘#‘. so when editing m files in emacs i often include the following local file variables
% Local Variables: % comment-start: "%" % comment-column: 0 % End:
at the end of the m-file. these lines are treated as comments by the matlab interpreter due to the ‘%’ prefix but emacs scans the file and reads these lines and now you can highlight text and use M-x comment-region or M-x uncomment-region. after adding the lines either kill the buffer and visit the file again or hit C-x C-v and then choose the same filename again.
there are probably many good features of the matlab editor but i have not used them much apart from the ability to run the current m file by hitting F5 or setting and clearing breakpoints with mouse clicks. i do like the F5 thingy but i m sure a little bit of elisp can solve that problem. i m sure there are some good major modes for editing matlab files or integrating with matlab but i haven’t explored this area at all.
my emacs version is “GNU Emacs 23.4.1 (i686-pc-linux-gnu, GTK+ Version 2.24.10) of 2012-04-12 on shirley.hoetzel.info”
Try M-; (that is, hold down your meta key, probably labelled ‘Alt’, and press ‘;’).
thank you chad for your comment … the trouble is that while M-; works great for me in c-mode and cc-mode but behaves differently in octave-mode. in c-mode/cc-mode when i highlight a region and then hit M-; the region gets commented if not already commented and uncommented if commented i.e. there is a toggling action. if i simply hit M-; then it starts a comment line in c-mode/cc-mode which is all fine and dandy.
in octave-mode when i highlight region and hit M-; it places a “##” at the end of the last line *after* the region i.e. if the region consists of the following 3 lines:
highlight region and then hit M-; then the following is seen
which completely defeats my purpose. which is why i got in to the habbit of M-x comment-region and M-x uncomment-region in octave-mode.
C-h k RET M-; shows the following in octave-mode
C-h k RET M-; shows the following in c-mode as well as in cc-mode
what is M-; bound to in your octave-mode? which distro? and emacs version please
Octave itself (at least the version I’m using, 3.2.4) supports both % and # comments, so it might be easier to just set those variables in a function attached to octave-mode-hook. My Emacs-fu is a little rusty, but something like https://gist.github.com/2783141 should do the job.
thanks tom for your comment and may i say your emacs-fu is spot on and works great
thanks.
an earlier attempt of mine was to put the following in my ~/.emacs
(add-hook 'octave-mode-hook
(lambda()
(setq-default octave-comment-char 37)))
which did not work and i don’t even know why
my gnu octave version is 3.6.1 and it too accepts both % and the # as comment chars.