home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vile-src.zip / vile-8.1 / doc / macros.doc < prev    next >
Text File  |  1998-07-01  |  21KB  |  589 lines

  1.  
  2.  
  3. Programmed Macros in vile
  4. ==========================
  5.  
  6.     [ The following documentation is copied almost verbatim from
  7.     chapter 13 of "MicroEMACS Full Screen Text Editor Reference Manual,
  8.     Version 3.10 March 19, 1989" The copyright for that document is
  9.     "(C)opyright 1988, 1989 by Brian Straight and Daniel M. Lawrence". 
  10.     I have done a global substitute of "vile" for "MicroEMACS", and
  11.     adjusted the document in various other small ways to make it match
  12.     vile's current functionality.  (Paul Fox, 4/94) ]
  13.  
  14.     [ Some more examples of vile macros can be found in the file
  15.     vile.hlp.  ]
  16.  
  17.     Macros are programs that are used to customize the editor and to
  18.     perform complicated editing tasks.  They may be stored in files or
  19.     buffers and may be executed using an appropriate command, or bound
  20.     to a particular keystroke.  The execute-macro-<n> commands cause
  21.     the macro, numbered from 1 to 40, to be executed.  The execute-file
  22.     command allows you to execute a macro stored in a disk file, and
  23.     the execute-buffer command allows you to execute a macro stored in
  24.     a buffer.  Macros are stored for easy execution by executing files
  25.     that contain the store-macro command.
  26.  
  27.     If you need more than 40 macros, named macros, called procedures,
  28.     can be used.  The store-procedure command takes a string argument
  29.     which is the name of a procedure to store.  These procedures then
  30.     can be executed with the run command.  Also, giving the name of a
  31.     stored procedure within another macro will execute that named
  32.     procedure as if it had been called up with the run command.
  33.  
  34.     There are many different aspects to this macro language.  Editor
  35.     commands are the various commands that manipulate text, buffers,
  36.     windows, et cetera, within the editor.  Directives are commands
  37.     which control what lines get executed within a macro.  Also there
  38.     are various types of variables.  Environmental variables both
  39.     control and report on different aspects of the editor.  User
  40.     variables hold string values which may be changed and inspected. 
  41.     Buffer variables allow text to be placed into variables. 
  42.     Interactive variable allow the program to prompt the user for
  43.     information.  Functions can be used to manipulate all these
  44.     variables.
  45.  
  46.  
  47.  
  48. Comments
  49. --------
  50.     Any line beginning with a semi-colon (;) or double-quote (")
  51.     character is ignored.  The semi-colon is inherited from MicroEMACS,
  52.     the double-quote is for vi compatibility.
  53.  
  54. Constants
  55. ---------
  56.     All constants and variable contents are stored as strings of
  57.     characters.  Numbers are stored digit by digit as characters.  This
  58.     allows the variables to be "typeless", not having different
  59.     variables types be legal in different contexts.  This has the
  60.     disadvantage of forcing the user to be more careful about the
  61.     context of the statements variables are placed in, but in turn
  62.     gives them more flexibility in where they can place variables. 
  63.     Needless to say, this also allows the expression evaluator to be
  64.     both concise and quick.
  65.  
  66.     Wherever statements need to have arguments, it is legal to place
  67.     constants.  A constant is a double quote character, followed by a
  68.     string of characters, and terminated by another double quote
  69.     character.  To represent various special characters within a
  70.     constant, the backslash (\) character is used.  The character
  71.     following the backslash is interpreted according to the following
  72.     table:
  73.  
  74.     Sequence        Result
  75.     \n              ^J      newline character
  76.     \r              ^M      carriage return
  77.     \\              \       backslash
  78.     \b              ^H      backspace
  79.     \f              ^L      formfeed
  80.     \t              ^I      tab
  81.     \a              ^G      bell
  82.     \s                      space
  83.     \"              "       quote
  84.     \xNN        0xNN    the character in hex
  85.     \NNN        NNN    the character in octal
  86.  
  87.     Any character not in the table which follows a backslash will be
  88.     passed unmodified.  This action is similar to the ^V
  89.     quote-character command available from the keyboard.
  90.  
  91.     The double quotes around constants are not needed if the constant
  92.     contains no internal white space and it also does not happen to
  93.     meet the rules for any other commands, directives, variables, or
  94.     functions.  This is reasonably useful for numeric constants.
  95.  
  96.  
  97. Variables
  98. ---------
  99.     Variables can be used to return values within expressions, as
  100.     repeat counts to editing commands, or as text to be inserted into
  101.     buffers and messages.  The value of these variables is set using
  102.     the set-variable command.  The shorter form, "setv", may also
  103.     always be used.  For example, to set the current column position to
  104.     64, the following macro line would be used:
  105.  
  106.         set-variable curcol 64
  107.  
  108.     or to have the contents of %name inserted at the point in the
  109.     current buffer, the command to use would be:
  110.  
  111.         insert-string %name
  112.  
  113.  
  114.  
  115. Environmental Variables
  116. -----------------------
  117.     These variables are used to change different aspects of the way the
  118.     editor works.  Also they will return the current settings if used
  119.     as part of an expression.  All environmental variable names begin
  120.     with a dollar sign ($) and are in lower case.
  121.  
  122.     $abufname    [READ ONLY] Name of the "other" buffer, the one most
  123.             recently visited.  This is what you would get if you
  124.             typed '#' at a prompt.
  125.  
  126.     $buffer-hook    Name of procedure to run when switching to a buffer.
  127.  
  128.     $cbufname    Name of the current buffer.
  129.  
  130.     $cd-hook    Name of procedure to run when changing directories.
  131.  
  132.     $cfilname    File name of the current buffer.
  133.  
  134.     $char        Ascii value of the character currently at the
  135.             point.
  136.  
  137.     $curcol     Current column of point in current buffer.
  138.  
  139.     $curline    Current line of point in current buffer.
  140.  
  141.     $cwd        [READ ONLY] Current directory.
  142.  
  143.     $cwline     Current display line in current window.
  144.  
  145.     $debug        Flag to trigger macro debugging.
  146.  
  147.     $directory    Controls location of temp files.  unused.
  148.  
  149.     $discmd     Flag to disable the echoing of messages on the
  150.             command line.
  151.  
  152.     $disinp     Flag to disable the echoing of characters
  153.             during command line input.
  154.  
  155.     $exit-hook    Name of procedure to run when quitting.
  156.  
  157.     $flicker    Flicker Flag set to TRUE if IBM CGA set to
  158.             FALSE for most others.
  159.  
  160.     $font        under X11, contains the name of the current font.
  161.  
  162.     $iconname    under X11, contains current icon name.
  163.  
  164.     $identifier    the name of the current "identifier-like" word under
  165.             the cursor.
  166.  
  167.     $kill        This contains the first 127 characters currently
  168.             in the kill buffer and can be used to set the
  169.             contents of the kill buffer.
  170.  
  171.     $lastkey    [READ ONLY] Last keyboard character typed.
  172.  
  173.     $line        The current line in the current buffer can be
  174.             retrieved and set with this environment variable.
  175.  
  176.     $llength    [READ ONLY] Returns the number of characters in
  177.             the current line.
  178.  
  179.     $match        [READ ONLY] Last string matched in a search.
  180.  
  181.     $mode        [READ ONLY] "insert" or "command" mode.
  182.  
  183.     $ocwd        [READ ONLY] Previous directory.
  184.  
  185.     $os        [READ ONLY] Operating system for which was vile was
  186.             built.  Currently "unix", "dos", "vms", "os/2".
  187.  
  188.     $pagelen    Number of screen lines used currently.
  189.  
  190.     $pagewid    Number of screen columns used currently.
  191.  
  192.     $palette    string used to control the color mappings in some
  193.             versions.  The usual form consists of digits setting
  194.             the red, green, and blue levels.
  195.  
  196.     $pathname    [READ ONLY] current "path-like" word, under the cursor.
  197.  
  198.     $pending    [READ ONLY] Flag to determine if there are user
  199.             keystrokes waiting to be processed.
  200.  
  201.     $progname    [READ ONLY] Contains the string "vile" "xvile", or,
  202.             "winvile" as appropriate.
  203.  
  204.     $qidentifier    the name of the current "qualified-identifier-like"
  205.             word under the cursor, useful for C++ programmers.
  206.  
  207.     $replace    Current default replace string.
  208.  
  209.     $search     Current default search string.
  210.  
  211.     $seed        Integer seed of the random number generator.
  212.  
  213.     $shell        Name of the shell program for spawned commands.
  214.  
  215.     $sres        Current  screen resolution on a PC.  Values:
  216.               "2",       "25",    "80x25",
  217.               "4",       "43",    "80x43",
  218.               "5",       "50",    "80x50",
  219.               "80x14", "80x28",
  220.               "40x12", "40x21", "40x25", "40x28", "40x50"
  221.  
  222.     $status     [READ  ONLY] Status of the success of the last
  223.             command (TRUE or FALSE).  This is usually used with
  224.             ~force to check on the success of a search, or a
  225.             file operation.
  226.  
  227.     $title        Under X11, the current window title.
  228.  
  229.     $tpause     On a PC, time to pause for paren matching.
  230.  
  231.     $version    [READ  ONLY] Contains  the  current  vile version number.
  232.  
  233.     $wline        Number of display lines in current window.
  234.  
  235.     $word        [READ ONLY] The current "word" under the cursor.
  236.  
  237.     $write-hook    Name of procedure to run before a file is written
  238.  
  239.  
  240. User variables
  241. --------------
  242.     User variables allow you, the user, to store strings and manipulate
  243.     them.  These strings can be pieces of text, numbers (in text form),
  244.     or the logical values TRUE and FALSE.  These variables can be
  245.     combined, tested, inserted into buffers, and otherwise used to
  246.     control the way your macros execute.  All users variable names must
  247.     begin with a percent sign (%) and may contain any printing
  248.     characters.
  249.  
  250.  
  251. Interactive variables
  252. ---------------------
  253.     Interactive variables are actually a method to prompt the user for
  254.     a string.  This is done by using an at sign (@) followed either
  255.     with a quoted string, or a variable containing a string.  The
  256.     string is then placed on the bottom line, and the editor waits for
  257.     the user to type in a string.  Then the string typed in by the
  258.     user is returned as the value of the interactive variable.  For
  259.     example:
  260.  
  261.         set-variable %quest "What file? "
  262.         find-file @%quest
  263.  
  264.     will ask the user for a file name, and then attempt to find it. 
  265.     Note also that complex expressions can be built up with these
  266.     operators, such as:
  267.  
  268.     @&cat &cat "File to decode[" %default "]: "
  269.  
  270.         which prompts the user with the concatenated string.
  271.  
  272.  
  273. Functions
  274. ---------
  275.     Functions can be used to manipulate variables in various ways. 
  276.     Functions can have one, two, or three arguments.  These arguments
  277.     will always be placed after the function on the current command
  278.     line.  For example, if we wanted to increase the current fill
  279.     column by two, using the set-variable command, we would write:
  280.  
  281.         setv $fillcol &add $fillcol 2
  282.          \      \      \      \     \____second operand
  283.           \      \      \      \_________first operand
  284.            \      \      \_______________function to execute
  285.             \      \_____________________variable to set
  286.              \___________________________set-variable command
  287.  
  288.     Function names always begin with the ampersand (&) character, and
  289.     are only significant to the first three characters after the
  290.     ampersand.  Functions will normally expect one of three types of
  291.     arguments, and will automatically convert types when needed.
  292.  
  293.     <num>         an ascii string of digits which is interpreted
  294.               as a numeric value.  Any string which does not
  295.               start with a digit or a minus sign (-) will be
  296.               considered zero.
  297.  
  298.  
  299.     <str>         An  arbitrary  string  of  characters.  At the
  300.               moment, strings are limited to  128 characters
  301.               in length.
  302.  
  303.     <log>         A  logical  value  consisting  of  the  string
  304.               "TRUE" or "FALSE".   Numeric strings will also
  305.               evaluate to "FALSE" if they are equal to zero,
  306.               and "TRUE" if they  are  non-zero.   Arbitrary
  307.               text strings will have the value of "FALSE".
  308.  
  309.     A list of the currently available functions follows: Functions are
  310.     always used in lower case, the uppercase letters in the function
  311.     table are the short form of the function (i.e.  &div for ÷).
  312.  
  313.     Numeric Functions:      (returns <num>)
  314.  
  315.     &ADD            <num> <num>     Add two numbers
  316.     &SUB            <num> <num>     Subtract the second number from the
  317.                     first
  318.     &TIMes          <num> <num>     Multiply two numbers
  319.     &DIVide         <num> <num>     Divide the first number by the second
  320.                     giving an integer result
  321.     &MOD            <num> <num>     Return the reminder of dividing the
  322.                     first number by the second
  323.     &NEGate         <neg>           Multiply the arg by -1
  324.     &LENgth         <str>           Returns length of string
  325.     &SINdex         <str1> <str2>   Finds the position of <str2> within
  326.                     <str1>. Returns zero if not found.
  327.     &ASCii          <str>           Return the ascii code of the first
  328.                     character in <str>
  329.     &RND            <num>           Returns a random integer between 1 and
  330.                     <num>
  331.     &ABS            <num>           Returns the absolute value of <num>
  332.  
  333.     String manipulation functions:  (returns <str>)
  334.  
  335.     &CAT            <str> <str>     Concatenate the two strings to form
  336.                     one
  337.     &LEFt           <str> <num>     return the <num> leftmost characters
  338.                     from <str>
  339.     &RIGht          <str> <num>     Starting from <num> position in <str>,
  340.                     return substring to the right.
  341.     &MID            <str> <num1> <num2>
  342.                     Starting from <num1> position in <str>,
  343.                     return <num2> characters.
  344.     &UPPer          <str>           Uppercase <str>
  345.     &LOWer          <str>           lowercase <str>
  346.     &CHR            <num>           return a string with the character
  347.                     represented by ascii code <num>
  348.     >K                            return a string containing a single
  349.                     keystroke from the user
  350.     &ENV            <str>           If the operating system is capable,
  351.                     this returns the environment string
  352.                     associated with <str>
  353.     &BIND           <str>           return the function name bound to the
  354.                     keystroke <str>
  355.  
  356.     Logical Testing functions:      (returns <log>)
  357.  
  358.     &NOT            <log>           Return the opposite logical value
  359.     &AND            <log1> <log2>   Returns TRUE if BOTH logical arguments
  360.                     are TRUE
  361.     &OR             <log1> <log2>   Returns TRUE if either argument
  362.                     is TRUE
  363.     &EQUal          <num> <num>     If <num> and <num> are numerically
  364.                     equal, return TRUE
  365.     &LESs           <num1> <num2>   If <num1> is less than <num2>, return
  366.                     TRUE.
  367.     &GREater        <num1> <num2>   If <num1> is greater than, or equal to
  368.                     <num2>, return TRUE.
  369.     &SEQual         <str1> <str2>   If the two strings are the same,
  370.                     return TRUE.
  371.     &SLEss          <str1> <str2>   If <str1> is less alphabetically than
  372.                     <str2>, return TRUE.
  373.     &SGReater       <str1> <str2>   If <str1> is alphabetically greater
  374.                     than or equal to <str2>, return TRUE.
  375.     &RD              <str>           Is the named file <str> readable?
  376.     &WR              <str>           Is the named file <str> writable?
  377.  
  378.     Special Functions:
  379.  
  380.     &GLObal        <modename>    retrieves global mode setting
  381.     &LOCal        <modename>    retrieves local mode setting
  382.  
  383.     &INDirect       <str>           Evaluate <str> as a variable.
  384.  
  385.     This last function deserves more explanation.  The &IND function
  386.     evaluates its argument, takes the resulting string, and then uses
  387.     it as a variable name.  For example, given the following code
  388.     sequence:
  389.  
  390.         ; set up reference table
  391.  
  392.         setv %one        "elephant"
  393.         setv %two        "giraffe"
  394.         setv %three      "donkey"
  395.  
  396.         setv %index "two"
  397.         insert-string &ind %index
  398.  
  399.     the string "giraffe" would have been inserted at the point in the
  400.     current buffer.  This indirection can be safely nested up to about
  401.     10 levels.
  402.  
  403.  
  404. Directives
  405. ----------
  406.     Directives are commands which only operate within an executing
  407.     macro, i.e.,  they do not make sense as a single command.  As such,
  408.     they cannot be called up singly or bound to keystroke.  Used within
  409.     macros, they control what lines are executed and in what order.
  410.  
  411.     Directives always start with the tilde (~) character and must be
  412.     the first thing placed on a line.  Directives executed
  413.     interactively (via the execute-command- line command) will be
  414.     ignored.
  415.  
  416.  
  417.     ~ENDM Directive
  418.     ---------------
  419.     This directive is used to terminate a macro being stored.  For
  420.     example, if a file is being executed contains the text:
  421.  
  422.         ;       Read in a file in view mode, and make the window red
  423.         ;    (color is only settable on some platforms, e.g. DOS)
  424.  
  425.         26      store-macro
  426.             find-file @"File to view: "
  427.             set-mode "view"
  428.             set-mode bcolor "red"
  429.         ~endm
  430.  
  431.         write-message "[Consult macro has been loaded]"
  432.  
  433.     only the lines between the store-macro command and the ~ENDM
  434.     directive are stored in macro 26.  Both numbered macros and named
  435.     procedures (via the store-procedure command) should be terminated
  436.     with this directive.
  437.  
  438.  
  439.     ~FORCE Directive
  440.     ----------------
  441.     When a macro is executed, if any command fails, the macro is
  442.     terminated at that point.  If a line is preceded by a ~FORCE
  443.     directive, execution continues whether the command succeeds or not. 
  444.     For example:
  445.  
  446.         set-variable %quest "What file? "
  447.         find-file @%quest
  448.         ~force set-mode bcolor "red"
  449.         goto-beginning-of-file    
  450.         insert-string "this is a new first line in that file\n"
  451.  
  452.     will function correctly even if this version of the editor does not
  453.     support color.  (Currently, only the DOS version supports color.)
  454.  
  455.  
  456.     ~IF, ~ENDIF, ~ELSE, and ~ENDIF Directives
  457.     -----------------------------------------
  458.     This directive allows statements to be executed only if a condition
  459.     specified in the directive is met.  Every line following the ~IF
  460.     directive, until the first ~ELSE or ~ENDIF directive, is only
  461.     executed if the expression following the ~IF directive evaluates to
  462.     a TRUE value.  For example, the following macro segment creates the
  463.     portion of a text file automatically.
  464.  
  465.         ~if &sequal %curplace "timespace vortex"
  466.             insert-string "First, rematerialize\n"
  467.         ~endif
  468.         ~if &sequal %planet "earth"     ;If we have landed on earth...
  469.         ~if &sequal %time "late 20th century"  ;and we are then
  470.             write-message "Contact U.N.I.T."
  471.         ~else
  472.             insert-string "Investigate the situation....\n"
  473.             insert-string "(SAY 'stay here Sara')\n"
  474.         ~endif
  475.         ~elseif &sequal %planet "luna"  ;If we have landed on our neighbor...
  476.         write-message "Keep the door closed"
  477.         ~else
  478.         setv %conditions @"Atmosphere conditions outside? "
  479.         ~if &sequal %conditions "safe"
  480.             insert-string &cat "Go outside......" "\n"
  481.             insert-string "lock the door\n"
  482.         ~else
  483.             insert-string "Dematerialize..try somewhen else"
  484.             newline
  485.         ~endif
  486.         ~endif
  487.  
  488.  
  489.     ~GOTO Directive
  490.     ---------------
  491.     Flow can be controlled within a macro using the ~GOTO directive. 
  492.     It takes as an argument a label.  A label consists of a line
  493.     starting with an asterisk (*) and then an alphanumeric label.  Only
  494.     labels in the currently executing macro can be jumped to, and
  495.     trying to jump to a non-existing label terminates execution of a
  496.     macro.  For example..
  497.  
  498.         ;Create a block of DATA statements for a BASIC program
  499.  
  500.             insert-string "1000 DATA "
  501.             setv %linenum 1000
  502.  
  503.         *nxtin
  504.             update-screen           ;make sure we see the changes
  505.             setv %data @"Next number: "
  506.             ~if &equal %data 0
  507.                 ~goto finish
  508.             ~endif
  509.  
  510.             ~if &greater $curcol 60
  511.                 2 delete-previous-character
  512.                 newline
  513.                 setv %linenum &add %linenum 10
  514.                 insert-string &cat %linenum " DATA "
  515.             ~endif
  516.  
  517.             insert-string &cat %data ", "
  518.             ~goto nxtin
  519.  
  520.         *finish
  521.  
  522.             2 delete-previous-character
  523.             newline
  524.  
  525.  
  526.     ~WHILE and ~ENDWHILE Directives
  527.     -------------------------------
  528.     This directive allows you to set up repetitive tasks easily and
  529.     efficiently.  If a group of statements need to be executed while a
  530.     certain condition is true, enclose them with a while loop.  For
  531.     example,
  532.  
  533.         ~while &less $curcol 70
  534.             insert-string &cat &cat "[" <stuff "]"
  535.         ~endwhile
  536.  
  537.     places items from buffer "stuff" in the current line until the
  538.     cursor is at or past column 70.  While loops may be nested and can
  539.     contain and be the targets of ~GOTOs with no ill effects.  Using a
  540.     while loop to enclose a repeated task will run much faster than the
  541.     corresponding construct using ~IFs.
  542.  
  543.  
  544.     ~BREAK Directive
  545.     ----------------
  546.     This directive allows the user to abort out of the currently most
  547.     inner while loop, regardless of the condition.  It is often used to
  548.     abort processing for error conditions.  For example:
  549.  
  550.     ;       Read in files and substitute "begining" with "beginning"
  551.  
  552.         setv %filename <list
  553.         ~while ¬ &seq %filename "<end>"
  554.             ~force  find-file %filename
  555.             ~if &seq $status FALSE
  556.                 write-message "[File read error]"
  557.                 ~break
  558.             ~endif
  559.             beginning-of-file
  560.             replace-string "begining" "beginning"
  561.             save-file
  562.             setv %filename <list
  563.         ~endwhile
  564.  
  565.     This while loop will process files until the list is exhausted or
  566.     there is an error while reading a file.
  567.  
  568.  
  569.     ~RETURN Directive
  570.     -----------------
  571.     The ~RETURN Directive causes the current macro to exit, either
  572.     returning to the caller (if any) or to interactive mode.  For
  573.     example:
  574.  
  575.     ;       Check the monitor type and setv %mtyp
  576.  
  577.         ~if &sres "CGA"
  578.             setv %mtyp 1
  579.         ~else
  580.             setv %mtyp 2
  581.             ~return
  582.         ~endif
  583.  
  584.         insert-string "You are on an old machine!\n"
  585.  
  586. -----------------------------------
  587.   $Header: /usr/build/vile/vile/doc/RCS/macros.doc,v 1.2 1998/07/01 23:18:08 cmorgan Exp $
  588. -----------------------------------
  589.