Created
May 15, 2011 02:26
-
-
Save sonota88/972842 to your computer and use it in GitHub Desktop.
block-indent.el
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; block-indent.el | |
;; author: sonota | |
;; license: GPL | |
;; save-excursionとカーソル周辺のブロック(関数など)の自動インデントを組み合わせてみた - 再発明日記 | |
;; http://d.hatena.ne.jp/sonota88/20101218/1292678664 | |
;;;; | |
;; ルールの書式: | |
;; ((modes) ; 関連付けるメジャーモードのリスト | |
;; ブロック先頭に移動する関数 | |
;; ブロック末尾に移動する関数 | |
;; インデントを行う関数 | |
;; 引数に開始・終了位置が必要なら non-nil) | |
;; .emacs で次のようにルールを追加する | |
;; (add-to-list | |
;; 'block-indent:rules | |
;; '((ruby-mode) | |
;; ruby-beginning-of-defun | |
;; nil | |
;; ruby-indent-exp nil)) | |
;; (add-to-list | |
;; 'block-indent:rules | |
;; '((js2-mode) | |
;; beginning-of-defun | |
;; forward-sexp | |
;; indent-region t)) | |
(defvar block-indent:default-rule | |
'(nil | |
backward-paragraph | |
forward-paragraph | |
indent-region t) | |
"デフォルトのルール(ルールが見つからなかったときに使われる)") | |
(defvar block-indent:rules | |
'(((emacs-lisp-mode lisp-interaction-mode) | |
block-indent:beginning-of-defun | |
nil | |
indent-pp-sexp nil)) | |
"ルールのリスト") | |
(defun block-indent--list-depth () | |
(nth 0 (parse-partial-sexp (point-min) (point)))) | |
(defun block-indent--toplevel-open-paren-p () | |
(and (= 0 (block-indent--list-depth)) | |
(= ?\( (char-after)))) | |
(defun block-indent:beginning-of-defun () | |
"すでにトップレベルの開き括弧にいる場合はその前に移動しない" | |
(unless (block-indent--toplevel-open-paren-p) | |
(beginning-of-defun))) | |
(defun block-indent--select-rule () | |
"メジャーモードをもとにルールを選択" | |
(let (rule modes) | |
(setq rule | |
(car | |
(remove-if-not | |
(lambda (x) | |
(memq major-mode (nth 0 x))) | |
block-indent:rules))) | |
(unless rule | |
(setq rule block-indent:default-rule)) | |
rule)) | |
(defun block-indent--sub (rule) | |
(let ((beginning-func (nth 1 rule)) | |
(forward-func (nth 2 rule)) | |
(indent-func (nth 3 rule)) | |
(region-args-p (nth 4 rule)) | |
(beg nil)) | |
(funcall beginning-func) ; ブロックの先頭に移動 | |
(setq beg (point)) ; 先頭位置を保存 | |
(when forward-func | |
(funcall forward-func)) ; ブロック末尾に移動 | |
;; インデント | |
(if region-args-p | |
(funcall indent-func beg (point)) | |
(funcall indent-func)))) | |
(defun block-indent () | |
(interactive) | |
(save-excursion | |
(block-indent--sub (block-indent--select-rule)))) | |
(provide 'block-indent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment