Since I wrote that post a month and a half ago, I figured I’d write a short one tonight. I’m about halfway through The Little Schemer now and I’ve spent a good amount of time trying to get Emacs configured in a way I like. My .emacs file has grown a bit and so I thought I’d share some of the cool things I’ve either found or figured out for myself.

M-x copy-line

;; Copy 1 Line
(defun copy-line ()
  (interactive)
  (kill-ring-save (line-beginning-position) (line-end-position)))

It took me a while to figure out how to get this working, as I could only find a snippet for “M-x copy-n-lines” (below). It’s quite simple after I worked out the syntax. It simply copies the text on a line to the kill-ring. It doesn’t care where your cursor is, just what line you are currently on. I’ve bound it to “M-k” to be analogous to “C-k” (kill line), in the same way “C-w” is kill region (cut) and “M-w” is copy region (copy).

;; Set M-k to be the shortcut for copy line
(global-set-key [(meta k)] 'copy-line)

Pretty simple.

Here is the snippet I based the above command on: (I can’t remember where I found it to attribute…eek)

;; Copy N Lines
(defun copy-n-lines (n)
    "Copy N lines at point to the kill-ring."
    (interactive "p")
    (kill-ring-save (line-beginning-position) (line-beginning-position (1+ n))))

This one isn’t as immediately useful to me as copying a single line. I have been using the copy single line command to copy text I enter directly into the scheme interpreter so I can reuse it. I don’t use this one as much, I tend to just select the region and copy the region.

M-x dot-emacs

(defun dot-emacs ()
  "Visit .emacs"
  (interactive)
  (find-file "~/.emacs"))

This is another snippet I found and that I’ve found to be extremely useful. I generally am deep within my scheme project folders and to delete the saved path in the find-file command buffer gets old fast. This has saved a fair bit of time already and I haven’t even been using Emacs very much. I haven’t bound it to a command yet. It autocompletes after typing ‘dot’.

lambda-mode

;; real lisp hackers use the lambda character
;; courtesy of stefan monnier on c.l.l
(defun sm-lambda-mode-hook ()
  (font-lock-add-keywords
   nil `(("\\"
   (0 (progn (compose-region (match-beginning 0) (match-end 0)
        ,(make-char 'greek-iso8859-7 107))
      nil))))))
(add-hook 'emacs-lisp-mode-hook 'sm-lambda-mode-hook)
(add-hook 'lisp-interactive-mode-hook 'sm-lamba-mode-hook)
(add-hook 'scheme-mode-hook 'sm-lambda-mode-hook)

This is probably the coolest Emacs thing I’ve seen yet. It works absolutely flawlessly. Whenever you write “lambda” it substitutes an actual lambda for you. If you backspace on the delete, it switches it back and you are left with “lambd”. It simply hides it. Amazing. I love Emacs already.

I’m still trying to figure out how to change the font within Emacs so that it displays a better looking lambda character. I haven’t had much luck yet with trying to get anything else working.

-Saterus


I originally wrote this on November 19th, but I didn’t get around to starting this site until January. I wanted to get this stuff down before it faded from memory and I didn’t want to let messing around with setting up a website stop me from writing it.

11.19.08:

The Little Schemer arrived today. I installed Ubuntu last weekend in preparation. I’ve been getting situated in Emacs all this week. I highly recommend the basic GNU Tutorial as a way to get used to maneuvering in Emacs. It’s a bit dry, but don’t let it wear you down. Get through it, hope to pick up half of the normal shortcuts and then start using Emacs for things. I plan to revisit the tutorial in a month to pick up on the shortcuts I missed the first time around. I’m actually writing this entire post within emacs as a dog-fooding way to get used to it.

I’ve sort of written a tutorial for getting things within Linux and Emacs up and running. I found a lot of ‘beginning linux’ things online, and a lot of ‘beginning emacs’ things, but nothing quite fit what I needed. What I really needed was a procedure to follow to see how things are done. It’s nice to be given the shortcuts in Emacs or told that I can do certain things with the Linux terminal, but without any clear goal, I was a bit lost.

I wrote this with the intention of the audience having roughly the same linux and scheme experience as I did at the time of its writing (very little). If you have helpful tips to add to this that I skip over, let me know and I will hopefully update this as a future reference. If you’re in the same situation I was in when I wrote this and you get stuck, let me know and I can try to make steps more clear.

Phase I: C

I recommend you play around with Emacs’ capabilities a bit before you try to actually accomplish anything. As a warmup, I wrote a classic Hello World program in C and compiled it and ran it in the Emacs *shell* buffer. It took me a while, but I was pretty pumped when it worked. Here’s what I had:

#include<stdio.h>
int main()
{
    printf("Hello Emacs!\n");
}

I saved this as “hello.c”. I used “M-x compile” to initiate a compile sequence within Emacs (1). The minibuffer will then ask you what you would like to do next. To compile a C program, you can simply use GCC(GNU Compiler Collection, which includes a C compiler) which should be included in your active distro. So in the minibuffer, I typed “gcc hello.c -o hello-executable” and created an executable file named “hello-executable” in the same directory as my hello.c file. If you don’t specify the “-o hello-executable”, gcc will still compile your file, but it will name it a.out. It should bring up a second buffer which congratulats you on your successful compilation (right?). Use “C-x 1” to get rid of this compilation buffer.

To execute our revolutionary program within Emacs, type “M-x shell”. This should bring up a buffer named *shell*. This lets us perform most shell operations here. I recommend using ‘ls’ to test it out (2). Anyway, now that we have our Emacs shell up, we can go ahead and run our C program. If you’re like me, you have no idea how to actually execute this thing in a linux shell. A few minutes of googling revealed the magic incantation of “./hello-executable”. As near as I can tell, the dot-slash lets the shell know you’re trying to access the file in the directory rather than asking for information about it. If you’ve been following along, congratulations! You just wrote, compiled, and ran your first C program in Emacs.

Phase II: Java

Next I decided to crank up the difficulty a notch and decided to make a small Java program. In order to actually be able to compile and run a Java program, we’ll need a JVM. “sudo apt-get install sun-java6-jdk” should get us set up with the Java packages we need to begin playing around. This is the test file I whipped up to try out Java in Emacs:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

public class foo extends JFrame
{
    public foo()
    {
        ImageIcon pic = CreateImageIcon( "bunny_pancake.jpg", "This is ridiculous." );

        this.add( new JLabel("", pic, JLabel.CENTER ) );
        this.setVisible(true);
        this.setPreferredSize( new java.awt.Dimension( 400, 300 ) );
        this.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        this.pack();
    }

    public static void main( String[] args )
    {
        new foo();
    }

    private ImageIcon CreateImageIcon( String path, String description)
    {
        java.net.URL imgURL = getClass().getResource(path);

        if( imgURL != null ){
            return new ImageIcon( imgURL, description );
        }
        else
        {
            System.err.println( "Couldn't find the file: " + path );
            return null;
        }
    }
}

If you notice a striking resemblance between my CreateImageIcon method and Sun’s JLabel tutorial method, don’t be shocked (I couldn’t remember the syntax for loading resources). Anyway, my basic test includes some gui components and an external file. If you’re following along, create a directory, copy this to foo.java. Find a random picture to display in your JLabel and save it in the directory. Rename the file or the path in the source as necessary.

Using some Emacs magic (“M-x compile”) we can compile our Java example. Now, when we define the parameters, we type “javac foo.java”. This should output “foo.class” to our test directory. Open our shell back up with “M-x shell”, or if it’s still running from before, “C-x b *shell*”. Make sure you are in the correct directory and type “java foo”. Volia! A bunny with a pancake on its head! A just reward.


Ridiculous...

Ridiculous...

Phase III: Scheme

Now the serious business starts. Since Emacs has its own variant of Lisp, it only makes sense that it supports Scheme. First, you must specify which Scheme interpreter you wish to use with Emacs. We have to make sure a Scheme interpreter is installed. To do this, I used “sudo apt-get install scm” in the shell. Let apt-get do its thing. Once it’s finished, type scm in the shell to make sure it’s working. Test it with “(+ 2 2)” in the shell. It should output 4. If it spits back an error, something went wrong somewhere. If it outputs 5, uninstall Big Brother Linux and get a normal distro because it’s just going to keep messing with you like that.

Now we have to configure Emacs to use scm. To do this, open your .emacs file (3). Type “;; Set scm *return* (setq scheme-program-name “scm”)”. After you’ve finished typing this, leave your cursor on the end of the line. That will set scm as our default scheme interpreter within Emacs. Use “C-x C-e” to evaulate the s-expression. Now use “M-x run-scheme” to load a scheme interpreter into memory and brings up another buffer.

Open a new file. I used atomic.scm. Let’s do something simple, such as the setup for starting The Little Schemer. The book uses a function named “atom?” throughout, though this is not a standard Scheme function. It instructs us to use:

(define atom?
    (lambda (x)
        (and (not (pair? x)) (not (null? x)))))

This will return a boolean value, telling us whether or not the given parameter x was an atom or not. With your cursor still on the same line, use “C-x C-e” again to evaluate the s-expression before the cursor. This loads the function into memory and makes it usable.

To test our new “atom?” function, lets go down 2 lines and type:

(atom? 5)

Evaluate it using  “C-x C-e” again. The output should be “#t”, true. Now go down another couple of lines and try:

(atom? '())

Again, evaluate. The output here should be “#f”, false. Victory, right? Yep. We’ve figured out how to write, compile, and run C, Java, and Scheme programs all from Emacs. Productive night if I do say so myself. Tomorrow I can start plunging into The Little Schemer and doing real examples. Now it is 1:38 and I have work in the morning.

-Saterus

Notes:

(1): “M-x compile” is going to translate to “press alt and x, then type out compile”. You can watch this in the minibuffer at the bottom of the screen. All “M-x” commands are going to be fully typed out command names, while “C-x” commands will be shortcut keys such as “C-x b” to switch buffers or “C-x C-g” to cancel an action.

(2): It’s hard to read, but the directory information is there if you look. I’m sure there is a way to clean this up and make it appear more like a normal shell. A quick google tells me that Dired should be what I will look into when I take the time to figure this out.

(3): The .emacs file is your all purpose Emacs configuration file. Open it by typing “C-x C-f /.emacs”. It is run everytime Emacs is opened. I’ve barely scratched the surface and I’ve already started throwing things in my .emacs file such as “;; Type “y”/”n” instead of “yes”/”no”. (fset ‘yes-or-no-p ‘y-or-n-p)”.

Useful Resources:

A nice list of Emacs commands. Nothing fancy, just ascii, but its exactly what I need sometimes.
http://www.cs.rutgers.edu/LCSR-Computing/some-docs/emacs-chart.html




Categories

Archives