summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
0 files changed, 0 insertions, 0 deletions
c4e2de9cd8cad7e28b33e68b1b76f9044fe510be'>^
a6bbcd0d ^

08a758ed ^

896489a6 ^


a6bbcd0d ^

c4e2de9c ^
4e0fd8c7 ^
eb2e57c9 ^
08a758ed ^
c0ccab92 ^
08a758ed ^
7d6c75e4 ^
eb2e57c9 ^
896489a6 ^
aaacdc35 ^
c4e2de9c ^
08a758ed ^
896489a6 ^
652251c2 ^
d9e430c8 ^
896489a6 ^
08a758ed ^

c4e2de9c ^

a6bbcd0d ^

7eee2e1f ^
c4e2de9c ^

c0ccab92 ^


7eee2e1f ^
3d6767ae ^
aaacdc35 ^
65307235 ^
8323cf21 ^
08a758ed ^
896489a6 ^
a6bbcd0d ^


0cfe4091 ^
c4e2de9c ^
08a758ed ^
a6bbcd0d ^
08a758ed ^
aaacdc35 ^
f3f13da8 ^
896489a6 ^
aaacdc35 ^
a6bbcd0d ^
941f7e43 ^
7d6c75e4 ^
d7a05e7a ^
6c4b7671 ^
d9e430c8 ^

6c4b7671 ^

639e56e3 ^
5be29f67 ^


5d8500c7 ^
5be29f67 ^











b00b6a1e ^
5be29f67 ^


3a12afa7 ^
b00b6a1e ^
5be29f67 ^
639e56e3 ^
941f7e43 ^
c4e2de9c ^
7eee2e1f ^






















c4e2de9c ^
08a758ed ^


4e0fd8c7 ^
08a758ed ^
4e0fd8c7 ^
08a758ed ^



4e0fd8c7 ^
08a758ed ^
4e0fd8c7 ^
08a758ed ^
d7a05e7a ^






a6bbcd0d ^
08a758ed ^
a6bbcd0d ^

08a758ed ^
a6bbcd0d ^
d7a05e7a ^
08a758ed ^
a6bbcd0d ^
d7a05e7a ^
a6bbcd0d ^


d7a05e7a ^
c4e2de9c ^
a6bbcd0d ^

d7a05e7a ^
a6bbcd0d ^
7eee2e1f ^







652251c2 ^
896489a6 ^
652251c2 ^
652251c2 ^
aaacdc35 ^
6c4b7671 ^
7eee2e1f ^


652251c2 ^




3d6767ae ^
7d6c75e4 ^
aaacdc35 ^

dcc5e065 ^
aaacdc35 ^
7d6c75e4 ^

aaacdc35 ^
a6bbcd0d ^

940e6516 ^
a6bbcd0d ^
652251c2 ^

aaacdc35 ^
a6bbcd0d ^


940e6516 ^
a6bbcd0d ^

652251c2 ^
a6bbcd0d ^

65b0b48f ^
940e6516 ^
a6bbcd0d ^













9a22a844 ^

7d6c75e4 ^
a6bbcd0d ^



a6bbcd0d ^

913fabdc ^








986522a9 ^
7d6c75e4 ^
986522a9 ^

7d6c75e4 ^
986522a9 ^
bbabb695 ^
4e0fd8c7 ^
986522a9 ^
a6bbcd0d ^

c0ccab92 ^






230ad947 ^
a6bbcd0d ^
230ad947 ^


a6bbcd0d ^
c0ccab92 ^

b31c6a20 ^
c0ccab92 ^
aaacdc35 ^
65b0b48f ^
c0ccab92 ^

230ad947 ^
c0ccab92 ^


a6bbcd0d ^


aaacdc35 ^
3a12afa7 ^
c0ccab92 ^
aaacdc35 ^
9edd4365 ^
aaacdc35 ^

65307235 ^
aaacdc35 ^


c0ccab92 ^

65307235 ^
a316ae24 ^
a6bbcd0d ^
65307235 ^
c0ccab92 ^




65307235 ^
c0ccab92 ^
230ad947 ^
c0ccab92 ^

a6bbcd0d ^
65307235 ^




7d6c75e4 ^

609e628a ^
7d6c75e4 ^



26e8968a ^
7d6c75e4 ^



7d6c75e4 ^




26e8968a ^
7d6c75e4 ^
26e8968a ^

609e628a ^
7d6c75e4 ^
c4e2de9c ^
a6bbcd0d ^
941f7e43 ^
81f800cb ^
941f7e43 ^

1b9c56ac ^
7d6c75e4 ^
b1794c09 ^
7d6c75e4 ^
aaacdc35 ^
c0ccab92 ^


aaacdc35 ^



c0ccab92 ^


d9e430c8 ^
aaacdc35 ^
7d6c75e4 ^



a6bbcd0d ^
c0ccab92 ^

















0cfe4091 ^
896489a6 ^
0cfe4091 ^
a6bbcd0d ^

08a758ed ^
6d6c589f ^
0cfe4091 ^
896489a6 ^
a6bbcd0d ^
a13955ef ^

c4e2de9c ^
a13955ef ^
a6bbcd0d ^
08a758ed ^


00b54f6f ^
08a758ed ^



a6bbcd0d ^


b35ec1b3 ^



a6bbcd0d ^


08a758ed ^
00b54f6f ^
08a758ed ^


639e56e3 ^
08a758ed ^
b35ec1b3 ^







a6bbcd0d ^
08a758ed ^
08722702 ^































aaacdc35 ^

a6bbcd0d ^
5be29f67 ^
3a12afa7 ^
652251c2 ^


a6bbcd0d ^









cadad895 ^



a6bbcd0d ^







08a758ed ^
a6bbcd0d ^





