All-in-one examine command of CVS/SVN/Git in Emacs

December 21st, 2010 wlamos 1,037 views No comments
* All-in-one examine command of CVS/SVN/Git in Emacs
公司在用CVS,自己平时用Git,偶尔还要用SVN,cvs的examine我bind到了C-x v e上,
也用习惯了,就想何不写个统一的command来调用
cvs-examine/svn-status/magit-status。想法比较简单:
- 查看当前目录下有没有CVS/, 如果有则call cvs-examine;
- 查看当前目录下有没有.svn/, 如果有则call svn-status;
- 查看当前目录是否在git repo中,如果是则call magit-status
- 上面条件都不成立,则让用户选择要使用的vcs tool与目录 (用one-key实现)
- 如果有prefix arg,则直接让用户选择,不自动检测
于是有了下面的简单command:
<pre LANG=”lisp”>
(require ‘psvn)
(require ‘magit)
(require ‘one-key)
(defvar one-key-vcs-alist nil
“`One-Key’ menu list for VCS.”)
(setq one-key-vcs-alist
‘(((“c” . “CVS”) . cvs-examine)
((“s” . “SVN”) . svn-examine)
((“g” . “Git”) . magit-status)))
(defun one-key-menu-vcs ()
“`One-Key’ menu for VCS”
(interactive)
(one-key-menu “EMMS” one-key-vcs-alist t))
(defun zwl-vcs-examine ()
“Examine directory using a VCS (Version Control System) tool.
If current directory contains CVS/, using cvs;
If current directory contains .svn/, using svn;
If current directory is in magit directory, using magit;
otherwise pop a one-key and let user choose which command will be used.”
(interactive)
(if current-prefix-arg
(call-interactively ‘one-key-menu-vcs)
(cond
((file-exists-p “CVS/”) (call-interactively ‘cvs-examine))
((file-exists-p “.svn/”) (call-interactively ‘svn-examine))
((magit-get-top-dir default-directory) (call-interactively ‘magit-status))
(t (call-interactively ‘one-key-menu-vcs)))))
(define-key global-map “\C-xve” ‘zwl-vcs-examine)
</pre>
- “C-x v e “, 自动检测所需要的vcs工具
- “C-u C-x v e”, 让用户选择所需要的vcs工具
最后推荐一下One-Key, 很好用的工具。All-in-one examine command of CVS/SVN/Git in Emacs

公司在用CVS,自己平时用Git,偶尔还要用SVN,cvs的examine我bind到了C-x v e上,也用习惯了,就想何不写个统一的command来调用  cvs-examine/svn-status/magit-status。想法比较简单:

  • 查看当前目录下有没有CVS/, 如果有则call cvs-examine;
  • 查看当前目录下有没有.svn/, 如果有则call svn-status;
  • 查看当前目录是否在git repo中,如果是则call magit-status
  • 上面条件都不成立,则让用户选择要使用的vcs tool与目录 (用OneKey实现)
  • 如果有prefix arg,则直接让用户选择,不自动检测

于是有了下面的简单command:

