Chapter 3: Advanced Use

Organization of This Chapter

This chapter takes you through an advanced Ce edit session. We encourage you to follow the examples by using Ce while you read this chapter.

This chapter describes the following:

All of the information is also available on-line in the various Ce help files and HTML documentation. In many cases the online documentation is more complete with more examples than this users guide.

Regular Expressions

Regular expressions (also known as "patterns") are used to find patterns of characters in a text region. The forward search, backward search, substitute, and substitute once commands all exploit regular expression functionality for performing sophisticated text searches and substitutions.

Ce supports the use of both AEGIS regular expressions and UNIX expressions (per the UNIX "ed" command). Only one style prevails per edit session, and that style can be explicitly set by either the the "-expr" command line option or the "Ce.expr" X resource. The default regular expression mode is AEGIS mode.

AEGIS regular expressions and UNIX regular expressions have much in common. Nonetheless the following descriptions treat the styles separately. This chapter does conclude with a table summarizing the similarities between the two styles.

AEGIS Regular Expressions

% Matches the beginning-of-line, but only if it is the first character in the pattern; otherwise it is interpreted literally.

$ Matches the end-of-line, but only if it is the last character in the pattern; otherwise it is interpreted literally.

? Matches any single character except newline.

* Matches zero or more occurrences of the preceding character in the pattern, but only if this is NOT the first character in the expression; otherwise it is interpreted literally.

@f Matches a form feed character (hex 0C).

@n Matches a newline character (hex 0A).

@t Matches a tab character (hex 09).

@c Matches the literal character "c". This is used primarily to match a character that normally serves as a special regular expression character. For example, "@?" will match a literal "?".

[string] Matches a single character that appears in "string". For example, "[abyz]" will match the first occurrence of either an "a", "b", "y", or "z". "string" may also indicate ranges of characters, so that "[a-cx-z4-6]" would match any one of "a", "6", "c", "x", "y", "z", "4", "5", or "6".

[~string] Matches a single character that does not appear in "string". If "~" is not the first character following "[", it is interpreted literally. The same rules regarding ranges of characters that are defined in "[string]" also apply here.

{pat} The curly brackets are used to "tag" an expression so that a string that matches the given pattern can be referenced in a substitution operation. For example, the substitution command "s/{[abc]}?/+@1+/" means that for every occurrence of an "a", "b", or "c" (i.e., "{[abc]}"), replace the occurrence with a "+" followed by the "a", "b", or "c" (i.e., "@1") followed by another "+".

All other characters appearing in AEGIS regular expressions are interpreted literally.

UNIX Regular Expressions

^ Matches the beginning-of-line, but only if it is the first character in the pattern; otherwise it is interpreted literally.

$ Matches the end-of-line, but only if it is the last character in the pattern; otherwise it is interpreted literally.

. (period)
Matches any single character except newline.

* Matches zero or more occurrences of the preceding character in the pattern, but only if this is NOT the first character in the expression; otherwise it is interpreted literally.

\c Matches the literal character "c". This is used primarily to match a character that normally serves as a special regular expression character. For example, "\." will match a literal ".".

[string] Matches a single character that appears in "string". For example, "[abyz]" will match the first occurrence of either an "a", "b", "y", or "z". "string" may also indicate ranges of characters, so that "[a-cx-z4-6]" would match any one of "a", "b", "c", "x", "y", "z", "4", "5", or "6". To include a literal "~", "]", "-", or "\" in "string", precede the character with "\". If "]" is the first character in "string", it is interpreted literally and not as the pattern's terminating `]'.

[^string] Matches a single character that does not appear in "string". If "^" is not the first character following "[", it is interpreted literally. The same rules regarding ranges of characters that are defined in "[string]" also apply here.