4df668fd ^
896489a6 ^
08a758ed ^
5be29f67 ^


aaacdc35 ^
08a758ed ^
5be29f67 ^






5be29f67 ^
cadad895 ^
08a758ed ^
7eee2e1f ^
08a758ed ^
639e56e3 ^
08a758ed ^
a6bbcd0d ^
a13955ef ^
08a758ed ^
a13955ef ^

08a758ed ^

652251c2 ^
08a758ed ^
7d6c75e4 ^
ee930b0f ^
aaacdc35 ^

7d6c75e4 ^

c0ccab92 ^

e80ae4b1 ^
7d6c75e4 ^
e80ae4b1 ^
d9e430c8 ^
672ab553 ^
e80ae4b1 ^

d0931964 ^
bfaf210d ^
4e0fd8c7 ^
6c4b7671 ^
e80ae4b1 ^
d9e430c8 ^
e80ae4b1 ^
d9e430c8 ^




e80ae4b1 ^








bfaf210d ^

e80ae4b1 ^

4e0fd8c7 ^



6c4b7671 ^
08a758ed ^
7eee2e1f ^
4e0fd8c7 ^

e80ae4b1 ^


a6bbcd0d ^
08a758ed ^

a6bbcd0d ^
08a758ed ^



a6bbcd0d ^



4e0fd8c7 ^
a6bbcd0d ^

e80ae4b1 ^


00b54f6f ^


a6bbcd0d ^
3d6767ae ^
08a758ed ^
e7f157c7 ^
08a758ed ^
d9e430c8 ^

e80ae4b1 ^
03480fea ^




d9e430c8 ^

08a758ed ^




4e0fd8c7 ^

d9e430c8 ^

08a758ed ^


a6bbcd0d ^
08a758ed ^

4e0fd8c7 ^
08a758ed ^
a6bbcd0d ^
4e0fd8c7 ^
7851427a ^


1b9c56ac ^




a6bbcd0d ^
aaacdc35 ^
a6bbcd0d ^

4e0fd8c7 ^
a6bbcd0d ^


4e0fd8c7 ^

a6bbcd0d ^

0cfe4091 ^
a6bbcd0d ^
4e0fd8c7 ^
0cfe4091 ^
a6bbcd0d ^
e80ae4b1 ^

0cfe4091 ^
4e0fd8c7 ^


0cfe4091 ^


d7a05e7a ^
0cfe4091 ^
8323cf21 ^



a6bbcd0d ^






986522a9 ^
a6bbcd0d ^


7d6c75e4 ^
a6bbcd0d ^
1a24350e ^
a6bbcd0d ^
4e0fd8c7 ^
a6bbcd0d ^
4e0fd8c7 ^
a6bbcd0d ^
a6bbcd0d ^



986522a9 ^
941f7e43 ^
a6bbcd0d ^













d7a05e7a ^
a6bbcd0d ^




e80ae4b1 ^
0cfe4091 ^

8323cf21 ^
0cfe4091 ^
a6bbcd0d ^









7eee2e1f ^


a6bbcd0d ^







8323cf21 ^
a6bbcd0d ^



08a758ed ^

3a12afa7 ^

7851427a ^
3a12afa7 ^



d7a05e7a ^
08a758ed ^

0cfe4091 ^
4e0fd8c7 ^

08a758ed ^
7eee2e1f ^
d7a05e7a ^
a6bbcd0d ^
3a12afa7 ^
a6bbcd0d ^

3d6767ae ^
a6bbcd0d ^



3a12afa7 ^
181ea25b ^
a6bbcd0d ^
a6bbcd0d ^
a6ba161a ^



b35ec1b3 ^



a6bbcd0d ^








a6ba161a ^
a6bbcd0d ^

a6ba161a ^

3d6767ae ^

a6bbcd0d ^



4e0fd8c7 ^
5da4c6e6 ^



89750ef4 ^

1b9c56ac ^
181ea25b ^
a6bbcd0d ^
4df668fd ^

a13955ef ^
a6bbcd0d ^
4df668fd ^
a6bbcd0d ^

941f7e43 ^
81f800cb ^
a6bbcd0d ^

d7a05e7a ^
eb2e57c9 ^







a6bbcd0d ^
c0ccab92 ^





aaacdc35 ^



4df668fd ^


a6bbcd0d ^

08a758ed ^
181ea25b ^
e7f157c7 ^
d9e430c8 ^
e7f157c7 ^
181ea25b ^
e7f157c7 ^
181ea25b ^
3a12afa7 ^

