The Last Answer

For some reason The Last Answer, an Isaac Asimov short story from 1980 was popping up on the Internet this week. Oddly, I’d never read it before and being an Asimov fan, I thought I’d read most of his fiction. Being that he started publishing short stories in 1939, this story was written fairly late in his career—he died in 1992. On the other hand, he was active right up until the time of his death so perhaps it wasn’t that late.

In any event, The Last Answer is the quintessential Asimov short story: short and pithy. It reminds me of The Feeling of Power, one of my favorite Asimov short stories. If you like SF, and especially if you like Asimov’s SF, you should definitely spend 5 minutes to read this story, even if you’ve read it before. I’m sure you won’t be sorry.

Posted in General | Leave a comment

EmacsGolf 4

Xah Lee is doing some refactoring and wants to change this

;; define several class of keywords

(defvar mylsl-keywords
  '("break" "default" "do" "else" "for" "if" "return" "state" "while")
  "LSL keywords.")

(defvar mylsl-types
  '("float" "integer" "key" "list" "rotation" "string" "vector")
  "LSL types.")

(defvar mylsl-constants
  '("ACTIVE" "AGENT" "ALL_SIDES" "ATTACH_BACK")
  "LSL constants.")

(defvar mylsl-events
  '("at_rot_target" "at_target" "attach")
  "LSL events.")

(defvar mylsl-functions
  '("llAbs" "llAcos" "llAddToLandBanList" "llAddToLandPassList")
  "LSL functions.")

into this

;; define several class of keywords

