Cocoa Emacs: clean up defaults stored by some earlier versions!

I found my Cocoa Emacs defaults to use some of my old color-theme settings, noticably, black background.  This is even the case when I run it with `emacs -q'!! wtf... 

Hunting down for hours, I found that it was due to NSUserDefaults.  Some earlier version(possibly i built from cvs/bzr repo) of Cocoa Emacs seemed to store some user settings to standardUserDefaults, and now it is not saving this any more. If you encounter a similar problem, you can verify by: (say foreground, background are messed up)

  (ns-get-resource nil "Foreground")  
  (ns-get-resource nil "Background")

To remove that immediately,

  (ns-set-resource nil "Foreground" nil)  
  (ns-set-resource nil "Background" nil)

To clean up all previously stored defaults, add a line before last `return' in nsfns.m/x_get_string_resource:

  Fns_set_resource (Qnil, build_string(toCheck), Qnil);

Now the world becomes sane again.  I was thinking of reinstalling snow leopard, now i do not have to... 

convert-standard-filename, file-truename, expand-file-name

See an example, do you know what they eval to?

Real File Name: 
  "c:/Documents and Settings/wixu/.scratch"

  (convert-standard-filename "~/.SCRATCH")
  (file-truename "~/.SCRATCH")
  (expand-file-name "~/.SCRATCH")

1. expand-file-name: *only* looks at part that needs to expanded, leaving other
   part alone.

2. file-truename: return abosulute, even following symbolics.

3. convert-standard-filename: defined in w32-fns.el.  Mainly for
   replacing/quoting invalid characters, like `/'.

So here are our results:
  
  (convert-standard-filename "~/.SCRATCH")
     => "~\\.SCRATCH"
  
  (file-truename "~/.SCRATCH")
     => "c:/Documents and Settings/wixu/.scratch"
  
  (expand-file-name "~/.SCRATCH")
     => "c:/Documents and Settings/wixu/.SCRATCH"
  
  (convert-standard-filename (file-truename "~/.SCRATCH"))
     =>"c:\\Documents and Settings\\wixu\\.scratch"

Super Intuitive way to highlight changes for some buffer

Highlight_changes_in_twitterin

I'm doing irc, twitter, etc. inside emacs.  When there are new messages coming up in these buffers, it is a bit uneasy for me to find out what is new there.  Now with the help of highlight-changes-mode, we can easily decorate new message differently from read messages, like giving unread messages a different background.  Take a look!

Isn't it SUPER nice?  You only need this:

    ;; ,----
    ;; | Track cahnges for some buffer
    ;; `----
    (defadvice switch-to-buffer (before
                                 highlight-changes-for-some-buffer
                                 activate)
      (when (memq major-mode (list 'erc-mode 'twittering-mode))
        (let ((buffer-read-only nil)
              (inhibit-read-only t))
          (highlight-changes-mode -1)
          (highlight-changes-mode 1))))

ERC 使用简介

_2010-03-20_11

ERC, emacs irc client, 即是 emacs 里登录 irc 的客户端。irc 是什么?
internet relay chat, 简单地说,就是用于群聊的。一帮无聊的 geeks 成天没
事干,就在上面灌水,所以上面的 channel 技术类的占绝大数,比如 emacs,
c++, debian, scheme 等等。

好,开始 ERC 之旅,先让我们登录到 #emacs-cn 上去:

a) M-x erc-select

   host: irc.debian.org
   port: 6667 到 7000 随便用一个
   username: 先随便填一个,比如 foo 
   password: 还没注册的可以直接回车

b) 这时候你就进入了一个名字类似 "irc.debian.org:6669" 的 server
   buffer. 接着执行:

     ERC> /join #emacs-cn

   就进入 #emacs-cn channel 啦!

c) 如何注册

   切换到刚才那个 server buffer, 输入:

     ERC> /msg nickserv help register

   按着提示一步一步来就行,根据 irc server 的不同,有些会需要你用有效邮
   箱来确认一下。注册 id 的好处就是,这个 id 就不会被别人抢走了,比如有
   人已经用 foo 登录了,但这被你注册了,你连进去的时候,就能把名字抢过
   来,而对方可能会变成 foo` 之类的别名。

以上其实对于所有 irc client 来讲都是通用的。接下来是我的 ERC 一些配置
供参考:

1. 基本设置

   编码,尽量 utf-8 (#emacs-cn 也是用 utf-8):

     (setq erc-default-coding-system '(utf-8 . utf-8))

   如果某个 channel 是别的编码,也可以单独设置,例如国内某个 irc
   server 上 #linuxfire 就是用 gbk 编码,我们单独为它设置:

     (setq erc-encoding-coding-alist '(("#linuxfire" . chinese-iso-8bit))

   设置 nick, 全名: nick 就是登录时用的,full name 是别人查询你的
   时候显示的信息。(类似BBS 的 C-a )

     (setq erc-nick "xwl"
           erc-user-full-name "William Xu")

2. 登录后自动加入预定的 channels

     (erc-autojoin-mode 1)
     (setq erc-autojoin-channels-alist
            '(("oftc.net"                 ; debian.org 是它的别名
               "#debian-zh"
               "#emacs-cn")))
 
3. 对于已经注册的 ID,我可不想每次都要输入密码,用下面的方法可自动认证:
   (pwerc 存的是你的密码)

     (defun xwl-erc-auto-identify (server nick)
       (erc-message "PRIVMSG"
                    (format "NickServ identify %s" pwerc)))
    
     (add-hook 'erc-after-connect 'xwl-erc-auto-identify)

4. 过滤信息

   如果你对某些消息或者某个人说的话特别感兴趣,我们可以通过关键字匹配
   对相关信息进行高亮。例如:

     (erc-match-mode 1)
     (setq erc-keywords '("emms" "python"))
     (setq erc-pals '("rms"))

  相反地,如果你对某些消息不感兴趣,比如有人进来啦,有人出去啦,如此这
  般一下就不会看到了:

     (setq erc-ignore-list nil)
     (setq erc-hide-list
           '("JOIN" "PART" "QUIT" "MODE"))

5. 新信息提醒

   信息一般可分为三种:

   1) 某人悄悄跟你说话(即所谓的 private message),这会打开一个新小窗,
      即 buffer.

      ERC>/msg NICK how are you doing

   2) 某人公开地跟你说话,即别的在 channel 里的人也能看到。一般来说,
      习惯用 nick 加 `:' 表示。(要输入某人 nick 的时候,首字母加 TAB
      就能帮你补全,一次不行,多 TAB 几次可以选择)

      <xwl> ahei: 你可以 match regexp,
  
   3) 别的情形。

   ERC 会通过 erc-modified-channels-object 来设置 mode line,提示有新
   消息,类似:

       [#o: 38, #emacs-cn: 5]

   为什么要区分以上三种情形呢? 因为我们可以对不同信息,用不同的颜色在
   mode line 来提示,这样方便你决定是不是要及时地去查阅这条消息。

   ERC 本身只在 mode line 提示新消息,如果你切换到别的程序去了,比如在
   firefox 里看 ppmm,还想被提醒的话,可以借助一些外部工具来实现。mac 下
   用 growl,linux 可以用 zenity,windows 不知有什么类似工具? 给个例子:

      (defun xwl-erc-text-matched-hook (match-type nickuserhost message)
       "Shows a growl notification, when user's nick was mentioned.
     If the buffer is currently not visible, makes it sticky."
       (when (and (erc-match-current-nick-p nickuserhost message)
                  (not (string-match (regexp-opt '("Users"
                                                   "User"
                                                   "topic set by"
                                                   "Welcome to "
                                                   "nickname"
                                                   "identified"
                                                   "invalid"
                                                   ))
                                     message)))
         (let ((s (concat "ERC: " (buffer-name (current-buffer)))))
           (case system-type
             ((darwin)
              (xwl-growl s message))))))
    
     (add-hook 'erc-text-matched-hook 'xwl-erc-text-matched-hook)
    
     (defun xwl-growl (title message)
       (start-process "growl" " growl" growlnotify-command title "-a" "Emacs")
       (process-send-string " growl" message)
       (process-send-string " growl" "\n")
       (process-send-eof " growl"))

    
6. 时间戳

     (erc-timestamp-mode 1)

   下面这个变量可以控制时间戳的显示方式,比如位置什么的,默认值:

     (setq erc-insert-timestamp-function 'erc-insert-timestamp-left)

7. log

   我们可以将 channel 里的聊天记录都保存下来,方便日后查询,或者有时候
   你的 emacs 突然挂掉的时候,还能找到挂之间有没有人对你说了什么。

     (require 'erc-log)
     (erc-log-mode 1)
     (setq erc-log-channels-directory "~/var/erc/"
           erc-save-buffer-on-part t
           erc-log-file-coding-system 'utf-8
           erc-log-write-after-send t
           erc-log-write-after-insert t)
    
     (unless (file-exists-p erc-log-channels-directory)
       (mkdir erc-log-channels-directory t))
    

最后,ERC 上 irc 还是蛮舒服的,因为所有的、你熟悉的 emacs 编辑命令
都在那里! 国内 irc 用户还是少了点,对岸台湾倒是蛮多的。大家有空就
上来玩吧~ 要是想看我的配置可以在 github.com 上看我的配置文件:

  http://github.com/xwl/xwl-emacs-config/blob/master/.emacs.d/site-lisp/config/xwl-erc.el

贴个图:

Build emacs with image support on w32

Should not be difficult, still some notes.  

General build steps: 

  C:\> cd nt
  C:\> configure --no-cygwin --cflags=-Ic:\gnuwin32\include --ldflags=-Lc:\gnuwin32\lib
  C:\> make
  C:\> make install

Usually, you will see it complaining missing some image libraries.
Let us fix it:

1. Install image libraries from: 

2. Notice the cflags, ldflags in the above configure command.  

3. xpm problem: missing simx.h.  Actually xpm library itself doesn't
   depend on simx.h, only when compiling emacs...  So download one
   from here: 

4. After `make install', make sure "c:\gnuwin32\bin" is included in
   %path%, or you will get error like invalid image format, etc.  

Cheers! 

about Tibet

Tibet became a province of China for a few hundred years. A hundred years ago, the people who destroyed the last dynasty of China and united the nation (the Nationalist Party which is the current ruling party of Taiwan, the Communist Party, and a bunch of democratic parties currently in mainland China) originally wanted to take the 18 original-Chinese provinces back from Manchu people (the ruling civilization of China's last dynasty), excluding Tibet etc. 

That's originally. But if you know Chinese history, China breaks up into some 2 to 10+ countries every 250-400 years after its first unity. And the final goal and hope of every scholar and power are to unite the country. In those countries, many are formed by non-Chinese civilizations, and somehow, they also share the same goal, unite China, including themselves. Now back to modern history. When Republic of China was still fighting to unite the country, Tibet decided to join Republic of China. It break away again when the communist party come in power. But being in China for a few hundred years, most Chinese already see it as part of the country (and China have a lot of civilizations living in their own place within China all the time, so we're also used to that). So basically, the communist party and even Republic of China see it as part of the country. It's more like "unite the country" instead of "invade it". Note that Republic of China (Taiwan) does not recognize the independence of Mongolia until a few years ago, under the very same reasoning, and many Chinese people who know how the history is still very angry about that. Because Chinese already see the Qing Dynasty area as "China". And the rule of Chinese based culture is, a country always have a chance to break up, but must finally be united. 

Anyway, the "liberation" (in Communist term) of Tibet had never been and will never be see as "invading" in China, so I don't think we'll ever see that as "lose face". It's never in Chinese question. Also, it IS a peaceful action. In fact, a very peaceful one. How did the government of the then Tibet and Dalai Lama remained safe after the communist's conquer? Because they were just forced out of the palace and power but were not hurt. It was a war to unite another piece of land in Chinese view, and this is probably as peaceful as it could have been. 

No, I'm not communist. No I'm not from mainland. But I know the common Chinese view better than you. Don't use western view when trying to interpret other culture's history. It simply doesn't work because people don't think the same way as you.

// 说得真透彻。。太了解中国了,此老外。

Trim erc nicks

Bored.  Gotta find something to do.  So after trimming "From" in Gnus, I would like to trim erc nicks as well.  Some badGuyLongLongNick just screws up my erc.  Stop now.

;; trim erc nicks
(setq erc-format-nick-function 'xwl-erc-format-nick)

(defun xwl-erc-format-nick (&optional user channel-data)
  "Like `erc-format-nick' but trim nick to a fixed length. "
  (let ((nick (erc-format-nick user channel-data)))
    (when (> (length nick) 7)
      (setq nick (concat (substring nick 0 4)
                         ".."
                         (substring (substring nick 7) -1))))
    nick))

Now, badGuyLongLongNick will be cut down to: badG..k.