181ea25b ^
3a12afa7 ^
181ea25b ^
3a12afa7 ^
c4e2de9c ^

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
             
          

              

              

              


                    

                       
                    
                   
                   
                  
                 
                 
                
                 
                     
                       
                    
               
                         
                  
                   
                       

                   

    

                                                                         
                                             

                     


                              
                    
                 
                       
                 
                                
                         
                           


                               
                     
                  
                        
                         
                       
                          
             
                                   
                                    
                      
                 
                   
                                          
                             

                                        

                                                             
 


                                               
          











                                                                
                            


                                       
             
 
                                                           
                     
                     
 






















                                                                                       
 


                                          
                                                                                
         
                                                                                



                                          
                                                                                
         
                                                                                
 






                                                                                                                                               
                       
 

                                  
 
                                             
                                  
 
                                              
                                  


                                              
                                   
 

                                               
                                   
 







                                                                                                        
                    
                           
                   
                                                         
                                          
                                             


                                        




                                            
                       
 

                                
                                      
 

                                    
                    

                                 
                                                                         
 

                                                                         
                                                                


                      
                                                                                                                    

                                       
                            

                                                   
                      
                                                                            













                                                                             

                          
                                                   



                                                

                                                 








                                                                              
                                                              
                                                            

                                                                        
                                                                          
                    
                                                             
                                                                           
                                                         

          






                                                                         
                                                       
                     


                                           
           

                                                               
         
                                  
                                       
                    

                       
              


                                    


                                      
                             
                             
                                    
                              
                         

                                                
                        


                                             

                            
                         
                          
                                                                           
                                             




                                                  
         
                              
                                                                 

                                                                         
 




                                                                     

                                                                       
                                                      



                                    
               



                            




                                    
                                                      
                                                       

                                                                
                       
                 
 
                                      
                     
                          

                          
                                   
                         
                  
                          
                                       


                             



                                                                                        


                                                                 
                                                                                 
       



                                                                            
 

















                                                                        
                                                             
                                              
 

                                                        
                            
                                                 
                                    
                               
 

                                                                                  
 
                                                                
                                                                    


                                                                      
                                                



                                   


                                                               



                                                          


                                              
 
                                                


                                      
                                                   
               







                                                          
              
 































                                                               

                                                   
 
                                                          
                    


                                         









                                                         



                                                  







                                                
       





                                    
                                                                            
                                                            
 


                                                                          
                                              
       






                                                                        
       
                                          
 
                                            
                                       
                                                  
       
                                                                            
                  
         

                                                                                                

                                          
                                 
 
                                                           
                                

                                                             

                                    

                           
                          
 
                                                                  
                        
                             

                          
                      
                     
                                                       
                           
                                                   
              
                                                        




                                   








                                                                                  

                                  

                                      



                                                                      
 
                               
                                                                 

                                                                       


                                                     
                                                                   

                                                               
                                                       



                                                                             



                              
                  

                           


                                                                                                   


                                                
                                 
                     
       
                                                  
 

                                                   
                           




                                                                                    

          




                                                                            

                                                              

                                                         


                                         
                                           

               
                                                              
          
                    
                                                          


                                                    




                                                                                                     
                   
                                     

                               
                                                                 


                                  

                                                                         

                            
           
                                               
                
                                              
   

                                                                        
 


                                                                        


                                       
                                         
 



                                                      






                                                              
                                           


                                                    
                                      
                           
                                 
                               
                                                                                   
           
                                                                                   
                                   



                                               
                                           
                       













                                               
                                                              




                                                         
                                                                                                                                    

                          
                             
                         









                                                       


                                   







                                               
                                           



                                        

                                                

                                            
                           



                                                       
                                             

                                                                 
                                      

                                                                       
 
                              
                                           
 
                                                                                    

              
                     



                                                                                     
                                                                                                       
                
             
                                



                                                        



                                                         








                                                                         
                                      

                                              

                                              

                     



                                    
                                                                   



                                                                         

                                                             
                                      
                  
            

                                                
                                                            
               
                                                       

                                    
                         
                                              

                                    
                                                                                               







                                                   
          





                                                                                                    



                                    


                                    

                      
 
                                                        
                                 
                                           
                                                
           
 
                                                       

                           
                                 
                 
           
 

                                      
import deques
import net
import options
import os
import streams
import tables
import unicode

when defined(posix):
  import posix

import buffer/cell
import buffer/container
import config/config
import data/charset
import display/term
import io/lineedit
import io/promise
import io/request
import io/window
import ips/editor
import ips/forkserver
import ips/socketstream
import js/javascript
import js/regex
import types/buffersource
import types/color
import types/cookie
import types/dispatcher
import types/url
import utils/twtstr

type
  LineMode* = enum
    NO_LINEMODE, LOCATION, USERNAME, PASSWORD, COMMAND, BUFFER, SEARCH_F,
    SEARCH_B, ISEARCH_F, ISEARCH_B, GOTO_LINE

  Pager* = ref object
    askpromise*: Promise[bool]
    askprompt: string
    askcursor: int
    jsctx: JSContext
    numload*: int
    alerts: seq[string]
    alerton: bool
    commandMode* {.jsget.}: bool
    container*: Container
    dispatcher*: Dispatcher
    lineedit*: Option[LineEdit]
    linemode*: LineMode
    username: string
    scommand*: string
    config: Config
    regex: Option[Regex]
    iregex: Option[Regex]
    reverseSearch: bool
    statusgrid*: FixedGrid
    tty: File
    procmap*: Table[Pid, Container]
    unreg*: seq[(Pid, SocketStream)]
    display: FixedGrid
    redraw*: bool
    term*: Terminal
    linehist: array[LineMode, LineHistory]
    siteconf: seq[SiteConfig]
    omnirules: seq[OmniRule]
    cookiejars: Table[string, CookieJar]

func attrs(pager: Pager): WindowAttributes = pager.term.attrs

func getRoot(container: Container): Container =
  var c = container
  while c.parent != nil: c = c.parent
  return c

iterator all_children(parent: Container): Container {.inline.} =
  var stack = newSeqOfCap[Container](parent.children.len)
  for i in countdown(parent.children.high, 0):
    stack.add(parent.children[i])
  while stack.len > 0:
    let c = stack.pop()
    yield c
    for i in countdown(c.children.high, 0):
      stack.add(c.children[i])

iterator containers*(pager: Pager): Container {.inline.} =
  if pager.container != nil:
    let root = getRoot(pager.container)
    yield root
    for c in root.all_children:
      yield c

proc setContainer*(pager: Pager, c: Container) {.jsfunc.} =
  pager.container = c
  pager.redraw = true

proc hasprop(pager: Pager, s: string): bool {.jshasprop.} =
  if pager.container != nil:
    let cval = toJS(pager.jsctx, pager.container)
    let val = JS_GetPropertyStr(pager.jsctx, cval, s)
    if val != JS_UNDEFINED:
      result = true
    JS_FreeValue(pager.jsctx, val)

