Search This Blog

Wednesday, June 21, 2006

最新版本smart-operator

(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?
(fir-sp t) ;space before op by default
(sec-sp t)) ;space after op by default
(unless (bolp) ;if the point not at the beginning of a line
(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 (or (comint-within-quotes 0 (point)) (and (or (equal op "/") (equal op ">")) (looking-back "^\\#include.*")))
((lambda ()
(setq fir-sp nil)
(setq sec-sp nil)
()))
(if (or (and (equal op "< ") (looking-back "^\\#include.*")) (and (equal op "*") (looking-back "int\\|char\\|float\\|double\\|short\\|long\\|signed\\|unsigned\\|void")))
(setq sec-sp nil)
(if (or (and (equal op "=") op-p) (and (or (equal op "<") (equal op ">") (equal op "+") (equal op "-") (equal op "|") (equal op "&")) (looking-back op)))
(setq fir-sp nil)
())))
(if (or only-back (not fir-sp) (bolp))
(insert op)
(insert (concat " " op)))
(delete-horizontal-space)
(if sec-sp
(insert " "))))


这个版本针对C语言“优化”过了,比如#include之后尖括号老是加空格就比较烦了,还有以前如果先有=再有-就会当成一个运算符,实际上C里没有这么一个运算符,等等
应该还有什么没想到的,到时候再添加

6月22日:我就知道有什么东西忘记了,娃哈哈,这次这个版本对"->"特殊处理,两边都没空格
不过代码是真的越写越丑了:(
(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?
(fir-sp t) ;space before op by default
(sec-sp t)) ;space after op by default
(unless (bolp) ;if the point not at the beginning of a line
(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 (or (comint-within-quotes 0 (point)) (and (or (equal op "/") (equal op ">")) (looking-back "^\\#include.*")))
((lambda ()
(setq fir-sp nil)
(setq sec-sp nil)
()))
(if (or (and (equal op "< ") (looking-back "^\\#include.*")) (and (equal op "*") (looking-back "int\\|char\\|float\\|double\\|short\\|long\\|signed\\|unsigned\\|void")))
(setq sec-sp nil)
(if (or (and (equal op "=") op-p) (and (or (equal op "<") (equal op ">") (equal op "+") (equal op "-") (equal op "|") (equal op "&")) (looking-back op)))
(setq fir-sp nil)
(if (and (equal op ">") (looking-back "-"))
((lambda ()
(setq fir-sp nil)
(setq sec-sp nil)
(backward-char)
(delete-horizontal-space)
(forward-char)
()))))))
(if (or only-back (not fir-sp) (bolp))
(insert op)
(insert (concat " " op)))
(delete-horizontal-space)
(if sec-sp
(insert " "))))

No comments: