Search This Blog

Tuesday, March 02, 2010

how to make a div tag fill the remaining height of its parent tag?

It is a common problem I think, but the solutions that can be found via google are somehow too   ugly, however, like an absolute position or table-like display(see css property position and display).

 Finally, I found that css3 gives a perfect solution: with a {display:box} and {box-orient:vertical} applied to the parent tag and {box-flex: 1} applied to the greedy div tag. If you are using chrome/chromium, replace box with -webkit-box, box-orient with -webkit-box-orient and box-flex with -webkit-box-flex, elif firefox, google it, elif IE, forget it.

Sunday, February 21, 2010

make ipython work better with emacs

ipython is an excellent python shell to be used with emacs. ipython.el is the emacs extension which communicates with ipython on behalf of emacs.

When ipython is activated in emacs, we can complete functions, attributes, variables or something else with the TAB key, if there are more than one possible completions, a new buffer  will created to show them. However, the new buffer can not automatically disappear just as in slime does, so I make some modifications to ipython.el.

1. create a new file named tempbuffer.el with the following elisp source code as content:

(defvar saved nil)

(defun restore-after-temp-buffer ()
  (remove-hook 'pre-command-hook 'restore-after-temp-buffer)
  (when saved
    (progn (set-window-configuration saved)
           (setf saved nil))))

(defun delay-restoring-after-temp-buffer ()
  (add-hook 'pre-command-hook 'restore-after-temp-buffer))

