2010-05-21

emacs, copy and cut current line

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

Emacs: How to Copy/Cut Current Line

Xah Lee, 2010-05-21

Last month, i wrote Emacs: Shortcut to Delete Whole Line, because i find that deleting whole line is much frequently needed. But also, i didn't mention that i often find the need to copy the current line too. Usually, this meas moving the cursor to beginning of line, mark, move to end of line, then copy. This is 4 operations. The following code will make it just a single operation:

(defadvice kill-ring-save (before slick-copy activate compile)
  "When called interactively with no active region, copy the current line."
  (interactive
   (if mark-active
       (list (region-beginning) (region-end))
     (progn
       (message "Current line is copied.")
       (list (line-beginning-position) (line-end-position)) ) ) ))

(defadvice kill-region (before slick-copy activate compile)
  "When called interactively with no active region, cut the current line."
  (interactive
   (if mark-active
       (list (region-beginning) (region-end))
     (progn
       (message "Current line is cut.")
       (list (line-beginning-position) (line-end-position)) ) ) ))

With the above code, when you do not have a text selection, copy will just copy the current line. Similar for cut. Super!

Thanks to Joseph O'Donnell for mentioning this. The code is originally from emacswiki.org SlickCopy. Apparently, this behavior is default in VisualStudio, TextMate, SlickEdit.

Was this page useful? If so, please do donate $3, thank you donors!

2010-05-20

The Dvorak Keyboard Layout

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

The Dvorak Keyboard Layout

Xah Lee, 2010-05-20

This page shows you what Dvorak keyboard layout is, its advantages, and my personal experience in using it since 1994.

The QWERTY Layout

qwerty keyboard layout

The QWERTY layout.

The keyboard layout on our keyboard is called QWERTY, because that's the letters showing on the top row. This layout was designed together with the invention of typewriter, by Christopher Latham Sholes, in 1874.

You might wonder why the letters are arrange that way, why not alphabetical? Originally, it was alphabetical. However, it was discovered that the keys would jam when people type too fast. So, the typewriter inventor re-arranged the letters, effectively slow down typing, so that typewriter wouldn't jam.

typewriter typebar jam

Early typewriter key jam.

The Dvorak Layout

In 1936, Dr August Dvorak and Dr William Dealey studied and invented the Dvorak layout, designed for efficient typing.

Dvorak keyboard layout

The Dvorak keyboard layout.

Here's some major features why it is much better:

  • Most frequently used letters are placed on the home row. So, you don't have to move fingers to type them.
  • All vows are on the home row of left hand. So, typing usually means alternating hands.
  • The top row has letters that are more often used than the bottom row, because moving fingers up is easier than moving them down.

Here's a comparison table:

Key stroke distribution
RowQWERTYDvorak
Top52%22%
Home32%70%
Bottom16%8%

So, overall, touch typing on the Dvorak keyboard is more comfortable, less error, and faster.

How to Switch to Dvorak Layout

Windows

Here's how to switch your layout on Windows Vista. It should be similar for Windows 7 or Windows XP.

Press the Start key to open the Start Menu.

Click on Control Panel.

Click on “Clock, Language, and Region”.

Click on “Regional and Language Options”. A dialog window will pop up.

Regional and Language Options window

Regional and Language Options window

Click on the “Keyboards and Languages” tab.

Add Input Language window

Add Input Language window

Click the “Change keyboards...” button.

Click “Add...” button, then select “English‣Keyboard‣United States-Dvorak”.

Mac OS X

On Mac OS X, just go to the menu “Apple‣System Preferences...”, then click the “International”, then the Input Menu tab. Check box the “Dvorak” there.

Dvorak Mac OS X dialog

The “International” dialog.

Learn Touch-Type on Dvorak

There are several websites that teach touch-typing on Dvorak. Here's a few i recommend:

My Experiences with Dvorak

I learned touch-typing on a typewriter in about 1985. For some reason, i am fascinated by typewriter. (at the time, personal computers are not around) The first typewriter i had is one from the brand name Brother.

