Search This Blog

Wednesday, October 12, 2005

一个elisp函数,smart-operator

这是从一个smart-operator.el里翻出来的,我把它改了一下,更符合我的习惯
原来的smart-insert-operator函数对输入的smart-operator-alist里的运算符都会自动在两边加上空格,但是我不喜 欢->两边有空格,因为我觉得这么一个东西:struct->somevar本身应该是一个变量,不应该在中间加上空格,还有"!"我也不喜 欢它加上空格,还有",",".",哦,对了,还有"*"当指针用的时候,比如"int *p",我也不喜欢"*"和"p"之间有空格,所以输入时往前搜索,如果发现"int""char"之类的关键字,后面就不加空格。OK,就这些了
(defvar smart-operator-alist
'( "=" "< " ">" "%" "\+" "-" "\*" "/" "&" "|" ":" "\?"))


(defun smart-insert-operator (op &optional only-back)
"Automatically insert whitespaces before and after '=', '>=', etc.
Make it look nicer: ' = ', ' >= '.


OP is the operator. If optional ONLY-BACK is t, only insert one
whitespace at back. When confused, try C-q."
(interactive "s")
(delete-horizontal-space)
(let ((op-p nil) ; another op at front?
(one-char-back nil) ; have we stepped back one char?
(sec-sp nil)
(no-fir-sp nil))
(unless (bolp)
(unless (looking-back "int\|char\|float\|double\|short\|long\|signed\|unsighned\|void\|\(")
(setq sec-sp t))
(if (looking-back "\(")
(setq no-fir-sp t))
(backward-char)
(setq one-char-back t))
(setq op-p
(catch 'return
(dolist (front smart-operator-alist)
(when (looking-at front)
(throw 'return t)))))
(when (and (or op-p (not (and (bolp) (eolp))))
one-char-back)
(forward-char))
(if (equal op ">")
((lambda ()
(while (looking-back " ")
(backward-char))
(if (looking-back "-")
((lambda ()
(setq sec-sp nil)
(setq no-fir-sp t)
(backward-char)
(delete-horizontal-space)
(forward-char)
(delete-horizontal-space)
())))))
())
(if (or op-p only-back no-fir-sp (bolp))
(insert op)
(insert (concat " " op)))
(delete-horizontal-space)
(if sec-sp
(insert " "))))
(defun smart-insert-operator-hook ()
(local-set-key (kbd "=") (lambda () (interactive) (smart-insert-operator "=")))
(local-set-key (kbd "+") (lambda () (interactive) (smart-insert-operator "+")))
(local-set-key (kbd "-") (lambda () (interactive) (smart-insert-operator "-")))
(local-set-key (kbd "/") (lambda () (interactive) (smart-insert-operator "/")))
(local-set-key (kbd "%") (lambda () (interactive) (smart-insert-operator "%")))
(local-set-key (kbd "&") (lambda () (interactive) (smart-insert-operator "&")))
(local-set-key (kbd "*") (lambda () (interactive) (smart-insert-operator "*")))
(local-set-key (kbd "!") (lambda () (interactive) (smart-insert-operator "!")))
(local-set-key (kbd "|") (lambda () (interactive) (smart-insert-operator "|")))
(local-set-key (kbd "< ") (lambda () (interactive) (smart-insert-operator "<")))
(local-set-key (kbd ">") (lambda () (interactive) (smart-insert-operator ">")))
(local-set-key (kbd ",") (lambda () (interactive) (smart-insert-operator "," t)))
(local-set-key (kbd ".") (lambda () (interactive) (smart-insert-operator "." t)))
(local-set-key (kbd ":") (lambda () (interactive) (smart-insert-operator ":")))
(local-set-key (kbd "?") (lambda () (interactive) (smart-insert-operator "?"))))


(defun my-c-mode-common-hook()
(smart-insert-operator-hook)
(local-unset-key (kbd "."))
(local-unset-key (kbd "!"))
(local-unset-key (kbd ",")))


把my-c-mode-common-hook hook到你需要这个功能的mode上就可以了,enjoy:)

No comments: