Learning Emacs key bindings
aaronhawley

Emacs key bindings are intimidating at first since they are quite distinct from those used in other applications and platforms. The following is based on discussion on the Emacs IRC channel and is posted to the Emacs Wiki.

After studying the tutorial that comes with Emacs, most people learn best by doing and not just by reading over a list of commands and trying to memorize them. Get yourself a text file to play around with. Make a backup copy if you're nervous about losing it. But don't worry -- Emacs makes it hard to lose your work, and it has a great undo command: `C-/' or `C-_'.

Keep the following advice in mind.

  1. Try to memorize the most commonly used commands in the tutorial.
  2. Use Apropos or help to find commands to accomplish a task.
  3. Run the command for a task with `M-x' and typing it in.
  4. Run the command again by using `M-x M-p' or by typing it in again.
  5. On the third time, see if the command is bound to a key binding with `C-h w'.
  6. If you forget a key binding (sometimes you forget even the basic commands introduced in the tutorial), try `C-h w M-p' to peek at it again or use `C-h b' to browse the available bindings.
  7. If you can't burn a key binding in to your brain with this approach, then you're not using it enough and it doesn't deserve a key binding.
  8. If a key binding is atrocious, only then bind the command to an unused key.

Some beginners rush too quickly and add or change key bindings. This avoids learning Emacs and risks spending more time configuring. Competency with `M-x' and help are actually core Emacs skills for life. `M-x' should become your command line in Emacs.


Executive Director, GNOME Foundation
aaronhawley
It was announced today that Karen Sandler was named new Executive Director of the GNOME Foundation. I don't know Karen, nor am I currently a stock holder of Karen Sandler or the GNOME foundation, but she is a co-host of the Free as in Freedom oggcast of which I'm a dedicated listener.

Given her years as general counsel at the Software Freedom Law Center (SFLC) and the usual legal experience appointing a lawyer to a board gives, Karen is also a good appointment for her non-law insights on free software development by building organizations that sustain and protect the free software ecosystem.

The relationship between CentOS and Red Hat
aaronhawley

Karanbir Singh (KS) of CentOS was interviewed last November 2010 by Randal Schwartz (RS) and Dan Lynch (DL) on the "FLOSS Weekly" radio program. Here was an interesting portion of the interview.

RS: Give us the top-level view of what CentOS is all about?

KS: So, it's quite a long story, but in a short one-minute setup what happened was that a few years back, Red Hat decided to change their enterprise Linux strategy, and they brought in a subscription model for the product they were going to support. What they also did was left the sources for that product available to the public should anybody want to use it.

What CentOS does is we take those sources and build the distribution with those sources to be as close to the Red Hat offering as possible -- as binary compatible, as ABI-compliant, as API-compliant -- with the only difference being things look different because they have branding in it and you don't get any support. [...]

DL: Is it a good working relationship that you have with Red Hat?

KB: That's a good question. The perspective varies depending on who you speak to. From our end at CentOS, we think we have a very good relationship with Red Hat in that we don't actually have a direct relationship with Red Hat. The components that we are consuming and that Red Hat are putting out they are making available to anybody and everybody, so it could be CentOS it could be Joe Blow.

From the Red Hat side of things, it gets slightly more interesting -- this is of course my personal opinion, not Red Hat opinion -- I feel that Red Hat engineering and Red Hat technical are quite happy with what we're doing because it's not really a one-way street. We take the sources and build a distribution around it, build a platform around it, but what we also do is we file a lot of feature requests, we file patches, we file bug reports. We work with some of their engineering teams to try and solve problems that users are having out in the open. We feel they are happy with us. If you shift the focus a bit and look at Red Hat sales and marketing they're not really that happy with what's happening with CentOS. The one message I try to get across to them is that if there are X number of users using CentOS, then there are X users using the Red Hat platform. [...]

There's some concern that CentOS 6 isn't released even though RHEL 6 has been available for a year, so there must be a crisis or viability issue with CentOS. However, updating CentOS after a major release of RHEL is non-trivial. If folks want a version of CentOS 6 sooner, then there should be resources offered to make that happen. Otherwise, buy a support subscription of RHEL 6 from Red Hat. I hear it's compatible with the unreleased CentOS 6.

CentOS is important for the free software community since it is an affordable, commercially-developed GNU/Linux distribution. It's also important because it is material proof that a commercially-developed GNU/Linux distribution, Red Hat in this case, can be built from source independently and distributed by a third-party -- commercial or community. CentOS deserves thanks for that as well.