I started to use a computer in 1990, using the QWERTY keyboard. In ~1992, i worked for over a year at Graebel Van Lines, as a secretary with typing duties, including taking dictation from sales people or managers. My typing speed, is about 400 keystrokes perm minute (80 WPM) with about 99% accuracy, as tested on a typewriter in temp agencies back then.

In ~1994 i switched to Dvorak. It took me about a month to re-learn touch typing, and 2 months to become really fluent. My profession changed from secretary to programer, so i have not had any heavy typing needs as before. Due to lack of intensive typing needs, i recall that my typing speed have not reached my previous speed for many years. Though, one thing i noticed is that Dvorak is much more comfortable, with less typing errors. I recall this vividly, because occasionally i tried to type on QWERTY on other's keyboards or at library, the first thing that jumped to me was that my fingers has to do acrobatics.

When working as a unix system administrator starting in 1998, occasionally i have to type on other people's keyboards. So, i kinda picked up qwerty touch-typing a bit. But the speed is more or less 30 wpm, due to lack of practice or need. I've heard that some Dvorak users can touch type both Dvorak and QWERTY fast, but i just never needed to.

References

Some references and other interesting sites about typing:

2010-05-19

Angel problem of John Horton Conway; converting math problem into formal language question

Perm url with updates: http://xahlee.org/math/math_problem_into_formal_language.html

Modeling Math Problem Into a Question in Formal Language

Xah Lee, 2010-05-19

Learned of The angel problem of John Horton Conway. (solved in 2006)

Here's a rephrasing of the problem, perhaps easier to understand than the one given in Wikipedia:

On a chess board of infinite size, suppose we place a king, call him the angel. And there's a devil. The angel and devil take turns. When it's angel's turn, he can move the king 2 times. When it is devil's turn, he can delete one square at any place. When a square is deleted, the king/angel will not be able to land on it. If the devil can delete enough squares completely surrounds the angel and is more than 2 steps thick, so that the angel cannot get out, the angel loses. Else the angel win.

The above is called 2-angel problem, because the angel can move 2 steps in each turn. If the angel can move n steps in each turn, that problem is called n-angel problem. Note that when the angel makes a move, she can pass thru the deleted square, just can't land on it. So, in a n-angel problem, it is not enough for the devil to circle the angel with deleted squares, but the “wall” must be n-thick.

At first i don't quite understand the problem, but in trying to understand it, it grew on me. Not a trivial problem at all. (Conway is the one who created the “Game of Life” cellular automata.)

Be sure to read the original paper by John H Conway: The Angel problem (PDF)

Apparently, it is not easy to prove that angel can win even if the angel can make 1000 steps in each turn. e.g. the 1000-angel problem. But it seems that the angel can win for n greater than 1. This is solved, proven, in 2006. However, as implied in Wikipedia, a explicit optimal strategy for the angel is not known.

Converting a Math Problem Into a Question in Formal Language

One thing i wonder about these type of problems is that, how to create a model of it in mathematical logic? More specifically, how to turn this problem into a question with Formal language. For ease of explanation, let's focus on the specific 2-angel case. So, when turned into a formal language equivalent, the question would be something like whether a particular string is possible with a given formal language.

I suspect that the process of turning such a problem as formal language problem, lots insights can be gained, though am not sure it helps deriving solutions.

When this problem is modeled as a formal language, it may become convoluted and difficult to understand. However, i think it actually makes the problem the most simple, because it condenses the problem into its logical gist. And this is why, that many math problems that are simple to state that even a child can understand the question, yet are most difficult to solve. (e.g. many number theory problems and problems in recreational math) But really, when turned into its math gist, as a logic problem, in formal language, of a bunch of strings and transformation rules, and asking whether a given string is possible, one can see that these “simple” problems are not simple to begin with, their real nature appears.

Back to the 2-angel problem. Another interesting thing is how to represent the concept of optimal strategy in terms of a question in formal language? I have no idea... but i think it be represented as generating a particular shortest or longest string, of particular pattern. Or, perhaps the least number of steps to arrive at a given string.

Simple Example Problem

