Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
interpret.ml1 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(* * interpret.ml * ----------- * Copyright : (c) 2019 - 2020, ZAN DoYe <zandoye@gmail.com> * Licence : MIT * * This file is a part of mew_vi. *) open Vi_action open Edit_action open React module Make (Concurrent:Mew.Concurrent.S) = struct module MsgBox = Concurrent.MsgBox module Thread = Concurrent.Thread let (>>=)= Thread.bind module Register = struct type t= string let compare= String.compare type content= | Seq of string | Line of string let compare_content t1 t2= match t1, t2 with | Seq s1, Seq s2-> String.compare s1 s2 | Line s1, Line s2-> String.compare s1 s2 | Seq _, Line _-> 1 | Line _, Seq _-> -1 end module RegisterMap = Map.Make(Register) type register= string option type count= int option type keyseq= Modal.Key.t list module Resolver = struct type t= config -> status -> keyseq -> result and config= { mode: Mode.Name.t signal; set_mode: ?step:step -> Mode.Name.t -> unit; keyseq: keyseq signal; set_keyseq: ?step:step -> keyseq -> unit; mutable resolver_insert: t; mutable resolver_normal: t; mutable resolver_visual: t; mutable resolver_command: t; } and status= { register: register; count: count; } and result= | Accept of (Edit_action.t * keyseq * Mode.Name.t) | Continue of (t * status * keyseq) | Rejected of keyseq let resolver_dummy= fun _config _status keyseq-> Rejected keyseq let resolver_insert _config _status keyseq= match keyseq with | []-> Rejected [] | key::tl-> if key.Key.control && key.code = Char "[" then Accept ( Vi [Motion (Left, 1); ChangeMode Normal] , tl , Mode.Name.Normal) else if key.code = Escape then Accept ( Vi [Motion (Left, 1); ChangeMode Normal] , tl , Mode.Name.Normal) else Accept ( Bypass [key] , tl , Mode.Name.Insert) module Common = struct let get_count status= match status.count with | Some count-> count | None-> 1 let get_register status= match status.register with | None | Some "\'"-> "\"" | Some reg-> reg let try_count continuation config status keyseq= let get_count numseq= match numseq with | ""-> status.count | _-> let num= int_of_string numseq in match status.count with | Some count-> Some (count * num) | None-> Some num in let rec other_num numseq config status keyseq= match keyseq with | []-> Rejected keyseq | key::tl-> match key.Key.code with | Char code-> if String.length code = 1 && code >= "0" && code <= "9" && not (key.Key.control || key.Key.meta || key.Key.shift) then let resolver= other_num (numseq ^ code) in Continue (resolver, status, tl) else continuation config { status with count= (get_count numseq) } keyseq | Escape-> Rejected tl | _-> continuation config { status with count= get_count numseq } keyseq in let first_num ()= match keyseq with | []-> Rejected keyseq | key::tl-> match key.Key.code with | Char code-> if String.length code = 1 && not (key.Key.control || key.Key.meta || key.Key.shift) then if code >= "1" && code <= "9" then let resolver= other_num code in Continue (resolver, status, tl) else continuation config { status with count= get_count "" } keyseq else continuation config { status with count= get_count "" } keyseq | Escape-> Rejected tl | _-> continuation config status keyseq in first_num () let try_register next_mode continuation config status keyseq= let get_register _config status keyseq= match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char code-> let resolver= continuation in Continue (resolver, { status with register= Some code }, tl) | _-> Rejected keyseq else Accept (Bypass [key], tl, Mode.Name.Normal) in match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char code-> if code = "\"" then Continue (get_register, status, tl) else continuation config status keyseq | _-> Rejected keyseq else Accept (Bypass [key], tl, next_mode) let try_motion next_mode config status keyseq= let try_motion_g _config status keyseq= let count= get_count status in match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char "e"-> Accept ( Vi [Motion (Word_back_end, count)] , tl , next_mode) | Char "E"-> Accept ( Vi [Motion (WORD_back_end, count)] , tl , next_mode) | Char "g"-> Accept ( match status.count with | None-> Vi [Motion (GotoLine_first, count)] , tl , next_mode | Some count-> Vi [Motion (GotoLine, (count-1))] , tl , next_mode) | _-> Rejected keyseq else Accept (Bypass [key], tl, next_mode) in let try_motion_occurence ?(backward=false) _config status keyseq= let count= get_count status in match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char chr-> if backward then Accept ( Vi [Motion (Occurrence_inline_back chr, count)] , tl , next_mode) else Accept ( Vi [Motion (Occurrence_inline chr, count)] , tl , next_mode) | _-> Rejected keyseq else Accept (Bypass [key], tl, next_mode) in let try_motion_occurence_till ?(backward=false) _config status keyseq= let count= get_count status in match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char chr-> if backward then Accept ( Vi [Motion (Occurrence_inline_till_back chr, count)] , tl , next_mode) else Accept ( Vi [Motion (Occurrence_inline_till chr, count)] , tl , next_mode) | _-> Rejected keyseq else Accept (Bypass [key], tl, next_mode) in let try_motion_n _config status keyseq= let count= get_count status in match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char "h"-> Accept ( Vi [Motion (Left, count)] , tl , next_mode) | Char "l"-> Accept ( Vi [Motion (Right, count)] , tl , next_mode) | Char "j"-> Accept ( Vi [Motion (Downward, count)] , tl , next_mode) | Char "k"-> Accept ( Vi [Motion (Upward, count)] , tl , next_mode) | Char "0"-> Accept ( Vi [Motion (Line_FirstChar, count)] , tl , next_mode) | Char "$"-> Accept ( Vi [Motion (Line_LastChar, count)] , tl , next_mode) | Char "^"-> Accept ( Vi [Motion (Line_FirstNonBlank, count)] , tl , next_mode) | Char "w"-> Accept ( Vi [Motion (Word, count)] , tl , next_mode) | Char "W"-> Accept ( Vi [Motion (WORD, count)] , tl , next_mode) | Char "b"-> Accept ( Vi [Motion (Word_back, count)] , tl , next_mode) | Char "B"-> Accept ( Vi [Motion (WORD_back, count)] , tl , next_mode) | Char "e"-> Accept ( Vi [Motion (Word_end, count)] , tl , next_mode) | Char "E"-> Accept ( Vi [Motion (WORD_end, count)] , tl , next_mode) | Char "G"-> Accept ( match status.count with | None-> Vi [Motion (GotoLine_last, count)] , tl , next_mode | Some count-> Vi [Motion (GotoLine, (count-1))] , tl , next_mode) | Char "g"-> let resolver= try_motion_g in Continue (resolver, status, tl) | Char "f"-> let backward= false in let resolver= try_motion_occurence ~backward in Continue (resolver, status, tl) | Char "F"-> let backward= true in let resolver= try_motion_occurence ~backward in Continue (resolver, status, tl) | Char "t"-> let backward= false in let resolver= try_motion_occurence_till ~backward in Continue (resolver, status, tl) | Char "T"-> let backward= true in let resolver= try_motion_occurence_till ~backward in Continue (resolver, status, tl) | Char "%"-> Accept ( Vi [Motion (Match, 1)] , tl , next_mode) | _-> Rejected keyseq else Accept (Bypass [key], tl, next_mode) in try_count try_motion_n config status keyseq end module Normal = struct let try_change_mode _config _status keyseq= match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char "i"-> Accept ( Vi [ChangeMode Insert] , tl , Mode.Name.Insert) | Char "I"-> Accept ( Vi [ Motion (Line_FirstNonBlank, 1); ChangeMode Insert] , tl , Mode.Name.Insert) | Char "a"-> Accept ( Vi [ Motion (Right_nl, 1); ChangeMode Insert] , tl , Mode.Name.Insert) | Char "A"-> Accept ( Vi [ Motion (Line_LastChar_nl, 1); ChangeMode Insert] , tl , Mode.Name.Insert) | Char "v"-> Accept ( Vi [ ChangeMode Visual] , tl , Mode.Name.Visual) | _-> Rejected keyseq else Rejected keyseq let try_modify config status keyseq= let open Common in let try_motion_n ~action _config status keyseq = let next_mode= if action = `Change then Mode.Name.Insert else Mode.Name.Normal in let make_actions status tl motion= let action= let register= get_register status and count= get_count status in match action with | `Change-> Change (register, motion, count) | `Delete-> Delete (register, motion, count) | `Yank-> Yank (register, motion, count) in Accept ( Vi [action] , tl , next_mode) in let try_motion_g _config status keyseq= match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char "e"-> make_actions status tl Word_back_end | Char "E"-> make_actions status tl WORD_back_end | _-> Rejected keyseq else Accept (Bypass [key], tl, Mode.Name.Normal) in let try_motion_quote ?(inner=false) _config status keyseq= match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char chr-> if inner then make_actions status tl (Quote_inner chr) else make_actions status tl (Quote_include chr) | _-> Rejected keyseq else Accept (Bypass [key], tl, Mode.Name.Normal) in let try_motion_object ?(inner=false) _config status keyseq= match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char "(" | Char ")" | Char "b"-> if inner then make_actions status tl Parenthesis_inner else make_actions status tl Parenthesis_include | Char "[" | Char "]"-> if inner then make_actions status tl Bracket_inner else make_actions status tl Bracket_include | Char "<" | Char ">"-> if inner then make_actions status tl AngleBracket_inner else make_actions status tl AngleBracket_include | Char "{" | Char "}"-> if inner then make_actions status tl Brace_inner else make_actions status tl Brace_include | Char "'"-> if inner then make_actions status tl (Quote_inner "'") else make_actions status tl (Quote_include "'") | Char "\""-> if inner then make_actions status tl (Quote_inner "\"") else make_actions status tl (Quote_include "\"") | Char "w"-> if inner then make_actions status tl Word_inner else make_actions status tl Word_include | Char "W"-> if inner then make_actions status tl WORD_inner else make_actions status tl WORD_include | Char "q"-> let resolver= try_motion_quote ~inner in Continue (resolver, status, tl) | _-> Rejected keyseq else Accept (Bypass [key], tl, Mode.Name.Normal) in let try_motion_occurence ?(backward=false) _config _status keyseq= match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char chr-> if backward then make_actions status tl (Occurrence_inline_back chr) else make_actions status tl (Occurrence_inline chr) | _-> Rejected keyseq else Accept (Bypass [key], tl, Mode.Name.Normal) in let try_motion_occurence_till ?(backward=false) _config status keyseq= match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char chr-> if backward then make_actions status tl (Occurrence_inline_till_back chr) else make_actions status tl (Occurrence_inline_till chr) | _-> Rejected keyseq else Accept (Bypass [key], tl, Mode.Name.Normal) in match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char "h"-> make_actions status tl Left | Char "l"-> make_actions status tl Right | Char "j"-> make_actions status tl Downward | Char "k"-> make_actions status tl Upward | Char "0"-> make_actions status tl Line_FirstChar | Char "$"-> make_actions status tl Line_LastChar | Char "^"-> make_actions status tl Line_FirstNonBlank | Char "w"-> make_actions status tl Word | Char "W"-> make_actions status tl WORD | Char "b"-> make_actions status tl Word_back | Char "B"-> make_actions status tl WORD_back | Char "e"-> make_actions status tl Word_end | Char "E"-> make_actions status tl WORD_end | Char "g"-> let resolver= try_motion_g in Continue (resolver, status, tl) | Char "d"-> if action = `Delete then make_actions status tl Line else Rejected keyseq | Char "a"-> let inner= false in let resolver= try_motion_object ~inner in Continue (resolver, status, tl) | Char "i"-> let inner= true in let resolver= try_motion_object ~inner in Continue (resolver, status, tl) | Char "f"-> let backward= false in let resolver= try_motion_occurence ~backward in Continue (resolver, status, tl) | Char "F"-> let backward= true in let resolver= try_motion_occurence ~backward in Continue (resolver, status, tl) | Char "t"-> let backward= false in let resolver= try_motion_occurence_till ~backward in Continue (resolver, status, tl) | Char "T"-> let backward= true in let resolver= try_motion_occurence_till ~backward in Continue (resolver, status, tl) | Char "%"-> make_actions status tl Match | Char "y"-> if action = `Yank then make_actions status tl Line else Rejected keyseq | _-> Rejected keyseq else Accept (Bypass [key], tl, Mode.Name.Normal) in let determin _config status keyseq= let count= get_count status and register= get_register status in match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char "u"-> Accept (Vi [Undo count], tl, Mode.Name.Normal) | Char "p"-> Accept (Vi [Paste_after (register, count)], tl, Mode.Name.Normal) | Char "P"-> Accept (Vi [Paste_before (register, count)], tl, Mode.Name.Normal) | Char "d"-> let resolver= Common.try_count (try_motion_n ~action:`Delete) in Continue (resolver, status, tl) | Char "c"-> let resolver= Common.try_count (try_motion_n ~action:`Change) in Continue (resolver, status, tl) | Char "D"-> Accept ( Vi [Delete (register, Line_LastChar, count)], tl, Mode.Name.Normal) | Char "C"-> Accept ( Vi [Delete (register, Line_LastChar, count)], tl, Mode.Name.Insert) | Char "x"-> Accept ( Vi [Delete (register, Right, count)] , tl , Mode.Name.Normal) | Char "s"-> Accept ( Vi [Delete (register, Right, count)] , tl , Mode.Name.Insert) | Char "J"-> Accept ( Vi [(Join count)] , tl , Mode.Name.Normal) | Char "y"-> let resolver= Common.try_count (try_motion_n ~action:`Yank) in Continue (resolver, status, tl) | _-> Rejected keyseq else Accept (Bypass [key], tl, Mode.Name.Normal) in determin config status keyseq let try_insert _config status keyseq= let count= Common.get_count status in match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char "o"-> Accept ( Vi [Insert ((Newline_below ""), count)] , tl , Mode.Name.Insert) | Char "O"-> Accept ( Vi [Insert ((Newline_above ""), count)] , tl , Mode.Name.Insert) | _-> Rejected keyseq else Accept (Bypass [key], tl, Mode.Name.Normal) let try_motion_modify_insert config status keyseq= match Common.try_motion Mode.Name.Normal config status keyseq with | Rejected keyseq-> let resolver config status keyseq= match try_modify config status keyseq with | Rejected keyseq-> let resolver= try_insert in Continue (resolver, status, keyseq) | r-> r in Continue (resolver, status, keyseq) | r-> r let resolver_normal config status keyseq= match keyseq with | []-> Rejected [] | _-> match try_change_mode config status keyseq with | Rejected keyseq-> Common.try_register Mode.Name.Normal (Common.try_count (Common.try_register Mode.Name.Normal try_motion_modify_insert)) config status keyseq | r-> r end module Visual = struct let try_change_mode _config _status keyseq= match keyseq with | []-> Rejected [] | key::tl-> if key.Key.control && key.code = Char "[" then Accept ( Vi [ChangeMode Normal] , tl , Mode.Name.Normal) else if key.code = Escape then Accept ( Vi [ChangeMode Normal] , tl , Mode.Name.Normal) else if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char "v"-> Accept ( Vi [ ChangeMode Normal] , tl , Mode.Name.Normal) | _-> Rejected keyseq else Rejected keyseq let try_motion= Common.try_motion Mode.Name.Visual let try_modify _config status keyseq= let register= Common.get_register status in match keyseq with | []-> Rejected [] | key::tl-> if not (key.Key.control || key.Key.meta || key.Key.shift) then match key.Key.code with | Char "c" | Char "s"-> Accept ( Vi [ DeleteSelected register; ChangeMode Insert] , tl , Mode.Name.Insert) | Char "d" | Char "x"-> Accept ( Vi [ DeleteSelected register; ChangeMode Normal ] , tl , Mode.Name.Normal) | Char "y"-> Accept ( Vi [ YankSelected register; ChangeMode Normal ] , tl , Mode.Name.Normal) | _-> Rejected keyseq else Rejected keyseq let try_motion_modify config status keyseq= match try_motion config status keyseq with | Rejected keyseq-> Continue (try_modify, status, keyseq) | r-> r let resolver_visual config status keyseq= match keyseq with | []-> Rejected [] | _-> match try_change_mode config status keyseq with | Rejected keyseq-> Common.try_register Mode.Name.Visual (Common.try_count try_motion_modify) config status keyseq | r-> r end let make_config ?(mode= Mode.Name.Insert) ?(keyseq=[]) ?(resolver_insert= resolver_insert) ?(resolver_normal= Normal.resolver_normal) ?(resolver_visual= Visual.resolver_visual) ?(resolver_command= resolver_dummy) () = let mode, set_mode= React.S.create mode in let keyseq, set_keyseq= React.S.create keyseq in { mode; set_mode; keyseq; set_keyseq; resolver_insert; resolver_normal; resolver_visual; resolver_command; } let rec interpret ?resolver ?(keyseq=[]) config status (keyIn: Modal.Key.t MsgBox.t) (action: Edit_action.t MsgBox.t) () = let resolver= match resolver with | Some resolver-> resolver | None-> match S.value config.mode with | Mode.Name.Insert-> config.resolver_insert | Mode.Name.Visual-> config.resolver_visual | _-> config.resolver_normal in (match keyseq with | []-> MsgBox.get keyIn >>= fun key-> Thread.return [key] | _-> Thread.return keyseq) >>= fun keyseq-> match resolver config status keyseq with | Accept (edit, keyseq, next_mode)-> config.set_mode next_mode; MsgBox.put action edit >>= interpret config { status with count= None} ~keyseq keyIn action | Continue (resolver, status, keyseq)-> interpret config status ~resolver ~keyseq keyIn action () | Rejected _keyseq-> MsgBox.put action Dummy >>= interpret config { status with count= None } keyIn action end end