proc reflect(ctx: JSContext, this_val: JSValue, argc: cint, argv: ptr JSValue,
             magic: cint, func_data: ptr JSValue): JSValue {.cdecl.} =
  let fun = cast[ptr JSValue](cast[int](func_data) + sizeof(JSValue))[]
  return JS_Call(ctx, fun, func_data[], argc, argv)

proc getter(pager: Pager, s: string): Option[JSValue] {.jsgetprop.} =
  if pager.container != nil:
    let cval = toJS(pager.jsctx, pager.container)
    let val = JS_GetPropertyStr(pager.jsctx, cval, s)
    if val != JS_UNDEFINED:
      if JS_IsFunction(pager.jsctx, val):
        var func_data = @[cval, val]
        let fun = JS_NewCFunctionData(pager.jsctx, reflect, 1, 0, 2, addr func_data[0])
        return some(fun)
      return some(val)

proc searchNext(pager: Pager) {.jsfunc.} =
  if pager.regex.issome:
    if not pager.reverseSearch:
      pager.container.cursorNextMatch(pager.regex.get, pager.config.search.wrap)
    else:
      pager.container.cursorPrevMatch(pager.regex.get, pager.config.search.wrap)

proc searchPrev(pager: Pager) {.jsfunc.} =
  if pager.regex.issome:
    if not pager.reverseSearch:
      pager.container.cursorPrevMatch(pager.regex.get, pager.config.search.wrap)
    else:
      pager.container.cursorNextMatch(pager.regex.get, pager.config.search.wrap)

proc getLineHist(pager: Pager, mode: LineMode): LineHistory =
  if pager.linehist[mode] == nil:
    pager.linehist[mode] = newLineHistory()
  return pager.linehist[mode]

proc setLineEdit(pager: Pager, prompt: string, mode: LineMode, current = "", hide = false) =
  pager.lineedit = some(readLine(prompt, pager.attrs.width, current = current, term = pager.term, hide = hide, hist = pager.getLineHist(mode)))
  pager.linemode = mode

proc clearLineEdit(pager: Pager) =
  pager.lineedit = none(LineEdit)

proc searchForward(pager: Pager) {.jsfunc.} =
  pager.setLineEdit("/", SEARCH_F)

proc searchBackward(pager: Pager) {.jsfunc.} =
  pager.setLineEdit("?", SEARCH_B)

proc isearchForward(pager: Pager) {.jsfunc.} =
  pager.container.pushCursorPos()
  pager.setLineEdit("/", ISEARCH_F)

proc isearchBackward(pager: Pager) {.jsfunc.} =
  pager.container.pushCursorPos()
  pager.setLineEdit("?", ISEARCH_B)

proc gotoLine[T: string|int](pager: Pager, s: T = "") {.jsfunc.} =
  when s is string:
    if s == "":
      pager.setLineEdit("Goto line: ", GOTO_LINE)
      return
  pager.container.gotoLine(s)

proc newPager*(config: Config, attrs: WindowAttributes, dispatcher: Dispatcher, ctx: JSContext): Pager =
  let pager = Pager(
    dispatcher: dispatcher,
    config: config,
    display: newFixedGrid(attrs.width, attrs.height - 1),
    statusgrid: newFixedGrid(attrs.width),
    term: newTerminal(stdout, config, attrs),
    jsctx: ctx,
    siteconf: config.getSiteConfig(ctx),
    omnirules: config.getOmniRules(ctx)
  )
  return pager

proc launchPager*(pager: Pager, tty: File) =
  pager.tty = tty
  pager.term.start(tty)

proc dumpAlerts*(pager: Pager) =
  for msg in pager.alerts:
    stderr.write("cha: " & msg & '\n')

proc quit*(pager: Pager, code = 0) =
  pager.term.quit()
  pager.dumpAlerts()

proc clearDisplay(pager: Pager) =
  pager.display = newFixedGrid(pager.display.width, pager.display.height)

proc buffer(pager: Pager): Container {.jsfget, inline.} = pager.container

proc refreshDisplay(pager: Pager, container = pager.container) =
  var r: Rune
  var by = 0
  pager.clearDisplay()
  for line in container.ilines(container.fromy ..< min(container.fromy + pager.display.height, container.numLines)):
    var w = 0 # width of the row so far
    var i = 0 # byte in line.str
    # Skip cells till fromx.
    while w < container.fromx and i < line.str.len:
      fastRuneAt(line.str, i, r)
      w += r.twidth(w)
    let dls = by * pager.display.width # starting position of row in display
    # Fill in the gap in case we skipped more cells than fromx mandates (i.e.
    # we encountered a double-width character.)
    var k = 0
    if w > container.fromx:
      while k < w - container.fromx:
        pager.display[dls + k].str &= ' '
        inc k
    var cf = line.findFormat(w)
    var nf = line.findNextFormat(w)
    let startw = w # save this for later
    # Now fill in the visible part of the row.
    while i < line.str.len:
      let pw = w
      fastRuneAt(line.str, i, r)
      let rw = r.twidth(w)
      w += rw
      if w > container.fromx + pager.display.width:
        break # die on exceeding the width limit
      if nf.pos != -1 and nf.pos <= pw:
        cf = nf
        nf = line.findNextFormat(pw)
      if cf.pos != -1:
        pager.display[dls + k].format = cf.format
      if r == Rune('\t'):
        # Needs to be replaced with spaces, otherwise bgcolor isn't displayed.
        let tk = k + rw
        while k < tk:
          pager.display[dls + k].str &= ' '
          inc k
      else:
        pager.display[dls + k].str &= r
        k += rw
    # Finally, override cell formatting for highlighted cells.
    let hls = container.findHighlights(container.fromy + by)
    let aw = container.width - (startw - container.fromx) # actual width
    for hl in hls:
      let area = hl.colorArea(container.fromy + by, startw .. startw + aw)
      for i in area:
        var hlformat = pager.display[dls + i - startw].format
        hlformat.bgcolor = pager.config.display.highlight_color.cellColor()
        pager.display[dls + i - startw].format = hlformat
    inc by

