Xah Emacs Blog Archive 2014-06

the tab and space emacs lisp mode

in xah-elisp-mode, all you ever have to do is press tab or space. Never need to insert a paren or indent or whatnot. neva❗

just type, then press tab or space.

xah fly keys mode. Now on github as a project. See Emacs: Xah Fly Keys.

emacs lisp: region-active-p vs use-region-p

changed region-active-p to use-region-p sitewide.

what's the difference? if you look at the source code, region-active-p just check if transient-mark-mode is on and mark-active is true. And use-region-p added a check for use-empty-active-region.

(defun region-active-p ()
  "Return t if Transient Mark mode is enabled and the mark is active.

Some commands act specially on the region when Transient Mark
mode is enabled.  Usually, such commands should use
`use-region-p' instead of this function, because `use-region-p'
also checks the value of `use-empty-active-region'."
  (and transient-mark-mode mark-active))

updated ELisp: Mark, Region, Active Region

on the web, almost every day you see a post about Repetitive Strain Injury. hand pain! here's a recent one for example http://geekhack.org/index.php?topic=59828.0

who wrote emacs since 2000?

since few years ago, i have a habit to see who is the author of a package.

we all know that GNU Emacs was first emacs written in lisp, and is written by rms. But other than rms, most have a hard time coming up with a second name.

emacs's been around for 3 decades now. There are many fundamental and non-trivial core features in past few years. For example, who implemented the Unicode engine in emacs? who implemented the behavior where arrow down key moves by visual line? Who revamped emacs's completion engine? who implemented right-to-left languages support? who build the Chinese input system?

for packages, it's very easy to see who wrote it. Just Alt+x describe-function, then tab a few times to the elisp source code. (if you have emacs compiled, it's better, because binary emacs don't come with source code)

i started to look at author info since emacs 23, and link and thank them: Emacs 23 (Released 2009-07)

comment at https://plus.google.com/113859563190964307534/posts/VvxuYV4mY16

Bad Advices about Typing and Keyboard Bad Advice on Typing and Keyboard from Programers (repost)

ELisp: Determine Cursor is Inside String/Comment

ELisp: Determine OS, Emacs Version, Machine Host Name (minor update)

ELisp: Print Date Time (repost)

ELisp: Modify Syntax Table Temporarily

just finished reading John Wiegley's Git from the Bottom Up.

[Git from the Bottom Up By John Wiegley. At http://jwiegley.github.io/git-from-the-bottom-up/ , accessed on 2014-06-26 ]

this is excellent for people who have already used git on a daily basis, and wish to understand how it works underneath.

John Wiegley is a emacs expert, and also a heavy org-mode user. He has been interviewed by Sacha at http://sachachua.com/blog/2012/06/emacs-chatting-with-john-wiegley-about-the-cool-things-he-does-with-emacs/

if you want practical git, check out my beginners tutorial Git Tutorial: Practical Git in 1 Hour

improved “xah-select-text-in-bracket” using “with-syntax-table”. see Emacs Select Word Command Problem

Emacs Hello ergoemacs-mode Again

ELisp: Enable Undo in Buffer (on its own page)

Thanks to jcs at irreal.org

xah-elisp-mode is ready for public use. Emacs: Xah Elisp Mode (xah-elisp-mode.el)

Been working on it in past week full time. Tremendous Thank You for many who have donated.

Try it first without using auto-complete-mode or yasnippet.

Emacs Keys: Super Hyper (minor update)

Emacs Select Word Command Problem (updated)

Emacs: Move Cursor to Bracket 🚀 (updated)

Emacs: Move Cursor to Beginning of Line or Paragraph 🚀 (updated)

Emacs: Move Cursor by Text Block 🚀 (updated)

Emacs Lisp Toothpick Syndrome

Emacs: Problems of the Scratch Buffer (minor update)

Emacs: New Empty Buffer 🚀

Syntax Coloring Compared: Emacs, Vim, Atom, Sublime, Visual Studio, WebStorm

emacs: display function parameter

learned (turn-on-eldoc-mode). It displays the function's parameter in the minibuffer, of the function your cursor is on. Works in emacs-lisp-mode and other programing language modes.

i learned this from sacha's http://emacslife.com/how-to-read-emacs-lisp.html

eldoc-mode is written by [Noah Friedman https://plus.google.com/+NoahFriedman/posts], first created in 1995.

Emacs: Turn Off Auto Backup~ (updated)

emacs: efficient ways of going into a directory

dired-jump, when in dired, it'll go up a directory. Very cool. Because, normally you have to press ^. But, if you've given a easier key to dired-jump than that, then you just press the dired jump key instead!

you'll need this in your init file first:

(require 'dired-x)

it's bundled with emacs for years.

note: you should never need to call dired directly. Either use dired-jump, or use find-file. (be sure to have ido on, and when in ido-find-file, press Ctrl+d to go into dir.)

i haven't called dired command for about 6 years. First, i noticed that find-file will do equivalent when your path is a dir. This way, you don't need another key squatting a key spot. Just give a simple key for find-file. (in ergoemacs-mode, the find-file has key Ctrl+o) Then, ido improves the find-file situation by having automated path completion, so that saves you much key strokes. Then, dired-jump replaced other methods to go into dired more than 50% of the time.

comment at https://plus.google.com/113859563190964307534/posts/RAjAqcteEGu

isearch-forward-word search for sequence of words, regardless it's separeted by space or - or _

Emacs: Search Text in Current File (updated)

htmlize syntax color

today's fruit

(defun xah-redo-syntax-coloring-html-buffer ()
  "redo all pre lang code syntax coloring in current html page."
  (interactive)
  (let (langCode p1 p2)
    (goto-char (point-min))
    (while
      (re-search-forward "<pre class=\"\\([-A-Za-z0-9]+\\)\">" nil "NOERROR")
      (setq langCode (match-string 1))
      (setq p1 (point))
      (backward-char 1)
      (xah-html-skip-tag-forward)
      (search-backward "</pre>")
      (setq p2 (point))
      (save-restriction
        (narrow-to-region p1 p2)
        (xah-html-dehtmlize-precode (point-min) (point-max))
        (xah-html-htmlize-region (point-min) (point-max) (xah-html-langcode-to-major-mode-name langCode xah-html-lang-name-map) t)))))

so, i press one button, and the current HTML file's pre tag of programing lang source code are all regenerated.

to redo my entire website's syntax coloring, i can run this:

(defun my-process-file (fPath)
  "Process the file at path FPATH"
  (with-temp-buffer
    (insert-file-contents fPath)
    (xah-html-redo-syntax-coloring-buffer)
    (write-region 1 (point-max) fPath)))

(require 'find-lisp)

(mapc 'my-process-file (find-lisp-find-files "~/web/" "\\.html$"))

why do i want to regenerate? because i'm using several of my own emacs modes for coloring, including xah-elisp-mode, xah-html-mode, xah-css-mode, xah-js-mode, because the default doesn't color many keywords. But in my modes, i constantly add new keywords or tweak how i want them colored. For example, in JavaScript, should coloring be grouped by keywords, object name, function name, or native object vs hosted object, or methods vs properties, etc. So, when i made a change to how the mode colors, i sometimes want to update all pages on my site.

for the basics of how this works, see ELisp: Syntax Color Source Code in HTML

comment at https://plus.google.com/113859563190964307534/posts/7tcHTo5Ys26

emacs lisp: name your predicate ending in question mark?

by convention, predicate functions in emacs lisp ends in “p”. For example: file-exists-p

but it's inconsistent. Some functions ends in “p” but is not a predicate. For example: {pop, defgroup, make-sparse-keymap, forward-sexp}.

also, some ends in “-p” some without the hyphen. For example: {file-exists-p, integerp}

you should name it ending in “?”. See: Predicate in Programing Languages and Naming

new version of emacs tutorial. Heavily updated in past weeks.

Buy Xah Emacs Tutorial

thanks to so many of you for support. The new version will be emailed to you today.

tweet, repost, share, buy. Thank you.

Keyboard: What's the Difference Between Alt Graph Key, Compose Key, Dead Key?

see also: A Curious Look at Emacs One Thousand Keybindings (updated)

Emacs: Newline Convention CR LF (updated)

Emacs: Search Text in Current File (complete rewrite)

Emacs: isearch-forward-symbol-at-point Problem

ELisp: Write grep (updated)

Emacs Init: Restore Opened Files (desktop-save-mode) (updated)

Emacs: List Buffers (updated)

Emacs: Open Recently Opened File (updated)

Emacs 22: iswitchb vs ido mode 👎 (updated)

Emacs: icomplete vs ido mode 👎

Emacs: Switch Buffer (updated)

what happens to a package when it joins the mothership?

once a package goes into GNU emacs, as a property of FSF, the package becomes moribund.

all innovation happens outside of it, and lots of re-inventing the wheel, but usually better wheel.

well, it's not exactly dead, but will be revived and improved after half a decade, when there are several better wheels out there.

comment at https://plus.google.com/+XahLee/posts/4kzBadna2Gq

emacs icomplete.el, a grand-daddy class submarine, 1993 by Ken Manheimer

;;; icomplete.el --- minibuffer completion incremental feedback

;; Copyright (C) 1992-1994, 1997, 1999, 2001-2019 Free Software
;; Foundation, Inc.

;; Author: Ken Manheimer <klm@i.am>
;; Maintainer: Ken Manheimer <klm@i.am>
;; Created: Mar 1993 Ken Manheimer, klm@nist.gov - first release to usenet
;; Keywords: help, abbrev

in the thanks section:

;; Thanks to everyone for their suggestions for refinements of this
;; package.  I particularly have to credit Michael Cook, who
;; implemented an incremental completion style in his 'iswitch'
;; functions that served as a model for icomplete.  Some other
;; contributors: Noah Friedman (restructuring as minor mode), Colin
;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and others.

it mentioned [Noah Friedman https://plus.google.com/+NoahFriedman/posts] , which i recognize, thank to [Nick Alcock https://plus.google.com/115849739354666812574/posts] (who is currently the great mole with all sensors extended).

also, look at the author's email address. “i.am”, how cool is that?

comment at https://plus.google.com/113859563190964307534/posts/39EyKHehY22

emacs: interactive completion sans smex

you can have ido-like completion for describe-function. Just turn on icomplete mode.

;; interactive name completion for describe-function, describe-variable, execute-extended-command, etc.
(icomplete-mode 1)

;; make icomplete prettier
(setq icomplete-separator "\n")
(setq icomplete-prospects-height 3)
(setq icomplete-hide-common-prefix nil)
(setq icomplete-in-buffer t)

icomplete doesn't have flex matching as ido. (that is, matches middle of the word) However, it supports wildcard. For example, when you Alt+x execute-extended-command looking for a command named “abc”, you can type “*b” to make the completion candidates show.

comment at https://plus.google.com/113859563190964307534/posts/8w2WUsyrnnM

Make Ido Mode Display Vertically

see updated at Emacs: Switch Buffer

Linux Command to Download YouTube Video

The command youtube-dl has a package in Ubuntu's apt-get, but it's outdated and doesn't work. I learned from Christopher Wellons that it has a website. Wellons was recently interviewed by Sacha. There are lots goodies in Wellons's blog. Check it out.

Emacs: Interactive Abbrev 🚀 (added a screenshot)

Vim: How to Remap the Escape Key?

updated Emergency vim

and, get vimrc-mode to make your .vimrc pretty:

(autoload 'vimrc-mode "vimrc-mode" "loads vimrc-mode" "INTERACTIVE")

(when (fboundp 'vimrc-mode)
  (add-to-list 'auto-mode-alist '(".vim\\(rc\\)?$" . vimrc-mode))
)

and, a interesting thing about vimrc syntax is that its comment starts with "

that's right, a single double quote at the beginning of line. I used another quote at the end of line just to make it familiar and save myself trouble of creating another CSS to deal with syntax coloring of vimrc code.

comment at https://plus.google.com/+XahLee/posts/Ag7XYJrQSXQ

Emacs: Search Text in Directory (minor update)

Keyboard Enter/Return Key Symbol — a Survey (updated)

Short Survey of Keyboard Shortcut Notations (minor update)

How to set cursor to a i-beam?

(modify-all-frames-parameters (list (cons 'cursor-type 'bar)))

Emacs: Init File Tutorial (updated)

btw, the proper Unicode char for cursor are:

for more, see: Unicode: User Interface Icons 🗑

Emacs Init: Auto Insert Closing Bracket (electric-pair-mode) (updated to the change yesterday)

Sacha's emacs's chat with Christopher Wellons

I have a new emacs hero. Christopher Wellons.

see sacha's emacs's chat with him at: http://sachachua.com/blog/2014/05/emacs-chat-christopher-wellons/

Wellons's wrote several heavy-duty cool emacs stuff. Not just simple pure elisp, but things that interact with browser, server, database.

you can see his heavy expertise in not just emacs lisp but industrial code.

also, i want to thank Sacha so much.

Emacs: Insert `emacs style quotes' by Pair

added “xah-insert-emacs-quote” command. It inserts quotes like `this'.

recently been doing heavy elisp. Thanks to a great guy David Pollak. https://twitter.com/dpp (Pollak is also a well known programer. He is the author of [Beginning Scala by David Pollak Buy at amazon ], and author of the Lift (web framework))

since coding a lot elisp not just for myself, i had to follow the GNU emacs convention. So, typing lots of `emacs quote' gets tiring. Thus the new command.

get code at Emacs Init: Auto Insert Closing Bracket (electric-pair-mode)

Emacs Keybinding for Inserting Brackets and Unicode Symbols

redesigned my keybinding for inserting Unicode chars and brackets, based on Computer Languages Character Distribution

here's the current state.

Key translations Starting With <menu> SPC:
key             binding
---             -------

<menu> SPC SPC  _
<menu> SPC 3    φ
<menu> SPC 4    ξ
<menu> SPC 6    ƒ
<menu> SPC 7    &
<menu> SPC 8    •
<menu> SPC 9    —
<menu> SPC \    、
<menu> SPC e    =
<menu> SPC l    …
<menu> SPC p    +
<menu> SPC u    -
<menu> SPC <down> ↓
<menu> SPC <left> ←
<menu> SPC <right> →
<menu> SPC <up> ↑

Global Bindings Starting With <menu> SPC:
key             binding
---             -------

<menu> SPC RET  xah-insert-unicode
<menu> SPC ,    xah-insert-greater-less
<menu> SPC b    xah-insert-black-lenticular-bracket【】
<menu> SPC c    xah-insert-ascii-single-quote
<menu> SPC d    xah-insert-double-curly-quote“”
<menu> SPC f    xah-insert-emacs-quote
<menu> SPC g    xah-insert-ascii-double-quote
<menu> SPC h    xah-insert-brace
<menu> SPC i    xah-insert-curly-single-quote‘’
<menu> SPC m    xah-insert-corner-bracket「」
<menu> SPC n    xah-insert-bracket
<menu> SPC t    xah-insert-paren
<menu> SPC v    xah-insert-double-angle-quote«»
<menu> SPC w    xah-insert-angle-bracket〈〉
<menu> SPC x    xah-insert-tortoise-shell-bracket〔〕
<menu> SPC y    xah-insert-single-angle-quote‹›
<menu> SPC <menu>               xah-insert-paren

they are for Dvorak Keyboard Layout.

on github at https://github.com/xahlee/xah_emacs_init/blob/master/xah_emacs_unicode_input.el

see also Emacs Keybinding, Keyboard, Articles Index

Emacs Lisp: How to Write a Filter Function, and Never Indent Your Code

ELisp: Filter a List

and now

Xah's edict of the day: never use dolist. Never use lisp macro. Never manually tuck your parens or indent a line. And, don't use CL.

the ideal format for the above code should be one single line.

which, is common in Mathematica. The editor shall display it nicely.

one thing lisp doesn't make this possible is its line-oriented comment syntax.

btw, there's a interesting tidbit in the above, that's the trick of using “logand” (binary logical and) to compute whether a integer is even. I got it from the CL lib in emacs. Bitwise Operations (ELISP Manual)

for more about how indentation should be ban'd, its relation to language syntax design, see:

ELisp: Cut Copy Paste, kill-ring (on its own page)

emacs lisp, bad style, good style

discovered a very bad form in one of the core elisp file.

(defun add-to-invisibility-spec (element)
  "Add ELEMENT to `buffer-invisibility-spec'.
See documentation for `buffer-invisibility-spec' for the kind of elements
that can be added."
  (if (eq buffer-invisibility-spec t)
      (setq buffer-invisibility-spec (list t)))
  (setq buffer-invisibility-spec
        (cons element buffer-invisibility-spec)))

the if there should be when.

this is in subr.el

emacs key Ctrl+?

some 7 years ago, i wrote that one of the key you shouldn't define is Ctrl+?.

Fuqiao Xue (xfq) https://github.com/xfq/ asked me where did i read that, but i couldn't remember where in the emacs/elisp manual.

today i came across it. See: Ctl-Char Syntax (ELISP Manual)

updated at Emacs Keys: Define Key.

How to make the Tab key always do indent or completion?

see updated: Emacs Init: Tab, Indent

How to set margin in emacs? see updated: Emacs: Init File Tutorial

Emacs: Novel Reading Mode 🚀

new crotchet: all function parameter names should start with φ. Sigil for Variable Names