robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1#include <stdbool.h> 2#include <stdint.h> 3#include <string.h> 4 5#include "nvim/api/private/defs.h" 6#include "nvim/api/private/helpers.h" 7#include "nvim/ascii_defs.h" 8#include "nvim/charset.h" 9#include "nvim/cursor_shape.h" 10#include "nvim/ex_getln.h" 11#include "nvim/gettext_defs.h" 12#include "nvim/globals.h" 13#include "nvim/highlight_group.h" 14#include "nvim/log.h" 15#include "nvim/macros_defs.h" 16#include "nvim/option_vars.h" 17#include "nvim/state_defs.h" 18#include "nvim/strings.h" 19#include "nvim/ui.h" 20 21#include "cursor_shape.c.generated.h" 22 23static const char e_digit_expected []= N_ ("E548: Digit expected" ); 24 25/// Handling of cursor and mouse pointer shapes in various modes. 26cursorentry_T shape_table [SHAPE_IDX_COUNT ]= { 27// Values are set by 'guicursor' and 'mouseshape'. 28// Adjust the SHAPE_IDX_ defines when changing this! 29 {"normal" ,0 ,0 ,0 ,700 ,400 ,250 ,0 ,0 ,"n" ,SHAPE_CURSOR + SHAPE_MOUSE }, 30 {"visual" ,0 ,0 ,0 ,700 ,400 ,250 ,0 ,0 ,"v" ,SHAPE_CURSOR + SHAPE_MOUSE }, 31 {"insert" ,0 ,0 ,0 ,700 ,400 ,250 ,0 ,0 ,"i" ,SHAPE_CURSOR + SHAPE_MOUSE }, 32 {"replace" ,0 ,0 ,0 ,700 ,400 ,250 ,0 ,0 ,"r" ,SHAPE_CURSOR + SHAPE_MOUSE }, 33 {"cmdline_normal" ,0 ,0 ,0 ,700 ,400 ,250 ,0 ,0 ,"c" ,SHAPE_CURSOR + SHAPE_MOUSE }, 34 {"cmdline_insert" ,0 ,0 ,0 ,700 ,400 ,250 ,0 ,0 ,"ci" ,SHAPE_CURSOR + SHAPE_MOUSE }, 35 {"cmdline_replace" ,0 ,0 ,0 ,700 ,400 ,250 ,0 ,0 ,"cr" ,SHAPE_CURSOR + SHAPE_MOUSE }, 36 {"operator" ,0 ,0 ,0 ,700 ,400 ,250 ,0 ,0 ,"o" ,SHAPE_CURSOR + SHAPE_MOUSE }, 37 {"visual_select" ,0 ,0 ,0 ,700 ,400 ,250 ,0 ,0 ,"ve" ,SHAPE_CURSOR + SHAPE_MOUSE }, 38 {"cmdline_hover" ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"e" ,SHAPE_MOUSE }, 39 {"statusline_hover" ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"s" ,SHAPE_MOUSE }, 40 {"statusline_drag" ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"sd" ,SHAPE_MOUSE }, 41 {"vsep_hover" ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"vs" ,SHAPE_MOUSE }, 42 {"vsep_drag" ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"vd" ,SHAPE_MOUSE }, 43 {"more" ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"m" ,SHAPE_MOUSE }, 44 {"more_lastline" ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"ml" ,SHAPE_MOUSE }, 45 {"showmatch" ,0 ,0 ,0 ,100 ,100 ,100 ,0 ,0 ,"sm" ,SHAPE_CURSOR }, 46 {"terminal" ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"t" ,SHAPE_CURSOR }, 47}; 48 49/// Converts cursor_shapes into an Array of Dictionaries 50/// @param arena initialized arena where memory will be allocated 51/// 52/// @return Array of the form {[ "cursor_shape": ... ], ...} 53Array mode_style_array (Arena * arena ) 54{ 55Array all = arena_array (arena ,SHAPE_IDX_COUNT ); 56 57for (int i = 0 ;i < SHAPE_IDX_COUNT ;i ++ ) { 58cursorentry_T * cur = & shape_table [i ]; 59Dict dic = arena_dict (arena ,3 + ((cur -> used_for & SHAPE_CURSOR ) ?9 :0 )); 60PUT_C (dic ,"name" ,CSTR_AS_OBJ (cur -> full_name )); 61PUT_C (dic ,"short_name" ,CSTR_AS_OBJ (cur -> name )); 62if (cur -> used_for & SHAPE_MOUSE ) { 63PUT_C (dic ,"mouse_shape" ,INTEGER_OBJ (cur -> mshape )); 64 } 65if (cur -> used_for & SHAPE_CURSOR ) { 66String shape_str ; 67switch (cur -> shape ) { 68case SHAPE_BLOCK : 69shape_str = cstr_as_string ("block" );break ; 70case SHAPE_VER : 71shape_str = cstr_as_string ("vertical" );break ; 72case SHAPE_HOR : 73shape_str = cstr_as_string ("horizontal" );break ; 74default : 75shape_str = cstr_as_string ("unknown" ); 76 } 77PUT_C (dic ,"cursor_shape" ,STRING_OBJ (shape_str )); 78PUT_C (dic ,"cell_percentage" ,INTEGER_OBJ (cur -> percentage )); 79PUT_C (dic ,"blinkwait" ,INTEGER_OBJ (cur -> blinkwait )); 80PUT_C (dic ,"blinkon" ,INTEGER_OBJ (cur -> blinkon )); 81PUT_C (dic ,"blinkoff" ,INTEGER_OBJ (cur -> blinkoff )); 82PUT_C (dic ,"hl_id" ,INTEGER_OBJ (cur -> id )); 83PUT_C (dic ,"id_lm" ,INTEGER_OBJ (cur -> id_lm )); 84PUT_C (dic ,"attr_id" ,INTEGER_OBJ (cur -> id ?syn_id2attr (cur -> id ) :0 )); 85PUT_C (dic ,"attr_id_lm" ,INTEGER_OBJ (cur -> id_lm ?syn_id2attr (cur -> id_lm ) :0 )); 86 } 87 88ADD_C (all ,DICT_OBJ (dic )); 89 } 90 91return all ; 92} 93 94/// Parses the 'guicursor' option. 95/// 96/// Clears `shape_table` if 'guicursor' is empty. 97/// 98/// @param what SHAPE_CURSOR or SHAPE_MOUSE ('mouseshape') 99/// 100/// @returns error message for an illegal option, NULL otherwise. 101const char * parse_shape_opt (int what ) 102{ 103char * p = NULL ; 104int idx = 0 ;// init for GCC 105int len ; 106bool found_ve = false;// found "ve" flag 107 108// First round: check for errors; second round: do it for real. 109for (int round = 1 ;round <=2 ;round ++ ) { 110if (round == 2 || * p_guicursor == NUL ) { 111// Set all entries to default (block, blinkon0, default color). 112// This is the default for anything that is not set. 113clear_shape_table (); 114if (* p_guicursor == NUL ) { 115ui_mode_info_set (); 116return NULL ; 117 } 118 } 119// Repeat for all comma separated parts. 120char * modep = p_guicursor ; 121while (modep != NULL && * modep != NUL ) { 122char * colonp = vim_strchr (modep ,':' ); 123char * commap = vim_strchr (modep ,',' ); 124 125if (colonp == NULL || (commap != NULL && commap < colonp )) { 126return N_ ("E545: Missing colon" ); 127 } 128if (colonp == modep ) { 129return N_ ("E546: Illegal mode" ); 130 } 131 132// Repeat for all modes before the colon. 133// For the 'a' mode, we loop to handle all the modes. 134int all_idx = -1 ; 135while (modep < colonp || all_idx >=0 ) { 136if (all_idx < 0 ) { 137// Find the mode 138if (modep [1 ]== '-' || modep [1 ]== ':' ) { 139len = 1 ; 140 }else { 141len = 2 ; 142 } 143 144if (len == 1 && TOLOWER_ASC (modep [0 ])== 'a' ) { 145all_idx = SHAPE_IDX_COUNT - 1 ; 146 }else { 147for (idx = 0 ;idx < SHAPE_IDX_COUNT ;idx ++ ) { 148if (STRNICMP (modep ,shape_table [idx ].name , (size_t )len )== 0 ) { 149break ; 150 } 151 } 152if (idx == SHAPE_IDX_COUNT 153|| (shape_table [idx ].used_for & what )== 0 ) { 154return N_ ("E546: Illegal mode" ); 155 } 156if (len == 2 && modep [0 ]== 'v' && modep [1 ]== 'e' ) { 157found_ve = true; 158 } 159 } 160modep += len + 1 ; 161 } 162 163if (all_idx >=0 ) { 164idx = all_idx -- ; 165 } 166 167// Parse the part after the colon 168for (p = colonp + 1 ;* p && * p != ',' ;) { 169 { 170// First handle the ones with a number argument. 171int i = (uint8_t )(* p ); 172len = 0 ; 173if (STRNICMP (p ,"ver" ,3 )== 0 ) { 174len = 3 ; 175 }else if (STRNICMP (p ,"hor" ,3 )== 0 ) { 176len = 3 ; 177 }else if (STRNICMP (p ,"blinkwait" ,9 )== 0 ) { 178len = 9 ; 179 }else if (STRNICMP (p ,"blinkon" ,7 )== 0 ) { 180len = 7 ; 181 }else if (STRNICMP (p ,"blinkoff" ,8 )== 0 ) { 182len = 8 ; 183 } 184if (len != 0 ) { 185p += len ; 186if (!ascii_isdigit (* p )) { 187return e_digit_expected ; 188 } 189int n = getdigits_int (& p , false,0 ); 190if (len == 3 ) {// "ver" or "hor" 191if (n == 0 ) { 192return N_ ("E549: Illegal percentage" ); 193 } 194if (round == 2 ) { 195if (TOLOWER_ASC (i )== 'v' ) { 196shape_table [idx ].shape = SHAPE_VER ; 197 }else { 198shape_table [idx ].shape = SHAPE_HOR ; 199 } 200shape_table [idx ].percentage = n ; 201 } 202 }else if (round == 2 ) { 203if (len == 9 ) { 204shape_table [idx ].blinkwait = n ; 205 }else if (len == 7 ) { 206shape_table [idx ].blinkon = n ; 207 }else { 208shape_table [idx ].blinkoff = n ; 209 } 210 } 211 }else if (STRNICMP (p ,"block" ,5 )== 0 ) { 212if (round == 2 ) { 213shape_table [idx ].shape = SHAPE_BLOCK ; 214 } 215p += 5 ; 216 }else {// must be a highlight group name then 217char * endp = vim_strchr (p ,'-' ); 218if (commap == NULL ) {// last part 219if (endp == NULL ) { 220endp = p + strlen (p );// find end of part 221 } 222 }else if (endp > commap || endp == NULL ) { 223endp = commap ; 224 } 225char * slashp = vim_strchr (p ,'/' ); 226if (slashp != NULL && slashp < endp ) { 227// "group/langmap_group" 228i = syn_check_group (p , (size_t )(slashp - p )); 229p = slashp + 1 ; 230 } 231if (round == 2 ) { 232shape_table [idx ].id = syn_check_group (p , (size_t )(endp - p )); 233shape_table [idx ].id_lm = shape_table [idx ].id ; 234if (slashp != NULL && slashp < endp ) { 235shape_table [idx ].id = i ; 236 } 237 } 238p = endp ; 239 } 240 }// if (what != SHAPE_MOUSE) 241 242if (* p == '-' ) { 243p ++ ; 244 } 245 } 246 } 247modep = p ; 248if (modep != NULL && * modep == ',' ) { 249modep ++ ; 250 } 251 } 252 } 253 254// If the 's' flag is not given, use the 'v' cursor for 's' 255if (!found_ve ) { 256 { 257shape_table [SHAPE_IDX_VE ].shape = shape_table [SHAPE_IDX_V ].shape ; 258shape_table [SHAPE_IDX_VE ].percentage = 259shape_table [SHAPE_IDX_V ].percentage ; 260shape_table [SHAPE_IDX_VE ].blinkwait = 261shape_table [SHAPE_IDX_V ].blinkwait ; 262shape_table [SHAPE_IDX_VE ].blinkon = 263shape_table [SHAPE_IDX_V ].blinkon ; 264shape_table [SHAPE_IDX_VE ].blinkoff = 265shape_table [SHAPE_IDX_V ].blinkoff ; 266shape_table [SHAPE_IDX_VE ].id = shape_table [SHAPE_IDX_V ].id ; 267shape_table [SHAPE_IDX_VE ].id_lm = shape_table [SHAPE_IDX_V ].id_lm ; 268 } 269 } 270ui_mode_info_set (); 271return NULL ; 272} 273 274/// Map cursor mode from string to integer 275/// 276/// @param mode Fullname of the mode whose id we are looking for 277/// @return -1 in case of failure, else the matching SHAPE_ID* integer 278int cursor_mode_str2int (const char * mode ) 279{ 280for (int mode_idx = 0 ;mode_idx < SHAPE_IDX_COUNT ;mode_idx ++ ) { 281if (strcmp (shape_table [mode_idx ].full_name ,mode )== 0 ) { 282return mode_idx ; 283 } 284 } 285WLOG ("Unknown mode %s" ,mode ); 286return -1 ; 287} 288 289/// Check if a syntax id is used as a cursor style. 290bool cursor_mode_uses_syn_id (int syn_id ) 291FUNC_ATTR_PURE 292{ 293if (* p_guicursor == NUL ) { 294return false; 295 } 296for (int mode_idx = 0 ;mode_idx < SHAPE_IDX_COUNT ;mode_idx ++ ) { 297if (shape_table [mode_idx ].id == syn_id 298|| shape_table [mode_idx ].id_lm == syn_id ) { 299return true; 300 } 301 } 302return false; 303} 304 305/// Return the index into shape_table[] for the current mode. 306int cursor_get_mode_idx (void ) 307FUNC_ATTR_PURE 308{ 309if (State == MODE_SHOWMATCH ) { 310return SHAPE_IDX_SM ; 311 }else if (State == MODE_TERMINAL ) { 312return SHAPE_IDX_TERM ; 313 }else if (State & VREPLACE_FLAG ) { 314return SHAPE_IDX_R ; 315 }else if (State & REPLACE_FLAG ) { 316return SHAPE_IDX_R ; 317 }else if (State & MODE_INSERT ) { 318return SHAPE_IDX_I ; 319 }else if (State & MODE_CMDLINE ) { 320if (cmdline_at_end ()) { 321return SHAPE_IDX_C ; 322 }else if (cmdline_overstrike ()) { 323return SHAPE_IDX_CR ; 324 }else { 325return SHAPE_IDX_CI ; 326 } 327 }else if (finish_op ) { 328return SHAPE_IDX_O ; 329 }else if (Visual .active ) { 330if (* p_sel == 'e' ) { 331return SHAPE_IDX_VE ; 332 }else { 333return SHAPE_IDX_V ; 334 } 335 }else { 336return SHAPE_IDX_N ; 337 } 338} 339 340/// Clears all entries in shape_table to block, blinkon0, and default color. 341static void clear_shape_table (void ) 342{ 343for (int idx = 0 ;idx < SHAPE_IDX_COUNT ;idx ++ ) { 344shape_table [idx ].shape = SHAPE_BLOCK ; 345shape_table [idx ].blinkwait = 0 ; 346shape_table [idx ].blinkon = 0 ; 347shape_table [idx ].blinkoff = 0 ; 348shape_table [idx ].id = 0 ; 349shape_table [idx ].id_lm = 0 ; 350 } 351}