Liuw’s Thinkpad

想要赢就先学会输,想要成功就先学会失败

Archive for the ‘emacs’ tag

Emacs中删除空行

without comments

m-x flush-lines
^$

Written by liuw

八月 31st, 2010 at 5:09 下午

Posted in UNIX-like

Tagged with ,

發一段以前寫的elisp

without comments

無任何實用價值,無聊的時候寫的,做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

Written by liuw

八月 28th, 2010 at 11:00 上午

Posted in Programming, 生活

Tagged with ,

近期收集的一些有趣的小東東

without comments

近來比較少寫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本身,不一而足。由於有的要么知道了,要么對我自己用處不大,這里就不多記了。

Written by liuw

八月 26th, 2010 at 7:00 下午

Posted in UNIX-like, 分享

Tagged with , ,

Get rid of ^M in file

without comments

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.

Written by liuw

七月 11th, 2010 at 8:04 下午

Posted in UNIX-like

Tagged with , , ,

How to Copy to / Paste from Clipboard for Emacs

without comments

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.

Written by liuw

二月 25th, 2010 at 11:21 下午

Posted in Programming, 分享

Tagged with , , ,

Org-mode使用笔记

without comments

Org-mode使用笔记
================

Author: liuw
Date: 2010-02-25 Thu.

Table of Contents
=================
1 介绍
2 具体配置
2.1 dotemacs中的org-mode配置
2.2 org文件中的配置
3 Org-mode作弊条
4 更多

1 介绍
~~~~~~~

用Emacs的时间也不短了,但是也仅限限于写写程序,作为一个很纯粹的编辑器来使用。我知道Emacs还有很多其他的功能,但是自己却懒得学。好吧,我确实是一个懒人,自己觉得事情老是做不完,又健忘,所以很多时候,必须有小纸条。

后来发现好东西了,就是Emacs的org-mode。虽然也不是第一次听说它了,但是一直懒得学。现在觉得是时间改一改坏习惯了,所以就打算用org-mode来把自己的时间管理起来。老外这方面的文章不少,把org-mode和GTD方法结合起来,作为自己的时间和日程管理工具。中文也有点资料,但是不多。这里就随便记一下自己的一些设置吧。

2 具体配置
~~~~~~~~~~~

2.1 dotemacs中的org-mode配置
=============================

除使用org-mode作为org文件的默认模式之外,也把它作为txt文件的默认模式(因为实在是太好用了)。另外还定义了一些全局的快捷键,可以更快地使用一些功能。

打开org-log-done,这样一个TODO处理完之后,会自动标记上CLOSED这样的语句,并且记录当时的时间。

另外,TODO keywords使用的是GTD风格的keyword。这样对于某一个TODO的执行情况可以进行更好的跟踪。

我喜欢把我的TODO分布到几个文件中去,这样在agenda看的时候,比较清楚点。更进一步的,我使用git把这些org文件进行统一的管理。当然,org-mode也有archive的功能,但是我目前还没有启用。


(add-to-list 'auto-mode-alist '("\.\(org\|org_archive\|txt\)$" . org-mode))
(define-key global-map "C-cl" 'org-store-link)
(define-key global-map "C-ca" 'org-agenda)
(setq org-log-done t)
(setq org-hide-leading-stars t)

(setq org-todo-keywords
      (list "TODO(t)" "STARTED(s!)" "WAITING(w@)" "|" "CANCELED(c)" "DONE(d)"))
(setq org-agenda-files
      (list "~/git/org/personal.org"
            "~/git/org/lab.org"
            "~/git/org/opensource.org"
            "~/git/org/todo.org"))
(defun todo ()
  (interactive)
  (find-file "~/git/org/todo.org")
  )

2.2 org文件中的配置
====================

org-mode也支持per-file-variable,所以可以对每个文件进行单独的配置。由于我使用类GTD的方法进行管理,所以我在每个文件中都加上类似的标签配置。


#+TAGS: @work(w) @home(h) @sport(s)
#+TAGS: laptop(l) pc(p)

这些标签用于标示GTD中的context概念。

3 Org-mode作弊条
~~~~~~~~~~~~~~~~~


  快捷键          功能            

  C-c a           agenda模式
  C-c C-s         设置schedule时间
  C-c C-d         设置deadline时间
  C-c C-t         设置事务状态
  C-c C-c         设置标签
  C-c ,           设置优先级
  S-/   改变优先级        

4 更多
~~~~~~~

Org-mode的强大之处是,它可以为你自动生成图形、表格(在表格中甚至还支持计算)。可以把org文件导出成为各种各样的格式。

目前我使用的功能已经足够了,本着能少学就少学的精神,很多东西还没有进行深究。有需要再看info去。

这个文件就是用org-mode的publishing功能生成了。原先写的是一个txt文件,呵呵。挺不错的。

org-mode

Written by liuw

二月 25th, 2010 at 3:54 下午

Posted in 分享, 生活

Tagged with ,

Elisp不支持闭包,白忙了

without comments

今天下午研究了一下Continuation,在Wikipedia上看到了Continuation Passing Style的相关介绍,带有例程(不过是用Scheme写的)。既然自己在用Emacs,那么试试把例子都用Elisp实现一下吧。

其中有一个函数,用来制作支持CPS的函数,原型是这样的:


(define (cps-prim f)
  (lambda args
    (let ((r (reverse args)))
      ((car r) (apply f
                (reverse (cdr r)))))))

这样用,比如说要制作一个支持CPS的减法运算:


(define cps- (cps-prim -))

假如不使用cps-prim,那么每个函数都要这样写:


(define cps- (a b k)
  (k (- a b)))

很显然,使用cps-prim更加方便点。于是我打算用Elisp也做一个同样的函数,谁让我这么懒呢。

Elisp比较乱,所以写了挺久的,但是总算是写出来了:


(defun cps-prim (f)
  #'(lambda (args)
      (let ((r (reverse args)))
        (funcall (car r) (apply f
                                (reverse (cdr r)))))))
(fset 'cps- (cps-prim '-))

