2011-11-12

Madonna - American Life

“Madonna - American life”. (censored version)

Note that there are a few versions. The most censored version simply show different flags in the background throughout the song. The above is a censored version, ending in George Bush catching the grenade. There's another uncensored version, with ending showing many gruesome pictures of injured and bloody soldiers and civilians.

For lyrics, updates, and Eurythmics's “the King and Queen of America”, see: Madonna - American Life

2011-11-10

Google Doing Research: What is a Browser?

“What is a Browser”

Fuck the tech geekers, the hackers, the linux/unix, emacs, fanatics, those who hog on slashdot, reddit, hacker news, who adamantly tell us how user interface should be, how code should be formatted, how we should use the web. Fucking idiotic scumbags.

Editing Lisp Code with ErgoEmacs Keybinding without ParEdit

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

This page shows you how to edit lisp code using ErgoEmacs keybinding and without paredit mode.

For programing lisp in emacs, there's a mode called “paredit-mode”, see: ParEdit.

Jorge Dias posted in twitter, asking how to get it to work with ErgoEmacs Keybinding, because a lot keys clash.

I never tried ParEdit mode, because i'm not sure by principle it is efficient. Here's what i do.

Setting Up Bracket Highlighting

When cursor is on a bracket, the entire bracketed region is highlighted. For how to setup, see: How to Edit Lisp Code with Emacs.

Brackets Are ALWAYS Inserted/Removed in Pairs

Any brackets are ALWAYS inserted and removed in pairs. For how to setup, see: How to Edit Lisp Code with Emacs.

Navigating Brackets

To navigate nested brackets as tree structure, use built-in keys provided by GNU Emacs. e.g. 【Ctrl+Alt+】【Ctrl+Alt+】【Ctrl+Alt+】【Ctrl+Alt+】. For detail, see: How to Edit Lisp Code with Emacs.

To move cursor to previous/next brackets freely, i use these keys {【Alt+】,【Alt+】,【Alt+】,【Alt+】}. See: Emacs: Commands and Keys to Navigate Brackets.

Selecting/Moving/Deleting Lisp Expressions

To select a lisp expression, put cursor on the beginning bracket, press the key 【Alt+8】 to select the entire expression, 【Alt+x】 to cut.

Both are in ErgoEmacs keybinding. For the code and detail, see:

Remove Empty Lines; Compact Ending Brackets

To remove empty lines, type 【Alt+w】 (shrink-whitespaces)

To compact ending brackets, place your cursor there then press 【Alt+q】 (compact-uncompact-block)

Pretty Format Lisp Code

Another principle i use is NEVER SPEND EFFORT TO MANUALLY PRETTY FORMAT CODE on a line-by-line basis.

When you need to make the format pretty, do them in batch on a region. Select text or whole file, then use the built-in command 【Ctrl+Alt+\】 (indent-region).

Ideally, the formatting should be done automatically, as in auto-fill-mode and Mathematica. I haven't coded this in ErgoEmacs yet. See:

elisp abbrev + auto-complete-mode + yasnippet

For name completion and function templates, i use {elisp abbrev, auto-complete-mode, yasnippet}, all are bundled and with ErgoEmacs and already configured.

  • elisp abbrev setup lets you expand abbrev to full function name. e.g. bsnp ▮buffer-substring-no-properties ▮ (See: YASnippet Templates for Emacs Lisp Mode.)
  • YASnippet lets you expand from lisp function name to a complete expression template. e.g. search-forward▮(search-forward "▮" &optional BOUND NOERROR COUNT).
  • “auto-complete-mode” lets you complete any lisp function or variable names. e.g. 【buffer- Tab】 shows a menu of elisp symbols that start with “buffer-”.

2011-11-09

Emacs Lisp: Writing a Command to Extract URL

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

This page shows you how to write a emacs lisp command to extract all URLs in a HTML file. If you don't know elisp, first take a look at Emacs Lisp Basics.