(defvar mylsl-keywords nil "LSL keywords.")
(setq mylsl-keywords '("break" "default" "do" "else" "for" "if" "return" "state" "while") )

(defvar mylsl-types nil "LSL types.")
(setq mylsl-types '("float" "integer" "key" "list" "rotation" "string" "vector"))

(defvar mylsl-constants nil "LSL constants.")
(setq mylsl-constants '("ACTIVE" "AGENT" "ALL_SIDES" "ATTACH_BACK"))

(defvar mylsl-events nil "LSL events.")
(setq mylsl-events '("at_rot_target" "at_target" "attach"))

(defvar mylsl-functions nil "LSL functions.")
(setq mylsl-functions '("llAbs" "llAcos" "llAddToLandBanList" "llAddToLandPassList"))

His first inclination was to just brute force it with cut and paste and some typing but then he asked himself what he would do if he had hundreds of lines like this to fix. I can think of at least two efficient ways to do this. What would you do?

Posted in General | Tagged | 7 Comments

Giant Emacs Reference Sheet Revised

We here at Irreal are big fans of Aaron Hawley’s Giant Emacs Reference Card and have often written about some of the tidbits contained therein. Now Hawley has announced that he’s revised the reference and added another 500 commands for a total of more than 2000. It now covers keyboard macros, Dired Mode, Occur Mode, table, and Calc.

For me, one of the nicest things about the old reference card was the pleasure to be had from reading random sections and learning something new. I’m looking forward to the same pleasure with the new version. If you haven’t yet seen it, be sure to head on over and take a look. I have it bookmarked in the Documents section of my browser for easy reference.

Posted in General | Tagged | Leave a comment

Maintaining My Blog Post Queue

Because inspiration doesn’t necessarily strike on demand, I like to stay a few posts ahead. For the last year, my procedure was to push a completed post to the site as a draft and then publish it when I was ready for it to appear on the site. The recent attack on WordPress administration pages and the subsequent inability to release pending drafts have made me rethink that strategy.

Now what I do is maintain a separate file on my local computer called push-queue.org that contains links to posts that are ready to go. Here’s what the file looks like1:

* Ready to Post [3/5]
- [X] one-way-function
- [X] action-report
- [X] tags
- [ ] worst-passwd-tips
- [ ] hyper-super
ORG-LIST-END-MARKER
* Pending Posts

Those links are in normal Org format. For instance, the tags link looks like:

[[file://Users/jcs/org/blog/tags.org][tags]]

Naturally, being lazy, I don’t want to maintain the push-queue.org file by hand so I wrote a bit of Elisp:

(defun jcs-add-to-push-queue ()
  "Add the file associated with the current buffer to post queue in
push-queue.org"
  (interactive)
  (let ((file (buffer-file-name)))
    (when (null file)
      (error "Buffer not associated with file."))
    (find-file "/Users/jcs/org/blog/push-queue.org")
    (beginning-of-buffer)
    (if (search-forward-regexp "^\\* Ready to Post")
        (progn
          (forward-line)
          (org-end-of-item-list)
          (insert (format "- [ ] [[%s][%s]]\n" file (file-name-base file)))
          (org-update-statistics-cookies 'all))
      (error "Missing top-level header."))))

Now when I finish a post, I just type 【Meta+x】 jcs-add-to-push-queue (or really, thanks to smex, just a few letters of it) and push-queue.org is updated automatically. When I’m ready to publish a post I just click on the link to open the post in an Emacs buffer from which I can publish it directly to WordPress.

Footnotes:

1 The ORG-LIST-END-MARKER isn’t really there. It appears to be an artifact of exporting a Org file from inside another Org file. Or something.

Posted in General | Tagged , | 1 Comment

disaster.el

This is great! It’s disaster.el, a really useful tool if you write in C/C++ and can grok assembly language. What is does is compile and disassemble the C/C++ code in the current buffer. In will even jump to the code generated by the C/C++ source that the cursor is on. It uses objdump, which is part of GNU binutils so you have to have it installed. It’s not installed on OS X, so you’ll have to load it if you want to use disaster.el on that platform. The amazing thing about disaster.el is that it’s a relatively simple package. That fact demonstrates, once again, how powerful a development platform Emacs is.

This project is almost enough to make me wish I was still writing in C. The ability to quickly look at the generated code can be a big win. Happily, CL and Slime let’s me do the same thing and even Elisp lets me look at interpreted code so I won’t have to go back to C.

Posted in Programming | Tagged | Leave a comment

A O(n) Solution to the Last Elisp Challenge

As I mentioned in the post, my An Elisp Challenge post was suggested by this Programming Praxis post. The next Programming Praxis post was a followup that discussed Booth’s Algorithm, which calculates how many left rotations a string must undergo to be lexicographically minimal. The thing about Booth’s algorithm is that it’s O(n) in the length, n, of the string. That gives us a way of making cyclep O(n) as well.

First, we need Booth’s algorithm. The following is pretty much a direct translation of the Python code in the Wikipedia article into Elisp

(defun booth (str)
  "Calculate the number of left rotations STR must undergo to be
lexicographically minimal."
  (let* ((str (concat str str))
         (n (length str))
         (fail (make-vector n -1))
         (k 0))
    (dolist (j (number-sequence 1 (1- n)) k)
      (let ((i (aref fail (- j k 1))))
        (while (and (/= i -1) (not (eq (aref str j) (aref str (+ i k 1)))))
          (when (< (aref str j) (aref str (+ i k 1)))
            (setq k (- j i 1)))
          (setq i (aref fail i)))
        (if (or (/= i -1) (eq (aref str j) (aref str (+ i k 1))))
            (aset fail (- j k) (1+ i))
          (when (< (aref str j) (aref str (+ i k 1)))
            (setq k j))
          (aset fail (- j k) -1))))))

Once we have Booth’s algorithm, the rest is easy:

(defun rotate-string (str n)
  "Rotate STR left N positions."
  (let ((k (mod n (length str))))
    (concat (substring str k) (substring str 0 k))))

(defun cyclep (s1 s2)
  "Check if S1 and S2 are cycles of each other."
  (string= (rotate-string s1 (booth s1)) (rotate-string s2 (booth s2))))

The rotate-string function does a left rotate of n positions on the string and the cyclep function ties everything together by rotating both strings into their lexicographically minimal forms and checking to see if they are equal.

All of this is sufficiently complicated that the previous O(n2) solution will probably be faster for reasonably sized strings. Still, it was fun to put this “faster” solution together even though I’m unlikely to need a cyclep of any speed.

Posted in Programming | Tagged , | Leave a comment

Xah Lee on Defining Super and Hyper Keys

If, like me, you are always writing little pieces of Elisp to do some task or another or if you have a lot of ELPA packages installed, chances are it’s hard to find key sequences for them. In theory, 【Ctrl+c】 is reserved for just this purpose but in practice rather than being reserved for users it gets used by almost every package. Even my beloved Org mode is guilty of this. 1 On the other hand, what choice do they have? There just aren’t enough key sequences based on the 【Ctrl】 and 【Meta】 keys.

Emacs, of course, can also make use of the 【Super】 and 【Hyper】 keys but modern keyboards no longer have these keys. All is not lost, though, because you can map some otherwise unused key to 【Hyper】 or 【Super】. Xah Lee has a useful post on defining hyper and super keys for Windows, OS X, and Linux. One trick that he misses for OS X is using the 【fn】 key for this. That’s something I learned from Magnar Sveen. I wrote about that here (don’t worry, you can still control music and the other special functions).

I have mapped several key sequence that begin with 【Hyper】. You almost never trip over a predefined sequence and even if you do that command invariably has some other key sequence assigned to it too. Take a look at Lee’s post and experience the joy of a whole new key sequence space.

Update: Corrected my misinterpretation of the reserved key rules. See the comments for further information.

Footnotes:

1 My understanding of the reserved key rules was faulty. In the comments, Phil points out that only 【Ctrl+c】 and a letter are reserved for users. Control characters and numbers are not reserved.

Posted in General | Tagged | 6 Comments

The Worst Password Tips

Mark Burnnet has an interesting post on The Worst Password Tips. His main thesis is that much—or even most—of the advice you hear about choosing passwords is no longer good advice. It used to be that passwords like p@r013 gave reasonable safety. Now substitutions like that are well known and all cracker software is configured to try them.

Similarly, passwords that take the first letter from each word of a phrase, such as my baloney has a first name → mbhafn, seem like a good idea because they won’t appear in any dictionary. The problem is that they are too short and in this age of GPU based crackers are subject to brute force attacks. The same goes for short random passwords such as n%C+k3q. Again it seems safe because it won’t be in any dictionary but it’s still short enough to be brute forced.

Burnnet recommends long passwords and, in fact, says that length is more important than randomness. I typically use a random 20 character password that gets generated automatically by my password management software. When sites allow it, the random password will have upper and lower case letters, numbers, and symbols. That increases the size of the “alphabet” that the cracker must deal with and makes his work much harder. Sadly, many sites have stupid, security-destroying restrictions on password length and composition but that’s another story. I’ll say only that such sites can be presumed to have other shoddy security practices and should be avoided if possible.

If you’re still making your passwords up by hand, you really should check out Burnnet’s post. It will give you some advice on what to avoid. Better yet, stop making them up and get some software such as KeePass or 1password to manage your passwords.

Posted in General | Tagged | 3 Comments

Tags with Emacs

One of the oldest and most general code navigation tools is tags. The idea is that you generate a tags database for your code base and then use it to tell your editor where to find the definition of a variable, function, class, or whatever. I used to use tags when I was a Vim user but haven’t bothered since I moved to Emacs. Mostly that’s because I don’t deal with large code bases anymore so I don’t have the need. Of course, that means I never bothered to learn how to work with them in Emacs.

Marius Mårnes Mathiesen has written an excellent post on using tags with Emacs. He covers all the basics, including how to generate the tags database and keep it up to date. If you deal with large code bases and don’t already have a tool that works for you, take a look at his post. Tags may be just what you need. Even if you already have a solution, it’s worth taking a look to see if tags will suit you better.

Posted in Programming | Tagged | Leave a comment

Outage Status Update and After Action Report

I’m happy to report that I can again log into the administrative page. I’ve released everything in the moderation queue and cleaned up a bit of a mess that I left while I was trying to figure out how to repush the posts that where already on the site but unreachable.

My hosting provider sent its customers an email explaining the situation and the actions that they took. It turns out that they were blocking connection attempts to the WordPress administrative page both to protect the accounts and also to lessen the stress on their servers. I suspected that they were filtering the logins to the admin page because I ran a packet trace on one of my attempt to log in and noticed that

  1. The initial TCP 3-way handshake completed in less that half a millisecond.
  2. Not a single GET request to the admin page was answered, even partially.

If the logins were failing because of too much traffic (a DOS attack, in effect), I would have expected to see the TCP handshake to show retransmissions and be slow. I would also have expected at least some of the GETs to succeed, if only partially.

In any event, the excitement is over for the time being although my hosting provider says that the attack is ongoing but that they have put less drastic measures in place. I want to take a moment to give my provider, IX Hosting, a shout out. They were very proactive in their reaction to this attack, first be recognizing there was a problem and then by blocking all access to the WordPress admin page when it became clear that the attacks were coming from thousands of IP addresses and that trying to block the attacking sites would not suffice.

When I first noticed the problem, I was able to engage a support tech on an IM chat immediately without having to open a support ticket or anything else. By and large things run smoothly with IX Hosting and when I do have issues, they are always dealt with in a timely, efficient, and friendly manner. If you’re looking for a reasonably priced home for your blog or other type of site, give them a look. I’ve been very happy with them.

Posted in General | Leave a comment