9

By default, comment-region inserts # for comments in Octave major mode:

# foo

I want to modify the behavior so that it inserts the percent sign

% foo 

I used the following

(setq octave-mode-hook
      (lambda () (setq octave-comment-char ?%) ))
(modify-syntax-entry ?% "<"  octave-mode-syntax-table)

But it gives me this error:

Symbol's value as variable is void: octave-mode-syntax-table

I don't know about elisp so well, so how can I solve this?

Edit

Here is the configuration needed to make octave-mode the default one for .m files and change commenting behaviour to that of MATLAB's. Note that for Octave, both % and # are valid for commenting.

(add-to-list 'auto-mode-alist '("\\.m\\'" . octave-mode))

(add-hook 'octave-mode-hook
    (lambda () (progn (setq octave-comment-char ?%)
                      (setq comment-start "% ")
                      (setq comment-add 0))))

2 Answers 2

11

Inside your hook put this: (setq comment-start "% "). Also, this will explain why:

comment-region is an interactive compiled Lisp function.

(comment-region BEG END &optional ARG)

Comment or uncomment each line in the region. With just C-u prefix arg, uncomment each line in region BEG .. END. Numeric prefix ARG means use ARG comment characters. If ARG is negative, delete that many comment characters instead.

The strings used as comment starts are built from comment-start and comment-padding; the strings used as comment ends are built from comment-end and comment-padding.

By default, the comment-start markers are inserted at the current indentation of the region, and comments are terminated on each line (even for syntaxes in which newline does not end the comment and blank lines do not get comments). This can be changed with comment-style.

2
  • This worked for me. Can you also help me with that syntax-table error? I want to change the commenting behavior so that it inserts only one percent sign instead of two.
    – osolmaz
    Commented Apr 19, 2015 at 21:09
  • 2
    @hos You don't need to make that modification to Octave mode's syntax table. Percents are already recognized as being the comment start symbol. The reason you were getting an error is perhaps because Octave mode wasn't loaded yet when that code ran. If you want a single character to begin a comment, you can similarly add (setq comment-add 0) to the hook.
    – wvxvw
    Commented Apr 20, 2015 at 5:52
3

The octave-comment-char was added specifically for your kind of use case, so all you should need is:

(setq octave-comment-char ?%)

No need to change the syntax table, since % is already recognized as a comment starter. The important detail is that the above setq needs to be executed before octave.el is loaded (so its values is correctly propagated to other vars such as comment-start).

2
  • Was this added recently?
    – osolmaz
    Commented Oct 13, 2018 at 9:31
  • Sorry I was confused, I was thinking about something else
    – osolmaz
    Commented Oct 13, 2018 at 16:59

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.