CS010 Practice 11

More Emacs, Preprocessor Commands, File Manipulation

C Mode

Wrapping long lines

A second useful feature is to allow Emacs to break long lines so that your lines all fit nicely in the window and print well. This works by specifying a column number to serve as your "fill column". If you type a space to the right of the fill column, Emacs will start a new line, indenting it as necessary. The default fill column is 70. To turn on the mode that breaks your long lines, type:

M-x auto-fill-mode

or place the following line in your C mode hook function in your .local_emacs file:

(auto-fill-mode 1)
   

Tags - finding definitions and uses of functions

Another useful feature is called tags. Tags creates a table of the functions declared in a collection of files. Within Emacs, you can ask to see a tag, and it will show you the definition of a tag. This is very convenient if you are looking at a function call and want to see the definition of the function. Before using tags, you must create a tags file. This is done with the Unix command called etags. You give etags a list of file names. It reads those files a builds a table of everything declared in those files and writes the table to a file called TAGS in your current directory.

To use tags within Emacs, you first need to tell Emacs where the tag file is:

M-x visit-tags-table 

You will be prompted for the name of the file containing the tags. It will list TAGS as a default, which you should accept by hitting the return key.

If you want to find the definition of a function, you ask Emacs to find a tag:

M-.

You will be asked for the name of the tag. It will show you the definition of that tag, reading in the file if necessary.

If you want to find everywhere that a function is used in the collection of files that has been tagged, use:

M-x tags-search

Enter the name at the prompt. To continue the search, type M-,

Suppose, you want to change the name of a function. You can use a command similar to query-replace except that it looks through all the files that have been tagged:

M-x tags-query-replace

If you want to see everything that has been declared in a single file, use:

M-x list-tags

You will be prompted for the filename.

Remember to keep your TAGS file up-to-date by re-running the Unix etags command when you change your files. (You could put a rule in your Makefile to do this.)

Customize Command

A relatively new command in Emacs is the customize command. This command gives you a hypertext style interface to move between customization forms. Each form contains a list of related Emacs customization variables with some documentation. You can customize these variables by changing the value on a form. You can then choose whether you want to apply the customization for just that Emacs session or save it for all future Emacs sessions. If you choose to save it, it will write the necessary Lisp code into your .local_emacs file for you!

I won't go into detail on how to use this command. It's just here for you to explore if you want to start to become an emacs geek. (not for everyone!)

Unfortunately, there are limits to what can be customized this way. First, it can only be used to change the values of variables. Second, those variables have to be declared as customizable variables in the Lisp code that created them. Since this is a new feature, older code does not do this. Nevertheless, this appears to be a very useful new feature allowing a lot of customizations without requiring Lisp programming.

Emacs Lisp Packages

The customizations that we have seen are written in Emacs Lisp, as is the .local_emacs file that we have been working with. Emacs is probably the first widely used open source project, predating Linux by 10 or more years. As a result, there are many, many customizations available. The good news, then, is that is rarely necessary to write your own. It may be useful to install and customize something that somebody else did, though.

Finding them

Emacs has been around a long time. Customizations that have proved to be popular and stable have become part of the standard release of Emacs. Thus, before searching the Web for some kind of package, you should first see if something is already available in your current installation. There are a few ways you can do that. First, you could use the apropos command to see if there is anything that meets your need. For example, suppose you want to do some Java programming and would like to use a Java mode similar to the C mode we have been using. You can type:

C-h a java

You will see that there is indeed a Java mode already available.

Another thing you can do is look at the files in the directory where Emacs stores the customizations it comes with. The directory is /usr/local/share/emacs/20.7/lisp. If you scan the list, some names will be more suggestive of what they do than others are. For example, we might guess that ediff is a version of diff (the file comparison utility) that runs inside of Emacs. Note that the files in this directory end in either .el or .elc. You should only look at the .el files; .elc are compiled files.

Creating your own

Creating your own goes way beyond the scope of this course. Read Programming in Emacs Lisp to get started. Be prepared to spend a lot of time if you want to master this!

Exercises

  1. Modify your concordance program to take the name of a file to run the concordance on from the command line. If you can't open the file, report an error and quit. If no file name is given on the command line, read the words from standard input. If more than one file is given, use them all to build your concordance.
  2. Change your concordance into a spell checker. Have it take two files. The first is the "dictionary" of words. Build the concordance from that. The second file is the file to be checked. Read each word from the file and check whether it is in the dictionary.

Return to CS 010 Home Page