Problem Description

Write a command “extract-url”. When called, all URLs in a text selection will be listed in a separate pane, one per line.

For example, suppose you have this text:

<div>1, <a href="iraq_pixra2.html">2</a>, <a href="http://en.wikipedia.org/wiki/Idiom">Idiom</a>, <a href="iraq_pixra3.html">3</a></div>

After calling the command, you'll get in a separate buffer showing this text:

iraq_pixra2.html
http://en.wikipedia.org/wiki/Idiom
iraq_pixra3.html

Solution

There are many ways to code this. Here's one:

(defun extract-url (p1 p2)
  "Print all URLs in region P1 P2.

When called interactively, use text selection as input, or
current paragraph.

Output in a buffer named 「*extract URL output*」.

This function assumes the input is HTML text.  WARNING: this
function extract all text of the form 「href=\"…\"」 only. It
does not extract {「href='…'」, 「src='…'」} or consider whether
that's plain text or proper tag."
  (interactive
   (if (region-active-p)
       (list (region-beginning) (region-end))
     (let ((bds (bounds-of-thing-at-point 'paragraph)))
       (list (car bds) (cdr bds)) ) ) )
  (let ( p3 p4 urlStr)
    (with-output-to-temp-buffer "*extract URL output*"
      (save-excursion 
        (save-restriction 
          (narrow-to-region p1 p2)
          (goto-char (point-min))
          (while
              (re-search-forward "href" nil t)
            (search-forward "=" nil t)
            (search-forward "\"" nil t)
            (setq p3 (point))
            (search-forward "\"" nil t)
            (setq p4 (- (point) 1))
            (setq urlStr (buffer-substring-no-properties p3 p4))
            (princ urlStr)
            (terpri) ) ) ) ) ))

Here's how it works.

Using “interactive” to get Arguments

First note that the function takes 2 arguments p1 p2 that is the boundary of a buffer.

When called interactively, we want {p1, p2} to be the text selection if there's one, or current paragraph.

Emac's “interactive” function is a way to get arguments for functions called interactively. It just need to return a list. Emacs will use the list elements as arguments. In our case, it's done by:

(interactive
   (if (region-active-p)
       (list (region-beginning) (region-end))
     (let ((bds (bounds-of-thing-at-point 'paragraph)))
       (list (car bds) (cdr bds)) ) ) )

(See: Emacs Lisp: Using thing-at-point.)

Output to Separate Buffer

To output to a separate buffer, we use “with-output-to-temp-buffer” and “princ”, like this:

(with-output-to-temp-buffer "*extract URL output*"
…
(princ urlStr)
…
 )

With (with-output-to-temp-buffer «buffer»), and printing functions will print to that buffer. The printing function we used is “princ”, which print lisp objects to a human readable form. (See: Emacs Lisp: print, princ, prin1, format, message.)

Extracting URL

To extract URL, there are many approaches. Here, we just search for “href” then search for text enclosed in straight quotes, mark their boundary, then print the text between them. Like this:

(while
    (re-search-forward "href" nil t)
  (search-forward "=" nil t)
  (search-forward "\"" nil t)
  (setq p3 (point))
  (search-forward "\"" nil t)
  (setq p4 (- (point) 1))
  (setq urlStr (buffer-substring-no-properties p3 p4))
  (princ urlStr)
  (terpri) )

Simple Solution

Note that this solution is very simple and practically useful, but isn't a fully correct solution. For example, it does not get URL that's enclosed by 'single straight quotes'. Nor does it get URL inside “img” tags or javascript tags with the “src” attribute src="http://xahlee.org/emacs/…".

You can easly modify it by just adding more “while” block, change the double quote to single, and also “href” to “src”. However, that approach won't have the URL in order they appears in the text.

Also, if you have text such as href = "34";, without any tags, it will also grab the “34” as URL.

A robust solution will require few hundred lines.

If you have get-selection-or-unit installed, you can replace the (interactive …) part by:

(interactive
   (let ((bds (get-selection-or-unit 'block)))
     (list (elt bds 1) (elt bds 2) ) ) )

This improves the code by selecting a block of text delimited by empty lines. It saves you a few keys to select a region. Emacs's “paragraph” concept as used by “thing-at-point” is dependent on current major mode's syntax table, which may not be useful. For example, paragraph in “html-mode” is very weird.

2011-11-08

the Skin I Live In

Perm url with updates: http://xahlee.org/arts/the_skin_i_live_in.html

“The Skin I Live In” (La piel que habito) trailer

Watch it, feel the sickness, and meanwhile, love the eerie music. Pretty sick, though, not in the way you think.

Here's my synopsis: mother, scientist, daughter, skin. Rape, death, robbery, sex change operation.

Here's a review from Wikipedia The Skin I Live In:

Kirk Honeycutt, writing for The Hollywood Reporter, said “Along with such usual Almodóvar obsessions as betrayal, anxiety, loneliness, sexual identity and death, the Spanish director has added a science-fiction element that verges on horror. But like many lab experiments, this melodramatic hybrid makes for an unstable fusion. Only someone as talented as Almodóvar could have mixed such elements without blowing up an entire movie.” Honeycutt continued: “The film's design, costumes and music, especially Alberto Iglesias' music, present a lushly beautiful setting, which is nonetheless a prison and house of horror. Almodóvar pumps his movie full of deadly earnestness and heady emotions.” David Gritten notes Almodóvar “reaches out tentatively into unexplored genre territory—horror … Yet despite squirm-worthy moments … the promise of horror gives way to Almodóvar's broader, familiar preoccupations: identity, blood ties, disguises and genetic traits.” According to Gritten, “A list of the story's various elements—date rape, murder, secrets, lies, mystery parents, gender ambiguity, unbreakable emotional bonds—confirms The Skin I Live In as essentially a melodrama. Yet Almodóvar's story-telling is nowhere near as shrill as it once was: as a mature artist, he has refined his skills to a point where these soap-opera tropes assimilate smoothly into a complex whole…. Typically for Almodóvar, it all looks ravishing, thanks to production designer Antxon Gómez and cinematographer José Luis Alcaine. All three men have the gift of investing mundane objects with a unique sheen; here even surgical instruments, about to be used malevolently, assume a dreamy, otherworldly quality. The Skin I Live In is the work of a master near the top of his game.”

Note, that the eerie musc is by Alberto Iglesias.

Blonde vs Blond, Brunette vs Brunet

Blonde vs Blond, Brunette vs Brunet. See: http://wordy-english.blogspot.com/2011/11/blonde-vs-blond-brunette-vs-brunet.html

google earth of Shanghai

Google Earth Shanghai
Screenshot of Google Earth on Shanghai, China

See: Google Earth Mouse Navigation and Keys.

more emacs keyboard macro examples

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

Kmacro Example: Repeatedly Eval a Emacs Lisp Expression

Here's another example of kmacro use.

I have a function “insert-random-uuid” (See: Emacs Lisp Exercise: insert-random-uuid.) I want to call it hundreds of times to see its output. Let's just say i want to call the elisp expression (random 100) one hundred times.

One way is to write a elisp command on the spot, like this:

(defun xx-random-test ()
  "test"
  (interactive)
  (dotimes (ii 100) (insert (format "%d " (random 100))))
  )

But that takes 5 minutes to write, plus you need to have elisp knowledge. You can use a kmacro to do this fast, with the following steps.

  • Start kmacro 【Ctrl+x (】.
  • Type 【Ctrl+u】, then call “eval-expression”, with this expression (random 100). The 【Ctrl+u】 will make “eval-expression” insert its result in current buffer.
  • Type a space.
  • End kmacro 【Ctrl+x )】.
  • Type 【Ctrl+u 100】 then call “kmacro-end-and-call-macro” 【Ctrl+x e】.