Refusing to learn
aaronhawley
An essay called The psychology of learning from 2003 by Robert Strandh, director of the Département d'Informatique at Université Bordeaux, France, describes a trend in computing where close-mindedness (my phrase) risks opportunities for learning and effectiveness.

The bad-mouthing of competing technologies by adherents of another technology is well known.  The practice goes back to early days of hacker culture when arguments would abound in choice of programming languages (or assembly), mainframe manufacturer or model (consider also "PC versus Mac" over the last two recent decades).  These divisions can partition customers, industries and educators into islands with different customs and ideas.  Strandh doesn't visit this in the essay.  No, according to Strandh, opinions like these arise in young students in more subtle but still worrisome ways.
[...] I have often observed that students are very inefficient in their work. They frequently use methods of working that are unproductive and slow. Some examples:
  • Students do not know how to touch-type. Instead of taking the relatively limited time to learn to do it, they waste many hours per week on slow typing and typing errors.
  • Students frequently do not know how to use advanced features in the text editor such as the interface to the version-control system, the interface to the Lisp system, etc. Again instead of taking a short time to learn, they waste much more time.
  • Students do not know how to use a debugger. Instead, they waste time debugging programs with trace output. [...]
Strandh suggests this could simply be "reluctance to learning new tools and methods", but can also become "a kind of reaction orders of magnitude stronger". He attributes the reaction to "a need to achieve performance immediately" (original emphasis). Continuing:
Such performance leaves no time for intellectual curiosity. Instead, techniques already known to them must be applied to solve problems. To these people, failure is a disaster whose sole feature is to harm instant performance. Similarly, learning represents the possibility of failure and must thus be avoided if possible. To the people in this category, knowledge in other people also represents a threat. As long as everybody around them use tools, techniques, and methods that they themselves know, they can count on outperforming these other people. But when the people around them start learning different, perhaps better, ways, they must defend themselves. [...]
Strandh attributes these behaviors to a description given to him personally by Boston College psychology professor, Lisa Feldman Barrett, who in turn cites the work of well-known psychologist Carol Dweck. The research which posits a duality of perfection-oriented and performance-oriented is worth looking into as an explanation.

The best part of the essay are not about the psychology theory, but his personal anecdotes on computing inspired by the psychology researcher's works.
[...] I talked to a student of computer science who told me why a particular programming language was bad. In fact he told me it was so bad that he had moved to a different university in order to avoid courses that used that particular language. When asked, he admitted he had never written a single program in that language. He simply did not know what he was talking about. And he was willing to fight for it. [...]

[...] I have seen professors in mathematics who were obviously perfection-oriented with respect to mathematics, be firmly in the performance-oriented category with respect to the efficient use of (say) word processors. It is almost a surrealistic experience to see a person in one situation full of intellectual curiosity and wanting to know everything about everything, and in another situation argue why you should not use a particular method that he himself does not know anything about, for reasons that are obviously totally artificial. [...]

[...] I have observed that people ignorant in a particular domain, or not knowing a particular tool or technique, would go to great trouble to explain why knowing this domain, tool, or technique, would be a complete waste of time. Usually these explanations were based on erroneous ideas of what it represented. To make things worse, they were perfectly willing to present their erroneous arguments to the very experts in the field in question. [...]
In this anecode, Strandh likely admits his membership in the Church of Emacs.
[...] I have heard people argue against a tool that they ignore based on the fact that it can do too much. Too much functionality in a tools is a problem only if unneeded or unwanted functionality somehow makes it harder to use the needed and wanted parts. I have heard people argue about the amount of memory a particular tool requires, whereas the additional memory required might represent a cost equivalent to a few hours of work at most. A favorite idea is to label a particular tool with a name suggesting what it ought to be doing, and then arguing that it is doing more than that. For instance, a text editor that is capable of automatic indentation would be accused of being a "kitchen-sink" tool because after all it does much more than allowing the user to just edit text. (original emphasis) [...]
Strandh ends by revealing his own transgressions of this same type.
[...] I myself recently discovered a marvelous feature in a programming language that I had purposely avoided for the past 10 years, simply because 10 years ago, a colleague (who did not know the feature) explained to me that it was no good. We were both victims of our own minds. My colleague because he obviously needed to defend that he had made a different choice, and myself because I subconsciously found it very appealing to be able to brush off the feature as useless and thus not having to learn it. It is hard to overestimate the wasted time I have put in during the past 10 years due to considerably lower productivity than I could have had, had I realized at the time what I now know about human psychology. [...]
I would add another possible explanation for these reactions is another one rooted in "human psychology":  It is a defense mechanism.  Computing possesses a great diversity and many ways to do things.  Barring a few mighty monopolies in hardware and software, there still exists much complexity and specialization in the field of computing.  As regressive overt opinions are, the instinct may be a method for people to manage and focus on their work.  This would especially be the case for budding students.  Avoiding potentially labor-saving tools and knowledge in the rush to finish a task is unfortunate, but investigating alternatives has to be limited or else a task is never started.  It's like the lumberjack (or jill) in the metaphor who sharpens their saw or axe but never cut the tree down since they never got around to swinging the axe.

