2011-11-05

brain hat

brain hat 4 medium2
“brain hat” by lani-rae. Source www.ravelry.com

emacs math symbol input mode, v1.3.2

New major version of math symbol input mode is out, at v1.3.2.

Feature additions:

  • Added support of input using unicode names. For example: greek small letter alpha
  • Added support of input using decimal forms, for examples: {955, #955} or hexadimal forms {x3bb, #x3bb}. XML entities forms such as {λ, λ, alpha} are still supported.
  • Added about 5 more subscript symbols, e.g. {ₐ ₑ ₕ ᵢ ⱼ ₖ ₗ ₘ ₙ ₒ ₚ ᵣ ₛ ₜ ᵤ ᵥ ₓ}. Their abbrev start with “_”.
  • When no valid input are found, list of valid abbrevs shows in a separate pane.

Buy it at: Emacs Unicode Math Symbols Input Mode (xmsi-mode).

If you have bought this package before, just email me with subject line “xmsi-mode update”, then i'll email it to you. Your support is much appreciated.

2011-11-04

Emacs Lisp Exercise: insert-random-uuid

This week's exercise is to write a function “insert-random-uuid”. When called, it should insert a UUID. Here's some examples of UUID:

0a1cd3bc-96fa-71d1-4338-27092ca4cfa5
1e27a053-60a4-af61-f38d-9f1f123740d6
115024d2-7c74-326e-c9ec-064f42d08b31
070f1f0b-2454-3ffa-4aa2-d6e0652d03fe

basically, it's a random string of symbols 0 to 9 and a to f, arranged in 8-4-4-4-12 blocks. There are many ways to implement this. I'll post a solution in a couple of days.

If you really don't know elisp, this weekend is a good time to learn as any. This article might get you started: Emacs Lisp Basics.

2011-11-08 solution at http://xahlee.org/emacs/elisp_generate_uuid.html

Emacs Lisp Programing: Beware of Region Boundary Change

Perm URL with updates: http://xahlee.org/emacs/elisp_beware_of_region_boundary_change.html

Got stung by region in emacs, wasted about 4 hours. Here's what i learned.

Whenever you work in a region, remember that the boundaries of the text that you are interested is changed when you add or remove text in that region. For example, suppose p1 and p2 is the boundary of some text you are interested. After doing some change there, suppose you want to do some more change. Don't just call (something-region p1 p2) again, because p2 is no longer the correct boundary.

Use save-restriction and narrow-to-region, like this:

(save-restriction
  (narrow-to-region pos1 pos2)
  (something1-region (point-min) (point-max))
  (something2-region (point-min) (point-max))
  …
)

Last week, i updated the article Emacs Lisp: Syntax Color Source Code in HTML and re-implemented several of my personal commands, and today found that it behaved incorrectly: it deleted text outside of a region!

Here's a function dehtmlize-span-region effected by this:

;; WRONG! INCORRECT! DO NOT USE
(defun dehtmlize-span-region (p1 p2)
  (interactive "r")
  (replace-regexp-pairs-region p1 p2 '(["<span class=\"[^\"]+\">" ""]))
  (replace-pairs-region p1 p2 '( ["</span>" ""] ["&amp;" "&"] ["&lt;" "<"] ["&gt;" ">"] ) )
  )

Note how innocuous it looks. p1 and p2 are my region boundaries, and i work on them. Here's the correct code:

;; correct
(defun dehtmlize-span-region (p1 p2)
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region p1 p2)
      (replace-regexp-pairs-region (point-min) (point-max) '(["<span class=\"[^\"]+\">" ""]))
      (replace-pairs-region (point-min) (point-max) '( ["</span>" ""] ["&amp;" "&"] ["&lt;" "<"] ["&gt;" ">"] ) ) ) ) )

Looks so trivial a thing, but i spent 4 hours debugging to realize the fact that when i modified the text in region by my first function, the region boundary has changed. I remember, few years ago, i also spent half a day on region problems and wrongly concluded that narrow-to-region should be avoided in elisp program.

2011-11-03

Unicode Symbols for Space

Here's some unicode chars to represent Space:

CharUnicode NameCode PointComment
·MIDDLE DOTU+00B7Useful when you want to show space as visible chars
OPEN BOXU+2423Useful when indicating keystrokes
SYMBOL FOR SPACEU+2420Useful in programing contexts. This usually indicates the ASCII 32.
BLANK SYMBOLU+2422

For more proper symbols to represent tabs, return, cursor pointer, …, in contexts of keyboard input, keyboard key symbol, visible glyph in text, programing character set, etc, see:

Klein Bottle Opener by Bathsheba

klein bottle opener klein bottle opener 2
klein bottle opener.

These are made by Bathsheba. You can buy it on her site, at: www.bathsheba.com.

Klein bottle is a exotic math surface that has only one side. That is, a ant crawling on the surface can get to the other side without crossing any edge. (the natural space of Klein bottle is 4th dimension. In 3-dimension, as in the picture, the ant would have to pass wall. For more about Klein bottle and a app for interactive rotation, see: Klein Bottle.)

Note: Bathsheba has also been creativen in Second Life.

ammonite 75
Flying Ammonite in Second Life by Bathsheba.

She has also made a Seaslug Ride. To see more pics of her Second Life creation and location to buy, see: Seaslug RideFlying Ammonite.

tip for geeks: seeing an attractive girl

Today's tip for geeks.

Q: What to do when you see an attractive girl?

A: Rush out of sight quietly.

a Choker Romance

cloisonne enamel choker
“Cloisonne Enamel Choker” Source etsy.com

a Choker Romance

2011-11-02

Samba's New Logo

Samba
Samba's old logo.
Samba Logo
Samba's new logo, 2011-05

The old logo is a much better design. It got spunk. The new is boring and lame.

For collection of logos of software, see: Logo Design & Software Industry.

emacs align and sort solutions

Emacs: solutions for last week's align and sort problem. See: http://xahlee.blogspot.com/2011/10/emacs-golf-align-and-sort.html

Big thanks to Jon Snader and “jm” for the excellent solutions.

feminism = modern scumbag

post removed because it might violate Google's ad program policy. See here instead: 〈Xah's Sex Blog〉 http://xahlee.org/sex/blog.html

2011-11-01

never date a girl who wears a choker

Have you heard this ghost story? The guy met a ravishingly beautiful girl, slept with her, then next day, waking up, she's not moving. When he unclasped the band-styled necklace she's wearing, her head rolled off on the floor?

Moral: never date a girl who wears a chocker.

The Adventure of the German Student by Washington Irving, 1824.

and, check out on Google Map the places where the public execution by guillotine was.

PS is there a name for the style of necklace that covers around the neck? Collar-styled necklace?

am looking for the correct word, e.g. the term used among necklace merchants, fashion critics, or among Haute Couture community.

For this Halloween, thou shall read horror! My other favorite is: The Masque of the Red Death , by Edgar Allan Poe, 1842.

Remember to have a dictionary installed in your browser so you can lookup words freely. See: Online English Dictionary Tools.

Math: ID System, Number Base vs Number of Digits

plot digits vs base
δ = Log[10^10]/Log[β]. Number Base β vs Number of digits δ. id_system_number_base_vs_number_of_digits.nb

see: http://xahlee.org/math/id_system_number_base_vs_number_of_digits.html

2011-10-30

Emacs Golf: Align and Sort

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

Emacs: Aligning Text and Sorting by Fields

, 2011-11-02

This page shows a practical example of using {“align-regex”, “sort-regexp-fields”, “sort-columns”}.

Problem Description

So today i was trying to understand the sizes of hectare and km². I got this list:

California 423,970 km²
Taiwan 36,008 km²
Japan 377,944 km²
Germany 357,021 km²
Iraq 438,317 km²
Iran 1,648,195 km²
Korea (North+South) 219,140 km²
Mexico 1,964,375 km²

but i want it to be in this form:

Taiwan                 36,008 km²
Korea (North+South)   219,140 km²
Japan                 377,944 km²
Germany               357,021 km²
California            423,970 km²
Iraq                  438,317 km²
Iran                1,648,195 km²
Mexico              1,964,375 km²

How would you do it? I do not have a solution.

Recently, Jon Snader (jcs), Mickey Petersen, Tim Visher are all doing emacs golf. It's interesting. Their blogs are:

Solution

Jon Snader and “jm”, provided the following solution.

Align Text with “align-regexp”

First, we align the text. Select the text first, then press 【Ctrl+u】 then call “align-regexp”, with the regexp .* \([0-9,]+\).* then choose -1 for group, 1 for spacing, and n for repeat.

Here's what it means. “align-regex” lets you align a region by a regex in complex ways.

  • The regex .* \([0-9,]+\).* matches a whole line (you can add ^ at the beginning and $ at end if you like, but is not necessary). The pattern \([0-9,]+\) captures our numbers part.
  • The prompt “Parenthesis group to modify (justify if negative):”, we answer “-1”, because we want the first matched pattern to be used for alignment, and we want it to be justified to the right (meaning, align to the right of text captured by our pattern).
  • The query “Amount of spacing (or column if negative): ”, we use 1.
  • In “Repeat throughout line?” we answer “n”.
  • Ctrl+u】 is necessary for “align-regex” to promp you for various parameters (though, “align-regex”'s inline doc does not mention it).

The result is this:

California             423,970 km²
Taiwan                  36,008 km²
Japan                  377,944 km²
Germany                357,021 km²
Iraq                   438,317 km²
Iran                 1,648,195 km²
Korea (North+South)    219,140 km²
Mexico               1,964,375 km²

Using “sort-regexp-fields” and “sort-columns”

To sort it, there are 2 methods. One is using “sort-regexp-fields”, with this regex ^.*\([0-9 ,]\{9\}\) km²$.

Another method is simply use “sort-columns”. This command sort lines by using a vertical column of text as sort key. The column is specified by the position of mark and cursor. So, place the cursor at the upper right, mark it, then move to lower left of our number, like this:

California             423,970 km²▮
Taiwan                  36,008 km²
Japan                  377,944 km²
Germany                357,021 km²
Iraq                   438,317 km²
Iran                 1,648,195 km²
Korea (North+South)    219,140 km²
Mexico              ▮1,964,375 km²

Then call “sort-columns”. We got our desired result:

Taiwan                  36,008 km²
Korea (North+South)    219,140 km²
Japan                  377,944 km²
Germany                357,021 km²
California             423,970 km²
Iraq                   438,317 km²
Iran                 1,648,195 km²
Mexico               1,964,375 km²

All these commands: {“align-regex”, “sort-regexp-fields”, “sort-columns”} will be quite useful when you need it. (Big thanks to Jon Snader and “jm” for the excellent solutions.)

Note: there's also these simpler commands if you don't know already:

  • “sort-fields”
  • “sort-lines”
  • “sort-numeric-fields”
  • “sort-pages”
  • “sort-paragraphs”

For working with text columns, “*rectangle*” commands are very useful. See: Emacs: Manipulate Column Text, string-rectangle, ASCII-Art.

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

Steps to reproduce:

  • Start emacs 23.2 (emacs -Q)
  • Create a file with this content: “myList”.
  • Call “toggle-case-fold-search” until it is disabled.
  • Call “isearch-forward” 【Ctrl+s】 and search for “mylist”.
  • Emacs won't find it. This is expected.
  • Now, call “toggle-case-fold-search” so it is enabled.
  • Press 【Ctrl+s】 twice to search “mylist” again.
  • Expected behavior is for emacs to find the word “myList”, but it doesn't. Calling “isearch-forward” and typing the word “mylist” again works.

emacs lisp tutorial updates

Several updates:

Updated and added a example of “read-from-minibuffer” at Emacs Lisp Idioms (for writing interactive commands).

Major rewrite: Emacs Lisp: Syntax Color Source Code in HTML.