第二基地

June 21, 2006

最新版本smart-operator

Filed under: linux, emacs

(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 ” “))))

2 Comments »

The URI to TrackBack this entry is: http://prometheus.blogsome.com/2006/06/21/new_smart_operator/trackback/

  1. Looks interesting. Do you have a downloadable version? I tried copying and pasting but it’s getting munged somehow.

    Comment by Marc — June 22, 2006 @ 2:31 am

  2. there is an emacs extension named smart-operator,u can google it and get all the .el files,then replace the function in smart-operator.el with my version,my mail:fvwmerATgmailDOTcom,mail me:)

    Comment by prometheus — June 22, 2006 @ 3:25 am

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>























Get free blog up and running in minutes with Blogsome | Theme designs available here