Dana Vrajitoru
C151 Multi-User Operating Systems

Lab 4

Due date: Monday, February 22, 2021.

In this lab you will get acquainted with the Emacs editor. For this lab it's better to work directly on a Linux computer that has Emacs installed or to log on to the remote host with an X11 client connection, either using the -XY for ssh, or from putty using Xming. If you work from putty without Xming, remember F10 as the key that gives you access to the menus. You may also want to remember Ctrl-g as the key combination that cancels operations.

Note.

The Meta key in the Emacs instructions is the Alt key under Linux or Windows/putty. In the Emacs documentation it is called Meta and the abbreviation for it is M. Under MacOs version 10.7 or less, it should be the Command key. If the Alt commands don't work properly from a Mac, click on the X application in the dashboard, go to the X11 menu at the top, then in Preferences, click on the Input tab, and unselect the Enable key equivalents under X11.

Ex. 1 Introduction to Emacs

This exercise is about using the Emacs editor for common code writing tasks.

Open a terminal in an X Client environment as seen in Lab 2. For this, if you work on a Linux computer, then simply open a terminal. Otherwise on a Windows machine, connect to one of the Linux computers using Xming from putty or from a MacOS machine, use the ssh command with the option -X -Y (or -XY). Open the Emacs editor with the command

emacs &

If you do this from home using putty without Xming, to access the menu you have to use the special key F10. In that case, don't use the & in the command above (or do fg if you've already done it to bring the editor back in focus).

Create a new buffer with the command Ctrl-x Ctrl-f. In the dialog at the bottom of the window, called the minibuffer, enter the name "my_eq.cc".

The same key combination can be used to create new files and to open existing files. The minibuffer should tell you it's a new file.

