Page 1 of 1

Execute highlighted code

PostPosted: September 19th, 2014, 3:05 pm
by Ajay Askoolum
I've been working primarily with SQL Server Management Studio in recent weeks. One of the features of the query window is that you can highlight any complete code snippet, press F5 and SSMS returns the results in the results window.

For example, with

select * from myTable where exists (select key from myTable1);

I could highlight

select key from myTable1

Press F5 and get the result.

In the last couple of days, I've been highlighting code in the APL+Win interactive session and pressing F5 to no avail.

Can you implement a similar feature? It would be a big bonus when debugging code. And if it execute in situ (i.e. without echoing the snippet to the bottom of the session as in SSMS), it will avoid clutter in the session.

Re: Execute highlighted code

PostPosted: September 22nd, 2014, 2:43 pm
by Tech Support
Thank you for your suggestion.

Re: Execute highlighted code

PostPosted: September 22nd, 2014, 3:43 pm
by Chizever
Create a tool with a keyboard shortcut of F5.

The entire command is a single del character.

Now whenever you highlight anything, press F5 and it will execute it in your session.

You may want to write a user command, SafeExecute, so that it can trap errors and/or pop up a screen with the result
Code: Select all
    ∇ CMDSAFEEXECUTE ∆∆a;∆∆result;⎕wself
[1]    :try
[2]       ⍎'∆∆result←',∆∆a
[3]       ∆∆result←⍕∆∆result
[4]       ∆∆result←∊∆∆result,⎕tcnl
[5]    :catchif 'VALUE ERROR'≡⎕EM
[6]       ∆∆result←'no result from executed expression'
[7]    :catchall
[8]       ∆∆result←'error executing expression:',⎕tcnl,⎕tcnl,⎕dm
[9]    :endtry
[10]
[11]   ⎕wself←'∆∆SafeExecute' ⎕wi 'Create' 'Form' 'Close'
[12]     ⎕wi 'caption' 'Safe Execute'
[13]     ⎕wi 'border' 2 16 64
[14]     ⎕wi 'icon' '>ICO 116 aplw.exe'
[15]     ⎕wi 'font' 'APLHELP'
[16]     ⎕wi 'onResize' '⎕wi ":ed.where" (0 0,2↑⎕warg)'
[17]
[18]   ⎕wself←⎕wi ':ed.New' 'Edit'
[19]     ⎕wi 'style' 4 8 16 64 2048 16384
[20]     ⎕wi 'text' (∆∆a,⎕tcnl,⎕tcnl,⎕tcnl,∆∆result)
[21]
[22]   0 0⍴⎕wi ':Show'
    ∇


Make the command in tools to be:
Code: Select all
⎕ucmd 'SafeExecute ∇'


Now, you'll execute the hilighted code and display the results in a pop up window.

Re: Execute highlighted code

PostPosted: September 22nd, 2014, 4:06 pm
by Ajay Askoolum
Thank you very much. That (just defining the tool) worked for me. And, it will be very handy albeit the highlighting is lost upon execution.

May be a future version will support this out of the box & keep the highlighting too: highlight code, press key (F5 or other) and the results display as a tooltip - any interaction loses the tooltip.

Re: Execute highlighted code

PostPosted: September 25th, 2014, 9:23 pm
by brent hildebrand
May I suggest a slight tweak - so that the []IO of the SafeExecute reflect the []IO of the current session?
Code: Select all
    ∇ CMDSAFEEXECUTE ∆∆a;∆∆result;⎕wself;⎕io
[1]   ⎕io←0 ⎕vget '⎕io'
[2]    :try
[3]       ⍎'∆∆result←',∆∆a
[4]       ∆∆result←⍕∆∆result
[5]       ∆∆result←∊∆∆result,⎕tcnl
[6]    :catchif 'VALUE ERROR'≡⎕EM
[7]       ∆∆result←'no result from executed expression'
[8]    :catchall
[9]       ∆∆result←'error executing expression:',⎕tcnl,⎕tcnl,⎕dm
[10]   :endtry
[11] 
[12]   ⎕wself←'∆∆SafeExecute' ⎕wi 'Create' 'Form' 'Close'
[13]     ⎕wi 'caption' 'Safe Execute'
[14]     ⎕wi 'border' 2 16 64
[15]     ⎕wi 'style' 2 16
[16]     ⎕wi 'icon' ('>ICO 116 ',∊↑/⎕wcall 'GetModuleFileName' (↑⎕wcall 'W_Init') (512⍴' ') 512)
[17]     ⎕wi 'font' 'APLHELP'
[18]     ⎕wi 'onResize' '⎕wi ":ed.where" (0 0,2↑⎕warg)'
[19] 
[20]   ⎕wself←⎕wi ':ed.New' 'Edit'
[21]     ⎕wi 'style' 4 8 16 64 2048 16384
[22]     ⎕wi 'text' (∆∆a,⎕tcnl,⎕tcnl,⎕tcnl,∆∆result)
[23] 
[24]   0 0⍴⎕wi ':Show'
    ∇