To get started in this... i think it would be fruitful to start with a simple similar problem. Say, suppose you have a chess board, and the question is, is it possible to lay 3 pieces so that they form a line (horizontally or vertically)? Of course, this is trivial and even idiotic a question. Our job, is to formulate a equivalent problem in formal language. This way we'll learn what is involved.

Launch Bar and Desktop files in Windows Vista

Perm url with updates: http://xahlee.org/mswin/Windows_start_menu_file_locations.html

Windows Start Menu, Desktop, Quick Launch, File Locations

Xah Lee, 2010-05-19

Some Windows Vista tech details.

Quick Launch Toolbar

In the Taskbar, you can turn on the Quick Launch toolbar. (right click on a empty area in Taskbar, then Properties, Toolbars tab.) When Quick Launch toolbar is on, you can drag any shortcut files to it. Also, each shortcut file also automatically creates keyboard shortcut for it, that's Win+1, Win+2, ..., corresponding to the order of the shortcuts in the Quick Launch bar. You can drag them around to change their order.

The Quick Launch tool bar stores its files at:

C:\Users\xah\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch

Desktop

In Windows Vista, there are 2 folders that represents the Desktop. They are at:

C:\Users\xah\Desktop
C:\Users\Public\Desktop

Anything in the public one will also automatically show in the user's one.

Start Menu

Start Menu app list is located at:

C:\ProgramData\Microsoft\Windows\Start Menu\

Chinese mathematicians...

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

Learned of the Chinese mathematician Fan Chung (金芳蓉). Wife of Ronald Graham. Remember, Ronald is the mathematician who co-authored with Knuth the book Concrete Mathematics (amazon).

In recent years, i learned quite a lot high powered mathematicians who are Chinese. Here's some list of Chinese mathematicians:

Wikipedia actually has a list: Category:Chinese mathematicians.

Chuu-Lian Terng is wife of Richard Palais. Richard i first met online in 1997, and them both in person in 2004, and has been personal friends since.

Here's some mathematicians that i admire. Typically, it more has to do with their subjects. Geometry, discrete math, combinatorics.

Stephen i met in 1995.

There are quite a few more but off-hand these comes to mind.

There are so many mathematicians today, that one hardly know much of them. Is there a list that lists the top one thousand mathematicians?

2010-05-18

emacs lisp function to decode url percent encoding?

Perm url with updates: http://xahlee.org/js/url_encoding_unicode.html

URL Percent Encoding and Unicode

Xah Recommends:
Kindle
Amazon Kindle. Read books under the sun. Review

Xah Lee, 2010-05-24

This page discuss some issues about what characters should be percent encoded in url, and how different browsers behave.

Browser Behavior

Some test on browser's behavior on url encoding/decoding. Apparently, some browsers automatically decode parts of the percent encoding.

Copy this line:

http://en.wikipedia.org/wiki/St._Jerome_in_His_Study_(Dürer)

then go to browser, open a new tab or window. 【Alt+d】 to select the url field, 【Ctrl+v】 to paste in. Enter to go to the page.

Then, 【Alt+d】 to select url field, 【Ctrl+a】 to select all, 【Ctrl+c】 to copy. Then, paste in a text editor. Here are the results:

• Google Chrome
http://en.wikipedia.org/wiki/St._Jerome_in_His_Study_(D%C3%BCrer)

• Safari
http://en.wikipedia.org/wiki/St._Jerome_in_His_Study_(Dürer)

• Firefox
http://en.wikipedia.org/wiki/St._Jerome_in_His_Study_%28D%C3%BCrer%29

• Opera
http://en.wikipedia.org/wiki/St._Jerome_in_His_Study_(Dürer)

• IE
http://en.wikipedia.org/wiki/St._Jerome_in_His_Study_(Dürer)

Now, try again, starting with this line:

http://en.wikipedia.org/wiki/St._Jerome_in_His_Study_%28D%C3%BCrer%29
• Google Chrome
http://en.wikipedia.org/wiki/St._Jerome_in_His_Study_(D%C3%BCrer)

• Safari
http://en.wikipedia.org/wiki/St._Jerome_in_His_Study_%28Dürer%29

• Firefox
http://en.wikipedia.org/wiki/St._Jerome_in_His_Study_%28D%C3%BCrer%29

