Emacs custom indentation

旧街凉风 提交于 2019-12-05 01:21:41

Derive a new mode from text-mode or something and create your own indentation function. I know it's easier said than done, so this might be close enough:

(define-derived-mode foo-mode text-mode "Foo"
  "Mode for editing some kind of config files."
  (make-local-variable 'foo-indent-offset)
  (set (make-local-variable 'indent-line-function) 'foo-indent-line))

(defvar foo-indent-offset 4
  "*Indentation offset for `foo-mode'.")

(defun foo-indent-line ()
  "Indent current line for `foo-mode'."
  (interactive)
  (let ((indent-col 0))
    (save-excursion
      (beginning-of-line)
      (condition-case nil
          (while t
            (backward-up-list 1)
            (when (looking-at "[[{]")
              (setq indent-col (+ indent-col foo-indent-offset))))
        (error nil)))
    (save-excursion
      (back-to-indentation)
      (when (and (looking-at "[]}]") (>= indent-col foo-indent-offset))
        (setq indent-col (- indent-col foo-indent-offset))))
    (indent-line-to indent-col)))

Open your file and do M-x foo-mode

It looks to me as though javascript-mode would do the right thing with your sample. It might not be perfect, but a lot easier than writing your own indentation mode.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!