7

I am using the octave major mode for editing my MATLAB code, since it is more convenient than the unsupported MATLAB packages for emacs on the internet.

One downside is whenever I try to auto-indent commented lines, the comment character is carried over to column 32, which makes indenting large blocks of code a bit of a problem.

I should note that I did

(setq comment-start "%")

in order to use the percent sign as the default comment character. Nevertheless, I have the same problem when using #. When I use comment-region, it actually inserts ## at the beginning of the lines, and the lines with ## do not shoot off like the ones with #.

The same question has been asked couple of times as I have found on Google, like this one. But none of them helped me.

I would like to solve this problem so that lines starting with % or # are auto-indented in a correct manner without shooting off to column 32. I am using GNU Emacs 24.5.1.

7

I achieved what I wanted by overriding some definitions from the original major mode. In /lisp/progmodes/octave.el (the file was zipped, I had to unzip it to view the source), I redefined octave-indent-comment which originally goes like this:

(defun octave-indent-comment ()
  "A function for `smie-indent-functions' (which see)."
  (save-excursion
    (back-to-indentation)
    (cond
     ((octave-in-string-or-comment-p) nil)
     ((looking-at-p "\\(\\s<\\)\\1\\{2,\\}")
      0)
     ;; Exclude %{, %} and %!.
     ((and (looking-at-p "\\s<\\(?:[^{}!]\\|$\\)")
           (not (looking-at-p "\\(\\s<\\)\\1")))
      (comment-choose-indent)))))

I removed the last bit and appended to octave-mode-hook in my configuration like so:

(setq octave-mode-hook
      (lambda () (progn (setq octave-comment-char ?%)
                        (setq comment-start "%")
                        (setq indent-tabs-mode t)
                        (setq comment-add 0)
                        (setq tab-width 2)
                        (setq tab-stop-list (number-sequence 2 200 2))
                        (setq octave-block-offset 2)

                        (defun octave-indent-comment ()
                          "A function for `smie-indent-functions' (which see)."
                          (save-excursion
                            (back-to-indentation)
                            (cond
                             ((octave-in-string-or-comment-p) nil)
                             ((looking-at-p "\\(\\s<\\)\\1\\{2,\\}") 0)))))))

which I guess removed all that functionality tied to different number of comment characters. Since that functionality was not desirable to me from the very beginning, I am pleased with this result. Now, the comment lines are always aligned with the preceding lines when I use comment-region.

Edit: I guess it is useful to add this link to my configuration, for the overrides I did to make octave-mode behave more pleasantly while editing MATLAB code.

  • I didn't want to answer my own question, but I solved it myself by luck and didn't think the answer would come from somewhere else. – osolmaz Aug 30 '15 at 12:00
  • 1
    Please do answer your own question, if you provided the answer. This lets people know that the question is not unanswered. On the other hand, if you are still looking for answers then you might want to leave it unaccepted. – Drew Aug 30 '15 at 15:23
6

Try using more than one % or # in a row: %% or %%% etc.

By default, the number of consecutive comment chars determines the kind of comment and its behavior (e.g., with respect to indentation).

See the Elisp manual, node Comment Tips.

  • 1
    Actually, that is the behavior I want to change. In MATLAB's own editor, # and ## have different meanings in terms of documentation, and the people I am working for use that editor, so I have to comply with their standards. – osolmaz Aug 30 '15 at 9:51

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.