dgutov / mmm-mode

New official home for mmm-mode, fixed for Emacs >= 23
http://mmm-mode.sourceforge.net/
GNU General Public License v2.0
332 stars 30 forks source link

Set Indent Function of Submode #45

Open Abdillah opened 9 years ago

Abdillah commented 9 years ago

How exactly I can set indentation function of sub-mode?

I have tried to

(defun js-embed-autoload ()
 "Embedded Javascript upstart configuration"
 (setq-local indent-line-function 'js-embed-indent-line))

;;{{{ JavaScript in HTML

;; We use two classes here, both for code in a <script> tag, one wrapped in
;; CDATA, another not. And another class to group them together.
;;
;; Usage: (mmm-add-mode-ext-class 'html-mode nil 'html-js)
(mmm-add-group
 'html-js
 '((js-script-cdata
    :submode js-mode
    :face mmm-code-submode-face
    :front "<script[^>]*>[ \t\n]*\\(//\\)?<!\\[CDATA\\[[ \t]*\n?"
    :back "[ \t]*\\(//\\)?]]>[ \t\n]*</script>"
    :creation-hook js-embed-autoload)
   (js-script
    :submode js-mode
    :face mmm-code-submode-face
    :front "<script[^>]*>[ \t]*\n?"
    :back "[ \t]*</script>"
    :insert ((?j js-tag nil @ "<script type=\"text/javascript\">\n"
                 @ "" _ "" @ "\n</script>" @))
    :creation-hook js-embed-autoload)))
(defun js-embed-set-indent-function ()
 "Embedded Javascript upstart configuration"
 (setq-local indent-line-function 'js-embed-indent-line))

(defun js-embed-autoload ()
 "Embedded Javascript upstart configuration"
 (add-hook 'js-mode-hook 'js-embed-set-indent-function))

;;{{{ JavaScript in HTML, same as Above }}}

Any idea?

dgutov commented 9 years ago

You set mmm-indent-line-function in the primary major mode. See mmm-erb.el for an example. That function can look up in which submode point is and delegate appropriately.

As another option, you can create a new major mode deriving from js-mode to use in subregions. That mode's indent-line-function will be the one to change.

Abdillah commented 9 years ago

Great, It works..! I choose to add new mode : js-embed-mode. Something like this,

(define-derived-mode js-embed-mode js-mode "JSed"
  "Javascript embedded mode as `js-mode' extension to be used in mmm-mode (or any mulimode package, if possible). Currently only adjust indentation."
  (set (make-local-variable 'mmm-indent-line-function) 'js-embed-indent-line)
  (set (make-local-variable 'standard-indent) 'js-embed-indent-after-tag))

Thanks.

dgutov commented 9 years ago

In that case, I don't think you need to set mmm-indent-line-function.

Abdillah commented 9 years ago

Hmm, why? (Is it is because js-mode is not primary mode) Then, what should I set?

I figuring out that using mode derived from js-mode in mmm-mode now failed. I will experiment with it more, and so I reopen this..

dgutov commented 9 years ago

Hmm, why? (Is it is because js-mode is not primary mode)

Yes.

Then, what should I set?

indent-line-function, I suppose.