text
stringlengths 12
786k
|
---|
let insert ctx rope = let position = Zed_cursor . get_position ctx . cursor in if not ctx . check || ctx . edit . editable position 0 then begin let len = Zed_rope . length rope in let text = ctx . edit . text and lines = ctx . edit . lines in if S . value ctx . edit . erase_mode then begin let text_len = Zed_rope . length ctx . edit . text in if position + len > text_len then begin let orig_width = Zed_string ( . aval_width ( width Zed_rope ( . to_string ( sub text position ( text_len - position ) position ) position ) position ) position ) position in let curr_width = Zed_string ( . aval_width ( width Zed_rope ( . to_string rope ) rope ) rope ) rope in ctx . edit . text <- Zed_rope . replace text position ( text_len - position ) position rope ; ctx . edit . lines <- Zed_lines . replace ctx . edit . lines position ( text_len - position ) position ( Zed_lines . of_rope rope ) rope ; modify ctx text lines position position len ( text_len - position ) position curr_width orig_width end else begin let orig_width = Zed_string ( . aval_width ( width Zed_rope ( . to_string ( sub text position len ) len ) len ) len ) len in let curr_width = Zed_string ( . aval_width ( width Zed_rope ( . to_string rope ) rope ) rope ) rope in ctx . edit . text <- Zed_rope . replace text position len rope ; ctx . edit . lines <- Zed_lines . replace ctx . edit . lines position len ( Zed_lines . of_rope rope ) rope ; modify ctx text lines position position len len curr_width orig_width ; end ; move ctx len end else begin let width_add = Zed_string . aval_width ( Zed_string . width ( Zed_rope . to_string rope ) rope ) rope in ctx . edit . text <- Zed_rope . insert ctx . edit . text position rope ; ctx . edit . lines <- Zed_lines . insert ctx . edit . lines position ( Zed_lines . of_rope rope ) rope ; modify ctx text lines position position len 0 width_add 0 ; move ctx len end end else raise Cannot_edit |
let insert_char ctx ch = if Zed_char . is_combining_mark ch then let position = Zed_cursor . get_position ctx . cursor in if not ctx . check || ctx . edit . editable position 0 then begin let text = ctx . edit . text and lines = ctx . edit . lines in try ctx . edit . text <- Zed_rope . insert_uChar ctx . edit . text position ch ; modify ctx text lines position position 1 1 0 0 ; move ctx 0 ; next_line_n ctx 0 ; with _ -> ( ) end else raise Cannot_edit else insert ctx ( Zed_rope . of_string ( fst ( Zed_string . of_uChars [ ch ] ch ) ch ) ch ) ch |
let insert_no_erase ctx rope = let position = Zed_cursor . get_position ctx . cursor in if not ctx . check || ctx . edit . editable position 0 then begin let len = Zed_rope . length rope and text = ctx . edit . text and lines = ctx . edit . lines in let width_add = Zed_string . aval_width ( Zed_string . width ( Zed_rope . to_string rope ) rope ) rope in ctx . edit . text <- Zed_rope . insert text position rope ; ctx . edit . lines <- Zed_lines . insert ctx . edit . lines position ( Zed_lines . of_rope rope ) rope ; modify ctx text lines position position len 0 width_add 0 ; move ctx len end else raise Cannot_edit |
let remove_next ctx len = let position = Zed_cursor . get_position ctx . cursor in let text_len = Zed_rope . length ctx . edit . text in let len = if position + len > text_len then text_len - position else len in if not ctx . check || ctx . edit . editable position len then begin let text = ctx . edit . text and lines = ctx . edit . lines in let width_remove = Zed_string ( . aval_width ( width Zed_rope ( . to_string ( sub text position len ) len ) len ) len ) len in ctx . edit . text <- Zed_rope . remove text position len ; ctx . edit . lines <- Zed_lines . remove ctx . edit . lines position len ; modify ctx text lines position position 0 len 0 width_remove ; end else raise Cannot_edit |
let remove_prev ctx len = let position = Zed_cursor . get_position ctx . cursor in let len = min position len in if not ctx . check || ctx . edit . editable ( position - len ) len len then begin let text = ctx . edit . text and lines = ctx . edit . lines in let width_remove = Zed_string ( . aval_width ( width Zed_rope ( . to_string ( sub text ( position - len ) len len ) len ) len ) len ) len in ctx . edit . text <- Zed_rope . remove text ( position - len ) len len ; ctx . edit . lines <- Zed_lines . remove ctx . edit . lines ( position - len ) len len ; modify ctx text lines ( position - len ) len position 0 len 0 width_remove ; end else raise Cannot_edit |
let replace ctx len rope = let position = Zed_cursor . get_position ctx . cursor in let text_len = Zed_rope . length ctx . edit . text in let len = if position + len > text_len then text_len - position else len in if not ctx . check || ctx . edit . editable position len then begin let rope_len = Zed_rope . length rope and text = ctx . edit . text and lines = ctx . edit . lines in let orig_width = Zed_string ( . aval_width ( width Zed_rope ( . to_string ( sub text position len ) len ) len ) len ) len in let curr_width = Zed_string ( . aval_width ( width Zed_rope ( . to_string rope ) rope ) rope ) rope in ctx . edit . text <- Zed_rope . replace text position len rope ; ctx . edit . lines <- Zed_lines . replace ctx . edit . lines position len ( Zed_lines . of_rope rope ) rope ; modify ctx text lines position position rope_len len curr_width orig_width ; move ctx rope_len end else raise Cannot_edit |
let newline_rope = Zed_rope . singleton ( Zed_char . unsafe_of_char ' \ n ' ) n ' |
let newline ctx = insert ctx newline_rope |
let next_char ctx = if not ( at_eot ctx ) ctx then move ctx 1 |
let prev_char ctx = if not ( at_bot ctx ) ctx then move ctx ( - 1 ) 1 |
let next_line ctx = let index = Zed_cursor . get_line ctx . cursor in if index = Zed_lines . count ctx . edit . lines then goto ctx ~ set_wanted_column : false ( Zed_rope . length ctx . edit . text ) text else begin let stop = if index + 1 = Zed_lines . count ctx . edit . lines then Zed_rope . length ctx . edit . text else Zed_lines . line_start ctx . edit . lines ( index + 2 ) 2 - 1 in let wanted_idx = Zed_lines . get_idx_by_width ctx . edit . lines ( index + 1 ) 1 ( Zed_cursor . get_wanted_column ctx . cursor ) cursor in goto ctx ~ set_wanted_column : false ( min wanted_idx stop ) stop end |
let prev_line ctx = let index = Zed_cursor . get_line ctx . cursor in if index = 0 then begin goto ctx ~ set_wanted_column : false 0 end else begin let stop = Zed_lines . line_start ctx . edit . lines index - 1 in let wanted_idx = Zed_lines . get_idx_by_width ctx . edit . lines ( index - 1 ) 1 ( Zed_cursor . get_wanted_column ctx . cursor ) cursor in goto ctx ~ set_wanted_column : false ( min wanted_idx stop ) stop end |
let join_line ctx = let is_space uchar = match UCharInfo . general_category uchar with | ` Cc | ` Zs | ` Zl | ` Zp | ` Mn -> true | _ -> false in let is_not_space uchar = not ( is_space uchar ) uchar in let text = ctx . edit . text in let lines = lines ctx . edit in let lines_num = Zed_lines . count lines in let index = line ctx in let position = Zed_lines . line_stop ctx . edit . lines index in let len = 1 in if index < lines_num then if not ctx . check || ctx . edit . editable position len then begin let width_remove = 1 in if is_not_space ( Zed_char . core ( Zed_rope . get text ( position - 1 ) 1 ) 1 ) 1 && is_not_space ( Zed_char . core ( Zed_rope . get text ( position + 1 ) 1 ) 1 ) 1 then let space = Zed_rope . of_string @@ Zed_string . of_utf8 " " in let lines_space = Zed_lines . of_rope space in ctx . edit . text <- Zed_rope . replace text position len space ; ctx . edit . lines <- Zed_lines . replace ctx . edit . lines position len lines_space ; modify ctx text lines position position 0 0 0 0 else ( ctx . edit . text <- Zed_rope . remove text position len ; ctx . edit . lines <- Zed_lines . remove ctx . edit . lines position len ; modify ctx text lines position position 0 len 0 width_remove ) width_remove end else raise Cannot_edit |
let goto_bol ctx = goto ctx ( Zed_lines . line_start ctx . edit . lines ( Zed_cursor . get_line ctx . cursor ) cursor ) cursor |
let goto_eol ctx = let index = Zed_cursor . get_line ctx . cursor in if index = Zed_lines . count ctx . edit . lines then goto ctx ( Zed_rope . length ctx . edit . text ) text else goto ctx ( Zed_lines . line_start ctx . edit . lines ( index + 1 ) 1 - 1 ) 1 |
let goto_bot ctx = goto ctx 0 |
let goto_eot ctx = goto ctx ( Zed_rope . length ctx . edit . text ) text |
let delete_next_chars ctx n = if not ( at_eot ctx ) ctx then begin ctx . edit . set_selection false ; remove_next ctx n ; end |
let delete_prev_chars ctx n = if not ( at_bot ctx ) ctx then begin ctx . edit . set_selection false ; remove_prev ctx n ; end |
let kill_next_chars ctx n = let position = Zed_cursor . get_position ctx . cursor in let end_pos = min ( position + n ) n ( Zed_rope . length ctx . edit . text ) text in let n = end_pos - position in ctx . edit . clipboard . clipboard_set ( Zed_rope . sub ctx . edit . text position n ) n ; ctx . edit . set_selection false ; remove ctx n |
let kill_prev_chars ctx n = let position = Zed_cursor . get_position ctx . cursor in let start = max 0 ( position - n ) n in let n = position - start in ctx . edit . clipboard . clipboard_set ( Zed_rope . sub ctx . edit . text start n ) n ; ctx . edit . set_selection false ; remove_prev ctx n |
let delete_next_char ctx = if not ( at_eot ctx ) ctx then begin ctx . edit . set_selection false ; remove_next ctx 1 end |
let delete_prev_char ctx = if not ( at_bot ctx ) ctx then begin ctx . edit . set_selection false ; remove_prev ctx 1 end |
let delete_next_line ctx = ctx . edit . set_selection false ; let position = Zed_cursor . get_position ctx . cursor in let index = Zed_cursor . get_line ctx . cursor in if index = Zed_lines . count ctx . edit . lines then remove_next ctx ( Zed_rope . length ctx . edit . text - position ) position else remove_next ctx ( Zed_lines . line_start ctx . edit . lines ( index + 1 ) 1 - position ) position |
let delete_prev_line ctx = ctx . edit . set_selection false ; let position = Zed_cursor . get_position ctx . cursor in let start = Zed_lines . line_start ctx . edit . lines ( Zed_cursor . get_line ctx . cursor ) cursor in remove_prev ctx ( position - start ) start |
let kill_next_line ctx = let position = Zed_cursor . get_position ctx . cursor in let index = Zed_cursor . get_line ctx . cursor in if index = Zed_lines . count ctx . edit . lines then begin ctx . edit . clipboard . clipboard_set ( Zed_rope . after ctx . edit . text position ) position ; ctx . edit . set_selection false ; remove ctx ( Zed_rope . length ctx . edit . text - position ) position end else begin let len = Zed_lines . line_start ctx . edit . lines ( index + 1 ) 1 - position in ctx . edit . clipboard . clipboard_set ( Zed_rope . sub ctx . edit . text position len ) len ; ctx . edit . set_selection false ; remove ctx len end |
let kill_prev_line ctx = let position = Zed_cursor . get_position ctx . cursor in let start = Zed_lines . line_start ctx . edit . lines ( Zed_cursor . get_line ctx . cursor ) cursor in ctx . edit . clipboard . clipboard_set ( Zed_rope . sub ctx . edit . text start ( position - start ) start ) start ; ctx . edit . set_selection false ; remove_prev ctx ( position - start ) start |
let switch_erase_mode ctx = ctx . edit . set_erase_mode ( not ( S . value ctx . edit . erase_mode ) erase_mode ) erase_mode |
let set_mark ctx = Zed_cursor . goto ctx . edit . mark ( Zed_cursor . get_position ctx . cursor ) cursor ; ctx . edit . set_selection true |
let goto_mark ctx = goto ctx ( Zed_cursor . get_position ctx . edit . mark ) mark |
let copy ctx = if S . value ctx . edit . selection then begin let a = Zed_cursor . get_position ctx . cursor and b = Zed_cursor . get_position ctx . edit . mark in let a = min a b and b = max a b in ctx . edit . clipboard . clipboard_set ( Zed_rope . sub ctx . edit . text a ( b - a ) a ) a ; ctx . edit . set_selection false end |
let copy_sequence ctx start len = ctx . edit . clipboard . clipboard_set ( Zed_rope . sub ctx . edit . text start len ) len |
let kill ctx = if S . value ctx . edit . selection then begin let a = Zed_cursor . get_position ctx . cursor and b = Zed_cursor . get_position ctx . edit . mark in let a = min a b and b = max a b in ctx . edit . clipboard . clipboard_set ( Zed_rope . sub ctx . edit . text a ( b - a ) a ) a ; ctx . edit . set_selection false ; goto ctx a ; let a = Zed_cursor . get_position ctx . cursor in if a <= b then remove ctx ( b - a ) a end |
let yank ctx = ctx . edit . set_selection false ; insert ctx ( ctx . edit . clipboard . clipboard_get ( ) ) |
let search_word_forward ctx = let len = Zed_rope . length ctx . edit . text in let rec loop idx = if idx = len then None else match ctx . edit . match_word ctx . edit . text idx with | Some idx ' -> Some ( Someidx , idx ' ) idx ' | None -> loop ( idx + 1 ) 1 in loop ( Zed_cursor . get_position ctx . cursor ) cursor |
let search_word_backward ctx = let rec loop idx = if idx = - 1 then None else match ctx . edit . match_word ctx . edit . text idx with | Some idx ' -> loop2 ( idx - 1 ) 1 ( idx , idx ' ) idx ' | None -> loop ( idx - 1 ) 1 and loop2 idx result = if idx = - 1 then Some result else match ctx . edit . match_word ctx . edit . text idx with | Some idx ' -> loop2 ( idx - 1 ) 1 ( idx , idx ' ) idx ' | None -> Some result in loop ( Zed_cursor . get_position ctx . cursor - 1 ) 1 |
let capitalize_word ctx = match search_word_forward ctx with | Some ( Someidx1 , idx2 ) idx2 -> goto ctx idx1 ; if Zed_cursor . get_position ctx . cursor = idx1 && idx1 < idx2 then begin let str = Zed_rope . sub ctx . edit . text idx1 ( idx2 - idx1 ) idx1 in let ch , str ' = Zed_rope . break str 1 in replace ctx ( Zed_rope . length str ) str ( Zed_rope . append ( CaseMap . uppercase ? locale ( : S . value ctx . edit . locale ) locale ch ) ch ( CaseMap . lowercase ? locale ( : S . value ctx . edit . locale ) locale str ' ) str ' ) str ' end | None -> ( ) |
let lowercase_word ctx = match search_word_forward ctx with | Some ( Someidx1 , idx2 ) idx2 -> goto ctx idx1 ; if Zed_cursor . get_position ctx . cursor = idx1 then begin let str = Zed_rope . sub ctx . edit . text idx1 ( idx2 - idx1 ) idx1 in replace ctx ( Zed_rope . length str ) str ( CaseMap . lowercase ? locale ( : S . value ctx . edit . locale ) locale str ) str end | None -> ( ) |
let uppercase_word ctx = match search_word_forward ctx with | Some ( Someidx1 , idx2 ) idx2 -> goto ctx idx1 ; if Zed_cursor . get_position ctx . cursor = idx1 then begin let str = Zed_rope . sub ctx . edit . text idx1 ( idx2 - idx1 ) idx1 in replace ctx ( Zed_rope . length str ) str ( CaseMap . uppercase ? locale ( : S . value ctx . edit . locale ) locale str ) str end | None -> ( ) |
let next_word ctx = match search_word_forward ctx with | Some ( Some_idx1 , idx2 ) idx2 -> goto ctx idx2 | None -> goto ctx ( Zed_rope . length ctx . edit . text ) text |
let prev_word ctx = match search_word_backward ctx with | Some ( Someidx1 , _idx2 ) _idx2 -> goto ctx idx1 | None -> goto ctx 0 |
let delete_next_word ctx = let position = Zed_cursor . get_position ctx . cursor in let word_end = match search_word_forward ctx with | Some ( Some_idx1 , idx2 ) idx2 -> idx2 | None -> Zed_rope . length ctx . edit . text in remove ctx ( word_end - position ) position |
let delete_prev_word ctx = let position = Zed_cursor . get_position ctx . cursor in let start = match search_word_backward ctx with | Some ( Someidx1 , _idx2 ) _idx2 -> idx1 | None -> 0 in remove_prev ctx ( position - start ) start |
let kill_next_word ctx = let position = Zed_cursor . get_position ctx . cursor in let word_end = match search_word_forward ctx with | Some ( Some_idx1 , idx2 ) idx2 -> idx2 | None -> Zed_rope . length ctx . edit . text in ctx . edit . clipboard . clipboard_set ( Zed_rope . sub ctx . edit . text position ( word_end - position ) position ) position ; ctx . edit . set_selection false ; remove ctx ( word_end - position ) position |
let kill_prev_word ctx = let position = Zed_cursor . get_position ctx . cursor in let start = match search_word_backward ctx with | Some ( Someidx1 , _idx2 ) _idx2 -> idx1 | None -> 0 in ctx . edit . clipboard . clipboard_set ( Zed_rope . sub ctx . edit . text start ( position - start ) start ) start ; ctx . edit . set_selection false ; remove_prev ctx ( position - start ) start |
let undo { check ; edit ; cursor } = if edit . undo_count > 0 then begin let index = if edit . undo_index = 0 then edit . undo_size - 1 else edit . undo_index - 1 in let text , lines , pos , new_pos , added , removed , added_width , removed_width = edit . undo ( . index ) index in if not check || edit . editable pos added then begin edit . undo_count <- edit . undo_count - 1 ; edit . undo_index <- index ; edit . text <- text ; edit . lines <- lines ; edit . send_changes { position = pos ; removed = added ; added = removed ; added_width = removed_width ; removed_width = added_width } ; Zed_cursor . goto cursor new_pos end else raise Cannot_edit end |
type action = | Insert of Zed_char . t | Insert_str of Zed_string . t | Newline | Next_char | Prev_char | Next_line | Prev_line | Join_line | Set_pos of int | Goto of int | Goto_bol | Goto_eol | Goto_bot | Goto_eot | Delete_next_chars of int | Delete_prev_chars of int | Kill_next_chars of int | Kill_prev_chars of int | Delete_next_char | Delete_prev_char | Delete_next_line | Delete_prev_line | Kill_next_line | Kill_prev_line | Switch_erase_mode | Set_mark | Goto_mark | Copy | Kill | Yank | Capitalize_word | Lowercase_word | Uppercase_word | Next_word | Prev_word | Delete_next_word | Delete_prev_word | Kill_next_word | Kill_prev_word | Undo |
let get_action = function | Insert ch -> ( fun ctx -> if Zed_char . length ch = 1 then insert_char ctx ( Zed_char . core ch ) ch else insert ctx ( Zed_rope . singleton ch ) ch ) ch | Insert_str str -> ( fun ctx -> insert ctx ( Zed_rope . of_string str ) str ) str | Newline -> newline | Next_char -> next_char | Prev_char -> prev_char | Next_line -> next_line | Prev_line -> prev_line | Join_line -> join_line | Set_pos n -> fun ctx -> set_position ctx n | Goto n -> fun ctx -> goto ctx n | Goto_bol -> goto_bol | Goto_eol -> goto_eol | Goto_bot -> goto_bot | Goto_eot -> goto_eot | Delete_next_chars n -> ( fun ctx -> delete_next_chars ctx n ) n | Delete_prev_chars n -> ( fun ctx -> delete_prev_chars ctx n ) n | Kill_next_chars n -> ( fun ctx -> kill_next_chars ctx n ) n | Kill_prev_chars n -> ( fun ctx -> kill_prev_chars ctx n ) n | Delete_next_char -> delete_next_char | Delete_prev_char -> delete_prev_char | Delete_next_line -> delete_next_line | Delete_prev_line -> delete_prev_line | Kill_next_line -> kill_next_line | Kill_prev_line -> kill_prev_line | Switch_erase_mode -> switch_erase_mode | Set_mark -> set_mark | Goto_mark -> goto_mark | Copy -> copy | Kill -> kill | Yank -> yank | Capitalize_word -> capitalize_word | Lowercase_word -> lowercase_word | Uppercase_word -> uppercase_word | Next_word -> next_word | Prev_word -> prev_word | Delete_next_word -> delete_next_word | Delete_prev_word -> delete_prev_word | Kill_next_word -> kill_next_word | Kill_prev_word -> kill_prev_word | Undo -> undo |
let doc_of_action = function | Insert _ -> " insert the given character . " | Insert_str _ -> " insert the given string . " | Newline -> " insert a newline character . " | Next_char -> " move the cursor to the next character . " | Prev_char -> " move the cursor to the previous character . " | Next_line -> " move the cursor to the next line . " | Prev_line -> " move the cursor to the previous line . " | Join_line -> " join two lines into one . " | Set_pos _ -> " move the cursor to the position without poking wanted_column " | Goto _ -> " move the cursor to the position " | Goto_bol -> " move the cursor to the beginning of the current line . " | Goto_eol -> " move the cursor to the end of the current line . " | Goto_bot -> " move the cursor to the beginning of the text . " | Goto_eot -> " move the cursor to the end of the text . " | Delete_next_chars _ -> " delete several characters after the cursor . " | Delete_prev_chars _ -> " delete several characters before the cursor . " | Kill_next_chars _ -> " cut several characters after the cursor . " | Kill_prev_chars _ -> " cut several characters before the cursor . " | Delete_next_char -> " delete the character after the cursor . " | Delete_prev_char -> " delete the character before the cursor . " | Delete_next_line -> " delete everything until the end of the current line . " | Delete_prev_line -> " delete everything until the beginning of the current line . " | Kill_next_line -> " cut everything until the end of the current line . " | Kill_prev_line -> " cut everything until the beginning of the current line . " | Switch_erase_mode -> " switch the current erasing mode . " | Set_mark -> " set the mark to the current position . " | Goto_mark -> " move the cursor to the mark . " | Copy -> " copy the current region to the clipboard . " | Kill -> " cut the current region to the clipboard . " | Yank -> " paste the contents of the clipboard at current position . " | Capitalize_word -> " capitalize the first word after the cursor . " | Lowercase_word -> " convert the first word after the cursor to lowercase . " | Uppercase_word -> " convert the first word after the cursor to uppercase . " | Next_word -> " move the cursor to the end of the next word . " | Prev_word -> " move the cursor to the beginning of the previous word . " | Delete_next_word -> " delete up until the next non - word character . " | Delete_prev_word -> " delete the word behind the cursor . " | Kill_next_word -> " cut up until the next non - word character . " | Kill_prev_word -> " cut the word behind the cursor . " | Undo -> " revert the last action . " |
let actions = [ Newline , " newline " ; Next_char , " next - char " ; Prev_char , " prev - char " ; Next_line , " next - line " ; Prev_line , " prev - line " ; Join_line , " join - line " ; Goto_bol , " goto - bol " ; Goto_eol , " goto - eol " ; Goto_bot , " goto - bot " ; Goto_eot , " goto - eot " ; Delete_next_char , " delete - next - char " ; Delete_prev_char , " delete - prev - char " ; Delete_next_line , " delete - next - line " ; Delete_prev_line , " delete - prev - line " ; Kill_next_line , " kill - next - line " ; Kill_prev_line , " kill - prev - line " ; Switch_erase_mode , " switch - erase - mode " ; Set_mark , " set - mark " ; Goto_mark , " goto - mark " ; Copy , " copy " ; Kill , " kill " ; Yank , " yank " ; Capitalize_word , " capitalize - word " ; Lowercase_word , " lowercase - word " ; Uppercase_word , " uppercase - word " ; Next_word , " next - word " ; Prev_word , " prev - word " ; Delete_next_word , " delete - next - word " ; Delete_prev_word , " delete - prev - word " ; Kill_next_word , " kill - next - word " ; Kill_prev_word , " kill - prev - word " ; Undo , " undo " ; ] |
let actions_to_names = Array . of_list ( List . sort ( fun ( a1 , _ ) _ ( a2 , _ ) _ -> compare a1 a2 ) a2 actions ) actions |
let names_to_actions = Array . of_list ( List . sort ( fun ( _ , n1 ) n1 ( _ , n2 ) n2 -> compare n1 n2 ) n2 actions ) actions |
let parse_insert x = if Zed_utf8 . starts_with x " insert ( insert " && Zed_utf8 . ends_with x ) " " then begin let str = String . sub x 7 ( String . length x - 8 ) 8 in if String . length str = 1 && Char . code str [ . 0 ] 0 < 128 then Insert ( InsertZed_char . unsafe_of_uChar ( UChar . of_char str [ . 0 ] 0 ) 0 ) 0 else if String . length str > 2 && str [ . 0 ] 0 = ' U ' && str [ . 1 ] 1 = ' + ' then let acc = ref 0 in for i = 2 to String . length str - 1 do let ch = str [ . i ] i in acc := ! acc * 16 + ( match ch with | ' 0 ' . . ' 9 ' -> Char . code ch - Char . code ' 0 ' | ' a ' . . ' f ' -> Char . code ch - Char . code ' a ' + 10 | ' A ' . . ' F ' -> Char . code ch - Char . code ' A ' + 10 | _ -> raise Not_found ) Not_found done ; try Insert ( InsertZed_char . unsafe_of_uChar ( UChar . of_int ! acc ) acc ) acc with _ -> raise Not_found else raise Not_found end else raise Not_found |
let parse_insert_str str = if Zed_utf8 . starts_with str " insert_str ( insert_str " && Zed_utf8 . ends_with str ) " " then let str = String . sub str 11 ( String . length str - 12 ) 12 in try Insert_str ( Zed_string . of_utf8 str ) str with _ -> raise Not_found else raise Not_found |
let parse_action_count action act_name str = let act_len = String . length act_name in if Zed_utf8 . starts_with str ( act_name ^ ( ) " " && Zed_utf8 . ends_with str ) " " then let str = String . sub str ( act_len + 1 ) 1 ( String . length str - ( act_len + 2 ) 2 ) 2 in try action ( int_of_string str ) str with _ -> raise Not_found else raise Not_found |
let parse_set_pos = parse_action_count ( fun c -> Set_pos c ) c " set - pos " |
let parse_goto = parse_action_count ( fun c -> Goto c ) c " goto " |
let parse_delete_next_chars = parse_action_count ( fun c -> Delete_next_chars c ) c " delete - next - chars " |
let parse_delete_prev_chars = parse_action_count ( fun c -> Delete_prev_chars c ) c " delete - prev - chars " |
let parse_kill_next_chars = parse_action_count ( fun c -> Kill_next_chars c ) c " kill - next - chars " |
let parse_kill_prev_chars = parse_action_count ( fun c -> Kill_prev_chars c ) c " kill - prev - chars " |
let parse_action_param x = try parse_insert x with Not_found -> try parse_insert_str x with Not_found -> try parse_set_pos x with Not_found -> try parse_goto x with Not_found -> try parse_delete_next_chars x with Not_found -> try parse_delete_prev_chars x with Not_found -> try parse_kill_next_chars x with Not_found -> parse_kill_prev_chars x |
let action_of_name x = let rec loop a b = if a = b then parse_action_param x else let c = ( a + b ) b / 2 in let action , name = Array . unsafe_get names_to_actions c in match compare x name with | d when d < 0 -> loop a c | d when d > 0 -> loop ( c + 1 ) 1 b | _ -> action in loop 0 ( Array . length names_to_actions ) names_to_actions |
let name_of_action x = let rec loop a b = if a = b then raise Not_found else let c = ( a + b ) b / 2 in let action , name = Array . unsafe_get actions_to_names c in match compare x action with | d when d < 0 -> loop a c | d when d > 0 -> loop ( c + 1 ) 1 b | _ -> name in let open Printf in let param_action_to_str name c = sprintf " % s ( s % d ) d " name c in match x with | Insert ch -> let code = UChar . code ( Zed_char . core ch ) ch in if code <= 255 then let ch = Char . chr ( UChar . code ( Zed_char . core ch ) ch ) ch in match ch with | ' a ' . . ' z ' | ' A ' . . ' Z ' | ' 0 ' . . ' 9 ' -> sprintf " insert ( insert % c ) c " ch | _ -> sprintf " insert ( insertU +% 02x ) 02x " code else if code <= 0xffff then sprintf " insert ( insertU +% 04x ) 04x " code else sprintf " insert ( insertU +% 06x ) 06x " code | Insert_str s -> sprintf " insert - str ( str % s ) s " ( Zed_string . to_utf8 s ) s | Set_pos c -> param_action_to_str " set - pos " c | Goto c -> param_action_to_str " goto " c | Delete_next_chars c -> param_action_to_str " delete - next - chars " c | Delete_prev_chars c -> param_action_to_str " delete - prev - chars " c | Kill_next_chars c -> param_action_to_str " kill - next - chars " c | Kill_prev_chars c -> param_action_to_str " kill - prev - chars " c | _ -> loop 0 ( Array . length actions_to_names ) actions_to_names |
module type S = sig type event type + ' a t val empty : ' a t val add : event list -> ' a -> ' a t -> ' a t val remove : event list -> ' a t -> ' a t val fold : ( event list -> ' a -> ' b -> ' b ) ' b -> ' a t -> ' b -> ' b val bindings : ' a t -> ( event list * ' a ) ' a list type ' a resolver type ' a pack val pack : ( ' a -> ' b ) ' b -> ' a t -> ' b pack val resolver : ' a pack list -> ' a resolver type ' a result = | Accepted of ' a | Continue of ' a resolver | Rejected val resolve : event -> ' a resolver -> ' a result end |
module Make ( Event : Map . OrderedType ) OrderedType = struct type event = Event . t module Event_map = Map . Make ( Event ) Event type ' a t = ' a node Event_map . t and ' a node = | Set of ' a t | Val of ' a let empty = Event_map . empty let rec add events value set = match events with | [ ] -> invalid_arg " Zed_input . Make . add " | [ event ] event -> Event_map . add event ( Val value ) value set | event :: events -> match try Some ( Event_map . find event set ) set with Not_found -> None with | None | Some ( Val _ ) _ -> Event_map . add event ( Set ( add events value empty ) empty ) empty set | Some ( Set s ) s -> Event_map . add event ( Set ( add events value s ) s ) s set let rec remove events set = match events with | [ ] -> invalid_arg " Zed_input . Make . remove " | [ event ] event -> Event_map . remove event set | event :: events -> match try Some ( Event_map . find event set ) set with Not_found -> None with | None | Some ( Val _ ) _ -> set | Some ( Set s ) s -> let s = remove events s in if Event_map . is_empty s then Event_map . remove event set else Event_map . add event ( Set s ) s set let fold f set acc = let rec loop prefix set acc = Event_map . fold ( fun event node acc -> match node with | Val v -> f ( List . rev ( event :: prefix ) prefix ) prefix v acc | Set s -> loop ( event :: prefix ) prefix s acc ) acc set acc in loop [ ] set acc let bindings set = List . rev ( fold ( fun events action l -> ( events , action ) action :: l ) l set [ ] ) module type Pack = sig type a type b val set : a t val map : a -> b end type ' a pack = ( module Pack with type b = ' a ) ' a type ' a resolver = ' a pack list let pack ( type u ) u ( type v ) v map set = let module Pack = struct type a = u type b = v let set = set let map = map end in ( module Pack : Pack with type b = v ) v let resolver l = l type ' a result = | Accepted of ' a | Continue of ' a resolver | Rejected let rec resolve_rec : ' a . event -> ' a pack list -> ' a pack list -> ' a result = fun ( type u ) u event acc packs -> match packs with | [ ] -> if acc = [ ] then Rejected else Continue ( List . rev acc ) acc | p :: packs -> let module Pack = ( val p : Pack with type b = u ) u in match try Some ( Event_map . find event Pack . set ) set with Not_found -> None with | Some ( Set set ) set -> resolve_rec event ( pack Pack . map set :: acc ) acc packs | Some ( Val v ) v -> Accepted ( Pack . map v ) v | None -> resolve_rec event acc packs let resolve event sets = resolve_rec event [ ] sets end |
type line = { length : int ; width : int ; width_info : int array ; } |
type t = | String of line | Return | Concat of t * t * int * int * int |
let empty_line ( ) = { length = 0 ; width = 0 ; width_info = [ ] || } |
let length = function | String line -> line . length | Return -> 1 | Concat ( Concat_ , _ , len , _ , _ ) _ -> len |
let count = function | String _ -> 0 | Return -> 1 | Concat ( Concat_ , _ , _ , count , _ ) _ -> count |
let depth = function | String _ | Return -> 0 | Concat ( Concat_ , _ , _ , _ , d ) d -> d |
let empty = String ( empty_line ( ) ) |
let unsafe_width ( ? tolerant = false ) false set idx len = let start = idx and len_all = len and acc = if tolerant then fun a b -> ( ) + ( if a < 0 then 1 else a ) a ( if b < 0 then 1 else b ) b else ( ) + in let rec unsafe_width set idx len = if len = 0 then Ok 0 else match set with | Return -> Error ( start + len_all - len ) len | String line -> Ok ( Array . fold_left acc 0 ( Array . sub line . width_info idx len ) len ) len | Concat ( set1 , set2 , _ , _ , _ ) _ -> let len1 = length set1 in if idx + len <= len1 then unsafe_width set1 idx len else if idx >= len1 then unsafe_width set2 ( idx - len1 ) len1 len else let r1 = unsafe_width set1 idx ( len1 - idx ) idx and r2 = unsafe_width set2 0 ( len - len1 + idx ) idx in match r1 , r2 with | Error ofs , _ -> Error ofs | Ok _ , Error ofs -> Error ofs | Ok w1 , Ok w2 -> Ok ( w1 + w2 ) w2 in unsafe_width set idx len |
let width ( ? tolerant = false ) false set idx len = if idx < 0 || len < 0 || idx + len > length set then raise Out_of_bounds else unsafe_width ~ tolerant set idx len |
let force_width set idx len = let acc a b = ( ) + ( if a < 0 then 1 else a ) a ( if b < 0 then 1 else b ) b in let rec force_width set idx len = if len = 0 then 0 else match set with | Return -> 0 | String line -> Array . fold_left acc 0 ( Array . sub line . width_info idx len ) len | Concat ( set1 , set2 , _ , _ , _ ) _ -> let len1 = length set1 in if idx + len <= len1 then force_width set1 idx len else if idx >= len1 then force_width set2 ( idx - len1 ) len1 len else let r1 = force_width set1 idx ( len1 - idx ) idx and r2 = force_width set2 0 ( len - len1 + idx ) idx in r1 + r2 in if idx < 0 || len < 0 || idx + len > length set then raise Out_of_bounds else force_width set idx len |
let rec line_index_rec set ofs acc = match set with | String _ -> acc | Return -> if ofs = 0 then acc else acc + 1 | Concat ( Concats1 , s2 , _ , _ , _ ) _ -> let len1 = length s1 in if ofs < len1 then line_index_rec s1 ofs acc else line_index_rec s2 ( ofs - len1 ) len1 ( acc + count s1 ) s1 |
let line_index set ofs = if ofs < 0 || ofs > length set then raise Out_of_bounds else line_index_rec set ofs 0 |
let rec line_start_rec set idx acc = match set with | String _ -> acc | Return -> if idx = 0 then acc else acc + 1 | Concat ( Concats1 , s2 , _ , _ , _ ) _ -> let count1 = count s1 in if idx <= count1 then line_start_rec s1 idx acc else line_start_rec s2 ( idx - count1 ) count1 ( acc + length s1 ) s1 |
let line_start set idx = if idx < 0 || idx > count set then raise Out_of_bounds else line_start_rec set idx 0 |
let line_stop set idx = if idx = count set then length set else line_start set ( idx + 1 ) 1 - 1 |
let line_length set idx = line_stop set idx - line_start set idx |
let concat set1 set2 = Concat ( Concat set1 , set2 , length set1 + length set2 , count set1 + count set2 , 1 + max ( depth set1 ) set1 ( depth set2 ) set2 ) set2 |
let append_line l1 l2 = { length = l1 . length + l2 . length ; width = l1 . width + l2 . width ; width_info = Array . append l1 . width_info l2 . width_info } |
let append set1 set2 = match set1 , set2 with | String { length = 0 ; _ } _ , _ -> set2 | _ , String { length = 0 ; _ } _ -> set1 | String l1 , String l2 -> String ( append_line l1 l2 ) l2 | String l1 , Concat ( ConcatString l2 , set , len , count , h ) h -> Concat ( ConcatString ( append_line l1 l2 ) l2 , set , len + l1 . length , count , h ) h | Concat ( Concatset , String l1 , len , count , h ) h , String l2 -> Concat ( Concatset , String ( Stringappend_line l1 l2 ) l2 , len + l2 . length , count , h ) h | _ -> let d1 = depth set1 and d2 = depth set2 in if d1 > d2 + 2 then begin match set1 with | String _ | Return -> assert false | Concat ( Concatset1_1 , set1_2 , _ , _ , _ ) _ -> if depth set1_1 >= depth set1_2 then concat set1_1 ( concat set1_2 set2 ) set2 else begin match set1_2 with | String _ | Return -> assert false | Concat ( Concatset1_2_1 , set1_2_2 , _ , _ , _ ) _ -> concat ( concat set1_1 set1_2_1 ) set1_2_1 ( concat set1_2_2 set2 ) set2 end end else if d2 > d1 + 2 then begin match set2 with | String _ | Return -> assert false | Concat ( Concatset2_1 , set2_2 , _ , _ , _ ) _ -> if depth set2_2 >= depth set2_1 then concat ( concat set1 set2_1 ) set2_1 set2_2 else begin match set2_1 with | String _ | Return -> assert false | Concat ( Concatset2_1_1 , set2_1_2 , _ , _ , _ ) _ -> concat ( concat set1 set2_1_1 ) set2_1_1 ( concat set2_1_2 set2_2 ) set2_2 end end else concat set1 set2 |
let rec unsafe_sub set idx len = match set with | String line -> let length = len in let width_info = Array . sub line . width_info idx length in let width = Array . fold_left ( ) + 0 width_info in String { length ; width ; width_info } | Return -> if len = 1 then Return else String ( empty_line ( ) ) | Concat ( Concatset_l , set_r , len ' , _ , _ ) _ -> let len_l = length set_l in if len = len ' then set else if idx >= len_l then unsafe_sub set_r ( idx - len_l ) len_l len else if idx + len <= len_l then unsafe_sub set_l idx len else append ( unsafe_sub set_l idx ( len_l - idx ) idx ) idx ( unsafe_sub set_r 0 ( len - len_l + idx ) idx ) idx |
let sub set idx len = if idx < 0 || len < 0 || idx + len > length set then raise Out_of_bounds else unsafe_sub set idx len |
let break set ofs = let len = length set in if ofs < 0 || ofs > len then raise Out_of_bounds else ( unsafe_sub set 0 ofs , unsafe_sub set ofs ( len - ofs ) ofs ) ofs |
let insert set ofs set ' = let set1 , set2 = break set ofs in append set1 ( append set ' set2 ) set2 |
let remove set ofs len = append ( sub set 0 ofs ) ofs ( sub set ( ofs + len ) len ( length set - ofs - len ) len ) len |
let replace set ofs len repl = append ( sub set 0 ofs ) ofs ( append repl ( sub set ( ofs + len ) len ( length set - ofs - len ) len ) len ) len |
let of_rope rope = let calc_widths widths = let width_info = widths |> List . rev |> Array . of_list in let width = Array . fold_left ( ) + 0 width_info in ( width , width_info ) width_info in let rec loop zip ( length , widths ) widths acc = if Zed_rope . Zip . at_eos zip then let width , width_info = calc_widths widths in append acc ( String { length ; width ; width_info } ) else let ch , zip = Zed_rope . Zip . next zip in if UChar . code ( Zed_char . core ch ) ch = 10 then let width , width_info = calc_widths widths in loop0 zip ( append ( append acc ( String { length ; width ; width_info } ) ) Return ) Return else loop zip ( length + 1 , Zed_char . width ch :: widths ) widths acc and loop0 zip acc = if Zed_rope . Zip . at_eos zip then acc else let ch , zip = Zed_rope . Zip . next zip in if UChar . code ( Zed_char . core ch ) ch = 10 then loop0 zip ( append acc Return ) Return else loop zip ( 1 , [ Zed_char . width ch ] ch ) ch acc in loop0 ( Zed_rope . Zip . make_f rope 0 ) 0 empty |
let get_idx_by_width set row column = let start = line_start set row in let stop = line_stop set row in let rec get idx acc_width = if acc_width >= column || idx >= stop then idx else let curr_width = force_width set idx 1 in if acc_width + curr_width > column then idx else get ( idx + 1 ) 1 ( acc_width + curr_width ) curr_width in get start 0 |
type ' a t = { recording : bool signal ; set_recording : bool -> unit ; mutable tmp_macro : ' a list ; mutable macro : ' a list ; count : int signal ; set_count : int -> unit ; counter : int signal ; set_counter : int -> unit ; } |
let create macro = let recording , set_recording = S . create false in let count , set_count = S . create 0 in let counter , set_counter = S . create 0 in { recording ; set_recording ; macro ; tmp_macro = [ ] ; count ; set_count ; counter ; set_counter ; } |
let recording r = r . recording |
let get_recording r = S . value r . recording |
let set_recording r state = match state with | true -> r . tmp_macro <- [ ] ; r . set_recording true ; r . set_count 0 ; r . set_counter 0 | false -> if S . value r . recording then begin r . macro <- List . rev r . tmp_macro ; r . tmp_macro <- [ ] ; r . set_recording false ; r . set_count 0 end |
let cancel r = if S . value r . recording then begin r . tmp_macro <- [ ] ; r . set_recording false ; r . set_count 0 end |
let count r = r . count |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.