comparison mcabber/mcabber/screen.c @ 2309:e00ae0763468

Do not use litteral command char (':', '/')
author Mikael Berthe <mikael@lilotux.net>
date Sat, 11 Feb 2017 12:40:43 +0100
parents e8c8665c824e
children 962c98e4864d
comparison
equal deleted inserted replaced
2308:e8c8665c824e 2309:e00ae0763468
781 gboolean scr_curses_status(void) 781 gboolean scr_curses_status(void)
782 { 782 {
783 return Curses; 783 return Curses;
784 } 784 }
785 785
786 static gchar *scr_vi_mode_guard(const gchar *key, const gchar *new_value)
787 {
788 int new_mode = 0;
789 if (new_value)
790 new_mode = atoi(new_value);
791 if (new_mode == 0 || new_mode == 1)
792 vi_mode = new_mode;
793 return g_strdup(new_value);
794 }
795
786 static gchar *scr_color_guard(const gchar *key, const gchar *new_value) 796 static gchar *scr_color_guard(const gchar *key, const gchar *new_value)
787 { 797 {
788 if (g_strcmp0(settings_opt_get(key), new_value)) 798 if (g_strcmp0(settings_opt_get(key), new_value))
789 colors_stalled = TRUE; 799 colors_stalled = TRUE;
790 return g_strdup(new_value); 800 return g_strdup(new_value);
812 ESCDELAY = (unsigned) settings_opt_get_int("escdelay"); 822 ESCDELAY = (unsigned) settings_opt_get_int("escdelay");
813 #else 823 #else
814 scr_LogPrint(LPRINT_LOGNORM, "ERROR: no ESCDELAY support."); 824 scr_LogPrint(LPRINT_LOGNORM, "ERROR: no ESCDELAY support.");
815 #endif 825 #endif
816 } 826 }
827
828 // Set up vi_mode guard
829 settings_set_guard("vi_mode", scr_vi_mode_guard);
817 830
818 parse_colors(); 831 parse_colors();
819 832
820 settings_set_guard("color_background", scr_color_guard); 833 settings_set_guard("color_background", scr_color_guard);
821 settings_set_guard("color_general", scr_color_guard); 834 settings_set_guard("color_general", scr_color_guard);
3807 void readline_disable_chat_mode(guint show_roster) 3820 void readline_disable_chat_mode(guint show_roster)
3808 { 3821 {
3809 scr_check_auto_away(TRUE); 3822 scr_check_auto_away(TRUE);
3810 if (chatmode) { 3823 if (chatmode) {
3811 scr_buffer_readmark(TRUE); 3824 scr_buffer_readmark(TRUE);
3812 if (settings_opt_get_int("vi_mode")) 3825 if (vi_mode)
3813 clear_inputline(); 3826 clear_inputline();
3814 } 3827 }
3815 currentWindow = NULL; 3828 currentWindow = NULL;
3816 chatmode = FALSE; 3829 chatmode = FALSE;
3817 if (current_buddy) 3830 if (current_buddy)
4179 #endif 4192 #endif
4180 4193
4181 static inline void refresh_inputline(void) 4194 static inline void refresh_inputline(void)
4182 { 4195 {
4183 #if defined(WITH_ENCHANT) || defined(WITH_ASPELL) 4196 #if defined(WITH_ENCHANT) || defined(WITH_ASPELL)
4184 if (settings_opt_get_int("spell_enable") && 4197 if (settings_opt_get_int("spell_enable") && (chatmode || !vi_mode)) {
4185 (chatmode || !settings_opt_get_int("vi_mode"))) {
4186 memset(maskLine, 0, INPUTLINE_LENGTH+1); 4198 memset(maskLine, 0, INPUTLINE_LENGTH+1);
4187 spellcheck(inputLine, maskLine); 4199 spellcheck(inputLine, maskLine);
4188 } 4200 }
4189 print_checked_line(); 4201 print_checked_line();
4190 wclrtoeol(inputWnd); 4202 wclrtoeol(inputWnd);
4434 } 4446 }
4435 4447
4436 static void scr_process_vi_arrow_key(int key) 4448 static void scr_process_vi_arrow_key(int key)
4437 { 4449 {
4438 const char *l; 4450 const char *l;
4439 char mask[INPUTLINE_LENGTH+1] = "/roster search "; 4451 char mask[INPUTLINE_LENGTH+1];
4440 size_t cmd_len = strlen(mask); 4452 size_t cmd_len = strlen(mask);
4441 size_t str_len = strlen(inputLine) - 1; 4453 size_t str_len = strlen(inputLine) - 1;
4442 4454
4443 switch (inputLine[0]) { 4455 strncpy(mask, mkcmdstr("roster search "), INPUTLINE_LENGTH);
4444 case ':': 4456
4445 inputLine[0] = '/'; 4457 if (inputLine[0] == COMMAND_CHAR) {
4446 if (key == KEY_UP) 4458 if (!chatmode) { // Command mode
4447 l = scr_cmdhisto_prev(inputLine, ptr_inputline - inputLine); 4459 if (key == KEY_UP)
4448 else 4460 l = scr_cmdhisto_prev(inputLine, ptr_inputline - inputLine);
4449 l = scr_cmdhisto_next(inputLine, ptr_inputline - inputLine); 4461 else
4450 if (l) 4462 l = scr_cmdhisto_next(inputLine, ptr_inputline - inputLine);
4451 strcpy(inputLine, l); 4463 if (l)
4452 inputLine[0] = ':'; 4464 strcpy(inputLine, l);
4453 break; 4465
4454 case '/': 4466 return;
4455 if (cmd_len + str_len > INPUTLINE_LENGTH) 4467 }
4456 return; 4468
4457 4469 // Chat mode
4458 memcpy(mask + cmd_len, inputLine + 1, str_len + 1); 4470
4459 if (key == KEY_UP) 4471 if (cmd_len + str_len > INPUTLINE_LENGTH)
4460 l = scr_cmdhisto_prev(mask, ptr_inputline - inputLine + cmd_len - 1); 4472 return;
4461 else 4473
4462 l = scr_cmdhisto_next(mask, ptr_inputline - inputLine + cmd_len - 1); 4474 memcpy(mask + cmd_len, inputLine + 1, str_len + 1);
4463 if (l) 4475 if (key == KEY_UP)
4464 strcpy(inputLine + 1, l + cmd_len); 4476 l = scr_cmdhisto_prev(mask, ptr_inputline - inputLine + cmd_len - 1);
4465 break; 4477 else
4466 default: 4478 l = scr_cmdhisto_next(mask, ptr_inputline - inputLine + cmd_len - 1);
4467 if (key == KEY_UP) 4479 if (l)
4468 process_command(mkcmdstr("roster up"), TRUE); 4480 strcpy(inputLine + 1, l + cmd_len);
4469 else 4481
4470 process_command(mkcmdstr("roster down"), TRUE); 4482 return;
4471 break; 4483 }
4472 } 4484
4485 if (inputLine[0] == VI_SEARCH_COMMAND_CHAR)
4486 return;
4487
4488 if (key == KEY_UP)
4489 process_command(mkcmdstr("roster up"), TRUE);
4490 else if (key == KEY_DOWN)
4491 process_command(mkcmdstr("roster down"), TRUE);
4473 } 4492 }
4474 4493
4475 // scr_process_key(key) 4494 // scr_process_key(key)
4476 // Handle the pressed key, in the command line (bottom). 4495 // Handle the pressed key, in the command line (bottom).
4477 void scr_process_key(keycode kcode) 4496 void scr_process_key(keycode kcode)
4478 { 4497 {
4479 int key = kcode.value; 4498 int key = kcode.value;
4480 int display_char = FALSE; 4499 int display_char = FALSE;
4481 int vi_completion = FALSE;
4482 int vi_search = FALSE; 4500 int vi_search = FALSE;
4483 static int ex_or_search_mode = FALSE; 4501 static int ex_or_search_mode = FALSE;
4484 4502
4485 lock_chatstate = FALSE; 4503 lock_chatstate = FALSE;
4486 4504
4495 default: 4513 default:
4496 bindcommand(kcode); 4514 bindcommand(kcode);
4497 key = ERR; // Do not process any further 4515 key = ERR; // Do not process any further
4498 } 4516 }
4499 4517
4500 if (settings_opt_get_int("vi_mode") && !chatmode) { 4518 if (vi_mode && !chatmode) {
4501 int got_cmd_prefix = FALSE; 4519 int got_cmd_prefix = FALSE;
4502 int unrecognized = FALSE; 4520 int unrecognized = FALSE;
4503 static char search_cmd[INPUTLINE_LENGTH+1] = "/roster search "; 4521 static char search_cmd[INPUTLINE_LENGTH+1] = "";
4504 4522
4505 if (key == KEY_UP || key == KEY_DOWN) { 4523 if (key == KEY_UP || key == KEY_DOWN) {
4506 scr_process_vi_arrow_key(key); 4524 scr_process_vi_arrow_key(key);
4507 key = ERR; // Do not process any further 4525 key = ERR; // Do not process any further
4508 } else if (ex_or_search_mode) { 4526 } else if (ex_or_search_mode) {
4511 clear_inputline(); 4529 clear_inputline();
4512 ex_or_search_mode = FALSE; 4530 ex_or_search_mode = FALSE;
4513 break; 4531 break;
4514 case 9: // Tab 4532 case 9: // Tab
4515 case 353: // Shift-Tab 4533 case 353: // Shift-Tab
4516 switch (inputLine[0]) { 4534 if (inputLine[0] == VI_SEARCH_COMMAND_CHAR)
4517 case ':': 4535 vi_search = TRUE;
4518 inputLine[0] = '/';
4519 vi_completion = TRUE;
4520 break;
4521 case '/':
4522 vi_search = TRUE;
4523 break;
4524 }
4525 break; 4536 break;
4526 case 13: // Enter 4537 case 13: // Enter
4527 case 343: // Enter on Maemo 4538 case 343: // Enter on Maemo
4528 switch (inputLine[0]) { 4539 if (inputLine[0] == COMMAND_CHAR) {
4529 case ':': 4540 {
4530 { 4541 char *p = strchr(inputLine, 0);
4531 char *p = strchr(inputLine, 0); 4542
4532 4543 while (*--p == ' ' && p > inputLine)
4533 while (*--p == ' ' && p > inputLine) 4544 *p = 0;
4534 *p = 0; 4545 }
4535 } 4546 if (!strcmp(inputLine, mkcmdstr("x")) ||
4536 if (!strcmp(inputLine, ":x") || 4547 !strcmp(inputLine, mkcmdstr("q")) ||
4537 !strcmp(inputLine, ":q") || 4548 !strcmp(inputLine, mkcmdstr("wq")))
4538 !strcmp(inputLine, ":wq")) 4549 strcpy(inputLine, mkcmdstr("quit"));
4539 strcpy(inputLine, ":quit"); 4550 if (isdigit((int)(unsigned char)inputLine[1]) &&
4540 if (isdigit((int)(unsigned char)inputLine[1]) && 4551 strlen(inputLine) <= 9) {
4541 strlen(inputLine) <= 9) { 4552 process_command(mkcmdstr("roster top"), TRUE);
4542 process_command(mkcmdstr("roster top"), TRUE); 4553 memcpy(inputLine + 13, inputLine + 1, 10);
4543 memcpy(inputLine + 13, inputLine + 1, 10); 4554 memcpy(inputLine + 1, "roster down ", 12);
4544 memcpy(inputLine + 1, "roster down ", 12); 4555 }
4545 } 4556 inputLine[0] = COMMAND_CHAR;
4546 inputLine[0] = '/'; 4557 process_command(inputLine, TRUE);
4547 process_command(inputLine, TRUE); 4558 scr_cmdhisto_addline(inputLine);
4548 scr_cmdhisto_addline(inputLine); 4559 } else if (inputLine[0] == VI_SEARCH_COMMAND_CHAR) {
4549 break; 4560 size_t cmd_len;
4550 case '/': 4561 size_t str_len = strlen(inputLine) - 1;
4551 { 4562
4552 size_t cmd_len = sizeof("/roster search ") - 1; 4563 strncpy(search_cmd, mkcmdstr("roster search "), INPUTLINE_LENGTH);
4553 size_t str_len = strlen(inputLine) - 1; 4564 cmd_len = strlen(search_cmd);
4554 4565
4555 if (cmd_len + str_len > INPUTLINE_LENGTH) 4566 if (cmd_len + str_len > INPUTLINE_LENGTH)
4556 return; 4567 return;
4557 4568
4558 memcpy(search_cmd + cmd_len, inputLine + 1, 4569 memcpy(search_cmd + cmd_len, inputLine + 1, str_len + 1);
4559 str_len + 1); 4570
4560 } 4571 process_command(search_cmd, TRUE);
4561 process_command(search_cmd, TRUE); 4572 scr_cmdhisto_addline(search_cmd);
4562 scr_cmdhisto_addline(search_cmd); 4573 } else if (inputLine[0] == 0) {
4563 break; 4574 if (buddy_gettype(BUDDATA(current_buddy)) ==
4564 case 0: 4575 ROSTER_TYPE_GROUP)
4565 { 4576 process_command(mkcmdstr("group toggle"), TRUE);
4566 if (buddy_gettype(BUDDATA(current_buddy)) == 4577 else
4567 ROSTER_TYPE_GROUP) 4578 open_chat_window();
4568 process_command(mkcmdstr("group toggle"), TRUE);
4569 else
4570 open_chat_window();
4571 }
4572 break;
4573 } 4579 }
4574 ex_or_search_mode = FALSE; 4580 ex_or_search_mode = FALSE;
4575 break;
4576 } 4581 }
4577 } else if (key >= '0' && key <= '9') { 4582 } else if (key >= '0' && key <= '9') {
4578 got_cmd_prefix = TRUE; 4583 got_cmd_prefix = TRUE;
4584 } else if (key == COMMAND_CHAR || key == VI_SEARCH_COMMAND_CHAR) {
4585 ex_or_search_mode = TRUE;
4586 cmdhisto_cur = NULL;
4579 } else { 4587 } else {
4580 switch (key) { 4588 switch (key) {
4581 case '/':
4582 case ':':
4583 ex_or_search_mode = TRUE;
4584 break;
4585 case ' ': 4589 case ' ':
4586 process_command(mkcmdstr("group toggle"), TRUE); 4590 process_command(mkcmdstr("group toggle"), TRUE);
4587 break; 4591 break;
4588 case '!': 4592 case '!':
4589 { 4593 {
4659 open_chat_window(); 4663 open_chat_window();
4660 break; 4664 break;
4661 case 'j': 4665 case 'j':
4662 if (isdigit((int)(unsigned char)inputLine[0]) && 4666 if (isdigit((int)(unsigned char)inputLine[0]) &&
4663 strlen(inputLine) <= 9) { 4667 strlen(inputLine) <= 9) {
4664 char down_cmd[32] = "/roster down "; 4668 char down_cmd[32];
4665 4669 strncpy(down_cmd, mkcmdstr("roster down "), 32);
4666 strcat(down_cmd, inputLine); 4670 strncat(down_cmd, inputLine, 16);
4667 process_command(down_cmd, TRUE); 4671 process_command(down_cmd, TRUE);
4668 } else 4672 } else
4669 process_command(mkcmdstr("roster down"), TRUE); 4673 process_command(mkcmdstr("roster down"), TRUE);
4670 break; 4674 break;
4671 case 'k': 4675 case 'k':
4672 if (isdigit((int)(unsigned char)inputLine[0]) && 4676 if (isdigit((int)(unsigned char)inputLine[0]) &&
4673 strlen(inputLine) <= 9) { 4677 strlen(inputLine) <= 9) {
4674 char up_cmd[32] = "/roster up "; 4678 char up_cmd[32];
4675 4679 strncpy(up_cmd, mkcmdstr("roster up "), 32);
4676 strcat(up_cmd, inputLine); 4680 strncat(up_cmd, inputLine, 16);
4677 process_command(up_cmd, TRUE); 4681 process_command(up_cmd, TRUE);
4678 } else 4682 } else
4679 process_command(mkcmdstr("roster up "), TRUE); 4683 process_command(mkcmdstr("roster up "), TRUE);
4680 break; 4684 break;
4681 case 'M': 4685 case 'M':
4682 if (inputLine[0] == 'z') { 4686 if (inputLine[0] == 'z') {
4683 GSList *groups = compl_list(ROSTER_TYPE_GROUP); 4687 GSList *groups = compl_list(ROSTER_TYPE_GROUP);
4684 GSList *g; 4688 GSList *g;
4685 4689
4686 for (g = groups; g; g = g_slist_next(g)) { 4690 for (g = groups; g; g = g_slist_next(g)) {
4687 char fold_cmd[128] = "/group fold "; 4691 char fold_cmd[256];
4688 size_t cmd_len = strlen(fold_cmd); 4692 size_t cmd_len, grp_len;
4689 size_t grp_len = strlen(g->data); 4693
4694 strncpy(fold_cmd, mkcmdstr("group fold "), 32);
4695 cmd_len = strlen(fold_cmd);
4696 grp_len = strlen(g->data);
4690 4697
4691 if (cmd_len + grp_len + 1 > sizeof(fold_cmd)) 4698 if (cmd_len + grp_len + 1 > sizeof(fold_cmd))
4692 continue; 4699 continue;
4693 memcpy(fold_cmd + cmd_len, g->data, grp_len + 1); 4700 memcpy(fold_cmd + cmd_len, g->data, grp_len + 1);
4694 process_command(fold_cmd, TRUE); 4701 process_command(fold_cmd, TRUE);
4713 if (inputLine[0] == 'z') { 4720 if (inputLine[0] == 'z') {
4714 GSList *groups = compl_list(ROSTER_TYPE_GROUP); 4721 GSList *groups = compl_list(ROSTER_TYPE_GROUP);
4715 GSList *g; 4722 GSList *g;
4716 4723
4717 for (g = groups; g; g = g_slist_next(g)) { 4724 for (g = groups; g; g = g_slist_next(g)) {
4718 char fold_cmd[128] = "/group unfold "; 4725 char fold_cmd[256];
4719 size_t cmd_len = strlen(fold_cmd); 4726 size_t cmd_len, grp_len;
4720 size_t grp_len = strlen(g->data); 4727
4728 strncpy(fold_cmd, mkcmdstr("group unfold "), 32);
4729 cmd_len = strlen(fold_cmd);
4730 grp_len = strlen(g->data);
4721 4731
4722 if (cmd_len + grp_len + 1 > sizeof(fold_cmd)) 4732 if (cmd_len + grp_len + 1 > sizeof(fold_cmd))
4723 continue; 4733 continue;
4724 memcpy(fold_cmd + cmd_len, g->data, grp_len + 1); 4734 memcpy(fold_cmd + cmd_len, g->data, grp_len + 1);
4725 process_command(fold_cmd, TRUE); 4735 process_command(fold_cmd, TRUE);
4829 } 4839 }
4830 } 4840 }
4831 4841
4832 if (completion_started && key != 9 && key != 353 && key != KEY_RESIZE) 4842 if (completion_started && key != 9 && key != 353 && key != KEY_RESIZE)
4833 scr_end_current_completion(); 4843 scr_end_current_completion();
4834 else if (vi_completion)
4835 inputLine[0] = ':';
4836 refresh_inputline(); 4844 refresh_inputline();
4837 4845
4838 if (ex_or_search_mode && inputLine[0] != ':' && inputLine[0] != '/') 4846 if (ex_or_search_mode &&
4847 inputLine[0] != COMMAND_CHAR && inputLine[0] != VI_SEARCH_COMMAND_CHAR)
4839 ex_or_search_mode = FALSE; 4848 ex_or_search_mode = FALSE;
4840 4849
4841 if (!lock_chatstate) { 4850 if (!lock_chatstate) {
4842 // Set chat state to composing (1) if the user is currently composing, 4851 // Set chat state to composing (1) if the user is currently composing,
4843 // i.e. not an empty line and not a command line. 4852 // i.e. not an empty line and not a command line.