• Opera
http://en.wikipedia.org/wiki/St._Jerome_in_His_Study_(Dürer)

• IE
http://en.wikipedia.org/wiki/St._Jerome_in_His_Study_%28D%C3%BCrer%29

Another example. Start with:

http://en.wikipedia.org/wiki/Sylvester%E2%80%93Gallai_theorem
• Google Chrome
http://en.wikipedia.org/wiki/Sylvester%E2%80%93Gallai_theorem

• Safari
http://en.wikipedia.org/wiki/Sylvester–Gallai_theorem

• Firefox
http://en.wikipedia.org/wiki/Sylvester%E2%80%93Gallai_theorem

• Opera
http://en.wikipedia.org/wiki/Sylvester–Gallai_theorem

• IE
http://en.wikipedia.org/wiki/Sylvester%E2%80%93Gallai_theorem

All results are on Windows Vista, using latest public released version of the browsers as of 2010-05-24.

Summary

Here's some summary of the behavior as it appears from above tests:

  • Firefox (v 3.6.3), is the most aggressive in turning characters in url into the percent encoded form.
  • Google Chrome (4.1.249.1064 (45376)) will change unicode chars into percent encoded form, but not parenthesis chars.
  • Safari (4.0.5 (531.22.7)) does convert some percent encoded chars into plain unicode char, but not all.
  • Opera (v 10.10, build 1893) is the best, it shows unicode and paren and en-dash as is.
  • IE (8.0.6001.18904), seems to take the approach that it doesn't do anything to the url. Whatever you pasted in, remain unchanged.

References

Emacs Question

Is there emacs lisp function that decode the url percent encoding? e.g.

http://en.wikipedia.org/wiki/Sylvester%E2%80%93Gallai_theorem

should become

http://en.wikipedia.org/wiki/Sylvester–Gallai_theorem

That's a EN DASH (unicode 8211, #o20023, #x2013).