(require 'psvn)
 
(require 'magit)
 
(require 'one-key)
 
(defvar one-key-vcs-alist nil
 
  "`One-Key' menu list for VCS.")
 
(setq one-key-vcs-alist
 
  '((("c" . "CVS") . cvs-examine)
 
    (("s" . "SVN") . svn-examine)
 
    (("g" . "Git") . magit-status)))
 
(defun one-key-menu-vcs ()
 
  "`One-Key' menu for VCS"
 
  (interactive)
 
  (one-key-menu "EMMS" one-key-vcs-alist t))
 
(defun zwl-vcs-examine ()
 
  "Examine directory using a VCS (Version Control System) tool.
 
If current directory contains CVS/, using cvs;
 
If current directory contains .svn/, using svn;
 
If current directory is in magit directory, using magit;
 
otherwise pop a one-key and let user choose which command will be used."
 
  (interactive)
 
  (if current-prefix-arg
 
    (call-interactively 'one-key-menu-vcs)
 
   (cond
 
    ((file-exists-p "CVS/") (call-interactively 'cvs-examine))
 
     ((file-exists-p ".svn/") (call-interactively 'svn-examine))
 
     ((magit-get-top-dir default-directory) (call-interactively 'magit-status))
 
     (t (call-interactively 'one-key-menu-vcs)))))
 
(define-key global-map "\C-xve" 'zwl-vcs-examine)
  • “C-x v e “, 自动检测所需要的vcs工具
  • “C-u C-x v e”, 让用户选择所需要的vcs工具

最后推荐一下OneKey, 很好用的工具。

Categories: emacs Tags: , , , , ,

Emacs中自动更改脚本文件为可执行

August 18th, 2010 wlamos 1,040 views No comments

工作中要写一些脚本,一般写完后,要chmod +x,十分不方便,在Emacs中利用after-save-hook,可以很容易的实现在保存文件时,自动将脚本文件的属性修改。这里有各种实现的讨论,我个人比较喜欢下面的方式,简单明了。

(add-hook 'after-save-hook
	  #'(lambda ()
	      (and (save-excursion
		     (save-restriction
		       (widen)
		       (goto-char (point-min))
		       (save-match-data
			 (looking-at "^#!"))))
		   (not (file-executable-p buffer-file-name))
		   (shell-command (concat "chmod u+x " buffer-file-name))
		   (message
		    (concat "Saved as script: " buffer-file-name)))))

在使用Emacs的过程中,非常重要的一点就是对重复性工作的敏感性,即要抓住那些琐碎你要重复性去做的事情,然后考虑一下是否可以有更好的方式让机器来自动完成这件事情(写几个函数,更改一下配置?)这种敏感跟代码重构时对重复代码的敏感是一样的。在工作中任何能够提高工作效率的努力都是值得的

Categories: emacs Tags: ,

在已有的Gnus配置下,利用getmail收取pop邮件

July 29th, 2010 wlamos 1,463 views No comments

利用Gnus读mail,上新闻组是一件比较舒服的事情。如果直接用Gnus去收,容易出现假死现象,做为Gmail的重度用户,我采用的是Gnus+offlineimap+dovecot+leafnode的方式,将mail抓到本地,完美解决假死问题,关于offlineimap+dovecot的设置,可参考这里,关于leafnode的安装与设置可参考这里

对于不提供imap收信方式的mailbox,我一般采用gmail将该mailbox中的mail收到gmail,然后用Gnus阅读,回复。但是gmail收取其他帐户的mail时间间隔有些长,并不能够做到实时收取,而且ms这个间隔还不能设置。在这种情况下,使用getmail或fetchmail将pop帐户的mail直接收到本地用Gnus阅读是一种解决方式。

我采用的是getmail,然后用nnmaildir指定mail source的方式。

.gnus配置只需要修改gnus-secondary-select-methods,加入nnmaildir的配置,比如我的在修改前的配置为:

(setq gnus-secondary-select-methods
         '((nntp "localhost") ; Using Leafnode to fetch the news to localhost
           ))

修改后为:

(setq gnus-secondary-select-methods
      '((nntp "localhost") ; Using Leafnode to fetch the news to localhost
	(nnmaildir ""	   ; nnmail directory using fetchmail
		   (directory "~/.nnmaildir"))
	))

可以看到nnmaildir的路径为~/.nnmaildir, 如果此目录不存在,创建之:

$cd
$mkdir .nnmaildir

这个时候,打开Gnus, M-x gnus, 在group buffer中用 G m 创建 Group name 为 work,From method为nnmaildir, gnus会在~/.nnmaildir下创建~/.nnmaildir/work文件夹,该文件夹下包含cur, new, tmp三个子文件夹。 ~/.nnmaildir/work也是我们的mail被收到的地方,下面是 getmail配置:

$cd
$mkdir .getmail

编辑.getmail/getmailrc如下

[options]
verbose = 1
read_all = true
delete=false    #保留服务器备份
message_log = ~/.getmail/log
 
[retriever]
type = SimplePOP3Retriever
server = POPServer
username = NAME
password = PASSWD
delete_dup_msgids=false
 
[destination]
type = Maildir
path = ~/.nnmaildir/work/  #邮件路径

getmail设置结束后,可以简单测试一下 (-n表示只收取未读的mail):

$getmail -n

如果一切顺利,此时在gnus group中应该能看到nnmaildir:work的group中有新mail了。如果nnmaildir:work未出现,可能是因为没有订阅,可以按 ^ 到server buffer中选择{nnmaildir:},在出现的buffer中按u来订阅该group。

最后利用crontab让getmail每隔4分钟收一次信:

*/4 * * * * /usr/bin/getmail -n

对crontab不熟悉的同学看这里.

Categories: emacs Tags: , ,

2009总结之emacs篇

December 30th, 2009 wlamos 1,385 views 4 comments

简单说一下:
1. 有了大显示器后,入了把cherry,非常顺手
2. 结识了另一Emacs资深用户,交流后,收获颇丰
3. 帮一个startup写了一个扩展,获得好评

总体说来,从开始用Emacs到现在,今年的提高最大,开始真正的根据自己的需要来写函数,工
作效率极大提高。

2010展望
1. 发展1到2名emacs用户
2. 在能力范围内帮助新手,继续进步

Categories: emacs Tags: ,

近期计划

December 15th, 2009 wlamos 1,215 views No comments
  1. 读一下SLIME的文档
  2. 读一下SBCL的文档
  3. 持续疯狂的周末购物
Categories: Uncategorized Tags:

literate programming

November 10th, 2009 wlamos 1,147 views No comments

这东东跟Lisp里的宏概念类似,可以关注一下。不过Emacs啥时候能完美支持Multiple Major Mode?!

唉,睡不着!

Categories: Uncategorized Tags:

在Emacs中查询国内城市天气预报的扩展

October 17th, 2009 wlamos 1,541 views 5 comments

使用Emacs后,就想让更多的事情在Emacs中完成。刚好看到sly9@newsmth的blog,, 其中有篇提供了获取国内城市天气预报的方法(该文章在这里),并提供了一个 json文件下载。所以我就写了个小插件, cn-weather, 这样在Emacs中就可以查看所在城市的天气预报了。在此感谢syl9.

使用方式: 下载后将它放到load path中,在你的.emacs文件中

(require 'cn-weather)

eval-current-buffer后,利用

M-x cn-weather-today

来得到今天的天气预报(输出在minibuffer中)。利用

M-x cn-weather-forecast

来得到未来两天的天气预报。

默认查询的城市为北京,如果要修改为其他城市,可以修改变量cn-weather-city,比如

(setq cn-weather-city "西安")

将查询西安的天气。

Categories: emacs Tags:

debian修复安装grub2

October 17th, 2009 wlamos 1,981 views No comments

昨天升级了一下系统,发现grub升级到grub2了,但是并没有完全将grub删除掉,而是将
grub2做为grub的一个item,另外一个提示说测试通过后,运行upgrade-from-grub-legacy
就ok了,然后我就运行了一下(手欠,没看清楚。。。)。重启后发现grub启动不了,报
Error 15. 而我手头还没安装盘或livecd盘。啥也别说了,眼泪。。。

早上到公司,找了rescue盘,折腾了半天搞定。主要是我的boot单独一个分区,而我不知道
如何chroot后能mount那个boot分区,也就是下面的第4,5步。解决的整个过程如下:

利用debian livecd或rescue得到一个shell,在该shell中进行操作。
首先将原有系统的root分区mount到某个目录下,可以利用fdisk -l来查看现有分区。
以我自己的情况为例,/dev/sda1 为 /boot, /dev/sda2/ 为/。

1. User:~# sudo mkdir /mnt/sda1 /mnt/sda2
2. User:~# sudo mount /dev/sda1 /mnt/sda1
3. User:~# sudo mount /dev/sda2 /mnt/sda2
4. User:~# sudo mount -t proc none /mnt/sda2/proc
5. User:~# sudo mount -o bind /dev /mnt/sda2/dev
6. User:~# sudo chroot /mnt/sda2
7. User:~# grub-install /dev/sda
8. User:~# update-grub2
9. User:~# reboot

Categories: Linux Tags: ,

如何判断一个buffer是否是某个主模式

October 16th, 2009 wlamos 1,116 views No comments

Elisp开发过程中,有时我们需要判断某个buffer是否在某个主模式下,我们可以通过在该
buffer中判断buffer-local的变量major-mode的值来判断此buffer的主模式。

major-mode变量保存了表示当前buffer主模式的符号(symbol)。比如要判断当前buffer的主模式是否
为c++-mode,则可以如下实现:

(defun c++-mode-p ()
  "Return t if current buffer's major mode is `c++-mode'."
  (interactive)
  (eq major-mode 'c++-mode))

进一步,可以得到更一般的函数

(defun major-mode-p (mode)
  "Return t if current buffer's major mode is MODE."
  (interactive)
  (eq major-mode mode))
Categories: emacs Tags: ,

努力进窄门

October 14th, 2009 wlamos 1,197 views No comments

“Enter by the narrow gate, for wide is the gate and broad is the way that leads to destruction, and there are many who go in by it. ”
“Because narrow is the gate difficult is the way leads to life, and there are few who find it.”

“你们要进窄门。因为引导灭亡,那门是宽的,路是大的,进去的人也多;引到永生,那门是窄的,路是小的,找着的人也少。”
——《新约·马太福音》第七章

Categories: Uncategorized Tags: