Monthly Archive for September, 2008

Colorize A 256-Color Mutt

Mutt 是一個我很愛用的 Mail Client,除了平常在用的 GMail 外,其他幾乎都是由它包辦,其實之前就知道 Mutt 支援 256-Color,只是一直沒有時間好好調校,昨天趁著颱風天的下午,在 FreeBSD 上把 Mutt 的 256-Color 設定好了。

在預設的 Mutt color setting 中,單純使用常用的 16-Color 就是用 black, green, red, blue, …, etc 這類的顏色詞來設定;但是在 256-Color 的環境下,必須使用 color0, color1, …, color254, color255 來做設定,關於顏色的對照表,可以參考這邊

這邊提供一下我的設定 (看是要放到 $HOME/.muttrc 或是另放一個檔案,再從 $HOME/.muttrc include 進去):

color normal            default         default         # normal text
color indicator         color214        color237        # actual message
color tree              color99         default         # thread arrows
color status            color118        color237        # status line
color error             color196        default         # errors
color message           color196        default         # info messages
color signature         brightblack     default         # signature
color attachment        brightblack     default         # MIME attachments
color search            brightyellow    red             # search matches
color tilde             brightmagenta   default         # ~ at bottom of msg
color markers           red             default         # + at beginning of wrapped lines

color hdrdefault        color33         default         # default header lines
color bold              red             default         # hiliting bold patterns in body
color underline         green           default         # hiliting underlined patterns in body

color quoted            color107        default         # quoted text
color quoted1           color66         default
color quoted2           color32         default
color quoted3           color30         default
color quoted4           color99         default
color quoted5           color36         default
color quoted6           color114        default
color quoted7           color109        default
color quoted8           color41         default
color quoted9           color138        default
# header
color header            color205        default         "^(From|Subject|To|Cc|Bcc):"

# body
color body              color214        default         "(http|https|ftp|news|telnet|finger)://[^ ]+"
color body              color81         default         "[-a-z_0-9.+]+@[-a-z_0-9.]+"
color body              red             default         "(^| )\\*[-a-z0-9*]+\\*[,.?]?[ \n]"
color body              green           default         "(^| )_[-a-z0-9_]+_[,.?]?[ \n]"

# index
uncolor index *         # unset all color index entries
color index             brightgreen     default         ~F      # Flagged
color index             color74         default         ~N      # New
color index             color169        default         ~T      # Tagged
color index             brightblack     default         ~D      # Deleted

這樣設定完成後,如果你跟我一樣使用 $TERM=xterm,沒意外的話會噴出這樣子的訊息:

Error in /home/yzlin/.mutt/color/color256, line 9: 214: color not supported by term
Error in /home/yzlin/.mutt/color/color256, line 10: 99: color not supported by term
Error in /home/yzlin/.mutt/color/color256, line 11: 118: color not supported by term
Error in /home/yzlin/.mutt/color/color256, line 12: 196: color not supported by term
Error in /home/yzlin/.mutt/color/color256, line 13: 196: color not supported by term
Error in /home/yzlin/.mutt/color/color256, line 20: 33: color not supported by term
Error in /home/yzlin/.mutt/color/color256, line 24: 107: color not supported by term
...

會出現這樣的問題,是因為 Mutt 使用到 ncurses,會判斷 termcap 中,你所使用的 TERM (在此是 xterm) 是否支援 256-Color,如果不支援那麼高,就會噴出這樣子的訊息,這時候有人就會把 TERM 設成 xterm-256color,如此一來,就會成功!?很遺憾的,照樣噴 XD,在 Linux 下,可以單純只設成 xterm-256color,但是在 FreeBSD 下,卻不行!

為何會有如此的差異?其實這個問題帶有一點點歷史的包袱,FreeBSD 預設使用的是 termcap,而 Linux 多半使用的是 terminfo,也因為如此,FreeBSD termcap 會受限於單一 entry 字元數必須小於 1024 個的限制,如此一來,沒辦法將全部的 feature 都包含進去,xterm-256color 就是一個例子,在 /usr/share/misc/termcap 中有一段說明:

# These aliases are for compatibility with the terminfo; termcap cannot provide
# the extra features, but termcap applications still want the names.
xterm-16color|xterm alias 1:tc=xterm-xfree86:
xterm-88color|xterm alias 2:tc=xterm-256color:
xterm-256color|xterm alias 3:tc=xterm-xfree86:

從說明可以看到 xterm-256color 其實引入的是 xterm-xfree86,再詳細去看,xterm-xfree86 當中並沒有 Co 的設定 (Co 指的是 “Maximum number of colors on screen”,相當於 terminfo 中的 colors),而是引入 xterm-basic 的設定,當中只支援 Co#8,這也是為什麼 xterm-256color 在 FreeBSD 中並不是真的支援 256 色。

若要知道目前使用的 TERM 所支援的 colors,可以使用:

tput Co

注意不是 co 而是 Co,co 是 columns 的意思。

既然無法針對 termcap 下手,那究竟該如何設定使用 Co#256 呢?好在我們可以自行設定一個環境變數 TERMCAP,來強制指定,在 $HOME/.cshrc 設定:

setenv TERMCAP 'xterm|xterm-color:Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm:tc=xterm-xfree86:'

由上面的設定,可以看到我們覆寫了 Co、AB、AF,這樣的設定跟 .screenrc 當中的設定很像,AF 指的是 foreground color 的表示方式;AB 則是 backgound color 的表示方式。設定完成如果要確定是否成功,可以利用 tput 去檢查 Co,出現 256 便是表示成功了,接著在這樣的環境下執行 Mutt,沒意外會看到這樣子的畫面:
mutt-256color

Zsh – autocomplete error

最近開始在嘗試一套蠻強大的 shell – zsh,我用過的 shell 沒有很多,最常用的還是 tcsh & sh,bash 以前有用過一陣子。試用了一陣子,覺得它真的蠻強的,可以把 prompt 的介面和 auto-complete 的功能弄得很炫。改天再發篇文寫一下自己的設定心得。

這篇主要是講一下之前遇到的一個問題,在 auto-complete 的設定下,會出現:

_alternative:69: command not found: _canonical_paths

雖然好像可以正常運作,但就是覺得怪怪的,後來 google 了一下,發現了別人的解法,原來是 $HOME/.zcompdump 爛掉,把它砍掉,讓 zsh 自動重建就行了。