Xah Emacs Blog Archive 2011-10 to 2011-10

Emacs Bug: “isearch-forward” Doesn't Respect New Value of “case-fold-search” When Repeating Last Seearch

added to Emacs Misc Bugs.

Several updates:

Updated and added a example of “read-from-minibuffer” at Elisp: How to Write Commands.

Major rewrite: Elisp: Syntax Color Source Code in HTML.

Space-cadet Keyboard and Lisp Machine Keyboards

Updated. Elisp: Writing a google-earth Function.

Elisp: Loop Thru a List

Learned 2 elisp functions: {dolist, number-sequence}.

dolist lets you loop over a list with a variable. Here's sample usage:

(dolist (x '(1 2 3)) (insert (number-to-string x))) ; inserts "123"

number-sequence just generates a list of numbers:

(number-sequence 3 8) ; ⇒ (3 4 5 6 7 8)

Both are parts of emacs 23, but they are implemented as lisp macros. (Call describe-function to see their doc and source code.)

Normally, you can loop with a while, but the dolist syntax is leaner.

I learned them from Jon Snader (jcs) blog. See:

Jon is a expert in Common Lisp, Scheme Lisp, and C. He often give useful tips about emacs. Very well written.

Thanks to Jon Snader (aka jcs)'s solution on last week's problem and some discussion, now updated: Elisp: Replace Digits by Subscript.

Emacs Cygwin gunzip Problem

Emacs Lisp, Perl, Python: Building a Multiplication Table

So, i was reading Jon Snader (aka jcs)'s blog At irreal.org, which lead me to watch a video of so-called VimGolf, but using emacs:

The task is to generate a multiplication table like this:

1   2   3   4   5   6   7   8   9   10
2   4   6   8   10  12  14  16  18  20
3   6   9   12  15  18  21  24  27  30
4   8   12  16  20  24  28  32  36  40
5   10  15  20  25  30  35  40  45  50
6   12  18  24  30  36  42  48  54  60
7   14  21  28  35  42  49  56  63  70
8   16  24  32  40  48  56  64  72  80
9   18  27  36  45  54  63  72  81  90
10  20  30  40  50  60  70  80  90  100

I thought this is interesting.

For this particular problem, i thought the most simple and practical way is just to use a programing language to spit it out. So i quickly wrote this:

(let (x i )
  (setq x 1 )
  (while (< x 10)
    (setq i 1 )
    (while (< i 10)
      (insert (format "%2d " (* x i)))
      (setq i (1+ i) )
      )
    (insert "\n")
    (setq x (1+ x) )
    )
  )

then just select the text and call “eval-region”. Or, with cursor at the end, call “eval-last-sexp” 【Ctrl+x Ctrl+e】.

the output is this:

 1  2  3  4  5  6  7  8  9
 2  4  6  8 10 12 14 16 18
 3  6  9 12 15 18 21 24 27
 4  8 12 16 20 24 28 32 36
 5 10 15 20 25 30 35 40 45
 6 12 18 24 30 36 42 48 54
 7 14 21 28 35 42 49 56 63
 8 16 24 32 40 48 56 64 72
 9 18 27 36 45 54 63 72 81 

Lisp is a bit verbose for this kinda thing due to the nested syntax. But you can write it in {Python, Ruby}, or any dynamic languages quickly, and press a button to have emacs call it and insert the result.

here's a Python version:

for x in range(1,10):
    for i in range(1,10):
        print "%(n)2d" % {"n":x*i},
    print "\n",

You can save this in a file, for example temp.py. Then type 【Ctrl+u Alt+| python temp.py】, then it'll insert the output in your cursor position. (The 【Alt+|】 is the hotkey for “shell-command-on-region”. The 【Ctrl+u】 is “universal-argument”. When the command “shell-command-on-region” gets a empty argument, it'll insert output in buffer. Normally, it places output in the “*Messages*” buffer.)

Here's a Perl version:

for $x (1..9) {
  for $i (1..9) {
    printf "%3d", $x*$i, " ";
  }
  print "\n";
}

Emacs Lisp Exercise of the Day: extract-url

Here's a fun exercise: write a command “extract-url” that will extract all URL in a text selection and put them in a buffer.

For example, suppose you have this text:

<div>1, <a href="../cats.html">cats</a>, <a href="http://en.wikipedia.org/wiki/Idiom">Idiom</a>, <a class="big" href="house.html">house</a></div>

After calling the command, you'll get in a separate buffer this text, one URL per line:

../cats
http://en.wikipedia.org/wiki/Idiom
house.html

I'll post a solution tomorrow.

Solution at: Elisp: Command to Extract URL.

Elisp: replace-digits-by-subscript Command

Famous Emacser, Steve Yegge, wrote a rant on Google that's circulating wildly now. Steve Yegge's Google Platforms Rant.

Richard Stallman on Steve Jobs's Death

GNU Emacs dev, Richard Stallman, Personality Cult

got a email from GNU emacs bug people about a bug being fixed.

the bug was filed 2 years ago, and was fixed 1 year ago. See: bug#4408.

i've filed perhaps 40 in the past. Perhaps 10 are taken and fixed.

in general, got no thanks, no appreciation. This isn't just me. I've been involved with GNU Emacs dev (casually) for 5 years now. I've seen and read enough to know. Many developers who contributed far more to emacs, with code and packages, are treated like that. Under communism, of the FSF branch, you are just a node for the good of all. The people who got thanked, perhaps the only person, is Richard Stallman. Nobody else. His communism ideology made it that way. He's now a Cult of personality.

am pretty tired Richard Stallman and his leadership.

am not likely to send any bug reports or anything anymore.

Anonymous wrote to ask how to write a emacs command to show the decimal value of a hexadecimal string under cursor in source code. Here's answer at bottom: Elisp: Converting Decimal and Hexadecimal Numbers.

Elisp: a Function That Works on String or Region.

Emacs Lisp's “if” function grammar.

Emacs Lisp's “if” function has a annoying grammar.

One would expect it to take 3 arguments, no more and no less, like this:

(if ‹test›
 ‹expression for true›
 ‹expression for false›
)

But it's actually takes many more args. From the 3rd args onward are all expressions for false. Like this:

(if ‹test›
 ‹expression for true›
 ‹expression for false›
 ‹expression for false 2›
 ‹expression for false 3›
 …
)

Here's a test code you can check:

(if nil
  (message "false")

  (message "true")
  (message "so true")
  (message "yes really")
  )

You can run the code by selecting it, then call “eval-region” 【Alt+x】. You can switch to the “*Messages*” buffer by 【Ctrl+h e】.

Is Common Lisp and Scheme Lisp the same way?

I think i'd prefer the simpler, logical, form: (if test true false).

Common Lisp is the same way. Scheme Lisp uses the logical form. Source www.r6rs.org. (thanks to Andrew Hyatt.)

Like my tutorial? Put $5 at patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

Ask me question on patreon