Mercurial > ~mikael > mcabber > hg
comparison mcabber/src/screen.c @ 98:f20831f7d349
[/trunk] Changeset 112 by mikael
* Completion system enabled for commands.
It almost works! ;-)
author | mikael |
---|---|
date | Wed, 20 Apr 2005 20:35:48 +0000 |
parents | 9e6b7897ec37 |
children | 7fffb8a6b229 |
comparison
equal
deleted
inserted
replaced
97:191f4d00d19b | 98:f20831f7d349 |
---|---|
43 int update_roster; | 43 int update_roster; |
44 | 44 |
45 static char inputLine[INPUTLINE_LENGTH+1]; | 45 static char inputLine[INPUTLINE_LENGTH+1]; |
46 static char *ptr_inputline; | 46 static char *ptr_inputline; |
47 static short int inputline_offset; | 47 static short int inputline_offset; |
48 static int completion_started; | |
48 | 49 |
49 | 50 |
50 /* Funciones */ | 51 /* Funciones */ |
51 | 52 |
52 int scr_WindowWidth(WINDOW * win) | 53 int scr_WindowWidth(WINDOW * win) |
580 | 581 |
581 update_panels(); | 582 update_panels(); |
582 doupdate(); | 583 doupdate(); |
583 } | 584 } |
584 | 585 |
586 // which_row() | |
587 // Tells which row our cursor is in, in the command line. | |
588 // -1 -> normal text | |
589 // 0 -> command | |
590 // 1 -> parameter 1 (etc.) | |
591 int which_row(void) | |
592 { | |
593 int row = -1; | |
594 char *p; | |
595 int quote = FALSE; | |
596 | |
597 // Not a command? | |
598 if ((ptr_inputline == inputLine) || (inputLine[0] != '/')) | |
599 return -1; | |
600 | |
601 // This is a command | |
602 row = 0; | |
603 for (p = inputLine ; p < ptr_inputline ; p++) { | |
604 if (quote) { | |
605 if (*p == '"' && *(p-1) != '\\') | |
606 quote = FALSE; | |
607 continue; | |
608 } | |
609 if (*p == '"' && *(p-1) != '\\') { | |
610 quote = TRUE; | |
611 } else if (*p == ' ' && *(p-1) != ' ') | |
612 row++; | |
613 } | |
614 return row; | |
615 } | |
616 | |
617 void scr_insert_text(const char *text) | |
618 { | |
619 char tmpLine[INPUTLINE_LENGTH+1]; | |
620 int len = strlen(text); | |
621 // Check the line isn't too long | |
622 if (strlen(inputLine) + len >= INPUTLINE_LENGTH) { | |
623 scr_LogPrint("Cannot insert text, line too long."); | |
624 return; | |
625 } | |
626 | |
627 strcpy(tmpLine, ptr_inputline); | |
628 strcpy(ptr_inputline, text); ptr_inputline += len; | |
629 strcpy(ptr_inputline, tmpLine); | |
630 } | |
631 | |
632 void scr_handle_tab(void) | |
633 { | |
634 int row; | |
635 row = which_row(); | |
636 | |
637 if (row == -1) return; // No completion if no leading slash | |
638 | |
639 if (row == 0) { // Command completion | |
640 if (!completion_started) { | |
641 GSList *list = compl_get_category_list(COMPL_CMD); | |
642 if (list) { | |
643 const char *cchar; | |
644 char *prefix = g_strndup(&inputLine[1], ptr_inputline-inputLine); | |
645 new_completion(prefix, list); | |
646 cchar = complete(); | |
647 if (cchar) | |
648 scr_insert_text(cchar); | |
649 g_free(prefix); | |
650 completion_started = TRUE; | |
651 } | |
652 } else { | |
653 char *c; | |
654 const char *cchar; | |
655 guint back = cancel_completion(); | |
656 // Remove $back chars | |
657 ptr_inputline -= back; | |
658 c = ptr_inputline; | |
659 for ( ; *c ; c++) | |
660 *c = *(c+back); | |
661 // Now complete again | |
662 cchar = complete(); | |
663 if (cchar) | |
664 scr_insert_text(cchar); | |
665 } | |
666 return; | |
667 } | |
668 scr_LogPrint("I'm unable to complete that yet"); | |
669 } | |
670 | |
671 void scr_cancel_current_completion(void) | |
672 { | |
673 char *c; | |
674 guint back = cancel_completion(); | |
675 // Remove $back chars | |
676 ptr_inputline -= back; | |
677 c = ptr_inputline; | |
678 for ( ; *c ; c++) | |
679 *c = *(c+back); | |
680 } | |
681 | |
682 void scr_end_current_completion(void) | |
683 { | |
684 done_completion(); | |
685 completion_started = FALSE; | |
686 scr_LogPrint("Freeing completion data"); | |
687 } | |
688 | |
585 // check_offset(int direction) | 689 // check_offset(int direction) |
586 // Check inputline_offset value, and make sure the cursor is inside the | 690 // Check inputline_offset value, and make sure the cursor is inside the |
587 // screen. | 691 // screen. |
588 inline void check_offset(int direction) | 692 inline void check_offset(int direction) |
589 { | 693 { |
643 case KEY_RIGHT: | 747 case KEY_RIGHT: |
644 if (*ptr_inputline) | 748 if (*ptr_inputline) |
645 ptr_inputline++; | 749 ptr_inputline++; |
646 check_offset(1); | 750 check_offset(1); |
647 break; | 751 break; |
752 case 7: // Ctrl-g | |
753 scr_cancel_current_completion(); | |
754 scr_end_current_completion(); | |
755 check_offset(-1); | |
756 break; | |
648 case 9: // Tab | 757 case 9: // Tab |
649 scr_LogPrint("I'm unable to complete yet"); | 758 scr_handle_tab(); |
759 check_offset(0); | |
650 break; | 760 break; |
651 case '\n': // Enter | 761 case '\n': // Enter |
652 chatmode = TRUE; | 762 chatmode = TRUE; |
653 if (inputLine[0] == 0) { | 763 if (inputLine[0] == 0) { |
654 scr_ShowBuddyWindow(); | 764 scr_ShowBuddyWindow(); |
709 break; | 819 break; |
710 default: | 820 default: |
711 scr_LogPrint("Unkown key=%d", key); | 821 scr_LogPrint("Unkown key=%d", key); |
712 } | 822 } |
713 } | 823 } |
824 if (completion_started && key != 9) | |
825 scr_end_current_completion(); | |
714 mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset); | 826 mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset); |
715 wclrtoeol(inputWnd); | 827 wclrtoeol(inputWnd); |
716 if (*ptr_inputline) { | 828 if (*ptr_inputline) { |
717 wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset); | 829 wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset); |
718 } | 830 } |