顯示具有 emacs 標籤的文章。 顯示所有文章
顯示具有 emacs 標籤的文章。 顯示所有文章

2012年10月1日 星期一

Emacs for objective-c development

Emacs for obj-C development.md

To develop GNUStep program under Debian, we need 2 handy tools:

1. Auto complete for obj-c.
2. Source browsing.

For the first one, emacs has auto complete package support for after Emacs24. If you are using Debian wheezy, you have to build from source. Before compile, the pre-requisite packages are:

 $ sudo aptitude install build-essential libxpm-dev libgif-dev libtiff-dev libjpeg-dev libgtk2.0-dev libdbus-1-dev texinfo

Build with these pre-requisite packages can let your emacs customise the X fonts easily. And the build commands are :

$ ./configure --with-x-toolkit --with-xft
$ make
$ sudo make install

ps.: It seems that the default configuration does the same work.

The default install directory is /usr/local/bin.

Since Emacs24, in emacs package provides a convient way to install e-lisp plug-ins, add the following lines in your .emacs :

(require 'package)
(add-to-list 'package-archives 
    '("marmalade" .
    "http://marmalade-repo.org/packages/"))
(package-initialize)
(add-to-list 'load-path "~/.emacs.d")    ; This may not be appeared if you have already added.

Now open emacs, install the auto-complete package by :

M-x package-install [RET] auto-complete [RET]

After that, append the following lines in your .emacs

(require 'auto-complete-config)
(ac-config-default)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/elpa/auto-complete-1.4/dict")
(add-to-list 'ac-modes 'objc-mode) ;;here load the objc-mode

Note : Here the version is 1.4, modify the version if necessary.

OK, now we should have the emacs support auto-complete for obj-c.

For the second one: Source browsing, can be done with Gnu Global (gtags).

Since the Debian/Ubuntu currently provide Global-5.7.1, which does not support '--encode-path' option, the option will be used in the plug-in. We have to build Global-6.2.4 from source then "configure" "make install", and complete the setup by copying gtags.el to ~/.emacs.d and append :

(add-to-list 'load-path "~/.emacs.d")    ; This may not be appeared if you have already added.
(autoload 'gtags-mode "gtags" "" t)

After that, the gtags command will be setup correctly. #

2010年8月26日 星期四

emacsclient font configurations

This text applies to version after Emacs 23.1
首先,我們可以把下列一行加入 ~/.bashrc
  alias ex='emacsclient -a "" -c "$@"'

解釋一下:
當我們使用 emacsclient 時,必須有一個 emacs server 啟動才可以用。
emacsclient 的參數有一個 -a "" 表示萬一 emacs server 沒有啟動,
就使用 `alternate editor' ,它可以是 vi, pico ... ,若是空字串 ("") ,表示 `那就叫一個 emacs server 起來好了'.
另外 參數 -c 表示 `create a new frame' 而不是用 server 的 frame.
至於後面要打開的檔案(可以一次指定開多個檔案來編輯),就用 bash 提供的 $@ 來代入即可。

第二,設定字型,因為 emacs server 可以在 X 之前就啟動了,因此,
很合理的,它不理會你指定在 X 環境下的字型。
現在的 X 環境下有兩種字體系統:核心字體系統與 Xft 字體系統。Xft 有許多優良的特性(向量,反鋸齒等),
所以 Emacs 23.1 開始支援 Xft as its font base. (see XftGnuEmacs).
只要在你的  ~/.Xresources 指定就好,例如,我們打算用 DejaVu Sans Mono 字型 :
    Emacs.FontBackend: xft
    Emacs.font: DejaVu Sans Mono-14

(謎:怎樣查出這個字型呢? M-x describe-font 然後給個 mono 讓它 match 出完整的字型敘述)

第三,設定 frame 的特性,我們希望打開 frame 時,是黑底白字,不要 tool bar, 不要 scroll bar ...
這些都與預設值不同,該怎麼設定呢?
emacs 的 default-frame-alist 裡面紀錄了 `新開一個 frame 所需的參數',設定這個就對了!
在你的 ~/.emacs 內這樣指定
(let ((frame '((left . 0)
               (top . 0)
               (width . 110)
               (height . 30)
               (tool-bar-lines . nil)
               (vertical-scroll-bars . nil)
               (background-color . "black")
               (foreground-color . "white")          
)))
  (setq default-frame-alist frame)
  (setq initial-frame-alist frame)

)

說明一下,[ GNU Emacs 手冊 ]查得到所有的 frame parameters。
第一次開啟的 frame(應該是 server frame)  參數會參考 `initial-frame-alist' ,往後開啟 frames(emacsclient frame)都會參考 `default-frame-alist' .

完工了!記得重新開啟 X 讓 .Xresources 生效啊啊~

2009年11月18日 星期三

Emacs 捲動的設定

Emacs 內定的捲動方式為一次半頁,這樣看起來跳動很大,有些不習慣。修改 .emacs 加入下面設定,可以變成『自然捲』...
(setq scroll-margin 0 scroll-conservatively 10000 )

另外,想定義新的 macro 並且紀錄於 .emacs 以供下次使用,可以這樣作,
(以 scroll other window (up/down) one line 這個動作為例) :

C-x (      定義新的 macro
M 1 C-M-v   我們的新 command --- scroll other window down one line
C-x )     結束 macro 定義

給它取一個名字:
M-x name-last-kbd-macro [Enter]
打入自取名稱 ,例如 scroll-other-window-down-one-line [Enter]

打開 ~/.emacs 準備加入這個新定義:
M-x insert-kbd-macro
打入剛剛取的名稱  scroll-other-window-down-one-line [Enter] 就會出現
(fset 'scroll-other-window-down-one-line
   "\261\226")
然後定義一個 hotkey 例如 F11 給它:
(define-key global-map [f11] 'scroll-other-window-down-one-line)

以後按 F11 就會 scroll down other window.

為了對稱,做了一個 scroll other window up one line ,最後在 .emacs 裡長成這樣:

...
(fset 'scroll-other-window-up-one-line
   "\2551\226")
(define-key global-map [M-f11] 'scroll-other-window-up-one-line)
(fset 'scroll-other-window-down-one-line
   "\261\226")
(define-key global-map [f11] 'scroll-other-window-down-one-line)
...

Technorati 標籤:

2009年1月20日 星期二

使用cscope 取代 source insight 以瀏覽 kernel source





解決linux下代碼查看問題

在 Windows 下面我們有 Source Insight 可以方便的瀏覽大工程中的代碼,切換到 Linux 環境下開發時, 我們也可以搭建一個這樣的環境。下面的內容將介紹如何搭建這樣一個開發環境(這裡我們假設讀者已經熟悉 emacs 的安裝和配置)。
  步驟一安裝下列軟件
 
 1)cscope :cscope是一個代碼瀏覽工具,它可以幫你在一個大的工程中,
快速定位到一個函數/變量的聲明位置,所有引用地方等,它可以結合vim和emacs一起使用。
單獨使用cscope時不同文件間的跳轉變得很難處理,這裡我們介紹cscope在emacs環境中的使用,它需要預先建立索引檔:根據
cscope.files 的內容來建立索引:cscope.[in][out]。

  步驟二修改或創建.emacs文件
  ;;加載我們需要的plugin (使用cscope的必備動作)
(load-file "/usr/share/emacs/site-lisp/xcscope.el")
(require 'xcscope)
(setq cscope-do-not-update-database "t")
  ;; 這行後面會解釋
(setq cscope-set-initial-directory "./")  ;; 在現有目錄下找 cscope.out

      步驟三添加工程:
  假設我們要把/home/tom/src/linux-2.6.23的源代碼做出cscope索引,我們可以這樣做,

       #>make ARCH=arm cscope  這裡 ARCH 可以等於 arm, x86, mips,  ...等等你想要的 CPU arch。

       手動自己來:

  1)#>cd /home/tom/src/linux-2.6.23 進入源代碼根目錄;
  2)#>touch cscope.sh 創建一個腳本文件,內容如下

#!/bin/bash
LNX=`pwd`
ARCH=arm
cd /
find $LNX/ \
-path "$LNX/arch/*" ! -path "$LNX/arch/$ARCH*" -prune -o \
-path "$LNX/include/asm-*" ! -path "$LNX/include/asm-$ARCH*" -prune -o \
-path "$LNX/tmp*" -prune -o \
-path "$LNX/Documentation*" -prune -o \
-path "$LNX/scripts*" -prune -o \
-path "$LNX/drivers*" -prune -o \
-name "*.[chxsS]" -print >$LNX/cscope.files
find $LNX/ -path "$LNX/include/asm-generic*" -name "*.[chxsS]" -print >> $LNX/cscope.files

 然後 #>cscope -b -k -q ( -q 是建立雙向鏈結,可增加搜尋速度!),這時候要等個幾分鐘,等待索引的建立!因為針對 ARCH=arm 來做,所以建出以 arm 為主的索引檔。

    成功後執行 cscope.sh 腳本。

  步驟四:關於cscope代碼瀏覽命令
            C-c s a 設定初始化的目錄(cscope-set-initial-directory) ,一般是你代碼的根目錄,為了省事,我們可以把這一行命令在進入 emacs 就執行,像剛剛的 .emacs 範例一樣。
       鬼 C-c s I (i 大寫) 對目錄中的相關文件建立列表並進行索引。內定會自動建立索引。記得嗎?我們剛剛做過索引了,並用 -q 參數,所以這裡不但不要用這個指令,(在 emacs 外面用 -q 建索引就好),還應該要 disable 自動建索引的功能,因為在 emacs 裡面會主動呼叫 cscope 建立索引。所以剛剛我們修改了(setq cscope-do-not-update-database "t")。

若不這樣改,在自動建立索引時會抱怨 -q 與 database 不合的警告,重建的 database 也將喪失雙向搜尋的能力,用起來會變得蠻慢的喔!因為 kernel symbol 實在太多了!

            C-c s s 序找符號
            C-c s g 尋找全局的定義(即是 cscope-find-global-definition)
            C-c s c 看看指定函數被哪些函數所調用
            C-c s C 看看指定函數調用了哪些函數
            C-c s e 尋找正則表達式
            C-c s f 尋找文件
            C-c s i 看看指定的文件被哪些文件include
            C-c s u 回到上一個 symbol  (即cscope-pop-mark)

結論:Source Insight 雖然直覺好用,但是你還得找到 Windows 環境才能 run,用wine 模擬 Windows 是一個辦法啦!這裡只是提供了『純 Linux 環境』的做法,供大家參考!

補充:用 etags 配合使用,更方便:(假設已經裝好 ctags 套件)       

       1)   在進入 emacs 之前先用 etags -R 建立 TAG 檔。
在 emacs 裏面的指令:
       2)   M-x visit-tags-table 會詢問是否用 default TAG file? 按 y 即可。
       3)   M-. 找定義,以游標所在位置的變數來找。
       4)   M-* 返回。
       5)   C-u M-. 尋找標籤的下一個定義。


補充:修改 .emacs 取代常用的 cscope 命令,在 (require 'xcscope) 後加上
(define-key global-map [f5] 'cscope-find-this-file)
(define-key global-map [f6] 'cscope-find-this-symbol)
(define-key global-map [f7] 'cscope-pop-mark)
(define-key global-map [f8] 'cscope-find-global-definition)
(define-key global-map [f9] 'cscope-find-global-definition-no-prompting)
(define-key global-map [M-up] 'cscope-prev-symbol)
(define-key global-map [M-down] 'cscope-next-symbol)
(define-key global-map [f12] 'c-down-conditional-with-else)
(define-key global-map [M-f12] 'c-up-conditional-with-else)

這樣,就可以用 F5, F6, F7, F8, F9 Esc-↑ Esc-↓ 來 browse code,您可以把常用的 key 如法泡製。