Friday, December 28, 2007

Install Proggy Fonts for Emacs in Gentoo

Coding horror discussed various nice programming fonts (see here and here). Intrigued by these posts, I decided to try these fonts on my Gentoo box. Following is a record of what I have done.

Install Fonts in Gentoo

I decided to use Proggy Clean font (website is here). This section is mainly related to this particular font, but I will discuss at the end of this section how to handle other fonts in the same site.

Instead of manually installing the font, I determined to writer one ebuild. Main rationale is to rely on font eclass to automate font installation work. The ebuild could be easily reused and even incorporate into Gentoo portage (hopefully). The other reason is that I want to practice a little bit on portage overlay and to write my first (trivial) ebuild.

Create portage overlay

If you have already setup a overlay, you may skip this part. However it should be noted that all the description in this post assumes that your overlay is located in /usr/local/portage.

Run the following commands.

# mkdir -p /usr/local/portage
# echo 'PORTDIR_OVERLAY="/usr/local/portage"' >> /etc/make.conf    

Create the ebuild

First, create the directories.

mkdir -p /usr/local/portage/media-fonts/ttf-proggy-clean

Next, create a file named ttf-proggy-clean-1.0.ebuild under the directory just created. Fill the contents as below.

inherit font

DESCRIPTION="Proggy Clean TTF font"
HOMEPAGE="http://www.proggyfonts.com/"
SRC_URI="http://www.proggyfonts.com/download/download_bridge.php?get=ProggyClean.ttf.zip"

LICENSE="Proggy"
SLOT="0"
KEYWORDS="alpha amd64 arm ia64 ppc s390 sh sparc x86"
IUSE=""

DEPEND=""

S="${WORKDIR}"
FONT_S=${S}
FONT_SUFFIX="ttf"
DOCS="Licence.txt Readme.txt"

RESTRICT="mirror" 

Some explanations of the ebuild:

  • Version: I believe the font is quite stable, therefore version 1.0 should be OK :-).
  • License: the license in the package is quite permissive, but since I cannot link it to any existing license, I category it simply as Proggy.

Run the following command to sign the ebuild.

# ebuild /usr/local/portage/media-fonts/ttf-proggy-clean/ttf-proggy-clean-1.0.ebuild digest

BTW, I refers a lot to this when writing this first ebuild (of course, the skeleton is from man font.eclass).

Install font

Now comes our familiar emerge.

# emerge media-fonts/ttf-proggy-clean

Now we need to add the font path. Open file /etc/X11/xorg.conf, find the section Section "Files", and add the following line within the section:

  FontPath "/usr/share/fonts/ttf-proggy-clean"

Then press Ctrl-Alt-Backspace to restart X server. After login again, you may issue the following command (if you have not installed xlsfonts, please do so by typing emerge xlsfonts).

# xlsfonts | grep proggy

You should see several lines looking like -altsys-proggycleantt.... If not, there is something wrong to fix (please check previous typings).

Caveat: before I add the FontPath line in section Files, that section is actually empty! Therefore X simply cannot restart. The solution is to add some default paths into that section, e.g. /usr/share/fonts/100dpi, /usr/share/fonts/misc.

How to use other fonts

Following is a guideline for other fonts listed in the same website.

  • Use appropriate directory and file name to represent the font. For example, for Proggy Square X font, one might setup directory /usr/local/portage/media-fonts/proggy-square, and create a ebuild called proggy-square-1.0.ebuild. The directory should be added into /etc/X11/xorg.conf as described above.
  • Modify the ebuild:
    • Specify appropriate DESCRIPTION.
    • Use correct SRC_URI.
    • Specify DOCS correctly. I found that X font package does not contain Readme.txt.
    • Specify FONT_SUFFIX. For X fonts, use gz; use ttf (like above) for TTF fonts.

Configure Emacs

Compared with the installation described above, this part is rather easy. Assume that you want to use the font -altsys-proggycleantt-medium-r-normal--0-0-0-0-m-0-iso8859-1 (you may select one from the output of xlsfonts as described above)d, simply add the following line in your .emacs:

(set-default-font "-altsys-proggycleantt-medium-r-normal--0-0-0-0-m-0-iso8859-1")

Monday, December 24, 2007

Weirdness of Erlang

But Erlang itself? It's too weird, and in my brief experiments, the implementation shows its age; we have in fact learned some things about software since way back then.
--Tim Bray, Prognostication

I'm just quite new to Erlang and read through Chapter 10 of Programming Erlang so far. The language really expand my mind greatly, with its pattern-matching capability (even for a Haskell user), and of course concurrency oriented programming. Inter-process communication, even across different machines, is a quite simple task in Erlang. However, as stated by Tim Bray, Erlang is too weird. In the following, I will list some weird things I met when studying Erlang. The focus is mainly on syntax, which seems to be non-essential but should have been easily improved (if backward compatibility is not an issue).

Usage of punctuation

A newcomer to Erlang will almost forget to type period after an expression one or more times. Why do we need such punctuation even in an interactive shell? I admit that using period, semicolon and comma in Emacs makes source editing more automatic, but I prefer not using them at all. Erlang should learn something from the elegance of Haskell in this aspect.

Awkward functional programming with fun

There is no fun when using fun. When writing an anonymous function, one have to use fun and append end. Even when calling a named function within a higher order function, one has to use fun like this lists:map(fun math:sqrt/1, [1.0, 2.0, 3.0]). This is awkward and Haskell way is way better again.

Handling Records

Although records are tuples in disguise, they are treated in a C-style way: i.e. if one record definition is to be used by multiple source files, it has to be put into an include file. Weird.

Macro

Fans of C/C++ may applaud when they learn that Erlang provided similar macro facilities. However it is doubtful whether this controversial feature is needed for such a functional programming language.

Makefile

