第二基地

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

February 22, 2006

用emacs上bbs

Filed under: linux, emacs

我一直想着要是在bbs上发文时能像emacs一样编辑,那可太爽了,不过既然bbs不支持emacs的编辑功能,那就用emacs上bbs么
其实很简单,M-x ansi-term,运行后emacs会让你选择程序,默认是bash,然后就会出来一个term,其他的就跟在终端下上bbs一样了
不过发呆时间一长就会掉线,所以可以写个函数防止发呆:
(defvar antiidle)
(defun enable-anti-idle ()
(interactive)
(setq antiidle (run-with-timer 0 180 '(lambda ()
(term-send-up)
(term-send-down)))))

这个函数让emacs每三分钟向term发送一个“上”和一个“下”,然后把run-with-timer返回的值赋给antiidle
(defun disable-anti-idle ()
(interactive)
(cancel-timer antiidle))

这个函数把刚才用run-with-timer“注册”的行为注销掉
这样就可以在需要的时候开启或关闭防止发呆功能,当然,设个快捷键会更方便
另外,在连接主机之前在终端下先运行stty rows 24 cols 80效果会更好
连接上主机以后,默认的是term-char-mode,在这个mode下每按一个键就会向终端发送,所以适合于浏览,如果要发文,那么就会出问题,可以切换到term-line-mode,然后像平时一样编辑,要发送之前再切换回term-char-mode就可以按C-w发文了
term-char-mode绑定到了C-c C-k
term-line-mode绑定到了C-c C-j

October 15, 2005

关于自动换行显示的问题,也就是truncate

Filed under: linux, emacs

我用ecb,edit窗口我一直是non truncate的,但是不知道那些ecb windows和speedbar window怎么才可以自动换行。于是先从.emacs里把控制edit window的truncate的var找了出来,是truncate-lines,然后C-h v ecb- TAB,这样会出来一个complete window,然后跳进去,C-s,找truncate,果然被我找到了,ecb-tree-truncate-lines和ecb-truncate-long-names,把这两个都设置成nil,发现ecb的d,s等窗口都可以自动换行了,但是speedbar还是不行,找了半天似乎也没有对应的变量,于是google之,还是没找着,最后发现,先speedbar-get-focus,然后toggle-truncate-lines就可以让speedbar自动换行,于是写个函数:
(defun my-speedbar-toggle-truncate ()
(interactive)
(speedbar-get-focus)
(toggle-truncate-lines -1)
(speedbar-get-focus))

把my-speedbar-toggle-truncate hook到ecb-activate-hook上就可以让ecb-speedbar自动换行了

October 14, 2005

今天搞明白了一个困扰已久的问题

Filed under: linux, emacs

我一直在纳闷ecb的speedbar为什么就跟死的一样,非得再另开一个speedbar的frame才会“活”。之前我一直理解为ecb-speedbar只是一个躯壳,需要一个真正的speedbar来驱动它工作。直到今天无意间发现一个函数:ecb-speedbar-activate,才明白原来ecb-speedbar是可以独立工作的,害得我以前还老是另开一个frame :P
只要把ecb-speedbar-activate这个函数hook到ecb-activate-hook上就可以了:)如下:
(add-hook 'ecb-activate-hook 'ecb-speedbar-activate)

又改了一下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?
(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 " "))))






















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