但是使用cps-的时候,会提示f无值。好吧,查一个info,心彻底凉了。11.9.2中明确写到,Elisp是不支持闭包的,所以f在cps-中是unbound的。想偷懒偷不成咯。

再进一步想想,Elisp不支持闭包,是否就不支持高阶函数呢?个人程序设计语言方面没什么了解,只能google了。显然我不是第一个想到这个问题的,还真是发现了一些有趣的东西。

http://lambda-the-ultimate.org/node/1936

有人说:

Closure = Function x Environment

不得不说,这个等式,比一个冗长的定义更清楚。

Written by liuw

一月 23rd, 2010 at 10:42 下午

Posted in Programming, Tech

Tagged with , , , ,

What If You Need Hint for Certain Keywords

without comments

When editing in Emacs, sometimes it’s necessary to search for certain commands, when you only know part of the command or only have one or two keywords as clue.


M-x apropos

Written by liuw

一月 18th, 2010 at 4:32 下午

Posted in UNIX-like, 分享

Tagged with ,

Fast Configuration of Company Mode

without comments

http://nschum.de/src/emacs/company-mode/

Add following lines to dotemacs.


(add-to-list 'load-path "/path/to/company")
(autoload 'company-mode "company" nil t)

;; Add hook for file types.
(add-hook 'c-mode-hook (lambda() (company-mode)))

;; Bind company-manual-begin, company-complete-common, company-complete,
;; company-select-next to a key in the global map.
(global-set-key "\t" 'company-complete-common)
;; Alternatively, set company-idle-delay to t, so it will complete immediately.

;; Helper functions
  (defun complete-or-indent ()
    (interactive)
    (if (company-manual-begin)
        (company-complete-common)
      (indent-according-to-mode)))
  (defun indent-or-complete ()
    (interactive)
    (if (looking-at "\\_>")
        (company-complete-common)
      (indent-according-to-mode)))

Since I would like to use TAB for both completion and indentation, I prefer the following helper function to make life easier - company will mess up TAB key when in minibuffer.


(defun tab-complete-or-indent ()
  (interactive)
  (if (minibufferp)
      (minibuffer-complete)
    (if (company-manual-begin)
        (company-complete-common)
      (indent-according-to-mode))
    )
  )

(global-set-key [tab] 'tab-complete-or-indent)

Currently, I’m using the following configuration.


(add-hook 'c-mode-hook (lambda() (company-mode)))
(add-hook 'lisp-mode-hook (lambda() (company-mode)))
(add-hook 'emacs-lisp-mode-hook (lambda() (company-mode)))

(setq company-begin-commands '(self-insert-command))
(setq company-idle-delay t)
(setq company-minimum-prefix-length 1)

Written by liuw

一月 12th, 2010 at 7:38 下午

Posted in UNIX-like, 分享

Tagged with ,

Emacs Local Variables

without comments

目前使用的C程序local variables如下:


/*
 * Local variables:
 * mode: C
 * c-set-style: "BSD"
 * c-basic-offset: 4
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */

关于Emacs的local variables,可以参考Emacs的info和下面的网页。

http://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html

Written by liuw

十二月 7th, 2009 at 3:34 下午

Posted in Programming, UNIX-like, 分享

Tagged with ,