proc writeStatusMessage(pager: Pager, str: string,
                        format: Format = newFormat(), start = 0,
                        maxwidth = -1, clip = '$'): int {.discardable.} =
  var maxwidth = maxwidth
  if maxwidth == -1:
    maxwidth = pager.statusgrid.len
  var i = start
  let e = min(start + maxwidth, pager.statusgrid.width)
  for r in str.runes:
    if i >= e:
      if e > start:
        pager.statusgrid[e - 1].str = $clip
      break
    if r.isControlChar():
      pager.statusgrid[i].str = "^" & getControlLetter(char(r))
    else:
      pager.statusgrid[i].str = $r
    pager.statusgrid[i].format = format
    i += r.twidth(i)
  result = i
  var def = newFormat()
  while i < e:
    pager.statusgrid[i].str = ""
    pager.statusgrid[i].format = def
    inc i

proc refreshStatusMsg*(pager: Pager) =
  let container = pager.container
  if container == nil: return
  if pager.tty == nil: return
  if pager.askpromise != nil: return
  if container.loadinfo != "":
    pager.alerton = false
    pager.writeStatusMessage(container.loadinfo)
  elif pager.alerts.len > 0:
    pager.alerton = true
    pager.writeStatusMessage(pager.alerts[0])
    pager.alerts.delete(0)
  else:
    var format = newFormat()
    format.reverse = true
    pager.alerton = false
    container.clearHover()
    var msg = $(container.cursory + 1) & "/" & $container.numLines & " (" &
              $container.atPercentOf() & "%)"
    let mw = pager.writeStatusMessage(msg, format)
    let title = " <" & container.getTitle() & ">"
    let hover = container.getHoverText()
    if hover.len == 0:
      pager.writeStatusMessage(title, format, mw)
    else:
      let hover2 = " " & hover
      let maxwidth = pager.statusgrid.width - hover2.width() - mw
      let tw = pager.writeStatusMessage(title, format, mw, maxwidth, '>')
      pager.writeStatusMessage(hover2, format, tw)

# Call refreshStatusMsg if no alert is being displayed on the screen.
proc showAlerts*(pager: Pager) =
  if not pager.alerton:
    pager.refreshStatusMsg()

proc drawBuffer*(pager: Pager, container: Container, ostream: Stream) =
  var format = newFormat()
  container.readLines(proc(line: SimpleFlexibleLine) =
    if line.formats.len == 0:
      ostream.write(line.str & "\n")
    else:
      var x = 0
      var w = 0
      var i = 0
      var s = ""
      for f in line.formats:
        var outstr = ""
        while x < f.pos:
          var r: Rune
          fastRuneAt(line.str, i, r)
          outstr &= r
          x += r.width()
        s &= pager.term.processOutputString(outstr, w)
        s &= pager.term.processFormat(format, f.format)
      s &= pager.term.processOutputString(line.str.substr(i), w)
      s &= pager.term.processFormat(format, newFormat()) & "\n"
      ostream.write(s))
  ostream.flush()

proc redraw(pager: Pager) {.jsfunc.} =
  pager.redraw = true
  pager.term.clearCanvas()

proc draw*(pager: Pager) =
  if pager.container == nil: return
  pager.term.hideCursor()
  if pager.redraw:
    pager.refreshDisplay()
    pager.term.writeGrid(pager.display)
  if pager.askpromise != nil:
    discard
  elif pager.lineedit.isSome:
    pager.term.writeGrid(pager.lineedit.get.generateOutput(), 0, pager.attrs.height - 1)
  else:
    pager.term.writeGrid(pager.statusgrid, 0, pager.attrs.height - 1)
  pager.term.outputGrid()
  if pager.askpromise != nil:
    pager.term.setCursor(pager.askcursor, pager.attrs.height - 1)
  elif pager.lineedit.isSome:
    pager.term.setCursor(pager.lineedit.get.getCursorX(), pager.attrs.height - 1)
  else:
    pager.term.setCursor(pager.container.acursorx, pager.container.acursory)
  pager.term.showCursor()
  pager.term.flush()
  pager.redraw = false

proc writeAskPrompt(pager: Pager) =
  let yn = " (y/n)"
  let maxwidth = pager.statusgrid.width - yn.len
  let i = pager.writeStatusMessage(pager.askprompt, maxwidth = maxwidth)
  pager.askcursor = pager.writeStatusMessage(yn, start = i)
  pager.term.writeGrid(pager.statusgrid, 0, pager.attrs.height - 1)

proc ask(pager: Pager, prompt: string): Promise[bool] {.jsfunc.} =
  pager.askprompt = prompt
  pager.writeAskPrompt()
  pager.askpromise = Promise[bool]()
  return pager.askpromise

proc fulfillAsk*(pager: Pager, y: bool) =
  pager.askpromise.resolve(y)
  pager.askpromise = nil
  pager.askprompt = ""

proc registerContainer*(pager: Pager, container: Container) =
  pager.procmap[container.process] = container

proc addContainer*(pager: Pager, container: Container) =
  container.parent = pager.container
  if pager.container != nil:
    pager.container.children.insert(container, 0)
  pager.registerContainer(container)
  pager.setContainer(container)

proc dupeContainer(pager: Pager, container: Container, location: URL): Container =
  return pager.dispatcher.dupeBuffer(container, pager.config, location, "")

proc dupeBuffer*(pager: Pager, location: URL = nil) {.jsfunc.} =
  pager.addContainer(pager.dupeContainer(pager.container, location))