I know there's a

 (require 'gnus-util)
 (gnus-url-unhex-string ...)

but that just unhex, and generates gibberish if the url contains unicode chars.

some study shows that the “%E2%80%93” are hexadecimals E2 80 93, and is the byte sequence of the en dash char by utf-8 encoding.

So, i guess i could parse the url then interpret the %x string as utf-8 hex bytes then turn them back to unicode chars. Any idea if there's built in function that helps this?

Some discussion and temp solutions at:

Reported to FSF: bug#6252.

From the above discussions, you can see that it does not seem clear what character should be percent encoded. In fact, different browsers have different behavior.

Was this page useful? If so, please do donate $3, thank you donors!

Disable Ctrl+Alt+S of HP Support Information

Perm url with updates: http://xahlee.org/mswin/disable_hp_support_info_key.html

Disable Ctrl+Alt+S of HP Support Information

Xah Lee, 2010-05-18

Am pissed yesterday to have to spend a hour to be able to disable a global shortcut key in Windows, caused by a pre-installed software from HP.

If you bought your PC from HP/Compaq, then when you press “Ctrl+Alt+s”, it'll pop up the HP Support Information window.

hp support info window

HP Support Information window.

This is a pain in the ass, because Ctrl+Alt+s is used in Second Life Emerald Viewer virtual world software, and the 3D modeler Blender, and Emacs, all i use daily. All these 3 are major software, with extensive keyboard shortcuts.

So, how do you disable it?

Disable the Hotkey for HP Support Information

Open your Start menu, and look for the folder named “PC Help & Tools”, right click the “HP support information”, then chose Properties.

hp support info pc help n tools

The HP support info software in Start menu.

Click on the Shortcut tab, then the Shortcut key field, then just delete it there.

hp support information properties

the Properties window.

camelCase and Source Code Formatting

Perm url with updates: http://xahlee.org/UnixResource_dir/writ/camelCase_code_formatting.html

camelCase and Source Code Formatting

Xah Lee, 2010-05-18

Xah Lee wrote:

(in emacs) Sometimes you want to delete the file of the current buffer. How do you do it? Here's a simple command that does it.

(defun delete-current-file () 
  "Delete the file associated with the current buffer." 
  (interactive) 
  (let (currentFile) 
    (setq currentFile (buffer-file-name)) 
    (when (yes-or-no-p (concat "Delete file: " currentFile)) 
      (kill-buffer (current-buffer)) 
      (delete-file currentFile) 
      (message (concat "Deleted file: " currentFile)) 
      ) ) ) 

Thomas Munro wrote:

Why use CamelCase

I find that using camelCase is a good way to distinguish my own symbol names from built-in ones.

Especially because emacs's emacs-lisp-mode's syntax coloring is flawed in that it only color about 10 or so keywords, rather against its own conventions in syntax coloring.

I've wrote some details of this before, see: Emacs Lisp Mode Syntax Coloring Problem.

Why use SETQ if you don't have to? How about this:

(let ((current-file (buffer-file-name))) 
      ... 

I find that the

(let ((var1 val1) (var2 val2) ...) 
 body)

form is harder to read, especially when not all your vars have pre- computable values, e.g.

(let (var1 (var2 val2) var3 (var4 val4)...)
 (setq var1 ...)
  ...
 (setq var3 ...)
 ...)

where many of the values are themselves compound expressions with many nested parens.

I think it is a good recommendation that one should always use:

(let (var1 var2 var3...)
 (setq var1 ...)
 (setq var2 ...)
 ...
 body
)

and only use the (var val) form if all the vars are just local constants.

Since you intend to delete the file AND kill the buffer, why not say so in your inline doc?

Yeah, probably better to say in in the inline doc. Originally i just deleted the file. But then realized it left a dangling buffer. So i added code to remove buffer...

Though, i don't think stating the fact about also removing buffer is necessarily a good thing. My point of view is that, if you go by emacs tradition and technicality, then yes it should mention that buffer is also removed. However, the emacs tradition here, involving the concept of buffer, a computer engineering-by-product (like floats, int, long), is a major problem. Also, in vast majority of software, or other things involving info presentation, it is more important to communicate to the user than being technically correct.

Also, it could use a space and a question mark (at least the way YES-OR-NO-P works on my system, maybe yours is different). I would have written:

 (when (yes-or-no-p (format "Delete file %s and kill buffer? "
                               current-file))
    ...

you are probably right.

      (kill-buffer (current-buffer))
      (delete-file currentFile)
      (message (concat "Deleted file: " currentFile))
      ) ) )

Arrgh! My eyes! How about:

      (kill-buffer (current-buffer))
      (delete-file current-file)
      (message "Deleted file: %s" current-file))))

I might have formatted it the way you did, but just haven't done so.

I consider this trivial, and consider that the common advice and attention for code formatting has done major damage to the computing industry, from wasting programer's time to long-term damage in the growth and direction of computer languages. I've written several articles about this. The key concept is hard-coded formatting vs semantic formatting. The following articles discuss it from various aspects.

In two of the articles, “the Harm of Hard-wrapping Lines” and “Fundamental Problems of Lisp”, it discuss the long-term consequences of focusing on the hard-coded formatting.

new Xah Particle Maker manual

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

Spend 3 hours and re-wrote my Xah Particle Maker manual. See:

2010-05-17

2010-05-16

emacs command to delete current file

A new convenient elisp function.

Sometimes you want to delete the file of the current buffer. How do you do it? Here's a simple command that does it.

(defun delete-current-file ()
  "Delete the file associated with the current buffer.
Delete the current buffer too.
If no file is associated, just close buffer without prompt for save."
  (interactive)
  (let (currentFile)
    (setq currentFile (buffer-file-name))
    (when (yes-or-no-p (concat "Delete file?: " currentFile))
      (kill-buffer (current-buffer))
      (when (not (equal currentFile nil))
        (delete-file currentFile) ) ) ) )

Perm url with updates: Emacs Lisp Examples

AutoHotKey Syntax Problems

Perm url with updates: http://xahlee.org/mswin/autohotkey_problems.html

AutoHotKey Syntax Problems

Xah Lee, 2010-05-16

The AutoHotKey language syntax are bad, from a pure design point of view. This page gives some examples.

If you add a blank line in the wrong place, it is a compiler error. For a example, the following compiles fine:

