changeset 1172:334ae9f498f1

Add some more iline functions (Lego12239)
author Mikael Berthe <mikael@lilotux.net>
date Tue, 03 Apr 2007 20:30:28 +0200
parents 03a38b7ad2e0
children 960f34ec22a2
files mcabber/src/commands.c mcabber/src/screen.c mcabber/src/screen.h mcabber/src/utf8.h
diffstat 4 files changed, 195 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/src/commands.c	Wed Mar 14 21:33:17 2007 +0100
+++ b/mcabber/src/commands.c	Tue Apr 03 20:30:28 2007 +0200
@@ -71,6 +71,8 @@
 static void do_help(char *arg);
 static void do_pgp(char *arg);
 static void do_iline(char *arg);
+static void do_screen_refresh(char *arg);
+static void do_chat_disable(char *arg);
 
 // Global variable for the commands list
 static GSList *Commands;
@@ -134,6 +136,8 @@
           COMPL_JID, COMPL_STATUS, &do_status_to);
   cmd_add("version", "Show mcabber version", 0, 0, &do_version);
   cmd_add("iline", "Manipulate input buffer", 0, 0, &do_iline);
+  cmd_add("screen_refresh", "Redraw mcabber screen", 0, 0, &do_screen_refresh);
+  cmd_add("chat_disable", "Disable chat mode", 0, 0, &do_chat_disable);
 
   // Status category
   compl_add_category_word(COMPL_STATUS, "online");
@@ -2727,6 +2731,12 @@
     readline_forward_kill_word();
   } else if (!strcasecmp(arg, "word_bdel")) {
     readline_backward_kill_word();
+  } else if (!strcasecmp(arg, "word_upcase")) {
+    readline_updowncase_word(1);
+  } else if (!strcasecmp(arg, "word_downcase")) {
+    readline_updowncase_word(0);
+  } else if (!strcasecmp(arg, "word_capit")) {
+    readline_capitalize_word();
   } else if (!strcasecmp(arg, "fchar")) {
     readline_forward_char();
   } else if (!strcasecmp(arg, "bchar")) {
@@ -2735,7 +2745,7 @@
     readline_forward_kill_char();
   } else if (!strcasecmp(arg, "char_bdel")) {
     readline_backward_kill_char();
-  } else if (!strcasecmp(arg, "char_swp")) {
+  } else if (!strcasecmp(arg, "char_swap")) {
     readline_transpose_chars();
   } else if (!strcasecmp(arg, "hist_prev")) {
     readline_hist_prev();
@@ -2751,9 +2761,27 @@
     readline_backward_kill_iline();
   } else if (!strcasecmp(arg, "send_multiline")) {
     readline_send_multiline();
+  } else if (!strcasecmp(arg, "iline_accept")) {
+    readline_accept_line();
+  } else if (!strcasecmp(arg, "iline_accept_down_hist")) { // May be too long
+    readline_accept_line_down_hist();
+  } else if (!strcasecmp(arg, "compl_cancel")) {
+    readline_cancel_completion();
+  } else if (!strcasecmp(arg, "compl_do")) {
+    readline_do_completion();
   }
 }
 
