Running Ved from Ved 
Author Message
 Running Ved from Ved

I wonder whether anyone can help with a Ved problem?

I'm running my Eden (autonomous agent) simulation from inside Ved, by
calling
    vedobey( 'FILE', simulate );
The simulator procedure goes round in a loop. At the start of each
cycle, it updates the edit window to display the current state of the
microworld. It then lets the agent "think", and updates the microworld
according to the agent's action, ready to be displayed on the next
cycle.

I am testing some code which allows the agent to behave
opportunistically, that is, to take advantage of events even if they
were not foreseen in its planning. For example, if a piece of "food"
suddenly appears, and the agent isn't too busy with other things, it
should grab and eat the food.

To exercise the opportunistic planner, I want to be able to deposit new
items in the microworld as the simulation runs. Since the microworld is
based on a 2d grid, one cell per character, the obvious way to do this
is to allow the user to edit the window, having the simulator then
read the changed buffer and update the world accordingly.

Here comes the problem. How do I invoke editing from inside a procedure
called by Ved? I thought that -edit(<filename>)- would do. According to
REF VEDPROCS, it "calls the editor with the file, whether in VED or
not". However, when I call it inside -simulate-, it immediately returns,
and doesn't allow the user to edit.

Secondly, even if I could invoke the editor, how would I redefine
a key temporarily? I need to redefine a key (haven't decided which one)
so that hitting it causes a return from editing, back to the simulator.
Incidentally, this key can't be ENTER, because I want the user to be able
to give ENTER commands from here.

I know one can imitate the Ved read-get edit proc-edit loop, and I could
do that if necessary. But I'd prefer a neater solution, especially as
I'm not sure that I could make my loop exactly imitate Ved's specification,
whatever that is.

A cut down version of the problem is below.

Jocelyn

;;; Main procedure. Call this to test.
;;;
define main();
    vedobey( 'FILE', simulate );
enddefine;

;;; This procedure is run from inside Ved. It goes round in a loop,
;;; incrementing a counter and displaying it on line 10 of the edit
;;; window. At the end of each cycle, it asks the user whether he
;;; wants to change the counter. If he does, it calls -edit_file-,
;;; which lets him freely edit the window, putting a new value for
;;; the counter on line 9.
;;;
define simulate();
    lvars c, counter = 0;

start:
    vedputmessage( 'Next cycle' );
    vedjumpto( 10, 1 );
    vedcleartail();
    vedinsertstring( counter><'' );
    vedputmessage( 'Do you want to change the counter (y/n)?' );
    vedinascii() -> c;
    if c = `y` then
        edit_file() -> counter
    else
        1 + counter -> counter;
    endif;
    goto start;
enddefine;

;;; This procedure is supposed to let the user edit the window being
;;; displayed. In this little example, he is assumed to be inserting
;;; an integer, the new value for -simulate-'s counter, at the start
;;; of line 9. -edit_file- will return this as its result.
;;;
define edit_file();

    vedputmessage( 'Insert new value on line 9: press <some key> to finish' );

edit('FILE');
        What I want here is some code which invokes Ved on the file
        FILE and lets the user edit it freely. The only change from
        normal is that <some key> should cause a return from
        editing, transferring control back to the next line below.
        This change in the effect of <some key> must be temporary,
        because I want it to behave as normal at all other times.

        Note: the obvious way to do this (modulo altering the <some
        key> function) is to call
            edit( 'FILE' )
        at this point. However, if I do so, it immediately returns,
        and doesn't allow the user to edit. Why?

    vedjumpto( 9, 1 );
    vednextitem();
enddefine;



Fri, 11 Aug 1995 04:58:41 GMT  
 Running Ved from Ved

Quote:

> Date: 21 Feb 93 20:58:41 GMT
> Organization: Oxford University VAX 6620

> I wonder whether anyone can help with a Ved problem?

I append the reply I sent to the pop-forum email list.
-------------------------------------------------------------------

Quote:
> Here comes the problem. How do I invoke editing from inside a procedure
> called by Ved? I thought that -edit(<filename>)- would do. According to
> REF VEDPROCS, it "calls the editor with the file, whether in VED or
> not". However, when I call it inside -simulate-, it immediately returns,
> and doesn't allow the user to edit.

REF VEDPROCS is inaccurate on this point and should be corrected. If VED
is already already running then edit(<file>) behaves as equivalent
roughly to

    vededitor(vedveddefaults, <file>)

(SHOWLIB EDIT, makes the equivalence clear.)

The entry for REF VEDPROCS/vededitor is more accurate. It says:

        If  this  procedure  is  called  from  inside  VED  (i.e.   when
        -vedinvedprocess- is already true), it simply calls

            vedgetfile(DEFAULTS_P, VEDFILE).

        and returns.

I.e. the file is simply read in (if necessary) and put at the front of
vedbufferlist, and made the "current" file so that procedures like
vedcharinsert, vedinsertstring, vedthisline, etc. and VED's global
variables refer to the file. It is also displayed on the screen if
necessary.

The reason nothing more is done is that if you are already in VED there
is already a top level loop reading in characters from the terminal, so
there is no need for vededitor, or edit, to set up a new one. If you
wish to set up a new one it can be tricky to exit from it, but there are
various things you can try.

This has become harder since the advent of Xved because more complicated
mechanisms had to be used to cope with the interaction with
Xved windows.

Anyhow, here's something that you can try, which may, in some ways,
be a bit more general than you need.
(Something like this should be available to users as a library utility).

define ved_get_and_edit(file);
    lvars
        file,           ;;; file to switch to.
        oldfile = vedcurrentfile;   ;;; file to return to

    define dlocal ved_q();
        ;;; redefine ved_q locally to return to old file,
        vededitor(identfn, oldfile);
        ;;; then exit from ved_get_and_edit
        exitfrom(ved_get_and_edit)
    enddefine;

    ;;; make the specified file current, after saving the oldfile
    edit(file);

    ;;; Invoke a replica of VED's top level loop, to be left when
    ;;; ved_q is invoked
    runproc(0, consproc(0,vedprocess));
enddefine;

/* test the above using mark and load

    ;;; make mapping to ved_q "soft", by mapping to word not procedure
    vedsetkey('\^[q', "ved_q");

    ;;; Now try this
    ved_get_and_edit('foo');
*/

If that doesn't work, you could try this version.

define ved_get_and_edit(file);
    lvars
        file,           ;;; file to switch to.
        oldfile = vedcurrentfile;   ;;; file to return to

    define dlocal ved_q();
        ;;; redefine ved_q locally to return to old file,
        vededitor(identfn, oldfile);
        ;;; then exit from ved_get_and_edit
        exitfrom(ved_get_and_edit)
    enddefine;

    ;;; make the specified file current, after saving the oldfile
    edit(file)

    ;;; simulate a subset of VED's top level loop, to be left when
    ;;; ved_q is invoked
    repeat
        ;;; get next command and obey it
        vedprocesschar();
        ;;; now sort out display
        if vedonstatus then
            vedsetstatus(nullstring, false, undef);
        else
            vedsetstatus(vedmessage, vedmessage /= nullstring, undef);
        endif;

        ;;; ensure cursor location is visible
        vedcheck();
        ;;; update cursor location after move procedures
        vedsetcursor();
    endrepeat;
enddefine;

Quote:
> Secondly, even if I could invoke the editor, how would I redefine
> a key temporarily? I need to redefine a key (haven't decided which one)
> so that hitting it causes a return from editing, back to the simulator.

I tried to illustrate that in the example above. Use vedsetkey (or
"vedset keys") to ensure that the key sequence that normally invokes
the procedure calls it via the procedure name, by using a word as
second argument. (It must be a "global" variable, or defined in the
current section, so that VED can get at it via valof.).

Then inside ved_get_and_edit, redefine that procedure locally to
return to the previous file (if you wish) then exitfrom
ved_get_and_edit.

Quote:
> Incidentally, this key can't be ENTER, because I want the user to be able
> to give ENTER commands from here.

I think that will work with the above procedure.

If John Gibson is reading this, he will probably give you a better
solution!

Aaron
--
--
Aaron Sloman,
School of Computer Science, The University of Birmingham, B15 2TT, England

Phone: +44-(0)21-414-3711       Fax:   +44-(0)21-414-4281



Fri, 11 Aug 1995 17:06:46 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Syntactic errors when compiling from a ved buffer

2. making the world safe for ved

3. ved and rxvt (Boring)

4. DRAFT Ved utility for editing files whose names have spaces

5. ved key-mapping to conventional use.

6. linux , non-X, ved ?

7. ved key-mapping ?

8. Stupid question about VED documentation files

9. VED sometimes overwrites current buffer when reading in file

10. windows version ox ved/xved?

11. Preventing ved from using bold fonts under xwindows

12. ved_gn (Ved newsreader) improved

 

 
Powered by phpBB® Forum Software