home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-08-30 | 6.4 KB | 159 lines | [TEXT/KEEN] |
- EnterAct's magic clipboard
- ------------------------
-
- Prerequisites: you should see the "hAWK User’s Manual" and read
- enough to know how to run hAWK programs before diving in here.
- However, no harm in browsing....
-
- Added for version 3 of EnterAct, you can now write hAWK
- programs that watch EnterAct's clipboard, and, when your
- specified conditions are met, write anything you like back to the
- clipboard. This gives you in effect a "magic clipboard", ideal for
- reducing repetitive, moderately complicated tasks to a simple
- Copy and Paste.
-
- When you're done with your hAWK "magic clipboard" program,
- you can stop it with <Command><period>.
-
- Example programs, in the "hAWK programs" folder:
- "$ClipGlossary" lets you remember and expand glossary abbreviations.
- "$ClipOldCDeclToNew" converts old C declarations to new ANSI style.
- "$ClipMagicTemplate" is a stripped-down simple example to build on,
- talked about below.
- "$ClipFormatFuncIntro" will reformat a messy function start into
- something elegant (instructions are at the beginning of its file).
-
- § How it works
- By default, any hAWK program you start up while using EnterAct
- will run concurrently with EnterAct. Big hAWK programs can take
- a while, so this allows you to continue working with EnterAct
- while hAWK chugs away, for the most part unobtrusively, in
- the background. Most of the time, you'll run hAWK programs that
- do one specific job and then quit -- but what if it just kept
- running and running and running?
-
- hAWK has two special built-in functions, getclip() and putclip(),
- that allow it to monitor and change EnterAct's private clipboard.
- Armed with these, you can write a hAWK program that runs
- "forever", and does specific things to the clipboard under
- specific circumstances. What all those specifics are is up to you.
-
- § An example
- Since an ounce of code is often worth a pound of explanation,
- here's an example of a loop you can use to monitor the clipboard
- and trigger action under specific circumstances:
-
- #$ClipMagicTemplate: you can use this as the basis for your
- # own clipboard changers. This example just truncates the
- # clip to its last line, provided it starts with a space.
- BEGIN {
- clipCharsToWatch = 32;
- while (1) # run until <Command><period>...
- {
- # see if clipboard has changed
- if ((newClip = getclip(clipCharsToWatch)) != oldClip)
- {
- oldClip = newClip;
- # a leading space is used as the trigger
- if (substr(newClip, 1,1) == " ")
- DoSomething()
- }
- }
- }
-
- function DoSomething( fullClip, numLines, lines, outClip, out)
- {
- fullClip = getclip(); # gets calling editor's private clip
- numLines = split(fullClip, lines, "\r");
- # with "split", the last line will be empty if the last
- # character copied was a return.
- if (lines[numLines] == "")
- --numLines;
-
- #...do things with the clipboard, and create the new clip "out"
- ### just for example, keep only the last line of the old clip
- out = out lines[numLines] "\r";
-
- # In this specific case, since we are looking for a space at
- # the beginning of the clip, make sure our new clip does NOT
- # start with a space
- sub(/^[ ]+/, "", out); # that's a [space] there
-
- # send the result back to the calling editor's clip
- putclip(out);
- # update "oldClip"
- oldClip = substr(out, 1, 32);
- # flash menu bar to signal something happened
- beep(0);
- }
- (you'll also find the above program in your "hAWK programs" folder)
-
- The monitoring all takes place in your BEGIN section (or, if you
- prefer, in your END section after reading some input). Running
- forever, we look at just the first 32 characters of the clipboard
- to see if the clip has changed. If the clip has changed, and the first
- character on it is a space, we call the function DoSomething(), which
- retrieves the entire clipboard to "fullClip", breaks it into separate
- lines in the array "lines", creates a new clipboard string in "out",
- writes it back to EnterAct with "putclip()", and then flashes the
- menu bar to show something happened as a result of the Copy in
- EnterAct.
-
- The line
- if ((newClip = getclip(clipCharsToWatch)) != oldClip)
- determines if the first 32 characters of the clipboard have changed
- since the last time the clipboard was looked at. If so, the specific
- trigger for doing something is
- if (substr(newClip, 1,1) == " ")
- DoSomething()
- which simply checks to see if the latest clipboard starts with a space.
-
- "DoSomething()" retrieves the full clipboard (up to a limit of 32K
- actually) and then breaks it into lines, after which the clip is at
- its mercy. A new clip string is created in "out", and written back to
- EnterAct with
- putclip(out);
- The "beep(0)" will signal you that this hAWK program has been
- triggered to rewrite the clipboard.
-
- With the above program running, if you copied several lines of text
- by picking Copy in Enteract, and the first line began with a space,
- then you would see the menu bar flash. If you then did a Paste, the
- pasted text would turn out to be only the last line you just copied
- (minus any leading spaces). If you copied text that did not begin
- with a space then the menu bar would not flash and your clip would
- be unchanged.
-
- § Other examples
- The program "$ClipOldCDeclToNew" is a very real example that converts
- a specific style of "old-fashioned" C function declaration to the new
- ANSI prototype style. It looks for a very specific pattern in the
- declaration, but you could adapt it to be more general. It saved me
- hours of tedious work.
-
- "$ClipGlossary" lets you remember and expand glossary abbreviations.
- Both making an entry in your glossary and expanding a glossary
- entry work by Copy and Paste: to make an entry, you would Copy
- text in the format gloss abbreviation full text, as in
- gloss forloop for (i = 0; i <= ??; ++i)
- {
-
- }
- --the "gloss" part serves a signal that you are making an entry,
- and the menu bar will flash to show that the entry has been made.
- To expand this entry, you would type "forloop" and then Copy it
- and Paste right over it (after the menu bar flashes) to produce
- for (i = 0; i <= ??; ++i)
- {
-
- }
-
- § (?)
- If the above didn't make much sense yet, but you really would like a
- magic clipboard, just start in on the "hAWK User's Manual" and you'll
- be writing your own hAWK programs in short order. hAWK is a
- Mac version of AWK, an interpreted language that's like C but with
- many built-in functions for text and data manipulation. As you've
- seen, running a hAWK program is pretty easy: writing them isn't that
- hard either, especially if you know C (a good bet).
-