+static void do_screen_refresh(char *arg)
+{
+  readline_refresh_screen();
+}
+
+static void do_chat_disable(char *arg)
+{
+  readline_disable_chat_mode();
+}
+
 static void do_connect(char *arg)
 {
   mcabber_connect();
--- a/mcabber/src/screen.c	Wed Mar 14 21:33:17 2007 +0100
+++ b/mcabber/src/screen.c	Tue Apr 03 20:30:28 2007 +0200
@@ -51,6 +51,10 @@
 static unsigned short int Roster_Width;
 
 static inline void check_offset(int);
+static void scr_cancel_current_completion(void);
+static void scr_end_current_completion(void);
+static void scr_insert_text(const char*);
+static void scr_handle_tab(void);
 
 static GHashTable *winbufhash;
 
@@ -2313,26 +2317,25 @@
 // Move  back  to the start of the current or previous word
 void readline_backward_word(void)
 {
-  char *old_ptr_inputLine = ptr_inputline;
-  int spaceallowed = 1;
+  int i = 0;
 
   if (ptr_inputline == inputLine) return;
 
-  for (ptr_inputline = prev_char(ptr_inputline, inputLine) ;
-       ptr_inputline > inputLine ;
-       ptr_inputline = prev_char(ptr_inputline, inputLine)) {
+  if (iswalnum(get_char(ptr_inputline)) &&
+      !iswalnum(get_char(prev_char(ptr_inputline, inputLine))))
+    i--;
+
+  for( ;
+      ptr_inputline > inputLine;
+      ptr_inputline = prev_char(ptr_inputline, inputLine)) {
     if (!iswalnum(get_char(ptr_inputline))) {
-      if (iswblank(get_char(ptr_inputline))) {
-        if (!spaceallowed) break;
-      } else spaceallowed = 0;
-    } else spaceallowed = 0;
+      if (i) {
+        ptr_inputline = next_char(ptr_inputline);
+        break;
+      }
+    } else i++;
   }
 
-  if (ptr_inputline < prev_char(old_ptr_inputLine, inputLine)
-      && iswblank(get_char(ptr_inputline))
-      && iswblank(get_char(next_char(ptr_inputline))))
-    ptr_inputline = next_char(ptr_inputline);
-
   check_offset(-1);
 }
 
@@ -2340,15 +2343,54 @@
 // Move forward to the end of the next word
 void readline_forward_word(void)
 {
-  int spaceallowed = 1;
+  int stopsymbol_allowed = 1;
+
+  while (*ptr_inputline) {
+    if (!iswalnum(get_char(ptr_inputline))) {
+      if (!stopsymbol_allowed) break;
+    } else stopsymbol_allowed = 0;
+    ptr_inputline = next_char(ptr_inputline);
+  }
+
+  check_offset(1);
+}
+
+void readline_updowncase_word(int upcase)
+{
+  int stopsymbol_allowed = 1;
 
   while (*ptr_inputline) {
-    ptr_inputline = next_char(ptr_inputline);
     if (!iswalnum(get_char(ptr_inputline))) {
-      if (iswblank(get_char(ptr_inputline))) {
-        if (!spaceallowed) break;
-      } else spaceallowed = 0;
-    } else spaceallowed = 0;
+      if (!stopsymbol_allowed) break;
+    } else {
+      stopsymbol_allowed = 0;
+      if (upcase)
+        *ptr_inputline = towupper(get_char(ptr_inputline));
+      else
+        *ptr_inputline = towlower(get_char(ptr_inputline));
+    }
+    ptr_inputline = next_char(ptr_inputline);
+  }
+
+  check_offset(1);
+}
+
+void readline_capitalize_word(void)
+{
+  int stopsymbol_allowed = 1;
+  int upcased = 0;
+
+  while (*ptr_inputline) {
+    if (!iswalnum(get_char(ptr_inputline))) {
+      if (!stopsymbol_allowed) break;
+    } else {
+      stopsymbol_allowed = 0;
+      if (!upcased) {
+        *ptr_inputline = towupper(get_char(ptr_inputline));
+        upcased = 1;
+      } else *ptr_inputline = towlower(get_char(ptr_inputline));
+    }
+    ptr_inputline = next_char(ptr_inputline);
   }
 
   check_offset(1);
@@ -2370,6 +2412,93 @@
   check_offset(1);
 }
 
