-
Notifications
You must be signed in to change notification settings - Fork 3
/
Zigzag.pm
1673 lines (1499 loc) · 44.8 KB
/
Zigzag.pm
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Xanadu(R) Zigzag(tm) Hyperstructure Kit, $Revision: 0.70 $
#
# Designed by Ted Nelson
# Programmed by Andrew Pam ("xanni") and Bek Oberin ("gossamer")
# Copyright (c) 1997-1999 Project Xanadu
#
# This is only a partial implementation, with only a few structure and view
# operations; however, they are enough to allow you to create, view and explore
# complex multidimensional structures in quantum hyperspace.
#
# A forthcoming tutorial will present you with strange spaces to explore,
# beginning with easy 2D concepts, the peculiar geography of this system and
# operating recommendations for getting around in it comfortably and changing
# your structures without losing cells or losing track of your stuff. (This
# will emphasize pragmatic adaptations to the peculiarities of this limited
# implementation.)
#
# Forthcoming documentation will explain the space, the theory, structure
# operations, view operations, and the official planned extensions.
#
# For more information, visit http://www.xanadu.net/zz/
#
# This file "Zigzag.pm" contains the implementation of the Zigzag data
# structures and operations. User interfaces for interactive manipulation of
# the Zigzag data structures can be created using the operations defined in
# this file. Example interfaces include "zigzag" (a text-based interface using
# Curses) and "zizl" (a web-based interface using HTML and HTTP).
#
# ===================== Change Log
#
# Inital Zigzag implementation
# $Id: Zigzag.pm,v 0.70 1999/05/14 13:43:21 xanni Exp $
#
# $Log: Zigzag.pm,v $
# Revision 0.70 1999/05/14 13:43:21 xanni
# * Imported atcursor_edit from front end
# * Implemented dimension_home()
# * Minor fix to add_contents()
# * Replaced $DB_Ref and %ZZ globals with @Filename, @DB_Ref and @Hash_Ref
# * Implemented cell_slice()
# * Replaced direct access to %ZZ with cell_get(), cell_set() and cell_nbr()
# * Replaced db_open(), db_close() and db_sync() with
# slice_open(), slice_close(), slice_close_all() and slice_sync_all()
# * Renamed db_upgrade() to slice_upgrade()
#
# Revision 0.69 1999/05/09 12:19:53 xanni
# Fixed view_rotate() to handle hidden dimensions, rewrote get_contained()
#
# Revision 0.68 1999/03/13 13:05:04 xanni
# Implemented is_essential(), cell_find() and dimension_rename()
# Implemented dimension_find() to replace dimension_exists()
# atcursor_delete() now checks for essential cells and dimensions
# Improved db_upgrade(), miscellaneous minor cleanup
#
# Revision 0.67 1999/03/13 05:01:31 xanni
# Minor naming fixes, moved cell_create() to front-end
# Made recycle pile a circular queue, reused oldest cell first
# Implemented db_upgrade(), dimension_exists() and cell_new()
#
# Revision 0.66 1999/01/07 07:15:30 xanni
# Minor fixes to db_open()
# Replaced window_draw_* functions with new layout_* functions
#
# Revision 0.65 1999/01/07 04:04:52 xanni
# Derived from the monolithic zigzag 0.64
#
# Revision 0.64 1999/01/07 03:55:11 xanni
# Minor cosmetic changes
#
# Revision 0.63 1999/01/06 05:07:14 xanni
# Created display_dirty() and generally fixed up dirty handling
# Minor cleanup of error handling and Meta support
#
# Revision 0.62 1998/11/23 13:59:55 xanni
# Renamed the "Midden" cell to "Recycle pile", fixed Meta (aka Alt) support
#
# Revision 0.61 1998/11/21 16:46:33 xanni
# Defined $Hcells and $Vcells, fixed $Cell_Width bug,
# created display_draw_link_* functions, finished making
# window_draw_* functions independent of the UI toolkit.
#
# Revision 0.60 1998/11/21 08:24:21 xanni
# Introduced window_draw_preview, cleaned up display_draw_quad
#
# Older revisions are listed in the CHANGES file
#
package Zigzag;
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw
(
$Command_Count $Hcells $Vcells $Input_Buffer
slice_open
slice_close
slice_close_all
slice_sync_all
reverse_sign
wordbreak
is_cursor
is_clone
is_selected
is_active_selected
get_accursed
get_active_selection
get_selection
get_which_selection
get_lastcell
get_distance
get_outline_parent
get_cell_contents
get_cursor
get_dimension
get_contained
get_links_to
do_shear
cell_get
cell_set
cell_nbr
cell_insert
cell_new
cursor_move_dimension
cursor_jump
cursor_move_direction
atcursor_execute
atcursor_clone
atcursor_copy
atcursor_select
rotate_selection
push_selection
atcursor_insert
atcursor_delete
atcursor_hop
atcursor_shear
atcursor_make_link
atcursor_break_link
cells_row
layout_preview
layout_Iraster
layout_Hraster
view_quadrant_toggle
view_raster_toggle
view_reset
view_rotate
view_flip
);
@EXPORT_OK = qw(link_make link_break is_essential cell_excise cell_find
dimension_find dimension_home dimension_is_essential);
use integer;
use strict;
use POSIX;
use DB_File;
#use Fcntl;
use File::Copy;
# Import functions
*atcursor_edit = *::atcursor_edit;
*display_dirty = *::display_dirty;
*display_status_draw = *::display_status_draw;
*user_error = *::user_error;
# Note: We are using the following coding conventions:
# Constants are named in ALLCAPS
# Global variables are named with Initial Caps
# Local variables and functions are named in lowercase
# Put brackets around all function arguments
# Prototype the number and type of arguments expected by functions
# Matching braces should line up with each other vertically
# Use the $TRUE and $FALSE constants instead of "1" and "0"
# Define constants
use vars qw($VERSION);
#($VERSION) = q$Revision: 0.70 $ =~ /([\d\.]+)/;
$VERSION = do { my @r = (q$Revision: 0.70 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
my $FALSE = 0;
my $TRUE = !$FALSE;
my $CURSOR_HOME = 10; # NOTE! This assumes it stays fixed!
my $SELECT_HOME = 21; # NOTE! This assumes it stays fixed!
my $DELETE_HOME = 99; # NOTE! This assumes it stays fixed!
my $FILENAME = "zigzag.zz"; # Default filename for initial slice
my $ZZMAIL_SUPPORT = $FALSE; # Enable preliminary ZZmail support
#my $COMMAND_SYNC_COUNT = 20; # Sync the DB after this many commands
my $BACKUP_FILE_SUFFIX = ".bak";
# Declare globals
use vars qw($Command_Count $Hcells $Vcells $Input_Buffer);
#my $Input_Buffer; # Cell number entered from the keyboard
#my $Command_Count; # Counts commands between DB syncs
my @Filename; # Array of filenames
my @DB_Ref; # Array of database references
my @Hash_Ref; # Array of hash references
#my $Hcells; # Horizontal cells per window
#my $Vcells; # Vertical cells per window
sub initial_geometry()
{
return (
0 => "Home",
"0-d.1" => 99,
"0+d.2" => 30,
"0+d.cursor" => 11,
1 => "d.1",
"1-d.1" => 10,
"1+d.1" => 99,
"1-d.2" => 8,
"1+d.2" => 2,
2 => "d.2",
"2-d.2" => 1,
"2+d.2" => 3,
3 => "d.3",
"3-d.2" => 2,
"3+d.2" => 4,
4 => "d.inside",
"4-d.2" => 3,
"4+d.2" => 5,
5 => "d.contents",
"5-d.2" => 4,
"5+d.2" => 6,
6 => "d.mark",
"6-d.2" => 5,
"6+d.2" => 7,
7 => "d.clone",
"7-d.2" => 6,
"7+d.2" => 8,
8 => "d.cursor",
"8-d.2" => 7,
"8+d.2" => 1,
10 => "Cursor home",
"10+d.1" => 1,
"10+d.2" => 11,
"10-d.1" => 21,
11 => "Menu",
"11+d.1" => 12,
"11-d.2" => 10,
"11+d.2" => 16,
"11-d.cursor" => 0,
"11+d.cursor" => 16,
12 => "+d.1",
"12-d.1" => 11,
"12+d.1" => 13,
13 => "+d.2",
"13-d.1" => 12,
"13+d.1" => 14,
14 => "+d.3",
"14-d.1" => 13,
"14+d.1" => 15,
15 => "I",
"15-d.1" => 14,
16 => "Event",
"16+d.1" => 17,
"16-d.2" => 11,
"16-d.cursor" => 11,
17 => "+d.1",
"17-d.1" => 16,
"17+d.1" => 18,
18 => "+d.2",
"18-d.1" => 17,
"18+d.1" => 19,
19 => "+d.3",
"19-d.1" => 18,
"19+d.1" => 20,
20 => "I",
"20-d.1" => 19,
21 => "Selection",
"21+d.1" => 10,
"21+d.2" => 21,
"21-d.2" => 21,
30 => "#Edit\natcursor_edit(1);",
"30+d.1" => 35,
"30-d.2" => 0,
"30+d.2" => 40,
35 => "#Clone\natcursor_clone(1);",
"35-d.1" => 30,
40 => "#L-ins\natcursor_insert(1, 'L');",
"40+d.1" => 41,
"40-d.2" => 30,
"40+d.2" => 50,
41 => "#R-ins\natcursor_insert(1, 'R');",
"41-d.1" => 40,
"41+d.1" => 42,
42 => "#U-ins\natcursor_insert(1, 'U');",
"42-d.1" => 41,
"42+d.1" => 43,
43 => "#D-ins\natcursor_insert(1, 'D');",
"43-d.1" => 42,
"43+d.1" => 44,
44 => "#I-ins\natcursor_insert(1, 'I');",
"44-d.1" => 43,
"44+d.1" => 45,
45 => "#O-ins\natcursor_insert(1, 'O');",
"45-d.1" => 44,
50 => "#Delete\natcursor_delete(1);",
"50+d.1" => 51,
"50-d.2" => 40,
"50+d.2" => 60,
51 => "#L-break\natcursor_break_link(1, 'L');",
"51-d.1" => 50,
"51+d.1" => 52,
52 => "#R-break\natcursor_break_link(1, 'R');",
"52-d.1" => 51,
"52+d.1" => 53,
53 => "#U-break\natcursor_break_link(1, 'U');",
"53-d.1" => 52,
"53+d.1" => 54,
54 => "#D-break\natcursor_break_link(1, 'D');",
"54-d.1" => 53,
"54+d.1" => 55,
55 => "#I-break\natcursor_break_link(1, 'I');",
"55-d.1" => 54,
"55+d.1" => 56,
56 => "#O-break\natcursor_break_link(1, 'O');",
"56-d.1" => 55,
60 => "#Select\natcursor_select(1);",
"60-d.2" => 50,
"60+d.2" => 70,
"60+d.1" => 61,
61 => "#Rot.Selection\nrotate_selection();",
"61-d.1" => 60,
"61+d.1" => 62,
62 => "#Push Selection\npush_selection();",
"62-d.1" => 61,
70 => "#L-Hop\natcursor_hop(1, 'L');",
"70+d.1" => 71,
"70-d.2" => 60,
"70+d.2" => 80,
71 => "#R-Hop\natcursor_hop(1, 'R');",
"71-d.1" => 70,
"71+d.1" => 72,
72 => "#U-Hop\natcursor_hop(1, 'U');",
"72-d.1" => 71,
"72+d.1" => 73,
73 => "#D-Hop\natcursor_hop(1, 'D');",
"73-d.1" => 72,
"73+d.1" => 74,
74 => "#I-Hop\natcursor_hop(1, 'I');",
"74-d.1" => 73,
"74+d.1" => 75,
75 => "#O-Hop\natcursor_hop(1, 'O');",
"75-d.1" => 74,
80 => "#Shear -^\natcursor_shear(1, 'D', 'L')",
"80-d.2" => 70,
"80+d.2" => 85,
"80+d.1" => 81,
81 => "#Shear -v\natcursor_shear(1, 'U', 'L')",
"81-d.1" => 80,
"81+d.1" => 82,
82 => "#Shear ^+\natcursor_shear(1, 'D', 'R')",
"82-d.1" => 81,
"82+d.1" => 83,
83 => "#Shear v+\natcursor_shear(1, 'U', 'R')",
"83-d.1" => 82,
85 => "#Chug",
"85-d.2" => 80,
"85+d.2" => 90,
90 => "#A-View toggle\nview_raster_toggle(0);",
"90+d.1" => 91,
"90-d.2" => 85,
"90+d.2" => 93,
91 => "#D-View toggle\nview_raster_toggle(1);",
"91-d.1" => 90,
"91+d.1" => 92,
92 => "#Quad view toggle\nview_quadrant_toggle(1);",
"92-d.1" => 91,
93 => "#X-rotate view\nview_rotate(1, 'X');",
"93+d.1" => 94,
"93-d.2" => 90,
"93+d.2" => 96,
94 => "#Y-rotate view\nview_rotate(1, 'Y');",
"94-d.1" => 93,
"94+d.1" => 95,
95 => "#Z-rotate view\nview_rotate(1, 'Z');",
"95-d.1" => 94,
96 => "#X-flip view\nview_flip(1, 'X');",
"96+d.1" => 97,
"96-d.2" => 93,
97 => "#Y-flip view\nview_flip(1, 'Y');",
"97-d.1" => 96,
"97+d.1" => 98,
98 => "#Z-flip view\nview_flip(1, 'Z');",
"98-d.1" => 97,
99 => "Recycle pile",
"99-d.1" => 1,
"99+d.1" => 0,
"99-d.2" => 99,
"99+d.2" => 99,
"n" => 100
);
}
sub slice_upgrade()
# Perform any upgrades necessary to maintain backward compatibility
# with old home slices
{
# Earlier than v0.44.1.1 not presently supported due to
# massive dimension renaming
die "Sorry, this data file predates Zigzag v0.44.1.1.\n"
unless dimension_find("d.1");
# Change to current dimension names (from v0.50)
dimension_rename("d.Cursor", "d.cursor");
dimension_rename("d.Clone", "d.clone");
dimension_rename("d.Mark", "d.mark");
dimension_rename("d.Contain", "d.inside");
dimension_rename("d.Contain2", "d.contents");
dimension_rename("d.contentlist", "d.contents");
dimension_rename("d.contain", "d.inside");
dimension_rename("d.containment", "d.inside");
# Make sure $SELECT_HOME exists (from v0.57)
if (not defined cell_get($SELECT_HOME))
{
cell_set($SELECT_HOME, "Selection");
link_make($SELECT_HOME, $SELECT_HOME, "+d.2");
cell_insert($SELECT_HOME, $CURSOR_HOME, "-d.1");
}
# Rename the "Midden" to the "Recycle pile" (from v0.62)
cell_set($DELETE_HOME, "Recycle pile") if cell_get($DELETE_HOME) eq "Midden";
# Make sure recycle pile is a circular queue (from v0.67)
my $first = get_lastcell($DELETE_HOME, "-d.2");
link_make($first, get_lastcell($DELETE_HOME, "+d.2"), "-d.2")
unless defined cell_nbr($first, "-d.2");
}
sub slice_open(;$)
{
# If there's a parameter, use it as the filename
my %hash;
my $DB_Ref;
my $Filename = shift;
# There's a default filename for the first (home) slice
$Filename = $FILENAME if (not defined $Filename) and ($#Filename < 0);
if (-e $Filename)
{
# we have an existing datafile - back it up!
move($Filename, $Filename . $BACKUP_FILE_SUFFIX)
|| die "Can't rename data file \"$Filename\": $!\n";
copy($Filename . $BACKUP_FILE_SUFFIX, $Filename)
|| die "Can't copy data file \"$Filename\": $!\n";
$DB_Ref = tie %hash, 'DB_File', $Filename, O_RDWR
|| die "Can't open data file \"$Filename\": $!\n";
push @Hash_Ref, \%hash;
slice_upgrade() if $#DB_Ref < 0;
}
else
{
# no initial data file
$DB_Ref = tie %hash, 'DB_File', $Filename, O_RDWR | O_CREAT
|| die "Can't create data file \"$Filename\": $!\n";
# resort to initial geometry for first (home) slice
%hash = initial_geometry() if $#Hash_Ref < 0;
push @Hash_Ref, \%hash;
}
push @Filename, $Filename;
push @DB_Ref, $DB_Ref;
}
sub slice_close($)
{
my $num = shift;
undef $DB_Ref[$num];
untie %{$Hash_Ref[$num]};
splice @Filename, $num, 1;
splice @DB_Ref, $num, 1;
splice @Hash_Ref, $num, 1;
}
sub slice_close_all()
{
my $DB_Ref;
while (defined ($DB_Ref = pop @DB_Ref))
{
pop @Filename;
undef $DB_Ref;
untie %{pop @Hash_Ref};
}
}
sub slice_sync_all()
{
foreach (@DB_Ref)
{ $_->sync(); }
}
#
# Some helper functions
#
sub reverse_sign($)
# Reverse the sign of the given cursor/dimension
{
return ((substr($_[0], 0, 1) eq "+") ? "-" : "+") . substr($_[0], 1);
}
sub wordbreak($$)
# Returns a string up to the first line break or the end of the last word
# that finishes before the given character position
{
$_ = substr($_[0], 0, $_[1]);
if (/^(.*)\n/)
{ $_ = "$1 "; }
elsif ((length eq $_[1]) && /^(.+)\s+\S*$/)
{ $_ = $1; }
return $_;
}
#
# Testing cell type
# Named is_*
#
sub is_cursor($)
{
my $cell = shift;
return (defined(cell_nbr($cell, "-d.cursor")) ||
defined(cell_nbr($cell, "+d.cursor")));
}
sub is_clone($)
{
my $cell = shift;
return (defined(cell_nbr($cell, "-d.clone")) ||
defined(cell_nbr($cell, "d.clone")));
}
sub is_selected($)
{
my $cell = shift;
my $headcell = get_lastcell($cell, "-d.mark");
return $headcell != $cell
&& defined get_distance($headcell, "+d.2", $SELECT_HOME);
}
sub is_active_selected($)
{
my $cell = shift;
my $headcell = get_lastcell($cell, "-d.mark");
return $headcell == $SELECT_HOME && $headcell != $cell;
}
sub is_essential($)
{
my $cell = shift;
return (($cell == 0) or ($cell == $CURSOR_HOME) or
($cell == $DELETE_HOME) or ($cell == $SELECT_HOME));
}
sub dimension_is_essential($)
{
return $_[0] =~ /^[+-]?d\.(1|2|cursor|clone|inside|contents|mark)$/;
}
sub dimension_home()
{
# Dimension list is +d.1 from Cursor home
return cell_nbr($CURSOR_HOME, "+d.1");
}
sub dimension_find($)
{
return cell_find(dimension_home(), "+d.2", $_[0]);
}
sub dimension_rename($$)
# Rename an entire dimension. Warning - traverses all cells!
{
my ($d_orig, $d_new) = @_;
my $cell = dimension_find($d_orig);
if ($cell and not dimension_find($d_new))
{
print STDERR "Renaming dimension $d_orig to $d_new. Please wait...";
cell_set($cell, $d_new);
foreach (@Hash_Ref)
{
while (my ($key, $value) = each %$_)
{ $key =~ s/$d_orig$/$d_new/; }
}
print STDERR "done.\n";
}
}
#
# Retrieving Information
# Named get_*
#
sub get_accursed($)
# Get the cell that is accursed in the specified window
# or by the specified cursor
{
my $n = shift;
get_lastcell(get_cursor($n), "-d.cursor");
}
sub get_active_selection()
{
get_selection(0);
}
sub get_selection($)
# Get the $nth selection and return a list of cells
{
my ($n) = @_;
my $sel;
for ($sel = $SELECT_HOME; $n && defined($sel); $n--)
{ $sel = cell_nbr($sel, "+d.2"); }
cells_row(cell_nbr($sel, "+d.mark"), "+d.mark");
}
sub get_which_selection($)
# Given a cell, get the headcell of the selection it is in, or undef
# if it is not part of a selection
{
my $cell = shift;
return undef unless defined cell_nbr($cell, "-d.mark");
get_lastcell($cell, "-d.mark");
}
sub get_lastcell($$)
# Find the last cell in a given direction
{
my ($cell, $dir) = @_;
die "No cell $cell" unless defined(cell_get($cell));
die "Invalid direction $dir" unless ($dir =~ /^[+-]/);
# Follow links to the end or until we return to where we started
$cell = $_ while defined($_ = cell_nbr($cell, $dir)) && ($_ != $_[0]);
return $cell;
}
sub get_distance($$$)
# Given cell A a direction, and cell B,
# find out how far B is from A in the specified direction.
# return undef if B cannot be reached from A in that direction
{
my ($start, $dir, $end) = @_;
my $cell;
return 0 if $start == $end;
my $dist = 1;
for ($cell = cell_nbr($start, $dir);
defined $cell && $cell != $end && $cell != $start;
$cell = cell_nbr($cell, $dir)
)
{ $dist++; }
return undef if !defined($cell) || $cell == $start;
return $dist;
}
sub get_outline_parent($)
# Find the "outline parent" (first cell -d.1 along -d.2) of a cell
{
my $cell = $_[0];
die "No cell $cell" unless defined(cell_get($cell));
# Move -d.2 until we find a -d.1 link or return to where we started
$cell = $_ while (!defined(cell_nbr($cell, "-d.1")) &&
defined($_ = cell_nbr($cell, "-d.2")) && ($_ != $_[0]));
$cell = $_ if defined($_ = cell_nbr($cell, "-d.1"));
return $cell;
}
sub get_cell_contents($)
# Return the contents of a cell
{
my $cell = $_[0];
die "No cell $cell" unless defined(cell_get($cell));
my $contents = cell_get(get_lastcell($cell, "-d.clone"));
if ($ZZMAIL_SUPPORT && $contents =~ /^\[(\d+)\+(\d+)\]/)
# Note 1: This should handle pointer lists, but currently only handles
# the first pointer.
# Note 2: This performs extremely badly for large primedia. It should
# make requests to an OSMIC server instead.
{
my $pos = $1;
my $len = $2;
my $PRIMEDIA = cell_get(get_outline_parent($cell));
if (open(PRIMEDIA, "<$PRIMEDIA"))
{
my $error = $FALSE;
seek(PRIMEDIA, $pos, SEEK_SET) || ($error = "seeking");
read(PRIMEDIA, $contents, $len) == $len || ($error = "reading");
close PRIMEDIA;
die "Error $error $PRIMEDIA" if $error;
}
}
return $contents;
}
sub get_cursor($)
# Return the given cursor
{
my $number = $_[0];
my $cell = $CURSOR_HOME;
# Count to the numbered cursor
for ($_ = 0; defined($cell) && ($_ <= $number); $_++)
{ $cell = cell_nbr($cell, "+d.2"); }
die "No cursor $number" unless defined($cell);
return $cell;
}
sub get_dimension($$)
# Get the dimension for a given cursor and direction
# Requires that each cursor have the current screen X, Y and Z axis
# dimension mappings linked +d.1 from the cursor cell
{
my ($curs, $dir) = @_;
die "Invalid direction $dir" unless ($dir =~ /^[LRUDIO]$/);
$curs = cell_nbr($curs, "+d.1");
if ($dir eq "L")
{ return reverse_sign(cell_get($curs)); }
if ($dir eq "R")
{ return cell_get($curs); }
$curs = cell_nbr($curs, "+d.1");
if ($dir eq "U")
{ return reverse_sign(cell_get($curs)); }
if ($dir eq "D")
{ return cell_get($curs); }
$curs = cell_nbr($curs, "+d.1");
if ($dir eq "I")
{ return reverse_sign(cell_get($curs)); }
if ($dir eq "O")
{ return cell_get($curs); }
}
sub add_contents($$$)
# Add list of cells "contained" within a cell to a referenced list and hash
{
my ($start, $listref, $hashref) = @_;
push @$listref, $start;
$hashref->{$start} = $TRUE;
my $cell = cell_nbr($start, "+d.inside");
while (defined $cell and not defined $hashref->{$cell})
{
push @$listref, $cell;
$hashref->{$cell} = $TRUE;
my $index = cell_nbr($cell, "+d.contents");
while (defined $index and
not defined $hashref->{$index} and
not defined cell_nbr($index, "-d.inside"))
{
add_contents($index, $listref, $hashref);
$index = cell_nbr($index, "+d.contents");
}
$cell = cell_nbr($cell, "+d.inside");
}
}
sub get_contained($)
# Return list of cells "contained" within a cell
{
my @list;
my %hash;
add_contents($_[0], \@list, \%hash);
return @list;
}
sub get_links_to($)
# Returns a list of all the links back to the specified cell
{
my $cell = $_[0];
my $index = dimension_home();
my @result;
do
{
my $dim = cell_get($index);
push @result, cell_nbr($cell, "-$dim") . "+$dim"
if defined cell_nbr($cell, "-$dim");
push @result, cell_nbr($cell, "+$dim") . "-$dim"
if defined cell_nbr($cell, "+$dim");
$index = cell_nbr($index, "+d.2");
die "Dimension list broken" unless defined $index;
} until $index == dimension_home();
return @result;
}
#
# Multiple-cell operations
# Named do_*
#
sub do_shear($$$;$$)
# Given a row of cells starting at $first_cell,
# move them all $n cells in the $dir direction.
# Cells that were linked in the $link direction
# have their links broken and relinked to new cells.
#
# Before: do_shear(A1, d.1, d.2, 1);
#
# ---> d.1
# V d.2
#
# A1 --- B1 --- C1 --- D1 --- E1
# | | | |
# A2 --- B2 --- C2 --- D2 --- E2
#
# After:
#
# A1 --- B1 --- C1 --- D1 --- E1
# | | |
# A2 --- B2 --- C2 --- D2 --- E2
#
# Optional fourth argument $n defaults to 1.
# Optional fifth argument $hang says whether the cell on the end
# should be linked back at the beginning or whether it should just
# be left hanging. $hang = false: Back at beginning.
# $hang = true: Leave hanging. Default: false.
{
my ($first_cell, $dir, $link, $n, $hang) = @_;
$n = 1 unless defined $n;
$hang = $FALSE unless defined $hang;
my $cell;
my ($prev_cell, $prev_linked);
my $first_linked = cell_nbr($first_cell, $link);
my @shear_cells = cells_row($first_cell, $dir);
my @linked_cells = map {cell_nbr($_, $link)} @shear_cells;
my @new_link = @linked_cells;
# Move some of these from the beginning
my @x = splice(@new_link, 0, $n);
# And put them back at the end.
push @new_link, @x;
my $i;
my $linkno = 0;
my $last_linked;
# Break all the links
for ($i=0; $i < @shear_cells; $i++)
{
my $old_link = $linked_cells[$i];
next unless defined $old_link;
my $shear_cell = $shear_cells[$i];
link_break($shear_cell, $old_link, $link);
}
$linkno = 0;
for ($i=0; $i < @shear_cells; $i++)
{
my $new_link = $new_link[$i];
next unless defined $new_link;
next if $i == $#shear_cells && $hang;
my $shear_cell = $shear_cells[$i];
link_make($shear_cell, $new_link, $link);
}
display_dirty();
}
#
# Functions that operate on links between cells
# Named link_*
#
sub link_break($$;$)
# Break a link between two cells in a given direction.
# To ensure consistency, this should be the only way links are ever broken
# Second argument is optional. If present, it must be linked from cell 1
# in the approprate dimension.
{
my ($cell1, $cell2, $dir, $slice);
if (@_ == 3)
{
($cell1, $cell2, $dir) = @_;
die "Invalid direction $dir" unless ($dir =~ /^[+-]/);
$slice = cell_slice("$cell1$dir");
die "$cell1 has no link in direction $dir" unless defined $slice;
die "$cell1 is not linked to $cell2 in direction $dir"
unless $cell2 == $Hash_Ref[$slice]{"$cell1$dir"};
}
else
{
($cell1, $dir) = @_;
die "Invalid direction $dir" unless ($dir =~ /^[+-]/);
$slice = cell_slice("$cell1$dir");
die "$cell1 has no link in direction $dir" unless defined $slice;
$cell2 = $Hash_Ref[$slice]{"$cell1$dir"}; # Infer second argument
}
die "No cell $cell1" unless defined cell_get($cell1);
die "No cell $cell2" unless defined cell_get($cell2);
delete $Hash_Ref[$slice]{"$cell1$dir"};
$dir = reverse_sign($dir);
delete $Hash_Ref[cell_slice("$cell2$dir")]{"$cell2$dir"};
}
sub link_make($$$)
# Make a link between two cells in a given direction.
# To ensure consistency, this should be the only way links are ever made
{
my ($cell1, $cell2, $dir) = @_;
die "No cell $cell1" unless defined cell_get($cell1);
die "No cell $cell2" unless defined cell_get($cell2);
die "Invalid direction $dir" unless ($dir =~ /^[+-]/);
my $back = reverse_sign($dir);
die "$cell1 already linked" if defined cell_nbr($cell1, $dir);
die "$cell2 already linked" if defined cell_nbr($cell2, $back);
$Hash_Ref[cell_slice($cell1)]{"$cell1$dir"} = $cell2;
$Hash_Ref[cell_slice($cell2)]{"$cell2$back"} = $cell1;
}
#
# Functions that operate on individual cells
# Named cell_*
#
sub cell_slice($)
# Returns the slice in which a cell resides
{
my $slice = 0;
my $found;
do
{ $found = defined $Hash_Ref[$slice++]{$_[0]}; }
until ($found or $slice > $#Hash_Ref);
return $found ? $slice - 1 : undef;
}
sub cell_get($)
# Retrieve cell contents
{
my $slice = cell_slice($_[0]);
return (defined $slice) ? $Hash_Ref[$slice]{$_[0]} : undef;
}
sub cell_set($$)
# Set cell contents
{
my $slice = cell_slice($_[0]);
die "No cell $_[0]" unless defined $slice;
$Hash_Ref[$slice]{$_[0]} = $_[1];
}
sub cell_nbr($$)
# Follow link from cell
{
my $slice = cell_slice("$_[0]$_[1]");
return (defined $slice) ? $Hash_Ref[$slice]{"$_[0]$_[1]"} : undef;
}
sub cell_insert($$$)
# Insert a cell next to another cell in a given direction
#
# Original state New state
# -------------- -------------
# $cell1---next
# $cell2---$cell3 $cell2---$cell1---($cell3 or next)
{
my ($cell1, $cell2, $dir) = @_;
die "No cell $cell1" unless defined(cell_get($cell1));
die "No cell $cell2" unless defined(cell_get($cell2));
die "Invalid direction $dir" unless ($dir =~ /^[+-]/);
my $cell3 = cell_nbr($cell2, $dir);
# Can't insert if $cell1 has inappropriate neighbours
if (defined(cell_nbr($cell1, reverse_sign($dir))) ||
((defined(cell_nbr($cell1, $dir)) && defined($cell3))))
{ user_error(2, "$cell1 $dir $cell2"); }
else
{
if (defined($cell3))
{
link_break($cell2, $cell3, $dir);
link_make($cell1, $cell3, $dir);
}
link_make($cell2, $cell1, $dir);
}
}
sub cell_find($$$)
# From a starting cell, travel a given direction to find given cell contents
{
my ($start, $dir, $contents) = @_;
die "Invalid direction $dir" unless ($dir =~ /^[+-]/);
my $cell = $start;
my $found = $FALSE;
# Follow links until we find a match or return to where we started
do
{
$found = $cell if (defined $cell) and (cell_get($cell) eq $contents);