In the browser click on the following link: eq.cc (http://www.cs.iusb.edu/~danav/teach/c151/eq.cc)

Copy the content of this file from your web browser into the new file you have just created.

For this, from a Mac, select the text in the browser, copy it with Cmd-c, then paste it by holding the option (Alt) key down, then clicking in the Emacs window, or use Paste from the Edit menu. If you are in a Linux environment, select the text in the browser, copy it with Ctrl-c, then paste it in the editor with Ctrl-y. If you are in Windows using putty, select the text and copy it (Ctrl-c), then right-click in the terminal to paste it.

This is a small program that inputs two real numbers a and b, and outputs the solution to

ax + b = 0

which is   x = -b / a .  A special case where a is 0 needs to be considered separately.

Save the file with Ctrl-x Ctrl-s.

Indentation

Notice that the program is badly indented. Let's fix that.

Select the entire code.

If your window interacts with the mouse (on any system but Windows with Putty), the normal click-and-drag should work. If you're in a text-only terminal like putty, you can go to the beginning of the main and set a mark for the beginning of the selected region on this point with Ctrl-space. Then move down to the end of the program (not including the comment). The entire code should be highlighted.

Indent this region using the C++ menu and the item Indent Line or Region inside it.

From a Mac or Linux environment, the menus react to the mouse click. If you work in a putty terminal, you can do this by pressing the function key F10, followed by a lowercase c to select the C++ menu, and then the uppercase I to select the option Indent Line or Region. Your program should now be correctly indented.

Copy-Paste

Select the code from line 24 (if (a... ) to line 33 (including 33).

You can use the same method as before for the selection process.

Copy this region with Alt-w. Paste it in the code after line 33 (before return 0) with Ctrl-y twice.

We are going to use the pasted part for editing, but let's assume that you pasted it twice by mistake and not because the teacher told you to.

Undo the second paste operation with Ctrl-_ (Ctrl shift minus).

You should still have one copy of the code. We're going to modify it now.

In the new code that you added replace (manually) all occurrences of the variable a with the variable b and the other way around.

This should compute the solution for

bx + a = 0

Compilation

This editor lets you run compilation commands directly from it and makes the results easily accessible in an additional buffer.

Compile the file from Emacs the following way: run the command compile by typing Alt-x then typing compile when prompted at the bottom of the window (in the minibuffer) where the M-x is displayed, and then pressing return.

The editor will ask for a compiling command. By default it will propose "make -k".

Delete the current compilation command and replace it with
g++ my_eq.cc

Emacs will remember it for the next time you're trying to compile, but not if you quit the editor and start again.

The program will not compile because there are some compiling errors. Let's fix this too.

Hide the compilation results by typing Ctrl-x 1.

This removed the dual-buffer display, but the information is still available as the buffer "*compilation*". You can find it again from the Buffers menu if you want to refer to it.

Go to line 18 by pressing Alt-g g, and then the line number. Replace the COUT symbol on this line with cout.

Search - Replace

Note that there might be other occurrences of COUT in the file. Let's make sure that we replace all of them.

From the Edit menu, choose Replace, then Replace String... (or Alt-%).

The minibuffer should say "Query replace: ".

Enter the misspelled COUT in the minibuffer, then Return.

It should now say "Query replace COUT with: ".

Enter cout.

The editor will highlight every entry of COUT one by one. To replace an entry you must type the spacebar or y, and to skip an entry you should enter n.

After replacing 2 of them, type ! to replace all of the remaining occurrences of COUT. Save this file.

Let's check if the file compiles now.

Compile the file again the same way.

The Compile command can also be found in the Tools menu. This time there should be no error and the result will be the file called a.out.

Verify in the terminal that this file was created.

You can try running it with ./a.out or simply a.out if you edited the .bashrc file correctly in Lab 2.

Execution

We will now use redirection to input the data for executing the program we created from a file.

Create a simple text file called "eqin" (Ctrl-x f to create a new file). In this file write two numbers for the variables a and b, each on a line. Save the file and close it (Ctrl-x k then return). Run the program by invoking the shell command execution first (Tools menu -> Shell Command or Alt-!) and then typing
a.out < eqin

The editor should display the result of the program execution.

Close that window with Ctrl-x 1.

To delete an entire region in Emacs, you can select it like before and type Ctrl-w. You can also use Ctrl-k to erase one line at a time. In both cases, the text you deleted is in fact in the kill ring, which is Emacs's version of the clipboard. Ctrl-y can be used to paste it.

In the file "my_eq.cc", delete the last comment containing the output of the program. Replace the name at the top of the file with your name. Save this file.

Spell checking.

Let's fix the spelling errors in the comment at the top of the file, but not in the code.

Select (highlight) the entire comment at the beginning of the program. From the menu Tools choose Spell Checking, then Spell Check Region.

Do not correct the name of the course or my name (space to leave unchanged). To correct a misspelled word, you have to look at the option offered by the spelling checker at the top of the window, and then hit the key corresponding to the one you think is the most appropriate (it normally starts with the digits). For example, for the word "computs" there are 7 alternatives being offered. The first one, "(0) computes" is the right one, so you have to type 0 to select it.

Correct all of the other words that are misspelled.

Word Wrapping.

The long sentence in the comment doesn't look good because it's so long. Emacs lets you automatically cut long lines into smaller ones, of about 70 characters, removes extra spaces, and rearranges the text around to have as much text on each line. This is done by the wrapping operation.

Place the cursor anywhere on the long line in the comment at the top of the program. Wrap this line with Alt-q.

When this operation is done on a single-line comment (starting with //, each new line will start with // and will be aligned with the first comment.

Save this file.

You will have to upload it to Canvas.

Ex. 2 Other Features

In this exercise we'll see some other useful features of Emacs. You can explore more of them on your own later.

Create a new file called lab4.txt.

You'll have to copy some things to it.

Display the named colors available for Linux using the Edit menu and Text Properties menu item, and copy some of them to the file lab4.txt.

For this, from the Edit menu, you can select Text properties, then Display Colors. The names that you see can be used in some options for Linux commands, such as we will see below. The codes starting with a # character followed by 6 digits are hexadecimal codes for colors that can be used in HTML as well as many other places.

Select a page or two of this list and copy it to the file lab4.txt.

The select and copy operations work like for any other buffer. Ctrl-b lets you switch between buffers if you're in a text-only environment.

Choose two of them that you like, then go back to the terminal (or hit Ctrl-z from putty) and run another Emacs editor, but this time with the options

emacs -bg color1 -fg color2

where in place of color1 and color2 you use the names of the colors you chose.

For example, if I chose the color1 to be RoyalBlue3 and the color2 to be SlateGray1, I would run the command the following way:

emacs -bg RoyalBlue3 -fg SlateGray1

Note the difference between the two Emacs windows.

Open the file "my_eq.cc" in the second window with the command Ctrl-x Ctrl-f to see how the -fg option (foreground color) has affected the way the text is displayed. You can close this second Emacs window now.

If you stopped the other Emacs window with Ctrl-z, you can go back to it with the command fg.

Copy the command that you have executed to open the second Emacs window and paste it the file lab4.txt.

This file should be the active buffer in the first Emacs window.

From the Tools menu select Calendar. From the Holidays menu, display the holidays for the window (which should be Dec-Feb), then copy them to the file lab4.txt.

Since you need to go back and forth between buffers, remember that Ctrl-b will let you switch between buffers in Emacs, or simply the Buffers menu.

After exiting Emacs, create a folder under c151 called week4. Move the files my_eq.cc and lab4.txt into this folder.

These are the result of the lab that you need to submit to Canvas.

Canvas Upload.

Upload the files lab4.txt and my_eq.cc to Canvas, Assignments, Lab 4. If you worked remotely, then you may have to transfer the file to your local computer first by sftp (see How To SFTP).