Search This Blog

Friday, October 14, 2005

又改了一下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?
(sec-sp nil)
(no-fir-sp nil))
(unless (bolp)
(unless (and (equal op "*") (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 (and (equal op "%") (comint-within-quotes 0 (point)))
((lambda ()
(setq sec-sp nil)
(setq no-fir-sp t)
())))
(if (or op-p only-back no-fir-sp (bolp))
(insert op)
(insert (concat " " op)))
(delete-horizontal-space)
(if sec-sp
(insert " "))))

No comments: