bug-apl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-apl] Near Proof of concept of an )edit somefunction_name


From: Christian Robert
Subject: Re: [Bug-apl] Near Proof of concept of an )edit somefunction_name
Date: Thu, 22 Dec 2016 22:02:42 -0500
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1

Well, at that time (a long time ago) I didn't actually see the power of what "Kacper 
Gutowski" just sent us !

Here is my version, highly inspired of what Kacper and Alexey wrote. (I love to 
reinvent the wheel)

and in fact it work very well. Sure there is still bugs to fix, improvement to 
do and corners to watch.

thanks to every one of you.

Xtian.


∇skipwrite edit funct;fd;lines;fs_name;z;vi
fs_name←18 ⎕cr funct                                     ⍝ The file name in the 
filesystem
⍝ ---------------------------------------------------------------------------
⍝ Check if it is a request to reedit the previous written file in filesystem
⍝ very usefull in case of a syntax error while ⎕fx 'ing the function ...
⍝ ---------------------------------------------------------------------------
→(2=⎕nc 'skipwrite')/SkipWrite                           ⍝ If a left arg then 
skip write to the file
lines←{(⎕ucs 10),⍨(∼⌽∧\⌽⍵=' ')/⍵} ¨ ⊂[⎕io+1] ⎕cr funct   ⍝ The lines of the 
function
⍝ --------------------------------------------------
⍝ write the contents of this "funct" into the file
⍝ --------------------------------------------------
fd← "w" ⎕FIO[3] fs_name                                  ⍝ Open for writing
→(0>fd)/Error                                            ⍝ Unable to open
⊣ {⍵ ⎕FIO[23] fd} ¨ lines                                ⍝ write all lines
⊣ ⎕FIO[4] fd                                             ⍝ Close the file
⍝ --------------------------------------------------
⍝ Now do the actual edition ...
⍝ --------------------------------------------------
SkipWrite:
z←"vi name;SaveTTY" "SaveTTY←⎕IO⊃⍎')HOST stty -g'" "⊣⍎')HOST >/dev/tty stty 
sane'"
z←z,"⊣⍎')HOST >/dev/tty ','/usr/bin/vi',' ',name" "⊣⍎')HOST >/dev/tty stty 
',SaveTTY"
⊣ ⎕fx z                                                  ⍝ make that local 
function alive
vi fs_name                                               ⍝ the actual editing, 
invoking the editor
⍝
⍝ and try ⎕fx it
⍝
→(0≠"r" ⎕fio[31] fs_name)/Error                          ⍝ Can not read file ? 
→ Error
z←⎕fio[26] fs_name                                       ⍝ First pass, read the 
whole file
lines←⍳+/((↑"\n")=z)                                     ⍝ Compute the iota for 
each line
z←(⍴lines)⍴⍬                                             ⍝ Preallocate "z" to 
the right size
fd←⎕fio[3] fs_name                                       ⍝ Open the file
⊣ {⊣z[⍵]←⊂19 ⎕cr ⎕ucs ¯1↓⎕fio[8] fd} ⍤0 lines            ⍝ Put each line in the 
preallocated "z"
⊣ ⎕fio[4] fd ⋄ →Fix                                      ⍝ Close the file and 
⎕FX it
Error: ⎕ES ∊'Error on file "',fs_name,'": ',⎕fio[2] | ⎕fio[1] ''
Fix: z←⎕fx z ⋄ →((⊂z)≡⊂funct)/0                          ⍝ ⎕fx worked
Problem: 9 ⎕cr "Beware that you can not edit a running function or a suspended function on the 
stack, try )sic before launching \"edit\"."
∊ "Error at line:" (z) ("( Use: 1 edit '" funct "' ) to have a chance to fix 
it.")
∇

On 2016-12-22 18:14, Alexey Veretennikov wrote:
Hi,

Resurrecting the old thread just to say thanks and what this solution
works for me on my Sharp Zaurus C3000, where I don't have enough 
CPU/RAM/keyboard
to run the modern Emacs. I've done couple of changes since I'm on X and
can start vim in the external terminal (separate session of rxvt) as
well as some more error-handlings, but I believe your approach is a way
to go and could be included as an extension to original GNU APL.
Even better if it could be used with some ')ed' command without the
neccessity to create custom command.


Kacper Gutowski <address@hidden> writes:

I recall there being some similar attempts presented on this list but
I couldn't find working example so I hacked up the following.  It has
few rough edges and wasn't tested thoroughly but appears to work.


]USERCMD ]EDIT EDIT⍙ 1

∇S EDIT⍙ Q;FN;FD
 ⍝ Edit a function or operator in external editor.
  ⍎(2>≡S)/"']EDIT NAME' ⋄ →0"
  S←(⎕IO+1)⊃S
  ⍎(~(⎕NC S)∊0 3 4)/"'Bad name ',S ⋄ →0"
  FN←'/tmp/',S,'.',⍕↑⎕AI
  FD←'w'⎕FIO[3] 18⎕CR FN
  ⍎(0>FD)/"'Can''t open file ',FN ⋄ →0"
  Q←(⎕IO+0≠⎕NC S)⊃(⍉⍪S)(⎕CR S)
  Q←{⍵↓⍨-+/∧\⌽' '=⍵}¨⊂[⎕IO+1]Q
  Q←∊Q,¨⊂"\n"
  ⊣ Q ⎕FIO[23]FD
  ⊣ ⎕FIO[4]FD
  EDIT⍙OPEN FN
  Q←⎕FIO[26] 18⎕CR FN
  ⊣ ⎕FIO[19] 18⎕CR FN
  ⍎(~' '≡↑0⍴Q)/"'Filed to read ',FN ⋄ →0"
  ⍎(0=⍴Q)/"'Empty' ⋄ →0"
  ⍎(0=⍴Q←19⎕CR Q)/"'Encoding error' ⋄ →0"
  ⎕FX (~"\n"=Q)⊂Q
∇

⍝ External editor to use; defaults to $EDITOR or vi
EDIT⍙EDITOR←,⎕ENV'EDITOR'
EDIT⍙EDITOR←(1+EDIT⍙EDITOR⍳⊂'EDITOR')⊃EDIT⍙EDITOR,⍬'vi'

∇EDIT⍙OPEN FILE;STTY
 ⍝ Open a file in external editor.
  ((''''=FILE)/FILE)←⊂"'\''"
  FILE←'''',∊FILE,''''
  STTY←⎕IO⊃⍎')HOST stty -g'
  ⊣⍎')HOST >/dev/tty stty sane'
  ⊣⍎')HOST >/dev/tty ',EDIT⍙EDITOR,' ',FILE
  ⊣⍎')HOST >/dev/tty stty ',STTY
∇


reply via email to

[Prev in Thread] Current Thread [Next in Thread]