2013-05-17

emacs: open file path under cursor fast

Perm URL with updates: http://ergoemacs.org/emacs/emacs_open_file_path_fast.html

Emacs has a command ffap. When your cursor is on a file path or URL, it'll open the file. This is extremely useful in shell ouputs or in HTML file.

One problem is that it'll prompt to confirm. This is annoying. Also, the command itself doesn't have a key.

You can write a similar command that doesn't need confirmation. Here's the code.

(defun open-file-at-cursor ()
  "Open the file path under cursor.
If there is text selection, uses the text selection for path.
If the path is starts with “http://”, open the URL in browser.
Input path can be {relative, full path, URL}.
This command is similar to `find-file-at-point' but without prompting for confirmation.
"
  (interactive)
  (let ( (path (thing-at-point 'filename)))
    (if (string-match-p "\\`https*://" path)
        (progn (browse-url path))
      (progn ; not starting “http://”
        (if (file-exists-p path)
              (progn (find-file path))
            (if (file-exists-p (concat path ".el"))
                (progn (find-file (concat path ".el")))
              (progn
                (when (y-or-n-p (format "file doesn't exist: 「%s」. Create?" path) )
                  (progn (find-file path ))) ) ) ) ) ) ))

Be sure to give it a easy key. 〔☛ Emacs: How to Define Keys

set git output non-interactive, set git date iso format

How to set git output non-interactive?

use git --no-pager, or, pipe it to cat, like this:

git log | cat

How to make git display date in ISO format?

Use --date=iso.

git log --date=iso > z

See also: git Tutorial

Google Plus new interface: Claustrophob Box

Google Plus UI tiny comment box 2013-05-17
new Google Plus interface, featuring the claustrophob box. 〔☛ User Interface Design: Peepshow Windows

2013-05-15

video review of the Truly Ergonomic keyboard

video review of the Truly Ergonomic keyboard. More info at Truly Ergonomic Computer Keyboard Review

Note: for those of you who know me, i got the keyboards free, but my opinion isn't bought, judge for yourself.

2013-05-14

emacs: open your own files FAST

Perm URL with updates: http://ergoemacs.org/emacs/emacs_hotkey_open_file_fast.html

Here's command that lets you open file fast, by typing a letter or digit:

(defvar xah-filelist nil "alist for files i need to open frequently. Key is a short abbrev, Value is file path.")
(setq xah-filelist
      '(
        ("3emacs" . "~/web/ergoemacs_org/emacs/blog.html" )
        ("4code" . "~/web/xahlee_info/comp/blog.html" )
        ("js" . "~/web/xahlee_info/js/blog.html" )
        ("math" . "~/web/xahlee_info/math/blog.html" )
        ("linguistics" . "~/web/wordyenglish_com/lit/blog.html" )
        ("chinese" . "~/web/wordyenglish_com/chinese/blog.html" )
        ("music" . "~/web/xahmusic_org/music/blog.html" )
        ("arts" . "~/web/xaharts_org/arts/blog.html" )
        ("sl" . "~/web/xahsl_org/sl/blog.html" )
        ("keys" . "~/git/xah_emacs_init/xah_emacs_keybinding.el" )
        ("ahk" . "~/git/xah_autohotkey_scripts/xah autohotkeys.ahk" )
        ("download" . "~/Downloads/" )
        ) )

(defun xah-open-file-fast (openCode)
  "Prompt to open a file from a pre-defined set."
  (interactive
   (list (ido-completing-read "Open:" (mapcar (lambda (x) (car x)) xah-filelist)))
   )
  (find-file (cdr (assoc openCode xah-filelist)) ) )

When called, it will prompt in the same way as ido-mode, with automatic name completion.

You type a key, then press Enter ↵, then the file will be opened.

You need to modify the file path for your own use. You can also easily modify the code above so that one letter opens several files at once, such as a coding project.

Note: You should also use the following:

All the above requires eyeball. That is, spending half a second to look at the screen and make a choice.

The purpose of “xah-open-file-fast” is that you never need to look at the screen, just muscle memory, but you may want to be sure that the first chars of your file name abbrevs are all unique.

emacs lisp: use ido completion for your own commands

emacs lisp: you can use ido-completing-read for your own cmds. It's bundled with emacs 24, at least. Example use:

(ido-completing-read "your prompt str:" ‹youList› )

Full example: Emacs: Change Brackets () {} [] in Text

Emacs: how i write blogs

A video recording on how i write blogs.

emacs: how i write blogs.

The commands used are:

The video presentation is pretty bad. I have much to learn, tech and presentation. I was using Google Plus's hangout feature with Hangout On Air to record directly to YouTube. Not sure what's a good solution on Linux yet.

also, i need to write code so that whatever command i called is displayed in a pane. So viewers can see it without me going over.

Programing Craft: Intention vs Coolness

Perm URL with updates: http://xahlee.info/comp/programing_craft_intention_vs_coolness.html

When you see a hash table, do you think “Ah, it's a flow control”?

in programing, we often do coolness.

Python doesn't have {switch, case, cond} construct. Someone asked about it on stackoverflow, and this is the most upvoted answer:

# -*- coding: utf-8 -*-
# python 3

# emulating “switch” statement in Python.
# works in both python 2 and 3

def f(x):
    return {
        "a": 1,
        "b": 2,
        }.get(x, 9)    # 9 is default if x not found


# test
y = "d"
print(f(y)) # 9

you use a hash table (aka dictionary) to emulate switch statement.

It is cool, but there is a problem: the intention of the programer isn't explicit. Instead, you have to engage your brain to figure out things.

here's a simpler, more verbose, version, but the programer intention is clear, and much easier to understand.

# -*- coding: utf-8 -*-
# python 3

# chained if, similar to “switch” conditional

def f(x):
    if x == "a": return 1
    if x == "b": return 2
    return 9

y = 2

print(f(y))

Unix Shell Use of Logical Operator as Flow Control

Another example is to use logical operators for program flow control. This is most abused in unix shell, and in fact is a idiom. Here's some example from Ubuntu Linux 〔/etc/init.d/bootlogd〕:

[ -x "$DAEMON" ] || exit 0
[ "$VERBOSE" != no ] && log_end_msg $ES

They are used for flow control.

It work by a implementation quirk on evaluating boolean expressions. For example, if you have x1 || x2 || x3 || x4, as soon as “x1” is true, you don't need to evaluate the rest anymore. Similar, if you have x1 && x2 && x3 && x4, if x1 is false, the whole result is false, no need to go on.

2013-05-13

new emacs modes for emacs lisp, php, html, css, and lean elisp manual

Am doing a pledge for a few new emacs projects. Vote for your emacs project: new html-mode, css-mode, php-mode, elisp-mode, or new lean emacs lisp manual. please see http://pledgie.com/campaigns/19973

2013-05-12

The Unfortunate Fate of Music MIDI File

MIDI is really a great tech, but unfortunately, it never picked up in computer.

MIDI is tiny in size, especially great for piano music. For example, a 5 minute piano piece is only 11k bytes.

10 years ago, i'd expect by now that on each computer we have high quality sampled sound library for MIDI, so that perhaps just a 100 mega bytes you'd have entire collection of piano music in history, recorded by professionals. Not so.

Today, on the web, MIDI isn't supported in html5 audio. 〔☛ HTML5 Audio Tag Example〕 〔☛ HTML5 Tags〕 And it won't play on Linux by default.

Here's entire collection of Bach's 〈Well Tempered Clavier〉, just 529k. Music: MIDI Collection of Bach 🎶