(defmacro with-output-to-restorable-temp-buffer (bufname &rest body)
  `(progn (setf saved (current-window-configuration))
          (with-output-to-temp-buffer ,bufname
            ,@body)
          (delay-restoring-after-temp-buffer)))

(provide 'tempbuffer)

2. put tempbuffer.el into somewhere emacs can find.

3. add (require 'tempbuffer) to ipython.el and replace with-output-to-tempbuffer with with-output-to-restorable-temp-buffer.

after done this, the new buffer showing possible completions will close as soon as we type something in emacs.

Tuesday, February 16, 2010

一个关于macro的故事

很久很久以前,有一个由lisp程序员组成的公司。这个公司所处的时代太早,以至于当时的lisp还没有macro。每当有不能用函数来定义或者特殊操作符来实现的东西,就得全部手工写,这是相当无聊的事。不幸的是,这个公司的程序员虽然都很有才华,同时却又灰常懒。在写程序的时候,当他们觉得写一长串代码实在太麻烦了,就会在程序里写上描述他们实际上所需要写的代码的记录。更不幸的是,这些人实在太懒了,所以他们也痛恨回过头去完成那些记录所描述的代码。没过多久,公司就拥有了一堆没人能运行地起来的程序,因为这些程序充斥着关于未完成的代码的记录。

大老板很绝望,请了一个名叫Mac的不那么牛逼的程序员,让他从程序里找出记录,写出所需的代码,然后把代码插入程序以代替记录。Mac从来不运行那些程序,因为很显然,程序还没完成,所以他没法运行。然而就算程序完成了,Mac也不知道应该给它们提供什么输入。所以他只是根据记录的内容写出代码,然后发给原作者。

在Mac的帮助下,很快所有的程序都完成了,公司卖掉这些程序,赚了一大笔钱,足够把开发人数加倍。但是不知道怎么回事,没人想到要再雇一个人帮Mac;很快,出现了Mac一个人协助一大坨程序员的状况。为了避免把所有的时间都花在找记录上,Mac对程序员们所用的编译器做了一点改动,这样,任何时候编译器遇到一个记录,就会通过email发给Mac,然后等Mac把代码发回来代替记录。悲剧的是,即使这样,Mac也很难赶上这么多程序员的进度。他尽可能认真地工作,但是有时候——尤其是记录写得不是很明确的情况下——他会犯错误。

然而程序员们注意到,如果他们的记录写地越精确,那么Mac就越能发回正确的代码。有一天,一个程序员写记录写得很痛苦,于是在一个记录里写了一段能生成他所需要代码的lisp代码。这下爽了Mac,他运行了这段代码,然后把结果送给编译器。

下一个创新是,一个程序员在他的一个程序里放了一个记录,包括一个函数定义和一段注释:"Mac,这里不用写代码,留着这个函数就行,我在别的记录里要用到这个函数。"这个程序里别的记录则是这样的:"Mac,用符号x和y作为参数运行刚才那个函数,返回的结果替代这个记录。"

这项技术流行地很快,没过几天,大部分程序都包含了很多定义函数的记录,而这些函数在其他记录中被使用。为了让Mac容易挑选只包含定义而不需要马上响应的记录,程序员们为这种记录加上了标准的开头:"Definition for Mac,read only",而这帮程序员又太懒,所以很快缩短为"DEF.MAC R/O",接着又变成"DEFMACRO"。

没过多久,给Mac的记录里已经没有真正的英语了。他每天所做的事就是接受、回复来自编译器的邮件,这些邮件是包含DEFMACRO的记录或者是对DEFMACRO里的函数的调用。既然记录里的lisp代码做了真正的工作,那么赶上email的进度就没那么困难了。Mac突然有了大把的时间可以坐在办公室里做着关于白色沙滩、蓝色海水和放着小纸伞的饮料(貌似是鸡尾酒?)的白日梦。

几个月以后,程序员们意识到已经有段时间没人见到过Mac了。当他们去了他的办公室,发现所有的东西上都蒙了一层薄薄的灰尘,桌子上放着一堆关于热带地区旅游的小册子,而电脑是关着的。但是编译器正常工作着啊,怎么可能呢?最后他们发现Mac给编译器做了在最后一个改动:编译器不再用email给Mac发记录,而是把DEFMACRO里定义的函数保存下来,当其他记录调用的时候就运行这些函数。程序员们觉得木有必要告诉大老板Mac再也不用来办公室了,所以Mac至今还领着薪水,时不时地从各种不同的热带场所给程序员们发张贺卡来。

*****************************华丽的分割线***************************

这个故事是《practical common lisp》第八章讲到的,我翻译地不好,不过领会精神是够了。

先看不相关的,懒惰是美德。故事里的人物,各个都懒得出奇,精英程序员们太过牛逼,以至于写代码都懒,直接写上伪代码了事。Mac虽然是junior programmer,但是一旦抓到机会,就会想到办法让机器帮他做事。而大老板,作为一个万恶的资本家,竟然连自己花钱雇用的工人光拿钱不干活也不知道,莫非就是每天打打高尔乎,心情好了就去公司闪现一下?哈哈。但是就是这么一帮人,也能挣大钱,也能创新,事实上,创新恰恰来自于懒惰,值得大家学习。

然后是一点关于macro的东西,主要是针对非科班出身又对编译原理很恐惧的同学。但凡编程语言,都不会对使用者掌握编译原理有明确的要求,但同时,诸如"掌握编译原理对写出正确、高效的代码至关重要"这类的言论也是非常多的。编译是一个灰常可怕的过程,如果说通过那门课程是皮毛的话,那我是只有皮木有毛。这里结合我自己的体会,讲讲macro,达人请无视。

不少童鞋初学编程语言,拿了书上的例子来跑,这样很好,动手才是王道。但是相当多的人,比如说学C,课后习题都做完了,却不知道自己的代码是怎么在机器上跑的,这样就很不好。下面是编译型语言的基本过程:

1 把代码敲入编辑器(editor),保存。
2 调用编译器(compiler)把源代码编译成可执行程序。
3 运行程序。

IDE会把编辑器和编译器以及调试器整成一坨,这样方便了开发者,却不利于初学者搞懂整个过程,以至于有些童鞋连编辑器和编译器都会混淆。在这里顺便鄙视一下某公司的Visual叉叉叉,我有一次试图写个程序,却被一大堆的菜单彻底弄晕了,以至于我至今不会用visual叉叉叉写程序。

而编译再分成几步,以C为例:

1 头文件插入,宏替换
2 词法分析
3 语法分析
4 中间代码生成
5 生成汇编代码
6 汇编,生成目标文件
7 链接,生成可执行文件

很多童鞋搞不清宏到底是怎么回事,从上面的过程可以看到,宏替换发生在第一步(严格来说,这一步不算在编译里),而你在代码里所写的函数是在你执行了第七步里生成的可执行文件以后才会被调用,这就是一个运行时的概念。所以宏和其他的代码的区别就很明显了:宏只在编译之前存在,编译以后,宏被展开,变成具体的代码,自己也消失了。

因为事实上C的macro基本上只是简单的替换,而common lisp的macro更为强大,所以主要还是八卦一下common lisp的macro吧。

其实开头的故事把macro和function的区别讲得很清楚,Mac没法运行程序,因为他工作的阶段是编译时,更确切地讲是宏展开时,而不是运行时,所以整个程序运行所依赖的环境还没建立起来,所以他根本不知道该给函数什么样的输入。而其他的精英程序员们后来在DEFMACRO里的函数其实就是macro,作用就是生成代码,Mac在调用DEFMACRO里的函数的时候,给的参数是精英程序员指定的,比如x和y,这两个参数不是来自于运行时环境,当编译完成后,DEFMACRO里的函数也就没有用了,因为它们已经根据参数生成了具体的代码,在运行时执行的则是具体的代码。大部分情况下,参数都是符号,而不是具体的变量,所以macro其实会涉及到很多的符号计算。


很多人说common lisp的macro太强大了,以至于跟C的macro根本不是一个东西。其实我还不是很理解这种说法。就我自己的体会,common lisp的macro强大在defmacro有整个语言的核心的支持,而C的macro并不作为编译器的一部分存在,而只是预处理器,并且也没有C语言的核心的支持,只有很简单的计算能力。至于在内部实现上,暂时我还没这个能力去探究,任重而道远啊。

Wednesday, January 20, 2010

use emacs to generate highlighted code

1. set htmlize-output-type to 'inline-css

2. type the code in emacs, with the appropriate mode;

3. run the command htmlize-buffer, a new buffer will rise

4. replace "<body>" and "</body>" with "<div>" and "</div>", and cut the whole "div" (former "body") into editor of blogger (in html mode)

5. preview it. it looks exactly like in emacs.

Tuesday, January 19, 2010

How to set cursor shape using clx

Actually it's just the same as using xlib.

1. open a font named "cursor", it's a collection of different cursor shapes.

2. create a cursor object which can be used in somewhere like :cursor of window or grab-pointer.

example:

(defun create-cursor (char)
  (let ((cursor-font (xlib:open-font *display* "cursor"))
        (black (xlib:make-color :red 0.0 :green 0.0 :blue 0.0))
        (white (xlib:make-color :red 1.0 :green 1.0 :blue 1.0)))
    (xlib:create-glyph-cursor :source-font cursor-font
                              :source-char char
                              :mask-font cursor-font
                              :mask-char (1+ char)
                              :background black
                              :foreground white)))

(defvar *cursor* (create-cursor 68))

(setf (xlib:window-cursor screen-root) *cursor)

the effect is to make the default cursor shape an arrow (indicated by 68).

the char argument of create-cursor indicated the shape, which can be revealed as the screenshot below by running "xfd -fn cursor" in shell.


Wednesday, January 13, 2010

去死吧,spammer们

之前忙考试,没空收拾你们,这次全清理掉了。

to spammers: go back to hell.

Saturday, April 11, 2009

comint-within-quotes in emacs

the function comint-within-quotes tells whether the cursor in quotes by counting the number of quotes from a start position to an end position.
but it can not handle an empty string like this: ""
it counts the first quote but ignores the second one.
and the single quote ' in common lisp has it's special using other than quoting something,oh,well,I mean it quotes something not with a pair but a single one.
so I hope I can have the function count only double quotes when editing common lisp and this is what I've defuned:

(defun comint-within-single-quotes (beg end)
  (let ((countsq (comint-how-many-region "\\(\\=\\|[^\\\\]\\)\'" beg end)))
    (= (mod countsq 2) 1)))
(defun comint-within-double-quotes (beg end)
  (let ((countdq (comint-how-many-region "\\(\\=\\|[^\\\\]\\)\"" beg end)))
    (= (mod countdq 2) 1)))
(defun* comint-within-quotes-2 (beg end &key (quotes :both))
  (case quotes
    (:both (comint-within-quotes beg end))
    (:single (comint-within-single-quotes beg end))
    (:double (comint-within-double-quotes beg end))))  

Friday, September 12, 2008

ipython in emacs with dark background

I use a black bg color theme in emacs,while ipython outputs like this by default:

 
well,some part of the output is black,it sucks
after a travel in the source code of ipython.el and python-mode.el,I found that ipython.el add a "-color lightbg" to command args when start ipython by default,as following:


(unless (member "-colors" py-python-command-args)
(setq py-python-command-args
(nconc py-python-command-args
(list "-colors"
(cond
((eq frame-background-mode 'dark)
"Linux")
((eq frame-background-mode 'light)
"LightBG")
(t ; default (backg-mode isn't always set by XEmacs)
"LightBG"))))))

so I can either modify the py-python-command-args variable or frame-background-mode variable to stop it sucking.
I choose the latter one.
 
BTW:both ways the variable must be set before ipython.el is loaded,for the code above is runned while loading rather than in a function which runned later.

Friday, July 25, 2008

ULK note (1):Segment in linux

According to ULK,linux uses segmentation in a very limit way,namely,linux uses 4 segments,two for user mode and two for kernel mode.Linux also defines macros as segment selectors of these 4 segments:__USER_CS,__USER_DS,__KERNEL_CS,__KERNEL_DS.

I lookup the definition in include/asm-i386/segment.h(crash here for details),let's take a look at __USER_CS and __KERNEL_CS:

#define GDT_ENTRY_DEFAULT_USER_CS 14
#define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS * 8 + 3)

#define GDT_ENTRY_KERNEL_BASE 12
#define GDT_ENTRY_KERNEL_CS (GDT_ENTYY_KERNEL_BASE + 0)
#define __KERNEL_CS (GDT_ENTRY_KERNEL_CS * 8)

well,14(GDT_ENTRY_DEFAULT_USER_CS) is the user data segment descriptor's offset in GDT.

8 is the length of segment descriptor,easy to understand,huh(it's my first thought)?But what is the "+ 3"?

In fact,8 is (1000) in binary,so multiplying 8 left shift GDT_ENTRY_DEFAULT_USER_CS by 3 bits.aha!It's the TI and RPL fields in segment selector!so what the red "+ 3" turns out:it means this segment is in GDT(TI cleared) and it's in user mode(RPL=11).

Similar to __USER_CS,RPL of __KERNEL_CS is set to 00 to indicate that the segment is in kernel mode,so nothing needs to be added.

Friday, July 04, 2008

a note about sbcl and slime

recently I switch to stumpwm,getting it work with sbcl,I enjoyed hacking.
but then I found that when slime read or print Chinese characters,the connection to sbcl will be broken.
Actually,I did set slime-net-coding-system to utf-8-unix,which dosen't help at all.
At last the problem turned out to be due to the coding system of swank side.
as we know,slime connects to a server that was provided by a package named swank.
if I type M-x slime in emacs,everything works well cause emacs runs an sbcl process and set coding system at both side for me,but when sbcl started seperatly by xdm from slime,I forgot to set coding system of swank.
so after I start swank like this:

(swank:create-server :dont-close t :coding-system "uft-8-unix")

I can input Chinese and get correct output at REPL.

Wednesday, April 02, 2008

ssh to remote server without password

  1. install openssh in both local and remote machine;
  2. run ssh-keygen in the local machine,you'll get id_rsa and id_rsa.pub;
  3. well,the file id_rsa stors your private key,you must keep it secret;
  4. while id_rsa.pub is the public key,just keep it from writing by others.
  5. deliver id_rsa.pub to the remote server you want to login without password by whatever ways,e.g. scp .ssh/id_rsa.pub user@server:.ssh/ of course you will be prompted for password this time,make sure there is a .ssh directory in the remote server and it's mode is 700
  6. cat .ssh/id_rsa.pub &gt;&gt; .ssh/authorized_keys (run in remote server)
  7. chmod 644 .ssh/id_rsa.pub .ssh/authorized_keys (also in remote server)
  8. now have a try.
note:when you run ssh server without a username,ssh asumes you want to login to remote server with the same user as the local one.
note2:
if you put your non-root user's public key to /root/.ssh of the remote server,then you can login to remote server as root directory,so either avoid this situation or protect the password of your local account.

Friday, February 22, 2008

a note about autoconf and automake

after several hours,I got autoconf and automake to work on my program,known as following(with autoconf-2.61 and automake-1.10):

1.run autoscan in the source directory,and we can get a file named configure.scan

2.
modify the configure.scan so that it makes sense to our project,

  • first of all is AC_INIT macro,i.e. our project name,version,and contact address;
  • then add a macro AM_INIT_AUTOMAKE (be careful of the red M),fill it with the project name and version,if it does not exist,then aclocal will not work properly;
  • and do not touch AC_CONFIG_HEADER ,it's for people who want make their project portable with a "config.h",so do not feed your project's header files to it;at last comes AC_OUTPUT,set it with "Makefile";
  • other macros are project specified,so we skip them here;
  • rename configure.scan to configure.ac or configure.in
3.run aclocal to generate aclocal.m4(if no aclocal.m4 generated,there must be something wrong)

4.run autoconf to generate configure

5.create a file Makefile.am,

  • set macro bin_PROGRAMS=names(it's the executable file's name,like emacs,mplayer,etc)
  • name_SOURCES=files(it's the sources files whitch the binary relies on)
  • name_LDFLAGS=flags(e.g. if we use expat, add -lexpat here)
many other macros can be useful,too,but omitted here.

6.run automake -a,touch files which it complains can not find.

7.all right,run ./configure now, we can get Makefile automatically,wonderful,isn't it?

Thursday, December 06, 2007

slime-close-parens-at-point

the default key binding of the function slime-close-parens-at-point is C-c C-q
but it's obsolete from slime-2.0_p20070816-r1,the comments in the source file are as following:

;; SLIME-CLOSE-PARENS-AT-POINT is obsolete:

;; It doesn't work correctly on the REPL, because there
;; BEGINNING-OF-DEFUN-FUNCTION and END-OF-DEFUN-FUNCTION is bound to
;; SLIME-REPL-MODE-BEGINNING-OF-DEFUN (and
;; SLIME-REPL-MODE-END-OF-DEFUN respectively) which compromises the
;; way how they're expect to work (i.e. END-OF-DEFUN does not signal
;; an UNBOUND-PARENTHESES error.)

;; Use SLIME-CLOSE-ALL-PARENS-IN-SEXP instead.


well,bind slime-close-all-parens-in-sexp to C-c C-q,and I can pretend that nothing happened in my world.

Tuesday, October 02, 2007

to thinkpaders,about tp-fancontrol

from a certain version of thinkpad_acpi,fan_control is disabled by default,as Documentation/thinkpad-acpi.txt said:

NOTE NOTE NOTE: fan control operations are disabled by default for
safety reasons. To enable them, the module parameter "fan_control=1"
must be given to thinkpad-acpi.

so the tp-fancontrol daemon won't work any more if the thinkpad_acpi module isn't told that it should enable the fan_control.

let's do that:

#vim /etc/modules.d/thinkpad_acpi

then add the following line:
options thinkpad_acpi fan_control=1

:wq

#update-modules

now tp-fancontrol should work again.

Thursday, September 13, 2007

Yodaesque

what?yes!yodaesque!
Practical Common Lisp,Page 285,it reads:
"Compared to the clauses I’ve described so far, with their prepositions and subclauses, do is a model of Yodaesque simplicity."

and...the footnote:
“No! Try not. Do . . . or do not. There is no try.” —Yoda, The Empire Strikes Back

Orz...

Tuesday, July 24, 2007

emacs-unicode-2 compile error

several days ago,an error was reported (SYNTAX_ENTRY_FOLLOW_PARENT undefined) when emerge emacs-cvs,I tried to make a patch to get it compiled,but failed.however,the problem is resolved taday after cvs update.

Tuesday, July 17, 2007

app-emacs/cedet-1.0_pre4-r1

maybe there is a bug in cedet-1.0_pre4-r1,everytime I open a .c file,emacs will complain that "Autoloading failed to define function semantic-default-c-setup",and it occurs even when I create a semanticdb,so I try to downgrade cedet to 1.0_pre3-r2,no problems any more.

Sunday, June 17, 2007

a gtk+'s bug fixed

the bug wat not fixed until gtk+-2.10.13
with previous gtk+ versions,a save dialog will stretch out and draw back very frequently,as a result,the cpu load will be 100%
now all gtk+ applications get right.

Friday, June 15, 2007

a bug of x60

if the modem is disabled in bios,then the sound card will be disabled,too.
after I disabled the modem,I must run "alsaconf" to drive the sound card every rebooting.

Tuesday, June 12, 2007

paging in linux

ever since 2.6.11,there is a new level named pud(page upper directory) plusing to pgd,pmd,pte,and so something chaged.
what confused me is that,as ULK said:
"For 32-bit architectures with no Physical Address Extension, two paging levels are sufficient. Linux essentially eliminates the Page Upper
Directory and the Page Middle Directory fields by saying that they contain zero bits. However, the positions of the Page Upper Directory
and the Page Middle Directory in the sequence of pointers are kept so that the same code can work on 32-bit and 64-bit architectures. The
kernel keeps a position for the Page Upper Directory and the Page Middle Directory by setting the number of entries in them to 1 and
mapping these two entries into the proper entry of the Page Global Directory."
so I wonder how does the kernel eliminate pud and pmd as well as keep the position of them to make the code work on 32-bit and 64-bit arch?

at first,I thought pgd still leads the paging unit to pud,then pud to pmd,blablabla...
but it is completely wrong,coz the paging unit in x86 knows only 2-level paging when with pae disabled!
so in the hardware paging unit(x86)'s perspective,there are only pgd,pte,nothing else.

but how to understand "the same code can work on 32-bit and 64-bit architechtures"?
well,I think it means the kernel folds pud and pmd away,so any functions or macros operating on pud or pmd will be lead to pgd,thus the code need no change between 32-bit and 64-bit.

anyone any ideas?plz show me:)