-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFix.aplf
115 lines (101 loc) · 4.39 KB
/
Fix.aplf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{linked}←where Fix src;link;nss;files;file;src;nc;nosrc;fsw;old;ns;name;⎕TRAP;oldname;z;ext
⍝ Fix a function/operator or script, preserving any existing source files
⍝ Used internally by EditorFix "afterfix" processing
⍝ May be called by other tools providing the source in ⎕NR/⎕SRC format on the right
⍝ One day, the left argument of (ns name) may be inferred, for now it must be provided
⍝ Returns 1 if a link was found for the name, else 0
⍝ NB: if 0 is returned, no ⎕FX/⎕FIX was done
:If U.debug=2
(1+⊃⎕LC)⎕STOP⊃⎕SI
:EndIf
:Trap DEBUG↓0
nosrc←0=≢src
(ns name oldname)←3↑where,'' ''
ns←⍎⍣(''≡0⍴∊ns)⊢ns
z←ns←⍬⍴ns
(linked link)←0 ⍬
:If 0≠⎕NC'⎕SE.Link.Links'
:AndIf 0≠≢⎕SE.Link.Links
nss←⍎¨⎕SE.Link.Links.ns
:Repeat
:If ~linked←z∊nss
linked←(z←z⍎'##')∊nss
:EndIf
:Until linked∨z∊# ⎕SE ⎕DMX
:If linked
link←(nss⍳z)⊃⎕SE.Link.Links
:EndIf
:EndIf
:If linked
nc←ns.⎕NC⊂,name
:If 0<≢file←4⊃ns U.GetLinkInfo name ⍝ Already saved in a file
:AndIf name≡oldname ⍝ Function not renamed
file←∊{name}¨@2⍣link.forceFilenames{link U.Tail nc}¨@3⍣link.forceExtensions ⎕NPARTS file
⍝ Interpreter will do everything needed here
:ElseIf ~link.flatten ⍝ Not flattened
file←(≢link.ns)↓⍕ns ⍝ Add sub.namespace structure
((file='.')/file)←'/' ⍝ Convert dots to /
file←link.dir,file ⍝ Add link directory
file,←'/',name
:If link.forceExtensions
:OrIf ~⎕NEXISTS file
ext←link U.Tail nc
:Else
ext←3⊃⎕NPARTS file
:EndIf
file,←ext ⍝ And the object name
:Else ⍝ New name in "flattened" link
⍝ NOT SURE THIS IS RIGHT. SEE ALSO BELOW! ↓↓↓
:If 3=(⍎link.ns).⎕NC link.beforeWrite ⍝ User exit to determine file name?
→(0=≢file←(link.ns⍎link.beforeWrite)ns name)⍴0 ⍝ Return file name or '' to abort
:Else
⎕←'Fix: unable to determine file name for "',name'"'
⍝ /// could use folder browser under Windows
:EndIf
:EndIf
:If nosrc ⍝ Source not provided - find it
:Select ⌊nc
:Case 2
src←ns⍎name
:CaseList 3 4
src←ns.⎕NR name
:Case 9
src←⎕SRC ns⍎name
:Else
⎕←'Fix: unable to handle ',(⍕ns),'.',name,' (⎕NC =',(⍕nc),')'
→0
:EndSelect
:EndIf
:If 2=⎕NC'_rename_temp' ⍝ // workaround for editor overwriting old file on rename
:AndIf (name oldname)≡(1⊃_rename_temp)[6 5]
(3⊃_rename_temp)⎕NPUT(2⊃_rename_temp)1 ⍝ Restore old file contents
⎕EX'_rename_temp' ⍝ Now leesten carefully, for I will do zis only vunce
:EndIf
⍝ NOT SURE THIS IS RIGHT. SEE ALSO ABOVE ↑↑↑
:If 3=link.⎕NC'onWrite'
→((⍎link.onWrite)ns name oldname nc src file)↓0
→(2=⌊nc)⍴0 ⍝ We don't do variables
:EndIf
:Trap 0
:If fsw←0≠⎕NC⊂'link.fsw.Object.EnableRaisingEvents'
old←link.fsw.Object.EnableRaisingEvents
link.fsw.Object.EnableRaisingEvents←0
:EndIf
3 ⎕MKDIR 1⊃⎕NPARTS file ⍝ make sure folder is there
(⊂src)⎕NPUT file 1
:Else
old←0
⎕←'*** Fix: Unable to write "',name,'" to file ',file
⎕←' '⎕DMX
:EndTrap
:If fsw ⋄ link.fsw.Object.EnableRaisingEvents←old ⋄ :EndIf
:Trap 0
2 ns.⎕FIX'file://',file
:Else
⎕←'*** Fix: Unable to re-fix "',name,'" from file ',file
⎕←' '⎕DMX
:EndTrap
:EndIf
:Else
U.Resignal 1
:EndTrap