Again advocates of C/C++ might be pleased to know that Erlang also relies on makefile for package management/build. Shed by the light of Haskell/Cabal, Common Lisp/ASDF or X/Y (put your favorite language and package management system here), one may expect that Erlang has a more modern way?

Conclusion

Apparently, above weird aspects are not show-stopper for the popularity of Erlang. However, appreciating the Ruby principle of Least Surprise, I cannot help but rant my impressions gathered when climbing the mind-blown-away learning curve.

Monday, December 17, 2007

Minimize Diagnostics in Common Lisp

Diagnostics generated by compilers are quite useful for programmers to spot bugs or inefficiencies in the first place. It is quite often to observe that command options -Wall -pedantic (or even -Werror) are used to invoke gcc. When investigating the situation in Common Lisp, a newcomer would be surprised to see that diagnostics and ways to handle them are actually standardized (just like other implementation-like-stuff-in-other-languages, say disassemble). In addition to the already standardized diagnostics like warning and style warning (the latter is actually a subtype of the former), some CL implementations (notably CMUCL and SBCL) provides efficiency notes, which alerts the user that the compiler has chosen a rather inefficient implementation for some operations. For SBCL users, it is highly recommended to read one part of SBCL manual to learn how to interpret SBCL diagnostics.

In typical development cycle, after writing/modifying some code and compilation, if compiler complains with such diagnostics, you know that somewhere needs your attention. However, you can only sense these new diagnostics if the number of existing diagnostics is quite small or zero. Otherwise you many not even notice the new diagnostics since you already have a bunch of them. In this sense, existing diagnostics are like broken windows, and they discourage you to identify new diagnostics. Your program will deteriorate if you do not fix these broken windows. In summary, to make the diagnostics useful, you have to minimize, or better yet, eliminate existing diagnostics.

There are two ways to stop the compiler emitting diagnostics: removal and muffling. We will discuss them separately. Note that we will use SBCL as our CL implementation in the following discussion.

1. Removing Diagnostics

This is of course our first choice, since diagnostics are indications of some risks in the code. In the following, we give some simple examples to illustrate how to delete such annoying (but useful somehow) complaints.

1.1 Warnings

Typically warnings are serious issues to be resolved immediately. One exception is that sometimes if one file contains multiple style warnings, a warning will be issued for the file itself as well. Therefore to remove such warning requires eliminating related style warnings, as discussed below.

1.2 Style Warnings

1.2.1 Variable X defined but never used.

The first consideration here should be to remove the variable causing diagnostics if possible, which has the additional benefit of simplifying code. However there are cases when we cannot change the function interface. For example, when we write reader macros with set-dispatch-macro-character, the 3rd argument is itself a function with 3 arguments like (stream char1 char2), and typically we do not use either char1 or char2. In this situation, we can use declarations like (declare (ignore char1 char2)). Sometimes, if the argument is used in some scenarios while not in other cases (typically in macro definitions), we can use declaration ignorable. An example here is the anaphoric macro acond2 defined in On Lisp, a declaration (declare (ignorable it)) is needed for the ubiquitous it.

1.2.2 Redefining FOO in BAR.

