2017-01-29 00:03:23 +00:00
/*
4 coder_clipboard . cpp - Copy paste commands and clipboard related setup .
*/
// TOP
2019-02-26 23:08:42 +00:00
static b32
2019-06-01 23:58:28 +00:00
post_buffer_range_to_clipboard ( Application_Links * app , i32 clipboard_index , Buffer_ID buffer , i32 first , i32 one_past_last ) {
2019-02-26 23:08:42 +00:00
b32 success = false ;
2019-04-05 02:03:36 +00:00
i32 buffer_size = 0 ;
buffer_get_size ( app , buffer , & buffer_size ) ;
if ( buffer ! = 0 & & 0 < = first & & first < one_past_last & & one_past_last < = buffer_size ) {
2019-06-01 23:58:28 +00:00
Scratch_Block scratch ( app ) ;
Range range = make_range ( first , one_past_last ) ;
2019-06-02 03:07:57 +00:00
String_Const_u8 string = push_buffer_range ( app , scratch , buffer , range ) ;
2019-06-01 23:58:28 +00:00
if ( string . size > 0 ) {
clipboard_post ( app , clipboard_index , string ) ;
2018-05-09 18:58:21 +00:00
success = true ;
2017-01-29 00:03:23 +00:00
}
}
2018-05-09 18:58:21 +00:00
return ( success ) ;
2017-01-29 00:03:23 +00:00
}
2017-11-15 23:57:21 +00:00
CUSTOM_COMMAND_SIG ( copy )
CUSTOM_DOC ( " Copy the text in the range from the cursor to the mark onto the clipboard. " )
{
2019-04-06 21:13:49 +00:00
View_ID view = 0 ;
get_active_view ( app , AccessProtected , & view ) ;
2019-04-05 02:03:36 +00:00
Buffer_ID buffer = 0 ;
2019-04-06 21:13:49 +00:00
view_get_buffer ( app , view , AccessProtected , & buffer ) ;
Range range = get_view_range ( app , view ) ;
2019-06-01 23:58:28 +00:00
post_buffer_range_to_clipboard ( app , 0 , buffer , range . min , range . max ) ;
2017-01-29 00:03:23 +00:00
}
2017-11-15 23:57:21 +00:00
CUSTOM_COMMAND_SIG ( cut )
CUSTOM_DOC ( " Cut the text in the range from the cursor to the mark onto the clipboard. " )
{
2019-04-06 21:13:49 +00:00
View_ID view = 0 ;
get_active_view ( app , AccessOpen , & view ) ;
2019-04-05 02:03:36 +00:00
Buffer_ID buffer = 0 ;
2019-04-06 21:13:49 +00:00
view_get_buffer ( app , view , AccessOpen , & buffer ) ;
Range range = get_view_range ( app , view ) ;
2019-06-01 23:58:28 +00:00
if ( post_buffer_range_to_clipboard ( app , 0 , buffer , range . min , range . max ) ) {
buffer_replace_range ( app , buffer , range , string_u8_litexpr ( " " ) ) ;
2018-05-09 18:58:21 +00:00
}
2017-01-29 00:03:23 +00:00
}
2017-11-15 23:57:21 +00:00
CUSTOM_COMMAND_SIG ( paste )
CUSTOM_DOC ( " At the cursor, insert the text at the top of the clipboard. " )
{
2019-06-01 23:58:28 +00:00
i32 count = 0 ;
clipboard_count ( app , 0 , & count ) ;
2017-01-29 00:03:23 +00:00
if ( count > 0 ) {
2019-04-06 21:13:49 +00:00
View_ID view = 0 ;
get_active_view ( app , AccessOpen , & view ) ;
if_view_has_highlighted_range_delete_range ( app , view ) ;
2018-09-30 12:14:47 +00:00
2019-06-01 23:58:28 +00:00
Managed_Scope scope = 0 ;
view_get_managed_scope ( app , view , & scope ) ;
2018-09-07 22:39:33 +00:00
managed_variable_set ( app , scope , view_next_rewrite_loc , RewritePaste ) ;
2019-02-26 23:08:42 +00:00
i32 paste_index = 0 ;
2018-09-07 22:39:33 +00:00
managed_variable_set ( app , scope , view_paste_index_loc , paste_index ) ;
2017-01-29 00:03:23 +00:00
2019-06-01 23:58:28 +00:00
Scratch_Block scratch ( app ) ;
2017-01-29 00:03:23 +00:00
2019-06-01 23:58:28 +00:00
String_Const_u8 string = { } ;
clipboard_index ( app , 0 , paste_index , scratch , & string ) ;
if ( string . size > 0 ) {
2019-04-05 02:03:36 +00:00
Buffer_ID buffer = 0 ;
2019-04-06 21:13:49 +00:00
view_get_buffer ( app , view , AccessOpen , & buffer ) ;
2019-04-05 02:03:36 +00:00
2019-04-06 21:13:49 +00:00
i32 pos = 0 ;
view_get_cursor_pos ( app , view , & pos ) ;
2019-06-01 23:58:28 +00:00
buffer_replace_range ( app , buffer , make_range ( pos ) , string ) ;
2019-04-06 21:13:49 +00:00
view_set_mark ( app , view , seek_pos ( pos ) ) ;
2019-06-01 23:58:28 +00:00
view_set_cursor ( app , view , seek_pos ( pos + ( i32 ) string . size ) , true ) ;
2017-01-29 00:03:23 +00:00
// TODO(allen): Send this to all views.
2018-11-20 08:18:54 +00:00
Theme_Color paste = { } ;
2017-01-29 00:03:23 +00:00
paste . tag = Stag_Paste ;
get_theme_colors ( app , & paste , 1 ) ;
2019-06-01 23:58:28 +00:00
view_post_fade ( app , view , 0.667f , pos , pos + ( i32 ) string . size , paste . color ) ;
2017-01-29 00:03:23 +00:00
}
}
}
2017-11-15 23:57:21 +00:00
CUSTOM_COMMAND_SIG ( paste_next )
CUSTOM_DOC ( " If the previous command was paste or paste_next, replaces the paste range with the next text down on the clipboard, otherwise operates as the paste command. " )
{
2019-06-01 23:58:28 +00:00
Scratch_Block scratch ( app ) ;
i32 count = 0 ;
clipboard_count ( app , 0 , & count ) ;
2017-01-29 00:03:23 +00:00
if ( count > 0 ) {
2019-04-06 21:13:49 +00:00
View_ID view = 0 ;
get_active_view ( app , AccessOpen , & view ) ;
2019-06-01 23:58:28 +00:00
Managed_Scope scope = 0 ;
view_get_managed_scope ( app , view , & scope ) ;
2018-09-30 12:14:47 +00:00
no_mark_snap_to_cursor ( app , scope ) ;
2017-01-29 00:03:23 +00:00
2019-02-27 05:49:35 +00:00
u64 rewrite = 0 ;
2018-09-07 22:39:33 +00:00
managed_variable_get ( app , scope , view_rewrite_loc , & rewrite ) ;
2018-06-23 03:03:58 +00:00
if ( rewrite = = RewritePaste ) {
2018-09-07 22:39:33 +00:00
managed_variable_set ( app , scope , view_next_rewrite_loc , RewritePaste ) ;
2019-02-27 05:49:35 +00:00
u64 prev_paste_index = 0 ;
2018-09-07 22:39:33 +00:00
managed_variable_get ( app , scope , view_paste_index_loc , & prev_paste_index ) ;
2019-02-26 23:08:42 +00:00
i32 paste_index = ( i32 ) prev_paste_index + 1 ;
2018-09-07 22:39:33 +00:00
managed_variable_set ( app , scope , view_paste_index_loc , paste_index ) ;
2017-01-29 00:03:23 +00:00
2019-06-01 23:58:28 +00:00
String_Const_u8 string = { } ;
clipboard_index ( app , 0 , paste_index , scratch , & string ) ;
Buffer_ID buffer = 0 ;
view_get_buffer ( app , view , AccessOpen , & buffer ) ;
Range range = get_view_range ( app , view ) ;
i32 pos = range . min ;
2017-01-29 00:03:23 +00:00
2019-06-01 23:58:28 +00:00
buffer_replace_range ( app , buffer , range , string ) ;
view_set_cursor ( app , view , seek_pos ( pos + ( i32 ) string . size ) , true ) ;
// TODO(allen): Send this to all views.
Theme_Color paste = { } ;
paste . tag = Stag_Paste ;
get_theme_colors ( app , & paste , 1 ) ;
view_post_fade ( app , view , 0.667f , pos , pos + ( i32 ) string . size , paste . color ) ;
2017-01-29 00:03:23 +00:00
}
else {
exec_command ( app , paste ) ;
}
}
}
2018-05-10 08:12:47 +00:00
CUSTOM_COMMAND_SIG ( paste_and_indent )
CUSTOM_DOC ( " Paste from the top of clipboard and run auto-indent on the newly pasted text. " )
{
paste ( app ) ;
auto_tab_range ( app ) ;
}
CUSTOM_COMMAND_SIG ( paste_next_and_indent )
CUSTOM_DOC ( " Paste the next item on the clipboard and run auto-indent on the newly pasted text. " )
{
paste_next ( app ) ;
auto_tab_range ( app ) ;
}
2017-01-29 00:03:23 +00:00
// BOTTOM