Archive for the ‘emacs’ tag
一个简单的AHK脚本
#IfWinActive ahk_class Emacs
Capslock::Control
Control::Capslock
#IfWinActive
假如当前的ahk_class是Emacs,那么交换Ctrl和Caps,否则不交换。
Emacs里面的MiniMap
Sublime Text的MiniMap比较有特色,正在想是不是能为Emacs也搞一个类似的东西,没想到已经有了。(不知道MiniMap是啥的,请移步Sublime Text的网站)
http://www.emacswiki.org/emacs/MiniMap
Emacs MiniMap的tricky之处在于,它并没有实现什么新的GUI控件,而是直接新开一个buffer,把font face缩小一下,达到MiniMap的类似效果。想出这法子的哥们可真行。
不过这也意味着MiniMap在字符界面是下不可用的——不过这谁又在乎呢,毕竟Sublime Text连CUI的版本都没有。在Emacs里面用这么小的代价实现相似的功能,一方面是Emacs这货实现是强大,另外一方面是写这东西的哥们实在也挺有想法的。
不过目前看来这个MiniMap功能是有了,但是体验不算特别好。用vertical split开的window在C-x 1之后就不见了,这可是很不爽的。依我的想法来看,能不能想办法让minimap window比较特殊一点,于是自己fork了一个别人已经做了修改的版本hack了一下。目前我主要hack了三个小地方:
1. 为delete-other-windows做了advice,总是保留MiniMap window;
2. 修正了make-overlay的一个错误参数,不改的话为空文件生成的overlay是不对的;
3. 只为图形界面的Emacs创建MiniMap,毕竟用了font face,在字符界面下意义不大。
随便改了一下,感兴趣的就拿走。
https://github.com/liuw/emacs-minimap
MiniMap的实现也不算特别稳定,有时会让Emacs死掉的。但是我没有那个水平,也懒得去debug了——说实话我还是用CUI的Emacs比较多点,看这东西单纯是觉得新奇而已。废话少说了,上图。
Emacs中的Rectangle
记一下Emacs里面的rectangle相关的操作。
Emacs的rectangle概念,就是你mark的起点和终点之间围起来的区域。由于rectangle没有专门的模式去select,所以在显示方面,transient-mark-mode不能只高亮这个rectangle。像在Vim里面,可以用C-v启用Visual mode blockwise,看起来会漂亮点。不过,看不看得清楚并不影响操作。
在Emacs中输入C-h a,然后输入rectangle就可以得到一些相关的函数。
比较有用的是对rectangle的delete、kill、yank操作。
然后还有replace-rectangle、string-insert-rectangle、string-rectangle。
shortcut就不写了,我是M-x党。
Emacs按序加载配置文件
我希望Emacs可以有按序加载配置文件能力,简单的说,就是希望它能按照文件名的字母表排序来加载配置文件。这样我就可以在文件名前加上几个数字作为加载优先级了。这在Unix的世界里面是很常见的。
我是个懒人,总是希望有别人做好给我用。我换了好几个关键词去搜索,都没有找到别人写好的代码。也许是这个功能太简单不值得放到网上,也许是我的关键词不对,总之我就是没有搜到。
好吧,到了这一步也就不想再偷懒了,自己写点代码来实现这个功能吧。真的只是一点点。
(defun liuw-find-el-in-directory (directory)
"List the .el files in DIRECTORY."
;; This can be made recursive
(interactive "DDirectory: ")
(let (el-files-list
(current-directory-list
(directory-files-and-attributes directory t)))
(while current-directory-list
(if (equal ".el"
(substring (car (car current-directory-list)) -3))
(setq el-files-list
(cons (car (car current-directory-list)) el-files-list)))
(setq current-directory-list (cdr current-directory-list)))
el-files-list))
(defun liuw-get-sorted-el-list (directory)
(interactive "DDirectory: ")
(let ((l (sort (liuw-find-el-in-directory directory) 'string<)))
l))
(defun liuw-load-ordered-startup-el (directory)
(interactive "DDirectory: ")
(let ((l (liuw-get-sorted-el-list directory)))
(while l
(load-file (car l))
(setq l (cdr l)))))
Elisp早已生疏,所以写这点代码也颇费了点时间。只能感叹自己学艺不精了。
Org-mode的Code Blocks功能
在使用Emacs的Org-mode记笔记的时候,难免会遇到要输入代码的情况,使用code block功能来做,方便又美观。
一般的格式是:
#+begin_src language org-switches header-arguments CODE HERE #+end_src
比如说我要输入Ruby代码:
#+begin_src ruby puts 'hello world' #+end_src
还有更酷的。在code block中使用
C-c '
可以调用org-edit-src-code,直接切换到语言对应的mode进行输入,充分享受Emacs对语言的支持。
如果不在code block中调用org-edit-src-code,会进入Artist mode,可以用鼠标画图。
另外,code block还支持代码执行。
详见:http://orgmode.org/worg/org-contrib/babel/intro.php
Emacs中删除空行
m-x flush-lines
^$
發一段以前寫的elisp
無任何實用價值,無聊的時候寫的,做ASCII Art動畫。
(defun cinema (file fps)
(interactive "sFile: \nnFPS: ")
(let ((int-time (/ 1.0 fps)))
(find-file file)
(while t
(scroll-up 25)
(sit-for int-time))))
具體效果見 ASCII Art Bad Apple Emacs 。
近期收集的一些有趣的小東東
近來比較少寫blog,實在是比較忙。用繁體寫是因為看簡體的字多了眼花,得換換口味了。
72pines的頁面很奇怪,假如我不用搜狗的全網加速功能去開,出來的頁面就沒有樣式了。可能它有一部份的伺服器被教育網墻掉了(?)。
在王亮先生的Emacs定制和擴展中,找到一個“能獲取root權限的輔助腳本”,解決了我一直頭痛的一個問題。原理挺簡單,使用TRAMP來完全的。以前一直以為TRAMP主要是用于遠程編輯文件的,沒有發現這個功能,沒仔細看Manual的後果。
(defun wl-sudo-find-file (file dir) (find-file (concat "/sudo:localhost:" (expand-file-name file dir))))
當然,其他的內容也很精彩。
第二個,是從Wowubuntu得來的三手信息。爲什麽說是三手呢,因為它也只是轉載的。主要是講Bash shell的一些奇技淫巧。
最牛B的 Linux Shell 命令 系列连载,有點標題黨了,呵呵。
比較有趣的是活用history功能的條目,比如說sudo執行前一條命令是:
$ sudo !!
兩個嘆號“!!”等價于“!-1”,數字換成其他也是可以的,不過誰記得了那麼多啊。
另外一個有趣的功能就是Bash的替換(原來它也有啊,呵呵):
$ !!:s/foo/bar/
很熟悉的語法,只是從來沒有想到過還有這個功能,sigh。
文章提到上面兩個技巧都是出自The Definitive Guide to Bash Command Line History。
還有一些其他的技巧,不僅僅限於Bash本身,不一而足。由於有的要么知道了,要么對我自己用處不大,這里就不多記了。
Get rid of ^M in file
In fact, a ‘^M’ in text file is a carriage return (CR), often introduced by DOS text file format. It’s possible to do a simple substitution to get rid of it.
Easiest way is to use a dedicated tool called `dos2unix’, and yes, there is a corresponding tool called `unix2dos’.
Or, we can use Vim/Emacs to convert it.
(first open file with Vim) :set ff=unix :set ff=dos
Or use Vim command line switches.
$ vim +"set ff=unix" +wq $DOS_FILE
In Emacs, just do a substitution. Use C-q C-m to input ^M.
How to Copy to / Paste from Clipboard for Emacs
It feels easy to copy/paste within Emacs, but sometimes we need to transfer data between Emacs and other programs. Many window systems have some sort of “clipboard” to do simple transfers.
Emacs can also make use of system clipboard. See following commands.
clipboard-kill-region clipboard-kill-ring-save clipboard-yank
I’ve been looking for them for years, gee.
