CSE 303: Concepts and Tools for Software Development, Winter 2008
  CSE Home   About Us   Search   Contact Info 
 
Course Home
 Home
Administation
 Overview
 Course Wiki
 Email archive
 Anonymous feedback
 View feedback
 Homework Turnin
 
Most Everything
 Schedule
 
Other Information
 UW/ACM Tutorials
 303 Computing: Getting Started
   

CSE 303 Homework 2

Due: Monday 1/14/08
Turnin: nothing to turn in

Emacs exercises

FAQ

HW2 Q&A Wiki Page

I like the snood editor. Do I have to use emacs?
I'd like you to know enough about emacs to make an informed decision whether to continue using it or not. Learning a new editor is always a pain. Allocating homework time for these exercises is intended to help. There might be a question or two on exams to verify that you at least tried it.

Why emacs?
It's the de facto standard on Unix (or one of maybe two). Among other things, many of the editing commands (e.g., 'move left') work in other tools, for instance, editing command lines in bash. Additionally, it does some things other editors can't (for example, allowing you to compile your program without leaving the editor) and integrates with the debugger in ways other editors don't.

Are there any choices of editors that are just plain wrong?
Yes. Microsoft Word is not suitable for editing most anything other than formatted documents, at most. Notepad and Wordpad are not suitable for editing anything - there are much better choices. (These are not facetious examples. I've seen our majors using those editors on C programs, presumably because that's what they knew already.)
[Note: 'facetious' is an answer to the classic challenge, "Name an English language word that has all vowels appearing exactly once, in order."]

Emacs Overview

Emacs edits. You can do the usual things: type new text; delete characters and lines; move the cursor up, down, left, right. move the cursor to the top of the window or top of the document; replace all occurences of the string 'snood' with the string 'emacs'; etc.

One design decision in emacs is that its default mode is 'insert' -- anything you type is inserted in the document at the position of the cursor. In contrast, the other de facto standard editor (vim and its cousins) takes the opposite approach -- by default you're in cursor control mode, and you have to issue commands to get into and out of insert mode. Personally I prefer the emacs default, but that's just me, and 30 million other people. 30 million distinct people prefer vim, and 30 million prefer something else entirely. (Those numbers are completely made up, by the way, but you get the point.)

Because by default you're in insert mode, performing editing commands (like moving the cursor) requires typing keystrokes that you'd probably never want to type as text - emacs can't let you just type 's' to start a substitute command, for instance, because typing 's' should insert an 's' into the document.

The primary solution emacs uses is Ctrl+x (hold down the control key and type x), for all available x. As that doesn't provide enough distinct keystrokes for all of its commands, it also uses M+x ("meta x"). The meta means "hit the esc key," so M+x means "esc x" (as two keystrokes). All the operations you'll commonly need (at least as judging by how I use emacs) can be done with these kinds of commands. As always with emacs, there is more (e.g., there are commands you issue using the alt key). We're not going to let that worry us.

Reference Information

  • A useful cheat sheet of essential commands, from Stanford.
  • The UW Student ACM Chapter's list of Emacs links, which contains a more comprehensive, printable cheat sheet (among other things).

Gotcha's

