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

2012-01-05

Publish org to remote host through ssh

I've used Emacs org-mode to manage my personal notes and work stuff for a while. Its flexibility always amazes me.

Normally, I keep my personal notes in a Dropbox directory that are synced automatically. But, for work stuff, I created a local directory for them in intranet and published them to a secured remote host through ssh. The org-publish supports this feature well after you've configured ssh.

Customize Emacs compile

At work, I use M-x compile to build the project I'm working on. Chances are that default compile-command is not what you want. You can customize it and bind it to hotkeys as follows, 
Now, you can use F8, F9 to build the optimized and debug version.

2011-12-27

Emacs server per workspace (GNU Screen)

I am a heavy user of GNU screen and Emacs at work and home. In the past, I created different GNU Screen instances for different projects and only one instance of Emacs server in one machine. For instance, you are aked to fix bugs in threee code branches and some fixes can't go into certain branches, I need to be very careful when I edit and switch buffers because only once emacs server out there.



What I really need to have setup like this,



Now, whenever you edit or switch files in a GNU screen by emacsclient, you can sit back and relax for a while. But, the problem is how do I make this a bit automatic? If you need to execute 'emacs --daemon=serverN' for every created GNU Screen, that will be very troublesome.
Here is what I have done to make this happen,



Now, when you instantiate GNU Screen by 'ws_XXX' aliases, an Emacs server identified by 'XXX' is created for you. And then you can use 'et' to edit a file in the proper Emacs server.

The only thing I don't like is that I am compelled to use another alias 'kill-emacs' to kill Emacs server. I often 'pkill' to kill process, but this will kill all Emacs servers in the same machine which is absolutely not the behavior I want.

The last thing to do is to patch the source code of emacsclient. In my experiment, emacsclient in the latest stable release (23.3b) doesn't pass correct argument for Emacs to start its daemon mode. So, I made minor changes to make it right.



After recompile and install, you can start using these two power tools in this way as I do.

2011-01-25

Use Emacs/JDEE in Android Development

Eclipse/ADT is a great tool for Android development. But for people using Emacs, it is difficult to get used to it. Fortunately, JDEE is easy to be customized to work on Android development. Here are my settings on my MacBookPro,

;; Load JDEE
(require 'jde)

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(jde-global-classpath (quote ("~/android/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar" "~/android/out/target/common/obj/JAVA_LIBRARIES/core_intermediates/classes.jar" "~/android/out/target/common/obj/JAVA_LIBRARIES/services_intermediates/classes.jar" "~/android/out/out/target/common/obj/JAVA_LIBRARIES/android.policy_intermediates/classes.jar")))
 '(jde-jdk-registry (quote (("1.5.0" . "/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0"))))
 '(jde-sourcepath (quote ("~/android/source/frameworks/base/services" "~/android/source/frameworks/base/core/java" "~/android/source/dalvik/libcore/luni/src/main/java")))

Here, I have android source code in ~/android/source and build in ~/android/out. If you are using SDK, you should add android.jar in SDK to jde-global-classpath. Also, you can put these settings into a per-project file prj.el in your project top directory for JDEE to load.

For example, my prj.el in one of my project looks like,
(jde-project-file-version "1.0")

;; Reset class path and source path
(jde-set-variables
 '(jde-project-name "openalarm-android")
 '(jde-gen-buffer-boilerplate
   '("/**"
     "*  OpenAlarm - an extensible alarm for Android"
     "*  Copyright (C) 2010 Liu Yen-Liang (Josh)"
     "*"
     "*"
     "*  This program is free software: you can redistribute it and/or modify"
     "*  it under the terms of the GNU General Public License as published by"
     "*  the Free Software Foundation, either version 3 of the License, or"
     "*  (at your option) any later version."
     "*"
     "*  This program is distributed in the hope that it will be useful,"
     "*  but WITHOUT ANY WARRANTY; without even the implied warranty of"
     "*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
     "*  GNU General Public License for more details."
     "*"
     "*  You should have received a copy of the GNU General Public License"
     "*  along with this program. If not, see ."
     "*/"))
 '(jde-log-max 5000)
 '(jde-enable-abbrev-mode t)
 ;; Class path for browsing files and generate code templates
 '(jde-global-classpath
   (quote ("$ANDROID_SDK_ROOT/platforms/android-7/android.jar"
           "$HOME/projects/openalarm-android/bin")))
 '(jde-sourcepath
   (quote ("$HOME/projects/openalarm-android/src"
           "$HOME/android/eclair_21/frameworks/base/core/java")
          ))
 '(jde-compile-option-directory "$HOME/projects/openalarm-android/bin") 
 '(jde-complete-function (quote jde-complete-menu)) 
 '(jde-run-working-directory "$HOME/projects/openalarm-android/bin")
 '(jde-help-docsets
   (quote (("Android SDK Doc" "$ANDROID_SDK_ROOT/docs/index.html" nil))))
 '(jde-build-function (quote (jde-ant-build)))
 '(jde-ant-args "-emacs")
 '(jde-ant-complete-target t)
 '(jde-ant-enable-find t) 
 '(jde-ant-read-args nil)
 '(jde-ant-read-buildfile nil)
 '(jde-ant-read-target nil)
 '(jde-ant-use-global-classpath nil)
 '(jde-ant-working-directory "$HOME/projects/openalarm-openalarm/bin/") 
 '(jde-built-class-path (quote ("$HOME/projects/openalarm-android/bin")))
 )