Despite my sympathy, people who let this close-minded instinct take over and influence themselves and others is indefensible.  People should simply answer "I don't know" rather than than making "complete fools of themselves" by expressing what are only hunches.  A couple famous quotes about "ignorance" and "fear" are probably worth inserting here.  I'll leave it as an exercise.  The point is stop being cavalier and just admit you don't know rather than trying to prove knowing everything.

Extending M-x in Emacs
aaronhawley

Over 6 months ago, I revealed an Emacs Lisp version of `M-x'. I was pleased to have a few Emacs users who greeted a Lisp replacement for this core primitive with excitement.

What's the benefit of having a Lisp version? Well, it's easier to change than the C version. The syntax is easier and there is no compilation step.

Here's a modification I made to the original. It's a small change, but makes for a good example. It introduces a different representation of the prefix argument in the Minibuffer.

Typically, hitting `C-u' before `M-x' will show "C-u M-x" in the Minibuffer. However, if you issue more than one `C-u', then it just becomes a numeric argument and becomes "16 M-x", "64 M-x", "256 M-x" and so on. The small patch inserted below will show "C-u C-u M-x", "C-u C-u C-u M-x", "C-u C-u C-u C-u M-x" and so on, respectively. There's a good argument for the original behavior, since the powers of 4 are not that memorable to everyone. Letting Emacs handle the arithmetic and show it is helpful. However, visual feedback for what you're typing is a good interface design rule as well. As you can tell I'm agnostic, but like how this small change only takes a small bit of change to the code.

--- m-x.el	2009/08/25 00:03:17	1.1
+++ m-x.el	2009/08/25 00:07:12	1.1.1.1
@@ -22,8 +22,11 @@
 	       (cond ((eq prefixarg '-)
 		      "- ")
 		     ((and (consp prefixarg)
-			   (= (car prefixarg) 4))
-		      "C-u ")
+			   (zerop (% (car prefixarg) 4)))
+		      (apply 'concat
+			     (make-list (truncate
+					 (log (car prefixarg) 4))
+					"C-u ")))
 		     ((and (consp prefixarg)
 			   (integerp (car prefixarg)))
 		      (format "%d " (car prefixarg)))

Emacs knows the difference between a prefix argument with `C-u' and a numeric argument with `M-4' or `C-4'. So, `M-1 M-6 M-x' does not show "C-u C-u M-x" in the Minibuffer.

Here's the complete version of the code.