# The prevBuffer and nextBuffer procedures emulate w3m's PREV and NEXT
# commands by traversing the container tree in a depth-first order.
proc prevBuffer(pager: Pager): bool {.jsfunc.} =
  if pager.container == nil:
    return false
  if pager.container.parent == nil:
    return false
  let n = pager.container.parent.children.find(pager.container)
  assert n != -1, "Container not a child of its parent"
  if n > 0:
    var container = pager.container.parent.children[n - 1]
    while container.children.len > 0:
      container = container.children[^1]
    pager.setContainer(container)
  else:
    pager.setContainer(pager.container.parent)
  return true

proc nextBuffer(pager: Pager): bool {.jsfunc.} =
  if pager.container == nil:
    return false
  if pager.container.children.len > 0:
    pager.setContainer(pager.container.children[0])
    return true
  var container = pager.container
  while container.parent != nil:
    let n = container.parent.children.find(container)
    assert n != -1, "Container not a child of its parent"
    if n < container.parent.children.high:
      pager.setContainer(container.parent.children[n + 1])
      return true
    container = container.parent
  return false

proc parentBuffer(pager: Pager): bool {.jsfunc.} =
  if pager.container == nil:
    return false
  if pager.container.parent == nil:
    return false
  pager.setContainer(pager.container.parent)
  return true

proc prevSiblingBuffer(pager: Pager): bool {.jsfunc.} =
  if pager.container == nil:
    return false
  if pager.container.parent == nil:
    return false
  var n = pager.container.parent.children.find(pager.container)
  assert n != -1, "Container not a child of its parent"
  if n == 0:
    n = pager.container.parent.children.len
  pager.setContainer(pager.container.parent.children[n - 1])
  return true

proc nextSiblingBuffer(pager: Pager): bool {.jsfunc.} =
  if pager.container == nil:
    return false
  if pager.container.parent == nil:
    return false
  var n = pager.container.parent.children.find(pager.container)
  assert n != -1, "Container not a child of its parent"
  if n == pager.container.parent.children.high:
    n = -1
  pager.setContainer(pager.container.parent.children[n + 1])
  return true

proc alert*(pager: Pager, msg: string) {.jsfunc.} =
  pager.alerts.add(msg)

proc deleteContainer(pager: Pager, container: Container) =
  container.cancel()
  if container.sourcepair != nil:
    container.sourcepair.sourcepair = nil
    container.sourcepair = nil
  if container.parent != nil:
    let parent = container.parent
    let n = parent.children.find(container)
    assert n != -1, "Container not a child of its parent"
    for i in countdown(container.children.high, 0):
      let child = container.children[i]
      child.parent = container.parent
      parent.children.insert(child, n + 1)
    parent.children.delete(n)
    if container == pager.container:
      if n == 0:
        pager.setContainer(parent)
      else:
        pager.setContainer(parent.children[n - 1])
  elif container.children.len > 0:
    let parent = container.children[0]
    parent.parent = nil
    for i in 1..container.children.high:
      container.children[i].parent = parent
      parent.children.add(container.children[i])
    if container == pager.container:
      pager.setContainer(parent)
  else:
    for child in container.children:
      child.parent = nil
    if container == pager.container:
      pager.setContainer(nil)
  container.parent = nil
  container.children.setLen(0)
  pager.unreg.add((container.process, SocketStream(container.iface.stream)))
  pager.dispatcher.forkserver.removeChild(container.process)

proc discardBuffer(pager: Pager, container = none(Container)) {.jsfunc.} =
  let c = container.get(pager.container)
  if c == nil or c.parent == nil and c.children.len == 0:
    pager.alert("Cannot discard last buffer!")
  else:
    pager.deleteContainer(c)

proc discardTree(pager: Pager, container = none(Container)) {.jsfunc.} =
  let container = container.get(pager.container)
  if container != nil:
    for c in container.all_children:
      pager.deleteContainer(c)
  else:
    pager.alert("Buffer has no children!")

proc toggleSource(pager: Pager) {.jsfunc.} =
  if pager.container.sourcepair != nil:
    pager.setContainer(pager.container.sourcepair)
  else:
    let contenttype = if pager.container.contenttype.get("") == "text/html":
      "text/plain"
    else:
      "text/html"
    let container = pager.dispatcher.dupeBuffer(pager.container, pager.config, nil, contenttype)
    container.sourcepair = pager.container
    pager.container.sourcepair = container
    pager.addContainer(container)

proc windowChange*(pager: Pager, attrs: WindowAttributes) =
  pager.term.windowChange(attrs)
  pager.display = newFixedGrid(attrs.width, attrs.height - 1)
  pager.statusgrid = newFixedGrid(attrs.width)
  for container in pager.containers:
    container.windowChange(attrs)
  if pager.askprompt != "":
    pager.writeAskPrompt()
  pager.refreshStatusMsg()

proc applySiteconf(pager: Pager, request: Request): BufferConfig =
  let url = $request.url
  let host = request.url.host
  var refererfrom: bool
  var cookiejar: CookieJar
  var headers: Headers
  var scripting: bool
  var charsets = pager.config.encoding.document_charset
  for sc in pager.siteconf:
    if sc.url.isSome and not sc.url.get.match(url):
      continue
    elif sc.host.isSome and not sc.host.get.match(host):
      continue
    if sc.subst != nil:
      let s = sc.subst(request.url)
      if s.isSome and s.get != nil:
        request.url = s.get
    if sc.cookie.isSome:
      if sc.cookie.get:
        # host/url might have changed by now
        let jarid = sc.sharecookiejar.get(request.url.host)
        if jarid notin pager.cookiejars:
          pager.cookiejars[jarid] = newCookieJar(request.url, sc.thirdpartycookie)
        cookiejar = pager.cookiejars[jarid]
      else:
        cookiejar = nil # override
    if sc.scripting.isSome:
      scripting = sc.scripting.get
    if sc.refererfrom.isSome:
      refererfrom = sc.refererfrom.get
    if sc.document_charset.len > 0:
      charsets = sc.document_charset
  return pager.config.getBufferConfig(request.url, cookiejar, headers,
    refererfrom, scripting, charsets)