There are a number of things (I'm aware of) that can commonly go wrong (where "commonly" means that I run into them all the time). They're caused by the chain of tools you happen to be using to talk with your emacs session. For instance, you might be working at home from a Windows machine, using cygwin to create a bash shell, using that shell to ssh to attu, and running emacs on attu.
  • The first common thing is that the Ctrl+space command (which is intended to set a 'mark' used in copying text from one place to another) doesn't work.
  • The second is that cut-and-paste from some window other than your emacs window doesn't work -- when you paste, you don't get the text you want.
I don't have good solutions to these problems. For the first, I have no solution at all (other than to not set marks, and instead use other commands to achieve the effect). My lazy approach is to avoid this situation, by setting up my tool chain in a way that it doesn't happen. (If you know of a solution, or even more about the cause, this would make a much appreciated Wiki topic.)

For the second, the issue is primarily one of understanding where the text is going when you do the cut (or copy) -- the clipboard on your local Windows box? the clipboard on the remote machine? somewhere else?

Here are two things to try. (The details of when they might or might not work would take longer to read than to just try them, even if I knew enough to give reliable guidelines.) First, try copying the text through whatever natural method there is - highlight and Ctrl+c is one example. Now move to the emacs window and hit the right mouse button. This MIGHT cause some tool to pretend that you just typed everything that is some buffer now containing the text you copied. Emacs will respond by inserting that text.

The second thing is that, if you're running X, it seems to have its own clipboard. Using the menu copy and paste operations can work even when the emacs paste (Ctrl+y) doesn't. Try it. (And Wiki it, if you have better information. Thanks.)

Caveat: There are multiple commonly found flavors of emacs. They can differ a bit in details.

Exercise 1

All exercises are intended to mimic the kinds of things you'll do in your courses, including this one. So, for one thing, the files we're going to create are programs. For another, I'll try to have you do the kinds of things you might do in writing and editting programs.
  1. Navigate (using cd) to the directory where you want your files for this assignment to live.
  2. emacs hw2.c
  3. You'll probably get a new window, and it will probably have some kind of message in it that I've never read. You can type any emacs input at this point. Ctrl+l (lower case L) is innocuous, in the sense that it gets rid of the message but doesn't do anything to your file.
  4. Now create the contents of your file, by typing the C program below. (We'll cut-and-paste it in exercise 3.) The beauty of typing it is that you'll make the kinds of mistakes that you individually are prone to, and so you'll end up spending your time on the kinds of emacs commands that are useful for fixing those mistakes.

    Have a cheat sheet handy for viewing. Try a few things (e.g., the backspace key) just to see what they do.

    While we're entering a C program, this assignment isn't about that, so don't worry about the details. You should recognize some of the code, and be able to say what it is doing, so long as you turn your brain off while doing it. Here's an example. You know what a for loop does, and how it's written. For one thing, it loops. For another, it is written with three parts: initialization; (non-)termination condition; update expression. The for loop in this program probably looks very odd. If you try to understand it as a gestalt you might well end up deciding you don't know anything. If you try to be as dumb as the compiler, you end up knowing something. It loops. Before executing the body, it executes c = getc(), which assigns something to the variable c. Etc. Starting by being careful to know what you can know often helps fix problems that the gestalt view doesn't help with.

    This program intentionally includes enough new C weirdness that it's likely you'll mistype something, even if you're very careful. (Don't worry -- the compiler will find typos. Usually.)

    Emacs will help with indenting while you're typing. You can cause it to re-evaluate the indenting of the line the cursor is on by hitting tab. (The cursor can be anywhere in the line when you hit tab. Emacs is smart.) It should also be colorizing (see HW0).

    You should look at the cheat sheet(s), but if you get into trouble note that Ctrl+g is a way of saying "abort" -- useful if you mistype something and find yourself in a mode that you don't even recognize -- and that there is an undo command -- useful if you hit some key and a lot of the text disappears unexpectedly, for instance.

    This program has (intentionally) some bugs.

    #include "stdio.h"
    
    // Reads from stdin.
    // Writes to stdout.
    
    void out(char c) {
      printf( "%c\t%3d\t0x%02x\n", c, c, c);
    }
    
    int main( int argc, char* argv[] ) {
      char c;
    
      printf("Enter whatever.  End with a blank line.\n");
    
      for ( c = getc(); c != '\n'; c = getc() ) {
        out(c);
        do {
          c = getc();
          out(c);
        } while ( c != '\n' );
      }
    
    }
    
  5. Make sure you've saved the file. (The file on disk is different from the one you're seeing in the edit buffer if you see a couple of asterisks near the lower left corner of the emacs window. It never hurts to hit Ctrl+x Ctrl+s to force a save, though.)
  6. Now we'll compile the program. Open a new window and navigate to the directory hw2.c is in, then issue this command:
         gcc hw2.c
         
    You'll see some errors. I get this:
    [attu3] ~/cse303/08wi/hw2-files> gcc hw2.c
    hw2.c: In function 'main':
    hw2.c:15: error: too few arguments to function '_IO_getc'
    hw2.c:15: error: too few arguments to function '_IO_getc'
    hw2.c:18: error: too few arguments to function '_IO_getc'
    
    We're going to use the line numbers in the error messages, but your line numbers may be a bit different - e.g., you may have two blank lines where I have one, or vice versa.

    (Note: The system you're using must have gcc installed, or you're going to see an entirely different kind of error. attu has gcc, worst case.)

  7. That's all very confusing. All we care about now are the line numbers. Return to the emacs window. Watch the bottom line of the window as you type the following:
    M+x goto-li<tab> <enter> 15 <enter>
    That will put the cursor on line 15, which has two errors. (You might notice a message flashing at the bottom suggesting a shorter way to type this in the version of emacs you're using.)
  8. The bug is that I should have used the function getchar rather than getc. That bug happens twice on line 15 and once on line 18. Move the cursor onto the 'g' of the first 'getc'. Type
    M+d
    That erases the characters from the cursor up to the next bit of whitespace. Now type getchar. (We could have just moved the cursor to the space after getc and typed har, but there's no fun in that.)
  9. Let's now fix all the rest of them (i.e., both). Let's also not break anything while we're doing it -- this is going to replace all occurences of the string 'getc' with 'getchar', from the current cursor position to the end of the file. Fortunately, all of them are broken and need fixing.
    M+x repl<tab>s<tab> <enter> getc <enter> getchar <enter>
  10. Save the file and re-compile. No error messgaes this time.
  11. The compiler has produced an executable program, named a.out. To run it, type
    ./a.out
    I get this, for example (where I have artificially introduced color on this web page so that you could see what I typed -- the stuff in red):
    [attu3] ~/cse303/08wi/hw2-files> ./a.out
    Enter whatever.  End with a blank line.
    abcdef 012345
    a        97     0x61
    b        98     0x62
    c        99     0x63
    d       100     0x64
    e       101     0x65
    f       102     0x66
             32     0x20
    0        48     0x30
    1        49     0x31
    2        50     0x32
    3        51     0x33
    4        52     0x34
    5        53     0x35
    
             10     0x0a
    ABCDEF +-/*
    A        65     0x41
    B        66     0x42
    C        67     0x43
    D        68     0x44
    E        69     0x45
    F        70     0x46
             32     0x20
    +        43     0x2b
    -        45     0x2d
    /        47     0x2f
    *        42     0x2a
    
             10     0x0a
    
    [attu3] ~/cse303/08wi/hw2-files>
    
    In an emergency, if you can't figure out how to exit the program gracefully, type Ctrl+c.
  12. Optional bonus question: What is it doing?
  13. Exit emacs: Ctrl+x Ctrl+c.

Exercise 2

  1. Delete hw2.c, using the rm command. Since that command's job is to delete files, if you're a novice Unix user you could consider looking at the man page (man rm) before issuing it. On the other hand, if your current working directory has nothing but files for these exercises in it, just winging it is risk free.

    Now do exercise 1 again.

    The mind numbs just at the mere thought of this mind numbing repetition, but repetition is a way to get fast at inherently mind numbing tasks. And the faster you can translate your thoughts into edited code, the less you'll be distracted from thinking about your code when making changes, and the better you'll be at the entire process.

Exercise 3

  1. Delete hw2.c. Start up emacs: emacs hw2.c
    Now try to recreate the contents of that file by cut-and-paste. Copy the text on this page (up above, in a box). Move to your (blank) emacs window and type:
    Ctrl+y
    which is the emacs yank command (yank being it's name for paste).

    The trick in this part is to get cut-and-paste between windows to work, so that you can use it in the future. It might just work, but it might not. If it doesn't, how to get it to work depends on many things - where your browser is running (and possibly what browser you are running); where emacs is running; how emacs is running (e.g., inside an X window?); how you're connected to the system on which emacs is running, etc.

    If you can't get it to work, have a look again at the Gotcha's section above - the suggestions there might suggest something to you that does work. If not, use the course Wiki to post questions, including enough detail about what machine(s) you're using, how you're connecting, etc. that there's a chance someone can provide some help.

    If you can get it to work, check the course Wiki. Someone might be having trouble and you can help!

Bonus Tip For Firefox Users

Firefox has an extension, called It's All Text!, that lets you use emacs to edit text boxes on web pages. It's handy to edit the Wiki pages, for instance, or anytime else you want to use an actual editor to enter more than your name and address on some web page.
  1. Firefox menu Tools/Add-ons.
  2. Follow the Get Extensions link in the lower right.
  3. Enter It's All Text in the search box at the top
  4. Follow the obvious link, and click on the big green button.
Shortcut: Go here to find the big green button.

(Okay, okay: this extension works with editors other than emacs, so you can use it even if for some crazy reason you don't want to use emacs as your external editor.)


Computer Science & Engineering
University of Washington
Box 352350
Seattle, WA  98195-2350
(206) 543-1695 voice, (206) 543-2969 FAX
[comments to zahorjan at cs.washington.edu]