2011-03-12

Programing Style: Variable Naming: English Words Considered Harmful

Perm url with updates: http://xahlee.org/comp/programing_variable_naming.html

Programing Style: Variable Naming: English Words Considered Harmful

Xah Lee, 2011-03-11

This page is some thoughts on variable naming in writing computer programs.

In emacs lisp, i usually use camelCase for my local variables. Here's a example:

(defun read-lines (filePath) 
  "Return a list of lines of a file at FILEPATH." 
  (with-temp-buffer 
    (insert-file-contents filePath) 
    (split-string (buffer-string) "\n" t)))

Some lisp coder question the use of camelCase, because it is not standard elisp style. Here's the reason why i'm using camelCase, and some thoughts about naming of variables.

Distinction from Language Keywords

It provides a easy way to distinguish variables from built-in symbols. Particularly because of the fact that emacs-lisp-mode's coloring scheme is not full. (See: Emacs Lisp Mode Syntax Coloring Problem.)

In particular, all my local variables are in camelCase. I could used under_score but that's more typing and less visually distinguishable to lisp's hypen-word-style.

Uniqueness & Referential Transparency

For variables, in recent years i developed a habit to avoid naming variables that's also a standard English word. So, i'd name “file” as “myFile” or “aFile”. “files” might become “fileList” or “filess”. “string” would be “str”, “myString”, “inputStr”, etc. A ultimate solution for uniqueness is to append or prepend a random number in var names. So, “string” would be “str-5w77o” or something like that. But the problem with this is that it's too long and disruptive in reading and typing. Recently i've been toying with the idea of attaching a unicode to all vars. e.g. all my var would start with “ξ”. So “string” would be “ξstring”. (works fine in elisp btw. See: Unicode Support in Ruby, Perl, Python, javascript, Java, Emacs Lisp, Mathematica.) This way solves the random string readability issue.

My reason to avoid English words is for easy source code transformation, out of practical reasons. (i do a lot search or find & replace in my source code.)

Imagine, if every local variable (or every symbol) are unique in the source code. This way, you could locate any variable in the whole project. It makes some aspect easier for debugging and code tracing and code management. It also makes refactoring easier.

The idea is similar to the idea of Referential transparency (computer science). (Referential transparency can be thought of as a notion of find & replace of function & values.)

