Archive for March, 2009

Working with MIT-Scheme

Monday, March 30th, 2009

I used MIT-Scheme for an assignment recently(writing a simple ML interpreter), and decided to write up my comments on the language(Scheme, not ML). While I have used Scheme before, I have not used MIT-Scheme, so I was curious how different it would be from the other Scheme varieties I had used.

For the most part, there weren’t too many differences. MIT-Scheme was missing some functions that were in the R6RS specification, but it also had some additions. The parse-buffers were very nice, at least until I found out that the version on our school computers did not come with them.

One issue I had was that by default, MIT-Scheme evaluates arguments from right to left. While this does not matter in most cases, there are a few times where I needed the order to matter - for example, when dealing with a token stream. To parse tokens, I created a function that would return the next token each time it was called, and so I had several bugs due to let-expressions not binding variables to the correct value.

I had a few problems with IO in MIT-Scheme. I spent a few hours debugging read-string, until I realized that the problem was that the function did NOT consume the ending character. That means that it worked the first time it was called, but the next time it would just return the empty string - because the first character was the semicolon that it just ended on! This seems very counter intuitive to me. I also wish their was a better was to suppress the output of the interpreter - I had to redirect the interaction-i/o-port to a file so that the values of evaluated expressions were not displayed.

I also did not manage to get the exception handler to work properly. I wanted to install my own handler to catch every error - invalid calls to
car, cdr, etc - and just restart the interpreter, instead of the scheme interpreter having an error and prompting for restarts. No matter how I installed the error handler, this type of error would still prompt for restarts.

Emacs Byte-Compiling

Sunday, March 29th, 2009

I recently became annoyed at how long my emacs took to start up, so I investigated ways to speed up the start time. The first thing I came across is huge: byte-compile all your elisp files.

Most major packages come with a makefile that will compile their .el files into .elc, but if not the following elisp function will compile all the .el files in a directory:

(byte-recompile-directory "~/emacs" 0)

However, I’ve found that doing this can mess up the current emacs session, so you may wish to use emacs from the command line to byte-compile files, as follows:

emacs -batch -f batch-byte-compile *.el

Compiling my elisp led to a speedup of roughly 5x when starting emacs, so I definitely suggest you compile your elisp files.

Installing MIT-Scheme on Ubuntu

Saturday, March 21st, 2009

Due to AppArmor support in the latest versions of the Ubuntu kernel, MIT-Scheme fails to work if the default install is used. To install and run MIT-Scheme on your Ubuntu machine, perform the following steps:

  • Download the source from here.
  • Unpack the .tar.gz file using:
    tar xzf mit-scheme-c-20090107.tar.gz
    
  • cd into the src directory of the unpacked folder
  • Build and install the package using the following commands:
    etc/make-liarc.sh
    make install
    

After you do this, you still need to configure the minimum mmap address:

sudo sysctl -w vm.mmap_min_addr=0

If you want to make this permanent instead of just until you restart your machine, edit your /etc/sysctl.conf file to add this binding.

Spacebar replacement for ido

Wednesday, March 18th, 2009

As a follow-up to my previous post, if the behaviour of space in ido-mode annoys you then put the following in your .emacs file:

(defun expand-fuzzy-match (str reg)
  "Returns the string starting at the beginning and containing all of
the characters in 'reg' in order.  Returns the smallest such string."
  (let ((new-reg (concat
                   "\\("
		   (apply 'concat
	           (mapcar (lambda (x) (concat ".*?" (string x))) reg)) "\\)")))
    (string-match new-reg str)
    (match-string 1 str)))
 
 
(defun ido-expand-match ()
  "Uses expand-fuzzy-match to expand the string in the minibuffer in ido"
  (interactive)
  (if (= 1 (length ido-matches))
      (ido-complete)
    (let ((full-str (expand-fuzzy-match (car ido-matches) ido-text)))
      (if (string-equal full-str ido-text) (ido-complete)
	(progn
	  (delete-region (minibuffer-prompt-end) (point))
	  (insert full-str))))))
 
(add-hook 'ido-setup-hook 'ido-my-keys)
 
(defun ido-my-keys ()
  "Add my keybindings for ido."
  (define-key ido-completion-map " " 'ido-expand-match))

This will change ’space’ from always using ido-complete to first expanding your search using the currently-selected option and then calls ido-complete if used again or if the expansion is the same as the original text.

Smex: A Replacement for M-x

Wednesday, March 18th, 2009

Smex is a replacement for execute-extended-command that uses ido to choose which commands to execute. If you don’t already use ido, you should. I’ll write more about it in a future post, but it makes buffer and file switching much nicer. It allows for fuzzy matching, history, file searching, and more. To enable it, add the following to your .emacs file:

(require ‘ido) ;interactive do
(setq ido-enable-flex-matching t) ;enable fuzzy matching in ido-mode
(ido-mode t) ;use ido for buffer/file switching
(ido-everywhere t) ;enable ido everywhere

Smex, as I mentioned before, uses ido to select a command. This lets you use all your ido customizations to select commands, such as fuzzy matching. This is incredibly useful, and something I had wished for before. It also doubles as an additional apropos, since once you have a function selected you can use “C-h f” to view that function’s documentation.

Smex does have its downsides, though. One of these is the behaviour of the spacebar. In execute-extended-command, spacebar will complete the next unique word in the currently selected command. In ido, it displays all the possible remaining completions in a buffer. Switching from these behaviours is quite annoying, and I would be much happier if the spacebar acted more like it did previously - I’ll probably hack up some elisp and post it later today. Smex also does not automatically update it’s cache unless emacs has been idle for 6- seconds, and so recently defined commands will not be available to use. This, however, is easily fixed by modifying smex-auto-update.

Another thing I noticed was that “C-a” does not move to the beginning of the minibuffer. I’ve had a few occasions where I was to start appending to the beginning of a command, but had to use backwards-work to get there. In ido, “C-a” toggles whether you are ignoring certain filetypes, which is irrelevant for smex.

To install smex, first install it from: http://github.com/nonsequitur/smex/.
Add the following to your .emacs file:

(require ’smex)
(smex-initialize)
(smex-auto-update)
(global-set-key (kbd “M-x”) ’smex)

After this, Smex should be set up on your system.

Posting from Emacs

Saturday, March 14th, 2009

I use Emacs for just about everything, and I just got it set up so that I can write these posts and publish them entirely within Emacs using weblogger.el. The installation and setup process for this is:

  • Download weblogger.el and xml-rpc.el.
  • Add (require ‘weblogger) to your .emacs file
  • Execute M-x weblogger-setup-weblog. This will ask for your server endpoint, which if you are using wordpress will be http://www.yoursite.com/xmlrpc.php. Make sure that RPC posting is enabled in your WordPress setup, as well. It will also prompt for your username and password.

Now, to post an entry, just use M-x weblogger-start-entry. Once you are finished, M-x weblogger-publish-entry will publish your buffer to your blog. By default, C-x C-s will also publish your entry, but if you are like me then you save roughly every other sentence, so I would suggest rebinding C-x C-s to something else.

To add tags to your post, use the ‘Summary’ field and add a comma-delimited list of tags. For example, the field for this entry is:
Summary: emacs, wordpress