#!/usr/local/bin/apl --script ⍝ ⍝ These functions are written as a sample extension of the Toronto ⍝ Toolkit. "explain 'xed'" will work, as will "comments 'xed'". ⍝ 'xed', 'xeda' and 'cr2lf' are added to toolkitlist, but only if the ⍝ toolkit is present. So, use ")copy 1 toolkit" before ")copy 1 xed" ⍝ for proper integration with the toolkit. ⍝ ⍝ xed is set to call up my "e" editor, which is a terminal application. ⍝ It launches xterm to run a new terminal with the editor. This is done ⍝ because )host doesn't restore terminal settings before running its ⍝ given command. ⍝ ⍝ As well, "explain 'del'" will provide a reminder of the features of ⍝ the del editor in GNU APL. (I used del until xed itself was ⍝ operational). ⍝ ⍝ 'xeda array' edits an array by converting to csv, and using an ⍝ external editor (libreoffice calc). It returns the edited array. ⍝ It uses CSV to convert the array to a CSV, libreoffice calc to edit ⍝ the array, and CSV to read it in again. The CSV library brings in ⍝ FILE_IO, which is also needed. ⍝ ⍝ )COPY 5 FILE_IO )COPY 1 CSV g∆nl ← ⎕UCS 10 g∆tmp ← '/tmp/' g∆editor ← 'e ' g∆editorcsv ← 'oocalc ' g∆terminal ← 'xterm -xrm XTerm.vt100.initialFont:6 -e ' '→' ⎕EA 'toolkitlist ← toolkitlist on ''xed''' '→' ⎕EA 'toolkitlist ← toolkitlist on ''xeda''' '→' ⎕EA 'toolkitlist ← toolkitlist on ''cr2lf''' ∇r←cr2lf s ⍝convert CR to LF in string ⍝.k text-editing ⍝.n fmgw ⍝.t 2015.8.5.10.33.0 ⍝.v 1.0 / 05aug15 r←s r[(s = ⎕UCS 13)/(⍳⍴s)]←g∆nl ∇ ∇del ⍝describe 'del' editor usage ⍝.k text-editing ⍝.n fmgw ⍝.t 2015.8.5.10.33.0 ⍝.v 1.0 / 05aug15 'Enter: explain ''del''' 'for a brief explanation of the ∇ editor and its use with GNU APL.' ∇ howcr2lf←'DOS (and APL2) used ASCII CR (carrier return, 13)' howcr2lf←howcr2lf, ' to separate lines.', g∆nl howcr2lf←howcr2lf, 'Linux (GNU APL) uses ASCII LF (line feed, 10)' howcr2lf←howcr2lf, ' for this function.', g∆nl howcr2lf←howcr2lf, 'cr2lf converts any CR characters in a string' howcr2lf←howcr2lf, ' to LF characters.' howdel←'Help for GNU APL built-in del (∇) editor', g∆nl, g∆nl howdel←howdel, '∇FUN open function FUN', g∆nl howdel←howdel, '∇FUN[⎕] open with command, ∆ and → not' howdel←howdel, ' allowed', g∆nl howdel←howdel, '∇FUN[⎕]∇ open, list, close', g∆nl, g∆nl howdel←howdel, '∇ close', g∆nl howdel←howdel, '⍫ close and lock', g∆nl, g∆nl howdel←howdel, '[⎕] show [⎕] [n⎕] [⎕m] [n⎕m] [⎕n-m]' howdel←howdel, g∆nl howdel←howdel, '[∆] delete [n∆] [∆m] [n∆m] [∆n-m]' howdel←howdel, ' [∆n1 n2 ...]', g∆nl howdel←howdel, '[→] escape (clear definition, keep' howdel←howdel, ' header)', g∆nl howdel←howdel, '[n] goto', g∆nl howdel←howdel, '[n] text replace existing text on line n' howdel←howdel, g∆nl howdel←howdel, ' text replace existing text', g∆nl, g∆nl howdel←howdel, 'After opening function, up and down arrows will' howdel←howdel, ' retrieve function', g∆nl howdel←howdel, '^K - kill to end of line, ^Y yank, ^A/^E, ^N/^P (can' howdel←howdel, ' retrieve input', g∆nl howdel←howdel, 'line, ^K, define a function, then ^Y).' howxed←'xed ''function''. Edits function by calling out to an external' howxed←howxed, ' editor.', g∆nl howxed←howxed, 'Uses globals g∆nl, g∆tmp, g∆editor and g∆terminal.' howxeda←'r ← xed a. Edits array by calling out to an external' howxeda←howxeda, ' editor.', g∆nl howxeda←howxeda, 'Uses globals g∆nl, g∆tmp and g∆editorcsv.' ∇xed f;fn;s;r;cmd;h;n;q;⎕IO ⍝edit function with external editor ⍝.k text-editing ⍝.n fmgw ⍝.t 2015.8.5.11.0.0 ⍝.v 1.0 /05aug15 ⎕IO ← 1 q ← '"' ⍝ ⍝ Convert function to del edit sequence, with lines separated ⍝ by unix newlines. ⍝ s ← ⎕CR f s ← ,(' ', s[⍳(⍴s)[1];], g∆nl) s[1] ← '∇' s ← s, '∇' ⍝ ⍝ Write out character string representing function to ./function.APL ⍝ file. Prepend line to indicate that this is a "dump" format. ⍝ fn ← g∆tmp, f, '.APL' h ← 'w' FIO∆fopen fn n ← ('#!/usr/local/bin/apl --script', g∆nl) FIO∆fwrite_utf8 h n ← s FIO∆fwrite_utf8 h r ← FIO∆fclose h ⍝ ⍝ Call external editor on file. Use terminal because APL messes with ⍝ terminal control. ⍝ cmd ← g∆terminal, q, g∆editor, fn, q r ← ⍎')host ', cmd ⍝ ⍝ No convenient way to read utf-8 string. Could read file as byte ⍝ vector and then use ⎕CR to convert... but, easier to "dress up" the ⍝ function as a del editor definition, and use ")copy" to bring it back ⍝ in. Note that we add the directory to fn, to allow this to work in ⍝ other than workspaces/. ⍝ r ← ⍎')copy ', fn ⍝ ⍝ We could erase the external edit file, and backup. Keep these around ⍝ for now. The new function is in ./fn.APL, the original in ⍝ ./fn.APL.bak ⍝ ⍝ r ← ⍎')host rm ', fn, ' ', fn, '.bak' ∇ ∇ra ← xeda a;fn;s;cmd;h;n;⎕IO ⍝edit array with external editor ⍝.k text-editing ⍝.n fmgw ⍝.t 2015.8.7.20.40.0 ⍝.v 1.0 / 07aug15 ⎕IO ← 1 ⍝ ⍝ Convert array to edit sequence, with lines separated ⍝ by unix newlines. ⍝ s ← ',' make∆csv a s ← ,(s[⍳(⍴s)[1];], g∆nl) ⍝ ⍝ Write out character string representing array to ./ARRAY.csv ⍝ file. ⍝ fn ← g∆tmp, 'ARRAY.csv' h ← 'w' FIO∆fopen fn n ← s FIO∆fwrite_utf8 h r ← FIO∆fclose h ⍝ ⍝ Call external editor on file. Doesn't use a terminal, because CSV ⍝ editor is a GUI application. ⍝ cmd ← g∆editorcsv, fn r ← ⍎')host ', cmd ⍝ ⍝ Read in resulting CSV file. ⍝ ra ← ',' read∆csvfile fn ⍝ ⍝ We could erase the external edit file, and backup. ⍝ ⍝ r ← ⍎')host rm ', fn, ' ', fn, '.bak' ∇