;;; make the numpad star “*” key to do next tab
$NumpadMult::
WinGet, myProcName, ProcessName, A
If ( WinActive("ahk_class Chrome_WindowImpl_0")
Or WinActive("ahk_class MozillaUIWindowClass")
Or WinActive("ahk_class gdkWindowToplevel") )
{
  Send ^{PgDn}
}
Else If (WinActive("ahk_class IEFrame")
  Or WinActive("ahk_class OpWindow")
or (myProcName = "safari.exe") )
{
  Send ^{Tab}
}
Else {
  Send {NumpadMult}
}
Return

But if the line:

If ( WinActive("ahk_class Chrome_WindowImpl_0")

has a newline after the paren like this:

If (
 WinActive("ahk_class Chrome_WindowImpl_0")

The compiler generates a error.

Following is another example on the newline problem. This is ok:

Else If (WinActive("ahk_class Emacs") 
  Or WinActive("ahk_class Chrome_WidgetWin_0") 
Or WinActive("ahk_class MozillaUIWindowClass") 
Or WinActive("ahk_class gdkWindowToplevel") )

But putting the closing paren in a separate line creates compiler error:

Else If (WinActive("ahk_class Emacs") 
  Or WinActive("ahk_class Chrome_WidgetWin_0") 
Or WinActive("ahk_class MozillaUIWindowClass") 
Or WinActive("ahk_class gdkWindowToplevel")
)

Syntax Soup

Also, the syntax is quite a syntax soup. e.g.

IfWinActive ahk_class Notepad
IfWinActive, ahk_class Notepad
if WinActive("ahk_class Notepad")
if WinActive("ahk_class" . "Notepad")

are pretty much the same. I still don't quite understand if they are exactly equivalent at the syntax level, or if they are just semantically equivalent, or if they are semantically similar but not equivalent.

Its syntax for keys is also badly designed, with huge amount of special cases. See: AutoHotKey Example Scripts.

For example, to define a combination of key presses, such as “Ctrl+N”, it is like this:

^n::Run Notepad ; this means Ctrl+n

The general form is one of:

‹key›
‹modifier›‹key›
‹modifier›‹modifier›‹key›
‹modifier›‹modifier›...‹key›

However, you can have special meaning characters in front of the key syntax, like this:

$NumpadDiv::Send !{Left}

That dollar sign means if a NumpadDiv press is generated by ahk, don't interpret it in this definition. (useful to prevent infinite recursion)

So, the meaning of the form “‹char›‹key name›” depends on whether if the “char” is a special char for modifiers.

Also, for some commands, you can add a “#” char in front and it became a directive. e.g.

#IfWinActive ahk_class Notepad
#Space::MsgBox You pressed Win+Spacebar in Notepad.

In the above, the “#IfWinActive” is a directive, changing the meaning all lines below. In a C-like syntax, it would be something like this:

If ( WinActive( ahk_class("Notepad") ) ) {
 #Space::MsgBox("You pressed Win+Spacebar in Notepad.")
}

However, not all commands can become a directive by adding a “#”. Also, some directives, starts with “#” but does not become command without “#”. e.g. “#NoTrayIcon”.

Also note, the prefix of “#” char has multiple meanings depending on what follows. In the above example, the “#Space” is a key syntax, meaning holding the Win Logo key and pressing the Space bar key.

Why It's Lousy

The reason the syntax is lousy is due to the language's history, of course. AHK is a language that evolved out of practical needs. It is a branch off of the AutoIt scripting language. AutoIt is scripting language for task automation for Windows. At the time, the language does not feature defining hotkeys. The AHK author Chris Mallett was a fan of AutoIt, suggested the hotkey feature, but got turned down or got no reaction, so he branched off and started AHK. So, the a key binding syntax is added onto the language. Further, it is safe to presume the keybinding features just got added gradually from practical experimentation, so even the key syntax is quite inconsistent.

I've been learning and using AHK since 2009-08. On average perhaps spending 1 hour per week for the past 10 months. That's a total of 40+ hours. However, i can't see any general principle of the syntax. It seems just a soup of commands, much like unix shells, but worse.