These settings enable us using method completion, source browsing, etc. But, I am still experimenting if we can build android target in JDEE.

For MacOS users: the environment variables must be defined in ~/.MacOSX/environemtn.plist

2009-12-14

Encrypt your important data in Emacs org-mode

Emacs org-mode is an awesome tool to help me manage your personal information. The beauty of it derives from its pure-text-file nature. And the ugly thing, well, also derives from its pure-text-file nature - Security issue. Every bit of your data could be seen if you don't carefully keep them safe.

But, don't panic first. Emacs 23 and org-mode have provided things to secure your data. Normally, I put my very personal data in a separated file and you can make Emacs encrypt it automatically when saving it. All you need to do is to
  1. Load EasyPG in your environment.
    (require 'epa)
    (epa-file-enable)
    
  2. Rename your personal file with .gpg as its extension
  3. Add the following lines to the beginning of the personal file
    -*- mode: org; epa-file-encrypt-to: ("yenliangl@gmail.com") -*-
When you save this file, Emacs will gpg-encrypt this file. And when you load this file back, it prompts you to enter your GPG key.


Second, what if you don't encrypt the whole file? You merely want some header sections encrypted. That's ok. The org-mode provides another module to do this for you - org-crypt. Here is my settings,
(require 'org-crypt) 
(org-crypt-use-before-save-magic)
(setq org-tags-exclude-from-inheritance (quote ("crypt")))
(setq org-crypt-key "0C6F5345") ;; yenliangl@gmail.com
The header sections with tag crypt are encrypted when you save the file. If you don't like this tag, go on to check out org-crypt.el for command to change it.


Note that if you encrypt the whole file, its content won't appear in the agenda view. But, encrypting a header section won't exclude itself from agenda view as long as you put time settings on the header not in its content because its content is encrypted.

2009-11-30

Uniquify buffer names

Sometimes you need to see two or more files with same name. In Emacs, buffer names confuse me a lot because they are like R.java, R.java<2>, R.java<3> and etc. You can make buffer name unique by using uniquify.;
(require 'uniquify)
(setq uniquify-buffer-name-style 'post-forward
     uniquify-separator ":")
There are several choices you can select for this uniquify-buffer-name-style (Copied from documentation).
    For example, files `/foo/bar/mumble/name' and `/baz/quux/mumble/name' would have the following buffer names in the various styles:
    • forward bar/mumble/name quux/mumble/name
    • reverse name\mumble\bar name\mumble\quux
    • post-forward name|bar/mumble name|quux/mumble
    • post-forward-angle-brackets name name
    • nil name name<2>

2009-07-02

Generate diagrams from Emacs

ditaa is a command-line utility written in Java that can converts text into diagram. It works pretty well in Emacs. If you don't need fancy diagrams, it should be a handy tool for you that can quickly capture your idea and convert into diagram. For example, the following text is a ditaa input text that illustrates the lifetime of a activity in Android.
#+begin_ditaa ~/public_html/activity.png -s 0.8
:                 /----------\
:                 | Activity |
:                 |  starts  |
:                 |   cBLU   |
:                 \-----+----/
:                       |
:                       v
:                 +-----+-----+
:                 | onCreate()|
:                 +-----+-----+
:                       |
:                       v
:                 +-----+-----+    +-------------+
:                 | onStart() |<---+ onRestart() |
:                 | o start1  |    |    cYEL     |
:                 | o start2  |    \-------------/
:                 | cRED      |
:                 +-----+-----+
:                       |
:                       v
:                 +-----+-----+
:                 | onResume()|<------+
:                 +-----------+       
#+end_ditaa

ditaa converts it into an image file activity.png at ~/public_html.

Cool. But it is still cumbersome to draw text in this format. Don't worry!!! The built-in artist-mode provides text-based drawing in Emacs so that we can save a lot of time on this. If you think ditaa is useful, you must try artist-mode.

2009-06-30

Squeeze out more display space from your small monitor :(

I don't have a big monitor at work. To squeeze more display spaces out to show more lines of code, we may do tricks like,
  1. Use an extremely small font to hurt your eyes.
  2. Disable menu bar, scroll bar and tool bar.
    (menu-bar-mode -1)
    (tool-bar-mode -1)
    (scroll-bar-mode -1)
    
  3. Use small fringes (2 in my settings)
    (setq default-frame-alist
          '(
            (left-fringe . 2)
            (right-fringe . 2)
            ;; ...
           ))
    
  4. Use smaller modeline.
    (set-face-attribute 'mode-line nil :background "Wheat" :foreground "DarkSlateGrey" :height 0.5))
    
  5. I often open a shell buffer inside Emacs. It is opened to do compiling, file operations and other stuff you may want to do in normal shell. You probably don't spend a lot of time working in it. After all, your job is coding, not listing files by ls -al all the time. So, can we use a smaller font in all shell buffers?
    ;; make a face
    (make-face 'font-lock-small-face)
    (set-face-attribute 'font-lock-small-face nil :height 0.5) ; have smaller font.
    
    ;; add this setting to all shell-mode buffers
    (add-hook 'shell-mode-hook '(lambda ()
                                (font-lock-mode t)
                                (buffer-face-mode t) ; only in emacs 23
                                (buffer-face-set 'font-lock-small-face)
                                ;; other stuff
                               ))
    
Finally, I want to say buying a big monitor saves you more time! :)

2009-05-05

Limit Emacs to show current function only

When working on a long function (too long? you should split it), you can use C-x n d to restrict Emacs to display current function only and use C-x n w to restore.

2009-04-30

在Emacs中顯示列數

在Vim中,可以使用:set number來顯示列數。那在Emacs中呢?當然可以啦!在Emacs中,可以利用linum這個套件來顯示列數。只要將下面加入你的.emacs中就可以了。
(require 'linum-mode)

若是嫌linum-mode預設的字型或顏色不合口味,也可以對linum的face作設定。

2009-04-29

使用 windmove在Emacs視窗中移動

我們知道在Gnome桌面環境裡,可以用Ctrl+Alt再加上上下左右鍵在虛擬桌面間移動。同樣的,有沒有辦法使用類似的鍵在Emacs中移動游標到不同的視窗中(Emacs中,Window是定義成一個分割的區域喔。Frame才是一般熟知的視窗喔)呢?答案是有的。就是使用windmove套件,將下面兩行貼入你的Scratch Buffer中先試試看。
(require 'windmove)
(windmove-default-keybindings 'control)
所以,Ctrl再加上下左右鍵就可以在視窗間移動囉。

指定Emacs使用的中文字型

If you don't like the default Chinese font in Emacs, you can change it by
(set-fontset-font (frame-parameter nil 'font)
                 'han '("cwTeXHeiBold" . "unicode-bmp"))

My Emacs,

2008-10-15

Emacs server + GNU screen daemon <-> Emacsclient + GNU screen

If the server mode of Emacs is activated and files are opened through emacsclient, opening files by Emacs can be as fast as vim. But you can see it running on you desktop anyhow, a showing window or a foreground process. Can we make it a background job? Yep, Screen can do the trick for us and I believe this can increase our productivity to a greater extent.

The first thing we need to do is to know how to activate server mode in Emacs. You can just type
M-x server-start
or put
(server-start)
in .emacs file.

Next, run emacs inside a screen started as a daemon,
screen -dmS emacs emacs-snapshot -nw

Note that the emacs used here is the snapshot version built from CVS repository. Only the CVS version of emacsclient supports the -t option that will be talked later.

Then, use emacsclient to edit a file called foo.txt
emacsclient.emacs-snapshot -t foo.txt

That's it. Happy emacsing.

xterm-mouse-mode

When using Emacs in console mode, sometimes you just want to move the cursor to a location in the buffer. You can hit up, down, left and right key a hundred times to get there or you can just use mouse. But how? The xterm-mouse-mode will do the trick. Read this,

26.20 Using a Mouse in Terminal Emulators

Some terminal emulators support mouse clicks in the terminal window.

In a terminal emulator which is compatible with xterm, you can use M-x xterm-mouse-mode to give Emacs control over simple use of the mouse—basically, only non-modified single clicks are supported. The normal xterm mouse functionality for such clicks is still available by holding down the SHIFT key when you press the mouse button. Xterm Mouse mode is a global minor mode (see Minor Modes). Repeating the command turns the mode off again.
In the console on GNU/Linux, you can use M-x t-mouse-mode. You need to have the gpm package installed and running on your system in order for this to work.

The original mouse copy-and-paste is still available by holding down the SHIFT plus left mouse button. I didn't know this until I turned to this page of the manual. It is proven that reading manual is helpful to your skill. :)

2008-09-28

Emacsclient in Scratchbox

I am using Emacs to program both at work and home. When I found I can't use Emacsclient inside ScratchBox, although, it is claimed OK in the documentation, I felt a bit surprised. The error reads,
[sbox-DIABLO_ARMEL: /tmp] > emacsclient emacsclient-1.c
Can't stat /tmp/esrv1000-yenliang-laptop
[sbox-DIABLO_ARMEL: /tmp] > ls /tmp/esrv1000-yenliang-laptop
ls: /tmp/esrv1000-yenliang-laptop: No such file or directory


Obviously, the socket file /tmp/esrv1000-yenliang-laptop is not created by Emacs server. I looked into the source code of Emacsclient shipped from Emacs and found that the socket file it is looking for is /tmp/emacs1000/server as indicated by the code below.

sprintf(socket_name, "/tmp/emacs%d/%s",
(int)geteuid(), server_name);


But, the code in ScratchBox version is,

sprintf(server.sun_path, "/tmp/eserv%d-%s",
(int)geteuid(), system_name);

I think if I change the socket file path above to match the one used by Emacs server and compiled a new emacsclient, it should work. And it does work.



The screenshot above says it works like a charm, though, the Emacs started the server is version 22.2, not CVS version. The emacsclient fails to connet to the server created by CVS version. My guess is that the CVS version returns the different ID as the client initializes the connection.

Another way you can try is to create a symbolic link at the home directory of the host pointing to the home directory in the ScratchBox system.
ln -s /scratchbox/users/$USER/home/$USER sbox

2008-09-16

Customize Emacs-Gtk menu bar, tool bar, scroll bar and the dialogs

If Emacs was built to use the GTK widget set, then the menu bar, tool bar,
scroll bar and the dialogs are customized with the standard GTK
customization file, ~/.gtkrc-2.0, or with the Emacs specific
file ~/.emacs.d/gtkrc. We recommend that you use
~/.emacs.d/gtkrc for customizations,
since ~/.gtkrc-2.0
seems to be ignored when running GConf with GNOME. These files apply
only to GTK widget features. To customize Emacs font, background,
faces, etc., use the normal X resources.



See http://www.gnu.org/software/emacs/manual/html_node/emacs/GTK-resources.html#GTK-resources for more details.

2008-06-21

在Cygwin/Emacs中使用中文

在Cygwin環境中的 .inputrc 檔,將下面幾行設定一下,
set meta-flag on
set convert-meta off
#set input-meta on
set output-meta on
接著江下面幾行加入你的.emacs檔中
(set-language-environment 'chinese-big5)
(set-keyboard-coding-system 'chinese-big5-dos)
(set-selection-coding-system 'chinese-big5-dos)
(set-terminal-coding-system 'chinese-big5-dos)
有圖有真相,

2008-06-20

Multi-TTY Emacs

身為一個Emacs的使用者,而且是習慣在文字模式下工作的人,應該常常會想是否在Terminal也可以使用Emacs的Server/Client編輯模式呢?答案是肯定的。由於Multi-TTY這個分支已經正式導入Emacs的主要CVS中,所以只要利用下面的指令就可以check out出來編譯。

cvs -z3 -d:pserver:anonymous@cvs.savannah.gnu.org:/sources/emacs co emacs
而且編譯成功後更可以利用screen這個可說是terminal的window manager來啟動一個內含emacs server的screen daemon。如下,

screen -dmS emacs emacs -nw
emacsclient -t YourFileName



2007-07-29

折疊隱藏檔案之內容

有時候,一個檔案內的function實在太多了,而你想要先看一下整個檔案的大略情況時,Emacs提供了一個功能
set-selective-display。這個功能將每一個縮排超過指定的數字的行全部隱藏起來。為了方便起見,可以用下面的指令:

M-1 C-x $
上面的 M-1 是將1這個數字傳入C-x $中,也就是set-selective-display中。


2007-07-28

Qt Programming - 發展環境

一般習慣在Windows環境工作的程式設計師常會使用IDE這類工具來編輯程式。使用與否,說實在的我個人認為並沒有甚麼好或不好,端看個人使用習慣。只是老實說在Unix/Linux下的Qt/IDE環境實在是凡善可陳。雖然是有Kdevelop這個名氣大的工具,不過我個人還是喜歡樸實一點(可能是鄉下俗吧)。因為,我是慣用Emacs的,所以當初在尋找可方便編輯Qt程式的Editor時,就只尋找有關於Qt/Emacs的資料。

雖然,KDE這個Team做了Kdevelop這個東西,但是鮮為人知的是他們也另外為了Emacs做了一個叫做kde-emacs的package。從他的名字可以知道,他是專門替在Emacs下寫Qt/KDE程式的工程師設計的。感動吧!
這套emacs的擴充套件可以從kde-sdk中取得。

此外,為了讓在Emacs下寫程式的工程師效率更高,另外也有一個好東西要介紹一下,那就是ECB了。顧名思義,就是Emacs Code Browser。若是將這兩套東西搭配起來用,其實我個人認為是遠超過商用IDE介面的。而且是跨平台。只要有Emacs的地方就可以用。

上圖就是整合起來的畫面。Cool? 用用看吧,你會喜歡他的。