當 jQuery().ajax() 遇到 ASP

利用 jqGrid 新增中文欄位資料時,到伺服器端時,就變成亂碼了。
請 FireBug 大神幫忙,發現 request header content-type 的編碼是 utf-8,查過jqGrid的 source code,裡面也只是調用 jQuery 的 ajax 函數而已。
照理來說,應該可以用 $.ajaxSetup() 來修正,但試了好一陣子,發現沒辦法,即使我在 contentType 裡指定了 charset=big5,最後送出時,仍然會是 utf-8…

好吧,山不轉路轉,再拜請Google大神,發現有人利用 escape() 解,也就是先用 javascript escape() 編碼,server 端再解碼,這樣就解了。
大致的代碼是這樣:

//
$("#jqGrid2").jqGrid(
// ... 略 ...
).navGrid( "#pager2", {
// ... 略 ...
add:true,
addfunc: function() {
$("#jqGrid2").editGridRow( "new", {
url: "server.asp",
beforeSubmit: function( postdata, o ) {
var s = postdata[ "your_field_name" ];
var ret=[true, "", ""];
postdata[ "your_field_name" ] = escape( s );
return ret;
}
} );
return false;
}
} );

Xming 的字太小?

Xming 等同是 Windows 上的 X Server,使用的說明可以參考:Xming 簡易使用說明,圖文並茂,寫的非常好。
對我來說,唯一的問題是字太小,該怎麼解決?我找了好久~
最後終於意外發現,只要在 XLaunch 最後一個步驟的畫面的 “Additional parameters for Xming” 裡填上 -dpi 100,就可以解決字太小的問題。

vim auto completion

在Linux Today看到這篇文章:Vi and Vim Editor: 5 Awesome Examples For Automatic Word Completion Using Ctrl-X Magic,看完的當下,非常的高興。
原本以為要安裝特定 plugin 才能達到的 auto completion 功能,居然早已內建,而且只需要在編輯時按下 ctrl+x ,再搭配下列按鍵即可:

  • ctrl+p:向前找可以自動完成的字
  • ctrl+n:向後找
  • ctrl+i:找所有單行開頭的,特別適合寫程式,因為通常函數都會是先以單行宣告,根據同事實驗,這還會去找 header 裡的…
  • ctrl+f:檔名的自動完成

能說什麼呢?只能說Vim太威了!

trinity.vim

前兩天想說找找看 Vim 有沒有 GNU Global 的 plugin 時,看到了 trinity.vim。
這個 plugin 整合了三個 plugin:Source explorer, taglist, NERD tree 在 Vim 模擬出接近 Source Insight 的效果。
有用過 Source Insight 的人,相信都知道很難找到替代品,我前幾年也曾經試圖找過,但都一直沒找到。
這個 plugin 真的不錯,讓Vim更好用了~
有優點當然也有缺點:

  1. Source explorer 裡呼叫 ctags 的地方是寫死的,所以如果你的 ctags 放在別的地方,最好自行去搜索有呼叫到 ctags 的地方,加上路徑。
  2. 把 Source explorer 打開的時候,速度會變慢,這是因為它試圖利用 ctags 資料庫去找跟游標所在位置有關的程式片段。我自己是比較少用,不能用對我影響不大。

以 bash script 為 sqlite database 產生 C/C++ struct

最近寫的一個 bash script,用來幫你把 sqlite database 裡的 table schema 轉成 C/C++ struct。
這裡只處理 text 與 integer 型態,text 轉成 wstring/string,integer 則轉為 int。轉換的工具是使用 awk/sed。

只要稍稍改動一下,也適用在其他語言上。

#!/bin/bash
if test -z "$1"
then
echo "You need to specify the database file name."
exit -1
fi
db=$1
dbname=`basename $1 .db`
tables=`sqlite3 $db ".table"`
output="db_${dbname}_schema.h"
touch $output
cat >> $output <> $output
sqlite3 $db ".schema $table" | awk '/^\ /{printf("%s %s;",$2,$1);}' | sed -e 's/,/\ /g' | sed -e 's/text/db_string /g' | sed -e 's/integer/int /g' >> $output
struct_name=`echo $table | awk -f cap.awk`
echo "}$struct_name;" >> $output
echo "" >> $output
done
cat >> $output << EOF
#endif
EOF
# call "astyle" to format the code.  Beside "astyle", you can use "indent".
astyle $output
exit 0

Project.vim

同事最近拋棄了 Source Insight 3,開始用 Vim 寫 code,所以有人跟我一起研究了。
他最近介紹我一個 plugin : project – Organize/Navigate projects of files (like IDE/buffer explorer),看起來不錯用,他可以把某個目錄下所有檔案都建立為專案檔,讓你可以快速瀏覽所有檔案,不過美中不足的是他沒有把 CVS、.svn、_svn 等目錄過濾掉,我在 VimDirListing 函數裡添加了兩行把這些目錄過濾掉。
這是原來的樣子:

if isdirectory(glob(fname))
let {a:dirvariable}={a:dirvariable}.a:padding.fname.a:separator
let {a:dircount}={a:dircount} + 1
else
" other stuff

改過以後:

if isdirectory(glob(fname))
if fname != 'CVS' && fname != '.svn' && fname != '_svn'
let {a:dirvariable}={a:dirvariable}.a:padding.fname.a:separator
let {a:dircount}={a:dircount} + 1
endif
else

如果你懶得看英文的話,對岸的朋友翻譯了該 plugin 的文檔:VIM-Project Plugin – I Know you Know – C++博客

Ubiquity command – findbook(更新)

上一版沒有對 uri 作 encoding,所以找中文時會出錯,現在補上,只要呼叫 Ubiquity 內建的 encodeURIComponent() 就行了…

CmdUtils.CreateCommand({
name: "findbook",
contributors: ["elleryq"],
license: "MPL",
description: "讓買書變成更簡單的決定!",
takes: {"書名關鍵字": noun_arb_text},
icon: "http://findbook.tw/favicon.ico",
execute: function(directObject) {
Utils.openUrlInBrowser( "http://findbook.tw/search?keyword_type=keyword&q=" + encodeURIComponent(directObject.text) );
}
});

Ubiquity command – findbook

Ubiquity已經很多人介紹了,這裡是分享一個 commands,如果你有用 Findbook 的話,可以用用看…
安裝 command 方法很簡單,打開 Ubiquity Command Editor 後,把下面的程式碼貼上去即可:

CmdUtils.CreateCommand({
name: "findbook",
author: { name: "elleryq"},
contributors: ["elleryq"],
license: "MPL",
description: "讓買書變成更簡單的決定!",
takes: {"書名關鍵字": noun_arb_text},
icon: "http://findbook.tw/favicon.ico",
execute: function(directObject) {
Utils.openUrlInBrowser( "http://findbook.tw/search?keyword_type=keyword&q=" + directObject.text );
}
});