# Load request in a new buffer.
proc gotoURL(pager: Pager, request: Request, prevurl = none(URL),
    ctype = none(string), cs = none(Charset), replace: Container = nil,
    redirectdepth = 0, referrer: Container = nil) =
  if referrer != nil and referrer.config.refererfrom:
    request.referer = referrer.source.location
  var bufferconfig = pager.applySiteconf(request)
  if prevurl.isnone or not prevurl.get.equals(request.url, true) or
      request.url.hash == "" or request.httpmethod != HTTP_GET:
    # Basically, we want to reload the page *only* when
    # a) we force a reload (by setting prevurl to none)
    # b) or the new URL isn't just the old URL + an anchor
    # I think this makes navigation pretty natural, or at least very close to
    # what other browsers do. Still, it would be nice if we got some visual
    # feedback on what is actually going to happen when typing a URL; TODO.
    let source = BufferSource(
      t: LOAD_REQUEST,
      request: request,
      contenttype: ctype,
      charset: cs,
      location: request.url
    )
    if referrer != nil:
      bufferconfig.referrerpolicy = referrer.config.referrerpolicy
    let container = pager.dispatcher.newBuffer(bufferconfig, source, redirectdepth = redirectdepth)
    if replace != nil:
      container.replace = replace
      container.copyCursorPos(container.replace)
    pager.addContainer(container)
    inc pager.numload
  else:
    pager.container.findAnchor(request.url.anchor)

proc omniRewrite(pager: Pager, s: string): string =
  for rule in pager.omnirules:
    if rule.match.match(s):
      let sub = rule.subst(s)
      if sub.isSome:
        return sub.get
      else:
        pager.alert("Error in substitution of rule " & rule.match.buf & " for " & s)
  return s

# When the user has passed a partial URL as an argument, they might've meant
# either:
# * file://$PWD/<file>
# * https://<url>
# So we attempt to load both, and see what works.
proc loadURL*(pager: Pager, url: string, ctype = none(string),
    cs = none(Charset)) =
  let url0 = pager.omniRewrite(url)
  let url = if url[0] == '~': expandPath(url0) else: url0
  let firstparse = parseURL(url)
  if firstparse.issome:
    let prev = if pager.container != nil:
      some(pager.container.source.location)
    else:
      none(URL)
    pager.gotoURL(newRequest(firstparse.get), prev, ctype, cs)
    return
  var urls: seq[URL]
  if pager.config.network.prepend_https and url[0] != '/':
    let pageurl = parseURL("https://" & url)
    if pageurl.isSome: # attempt to load remote page
      urls.add(pageurl.get)
  let cdir = parseURL("file://" & percentEncode(getCurrentDir(), LocalPathPercentEncodeSet) & DirSep)
  let localurl = percentEncode(url, LocalPathPercentEncodeSet)
  let newurl = parseURL(localurl, cdir)
  if newurl.isSome:
    urls.add(newurl.get) # attempt to load local file
  if urls.len == 0:
    pager.alert("Invalid URL " & url)
  else:
    let prevc = pager.container
    pager.gotoURL(newRequest(urls.pop()), ctype = ctype, cs = cs)
    if pager.container != prevc:
      pager.container.retry = urls

proc readPipe0*(pager: Pager, ctype: Option[string], cs: Option[Charset],
    fd: FileHandle, location: Option[URL], title: string): Container =
  let source = BufferSource(
    t: LOAD_PIPE,
    fd: fd,
    contenttype: some(ctype.get("text/plain")),
    charset: cs,
    location: location.get(newURL("file://-"))
  )
  let bufferconfig = pager.config.getBufferConfig(source.location)
  return pager.dispatcher.newBuffer(bufferconfig, source, title = title)

proc readPipe*(pager: Pager, ctype: Option[string], cs: Option[Charset],
    fd: FileHandle) =
  let container = pager.readPipe0(ctype, cs, fd, none(URL), "*pipe*")
  pager.addContainer(container)

proc command(pager: Pager) {.jsfunc.} =
  pager.setLineEdit("COMMAND: ", COMMAND)

proc commandMode(pager: Pager, val: bool) {.jsfset.} =
  pager.commandMode = val
  if val:
    pager.command()

proc updateReadLineISearch(pager: Pager, linemode: LineMode) =
  let lineedit = pager.lineedit.get
  case lineedit.state
  of CANCEL:
    pager.iregex = none(Regex)
    pager.container.popCursorPos()
    pager.container.clearSearchHighlights()
  of EDIT:
    let x = $lineedit.news
    if x != "": pager.iregex = compileSearchRegex(x)
    pager.container.popCursorPos(true)
    if pager.iregex.isSome:
      pager.container.hlon = true
      if linemode == ISEARCH_F:
        pager.container.cursorNextMatch(pager.iregex.get, pager.config.search.wrap)
      else:
        pager.container.cursorPrevMatch(pager.iregex.get, pager.config.search.wrap)
    pager.container.pushCursorPos()
  of FINISH:
    if pager.iregex.isSome:
      pager.regex = pager.iregex
    pager.reverseSearch = linemode == ISEARCH_B
    pager.container.clearSearchHighlights()
    pager.redraw = true