c \ { m \ } Matches a string containing exactly "m" occurrences of the character "c", where "c" is either a literal character or a single character pattern (such as `.'). Similarly, "c \ { m , \ }" matches at least "m" characters, and "c \ { m , n \ }" matches between "m" and "n" characters. "m" and "n" must both be integers between 0 and 256. In this form of regular expression, the curly brackets and backslashes "\ { ...\ }" are required.

\(pat\) The escaped parens are used to "tag" an expression so that a string that matches the given pattern can be referenced in a substitution operation. For example, the substitution command "s/\([abc]\)./+\1+/" means that for every occurrence of an "a", "b", or "c" (i.e., "\c[abc]\"), replace the occurrence with a "+" followed by the "a", "b", or "c" (i.e., "\1") followed by another "+".

All other characters appearing in UNIX regular expressions are interpreted literally.

Table 3-1. Similarities Between AEGIS and UNIX Regular Expressions

Meaning

AEGIS

UNIX

Beginning-of-line

%

^

End-of-line

$

$

Single character

?

.

Zero or more occurrences of the previous character in the pattern

*

*

Escape character to force literal interpretation

@

\

One character in a set

[string]

[string]

One character not in a set

[~string]

[^string]

Regular Expression Examples

Using regular expressions is a complex art and the subject of several text books. Following are some examples demonstrating various regular expressions in the context of find and substitute commands. Note these examples use UNIX regular expression mode.

kd Left \.\ ke
This key definition defines the left arrow key as searching backwards for any character. The backslashes indicate reverse search. The "." matches any character. Note that this definition creates a Left arrow key which jumps to the previous line when it gets to column one on a line.

1,$s/^ *//
Remove leading blanks from all lines in the file. The "1,$" is the range of lines. The "^" locks the pattern to the beginning of the line. "*" specifies zero or more of the preceding characters which is a blank. The replace string in the substitute is empty.

1,$s/ *$//
Remove trailing blanks from all lines in the file. The "1,$" is the range of lines. The "$" specifies that the blanks must be the last thing on the line. There are two spaces and a "*". The first space specifies that there must be one blank to match. The second space, followed by the "*" matches zero or more additional blanks.

s/ *\[/='%./
Over the following data:
printf("loadid [8]; /* c */
printf("module [31]; /* c */
" *\[" matches the blanks following "loadid" and "module" and the open square bracket. Note that the square bracket is escaped with a backslash to prevent it's being interpreted as a range of characters. The replacement characters "='%." are just literal characters.

1,$s/^/echo /
Put the word echo at the front of each line in a file. The "1,$" is the range of lines. The "^" matches zero characters at the beginning of the line. The word "echo" followed by a blank is placed at the front of each line. The beginning of line cannot actually go away. Using "F1" to mark a range of text, you can use this technique to shift code right by a fixed number of spaces when the replace string is blanks.

/[^-a-zA-Z._@$0-9\\/\\\~]/
Part of the click on a file key definition (m3), this regular express searches forward for the first character which is not part of a word. Broken down:

Character

Definition

/

Do a forward search.

[

Match on any of the following characters.

^

Invert the match, match on any but the following characters.

-

Match on a dash, must be first to avoid being taken as part of a range of characters.

a-z

Match any lower case alphabetic.

A-Z

Match any upper case alphabetic.

._@$

Match any of these literal characters.

0-9

Match any numeric.

\\/

Match forward slash, extra escape characters are due to this being in a key definition. The keydef parsing eats one backslash.

\\\

Match a backward slash, again the extra escape characters are due to this being a key definition.

~

Match the tilde character.

]

End of the list of characters to not match.

/^.\{9,19\}abc/
Find the string "abc" in positions 9 through 19. The "^" locks the pattern to the beginning of the line. The ".\{9,19\}" matches any character "." 9 to 19 times. The "abc" matches the string abc.

s/\(.*\)/ case "\1"/
Take the rest of the line and put it in a "case" statement. The "\(" and "\)" delimit the area to assign to the internal "\1" variable. The "\1" in the replacement string shows where to put the saved part of the search expression.

Copy/Cut/Paste Operations

Chapter 1, Basics includes a section on the basic use of copy, cut, and paste operations. This section presents additional detail on the following:

These sections assume that you know how to define a text region using the "Mark" keys (F1 and SHIFT/F1) and the cursor.

The Default Paste Buffer

Chapter 1, Basics described how Ce needs a default paste buffer to store information after it is copied or cut. That default paste buffer takes its name from the Ce resource dfltPasteBuf X Resource or the Ce default of "CLIPBOARD". If you exit the Ce session, the data is stored in a file in your paste buffer directory (Ce.pasteBuffDir). The file contains the data that was in the paste buffer of the Ce session which owned the paste buffer when it was shutdown. This is how a Ce session can paste data copied in a Ce window which was closed.

When a copy operation occurs, Ce contacts the X server and requests ownership of a named entity called a selection. It remembers internally what was copied and when a paste occurs, the paster requests that the contents of the named selection be attached to its window where it can be accessed. The X server forwards the request to the selection owner who then attaches the data to the paster's window. This is standard X copy/paste, processing.

The primary limitation of the default paste buffer is that it can contain data from only a single copy or cut operation. A copy or cut to the default paste buffer always replaces any existing data in the buffer.

Named Paste Buffers

Named paste buffers behave in the same way as the default paste buffer mentioned above. That is, such paste buffers

The advantage of named paste buffers is that you can do multiple consecutive copies and cuts, and have the data for each of those operations saved in distinct paste buffers. Subsequent paste operations can then address each of those paste buffers individually.

File Paste Buffers

There may be times at which you would like to copy or cut data from a file for use in non X based applications. To accommodate this need, Ce provides file paste buffers, that:

A file paste buffer is simply an ASCII disk file, with no other special attributes that directly relate it to Ce. In fact, you can paste data from any ASCII file into a Ce window if you treat that file as a file paste buffer when using the "xp" (paste) command. For example, "xp-f~/.profile" will paste your profile into the current Ce session.

Table 3-2. Paste Buffer Summary

Operation

Commands

Default Key

Copy data

to default pb

dr ; echo ; xc

F1, F2

rectangular to default pb

dr ; echo -r ; xc -r

<Shift> F1, <Shift> F2

to named pb

dr ; echo ; xc buf

rectangular to named pb

dr ; echo -r ; xc -r buf

to file pb

dr ; echo ; xc -f file

rectangular to file pb

dr ; echo -r ; xc -r -f file

Cut data

to default pb

dr ; echo ; xd

F1, F3

rectangular to default pb

dr ; echo -r ; xd -r

<Shift> F1, < Shift> F3

to named pb

dr ; echo ; xd buf

rectangular to named pb

dr ; echo -r ; xd -r buf

to file pb

dr ; echo ; xd -f file

rectangular to file pb

dr ; echo -r ; xd -r -f file

Paste data

from default pb

xp

F4

rectangular from default pb

xp -r

<Shift> F4

from named pb

xp buf

rectangular from named pb

xp -r buf

from file pb

xp -f file

rectangular from file pb

xp -r -f file

A Special Case of Rectangular Copying and Cutting

There exists one special case in which a "rectangular region" is not actually rectangular. When you initiate a rectangular region highlight via "<Shift> F1" ("dr ; echo -r") and move the cursor to the end of the region, the highlighted area will be rectangular unless the cursor is moved to a character that is in the same column as the initially marked point. In that special case, the highlighted area includes all text in and to the right of the column defined by the initial and current cursor positions. Furthermore, lines cut or copied from such a "rectangular region" are not padded with blanks in the paste buffer to make the lines of equal length. Since the marked rectangle ends at the left edge of the text cursor, this special case corresponds to what would be a zero width copy/cut; thus no functionality is lost by the syntax.

Command Scripts

Ce provides a way to execute a file that contains one or more Ce commands. Such a construct is useful for repetitive command sequences that are too complex to assign directly to keys or too lengthy to enter each time they are to be executed.

You execute such files via the "cmdf" command, whose syntax is "cmdf[-p]file ". "file" is an ASCII file containing a series of Ce commands. If you want to execute the commands that reside in a paste buffer, you must use the "-p" option to tell Ce to look in a paste buffer instead of a normal file.

Escape to Shell

Ce provides the ability to execute a shell or a program, and to provide to that program, text from the file you're editing as stdin. Stdout from the shell can be automatically or manually pasted into the edit session or interpreted as a command script. See the online help on the topic "bang" for the syntactic and semantic details.

File Saving and Backup Modes

There are a number of commands, environment options, and default key definitions that are related to

Each of these operations is described in the following paragraphs. This section concludes with a table summarizing the above operations.

Saving a File to Disk

Press the "F8" key, which executes the command sequence "pw ; wc". This sequence is actually two commands. The first, "pw", is what causes the file to be updated to disk. The second, "wc", is what causes the window to close. You will normally use the "F8" key to terminate an edit session.

Press "<Shift> F8", which executes the command sequence "pw ; ro". This sequence, also consisting of two commands, causes the file to be updated to disk ("pw") and the window to be put into read-only mode ("ro"). This is useful when you are finished editing a file, but would like a window to remain open on it for subsequent browsing. When the window is in read-only mode, an "R" inside a small box will be present in the window's title bar.

Press "<Ctrl> w", which executes the command "pw" by itself. Doing this will cause the file to be updated to disk, but the edit window will remain open and receptive to subsequent edit commands. It is wise to execute this operation periodically during an edit session to minimize the risk of data loss due to a system or power failure.

Set your autosave option (either via the "-autosave" command line option or the "Ce.autosave" X resource). The option takes as an argument a positive integer n; when active, Ce will automatically update the edit file to disk (effectively executing the "pw" command) every n keystrokes.

Closing an Edit Window

If you want to close an edit window and save the changes made to the file, use the "F8" key, which is assigned the command sequence "pw ; wc".

If you want to close an edit window without saving changes that have been made to the file since it was last written to disk, press "<Ctrl> F8", which executes the "wc" command ("window close"). Ce will ask you if you really want to abort the changes. You must respond affirmatively to really terminate the edit session without saving the changes.

If the window is in read-only mode, the "pw" command, which is what causes the file to be updated to disk, has no effect. Therefore, to close a read-only window, you can press "F8" or "<Ctrl> F8". The key sequence "<Ctrl> y" and "<Ctrl> m" are equivalent to "F8" and "<Ctrl> F8" respectively.

Toggling between Read-only and Edit Modes

Press "<Shift> F8" to change a Ce window's mode from edit to read-only. "<Shift> F8" is assigned the command sequence "pw ; ro". This means that before changing to read-only mode ("ro"), the file will be updated to disk ("pw").

Press "<Ctrl> m" to change a Ce window's mode from read-only to edit. The "R" in the window's upper border will disappear, indicating that the window is now in edit mode.

You can also use "<Ctrl> m" to change from edit mode to read-only mode if no changes have been made to the file.

Setting the Backup Mode and Creating a Backup File

Since there may be times at which you will want to revert to a previous version of a file, Ce can automatically create a backup file before saving a changed version.

As mentioned in the Section, Saving a File to Disk, there are a number of events that can take place that cause a file to be updated to disk. In fact, such events can occur multiple times during a single edit session. A backup file is created when such an event occurs for the first time during an edit session.

A backup file is given the same name as the original file, except that the suffix ".bak" is added to the backup file's name. If Ce attempts to create a ".bak" file and a file with that name already exists, the existing version is overwritten. (This should not be a problem unless files with names ending in ".bak" are being generated by some other application.)

Ce will always create a backup file unless you use the "-bkuptype none" command line option or the "Ce.bkuptype: none" X resource. Backup files can be created using one of two modes:

The default backup mode is "dm". Use the "-bkuptype" command line option or the "Ce.bkuptype" X resource to change the mode to "vi" or "none".

Renaming an Edit File During an Edit Session

Ce allows you to rename a file even while it is being edited. This is accomplished via the "pn" command, whose syntax is "pn [-c] pathname". Specifically, "pn" can be used in one of two ways:

One special feature of Ce is that it allows you to edit "standard input". For example, you can pipe output from one command to Ce's input, e.g. "% ls -al | ce". This is a special use of Ce in which the file being edited has no associated disk file. In order to save such a file, you must use the "pn" command to assign a pathname to the file being edited.

Table 3-3. Summary of File Saving and Backup Mode Operations

Operation

Command or Option

Update a file to disk and close it

F8 ("pw ; wc")

Update a file to disk and put it in read-only mode

<Shift> F8 ("pw ; ro")

Update a file to disk and leave it in edit mode

<Ctrl> w ("pw")

Abort an edit session

<Ctrl> F8 ("wc")

Changing a window's mode from read-only to edit

<Ctrl> m ("ro")

Changing a window's mode from edit to read-only

<Ctrl> m ("ro")

Enabling autosave mode

"-autosave n" or "Ce.autosave: n"

Enabling "dm" backup mode

"-bkuptype dm" or "Ce.bkuptype: dm"

Enabling "vi" backup mode

"-bkuptype vi" or "Ce.bkuptype: vi"

Disabling backup mode

"-bkuptype none" or "Ce.bkuptype: none"

Tab and Form Feed Character Processing

For the most part, Ce allows the editing of files that contain only ASCII characters. The sensible exceptions are the newline character (hex 0A), the tab character (hex 09), and the form feed character (hex 0C). In truth, Ce allows all character values except x' 00'. Adding key definitions utilizing the "er" ( enter raw) command allow you to type these characters.

Newline characters are processed in one and only one way: they determine where line breaks occur in the text. Newline characters only occur at the end of text lines; the last line of a file is the only line that might not contain a terminating newline character.

Unlike newline characters, tab and form feed characters require special processing. Each of these special characters is described below.

Tab Characters

Within a file, a tab character is represented as a single character. When such a character is displayed in the edit window, however, it is expanded to appear as a string of blanks so that text following the tab character starts at the next tab stop.

The expansion is determined solely by the tab stop settings. If your file contains tab characters and you change the tab stop settings during an edit session, lines containing tab characters will be automatically adjusted to the new tab settings.

There are three default key definitions related to tab processing:

There may be times at which you do not want tabs to be expanded as explained above, but would rather have tabs displayed as themselves. Use the "-hex yes" command line option or the hex Ce command to disable tab character processing and view unprintable characters. (As noted below, if tab processing is disabled in this way, form feed character processing is also disabled.)

Form Feed Characters

If tab processing is enabled (the default), form feed character processing is also enabled. A form feed forces the line on which it occurs to be displayed as the first line in the window, having the effect of starting a new page.

If a file contains multiple form feed characters, only one such character can occupy the first line and column of the window at one time. The only way in which the current form feed character will give up its position is if it is scrolled out of the window. When that happens, if text that is scrolled into the window contains a form feed character, the line that contains it is forced to the top of the window.

The extreme example of this is a file in which every line contains a form feed character, for such a file can be viewed only a single line at a time.

Editing Non-Ascii Characters

As mentioned before, Ce can process all characters except x' 00'. Most keyboards, however, have no way of typing non-ascii characters. Some keyboards use control sequences to enter non-ascii characters such as the special characters used in European alphabets. The isolatin option is used to prevent these sequences from being filtered out. Other non-ascii characters are just not on the keyboard. To type these characters requires setting up your own key definitions.

For example, suppose you want to be able to type the character x' 02' and want to use the key sequence "<Ctrl> 2" to enter it. You would execute the key definition "kd ^2 er 02 ke".

Character

Definition

kd

The Ce key definition command

^2

Key specification for the key2 with the <Ctrl> modifier

er

The Ce enter raw command to be assigned to this key sequence

02

The parameter to er, the hex value to enter

ke

End of the key definition marker

Editing the same document on multiple workstations

Often times it is convenient for more than one person to edit a file at one time. Having a friend proof read code or a memo without printing out hard copy, having it marked up, and then entering the changes cuts out several steps in the process. It is especially convenient if the other person is physically close so you can talk while you type.

ARPUS/Ce provides the "cc" command to facilitate this type of operation. The "cc" command takes most of the arguments available to ce and cv. This includes the "-display" option. Without the display option, the "cc" command creates an additional window on your workstation for the current file being edited. This allows two or more views into the file. For example, it may be nice to look at data declarations at the top of a file at the same time as working on code near the bottom. Ce theoretically supports any number of concurrent windows, but you will run out of real estate on the screen after a few.

If the "-display node:0" option is used on a "cc" command, the view into the file will be opened on the workstation specified by "node". The user of that workstation must allow the current workstation to open windows on it's display with the UNIX "xhost" command. If the the other nodes display has not been so unlocked, the results vary. On most systems, an error message specifying that the connection has been rejected will will be generated. On some systems, the window will hang for several minutes until a timeout message is generated. This is a function of the X server implementation on the target node.

Once a window has been opened on the other display, the other user is in edit on the same memory image of the file. Either user can save the file, make changes, etc. If both users are viewing the same lines in the file they can see what the other user is typing. (This is amusing the first time you see it.) One restriction is that both users cannot type on the same line at the same time. Each user uses the key definitions and X defaults associated with the node they are on. Thus one user could be using Aegis regular expressions and one standard UNIX regular expressions. This functionality can extend to multiple workstations. In theory any number is possible, but it has not been tried with more than 10 workstations.

The "cc" command also works with a ceterm. In this case only the transcript pad is displayed in the cc window along with a Ce "Command:" window and an output message window. This can be useful when providing support to a Ce user who is having some problem and has called on the phone. Open the current workstation to allow windows to be opened from the other workstation and have them do a "cc -display yournode:0" and a copy of their transcript pad will be placed on your workstation. You can then see the commands as they execute them. This really helps when the user types "ls-la" instead of "ls -la".

WARNING: When you open a window on another node, the other user is editing the file as you on your workstation . They can execute the "ce", "cv", "!", "ceterm", "cpo", etc. commands and create other processes running as you on your workstation. In some environments, this is not an issue. In others it is an issue. Use due caution. If you are running as root, you should probably not open windows on other peoples displays.

Record / Playback Modes - Learn Mode

ARPUS/Ce supports recording keystrokes (and mouse movement) and later playing them back as a command file. The actual keystrokes are not recorded, but instead the Ce commands stored under them are played back. Some editors refer to this as learn mode. While the default pulldown menus allow access to record and playback, most users choose to put them under key definitions.

For this example, let us assume that you have executed the UNIX command, "xmodmap -e "remove mod3 = Num_Lock" in your ".vueprofile (HP/VUE)", ".dtprofile (CDE"), or ".xinitrc" (others) file. This disables the shift-like modifier function of this key and free's it up for use as a normal key. We will use the "Num_Lock" key to turn on record mode, the key next to it (key pad divide) to turn it off, and the next key (key pad multiply) to do a playback. Many people find this arrangement convenient, but it is impractical to ship Ce with this as the default.

We now place the following key definitions into the .Cekeys file and run 'ce -reload' from the UNIX command line.

kd Num_Lock

rec -p cemacro ke

# macro record on

kd KP_Divide

rec ke

# macro record off

kd KP_Multiply

cmdf -p cemacro ke

# macro play

Suppose we want to insert a "/*" and three blanks at the beginning of each of a selected set of lines and three blanks and a "*/" at the end.

  1. Position the cursor at the beginning of the first line to operate on.
  2. Press Num_Lock to turn on recording.
  3. Press Home to go to the beginning of the line.
  4. Type "/* ".
  5. Press the 'End' key to go to the end of the line
  6. Type " */".
  7. Press the 'Home' key to go back to the beginning of the line.
  8. Press arrow down to go to the next line. Note it may be useful to have arrow down defined as kd Down [+1,] ke, so the window will scroll when you get to the bottom of the page.
  9. Press the key pad divide (/) key to turn off recording.
  10. Press key pad multiply (*) to repeat the operation on the next line.

The recorded command sequence will work in any Ce window until it is rerecorded over. You can see the recorded command sequence by pasting the paste buffer used by the record key. You would execute the command "xp cemacro" to perform the paste. You can also record to a real file. See the help on "rec" for more information.