-- This procedure maps the Rational keyboard functions to the IBM PS/2 keyboard.

with Compilation;
with Job;
with Debug;
with Access_List;
with Queue;
with Ada;
with Command;
with Common;
with Editor;
with Job;
with Library;
with Operator;
with Text;
with Visible_Key_Names;
with What;

procedure Ps2_Commands is

    -- Release D_8_0_0, last updated 8/8/86

    use Visible_Key_Names;

    type Intent is (Prompt, Execute, Interrupt);

    Action : Intent;

    Key_1 : Vt100_Key_Names;
    Key_2 : Vt100_Key_Names;
begin
    case Action is
        when Prompt =>
            case Key_1 is


                -- The commands following the "when prompt" case statement are
                -- not executed immediately upon pressing the corresponding
                -- function key sequence.  Rather, they open a command window
                -- with the proper procedure and named parameters displayed and
                -- wait for the user to enter the desired parameter values.


                when Esc_M | Esc_S_M =>
                    Debug.Modify (New_Value => "",
                                  Variable => "<SELECTION>",
                                  Stack_Frame => 0);

                when Esc_6 =>
                    Library.Create_Directory (Name => "");
                when Esc_5 =>
                    Text.Create (Image_Name => "");
                when Esc_Percent =>
                    Library.Create_World (Name => "");

                when Numeric_7 =>
                    case Key_2 is
                        when 'l' | 'L' =>
                            Common.Revert;
                        when others =>
                            null;
                    end case;

                when others =>
                    null;

            end case;


        when Execute =>
            case Key_1 is
                --ONE BUTTON FUNCTION KEYS--

                when Pf1 =>
                    Common.Promote;
                when Pf2 =>
                    Common.Complete;
                when Pf3 =>
                    Common.Format;
                when Numeric_6 =>
                    Editor.Line.Beginning_Of;
                when Numeric_Comma =>
                    Editor.Line.End_Of;
                when Numeric_1 =>
                    Common.Explain;
                when Numeric_0 =>
                    Editor.Set.Designation_Off;
                when Numeric_2 =>
                    Editor.Cursor.Next;


                when Pf4 =>                        --DEFINITION
                    Common.Definition;  
                when Enter =>                      --EDIT
                    Common.Edit;  
                when Numeric_3 =>                  --CREATE COMMAND
                    Common.Create_Command;
                when Dot =>                        --SEMANTICIZE
                    Common.Semanticize;


                    --CONTROL KEYS--

                when C_D =>
                    Command.Debug;
                when C_H =>                        --BACKSPACE
                    Editor.Char.Delete_Backward;
                when Delete =>                     --DELETE
                    Editor.Char.Delete_Backward;
                when C_I =>                        --TAB
                    Editor.Char.Tab_Forward;
                when C_L =>                        --REFRESH THE SCREEN
                    Editor.Screen.Redraw;
                when C_M =>                        --RETURN
                    Editor.Line.Indent;
                when C_N =>                        --SCROLL IN WINDOW
                    Editor.Cursor.Down;
                when C_U =>
                    Editor.Cursor.Up;
                when C_R =>                        --SEARCH BACK
                    Editor.Search.Previous;
                when C_Z =>                        --SEARCH FORWARD
                    Editor.Search.Next;
                when C_O =>
                    Editor.Search.Replace_Next;    --SEARCH AND REPLACE NEXT
                when C_K =>
                    Editor.Search.
                       Replace_Previous;           --SEARCH AND REPLACE PREVIOUS

                when C_G =>                        --DISCONNECT FROM JOB
                    Job.Interrupt;




                when Up =>                         --ARROW KEYS
                    Editor.Cursor.Up;
                when Down =>
                    Editor.Cursor.Down;
                when Right =>
                    Editor.Cursor.Right;
                when Left =>
                    Editor.Cursor.Left;



                when Numeric_8 =>                         --REGION--
                    case Key_2 is
                        when 'X' | 'x' =>
                            Editor.Region.Off;
                        when '<' | ',' =>
                            Editor.Region.Lower_Case;
                        when '>' | '.' =>
                            Editor.Region.Upper_Case;
                        when '6' =>
                            Editor.Region.Capitalize;
                        when 'c' | 'C' =>
                            Editor.Region.Copy;
                        when 'd' | 'D' | 'k' | 'K' =>
                            Editor.Region.Delete;
                        when 'm' | 'M' =>
                            Editor.Region.Move;
                        when '[' | '{' =>
                            Editor.Region.Start;
                        when ']' | '}' =>
                            Editor.Region.Finish;
                        when Numeric_2 =>
                            Editor.Region.End_Of;
                        when Numeric_1 =>
                            Editor.Region.Beginning_Of;
                        when Left =>
                            Editor.Hold_Stack.Previous;
                        when Right =>
                            Editor.Hold_Stack.Next;
                        when Up =>
                            Editor.Hold_Stack.Top;
                        when Down =>
                            Editor.Hold_Stack.Push;
                        when Delete =>
                            Editor.Hold_Stack.Delete_Top;
                        when '-' =>
                            Editor.Region.Comment;
                        when '=' =>
                            Editor.Region.Uncomment;
                        when others =>
                            null;
                    end case;



                when Numeric_9 =>                           --WINDOW--
                    case Key_2 is
                        when '!' | '1' =>
                            Editor.Window.Expand;
                        when '.' =>
                            Editor.Window.Expand (-4);
                        when '?' | '/' | Pf1 =>
                            Editor.Window.Directory;
                        when 'c' | 'C' =>
                            Editor.Window.Copy;
                        when 'j' | 'J' =>
                            Editor.Window.Join (1);
                        when 't' | 'T' =>
                            Editor.Window.Transpose;
                        when 'D' | 'd' =>
                            Editor.Window.Delete;
                        when Delete =>
                            Editor.Window.Join (-1);
                        when Numeric_1 =>
                            Editor.Window.Beginning_Of;
                        when Numeric_2 =>
                            Editor.Window.End_Of;
                        when Down =>
                            Editor.Window.Next;
                        when Dot =>
                            Editor.Window.Focus;
                        when Enter =>
                            Editor.Window.Promote;
                        when Pf2 =>
                            Editor.Window.Demote;
                        when Left =>
                            Editor.Window.Parent;
                        when Right =>
                            Editor.Window.Child;
                        when Up =>
                            Editor.Window.Previous;
                        when others =>
                            null;
                    end case;



                when Dash =>                                --IMAGE--
                    case Key_2 is
                        when Numeric_2 =>
                            Editor.Image.End_Of;
                        when Down =>
                            Editor.Image.Down;
                        when Numeric_1 =>
                            Editor.Image.Beginning_Of;
                        when Left =>
                            Editor.Image.Left;
                        when Right =>
                            Editor.Image.Right;
                        when Up =>
                            Editor.Image.Up;
                        when 'I' | 'i' =>
                            Editor.Set.Insert_Mode (True);
                        when 'O' | 'o' =>
                            Editor.Set.Insert_Mode (False);
                        when 'F' | 'f' =>
                            Editor.Set.Fill_Mode (True);
                        when 'X' | 'x' =>
                            Editor.Set.Fill_Mode (False);
                        when '?' | '/' =>
                            Editor.Image.Find ("");
                        when others =>
                            null;
                    end case;

                    --MARK--
                when Numeric_5 =>
                    case Key_2 is
                        when Down =>
                            Editor.Mark.Push;
                        when Right =>
                            Editor.Mark.Next;
                        when Left =>
                            Editor.Mark.Previous;
                        when Up =>
                            Editor.Mark.Top;
                        when Numeric_1 | '[' | '{' =>
                            Editor.Macro.Start;
                        when Numeric_2 | ']' | '}' =>
                            Editor.Macro.Finish;
                        when Enter | C_M =>
                            Editor.Macro.Execute;
                        when Pf1 =>
                            Editor.Macro.Bind;
                        when others =>
                            null;
                    end case;


                when Numeric_7 =>                          --OBJECT--
                    case Key_2 is

                        when Enter =>
                            Common.Commit;
                        when '1' | '!' =>
                            Common.Expand;
                        when '.' =>
                            Common.Elide;
                        when '/' | '?' =>
                            Common.Explain;
                        when 'c' | 'C' =>
                            Common.Object.Copy;
                        when 'd' | 'D' | 'k' | 'K' =>
                            Common.Object.Delete;
                        when 'g' | 'G' =>
                            Common.Abandon;
                        when 'i' | 'I' =>
                            Common.Object.Insert;
                        when 'm' | 'M' =>
                            Common.Object.Move;
                        when 'x' | 'X' =>
                            Common.Release;
                        when 'r' | 'R' =>
                            Common.Redo;
                        when 'u' | 'U' =>
                            Common.Undo;
                        when Down =>
                            Common.Object.Next;
                        when Left =>
                            Common.Object.Parent;
                        when Right =>
                            Common.Object.Child;
                        when Up =>
                            Common.Object.Previous;
                        when Numeric_1 =>
                            Common.Object.First_Child;
                        when Numeric_2 =>
                            Common.Object.Last_Child;
                        when Pf1 =>
                            Common.Definition;
                        when others =>
                            null;
                    end case;


                when Numeric_4 =>                           --LINE--
                    case Key_2 is
                        when 'I' | 'i' =>
                            Editor.Line.Insert;
                        when 't' | 'T' =>
                            Editor.Line.Transpose;
                        when '?' | '/' =>
                            What.Line;
                        when 'j' | 'J' =>
                            Editor.Line.Join;
                        when '$' | '4' =>
                            Editor.Line.Center;
                        when 'd' | 'D' | Delete =>
                            Editor.Line.Delete;
                        when 'c' | 'C' =>
                            Editor.Line.Copy;
                        when others =>
                            null;
                    end case;



                    --FUNCTION KEYS--

                when Esc_K | Esc_S_K =>
                    Editor.Key.Name;
                when Esc_J | Esc_S_J =>
                    What.Does ("help_on_help");
                when Esc_H | Esc_S_H =>
                    What.Does;
                when Esc_P | Esc_S_P =>
                    Queue.Print;
                when Esc_Y | Esc_S_Y =>
                    Debug.Stop;
                when Esc_A | Esc_S_A =>
                    Debug.Put;
                when Esc_S | Esc_S_S =>
                    Debug.Break;
                when Esc_W | Esc_S_W =>
                    Debug.Run (Debug.Returned);
                when Esc_F | Esc_S_F =>
                    Debug.Run (Stop_At => Debug.Local_Statement);
                when Esc_I | Esc_S_I =>
                    Debug.Execute;
                when Esc_R | Esc_S_R =>
                    Debug.Run;
                when Esc_U | Esc_S_U =>
                    What.Home_Library;
                when Esc_N | Esc_S_N =>
                    Common.Enclosing (In_Place => True, Library => False);  
                when Esc_E | Esc_S_E =>
                    Common.Enclosing;
                when Esc_O | Esc_S_O =>
                    Ada.Other_Part (Name => "<image>", In_Place => False);
                when Esc_D | Esc_S_D =>
                    Common.Definition
                       (Name => "<CURSOR>", In_Place => True, Visible => True);
                when Esc_Q | Esc_S_Q =>
                    Editor.Key.Prompt;
                when Esc_1 =>
                    Compilation.Make;
                when Esc_Exclam =>
                    Compilation.Promote;
                when Esc_2 =>
                    Ada.Code_Unit;
                when Esc_At_Sign =>
                    Ada.Install_Unit;  
                when Esc_3 =>
                    Ada.Source_Unit;
                when Esc_Sharp =>
                    Common.Demote;
                when Esc_4 =>
                    Compilation.Demote (Unit => "<SELECTION>",
                                        Goal => Compilation.Installed,
                                        Limit => "<WORLDS>",
                                        Effort_Only => False,
                                        Response => "<PROFILE>");  
                when Esc_Less_Than =>
                    Compilation.Demote;
                when Esc_Greater_Than =>
                    Ada.Withdraw;
                when Esc_Dollar =>
                    Ada.Create_Private;
                when Esc_5 =>  
                    Editor.Key.Prompt;
                    Text.Create;
                when Esc_Percent =>
                    Editor.Key.Prompt;
                    Library.Create_World;
                when Esc_6 =>
                    Editor.Key.Prompt;
                    Library.Create_Directory;
                when Esc_Ampersand =>
                    Common.Object.Insert;
                when Esc_7 =>
                    Ada.Create_Body;
                when Esc_Query =>
                    Access_List.Display;
                when Esc_Slash =>
                    Ada.Show_Unused;
                when Esc_Star =>
                    Common.Clear_Underlining;
                when Esc_Left_Paren =>
                    Ada.Get_Errors;
                when Esc_9 =>
                    Editor.Cursor.Previous;
                when Esc_Right_Paren =>
                    Editor.Cursor.Next (Prompt => False, Underline => True);
                when Esc_0 =>
                    Editor.Cursor.Next (Prompt => True, Underline => False);
                when Esc_Left_Brace =>
                    Job.Connect;
                when Esc_Right_Brace =>
                    Job.Kill (0);
                when Esc_Left_Bracket =>
                    Job.Enable (0);
                when Esc_Right_Bracket =>
                    Job.Disable (0);
                when Esc_Plus =>
                    What.Object;
                when Esc_Equal =>
                    What.Locks;
                when Esc_Tilde =>
                    What.Users;
                when Esc_T | Esc_S_T =>
                    What.Time;
                when others =>
                    null;
            end case;

        when Interrupt =>
            case Key_1 is

                when C_G =>
                    Job.Interrupt;
                when Esc_G =>
                    Job.Kill (0);

                when others =>
                    null;
            end case;
    end case;

end Ps2_Commands;