Typically such style warnings are issued when one file is reload, hence can be simply ignored. However there are some more complicated cases. One example is again related to the reader macro. If one want to use the reader macro in the same file, the definition of the reader macro should be encompassed with (eval-when (:compile-toplevel :load-toplevel :execute), as illustrated in HyperSpec. The problem is that if you write a function instead, every time you load the file, this style warning pops up. The solution? Either use anonymous functions as illustrated in HyperSpec, or spin out related part into a separate file without the eval-when stuff.

1.3 Efficiency Notes

It should be noted that efficiency notes are emitted only when we turn on optimization declarations, e.g. (declare (optimize (speed 3) (safety 0))). It is well known that premature optimization is the root of all evil. Therefore before battling against those notes, one should check whether the function in question is the bottleneck or not. If not, one should remove those optimization declarations, which can make the core simpler and more maintanable; otherwise (when the optimization is really necessary), one can proceed further with techniques discussed below.

As indicated by the informative CMUCL manual on efficiency notes, the solution to dismiss these notes is to provide sufficient type declarations. It should be noted that current SBCL is smart enough to perform type inference, therefore it is unnecessary to clutter the code with type declarations for every expressions.

1.3.1 Doing X to pointer coercion

Such notes are typically emitted for function return values which are of boxed types. For example, on 64-bit machines, double-float is still boxed (it requires exactly 64-bits!), therefore compiling the following functions will get a note doing float to pointer coercion (cost 13) to "<return value>".

Code-list-1

(defun df+ (x y)
  (declare (optimize (speed 3) (safety 0))
           (double-float x y))
  (+ x y))

How to remove these notes? One way is to use unboxed types. For example, in above example, one can use single-float instead of double-float for 64-bit CPUs. However such solution is not always available: one reason is that there are only a few unboxed types, and the other one is that sometimes the boxed types are what we really need: e.g. double-float is required from accuracy point of view. Another solution is to use local functions. Since compiler know how to utilize the return value, the efficiency notes will not be emitted. One example is shown below:

Code-list-2

(defun df-outer ()
  (flet ((df+ (x y)
           (declare (optimize (speed 3) (safety 0))
                    (double-float x y))
           (+ x y)))
    (coerce (df+ 1.0d0 2.0d0) 'float)))

Note that the above example is simply contrived to illustrate that using local functions can remove efficiency notes.

2. Muffling Diagnostics

There are many occasions that above solutions cannot be applied. In this case, we can muffle the diagnostics. The intention of muffling is not that we are going to adopt an ostrich policy for those diagnostics. The underlying rationale is actually as following:

  • We acknowledge that there is no way to eliminate the diagnostics.
  • The diagnostics do not impose any risks to the program per se.
  • The diagnostics have to be suppressed in order not to obscure other diagnostics.

One example is the above code-list-1. If such a standalone function optimized for double-float is really what we want, we cannot remove the efficiency note on 64-bit machines (when will 128-bit CPUs become mainstream?). Since there is no problem with the code, we can safely muffle the annoying note to avoid it to distract our attention further.

Common Lisp does provide a standard way to muffle standard diagnostics (i.e. warnings and style warnings, but not notes since they are not standardized). This is function muffle-warning, however its usage seems not straightforward. SBCL wraps it up and provides a pair of declarations sb-ext:muffle-conditions and sb-ext:unmuffle-conditions to muffle diagnostics. We will use them in the following discussion. If portability is desirable, one should use #+sbcl; however in the following examples, we do not use it for simplicity.

2.1 Local Control

It is preferred that we muffle the diagnostics in a local definition if possible. We can specify which types of diagnostics to muffle and use the pair of extensions for advanced purposes, as illustrated in SBCL manual. Following is an example to illustrate how it is applied to our example (code-list-1) above.

Code-list-3

(defun df+ (x y)
  (declare (optimize (speed 3) (safety 0))
           (double-float x y)
           (sb-ext:muffle-conditions sb-ext:compiler-note))
  (+ x y))

Note the usage of sb-ext:muffle-conditions in line 4 above.

2.2 Global Control

One can muffle diagnostics globally in the way like (declaim (sb-ext:muffle-conditions sb-ext:compiler-note)). However it is not recommend to do so since we will lose the whole point of using diagnostics. Nevertheless using pairs of global declarations is useful sometimes, since it can muffle the diagnostics issued for top level structures, and allow the compiler to complain again if it meets issues in other places.

Let's use code in On Lisp again as another example. When emulating Scheme-like continuations in Common Lisp, Paul Graham defined parameter *cont* as (setq *cont* #'identity) in top level. SBCL emits a warning undefined variable: *cont* when compiling the code. It should be noted that we cannot use defvar for this parameter, as discussed in the text. To muffle the warning, we can add a pair of declarations as in code-list-4 below.

Code-list-4

(declaim (sb-ext:muffle-conditions warning))
(setq *cont* #'identity)
(declaim (sb-ext:unmuffle-conditions warning))

Update: as pointed out by Lars Rune Nøstdal in the comment below, a cleaner way is to use locally. The above example can be simplified as following:

Code-list-5

(locally (declare (sb-ext:muffle-conditions warning))
  (setq *cont* #'identity))

Conclusion

To make the best of compiler diagnostics, it is good to remove the existing ones. Happy hacking beautiful code!

Tuesday, December 11, 2007

The Last Clip of SICP Video

This morning, I finished watching SICP video lectures with my tiny Dopod 838 QVGA screen when commuting on company shuttle bus. As I have pointed out in previous post, learning such a mind-expanding book has never been such a pleasure with the aid of videos.

When watching the video, one may have already noticed at the beginning that the atmosphere was somehow different, with Abelson wearing a foolscap and almost everybody wearing sunglasses. In this very last clip, after Sussman explained the simple-but-very-effective Minsky garbage collector and discussed the halting problem, the usual Q&A section started. The last tricky question was Is this the last question?, and Sussman replied Apparently yes after a long pause. And the lecture just closed here, with every student (actually HP employees) getting a close-up.

I just studied the lectures along with them, learned a lot of things, and was delighted and blown away many times. The video was taken in July 1986, when I had no idea of what computer is. It's interesting that I managed to learn things together with them. Although I have not really dived into the books and the exercises, I'm sure I will do so quite soon, at least to meet the authors again during reading.

Friday, December 7, 2007

Nicer Fonts for Gnuplot

Gnuplot is a great visualization tool to use. However the default font is not so pleasant for me. Following is the record on my adventure for alternative fonts. The description is mainly Gentoo oriented.

Which font to use?

I'd like to try TTF font for nicer looking, and Bitstream Vera bundled with Gnome just fits my taste. In case that you have not installed Gnome (e.g. you're pretty much satisfied with the plain terminal, or you use X only without any desktop environment, or you are a fan of other desktop environments like KDE or E17), you may install the font with one command in Gentoo: emerge ttf-bitstream-vera.

Build Gnuplot

Gnuplot should be built with libgd support. To do so, you should enable the USE flag "gd". You may add gd to your make.conf, or use the following command to add it for gnuplot alone:

echo "sci-visualization/gnuplot gd" >> /etc/portage/package.use

After that, build gnuplot by typing emerge gnuplot. The version I'm using for this post is 4.2.2.

Configure environment variable

Add the following line to one of the shell profiles (e.g. ~/.bash_profile):

export GDFONTPATH=/usr/share/fonts/ttf-bitstream-vera

Try it out

Now launch gnuplot. To test whether our settings take effect, type the following at gnuplot prompt:

gnuplot> set term png enhanced font "Vera,12"
Terminal type set to 'png'
Options are 'nocrop enhanced font Vera 12 '
gnuplot> set output "test.png"
gnuplot> plot sin(x)
gnuplot> quit

Now start your favorite photo viewer to examine test.png (I use Emacs in this case). Does the figure looks prettier than before? (Note: it seems that the font name is case sensitive and should be consistent with the actual file name of the font: "Vera" works in above example while "vera" not.)

One limitation is that TTF fonts are not available to X11 terminal therefore you cannot directly enjoy the effect within gnuplot. Emacs users using gnuplot-mode might not feel too much switching pain in this case. Another solution is: if you have ImageMagick installed, you can use png terminal, and pipe the output to ImageMagick by typing set output '|display png:-' instead of plain test.png as shown above.

Wednesday, November 28, 2007

Format Source Code for Web

Introduction

As both a programmer and blogger, one needs to publish source code on blogs frequently. So how to do it? As a newcomer to blogger community, I looked into the problem for a while. There are two issues related to this if you only want to display your code plainly, without syntax highlighting:

  1. HTML treats consecutive spaces, tabs, line breaks as a single space, which removes indentations of the code.
  2. Special characters like &, <, >, and " need escaping, especially for HTML code itself.

My initial thought is to use sed, since the above issues can be simply solved with line by line processing, which is what sed is designed for. Later on I realized that why not use Javascript? Then I can publish code everywhere, which means that I can mobilize web authoring, in addition to mobilizing web surfing.

Solution

Following is the tool to convert source code to HTML fragments to be inserted in blogs or normal web pages. Simply paste your source code into the 1st textarea, and copy the HTML code out from the 2nd textarea.

It should be noted that I use CSS to format the source code in <pre> element, therefore one attribute "code" appears in the 1st line of the HTML output. One may tweak it according to his/her own preference, e.g. change to inline style sheets.

Source code:
HTML output:

Implementation

The source code for above form and related script is shown below formatted with itself.

<form name="converter" action="">
  <table>
    <tr><td>Source code:<br/>
      <textarea name="source" onchange="formatCode();" rows="10" cols="80">
      </textarea>
    </td></tr>
    <tr><td>HTML output:<br/>
      <textarea name="output" rows="10" cols="80">
      </textarea>
    </td></tr>
  </table>
</form>

<script type="text/javascript">
  //<![CDATA[
  function formatCode () {
    var form = document.converter;
    var s = form.source.value.replace(/&/g, '&amp;');
    s = s.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
    form.output.value = "<pre class=\"code\">\n" + s + "\n</pre>\n";
    form.output.select();
  }
  //]]>
</script>      

We use HTML element <pre> to solve the 1st issue listed above. In the script, function formatCode escapes special characters; note that we should handle character & first.

Update (2008-1-17): for Emacs users, you may find htmlize useful for this task (with many cool features). Gentoo users can simple emerge htmlize. I just stumbled upon this cool extension when reading the comments for Steve Yegge's nice post.

Thursday, November 15, 2007

Mobilize Web Surfing

At first glance, web surfing is naturally in a mobile way. You can surf anywhere, anytime nowadays. However, the important data associated with web surfing are not mobilized at the begining. The most noteworthy example is your favorite websites. Traditionally, you have to store them locally in your computer, in a format only known by your chosen web browser. You have to copy your bookmarks around and import them whenever you use a different computer, or even try a new browser. That is possibly why web browsers are bundled with features like import/export bookmarks and even the tool to migrate bookmarks from competing browsers. Even armed with those weapons, you still have the problem of synchronizing your bookmarks between different computers; not to mention that the import/export process is really boring.

So here comes the so-called social bookmark, with the most successful example: del.icio.us. As its name suggests, it is really a delicious tool to use. After registration, you can install browser extentions to make the service provided by del.icio.us to integrate with your browser seamlessly. Now you can access your bookmark anywhere: even on a computer without the extension installed, you can simply access your bookmarks by visiting the del.icio.us website. With the latest Firefox extension, one bookmark sidebar is installed, which gives you instant access to your bookmarks. In addition to these handy bookmark functionalities, you also get social effects. When you add one nice page to your collection, you now can choose popular tags which is automatically provided by the service by examining who else chosen to tag the same page. You now can select the right tag easily.

A more efficient way, compared with checking updates of your favorite web pages individually, is to syndicate them and process them in a RSS reader. Most web surfers have already done so. At first, we use a offline RSS reader, i.e. subscription file is stored locally and RSS updates are also fetched to local computer. A question then arises, similar to the bookmark problem: how can we read RSS updates in different places: e.g. work and home? We can synchronize the subscription (although tedious), but we then have to manually mark those already read in one place (note that typically new items pop up very quickly). Online RSS reader comes into rescue now. Simple register one, e.g. Google Reader, import your subscription file and start reading everywhere. For my own experience, the online reader can compete with the offline reader in almost every area, and the developing pace of such online readers is also extremely fast!

Yet another example is online notebook like Google Notebook. Remember those yellow stick notes? One certainly take many notes during web surfing. Now you can write and read them everywhere.

There seems to be endless examples to fully mobilize your web surfing experience: you can put your calendar online, write your report online, investigate your financial status online, or prepare your presentation online. Such experience is really excellent, and the only thing we should be cautious is to guarantee the network connection to avoid any agony.

Thursday, November 8, 2007

Emacs + Firefox as Blog Development Environment

In my previous post, I advocated to use Emacs as a blog post editor. This post deals with how to setup a nice blog development environment with Emacs + Firefox.

Introduction

As discussed in the post on Immersing Tutorial, a development environment is more productive if it can reduce the time for context switching, i.e. keep edit, building and running in the same environment. With Emacs + nXhtml + w3m, we can achieve this goal. The only but critical problem is that the functionalities of w3m is quite limited compared with popular browsers like Firefox: e.g. it does not support Javascript!

After googling around the web, I got the conclusion that it is impossible to embed a Firefox browser within Emacs, i.e. there seems to be no interface like emacs-w3m, which links Emacs and w3m. I'm not sure whether this is due to the fundamental limitations of Emacs or there is purely no interest on such functionality (the former seems to be more likely since even Emacs guru Bill Clementson have to use both Emacs and Firefox).

I then started an adventure to investigate the alternatives. The first thing come into my mind is to resort to Eclipse. After setting up Eclipse + WTP and changing key scheme to Emacs, I successfully created a static Web project and started writing a new XHTML page. The problem I encountered next is that I cannot preview the web page just created! My intuition told me that the context menu "Run as Server" seems to be the solution, however "HTTP Preview" always complained that port 8080 is occupied by another process and I found nowhere to change the port setting. After long time searching, I finally gave up the Eclipse approach: I'm just such a novice to Eclipse that it may not be justified to learn this giant with the only purpose to write blogs.

I then settled down on the approaches to use Emacs and Firefox jointly. I first tried the Edit HTML Bookmarklet, it is quite handy to change the page you're viewing (with similar functionality as Firebug), but the drawback is that your changes are only saved in memory but not on disk. I then installed ViewSourceWith add-on. It can call an external application like Emacs to view and edit the web page you're viewing. It works pretty well but I soon realized that it is not a developing model I like. The underlying concept for this add-on is that your main environment is Firefox and you occasionally invokes Emacs to tweak the HTML code. However what I need is on the contrary: I'd like to work mainly in Emacs, and use Firefox to preview the blog post when necessary.

Finally I come to the current solution I'm using: setup Emacs browse-url functions for Firefox. By doing so, I can edit post in Emacs, and preview it with nXhtml key binding C-C C-h b f. Following is the detailed setup process.

Installation

We are going to look at how to setup the BDE (please do not confuse the term with Borland Database Engine). The easier part is on Firefox, so we treat it first.

Firefox

Just install the latest Firefox distribution. To make efficient usage of Firefox for blog development, one may need Firebug and Web Developer. If you are a fan of Emacs, you may also try Conkeror, but be warned that you should read the manual carefully since it will dramatically changes your Firefox (and your just installed Firebug or Web Developer may not work well with Conkeror).

Emacs

If you read this long, you should be a regular Emacs user therefore you may not need to install Emacs additionally. But just for my future reference, I'd like to note that Emacs 23 worked fine with nXhtml.

Following is a list of Emacs extensions and programs related to Emacs for blog development.

Aspell

We should strive to remove spelling errors from our blog posts. We may use spell checking functionalities provided by blog services, however this is inefficient considering that we are mainly editing our blogs offline. Therefore on-the-fly spell checking within Emacs is needed. Aspell is a popular spell checker and Emacs has built-in support for it. So just install aspell and the dictionary for your language, and add the following lines to your .emacs:

(setq-default ispell-program-name "aspell")
(autoload 'flyspell-mode "flyspell" "On-the-fly spelling checker." t)
(add-hook 'nxhtml-mode-hook 'flyspell-mode)
Note that you should ensure that aspell is in your searching path.

nXhtml mode

After download and extracting the package, add the following lines to your .emacs:

(load "/path/to/nxhtml/autostart.el")
(eval-after-load 'nxhtml '(define-key nxhtml-mode-map [f2] 'nxml-complete))
(setq nxhtml-skip-welcome t)

Some explanation of above code:

  • 1st line: change it to match the directory where you installed nXhtml.
  • 2nd line: if you are working in Windows, you need this line since normally nxml-complete is bound to M-tab which is used by Windows itself. You may use keys other than f2 of course.
  • 3rd line: this is to remove the welcome message.

Link Emacs with Firefox

Add the following code to your .emacs:

(setq browse-url-browser-function 'browse-url-firefox
      browse-url-new-window-flag  t
      browse-url-firefox-new-window-is-tab t)

Make sure Firefox is in your search path. Note that currently there is one issue here on Windows (I have not tested under Linux). If there is no running Firefox, the preview works just fine; otherwise, preview actually opens two new Firefox tabs (in addition to those already existed). This is annoying but not a big issue. I have tried a few browser-url related changes in .emacs but failed.

Other Emacs Extensions

You may install other Emacs packages e.g. CSS mode and Javascript mode if you like.

Final Words

After installation, we can now edit blog posts in Emacs (with the help of nXhtml), and preview it in Firefox. So far, so good. It would be exciting if we can embed Firefox in Emacs or vice versa.

Wednesday, November 7, 2007

Is Gentoo Linux the Choice for Minimalists?

Merriam-Webster defines minimalist as: one who favors restricting the functions and powers of a political organization or the achievement of a set of goals to a minimum. I tends to believe that myself is a minimalist, especially in the areas of computer usage. So what should be my choice for Linux distribution?

There are hundreds of Linux distributions listed in distrowatch while my favorite is Gentoo. To discuss whether it is suitable for minimalists, we need one example for comparison. Here I will choose popular Ubuntu, without any intention to start a flamewar between the two distributions.

If one is the minimalist in terms of installation procedure, Ubuntu seems to be a better choice: the setup procedure may take less than half an hour on modern systems, and during setup, one only needs to make a few selections. Gentoo, on the contrary, requires a rather long process: if you perform a stage 1/3 install, you need to type many commands, setup some configuration files, and modify other configuration files; you need a decent Internet connection, and the compilation process might be quite long (if not longer than LFS). On a not-so-modern setup, to build a system with similar functionalities as what Ubuntu offered, you may need several days. So why bother?

The reasons are:

  • Minimal Components: you can build a minimal system out of your own taste with Gentoo: you only need to install programs you like to use (and their dependencies of course, note that you also have some freedom to choose which dependencies to install by control the USE flag), you also specify the minimum number of services to start at boot time. In the end, you have setup your own system with minimal components.
  • Minimal Upgrade: The upgrade system is also minimal. After installation, Gentoo is versionless. For example, you may use your Gentoo box for a long time without any upgrading. But one day, you may be tempted to upgrade one program, say Firefox, since the new features attract you. Then after synchronization of portage, one simple emerge command can bring this particular program up to date (this may update some dependencies or install new dependencies if needed).
  • Minimal Security Risk: you are less prone to security problems since you have less programs installed.

In summary, the installation for Gentoo can not be labelled as minimal, however this is only needed once for one computer. After installation, you can enjoy all the other benefits from Gentoo. The customization capability of Gentoo tends to encourage me to link it with Emacs: if you like Emacs and want to customize everything, Gentoo might be your favorite distribution.

Monday, October 29, 2007

Immersing Tutorials

It is widely accepted that less context switching, more productivity. For example, suppose you have two tasks, say task A and B. If you focus on task A for one hour, and then switch to task B, your productivity would be much higher compared with the case that you work on two tasks for 10 minutes alternatively. The reason is that it requires really long time to enter the state of one particular task, therefore context switching consumes too much time, leading to less productivity.

Let's now consider how we learn the usage of one program. Although this is only one task per se, it typically seems to be two completely different processes on computer. We need to start the program to learn, and also open another program to read the tutorial. Normally tutorials are distributed as HTML, PDF, or Info files. So when learning, we read several paragraphs in tutorial, and when we meet something intersting enough for practicing, we switched to the target program and try something there. We then switch back and read again. Sometimes we may switch several times within one minute. Such learning practice normally is not so satisfactory. Fortunately there are some programs whole learning process is not segmented as what we described above. The characteristic of such programs is that the tutorial itself is distributed in one of the formats that the program itself can view and/or manipulate. For example, web browser, PDF reader, Emacs when used as an Info reader obviously fall into such category, since they are designed to be the program for these file types. There are also other examples:

  • Emacs: although I have already mentioned it before as an Info reader, I'd like to point out that the plain Emacs tutorial is also excellent. Simply typle C-h t to bring up this nice tutorial. The tutorial file is self is a plain text file, however the command you just typed manuipulates the file, and setup a nice environment full of interactivity, which guide you into the realm of this mighty editor.
  • Inkscape is yet another excellent example. When I first tried to learn it, I googled around and find the tutorials online. I soon discovered that those tutorials are already bundled with the distribution in SVG format, which encourages you to fool around. This is the first time I ever feel that learning a drawing program is so pleasant.
  • FreeMind is another example. It distributes its help in the form of mind map, which is what the program is created for. When you read the help along, you immediately got the idea on how the program is used and what it can do. The only drawback is that you cannot try some modification operations when you read the help. But this can be easily overcome by copy the help file into a different place and open it directly.

In summary, these nice programs provide immersing tutorials. When you are learning, you are reading and practicing simultaneously. You use the same mouse/keyboard operation to scroll/zoom the tutorial as you would use for the program itself. You try things out on the tutorial directly, which provide great involved experience.

Can programs other than viewers or editors provide us immersing tutorials? Currently my thinking is that it would be good for programs to at least support displaying HTML. This can be done by reuse some nice web engines like Gecko. Then the help window can be opened in a separate window in the program. The learning process might not be so amazing as the examples listed above, but the productivity can still be improved compared with two-separate-program-approach.

Friday, October 19, 2007

Edit Blog Post Seriously

Previously when I edit my blog posts, I rely on WYSIWYG editor provided by Blogger, and made minor tweaks if necessary.

When looking at the HTML code by clicking tab "Edit Html", I realized that the code generated is not what I desired. For example, it used tag <span> a lot with inline style to make font bold or italic, which violates basic HTML/CSS principle to separate content and display. It may also messy the structure of the post, which makes reading the HTML code rather painful.

In addition to the problems generated by the automatic tools, I now feel greater temptation to modify the HTML code by myself, e.g. using the nice tags (code, var, kbd, samp etc.) provided by HTML to describe source code in post, and also to make my post strictly conforming to XHTML standard.

Therefore I decided to use Emacs to edit blog posts. The bundled HTML mode is not satisfying, therefore I tried nXhtml mode instead. It is pretty good at the job: auto-completion of tags and attributes, validating the code on the fly, etc.

There are two settings needing modifications:

  • In "Settings/Basic" tab, select No for "Show Compose Mode for all your blogs?". The intention is to avoid Blogger to mess up the post when swithing between "Compose Mode" and "Edit Html" mode.
  • In "Settings/Formatting" tab, select No for "Convert line breaks". By selecting this, when you edit in Emacs, you can use <p>tag for paragraph, command M+Q to fill the paragraph, and the pasted code into Blogger would result in the same look as you previewed in Emacs. If this option is kept to "Yes", you have to manually tweak your code (remove <p> tag and also hard line breaks since Blogger will add line breaks in that case).

I also plans to rewrite my previous posts, seriously.

Tuesday, October 9, 2007

Tag Local Files

Nowadays we use tags for almost every online service: blogs, photos, videos etc. They are quite helpful for organizing contents. How about our local files? We still need them for various reasons e.g. archive, privacy, security and so on. I'm going to discuss a simple way to tag local files. We assume you are using a Unix-like system (Linux in the example), where soft link is an inherent system feature.

The idea is quite simple: we setup directory ~/tags and make a subdirectory for each tag. For every file/directory with tags, one soft link to the original file is put under each tag subdirectory. To make tags, we use a command called tag which has the following synopsis:

tag src tag-name...

where src is the path to the original file/directory, and tag-name... are the list of tags to be applied delimited with spaces.

For example, assume there is a image file ~/photo/wonderful.jpg. When you change to directory ~/photo and type the command

tag wonderful.jpg france paris 2007 october

(assume that the image is taken in Paris in October 2007), there will be four directories created: ~/tags/france, ~/tags/paris, ~/tags/2007, ~/tags/october, and each directory contains a soft link to the original file.

Following is a snippet of the content of script tag.

#!/usr/bin/env bash

src=$1
dir=`pwd`
shift 1
for x in "$@"
do
  dst=~/tags/${x}/
  mkdir -p $dst
  ln -s $dir\/$src $dst
done

In the above script, we create directory for each tag and then make soft link within that directory.

To install this script, simply make it executable (by typing command chmod 755 tag), and copy it into a directory included in your search path (e.g. /usr/local/bin, you might need to su to root to gain access for these system directories).

In addition to the operation of adding tags as shown above, you might wonder how can we handle other operations on tags, e.g. deletion, search, and renaming. Fortunately, we don't need additional scripts any more. The reason is that tags are themselves ordinary directories, therefore we can use normal console commands to perform these tasks (e.g. rm for tag deletion, find for search and mv for renaming).

Enjoy organizing your local files with tags!

Update (October 10, 2007) As pointed out by aspir8or: The original script creates links whose paths are missing. He also provides a correction to the issue. The current script is based on his update, with slight changes purely from the author's own preference on variable naming.

Saturday, October 6, 2007

History of Header Images

Just like the record of milestones of this blog, this entry documents the header images used. The date format used is yyyy-mm-dd.

  • 2008-06-14 ~ Present: Manipulation of words, inspired by GEB, drawn with Inkscape..
  • 2007-10-06 ~ 2008-06-13: Panorama view of volcano Kintanami of Bali, taken on 2004-06-15.

Saturday, September 29, 2007

Milestones of My Blog

Following is the list of the milestones associated with this blog, in chronological order (as in date format yyyy-mm-dd). Since today is still in the month I created this blog, therefore I (or others?) can easily look up this blog in the future. Only time can tell whether this blog will prosper or diminish.

  • 2007-09-17: blog setup.
  • 2007-09-18: 1st post.
  • 2007-09-30: Google indexed this blog.
    • Google Blog Search seemed to include Blogspot blogs immediately after posting (with no surprise) . On the contrary, this blog has almost no trace on other blog search engines in the first one or two weeks.
    • Just for record, I submitted the correct sitemap to Google on 2007-09-24 and the sitemap was verified several minutes later. However Googlebot seems to have its own mind on when to index new sites (of course, there are hundreds of factors and maybe millions of code related to how this crawler work: this is anyway the bread and butter of Google) , no matter how anxiously and frequently I visited Webmaster Tools and also typed site:*** in Google search box. After some frustration with the long delay to index, I also tried to submit the blog to Yahoo. It's very interesting that just after the submission, the blog related search results just magically appear. Bingo! It seems that the bot was pushed by the competition (although the site was actually crawled 23 hours ago but just shown up in search results).Google Search Results
  • 2007-10-10: first comment from aspir8or (very constructive comment!).
  • 2007-12-02: 1st post marked by others.
  • 2007-12-24: post Weirdness of Erlang emerged in programming.reddit, and number of daily visits (according to Google analytics) is 306.
  • 2008-01-17: 1st authority point in Technorati.
  • 2008-01-30: first post to achieve page rank 2. Actually this is the first time my blog posts are credited with page ranks in Google dance. I discovered this by investigating "Crawl Stats" page in Google Webmaster Tools. It is interesting to note that the homepage of the blog itself does not have any page rank (even 0).
  • 2008-10-08: 10 Google Reader subscribers.

Note: this is a live document, therefore stay tuned for the new exciting events showing up!

Friday, September 28, 2007

Necessary Manual Changes to Blogger Template

I'm trying to be a minimalist if possible. Therefore when writing blogs, I'm using the new template instead of the classic one. I manipulates the page elements instead of directly change the html code, resisting temptations of adding cool features which requires modifications of template by hand. However there are still some nice stuff mandating such modifications. I will list them below in case I change my template in the future (I'm pretty satisfied with my current template, Stretch Denim Light, though). Since I'm really annoyed by these "hacks", in the list below I also discuss the possibility to eliminate them in the future.

  • Google Analytics code
    • Usage: analyze the visitors to your blog via web browsing.
    • How to change: just add the html code provided by Google immediately before the end body tag.
    • Possibility to remove this hack: possible. Once I have tried to implement this in the page element "HTML/Javascript", but Google Analytics failed to collect data since then although the status is still OK ("Receiving Data"). After I switched back, everything went fine.
  • Google Webmaster Tools verification tag
    • Usage: to prove to Google that you are the owner of your blog therefore Google can provide statistics data from its crawler for your blog. Even after successful verification, you still need this tag since the verification is done continuously.
    • How to change: just add the metatag provided by Google immediately after start tag head.
    • Possibility to remove this hack: seems difficult since the verification tag is always required.
  • Yahoo Site Explorer verification tag
    • Usage: the purpose is same as Google Webmaster Tools above.
    • How to change: just add the metatag provided by Yahoo immediately after start tag head. It seems that the tag provided by Yahoo is considered "not well-formed" by Blogger, therefore you need to add "/" before the end ">". Whether you place Google or Yahoo verification tag first is simply a judgment call.
    • Possibility to remove this hack: similar as Google.
  • Ad Sense between Posts
    • Usage: Google has issues on integrating Blogger and Ad Sense for some users (including me), therefore manual change to template is needed to display ads between posts.
    • How to change: just follow this tip. Refine the code and put it before "post-footer-line".
    • Possibility to remove this hack: hope Google can fix the issue soon.
  • Align header text and image
    • Usage: center the title, description and header image.
    • How to change: just follow this tip.
    • Possibility to remove this hack: if Blogger can support configuration of page header.
  • Add CSS for Code
    • Usage: provide uniform style sheet for code.
    • How to change: add the style sheet for div in the header.
    • Possibility to remove this hack: unlikely. Such customization should not be present in common template.
  • Display Permalink
    • Usage: In the footer of each post, Blogger shows the timestamp, which actually points to the permalink. This is rather confusing.
    • How to change: Based on this permalink tip. Check "Expand Widget Template", search through the template, between the end tag of "data:post.timestamp" and end tag of "abbr", insert " | Permalink". The operation is shown in figure below.Change Permalink
    • Possibility to remove this hack: yes. Maybe Blogger will improve the template?
  • Flash Tag Cloud
  • AddThis button
  • Use internal CSS

Wednesday, September 19, 2007

Learn Prolog Inside Lisp

Originally Prolog is not the first priority in programming languages I intended to learn after Lisp. But during reading along the Prolog chapter in On Lisp, I realized that Prolog is really an interesting language. Therefore I downloaded swi-prolog, update Emacs with more powerful prolog mode, and started to follow one Prolog tutorial.

Suddenly I realized that it might be better for me to learn Prolog inside Lisp. The implementation of Prolog presented in On Lisp is already quite powerful (provided the functionality like cut, is, and especially the ability to integrate with Lisp functionalities). Therefore I could try the examples in Prolog tutorials/books both in Prolog (as reference) and Lisp. If there are some Prolog functionalities that are not available in the Lisp implementation, I can try to implement them if possible. Sounds quite interesting.

Visitors

I just setup this blog in recent days and was curious about who will visit my blog. So I setup account for Google Analytics and inserted the javascript code in my template.

No surprise, the only initial visits come from myself when I looked at report from Google Analytics yesterday. Although I do not expect the situation would change, I investigated the report again today and found that there are visitors other than from China. I was somehow pleased until I realized that those visits are from search engines.

Tuesday, September 18, 2007

Performance vs. Productivity

Programming languages rank differently in terms of performance and productivity. Performance here mainly means the running speed of the application while productivity is related to the consuming of developer time.

Some languages are quite good at performance and bad at productivity. C/C++ is in such category. It is quite difficult to beat C/C++ in terms of performance (I know that in Language Shootout, Ocaml, Haskell and other language can beat C/C++ in some fields). However the development cycle of C/C++ is awful. Write some code, compile it, test it, debug it. The whole development cycle tends to diverge the developer's mind hither and thither, let alone the compilation time of C++ is also quite long.

Other languages are quite good at productivity and bad at performance. Ruby and Python are nice examples of this category. They have nice interpreters to easy the development cycle, yet the generated code is quite slow.

Lisp is somehow different. The productivity is high thanks to REPL. The performance is also not bad. The speed of raw code cannot match that of C/C++. But with careful declarations, the performance of Lisp is not far from C/C++. Note that you only need to optimize the hot spot code.

So which one to choose? It depends on the application type and also on the customer. For system software like OS, C is the preferred choice. The speed of the end product is crucial here. For web applications and scripts where speed is not a key issue, Perl/Python/Ruby are used extensively.

How about numerical simulation? We are desperate for speed. On the other hand, we ourselves are the customers of the simulator we write. Our productivity is mainly related to the time for developing the simulator, not the actual running time. Actually during the long simulation run, we have other issues to do: brainstorming, writing documentation etc. Therefore the language with high productivity while acceptable performance seems to be a good choice. That's probably why MATLAB is so popular in the community. Lisp seems to be good too but rarely heard of.

Learning by Watching

Typically learning is by reading books. But recently I find watching is of great help. Watching here means watching the lecture videos.

I have an obsession when reading technical books: I should not start a new section if I have not completely understand the previous session and finished all the exercises there. Such obsession haunted me so severely that sometimes I tries to browse later chapters just for fun. Indeed this kind of obsession is quite harmful for reading: it is quite difficult for me to read through the whole book. It should be noted that in many books, the later part normally contained advanced topics, which I miss.

Watching video is another story. I'm now trying to learn SICP but my obsession just stopped me before finishing chapter 1. However I have also download the video lectures. Watching video lectures does not impose any obsessions for me. Actually it is very interesting when I'm sitting in a shuttle bus or in public transportation. I just feel that it is rather easy to comprehend the video. It seems that I may finish watching all the videos quite early before completing the book.

Just hope all the great books have video lectures.

Languages to Learn, Books to Read

Following is a list of programming languages I intended to learn in the future, including those I'm already learning or have learned. The list is in particular order.

  • Common Lisp/Scheme
    • Implementation: SBCL/PLT-Scheme
    • Lisp is fantastic once you have started. The parenthesis syntax is simply beautiful: simple yet powerful. I started to touch upon Lisp (actually Elisp) in last year, with the intention to be able to configure Emacs in a more free way. Unfortunately I still have not finished the Elisp introduction document which accompanies Emacs distribution and I will finish it in the near future. Afterwards I was attracted by Haskell and devoted nearly half a year to learn it, with the focus on migration my C++ simulator to Haskell. Early this year, when I was browsing reddit as usual, I found this post, which proposed the analogy that hacking Lisp is like riding a bike. I was curious about such metaphor and decided to try myself. And the result is that I'm now a big fan of Lisp. I've almost translated all my Haskell code to Common Lisp, migrated many C++ files too, and added many new functionalities to my simulator. Lisp is just wonderful! It really grows into you. I have read Practical Common Lisp (although I skipped almost all the practical examples and will come back to them later), and now nearly finished On Lisp.
    • Book list for further reading
      • SICP: a must read. Free book available. The video lectures are of great help too.
      • PAIP: I'm also interested in AI and also a fan of Peter Norvig. So I may purchase this book although it is somehow expensive.
      • Object-Oriented Programming in Common Lisp: A Programmer's Guide to CLOS: I use CLOS a lot and like to be shocked by the book.
      • AMOP: A fancy text I heard of.
      • Lisp In Small Pieces: A must read for Lisp/Scheme implementation.
      • Lisp 1.5 Programmer's Manual: Definitive manual for ancient Lisp?
      • Ansi Common Lisp: This book might not be so advanced compared with books listed above, but I really like the articles/books from Paul Graham.
      • Other nice scheme books, especially the series: The Little Schemer/The Seasoned Schemer/The Reasoned Schemer, and How to Design Programs.
  • Oz
    • Implementation: Mozart
    • It seems that Oz encompasses almost all the programming paradigms therefore it would be nice to try.
    • CTM seems to be the book to try.
  • Ruby
  • Prolog
    • Implementation: swi-prolog
    • Another AI language. It seems that many Lisp books (e.g. On Lisp, PAIP) try to build a Prolog interpreter/compiler and then use it to solve problems. It might be of great fun to directly dive into Prolog.
  • Smalltalk
    • Implementation: Squeak
    • Quite shocked during the first use of Squeak 3 years ago.
  • Erlang

#1 Post

About half a year ago, I opened my first blog in Donews, but I only posted a few articles after registration. Now I decided to switch to blogger, mainly because I'm a fan of Google and really like the attractiveness of integration of various services provided by Google.

For this particular blog, I may post either in Chinese or English, depending on my taste :-) Hope this blog can open my mind to pop out things out, hopefully nice things. And I may post several articles in a row today to test my passion.