;; Based on Fexecute_extended_command in keyboard.c of Emacs.
;; Aaron S. Hawley <aaron.s.hawley(at)gmail.com> 2009-08-24
(defun execute-extended-command (prefixarg)
  "Read function name, then read its arguments and call it.

To pass a numeric argument to the command you are invoking with, specify
the numeric argument to this command.

Noninteractively, the argument PREFIXARG is the prefix argument to
give to the command you invoke, if it asks for an argument."
  (interactive "P")
  ;; The call to completing-read wil start and cancel the hourglass,
  ;; but if the hourglass was already scheduled, this means that no
  ;; hourglass will be shown for the actual M-x command itself.
  ;; So we restart it if it is already scheduled.  Note that checking
  ;; hourglass_shown_p is not enough,  normally the hourglass is not shown,
  ;; just scheduled to be shown.
  (let* ((hstarted (and (symbolp window-system)
                        (eq void-text-area-pointer 'hourglass)))
         (saved-keys (this-command-keys-vector)) ;; ?\M-x
         (buf (concat 
               (cond ((eq prefixarg '-)
                      "- ")
                     ((and (consp prefixarg)
                           (zerop (% (car prefixarg) 4)))
                      (apply 'concat
                             (make-list (truncate
                                         (log (car prefixarg) 4))
                                        "C-u ")))
                     ((and (consp prefixarg)
                           (integerp (car prefixarg)))
                      (format "%d " (car prefixarg)))
                     ((integerp prefixarg)
                      (format "%d " prefixarg))
                     (t ""))
               "M-x "))
         (function (completing-read buf obarray 'commandp t nil
                                    'extended-command-history)))
    (if hstarted (setq void-text-area-pointer 'hourglass))
    (if (and (stringp function)
             (= (length function) 0))
        (error "No command name given")
      ;; Set this_command_keys to the concatenation of saved-keys and
      ;; function, followed by a RET.
      (setq saved-keys (vconcat saved-keys
                                function
                                [return]))
      (setq function (intern function)))
    (setq prefix-arg prefixarg)
    (setq this-command function)
    (command-execute function 'record saved-keys)
    ;; If enabled, show which key runs this command.
    (if (and (not (null suggest-key-bindings))
             (null executing-kbd-macro))
        ;; If the command has a key binding, print it now.
        (let ((bindings (where-is-internal function
                                           overriding-local-map t))
              (waited))
          ;; But first wait, and skip the message if there is input.
          (when (and (not (null bindings))
                     (not (and (vectorp bindings)
                               (eq (aref bindings 0)
                                   'mouse-movement))))
              ;; If this command displayed something in the echo area;
              ;; wait a few seconds, then display our suggestion message.
              (if (null (current-message))
                  (setq waited (sit-for 0))
                (if (numberp suggest-key-bindings)
                    (setq waited (sit-for suggest-key-bindings))
                  (setq waited (sit-for 2))))
              (when (and (not (null waited))
                         (not (consp unread-command-events)))
                (with-temp-message (current-message)
                  (let ((binding (key-description bindings)))
                    (message "You can run the command `%s' with %s"
                             function binding)
                    (if (numberp suggest-key-bindings)
                        (setq waited (sit-for suggest-key-bindings))
                      (setq waited (sit-for 2)))))))))))

(provide 'm-x)

Consider putting the above in a file called m-x.el, then put the file in you load-path and add the following to your .emacs file.

(load "m-x")

In the next installment, I will show how to change `M-x' so that it notices a function name at point and offers it as the default command.


Coding Emacs's M-x in Lisp
aaronhawley

I have always wondered what terms stipulate whether an Emacs internal routine is written in Emacs Lisp or if it exists as a built-in and part of Emacs's C source code. I have grown accustomed to how most core Emacs commands and functions, no matter how small, are written in Emacs Lisp. There is no doubt a lot of Emacs that has to be written in C.

I recently wanted to change how `M-x' works. The command behind it is called `execute-extended-command'. It is written in C. This is disappointing for my desires to tinker, but not all together surprising either. It is a pretty central piece of the Emacs infrastructure.

To extend the `M-x' command, I didn't want to jump into having to write C code. Although, it was a chance to practice compiling Emacs, and potentially debugging my extension to the command. I wanted to avoid this development cycle, so I tried translating the C code into Emacs Lisp. Given how cleanly written the Emacs sources are, it was not a very difficult task. For extra cuteness, I even carried over the C comments.

Use at your own risk. I would be interested in feedback on how this works for people, but redefining a central command like this could potentially create a lot of problems for your Emacs. With that out of the way, I have used this for a few weeks without a problem.

The block of C code for defining Fexecute_extended_command hasn't changed significantly over the last ten years, so the Emacs Lisp version I present below should work as a replacement with the last two major releases of Emacs -- versions 22 and 23.

;; Based on Fexecute_extended_command in keyboard.c of Emacs.
;; Aaron S. Hawley <aaron.s.hawley(at)gmail.com> 2009-08-24
(defun execute-extended-command (prefixarg)
  "Read function name, then read its arguments and call it.

To pass a numeric argument to the command you are invoking with, specify
the numeric argument to this command.

Noninteractively, the argument PREFIXARG is the prefix argument to
give to the command you invoke, if it asks for an argument."
  (interactive "P")
  ;; The call to completing-read wil start and cancel the hourglass,
  ;; but if the hourglass was already scheduled, this means that no
  ;; hourglass will be shown for the actual M-x command itself.
  ;; So we restart it if it is already scheduled.  Note that checking
  ;; hourglass_shown_p is not enough,  normally the hourglass is not shown,
  ;; just scheduled to be shown.
  (let* ((hstarted (and (symbolp window-system)
                        (eq void-text-area-pointer 'hourglass)))
         (saved-keys (this-command-keys-vector)) ;; ?\M-x
         (buf (concat 
               (cond ((eq prefixarg '-)
                      "- ")
                     ((and (consp prefixarg)
                           (= (car prefixarg) 4))
                      "C-u ")
                     ((and (consp prefixarg)
                           (integerp (car prefixarg)))
                      (format "%d " (car prefixarg)))
                     ((integerp prefixarg)
                      (format "%d " prefixarg))
                     (t ""))
               "M-x "))
         (function (completing-read buf obarray 'commandp t nil
                                    'extended-command-history)))
    (if hstarted (setq void-text-area-pointer 'hourglass))
    (if (and (stringp function)
             (= (length function) 0))
        (error "No command name given")
      ;; Set this_command_keys to the concatenation of saved-keys and
      ;; function, followed by a RET.
      (setq saved-keys (vconcat saved-keys
                                function
                                [return]))
      (setq function (intern function)))
    (setq prefix-arg prefixarg)
    (setq this-command function)
    (command-execute function 'record saved-keys)
    ;; If enabled, show which key runs this command.
    (if (and (not (null suggest-key-bindings))
             (null executing-kbd-macro))
        ;; If the command has a key binding, print it now.
        (let ((bindings (where-is-internal function
                                           overriding-local-map t))
              (waited))
          ;; But first wait, and skip the message if there is input.
          (when (and (not (null bindings))
                     (not (and (vectorp bindings)
                               (eq (aref bindings 0)
                                   'mouse-movement))))
              ;; If this command displayed something in the echo area;
              ;; wait a few seconds, then display our suggestion message.
              (if (null (current-message))
                  (setq waited (sit-for 0))
                (if (numberp suggest-key-bindings)
                    (setq waited (sit-for suggest-key-bindings))
                  (setq waited (sit-for 2))))
              (when (and (not (null waited))
                         (not (consp unread-command-events)))
                (with-temp-message (current-message)
                  (let ((binding (key-description bindings)))
                    (message "You can run the command `%s' with %s"
                             function binding)
                    (if (numberp suggest-key-bindings)
                        (setq waited (sit-for suggest-key-bindings))
                      (setq waited (sit-for 2)))))))))))

(provide 'm-x)

The result is half as many lines of code compared to the C. But the real benefit is being able to mess around with it. If this version that mimics the standard behavior is proved to be stable, I may try and see if a few customizations to these new bits will hold up as well.

I currently put the above code in a file called m-x.el, then put the file in my load-path and add the following to my .emacs file.

  (load "m-x")

Feed: Alex Schroeder
aaronhawley
Alex Schroeder is the author of the Oddmuse Wiki engine and various Emacs modes, including SQL mode and ANSI color. He is also responsible for maintaining the Emacs Wiki for the emacsen community.

Alex maintains two other sites. Community Wiki discusses online culture, and the Campaign Wiki organizes materials on role playing games (RPG).

LiveJournal users can sign up for updates from Alex's blog at kensanata_diary and adding the feed to their friends list.

Feed: Sacha Chua
aaronhawley
Sacha Chua works for Global Business Services at IBM, and was previously an IBM Enterprise 2.0 consultant. She is also a speaker, a self-declared "technology evangelist", application developer, Web 2.0 expert and "geek".

A prolific writer who can posts multiple times daily with extremely technical articles about Drupal or Emacs, some analysis on working and self-organization, simple reflections on hobbies and personal life, and sometimes short story fiction.

LiveJournal users can sign up for updates from Chua's blog at sachachuawiki and adding the feed to their friends list.

Feed: kfogel rants
aaronhawley
Karl Fogel works for Canonical, the English and South African company behind Ubuntu, and is well-known for his contributions to CVS and is an author of Subversion. He wrote the CVS book and Producing Open Source Software: How to Run a Successful Free Software Project.

His blog posts include technical and political issues related to free software and copyright reform, but random musings also show up.

LiveJournal users can sign up for updates from Fogel's blog at kfogelrants and adding the feed to their friends list.

Feed: SFLC Tech Blog
aaronhawley
The Tech blog at the Software Freedom Law Center (SFLC) features Bradley M. Kuhn, former (and first ever) director of the Free Software Foundation. Kuhn is now the Policy Analyst and Technology Director for the SFLC.

His articles are about licensing and legal issues around free software, but he also gives reviews on software and gives updates on the computing infrastructure for the SFLC of which he manages.

LiveJournal users can sign up for updates from Kuhn's blog at sflc_tech and adding the feed to their friends list.