The desire to have unique identifier in source code comes in many guises. At the extreme is a desire to eliminate variables completely. For example: if every variable in source code can be unique somehow, then much of the desire for lexical scope over dynamic scope is gone. Some namespace problem is also solved. (in particular, elisp does not support namespace.) Combinatory logic is a desire to get rid of variables from lambda calculus. “Point-free programing” is a invention of syntax for defining functions without the need to write out its parameter. (See: What's Point-free Programing? (point-free function syntax)) Unique variable name is also the impetus for Hygienic macro.

Variable Name: English Prose vs Mathematical Abstraction

Another reason that somewhat pushed me in this naming exaperiment is that... instead of naming your vars in some meaningful english words, the opposite is to name them completely abstractly, as in math's x, y, z, α, β, γ.

So, i'd name “counter” or “num” as just “i” or “n”. (since these are 1-letter string and too common, so with the unique naming idea above, i usually name them “ii” or “nn” or might be “ξi”)

The idea with abstract naming is that it forces you to understand the code as a math expression that specify algorithm, instead of like english prose. Readability of source code is helped by coding in a pure functional programing style (e.g. functions, input, output), and good documentation of each function. So, to understand a function, you should just read the doc about its input output. While inside a code snippet, it is understood by simple functional style programing constructs.

To illustrate from the opposite view, the problem with english naming is that often it interfere with what the code is actually doing. For example, in normal convention often you'll see names like “thisObject”, “thatTree”, “fileList”, or “files”, your focus is on the meaning of these words, but not what the data type actually are or the function's actual mathematical behavior. The words can be deceptive. e.g. “file” can be a file handle, file path, file content. This is especially a problem when you are reading source code of a lang you do not know. e.g. when you encounter the word “object”, you don't know if that's a keyword in the language, a keyword for a pattern, a keyword for datatype, something, or just a user defined variable name that can be arbitrary. When you read a normal source code, half of the words are like that unless the editor does syntax coloring that distinguish the lang's keyword.

For example, here's a elisp code with naming following elisp convention:

(defun dosomething-region (begin end)
  "Prints region beginning and ending positions." 
  (interactive "r")
  (let () 
    (message "Region begins: %d, end at: %d" begin end)
  )
)

Unless you are familiar with elisp, you wouldn't know what those “begin” and “end” are. Maybe they are built-in keywords and have significance to the construct, and if you change them, the code wouldn't work.

But if the code is like this:

(defun dosomething-region (φ1 φ2)
  "Prints region beginning and ending positions." 
  (interactive "r")
  (let () 
    (message "Region begins: %d, end at: %d" φ1 φ2)
  )
)

Then you know that φ1 and φ2 are probably just arbitrary names.

To view this idea in another way ... when you read math, you never see mathematician name their variables with a multi-letter descriptive word, but usually a single symbol (a, b, c, x, y, z, α, β, γ ...), yet there's no problem understanding the expression. Your focus and understanding is on the abstract process and structure.

2 Types of Letter Sequence in Source Code

When reading a source code, you see symbols (operators) and identifiers (function names, var names, built-in lang names). Among the identifiers, it can be divided into 2 types: ① Those that cannot be changed without effecting the meaning of the program. ② Those that are arbitrary and can be changed.

The ones in the first category are language keywords. e.g. “for”, “while”, “class”, “function”, “extends”, “this”, “self”, “public”, “static”, “System”, “from”, “begin”, “end”, “map”, “require”, “import”, “let”, “list”, “defun”, “lambda”, “Take”, “Pattern”, “Table”, “Blank”, etc. These are the words in the source code that are critical, and they are almost always English words. To be able to know at a glance which words are lang keywords in a source code greatly helps in understanding, especially when you do not know the language well. This particularly applies to non-mainstream languages e.g. OCaml, PowerShell, Haskell, Erlang, J, Mathematica, Oz, etc.

The above ideas is just a experiment. Without actually doing it, you never know what's really good or bad.

This essay is originally a post in comp.lang.lisp @ Source groups.google.com.

2011-03-11

palimpsest

palimpsest

Day by day and almost minute by minute the past was brought up to date. In this way every prediction made by the Party could be shown by documentary evidence to have been correct, nor was any item of news, or any expression of opinion, which conflicted with the needs of the moment, ever allowed to remain on record. All history was a palimpsest, scraped clean and reinscribed exactly as often as was necessary. — 1984, George Orwell
Deleting History: Why Governments Demand Google Censor the Truth (2011-03-11) By Lauren Weinstein. @ Source lauren.vortex.com

emacs lisp: unicode to ascii words

Perm url with updates: http://xahlee.org/emacs/emacs_zap_gremlins.html

Here's a more polished solution. The “asciify-word-or-selection” is a command. It works on current word or text selection.

(defun asciify-string (inputstr)
"Make Unicode string into equivalent ASCII ones.
For example, “passé” becomes “passe”.
This function works on chars in European languages, and does
not transcode arbitrary unicode chars (such as Greek).
Un-transformed unicode char remains in the string."
  (let ()
   (setq inputstr (replace-regexp-in-string \\|à\\|â\\|ä\\|ã\\|å" "a" inputstr))
   (setq inputstr (replace-regexp-in-string \\|è\\|ê\\|ë" "e" inputstr))
   (setq inputstr (replace-regexp-in-string \\|ì\\|î\\|ï" "i" inputstr))
   (setq inputstr (replace-regexp-in-string \\|ò\\|ô\\|ö\\|õ\\|ø" "o" inputstr))
   (setq inputstr (replace-regexp-in-string \\|ù\\|û\\|ü" "u" inputstr))
   (setq inputstr (replace-regexp-in-string "ñ" "n" inputstr))
   (setq inputstr (replace-regexp-in-string "ç" "c" inputstr))
   (setq inputstr (replace-regexp-in-string "ð" "d" inputstr))
   (setq inputstr (replace-regexp-in-string "þ" "th" inputstr))
   (setq inputstr (replace-regexp-in-string "ß" "ss" inputstr))
   (setq inputstr (replace-regexp-in-string "æ" "ae" inputstr))
    inputstr
    ))
(defun asciify-word-or-selection ()
  "Make Unicode string into equivalent ASCII ones.
For example, “passé” becomes “passe”.
This command works on chars in European languages, and does
not transcode arbitrary unicode chars (such as Greek).
They remain in the string.
This command calls `asciify-string' to do the string transformation."
  (interactive)
  (let (bds p1 p2 inputstr)
    (setq bds (get-selection-or-unit 'word))
    (setq inputstr (elt bds 0) p1 (elt bds 1) p2 (elt bds 2)  )
    (setq inputstr (asciify-string inputstr))
    (delete-region p1 p2 )
    (insert inputstr)
    ))

The command uses “get-selection-or-unit”. You can get the code for that at Emacs Lisp: Using thing-at-point.

2011-03-10

GNU Emacs Default Keybinding

Perm url with updates: http://xahlee.org/emacs/gnu_emacs_keybinding.html

A Curious Look at GNU Emacs's 1000+ Default Keybinding

Xah Lee, 2011-03-10

In Emacs, there's a command “describe-bindings” 【F1 b】. By default, there are a total of 1353 bindings. In this page, let's look at what they are. (you can get the list by calling “describe-bindings” in emacs, or see this file: gnu_emacs_keybinding.txt.)

(A bit tech detail: “describe-bindings” lists all keyboard shortcuts that's available to you in your current setup. The exact result depends on the current major mode, minor modes, and your custom keybinding.)

The Space-cadet keyboard

Many of the bindings refer to keys on lisp machine keyboard. So, here's a photo of lisp keyboard for reference.

Space-Cadet keyboard-2m

The Space-cadet keyboard, one of the early Lisp Machine keyboards. Source

(For more lisp machine keyboard photos, see: lisp keyboard photos.)

Alt Key

It has 137 bindings starting with the notation A-.

A-/                Prefix Command
A-1                Prefix Command
A-3                Prefix Command
A-<                «
A-=                ¯
A->                »
A-?                ¿
A-C                ©

I think these refer to the key labeled “Alt mode” on lisp keyboard. (if you know, please comment)

The purpose seems to be for inputting non-ASCII symbols. Today such key is called Alt Graph key. (on the Mac, the Opt key function as AltGr. On Windows, the right Alt becomes AltGr if you set your keyboard layout to one of European layouts.)

Dead & Mute Keys

There are 226 keys with the notation of dead- in it. e.g.

<S-dead-acute>  Prefix Command
<dead-acute>  Prefix Command
<S-dead-tilde> A Ã
<S-dead-asciitilde> A Ã
<dead-tilde> A  Ã
<S-dead-grave> A À
<dead-grave> A  À
<S-dead-circumflex> 1 ¹
<S-dead-circum> 1 ¹
<S-dead-asciicircum> 1 ¹
<dead-circumflex> 1 ¹
<dead-circum> 1 ¹
<dead-asciicircum> 1 ¹
<S-dead-acute> A Á
<dead-acute> A  Á
<S-dead-diaeresis> A Ä
<dead-diaeresis> A Ä

These are binding for Dead key for entering chars with Diacritic marks. A dead key is a modifier key similar in concept to Alt Graph key, except that it doesn't produce a char until another key is pressed.

Here's example of letters with diacritic and their names:

  • é = e with acute mark
  • à = a with grave mark
  • ê = e with circumflex
  • ñ = n with tilde
  • ü = u with diaeresis

Note that the S- is the notation for the “Shift” key. So, <S-dead-tilde> A would be something like 【Shift+dead~ A】.

There's also 70 keys with the notation of mute-, similar to the “dead” above. Not sure what they are.

(Thanks to Frédéric Perrin and Jason Rummey for help.)

“C-x 8” for Special Symbol Input

There are about 1k bindings left after the discounting the above.

There are 140 bindings for C-x 8. Example:

C-x 8 <   «
C-x 8 =   ¯
C-x 8 >   »
C-x 8 ?   ¿
C-x 8 C   ©
C-x 8 L   £
C-x 8 P   ¶
C-x 8 R   ®
C-x 8 S   §
C-x 8 Y   ¥
C-x 8 ^   Prefix Command

These are for entering special symbols that we can actually use within emacs today for inputting special chars — without using operating system's AltGr mechanism. (See: Emacs and Unicode Tips.)

Help, F1, “C-h”

There are 48 keys with <help> notation. Example:

<help>    help-command
C-h <help>  help-for-help
<f1> <help> help-for-help
<help> C-a  about-emacs
<help> C-c  describe-copying
<help> C-d  view-emacs-debugging
<help> C-e  view-external-packages
<help> C-f  view-emacs-FAQ
...
<help> i  info
<help> k  describe-key
<help> l  view-lossage
<help> m  describe-mode

The “Help” is a key on lisp keyboard, but also on many other keyboards, including NeXT, Sun Microsystems, and older Apple keyboard. See: Source www.pfu.fujitsu.com.

Then there are 48 keys with <f1> notation. Example:

<f1>    help-command
C-h <f1>  help-for-help
<f1> C-a  about-emacs
<f1> C-c  describe-copying
<f1> C-d  view-emacs-debugging
<f1> C-e  view-external-packages
<f1> C-f  view-emacs-FAQ
...
<f1> i    info
<f1> k    describe-key
<f1> l    view-lossage
<f1> m    describe-mode

There are also 48 keys with C-h notation.

C-h   help-command
<C-home>  beginning-of-buffer
C-h C-a   about-emacs
C-h C-c   describe-copying
C-h C-d   view-emacs-debugging
C-h C-e   view-external-packages
C-h C-f   view-emacs-FAQ
...
C-h i   info
C-h k   describe-key
C-h l   view-lossage
C-h m   describe-mode

All these are equivalent maps. (e.g. F1 = Help = 【Ctrl+h】) It's interesting to note that these do not seem to be key translations. Instead, each binding is defined in each set.

C-x Prefix

There are 204 with the C-x notation. Example:

C-x C-@   pop-global-mark
C-x C-b   list-buffers
C-x C-c   save-buffers-kill-terminal
C-x C-d   list-directory
C-x C-e   eval-last-sexp
C-x C-f   find-file
C-x TAB   indent-rigidly
...
C-x $   set-selective-display
C-x '   expand-abbrev
C-x (   kmacro-start-macro
C-x )   kmacro-end-macro
C-x *   calc-dispatch
C-x +   balance-windows
C-x -   shrink-window-if-larger-than-buffer
C-x .   set-fill-prefix
C-x 0   delete-window
C-x 1   delete-other-windows
C-x 2   split-window-vertically
...
C-x a   Prefix Command
C-x b   switch-to-buffer
C-x d   dired
C-x e   kmacro-end-and-call-macro
C-x f   set-fill-column
C-x h   mark-whole-buffer
...
C-x <C-left>  previous-buffer
C-x <C-right> next-buffer
C-x <left>  previous-buffer
C-x <right> next-buffer
C-x C-k C-a kmacro-add-counter
C-x C-k C-c kmacro-set-counter
C-x C-k C-d kmacro-delete-ring-head
C-x C-k C-e kmacro-edit-macro-repeat
C-x C-k C-f kmacro-set-format

“view-mode” Minor Mode Keys

There are 38 keys for “view-mode”. They are almost all single symbol keys. Example:

0 .. 9    digit-argument
<   beginning-of-buffer
=   what-line
>   end-of-buffer
?   describe-mode
@   View-back-to-mark
C   View-kill-and-leave
E   View-exit-and-edit
F   View-revert-buffer-scroll-page-forward
H   describe-mode
Q   View-quit-all

Ctrl+Meta Bindings (C-M)

There are 36 bindings with notation C-M-. Example:

<C-M-down>  down-list
<C-M-end> end-of-defun
<C-M-home>  beginning-of-defun
<C-M-left>  backward-sexp
<C-M-right> forward-sexp
<C-M-up>  backward-up-list
C-M-@   mark-sexp
C-M-a   beginning-of-defun
C-M-b   backward-sexp
C-M-c   exit-recursive-edit
C-M-d   down-list
C-M-j   indent-new-comment-line
C-M-k   kill-sexp

These are designed to navigate/edit lisp code. (See: How to Edit Lisp Code with Emacs.)

Note the M- is the syntax for Meta key. It is a key on Lisp keyboards. Today, by default, the Alt key on PC keyboards is interpreted as Meta.

Number Pad Keys

There are 56 bindings for the keys on the numberical keypad. They have notation kp-. Example:

<C-S-kp-1>  <C-S-end>
<C-S-kp-2>  <C-S-down>
<C-S-kp-3>  <C-S-next>
...
<M-kp-next> <M-next>
<S-kp-down> <S-down>
<S-kp-end>  <S-end>
<S-kp-home> <S-home>
<S-kp-left> <S-left>
<S-kp-next> <S-next>
...
<kp-0>    0
<kp-1>    1
<kp-2>    2
...
<kp-add>  +
<kp-decimal>  .
<kp-delete> C-d
<kp-divide> /
<kp-down> <down>
<kp-end>  <end>

All these are key translations and does not do anything special. For example, keypad 0 is the same as the 0 key on the main typing area. So, the code <kp-0> translates to 0. When Num Lock is off, the 4 key on numpad is the key. So, the code <S-kp-left> (which means holding down Shift then pressing the on the numpad) simply translates to <S-left>.

Ctrl, Meta, Esc keys

Now, there are 300+ bindings left. 98 of them are Meta keys with the notation M-. 105 of them are Ctrl keys with the notation C-. 20 of them are Esc keys with the notation ESC. Some of the Ctrl definitions is Ctrl with mouse button.

Ancient Special Keys

Now, there about 100 bindings left.

Here's the file: gnu_emacs_keybinding_rest.txt.

Some keys interesting are these:

<again>   repeat-complex-command
<begin>   beginning-of-buffer
<compose-last-chars>    compose-last-chars
<copy>    clipboard-kill-ring-save
<cut>   clipboard-kill-region
<delete-frame>  handle-delete-frame
<deletechar>  delete-char
<deleteline>  kill-line
<escape>  ESC
<linefeed>  C-j
<execute> execute-extended-command

<f16>   clipboard-kill-ring-save
<f18>   clipboard-yank
<f20>   clipboard-kill-region
<find>    search-forward
<header-line> Prefix Command
<iconify-frame>     ignore-event
<insertchar>  overwrite-mode
<insertline>  open-line
<language-change>   ignore
<left-fringe> Prefix Command
<lwindow> ignore
<make-frame-visible>    ignore-event
<menu>    execute-extended-command
<mode-line> Prefix Command
<select-window>     handle-select-window
<switch-frame>      handle-switch-frame

I think most of these are actual keys. You can still see some of these keys on Sun Microsystem's keyboard. (alas, Sun just died a year ago.) For photo of Sun keyboard, see: Sun Microsystem's “Type 6” Keyboard.

Super & Hyper?

Curiously, there's no definition for Super s- and Hyper H- keys. These are the other prominent keys on lisp keyboard other than Meta M-. (See: Emacs: How to define Hyper & Super Keys.)

How I lost my faith in Lisp

Perm url with updates: http://xahlee.org/comp/blog.html

How I lost my faith [in lisp], Ron Garret

A old comp.lang.lisp newsgroup post has been dug up by Hacker News. The post is: How I lost my faith (2002-02-19) By Erann Gat. @ Source groups.google.com.

i read it at the time it was posted, and just read it again. Note that its author, Erann Gat (aka Ron Garret), is somewhat a controversial figure in comp.lang.lisp. I think primarily having to do with the nasty fight between him and Erik Naggum. Also, i don't know the details, but he also doesn't get alone with another lisp regular Kenny Tilton.

Over all, i'll say i agree with his post there, that lisp isn't that great a language today, as i've expressed many times in the past years. My path to lisp is similar. For me, lisp started in 1998. First, there's awful amount of admiration and respect from me, even just for the association with the phrase “artificial intelligence”. Though, i never really got to the lisp-loving stage. I find problems with the lisp's list and cons in the very beginning, due to my experience with Mathematica. (a question about cons was my first post to comp.lang.scheme and flamed by one Naggum, in 1998. See: Death Of A Troll (My Memory of Erik Naggum).) For real life reasons, i never actually got to code lisp. Only untill around 2006, i started to dig seriously into emacs lisp.

For all my love of elisp, it is primarily due to it being a functional language, and the emacs environment system. I can't say the love came from the lang being lispy. Ι HATE the cons, i HATE the parens (actually i LOVE the regular nested syntax (as in Mathematica), but lisp screwed it so bad). (See: Fundamental Problems of Lisp.) And other identy of lisp that lispers perennially get excited about — the macros — i find abominable and useless. (and Schemer's call-cc, i find to be a lang design feature that i have no interest)

(everytime you use a vector instead of list/cons, the lisp fanatics stare at you like crazy. Everytime you didn't tuck-in the closing parens at end, they think you are a newb. For all the AI automation lisp is renowned for, the lispers at comp.lang.lisp still don't get the concept of automated code formatting. They diddle with code formatting just like every stupid imperative lang. This is particularly disgraceful because lisp's got such a superior syntax at maintaining structural info, which IS the main advantage of purely nested syntax. (See: A Simple Lisp Code FormatterA Text Editor Feature: Extend Selection by Semantic Unit.))

2011-03-09

Unicode Char Equivalence Support in Browsers

Perm url with updates: http://xahlee.org/comp/unicode_browser_char_equiv.html

Unicode Char Equivalence Support in Browsers

Xah Lee, 2011-03-09

It's very nice, in Google Chrome and Safari, that if you type 3 dots “...” in the find box 【Ctrl+f】, it'll find the unicode character ellipsis “…”. Here's some other equivalences:

  • e ⟺ é
  • a ⟺ à
  • i ⟺ î
  • n ⟺ ñ
  • u ⟺ ü
  • others accented letters e.g. ÀÁÂÃÄÅÆ Ç ÈÉÊË ÌÍÎÏ ÐÑ ÒÓÔÕÖ ØÙÚÛÜÝÞß àáâãäåæç èéêë ìíîï ðñòóôõö øùúûüýþÿ
  • straight double quote ⟺ curly double quote
  • 1 ⟺ ①
  • 2 ⟺ ②
  • 2 ⟺ ③
  • ... ⟺ …
  • any char is equivalent to its fullwidth version. e.g. () [] {} vs () [] {}, and fullwidth forms of punctuations “, . ; ? !” etc.

I think some of these are based on Unicode equivalence but not all.

Note: Firefox, Opera, does not support this. It's funny that IE8 support some of them. If you type “1”, it'll find “①”, but if you type “e”, it won't find “é”. (all browsers tested are latest public versions as of 2011-03-09.) You can test by 【Ctrl+f】 right on this page.

music video: Sugababes - round round

Perm url with updates: http://xahlee.org/Periodic_dosage_dir/sanga_pemci/sugababes_round_round.html

Sugababes - Round Round

Xah Lee, 2011-03-09

A fantastic dance-pop eletronica song. Heard it first from a home recorded sex tape's background music. 〔http://xahporn.org/porn/ama_blow.html〕

Sugababes - Round Round.

Title: Round Round
Date: 2002
Singer: Mutya Buena, Heidi Range, Keisha Buchanan (Sugababes)

Here's the lyrics.

 Round, round baby round, round
 Spend the night on me
 I don't need no man
 Got my kicks for free
 Realise the vibe gonna be down low (?)

I don't need nobody got my honeys
When I go round, baby round round
Spend the night on me
I don't need no man
Got my kicks for free

When you stare in my face
You're messing with my brain
If you're trying to convince me
Then you better think again
If you move to the music
The music's got to give
If its too complicated
That's the way I want to live

If you hate me
I will show you
How to break me
Into something new
If you want me
Run away now
If you stop me
Then I'll hit the ground
(Can we go, here we go)

...

When I go

Explanations - no you don't need one
Having a good time
Getting down to the fun
And it's your time
Just tonight he'll be mine
And he's gone
Yeah

People moving, bodies grooving
Never ending
Place is moving
(Don't you worry 'bout a thing all you need is off your sting)
All my girls are here with me
Worry now they've got their need
(Do you feel me, do you feel me, do you feel me, do you feel me)
Woo

...

Does it hurt, when you see
How I've done
Without you
Thought that I could belong
Never thought that you could be so cruel
All you need is some friends
Won't be by yourself
Let's go round and round and round and round and round

...

“Sugababes - Round Round” music video. Mutya Buena (the goth with 2 braids), Keisha Buchanan (the black girl), Heidi Range (the blonde)

There are about 2 lines in the lyrics i'm not sure i got correct. All web searchs turns up the same content farm spam. Is there a official print of the lyrics somewhere? If you know the correct lyric, please comment. Thanks.

“Sugababes - Round Round. mp3” amazon

2011-03-08

Google js lib Closure

Discovered Google javascript library Closure Library. It's used by many of Google's services (e.g. gmail, maps, docs, sites, books, reader, blogger, picasa) Worth looking into. It's a heavy weight one. It even supports drag'n'drop. At http://code.google.com/closure/library/.

The other most popular js lib is jQuery. But in comparison, it's peanuts. See: jQuery Basics Tutorial.

emacs sgml-delete-tag bug

sgml-delete-tag bug

discovered a bug in html-mode. If you have this:

<p>some'</p>

and your cursor is at the beginning of line, then press 【Ctrl+c Ctrl+d】 (sgml-delete-tag), it'll also delete the apostrophe.

This is “GNU Emacs 23.2.1 (i386-mingw-nt6.0.6002) of 2010-05-08 on G41R2F1”

some problems with emacs slowdown

Perm url with updates: http://xahlee.org/emacs/emacs_font_slowdown.html

Emacs: some problems of slowdown (tabbar, font)

Xah Lee, 2011-03-08

Sometimes i noticed that emacs slows down. The problem is hard to track. Here's my experiences.

tabbar-mode slowdown. I've noticed in past years that tabbar-mode slows down emacs. Just re-affirmed it today. In today's case, it significantly slows down your emacs if you have lots of files open. For example, i have 300 files open. (they are opened by batch Find & Replace (dired-do-query-replace-regexp)) Then, calling “M-x” (execute-extended-command) takes 3 seconds to show the prompt. Opening a file also take a few seconds. This is not always reproducible though. For example, right now i still have 300+ files open, but the problem went away.

If you have desktop-save-mode on, and you have 300 files from last session. Starting emacs (with tabbar-mode one) can take a long time.

Set Keyboard Repeat Rate to Test Slow Down. When slow down happens, i notice it because emacs can't keep up with your typing. Here's a sure way to test a slow down. I have my key repeated rate set to the highest in Windows (or Mac), and onset delay to the lowest. Normally, if i hold a key, it'll repeat fast something like 18 char per second, and the chars appears on the screen continuously (as opposed in bursts). You can also test this by holding the right arrow key on a long paragraph.

When emacs slow down happens, the screen update happens in bursts. e.g. I hold a key, nothing happens in screen, and every 1 seconds or 2 they appear.

Font May Cause Slowdown. Also, related but i don't know exactly what's going on yet... sometimes i noticed a slow down in emacs that has to do with what font you are using, and probably related to what chars you have in the current buffer (e.g. Chinese or unicode math symbols). When this happens, if i switch font to “Lucida Sans Unicode-10”, then the problem goes away. The worst seems to be the default font Courier New.

Same Buffer in Multiple Windows May Cause Slowdown. If you have the same buffer displayed in more than one windows (what emacs call “frames”), that may also slow down.

This is “GNU Emacs 23.2.1 (i386-mingw-nt6.0.6002) of 2010-05-08 on G41R2F1”, “ErgoEmacs distribution 1.9.2”

I don't think this problem is specific to ErgoEmacs, but probably Windows+Emacs, or just my particular machine.

If you want to experiment with the font problem, you'll need to install Lucida Sans Unicode and the DejaVu font set. (you should anyway, they are the best unicode font.) Lucida Sans Unicode is the best variable width unicode font. DejaVu Mono best for fixed-width. (See: Best Fonts for Unicode for download location.)

then, to switch between your fav fonts, use this code:

(defun cycle-font-2 (num)
  "Change font in current frame between 2 fonts.."
  (interactive "p")

  ;; this function sets a property “state”. It is a integer. Possible values are any index to the fontList.
  (let (fontList fontToUse currentState nextState )
    ;; (setq fontList (list "DejaVu Sans Mono-10" "Lucida Console-10" )) ; fixed-width
    ;; (setq fontList (list "DejaVu Sans-10" "Lucida Sans Unicode-10" )) ; variable-with
    (setq fontList (list "DejaVu Sans Mono-10" "Lucida Sans Unicode-10" )) ; variable-with / fixed-width
    (setq currentState (if (get 'cycle-font-2 'state) (get 'cycle-font-2 'state) 0))
    (setq nextState (% (+ currentState (length fontList) num) (length fontList)))

    (setq fontToUse (nth nextState fontList))
    (set-frame-parameter nil 'font fontToUse)
    (redraw-frame (selected-frame))
    (message "Current font is: %s" fontToUse )

    (put 'cycle-font-2 'state nextState)
    )
  )

(For detail about this code, see: How to Quickly Switch Fonts in Emacs.)

Give it a hotkey. (see: Emacs: How to Define Keyboard Shortcuts.)

2011-03-07

google computer programing style guide

Perm url with updates: http://xahlee.org/comp/blog.html

Discovered Google computer programing style guide. It has style guide for lang including: C++, JavaScript, Objective-C, Python, XML. At http://code.google.com/p/google-styleguide/.

The ones am interested are:

I spent 30 min to read the js one. Haven't finished yet. Very good reading. If you are not a expert of the lang, you learn something about the lang too.

I'll be reading them all.

Emacs: Zap Gremlins (UNICODE chars ⇒ ASCII)

Perm url with updates: http://xahlee.org/emacs/emacs_zap_gremlins.html

Emacs: Zap Gremlins (UNICODE chars ⇒ ASCII)

Xah Lee, 2011-03-07

This page shows a little function that changes unicode string into ASCII. For example “passé” becomes “passe”, “voilà” becomes “voila”.

When refactoring my elisp code last week, i split out this little function. It turns unicode chars into roughly equivalent ASCII ones. I needed this because the open source dictionary will choke on words with unicode chars. (See: Emacs Dictionary LookupProblems of Open Source Dictionaries.)

I remember, in the popular Mac editor BBEdit i used 10 years ago before emacs, there's such a command in the menu called “zap gremlins”. Though, i'm not aware there's one in emacs, but might be. Anyway, here's the code:

(defun asciify-string (inputstr)
  "Make unicode string into equivalent ASCII ones.
Todo: this command is not exhaustive."
  (let ()
   (setq inputstr (replace-regexp-in-string \\|à\\|â\\|ä" "a" inputstr))
   (setq inputstr (replace-regexp-in-string \\|è\\|ê\\|ë" "e" inputstr))
   (setq inputstr (replace-regexp-in-string \\|ì\\|î\\|ï" "i" inputstr))
   (setq inputstr (replace-regexp-in-string \\|ò\\|ô\\|ö" "o" inputstr))
   (setq inputstr (replace-regexp-in-string \\|ù\\|û\\|ü" "u" inputstr))
    inputstr
    ))

You might improve this code, as right now it's puny. It doesn't consider many other unicode chars. Here's the common non-english letters: ÀÁÂÃÄÅÆ Ç ÈÉÊË ÌÍÎÏ ÐÑ ÒÓÔÕÖ ØÙÚÛÜÝÞß àáâãäåæç èéêë ìíîï ðñòóôõö øùúûüýþÿ. You might also consider changing unicode bullet “•” to “*”, and others such as “→” to “->”, “≥” to “>=”, etc.

Depending on how you want the function to go, you can think of its primary purpose as removing all non-ascii chars. So, at the end, you simply delete any non-ascii char that hasn't been transcoded. (use the emacs regex [:nonascii:]) Or, you can consider its primary purpose as transcoding. This means, leave untranslated unicode chars as is.

Also, right now it's a function that takes in a string. You might also create a version that works on region, or better yet, works on text selection if there's one, else on current word (or line, or paragraph, or buffer, your design call). (For how, see: Emacs Lisp: Using thing-at-point.)

Or, perhaps you know someone has written this somewhere?

Accumulator vs Parallel Programing

When looking at my code, another thing that piqued my interest is that, notice how the algorithm is of sequential nature? The paradigm is similar to what's called “accumulator” or “iteration”. Recently, i watched Guy Steele's talk on parallel programing (See: Guy Steele on Parallel Programing.) and learned that the iteration style is very difficult for compiler to automatically generate parallel code.

A better way to write it for parallel programing, is to “map” a char-transform function to the string. (in elisp, a string datatype is a type of array datatype. Array and List are both “sequence”. The “mapcar”'s second argument can be any “sequence” datatype.) It will probably become slower, but it'll be good when someday emacs lisp becomes Scheme Lisp or something.

2011-03-06

Motorola TV Ad: Apple 1984

Perm url with updates: http://xahlee.org/funny/motorolla_vs_apple_1984.html

Motorola TV Ad: Apple 1984

Xah Lee, 2011-03-06

“Welcome to the new decade: Java is a restricted platform, Google is evil, Apple is a monopoly and Microsoft are the underdogs” — Phil Nash 2010-08-14

This is just too good a commercial. Motorola pokes fun at Apple.

“Motorola - Empower the People”

Notice that ubiquitous white iPad earphone? That's Apple.

Here's Apple's TV ad in 1984.

Apple's 1984 commercial.

In the 1980s, the Big Brother in computing industry alludes to IBM.

Nineteen Eighty-Four is dystopian novel by a George Orwell. It brought us the term Big Brother — a government that practices total surveillance. For a short essay by Orwell, see: Politics and the English Language.

A fantastic scifi movie that depicts Big Brother in a similar way is Brazil (film) (1985), directed by Terry Gilliam. amazon