+int readline_accept_line(void)
+{
+  scr_CheckAutoAway(TRUE);
+  if (process_line(inputLine))
+    return 255;
+  // Add line to history
+  scr_cmdhisto_addline(inputLine);
+  // Reset the line
+  ptr_inputline = inputLine;
+  *ptr_inputline = 0;
+  inputline_offset = 0;
+
+  // Reset history line pointer
+  cmdhisto_cur = NULL;
+
+  return 0;
+}
+
+int readline_accept_line_down_hist(void)
+{
+  scr_CheckAutoAway(TRUE);
+  if (process_line(inputLine))
+    return 255;
+  // Add line to history
+  scr_cmdhisto_addline(inputLine);
+  // Reset the line
+  ptr_inputline = inputLine;
+  *ptr_inputline = 0;
+  inputline_offset = 0;
+
+  // Use next history line instead of a blank line
+  const char *l = scr_cmdhisto_next("", 0);
+  if (l) strcpy(inputLine, l);
+  // Reset backup history line
+  cmdhisto_backup[0] = 0;
+
+  return 0;
+}
+
+void readline_cancel_completion(void)
+{
+  scr_cancel_current_completion();
+  scr_end_current_completion();
+  check_offset(-1);
+}
+
+void readline_do_completion(void)
+{
+  int i, n;
+
+  if (scr_get_multimode() != 2) {
+    // Not in verbatim multi-line mode
+    scr_handle_tab();
+  } else {
+    // Verbatim multi-line mode: expand tab
+    char tabstr[9];
+    n = 8 - (ptr_inputline - inputLine) % 8;
+    for (i = 0; i < n; i++)
+      tabstr[i] = ' ';
+    tabstr[i] = '\0';
+    scr_insert_text(tabstr);
+  }
+  check_offset(0);
+}
+
+void readline_refresh_screen(void)
+{
+  scr_CheckAutoAway(TRUE);
+  ParseColors();
+  scr_Resize();
+  redrawwin(stdscr);
+}
+
+void readline_disable_chat_mode(void)
+{
+  scr_CheckAutoAway(TRUE);
+  currentWindow = NULL;
+  chatmode = FALSE;
+  if (current_buddy)
+    buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
+  scr_RosterVisibility(1);
+  scr_UpdateChatStatus(FALSE);
+  top_panel(chatPanel);
+  top_panel(inputPanel);
+  update_panels();
+}
+
 void readline_hist_prev(void)
 {
   const char *l = scr_cmdhisto_prev(inputLine, ptr_inputline-inputLine);
@@ -2915,49 +3044,16 @@
         readline_forward_char();
         break;
     case 7:     // Ctrl-g
-        scr_cancel_current_completion();
-        scr_end_current_completion();
-        check_offset(-1);
+        readline_cancel_completion();
         break;
     case 9:     // Tab
-        if (scr_get_multimode() != 2) {
-          // Not in verbatim multi-line mode
-          scr_handle_tab();
-        } else {
-          // Verbatim multi-line mode: expand tab
-          char tabstr[9];
-          int i, n;
-          n = 8 - (ptr_inputline - inputLine) % 8;
-          for (i = 0; i < n; i++)
-            tabstr[i] = ' ';
-          tabstr[i] = '\0';
-          scr_insert_text(tabstr);
-        }
-        check_offset(0);
+        readline_do_completion();
         break;
     case 13:    // Enter
+        if (readline_accept_line() == 255) return 255;
+        break;
     case 15:    // Ctrl-o ("accept-line-and-down-history")
-        scr_CheckAutoAway(TRUE);
-        if (process_line(inputLine))
-          return 255;
-        // Add line to history
-        scr_cmdhisto_addline(inputLine);
-        // Reset the line
-        ptr_inputline = inputLine;
-        *ptr_inputline = 0;
-        inputline_offset = 0;
-
-        if (key == 13)            // Enter
-        {
-          // Reset history line pointer
-          cmdhisto_cur = NULL;
-        } else {                  // down-history
-          // Use next history line instead of a blank line
-          const char *l = scr_cmdhisto_next("", 0);
-          if (l) strcpy(inputLine, l);
-          // Reset backup history line
-          cmdhisto_backup[0] = 0;
-        }
+        if (readline_accept_line_down_hist() == 255) return 255;
         break;
     case KEY_UP:
         readline_hist_prev();
@@ -3010,25 +3106,13 @@
         readline_forward_word();
         break;
     case 12:    // Ctrl-l
-        scr_CheckAutoAway(TRUE);
-        ParseColors();
-        scr_Resize();
-        redrawwin(stdscr);
+        readline_refresh_screen();
         break;
     case KEY_RESIZE:
         scr_Resize();
         break;
     case 27:    // ESC
-        scr_CheckAutoAway(TRUE);
-        currentWindow = NULL;
-        chatmode = FALSE;
-        if (current_buddy)
-          buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
-        scr_RosterVisibility(1);
-        scr_UpdateChatStatus(FALSE);
-        top_panel(chatPanel);
-        top_panel(inputPanel);
-        update_panels();
+        readline_disable_chat_mode();
         break;
     default:
         display_char = TRUE;
--- a/mcabber/src/screen.h	Wed Mar 14 21:33:17 2007 +0100
+++ b/mcabber/src/screen.h	Tue Apr 03 20:30:28 2007 +0200
@@ -132,8 +132,16 @@
 void readline_backward_kill_word(void);
 void readline_backward_word(void);
 void readline_forward_word(void);
+void readline_updowncase_word(int);
+void readline_capitalize_word(void);
 void readline_backward_char(void);
 void readline_forward_char(void);
+int readline_accept_line(void);
+int readline_accept_line_down_hist(void);
+void readline_cancel_completion(void);
+void readline_do_completion(void);
+void readline_refresh_screen(void);
+void readline_disable_chat_mode(void);
 void readline_hist_prev(void);
 void readline_hist_next(void);
 void readline_backward_kill_char(void);
--- a/mcabber/src/utf8.h	Wed Mar 14 21:33:17 2007 +0100
+++ b/mcabber/src/utf8.h	Tue Apr 03 20:30:28 2007 +0200
@@ -21,6 +21,8 @@
 # define iswblank(c) (c == ' ')
 # define iswalnum(c) isalnum(c)
 # define iswprint(c) isprint(c)
+# define towupper(c) toupper(c)
+# define towlower(c) tolower(c)
 #endif
 
 extern int utf8_mode;