proc updateReadLine*(pager: Pager) =
  let lineedit = pager.lineedit.get
  template s: string = $lineedit.news
  if pager.linemode in {ISEARCH_F, ISEARCH_B}:
    pager.updateReadLineISearch(pager.linemode)
  else:
    case lineedit.state
    of EDIT: return
    of FINISH:
      case pager.linemode
      of LOCATION: pager.loadURL(s)
      of USERNAME:
        pager.username = s
        pager.setLineEdit("Password: ", PASSWORD, hide = true)
      of PASSWORD:
        let url = newURL(pager.container.source.location)
        url.username = pager.username
        url.password = s
        pager.username = ""
        pager.gotoURL(newRequest(url), some(pager.container.source.location), replace = pager.container, referrer = pager.container)
      of COMMAND:
        pager.scommand = s
        if pager.commandMode:
          pager.command()
      of BUFFER: pager.container.readSuccess(s)
      of SEARCH_F:
        let x = s
        if x != "": pager.regex = compileSearchRegex(x)
        pager.reverseSearch = false
        pager.searchNext()
      of SEARCH_B:
        let x = s
        if x != "": pager.regex = compileSearchRegex(x)
        pager.reverseSearch = true
        pager.searchNext()
      of GOTO_LINE:
        pager.container.gotoLine(s)
      else: discard
    of CANCEL:
      case pager.linemode
      of USERNAME: pager.discardBuffer()
      of PASSWORD:
        pager.username = ""
        pager.discardBuffer()
      of BUFFER: pager.container.readCanceled()
      of COMMAND: pager.commandMode = false
      else: discard
  if lineedit.state in {CANCEL, FINISH}:
    if pager.lineedit.get == lineedit:
      pager.clearLineEdit()

# Open a URL prompt and visit the specified URL.
proc load(pager: Pager, s = "") {.jsfunc.} =
  if s.len > 0 and s[^1] == '\n':
    pager.loadURL(s[0..^2])
  else:
    var url = s
    if url == "":
      url = pager.container.source.location.serialize()
    pager.setLineEdit("URL: ", LOCATION, url)

# Reload the page in a new buffer, then kill the previous buffer.
proc reload(pager: Pager) {.jsfunc.} =
  pager.gotoURL(newRequest(pager.container.source.location), none(URL),
    pager.container.contenttype, replace = pager.container)

proc authorize(pager: Pager) =
  pager.setLineEdit("Username: ", USERNAME)

proc handleEvent0(pager: Pager, container: Container, event: ContainerEvent): bool =
  case event.t
  of FAIL:
    dec pager.numload
    pager.deleteContainer(container)
    if container.retry.len > 0:
      pager.gotoURL(newRequest(container.retry.pop()), ctype = container.contenttype)
    else:
      pager.alert("Can't load " & $container.source.location & " (error code " & $container.code & ")")
    return false
  of SUCCESS:
    if container.replace != nil:
      let n = container.replace.children.find(container)
      if n != -1:
        container.replace.children.delete(n)
        container.parent = nil
      let n2 = container.children.find(container.replace)
      if n2 != -1:
        container.children.delete(n2)
        container.replace.parent = nil
      container.children.add(container.replace.children)
      for child in container.children:
        child.parent = container
      container.replace.children.setLen(0)
      if container.replace.parent != nil:
        container.parent = container.replace.parent
        let n = container.replace.parent.children.find(container.replace)
        assert n != -1, "Container not a child of its parent"
        container.parent.children[n] = container
        container.replace.parent = nil
      if pager.container == container.replace:
        pager.setContainer(container)
      pager.deleteContainer(container.replace)
      container.replace = nil
  of LOADED:
    dec pager.numload
  of NEEDS_AUTH:
    if pager.container == container:
      pager.authorize()
  of REDIRECT:
    if container.redirectdepth < pager.config.network.max_redirect:
      pager.alert("Redirecting to " & $event.request.url)
      pager.gotoURL(event.request, some(container.source.location),
        replace = container, redirectdepth = container.redirectdepth + 1,
        referrer = pager.container)
    else:
      pager.alert("Error: maximum redirection depth reached")
      pager.deleteContainer(container)
      return false
  of ANCHOR:
    var url2 = newURL(container.source.location)
    url2.hash(event.anchor)
    pager.addContainer(pager.dupeContainer(container, url2))
  of NO_ANCHOR:
    pager.alert("Couldn't find anchor " & event.anchor)
  of UPDATE:
    if container == pager.container:
      pager.redraw = true
      if event.force: pager.term.clearCanvas()
  of READ_LINE:
    if container == pager.container:
      pager.setLineEdit("(BUFFER) " & event.prompt, BUFFER, event.value, hide = event.password)
  of READ_AREA:
    if container == pager.container:
      var s = event.tvalue
      if openInEditor(pager.term, pager.config, s):
        pager.container.readSuccess(s)
      else:
        pager.container.readCanceled()
      pager.redraw = true
  of OPEN:
    if pager.container == nil or not pager.container.isHoverURL(event.request.url):
      pager.ask("Open pop-up? " & $event.request.url).then(proc(x: bool) =
        if x:
          pager.gotoURL(event.request, some(container.source.location), referrer = pager.container))
    else:
      pager.gotoURL(event.request, some(container.source.location), referrer = pager.container)
  of INVALID_COMMAND: discard
  of STATUS:
    if pager.container == container:
      pager.refreshStatusMsg()
  of ALERT:
    if pager.container == container:
      pager.alert(event.msg)
  of NO_EVENT: discard
  return true

proc handleEvents*(pager: Pager, container: Container) =
  while container.events.len > 0:
    let event = container.events.popFirst()
    if not pager.handleEvent0(container, event):
      break

proc handleEvent*(pager: Pager, container: Container) =
  try:
    container.handleEvent()
    pager.handleEvents(container)
  except IOError:
    discard

proc addPagerModule*(ctx: JSContext) =
  ctx.registerType(Pager)