Mercurial > hg
annotate mcabber/src/screen.c @ 260:33e1a05864a6
Add "verbatim multi-line" mode, with commands disabled
"/msay verbatim" enters multi-line mode with all commands disabled
but "/msay", so that lines beginning with a slash '/' can be sent.
Note: <Tab> still do commands completion.
author | mikael@frmp8452 |
---|---|
date | Fri, 01 Jul 2005 23:47:03 +0100 |
parents | cb7f944741e4 |
children | 259ed1de5bdc |
rev | line source |
---|---|
24 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 #include <ncurses.h> | |
5 #include <panel.h> | |
6 #include <time.h> | |
7 #include <ctype.h> | |
8 #include <locale.h> | |
232 | 9 #include <langinfo.h> |
24 | 10 |
11 #include "screen.h" | |
81 | 12 #include "hbuf.h" |
47 | 13 #include "commands.h" |
95 | 14 #include "compl.h" |
81 | 15 #include "roster.h" |
180 | 16 #include "histolog.h" |
24 | 17 #include "parsecfg.h" |
81 | 18 #include "utils.h" |
24 | 19 #include "list.h" |
20 | |
21 #define window_entry(n) list_entry(n, window_entry_t, list) | |
22 | |
151 | 23 inline void check_offset(int); |
24 | |
24 | 25 LIST_HEAD(window_list); |
26 | |
27 typedef struct _window_entry_t { | |
28 WINDOW *win; | |
108 | 29 PANEL *panel; |
30 char *name; | |
31 GList *hbuf; | |
181 | 32 GList *top; // If top is NULL, we'll display the last lines |
33 char cleared; // For ex, user has issued a /clear command... | |
24 | 34 struct list_head list; |
35 } window_entry_t; | |
36 | |
37 | |
38 static WINDOW *rosterWnd, *chatWnd, *inputWnd; | |
39 static WINDOW *logWnd, *logWnd_border; | |
40 static PANEL *rosterPanel, *chatPanel, *inputPanel; | |
41 static PANEL *logPanel, *logPanel_border; | |
42 static int maxY, maxX; | |
43 static window_entry_t *currentWindow; | |
44 | |
45 static int chatmode; | |
238 | 46 static int multimode; |
47 static char *multiline; | |
30 | 48 int update_roster; |
232 | 49 int utf8_mode = 0; |
24 | 50 |
174 | 51 static char inputLine[INPUTLINE_LENGTH+1]; |
52 static char *ptr_inputline; | |
53 static short int inputline_offset; | |
54 static int completion_started; | |
173 | 55 static GList *cmdhisto; |
56 static GList *cmdhisto_cur; | |
174 | 57 static char cmdhisto_backup[INPUTLINE_LENGTH+1]; |
24 | 58 |
59 | |
99 | 60 /* Functions */ |
24 | 61 |
74 | 62 int scr_WindowWidth(WINDOW * win) |
24 | 63 { |
64 int x, y; | |
65 getmaxyx(win, y, x); | |
66 return x; | |
67 } | |
68 | |
74 | 69 void scr_clear_box(WINDOW *win, int y, int x, int height, int width, int Color) |
70 { | |
71 int i, j; | |
72 | |
73 wattrset(win, COLOR_PAIR(Color)); | |
74 for (i = 0; i < height; i++) { | |
75 wmove(win, y + i, x); | |
76 for (j = 0; j < width; j++) | |
77 wprintw(win, " "); | |
78 } | |
79 } | |
80 | |
24 | 81 void scr_draw_box(WINDOW * win, int y, int x, int height, int width, |
82 int Color, chtype box, chtype border) | |
83 { | |
84 int i, j; | |
85 | |
86 wattrset(win, COLOR_PAIR(Color)); | |
87 for (i = 0; i < height; i++) { | |
88 wmove(win, y + i, x); | |
89 for (j = 0; j < width; j++) | |
90 if (!i && !j) | |
91 waddch(win, border | ACS_ULCORNER); | |
92 else if (i == height - 1 && !j) | |
93 waddch(win, border | ACS_LLCORNER); | |
94 else if (!i && j == width - 1) | |
95 waddch(win, box | ACS_URCORNER); | |
96 else if (i == height - 1 && j == width - 1) | |
97 waddch(win, box | ACS_LRCORNER); | |
98 else if (!i) | |
99 waddch(win, border | ACS_HLINE); | |
100 else if (i == height - 1) | |
101 waddch(win, box | ACS_HLINE); | |
102 else if (!j) | |
103 waddch(win, border | ACS_VLINE); | |
104 else if (j == width - 1) | |
105 waddch(win, box | ACS_VLINE); | |
106 else | |
107 waddch(win, box | ' '); | |
108 } | |
109 } | |
110 | |
111 int FindColor(char *name) | |
112 { | |
113 if (!strcmp(name, "default")) | |
114 return -1; | |
115 if (!strcmp(name, "black")) | |
116 return COLOR_BLACK; | |
117 if (!strcmp(name, "red")) | |
118 return COLOR_RED; | |
119 if (!strcmp(name, "green")) | |
120 return COLOR_GREEN; | |
121 if (!strcmp(name, "yellow")) | |
122 return COLOR_YELLOW; | |
123 if (!strcmp(name, "blue")) | |
124 return COLOR_BLUE; | |
125 if (!strcmp(name, "magenta")) | |
126 return COLOR_MAGENTA; | |
127 if (!strcmp(name, "cyan")) | |
128 return COLOR_CYAN; | |
129 if (!strcmp(name, "white")) | |
130 return COLOR_WHITE; | |
131 | |
132 return -1; | |
133 } | |
134 | |
135 void ParseColors(void) | |
136 { | |
137 char *colors[11] = { | |
138 "", "", | |
139 "borderlines", | |
140 "jidonline", | |
139 | 141 "newmsg", |
24 | 142 "jidofflineselected", |
143 "jidoffline", | |
144 "text", | |
145 NULL | |
146 }; | |
147 | |
148 char *tmp = malloc(1024); | |
149 char *color1; | |
150 char *background = cfg_read("color_background"); | |
151 char *backselected = cfg_read("color_backselected"); | |
152 int i = 0; | |
153 | |
154 while (colors[i]) { | |
155 sprintf(tmp, "color_%s", colors[i]); | |
156 color1 = cfg_read(tmp); | |
157 | |
158 switch (i + 1) { | |
159 case 1: | |
160 init_pair(1, COLOR_BLACK, COLOR_WHITE); | |
161 break; | |
162 case 2: | |
163 init_pair(2, COLOR_WHITE, COLOR_BLACK); | |
164 break; | |
165 case 3: | |
166 init_pair(3, FindColor(color1), FindColor(background)); | |
167 break; | |
168 case 4: | |
169 init_pair(4, FindColor(color1), FindColor(backselected)); | |
170 break; | |
171 case 5: | |
172 init_pair(5, FindColor(color1), FindColor(background)); | |
173 break; | |
174 case 6: | |
175 init_pair(6, FindColor(color1), FindColor(backselected)); | |
176 break; | |
177 case 7: | |
178 init_pair(7, FindColor(color1), FindColor(background)); | |
179 break; | |
180 case 8: | |
181 init_pair(8, FindColor(color1), FindColor(background)); | |
182 break; | |
183 } | |
184 i++; | |
185 } | |
186 } | |
187 | |
188 | |
151 | 189 window_entry_t *scr_CreateBuddyPanel(const char *title, int dont_show) |
24 | 190 { |
151 | 191 int x; |
192 int y; | |
193 int lines; | |
194 int cols; | |
153 | 195 window_entry_t *tmp; |
196 | |
197 do { | |
198 tmp = calloc(1, sizeof(window_entry_t)); | |
199 } while (!tmp); | |
24 | 200 |
151 | 201 // Dimensions |
202 x = ROSTER_WIDTH; | |
203 y = 0; | |
204 lines = CHAT_WIN_HEIGHT; | |
205 cols = maxX - ROSTER_WIDTH; | |
206 | |
24 | 207 tmp->win = newwin(lines, cols, y, x); |
154 | 208 while (!tmp->win) { |
209 usleep(250); | |
210 tmp->win = newwin(lines, cols, y, x); | |
211 } | |
168 | 212 wbkgd(tmp->win, COLOR_PAIR(COLOR_GENERAL)); |
24 | 213 tmp->panel = new_panel(tmp->win); |
153 | 214 tmp->name = (char *) calloc(1, 96); |
215 strncpy(tmp->name, title, 96); | |
24 | 216 |
143 | 217 if (!dont_show) { |
24 | 218 currentWindow = tmp; |
219 } else { | |
220 if (currentWindow) | |
221 top_panel(currentWindow->panel); | |
222 else | |
223 top_panel(chatPanel); | |
224 } | |
143 | 225 update_panels(); |
24 | 226 |
181 | 227 // Load buddy history from file (if enabled) |
185 | 228 hlog_read_history(title, &tmp->hbuf, maxX - ROSTER_WIDTH - PREFIX_WIDTH); |
181 | 229 |
24 | 230 list_add_tail(&tmp->list, &window_list); |
231 | |
232 return tmp; | |
233 } | |
234 | |
50 | 235 window_entry_t *scr_SearchWindow(const char *winId) |
24 | 236 { |
237 struct list_head *pos, *n; | |
238 window_entry_t *search_entry = NULL; | |
239 | |
240 list_for_each_safe(pos, n, &window_list) { | |
241 search_entry = window_entry(pos); | |
242 if (search_entry->name) { | |
243 if (!strcasecmp(search_entry->name, winId)) { | |
244 return search_entry; | |
245 } | |
246 } | |
247 } | |
248 return NULL; | |
249 } | |
250 | |
143 | 251 // scr_UpdateWindow() |
252 // (Re-)Display the given chat window. | |
74 | 253 void scr_UpdateWindow(window_entry_t *win_entry) |
254 { | |
255 int n; | |
256 int width; | |
184 | 257 hbb_line **lines, *line; |
74 | 258 GList *hbuf_head; |
184 | 259 char date[32]; |
74 | 260 |
108 | 261 width = scr_WindowWidth(win_entry->win); |
262 | |
263 // Should the window be empty? | |
264 if (win_entry->cleared) { | |
168 | 265 werase(win_entry->win); |
108 | 266 return; |
267 } | |
268 | |
105 | 269 // win_entry->top is the top message of the screen. If it set to NULL, we |
270 // are displaying the last messages. | |
271 | |
74 | 272 // We will show the last CHAT_WIN_HEIGHT lines. |
273 // Let's find out where it begins. | |
105 | 274 if (!win_entry->top || |
275 (g_list_position(g_list_first(win_entry->hbuf), win_entry->top) == -1)) { | |
276 // Move up CHAT_WIN_HEIGHT lines | |
277 win_entry->hbuf = g_list_last(win_entry->hbuf); | |
278 hbuf_head = win_entry->hbuf; | |
279 win_entry->top = NULL; // (Just to make sure) | |
280 n = 0; | |
281 while (hbuf_head && (n < CHAT_WIN_HEIGHT-1) && g_list_previous(hbuf_head)) { | |
282 hbuf_head = g_list_previous(hbuf_head); | |
283 n++; | |
284 } | |
285 } else | |
286 hbuf_head = win_entry->top; | |
74 | 287 |
288 // Get the last CHAT_WIN_HEIGHT lines. | |
289 lines = hbuf_get_lines(hbuf_head, CHAT_WIN_HEIGHT); | |
290 | |
291 // Display these lines | |
292 for (n = 0; n < CHAT_WIN_HEIGHT; n++) { | |
168 | 293 wmove(win_entry->win, n, 0); |
184 | 294 line = *(lines+n); |
185 | 295 // NOTE: update PREFIX_WIDTH if you change the date format!! |
296 // You need to set it to the whole prefix length + 1 | |
184 | 297 if (line) { |
298 if (line->timestamp) { | |
185 | 299 strftime(date, 35, "%m-%d %H:%M", localtime(&line->timestamp)); |
184 | 300 } else |
185 | 301 strcpy(date, " "); |
197 | 302 if (line->flags & HBB_PREFIX_INFO) { |
303 char dir = '*'; | |
304 if (line->flags & HBB_PREFIX_IN) | |
305 dir = '<'; | |
306 else if (line->flags & HBB_PREFIX_OUT) | |
307 dir = '>'; | |
308 wprintw(win_entry->win, "%.11s *%c* ", date, dir); | |
309 } else if (line->flags & HBB_PREFIX_IN) | |
185 | 310 wprintw(win_entry->win, "%.11s <== ", date); |
184 | 311 else if (line->flags & HBB_PREFIX_OUT) |
185 | 312 wprintw(win_entry->win, "%.11s --> ", date); |
75 | 313 else { |
185 | 314 wprintw(win_entry->win, "%.11s ", date); |
75 | 315 } |
184 | 316 wprintw(win_entry->win, "%s", line->text); // line |
168 | 317 wclrtoeol(win_entry->win); |
184 | 318 g_free(line->text); |
168 | 319 } else { |
320 wclrtobot(win_entry->win); | |
321 break; | |
75 | 322 } |
74 | 323 } |
324 g_free(lines); | |
325 } | |
326 | |
143 | 327 // scr_ShowWindow() |
328 // Display the chat window with the given identifier. | |
50 | 329 void scr_ShowWindow(const char *winId) |
24 | 330 { |
74 | 331 window_entry_t *win_entry = scr_SearchWindow(winId); |
332 | |
181 | 333 if (!win_entry) |
180 | 334 win_entry = scr_CreateBuddyPanel(winId, FALSE); |
74 | 335 |
180 | 336 top_panel(win_entry->panel); |
337 currentWindow = win_entry; | |
338 chatmode = TRUE; | |
339 roster_msg_setflag(winId, FALSE); | |
340 roster_setflags(winId, ROSTER_FLAG_LOCK, TRUE); | |
341 update_roster = TRUE; | |
74 | 342 |
180 | 343 // Refresh the window |
344 scr_UpdateWindow(win_entry); | |
345 | |
346 // Finished :) | |
347 update_panels(); | |
142 | 348 |
349 top_panel(inputPanel); | |
24 | 350 } |
351 | |
143 | 352 // scr_ShowBuddyWindow() |
353 // Display the chat window buffer for the current buddy. | |
24 | 354 void scr_ShowBuddyWindow(void) |
355 { | |
105 | 356 const gchar *jid; |
140 | 357 |
105 | 358 if (!current_buddy) |
140 | 359 jid = NULL; |
360 else | |
361 jid = CURRENT_JID; | |
362 | |
363 if (!jid) { | |
364 top_panel(chatPanel); | |
143 | 365 top_panel(inputPanel); |
140 | 366 currentWindow = NULL; |
105 | 367 return; |
140 | 368 } |
369 | |
105 | 370 scr_ShowWindow(jid); |
24 | 371 } |
372 | |
373 | |
143 | 374 // scr_WriteInWindow() |
375 // Write some text in the winId window (this usually is a jid). | |
376 // Lines are splitted when they are too long to fit in the chat window. | |
377 // If this window doesn't exist, it is created. | |
184 | 378 void scr_WriteInWindow(const char *winId, const char *text, time_t timestamp, |
379 unsigned int prefix_flags, int force_show) | |
24 | 380 { |
74 | 381 window_entry_t *win_entry; |
24 | 382 int dont_show = FALSE; |
383 | |
74 | 384 // Look for the window entry. |
385 win_entry = scr_SearchWindow(winId); | |
386 | |
387 // Do we have to really show the window? | |
24 | 388 if (!chatmode) |
389 dont_show = TRUE; | |
74 | 390 else if ((!force_show) && ((!currentWindow || (currentWindow != win_entry)))) |
24 | 391 dont_show = TRUE; |
392 | |
74 | 393 // If the window entry doesn't exist yet, let's create it. |
394 if (win_entry == NULL) { | |
151 | 395 win_entry = scr_CreateBuddyPanel(winId, dont_show); |
24 | 396 } |
397 | |
220 | 398 // The message must be displayed -> update top pointer |
399 if (win_entry->cleared) | |
400 win_entry->top = g_list_last(win_entry->hbuf); | |
401 | |
184 | 402 hbuf_add_line(&win_entry->hbuf, text, timestamp, prefix_flags, |
185 | 403 maxX - ROSTER_WIDTH - PREFIX_WIDTH); |
74 | 404 |
108 | 405 if (win_entry->cleared) { |
220 | 406 win_entry->cleared = FALSE; |
407 if (g_list_next(win_entry->top)) | |
408 win_entry->top = g_list_next(win_entry->top); | |
409 } | |
410 | |
411 // Make sure the last line appears in the window; update top if necessary | |
412 if (win_entry->top) { | |
413 int dist; | |
414 GList *first = g_list_first(win_entry->hbuf); | |
415 dist = g_list_position(first, g_list_last(win_entry->hbuf)) - | |
416 g_list_position(first, win_entry->top); | |
417 if (dist >= CHAT_WIN_HEIGHT) | |
418 win_entry->top = NULL; | |
108 | 419 } |
420 | |
24 | 421 if (!dont_show) { |
74 | 422 // Show and refresh the window |
423 top_panel(win_entry->panel); | |
424 scr_UpdateWindow(win_entry); | |
142 | 425 top_panel(inputPanel); |
24 | 426 update_panels(); |
427 doupdate(); | |
428 } else { | |
148 | 429 roster_msg_setflag(winId, TRUE); |
30 | 430 update_roster = TRUE; |
24 | 431 } |
432 } | |
433 | |
434 void scr_InitCurses(void) | |
435 { | |
436 initscr(); | |
437 noecho(); | |
438 raw(); | |
35 | 439 halfdelay(5); |
24 | 440 start_color(); |
441 use_default_colors(); | |
442 | |
443 ParseColors(); | |
444 | |
445 getmaxyx(stdscr, maxY, maxX); | |
167 | 446 if (maxY < LOG_WIN_HEIGHT+2) |
447 maxY = LOG_WIN_HEIGHT+2; | |
24 | 448 inputLine[0] = 0; |
449 ptr_inputline = inputLine; | |
450 | |
35 | 451 setlocale(LC_CTYPE, ""); |
232 | 452 utf8_mode = (strcmp(nl_langinfo(CODESET), "UTF-8") == 0); |
24 | 453 |
454 return; | |
455 } | |
456 | |
81 | 457 void scr_TerminateCurses(void) |
458 { | |
459 clear(); | |
460 refresh(); | |
461 endwin(); | |
462 return; | |
463 } | |
464 | |
151 | 465 // scr_DrawMainWindow() |
157 | 466 // Set fullinit to TRUE to also create panels. Set it to FALSE for a resize. |
151 | 467 // |
468 // I think it could be improved a _lot_ but I'm really not an ncurses | |
469 // expert... :-\ Mikael. | |
470 // | |
471 void scr_DrawMainWindow(unsigned int fullinit) | |
24 | 472 { |
157 | 473 if (fullinit) { |
474 /* Create windows */ | |
475 rosterWnd = newwin(CHAT_WIN_HEIGHT, ROSTER_WIDTH, 0, 0); | |
476 chatWnd = newwin(CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, 0, ROSTER_WIDTH); | |
477 logWnd_border = newwin(LOG_WIN_HEIGHT, maxX, CHAT_WIN_HEIGHT, 0); | |
478 logWnd = newwin(LOG_WIN_HEIGHT-2, maxX-2, CHAT_WIN_HEIGHT+1, 1); | |
479 inputWnd = newwin(1, maxX, maxY-1, 0); | |
168 | 480 wbkgd(rosterWnd, COLOR_PAIR(COLOR_GENERAL)); |
481 wbkgd(chatWnd, COLOR_PAIR(COLOR_GENERAL)); | |
482 wbkgd(logWnd_border, COLOR_PAIR(COLOR_GENERAL)); | |
483 wbkgd(logWnd, COLOR_PAIR(COLOR_GENERAL)); | |
157 | 484 } else { |
485 /* Resize windows */ | |
486 wresize(rosterWnd, CHAT_WIN_HEIGHT, ROSTER_WIDTH); | |
487 wresize(chatWnd, CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH); | |
488 | |
489 wresize(logWnd_border, LOG_WIN_HEIGHT, maxX); | |
490 wresize(logWnd, LOG_WIN_HEIGHT-2, maxX-2); | |
491 mvwin(logWnd_border, CHAT_WIN_HEIGHT, 0); | |
492 mvwin(logWnd, CHAT_WIN_HEIGHT+1, 1); | |
493 | |
494 wresize(inputWnd, 1, maxX); | |
495 mvwin(inputWnd, maxY-1, 0); | |
168 | 496 |
497 werase(chatWnd); | |
157 | 498 } |
151 | 499 |
500 /* Draw/init windows */ | |
501 | |
74 | 502 mvwprintw(chatWnd, 0, 0, "This is the status window"); |
24 | 503 |
151 | 504 // - Draw/clear the log window |
24 | 505 scr_draw_box(logWnd_border, 0, 0, LOG_WIN_HEIGHT, maxX, COLOR_GENERAL, 0, 0); |
157 | 506 // Auto-scrolling in log window |
74 | 507 scrollok(logWnd, TRUE); |
24 | 508 |
509 | |
151 | 510 if (fullinit) { |
157 | 511 // Enable keypad (+ special keys) |
512 keypad(inputWnd, TRUE); | |
513 | |
151 | 514 // Create panels |
515 rosterPanel = new_panel(rosterWnd); | |
516 chatPanel = new_panel(chatWnd); | |
517 logPanel_border = new_panel(logWnd_border); | |
518 logPanel = new_panel(logWnd); | |
519 inputPanel = new_panel(inputWnd); | |
232 | 520 |
521 if (utf8_mode) | |
522 scr_LogPrint("WARNING: UTF-8 not yet supported!"); | |
157 | 523 } else { |
524 // Update panels | |
525 replace_panel(rosterPanel, rosterWnd); | |
526 replace_panel(chatPanel, chatWnd); | |
527 replace_panel(logPanel, logWnd); | |
528 replace_panel(logPanel_border, logWnd_border); | |
529 replace_panel(inputPanel, inputWnd); | |
151 | 530 } |
531 | |
532 // We'll need to redraw the roster | |
149 | 533 update_roster = TRUE; |
24 | 534 return; |
535 } | |
536 | |
151 | 537 // scr_Resize() |
538 // Function called when the window is resized. | |
157 | 539 // - Resize windows |
151 | 540 // - Rewrap lines in each buddy buffer |
541 void scr_Resize() | |
542 { | |
543 struct list_head *pos, *n; | |
544 window_entry_t *search_entry; | |
545 int x, y, lines, cols; | |
546 | |
547 // First, update the global variables | |
548 getmaxyx(stdscr, maxY, maxX); | |
167 | 549 if (maxY < LOG_WIN_HEIGHT+2) |
550 maxY = LOG_WIN_HEIGHT+2; | |
151 | 551 // Make sure the cursor stays inside the window |
552 check_offset(0); | |
553 | |
157 | 554 // Resize windows and update panels |
151 | 555 scr_DrawMainWindow(FALSE); |
556 | |
557 // Resize all buddy windows | |
558 x = ROSTER_WIDTH; | |
559 y = 0; | |
560 lines = CHAT_WIN_HEIGHT; | |
561 cols = maxX - ROSTER_WIDTH; | |
562 | |
563 list_for_each_safe(pos, n, &window_list) { | |
564 search_entry = window_entry(pos); | |
565 if (search_entry->win) { | |
189 | 566 GList *rescue_top; |
157 | 567 // Resize buddy window (no need to move it) |
568 wresize(search_entry->win, lines, cols); | |
168 | 569 werase(search_entry->win); |
151 | 570 // If a panel exists, replace the old window with the new |
571 if (search_entry->panel) { | |
572 replace_panel(search_entry->panel, search_entry->win); | |
573 } | |
574 // Redo line wrapping | |
189 | 575 rescue_top = hbuf_previous_persistent(search_entry->top); |
151 | 576 hbuf_rebuild(&search_entry->hbuf, |
185 | 577 maxX - ROSTER_WIDTH - PREFIX_WIDTH); |
189 | 578 if (g_list_position(g_list_first(search_entry->hbuf), search_entry->top) == -1) |
579 search_entry->top = rescue_top; | |
151 | 580 } |
581 } | |
582 | |
583 // Refresh current buddy window | |
157 | 584 if (chatmode) |
151 | 585 scr_ShowBuddyWindow(); |
586 } | |
587 | |
143 | 588 // scr_DrawRoster() |
589 // Actually, display the buddylist on the screen. | |
81 | 590 void scr_DrawRoster(void) |
24 | 591 { |
81 | 592 static guint offset = 0; |
593 char name[ROSTER_WIDTH]; | |
594 int maxx, maxy; | |
595 GList *buddy; | |
596 int i, n; | |
597 int rOffset; | |
164 | 598 enum imstatus currentstatus = jb_getstatus(); |
81 | 599 |
123 | 600 // We can reset update_roster |
601 update_roster = FALSE; | |
602 | |
81 | 603 getmaxyx(rosterWnd, maxy, maxx); |
604 maxx --; // last char is for vertical border | |
605 name[ROSTER_WIDTH-7] = 0; | |
606 | |
607 // cleanup of roster window | |
168 | 608 werase(rosterWnd); |
609 // Redraw the vertical line (not very good...) | |
81 | 610 wattrset(rosterWnd, COLOR_PAIR(COLOR_GENERAL)); |
168 | 611 for (i=0 ; i < CHAT_WIN_HEIGHT ; i++) |
612 mvwaddch(rosterWnd, i, ROSTER_WIDTH-1, ACS_VLINE); | |
81 | 613 |
614 // Leave now if buddylist is empty | |
615 if (!buddylist) { | |
616 offset = 0; | |
123 | 617 update_panels(); |
618 doupdate(); | |
81 | 619 return; |
620 } | |
621 | |
84 | 622 // Update offset if necessary |
623 i = g_list_position(buddylist, current_buddy); | |
624 if (i == -1) { // This is bad | |
625 scr_LogPrint("Doh! Can't find current selected buddy!!"); | |
626 return; | |
627 } else if (i < offset) { | |
628 offset = i; | |
629 } else if (i+1 > offset + maxy) { | |
630 offset = i + 1 - maxy; | |
631 } | |
81 | 632 |
633 buddy = buddylist; | |
634 rOffset = offset; | |
635 | |
84 | 636 for (i=0; i<maxy && buddy; buddy = g_list_next(buddy)) { |
81 | 637 |
638 char status = '?'; | |
639 char pending = ' '; | |
640 enum imstatus budstate; | |
149 | 641 unsigned short ismsg = buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG; |
642 unsigned short isgrp = buddy_gettype(BUDDATA(buddy)) & ROSTER_TYPE_GROUP; | |
643 unsigned short ishid = buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_HIDE; | |
81 | 644 |
645 if (rOffset > 0) { | |
646 rOffset--; | |
647 continue; | |
648 } | |
649 | |
149 | 650 // Display message notice if there is a message flag, but not |
651 // for unfolded groups. | |
652 if (ismsg && (!isgrp || ishid)) { | |
81 | 653 pending = '#'; |
654 } | |
655 | |
656 budstate = buddy_getstatus(BUDDATA(buddy)); | |
164 | 657 if (budstate >= 0 && budstate < imstatus_size && currentstatus != offline) |
81 | 658 status = imstatus2char[budstate]; |
659 if (buddy == current_buddy) { | |
660 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DESSEL)); | |
661 // The 3 following lines aim to color the whole line | |
662 wmove(rosterWnd, i, 0); | |
663 for (n = 0; n < maxx; n++) | |
664 waddch(rosterWnd, ' '); | |
665 } else { | |
149 | 666 if (pending == '#') |
139 | 667 wattrset(rosterWnd, COLOR_PAIR(COLOR_NMSG)); |
668 else | |
669 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DES)); | |
81 | 670 } |
671 | |
672 strncpy(name, buddy_getname(BUDDATA(buddy)), ROSTER_WIDTH-7); | |
149 | 673 if (isgrp) { |
133 | 674 char *sep; |
149 | 675 if (ishid) |
133 | 676 sep = "+++"; |
677 else | |
678 sep = "---"; | |
679 mvwprintw(rosterWnd, i, 0, " %c%s %s", pending, sep, name); | |
680 } | |
126 | 681 else |
682 mvwprintw(rosterWnd, i, 0, " %c[%c] %s", pending, status, name); | |
84 | 683 |
684 i++; | |
81 | 685 } |
686 | |
142 | 687 top_panel(inputPanel); |
81 | 688 update_panels(); |
689 doupdate(); | |
24 | 690 } |
691 | |
184 | 692 void scr_WriteMessage(const char *jid, const char *text, time_t timestamp, |
693 guint prefix_flags) | |
24 | 694 { |
184 | 695 if (!timestamp) timestamp = time(NULL); |
696 | |
697 scr_WriteInWindow(jid, text, timestamp, prefix_flags, FALSE); | |
47 | 698 } |
699 | |
190 | 700 // If prefix is NULL, HBB_PREFIX_IN is supposed. |
184 | 701 void scr_WriteIncomingMessage(const char *jidfrom, const char *text, |
190 | 702 time_t timestamp, guint prefix) |
47 | 703 { |
190 | 704 if (!prefix) prefix = HBB_PREFIX_IN; |
75 | 705 // FIXME expand tabs / filter out special chars... |
190 | 706 scr_WriteMessage(jidfrom, text, timestamp, prefix); |
24 | 707 update_panels(); |
708 doupdate(); | |
709 } | |
710 | |
50 | 711 void scr_WriteOutgoingMessage(const char *jidto, const char *text) |
47 | 712 { |
184 | 713 scr_WriteMessage(jidto, text, 0, HBB_PREFIX_OUT); |
47 | 714 scr_ShowWindow(jidto); |
715 } | |
716 | |
24 | 717 int scr_Getch(void) |
718 { | |
719 int ch; | |
720 ch = wgetch(inputWnd); | |
721 return ch; | |
722 } | |
723 | |
724 WINDOW *scr_GetRosterWindow(void) | |
725 { | |
726 return rosterWnd; | |
727 } | |
728 | |
729 WINDOW *scr_GetStatusWindow(void) | |
730 { | |
731 return chatWnd; | |
732 } | |
733 | |
734 WINDOW *scr_GetInputWindow(void) | |
735 { | |
736 return inputWnd; | |
737 } | |
738 | |
143 | 739 // scr_RosterTop() |
740 // Go to the first buddy in the buddylist | |
105 | 741 void scr_RosterTop(void) |
104 | 742 { |
143 | 743 enum imstatus prev_st = imstatus_size; // undef |
119 | 744 |
745 if (current_buddy) { | |
746 prev_st = buddy_getstatus(BUDDATA(current_buddy)); | |
747 if (chatmode) | |
748 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); | |
749 } | |
104 | 750 current_buddy = buddylist; |
119 | 751 if (chatmode && current_buddy) |
752 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); | |
753 | |
754 // We should rebuild the buddylist but not everytime | |
143 | 755 // Here we check if we were locking a buddy who is actually offline, |
756 // and hide_offline_buddies is TRUE. In which case we need to rebuild. | |
120 | 757 if (current_buddy && prev_st == offline && |
758 buddylist_get_hide_offline_buddies()) | |
119 | 759 buddylist_build(); |
104 | 760 if (chatmode) |
761 scr_ShowBuddyWindow(); | |
143 | 762 update_roster = TRUE; |
104 | 763 } |
764 | |
143 | 765 // scr_RosterBottom() |
766 // Go to the last buddy in the buddylist | |
105 | 767 void scr_RosterBottom(void) |
104 | 768 { |
143 | 769 enum imstatus prev_st = imstatus_size; // undef |
119 | 770 |
771 if (current_buddy) { | |
772 prev_st = buddy_getstatus(BUDDATA(current_buddy)); | |
773 if (chatmode) | |
774 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); | |
775 } | |
104 | 776 current_buddy = g_list_last(buddylist); |
143 | 777 // Lock the buddy in the buddylist if we're in chat mode |
119 | 778 if (chatmode && current_buddy) |
779 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); | |
780 | |
781 // We should rebuild the buddylist but not everytime | |
143 | 782 // Here we check if we were locking a buddy who is actually offline, |
783 // and hide_offline_buddies is TRUE. In which case we need to rebuild. | |
120 | 784 if (current_buddy && prev_st == offline && |
785 buddylist_get_hide_offline_buddies()) | |
119 | 786 buddylist_build(); |
143 | 787 |
104 | 788 if (chatmode) |
789 scr_ShowBuddyWindow(); | |
143 | 790 update_roster = TRUE; |
104 | 791 } |
792 | |
143 | 793 // scr_RosterUp() |
794 // Go to the previous buddy in the buddylist | |
105 | 795 void scr_RosterUp(void) |
81 | 796 { |
143 | 797 enum imstatus prev_st = imstatus_size; // undef |
119 | 798 |
81 | 799 if (current_buddy) { |
800 if (g_list_previous(current_buddy)) { | |
120 | 801 prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
119 | 802 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
81 | 803 current_buddy = g_list_previous(current_buddy); |
143 | 804 // Lock the buddy in the buddylist if we're in chat mode |
119 | 805 if (chatmode) |
806 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); | |
807 // We should rebuild the buddylist but not everytime | |
143 | 808 // Here we check if we were locking a buddy who is actually offline, |
809 // and hide_offline_buddies is TRUE. In which case we need to rebuild. | |
120 | 810 if (prev_st == offline && buddylist_get_hide_offline_buddies()) |
119 | 811 buddylist_build(); |
143 | 812 update_roster = TRUE; |
81 | 813 } |
814 } | |
104 | 815 |
816 if (chatmode) | |
817 scr_ShowBuddyWindow(); | |
81 | 818 } |
819 | |
143 | 820 // scr_RosterDown() |
821 // Go to the next buddy in the buddylist | |
105 | 822 void scr_RosterDown(void) |
81 | 823 { |
143 | 824 enum imstatus prev_st = imstatus_size; // undef |
119 | 825 |
81 | 826 if (current_buddy) { |
827 if (g_list_next(current_buddy)) { | |
120 | 828 prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
119 | 829 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
81 | 830 current_buddy = g_list_next(current_buddy); |
119 | 831 if (chatmode) |
832 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); | |
833 // We should rebuild the buddylist but not everytime | |
143 | 834 // Here we check if we were locking a buddy who is actually offline, |
835 // and hide_offline_buddies is TRUE. In which case we need to rebuild. | |
120 | 836 if (prev_st == offline && buddylist_get_hide_offline_buddies()) |
119 | 837 buddylist_build(); |
143 | 838 update_roster = TRUE; |
81 | 839 } |
840 } | |
104 | 841 |
842 if (chatmode) | |
843 scr_ShowBuddyWindow(); | |
81 | 844 } |
845 | |
236 | 846 // scr_RosterUnreadMessage(next) |
847 // Go to a new message. If next is not null, try to go to the next new | |
848 // message. If it is not possible or if next is NULL, go to the first new | |
849 // message from unread_list. | |
850 void scr_RosterUnreadMessage(int next) | |
851 { | |
852 enum imstatus prev_st = imstatus_size; // undef | |
853 | |
854 if (current_buddy) { | |
855 gpointer unread_ptr; | |
856 gpointer refbuddata; | |
857 gpointer ngroup; | |
858 GList *nbuddy; | |
859 | |
860 if (next) refbuddata = BUDDATA(current_buddy); | |
861 else refbuddata = NULL; | |
862 | |
863 unread_ptr = unread_msg(refbuddata); | |
864 if (!unread_ptr) return; | |
865 | |
866 // If buddy is in a folded group, we need to expand it | |
867 ngroup = buddy_getgroup(unread_ptr); | |
868 if (buddy_getflags(ngroup) & ROSTER_FLAG_HIDE) { | |
869 buddy_setflags(ngroup, ROSTER_FLAG_HIDE, FALSE); | |
870 buddylist_build(); | |
871 } | |
872 | |
873 nbuddy = g_list_find(buddylist, unread_ptr); | |
874 if (nbuddy) { | |
875 prev_st = buddy_getstatus(BUDDATA(current_buddy)); | |
876 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); | |
877 current_buddy = nbuddy; | |
878 if (chatmode) | |
879 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); | |
880 // We should rebuild the buddylist but not everytime | |
881 // Here we check if we were locking a buddy who is actually offline, | |
882 // and hide_offline_buddies is TRUE. In which case we need to rebuild. | |
883 if (prev_st == offline && buddylist_get_hide_offline_buddies()) | |
884 buddylist_build(); | |
885 update_roster = TRUE; | |
886 | |
887 if (chatmode) scr_ShowBuddyWindow(); | |
888 } else scr_LogPrint("Error: nbuddy == NULL"); | |
889 } | |
890 } | |
891 | |
143 | 892 // scr_ScrollUp() |
893 // Scroll up the current buddy window, half a screen. | |
105 | 894 void scr_ScrollUp(void) |
895 { | |
896 const gchar *jid; | |
897 window_entry_t *win_entry; | |
898 int n, nblines; | |
899 GList *hbuf_top; | |
900 | |
901 // Get win_entry | |
902 if (!current_buddy) | |
903 return; | |
904 jid = CURRENT_JID; | |
905 if (!jid) | |
906 return; | |
907 win_entry = scr_SearchWindow(jid); | |
908 if (!win_entry) | |
909 return; | |
910 | |
911 // Scroll up half a screen (or less) | |
912 nblines = CHAT_WIN_HEIGHT/2-1; | |
913 hbuf_top = win_entry->top; | |
914 if (!hbuf_top) { | |
915 hbuf_top = g_list_last(win_entry->hbuf); | |
109 | 916 if (!win_entry->cleared) |
917 nblines *= 3; | |
918 else | |
919 win_entry->cleared = FALSE; | |
105 | 920 } |
921 | |
922 n = 0; | |
923 while (hbuf_top && n < nblines && g_list_previous(hbuf_top)) { | |
924 hbuf_top = g_list_previous(hbuf_top); | |
925 n++; | |
926 } | |
927 win_entry->top = hbuf_top; | |
928 | |
929 // Refresh the window | |
930 scr_UpdateWindow(win_entry); | |
931 | |
932 // Finished :) | |
933 update_panels(); | |
934 doupdate(); | |
935 } | |
936 | |
143 | 937 // scr_ScrollDown() |
938 // Scroll down the current buddy window, half a screen. | |
105 | 939 void scr_ScrollDown(void) |
940 { | |
941 const gchar *jid; | |
942 window_entry_t *win_entry; | |
943 int n, nblines; | |
944 GList *hbuf_top; | |
945 | |
946 // Get win_entry | |
947 if (!current_buddy) | |
948 return; | |
949 jid = CURRENT_JID; | |
950 if (!jid) | |
951 return; | |
952 win_entry = scr_SearchWindow(jid); | |
953 if (!win_entry) | |
954 return; | |
955 | |
956 // Scroll down half a screen (or less) | |
957 nblines = CHAT_WIN_HEIGHT/2-1; | |
958 hbuf_top = win_entry->top; | |
959 | |
960 for (n=0 ; hbuf_top && n < nblines ; n++) | |
961 hbuf_top = g_list_next(hbuf_top); | |
962 win_entry->top = hbuf_top; | |
963 // Check if we are at the bottom | |
964 for (n=0 ; hbuf_top && n < CHAT_WIN_HEIGHT-1 ; n++) | |
965 hbuf_top = g_list_next(hbuf_top); | |
966 if (!hbuf_top) | |
967 win_entry->top = NULL; // End reached | |
968 | |
969 // Refresh the window | |
970 scr_UpdateWindow(win_entry); | |
971 | |
972 // Finished :) | |
973 update_panels(); | |
974 doupdate(); | |
975 } | |
976 | |
143 | 977 // scr_Clear() |
978 // Clear the current buddy window (used for the /clear command) | |
108 | 979 void scr_Clear(void) |
980 { | |
981 const gchar *jid; | |
982 window_entry_t *win_entry; | |
983 | |
984 // Get win_entry | |
985 if (!current_buddy) | |
986 return; | |
987 jid = CURRENT_JID; | |
988 if (!jid) | |
989 return; | |
990 win_entry = scr_SearchWindow(jid); | |
991 if (!win_entry) | |
992 return; | |
993 | |
994 win_entry->cleared = TRUE; | |
109 | 995 win_entry->top = NULL; |
108 | 996 |
997 // Refresh the window | |
998 scr_UpdateWindow(win_entry); | |
999 | |
1000 // Finished :) | |
1001 update_panels(); | |
1002 doupdate(); | |
1003 } | |
1004 | |
187 | 1005 // scr_BufferTop() |
1006 // Jump to the head of the current buddy window | |
1007 void scr_BufferTop(void) | |
1008 { | |
1009 const gchar *jid; | |
1010 window_entry_t *win_entry; | |
1011 | |
1012 // Get win_entry | |
1013 if (!current_buddy) return; | |
1014 jid = CURRENT_JID; | |
1015 if (!jid) return; | |
1016 win_entry = scr_SearchWindow(jid); | |
1017 | |
1018 if (!win_entry) return; | |
1019 | |
1020 win_entry->cleared = FALSE; | |
1021 win_entry->top = g_list_first(win_entry->hbuf); | |
1022 | |
1023 // Refresh the window | |
1024 scr_UpdateWindow(win_entry); | |
1025 | |
1026 // Finished :) | |
1027 update_panels(); | |
1028 doupdate(); | |
1029 } | |
1030 | |
1031 // scr_BufferBottom() | |
1032 // Jump to the end of the current buddy window | |
1033 void scr_BufferBottom(void) | |
1034 { | |
1035 const gchar *jid; | |
1036 window_entry_t *win_entry; | |
1037 | |
1038 // Get win_entry | |
1039 if (!current_buddy) return; | |
1040 jid = CURRENT_JID; | |
1041 if (!jid) return; | |
1042 win_entry = scr_SearchWindow(jid); | |
1043 | |
1044 if (!win_entry) return; | |
1045 | |
1046 win_entry->cleared = FALSE; | |
1047 win_entry->top = NULL; | |
1048 | |
1049 // Refresh the window | |
1050 scr_UpdateWindow(win_entry); | |
1051 | |
1052 // Finished :) | |
1053 update_panels(); | |
1054 doupdate(); | |
1055 } | |
1056 | |
44 | 1057 // scr_LogPrint(...) |
1058 // Display a message in the log window. | |
24 | 1059 void scr_LogPrint(const char *fmt, ...) |
1060 { | |
1061 time_t timestamp; | |
1062 char *buffer; | |
1063 va_list ap; | |
1064 | |
153 | 1065 do { |
157 | 1066 buffer = (char *) calloc(1, 1024); |
153 | 1067 } while (!buffer); |
24 | 1068 |
1069 timestamp = time(NULL); | |
1070 strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp)); | |
1071 wprintw(logWnd, "\n%s", buffer); | |
1072 | |
1073 va_start(ap, fmt); | |
153 | 1074 vsnprintf(buffer, 1024, fmt, ap); |
24 | 1075 va_end(ap); |
1076 | |
1077 wprintw(logWnd, "%s", buffer); | |
1078 free(buffer); | |
1079 | |
1080 update_panels(); | |
1081 doupdate(); | |
1082 } | |
1083 | |
143 | 1084 // scr_set_chatmode() |
1085 // Public fonction to (un)set chatmode... | |
129 | 1086 inline void scr_set_chatmode(int enable) |
1087 { | |
1088 chatmode = enable; | |
1089 } | |
1090 | |
238 | 1091 // scr_get_multimode() |
1092 // Public fonction to get multimode status... | |
1093 inline int scr_get_multimode() | |
1094 { | |
1095 return multimode; | |
1096 } | |
1097 | |
1098 // scr_set_multimode() | |
1099 // Public fonction to (un)set multimode... | |
260
33e1a05864a6
Add "verbatim multi-line" mode, with commands disabled
mikael@frmp8452
parents:
252
diff
changeset
|
1100 // Convention: |
33e1a05864a6
Add "verbatim multi-line" mode, with commands disabled
mikael@frmp8452
parents:
252
diff
changeset
|
1101 // 0 = disabled / 1 = multimode / 2 = multimode verbatim (commands disabled) |
238 | 1102 inline void scr_set_multimode(int enable) |
1103 { | |
1104 if (multiline) { | |
1105 g_free(multiline); | |
1106 multiline = NULL; | |
1107 } | |
260
33e1a05864a6
Add "verbatim multi-line" mode, with commands disabled
mikael@frmp8452
parents:
252
diff
changeset
|
1108 multimode = enable; |
238 | 1109 } |
1110 | |
1111 // scr_get_multiline() | |
260
33e1a05864a6
Add "verbatim multi-line" mode, with commands disabled
mikael@frmp8452
parents:
252
diff
changeset
|
1112 // Public fonction to get the current multi-line. |
238 | 1113 inline const char *scr_get_multiline() |
1114 { | |
1115 if (multimode && multiline) | |
1116 return multiline; | |
1117 else | |
1118 return ""; | |
1119 } | |
1120 | |
1121 // scr_append_multiline(line) | |
1122 // Public function to append a line to the current multi-line message. | |
1123 // Skip empty leading lines. | |
1124 void scr_append_multiline(const char *line) | |
1125 { | |
1126 static int num; | |
1127 | |
1128 if (!multimode) { | |
1129 scr_LogPrint("Error: Not in multi-line message mode!"); | |
1130 return; | |
1131 } | |
1132 if (multiline) { | |
1133 int len = strlen(multiline)+strlen(line)+2; | |
252 | 1134 if (len >= HBB_BLOCKSIZE - 1) { |
238 | 1135 // We don't handle single messages with size > HBB_BLOCKSIZE |
1136 // (see hbuf) | |
1137 scr_LogPrint("Your multi-line message is too big, this line has " | |
1138 "not been added."); | |
1139 scr_LogPrint("Please send this part now..."); | |
1140 return; | |
1141 } | |
1142 multiline = g_renew(char, multiline, len); | |
1143 strcat(multiline, "\n"); | |
1144 strcat(multiline, line); | |
1145 num++; | |
1146 } else { | |
1147 // First message line (we skip leading empty lines) | |
1148 num = 0; | |
1149 if (line[0]) { | |
1150 multiline = g_new(char, strlen(line)+1); | |
1151 strcpy(multiline, line); | |
1152 num++; | |
1153 } else | |
1154 return; | |
1155 } | |
1156 scr_LogPrint("Multi-line mode: line #%d added [%.25s...", num, line); | |
1157 } | |
1158 | |
173 | 1159 // scr_cmdhisto_addline() |
1160 // Add a line to the inputLine history | |
1161 inline void scr_cmdhisto_addline(char *line) | |
1162 { | |
1163 if (!line || !*line) return; | |
1164 | |
1165 cmdhisto = g_list_append(cmdhisto, g_strdup(line)); | |
1166 } | |
1167 | |
1168 // scr_cmdhisto_prev() | |
1169 // Look for previous line beginning w/ the given mask in the inputLine history | |
175 | 1170 // Returns NULL if none found |
173 | 1171 const char *scr_cmdhisto_prev(char *mask, guint len) |
1172 { | |
1173 GList *hl; | |
1174 if (!cmdhisto_cur) { | |
1175 hl = g_list_last(cmdhisto); | |
174 | 1176 if (hl) { // backup current line |
1177 strncpy(cmdhisto_backup, mask, INPUTLINE_LENGTH); | |
1178 } | |
173 | 1179 } else { |
1180 hl = g_list_previous(cmdhisto_cur); | |
1181 } | |
1182 while (hl) { | |
1183 if (!strncmp((char*)hl->data, mask, len)) { | |
1184 // Found a match | |
1185 cmdhisto_cur = hl; | |
1186 return (const char*)hl->data; | |
1187 } | |
1188 hl = g_list_previous(hl); | |
1189 } | |
1190 return NULL; | |
1191 } | |
1192 | |
1193 // scr_cmdhisto_next() | |
1194 // Look for next line beginning w/ the given mask in the inputLine history | |
175 | 1195 // Returns NULL if none found |
173 | 1196 const char *scr_cmdhisto_next(char *mask, guint len) |
1197 { | |
1198 GList *hl; | |
1199 if (!cmdhisto_cur) return NULL; | |
1200 hl = cmdhisto_cur; | |
1201 while ((hl = g_list_next(hl)) != NULL) | |
1202 if (!strncmp((char*)hl->data, mask, len)) { | |
1203 // Found a match | |
1204 cmdhisto_cur = hl; | |
1205 return (const char*)hl->data; | |
1206 } | |
175 | 1207 // If the "backuped" line matches, we'll use it |
1208 if (strncmp(cmdhisto_backup, mask, len)) return NULL; // No match | |
174 | 1209 cmdhisto_cur = NULL; |
1210 return cmdhisto_backup; | |
173 | 1211 } |
1212 | |
195 | 1213 // readline_transpose_chars() |
1214 // Drag the character before point forward over the character at | |
1215 // point, moving point forward as well. If point is at the end of | |
1216 // the line, then this transposes the two characters before point. | |
1217 void readline_transpose_chars() | |
1218 { | |
1219 char swp; | |
1220 | |
1221 if (ptr_inputline == inputLine) return; | |
1222 | |
1223 if (!*ptr_inputline) { // We're at EOL | |
1224 // If line is only 1 char long, nothing to do... | |
1225 if (ptr_inputline == inputLine+1) return; | |
1226 // Transpose the two previous characters | |
1227 swp = *(ptr_inputline-2); | |
1228 *(ptr_inputline-2) = *(ptr_inputline-1); | |
1229 *(ptr_inputline-1) = swp; | |
1230 } else { | |
196 | 1231 // Swap the two characters before the cursor and move right. |
195 | 1232 swp = *(ptr_inputline-1); |
1233 *(ptr_inputline-1) = *ptr_inputline; | |
1234 *ptr_inputline++ = swp; | |
1235 check_offset(1); | |
1236 } | |
1237 } | |
1238 | |
1239 // readline_backward_kill_word() | |
194 | 1240 // Kill the word before the cursor, in input line |
195 | 1241 void readline_backward_kill_word() |
194 | 1242 { |
1243 char *c, *old = ptr_inputline; | |
1244 int spaceallowed = 1; | |
1245 | |
1246 if (ptr_inputline == inputLine) return; | |
1247 | |
1248 for (c = ptr_inputline-1 ; c > inputLine ; c--) | |
1249 if (!isalnum(*c)) { | |
1250 if (*c == ' ') | |
1251 if (!spaceallowed) break; | |
1252 } else spaceallowed = 0; | |
1253 | |
1254 if (c != inputLine || *c != ' ') | |
1255 if ((c < ptr_inputline-1) && (!isalnum(*c))) | |
1256 c++; | |
1257 | |
1258 // Modify the line | |
1259 ptr_inputline = c; | |
1260 for (;;) { | |
1261 *c = *old++; | |
1262 if (!*c++) break; | |
1263 } | |
195 | 1264 check_offset(-1); |
194 | 1265 } |
1266 | |
98 | 1267 // which_row() |
1268 // Tells which row our cursor is in, in the command line. | |
1269 // -1 -> normal text | |
1270 // 0 -> command | |
1271 // 1 -> parameter 1 (etc.) | |
102 | 1272 // If > 0, then *p_row is set to the beginning of the row |
1273 int which_row(char **p_row) | |
98 | 1274 { |
1275 int row = -1; | |
1276 char *p; | |
1277 int quote = FALSE; | |
1278 | |
1279 // Not a command? | |
1280 if ((ptr_inputline == inputLine) || (inputLine[0] != '/')) | |
1281 return -1; | |
1282 | |
1283 // This is a command | |
1284 row = 0; | |
1285 for (p = inputLine ; p < ptr_inputline ; p++) { | |
1286 if (quote) { | |
1287 if (*p == '"' && *(p-1) != '\\') | |
1288 quote = FALSE; | |
1289 continue; | |
1290 } | |
1291 if (*p == '"' && *(p-1) != '\\') { | |
1292 quote = TRUE; | |
121 | 1293 } else if (*p == ' ') { |
1294 if (*(p-1) != ' ') | |
1295 row++; | |
102 | 1296 *p_row = p+1; |
1297 } | |
98 | 1298 } |
1299 return row; | |
1300 } | |
1301 | |
143 | 1302 // scr_insert_text() |
1303 // Insert the given text at the current cursor position. | |
1304 // The cursor is moved. We don't check if the cursor still is in the screen | |
1305 // after, the caller should do that. | |
98 | 1306 void scr_insert_text(const char *text) |
1307 { | |
1308 char tmpLine[INPUTLINE_LENGTH+1]; | |
1309 int len = strlen(text); | |
1310 // Check the line isn't too long | |
1311 if (strlen(inputLine) + len >= INPUTLINE_LENGTH) { | |
1312 scr_LogPrint("Cannot insert text, line too long."); | |
1313 return; | |
1314 } | |
1315 | |
1316 strcpy(tmpLine, ptr_inputline); | |
1317 strcpy(ptr_inputline, text); ptr_inputline += len; | |
1318 strcpy(ptr_inputline, tmpLine); | |
1319 } | |
1320 | |
143 | 1321 // scr_handle_tab() |
1322 // Function called when tab is pressed. | |
1323 // Initiate or continue a completion... | |
98 | 1324 void scr_handle_tab(void) |
1325 { | |
102 | 1326 int nrow; |
1327 char *row; | |
1328 const char *cchar; | |
103 | 1329 guint compl_categ; |
98 | 1330 |
102 | 1331 nrow = which_row(&row); |
98 | 1332 |
103 | 1333 // a) No completion if no leading slash ('cause not a command) |
1334 // b) We can't have more than 2 parameters (we use 2 flags) | |
1335 if (nrow < 0 || nrow > 2) return; | |
102 | 1336 |
103 | 1337 if (nrow == 0) { // Command completion |
1338 row = &inputLine[1]; | |
1339 compl_categ = COMPL_CMD; | |
1340 } else { // Other completion, depending on the command | |
1341 cmd *com = cmd_get(inputLine); | |
1342 if (!com || !row) { | |
1343 scr_LogPrint("I cannot complete that..."); | |
1344 return; | |
1345 } | |
1346 compl_categ = com->completion_flags[nrow-1]; | |
1347 } | |
1348 | |
1349 if (!completion_started) { | |
1350 GSList *list = compl_get_category_list(compl_categ); | |
1351 if (list) { | |
1352 char *prefix = g_strndup(row, ptr_inputline-row); | |
1353 // Init completion | |
1354 new_completion(prefix, list); | |
1355 g_free(prefix); | |
1356 // Now complete | |
98 | 1357 cchar = complete(); |
1358 if (cchar) | |
1359 scr_insert_text(cchar); | |
103 | 1360 completion_started = TRUE; |
98 | 1361 } |
103 | 1362 } else { // Completion already initialized |
1363 char *c; | |
1364 guint back = cancel_completion(); | |
1365 // Remove $back chars | |
1366 ptr_inputline -= back; | |
1367 c = ptr_inputline; | |
1368 for ( ; *c ; c++) | |
1369 *c = *(c+back); | |
1370 // Now complete again | |
1371 cchar = complete(); | |
1372 if (cchar) | |
1373 scr_insert_text(cchar); | |
102 | 1374 } |
98 | 1375 } |
1376 | |
1377 void scr_cancel_current_completion(void) | |
1378 { | |
1379 char *c; | |
1380 guint back = cancel_completion(); | |
1381 // Remove $back chars | |
1382 ptr_inputline -= back; | |
1383 c = ptr_inputline; | |
1384 for ( ; *c ; c++) | |
1385 *c = *(c+back); | |
1386 } | |
1387 | |
1388 void scr_end_current_completion(void) | |
1389 { | |
1390 done_completion(); | |
1391 completion_started = FALSE; | |
1392 } | |
1393 | |
24 | 1394 // check_offset(int direction) |
1395 // Check inputline_offset value, and make sure the cursor is inside the | |
1396 // screen. | |
1397 inline void check_offset(int direction) | |
1398 { | |
1399 // Left side | |
1400 if (inputline_offset && direction <= 0) { | |
1401 while (ptr_inputline <= (char*)&inputLine + inputline_offset) { | |
1402 if (inputline_offset) { | |
1403 inputline_offset -= 5; | |
1404 if (inputline_offset < 0) | |
1405 inputline_offset = 0; | |
1406 } | |
1407 } | |
1408 } | |
1409 // Right side | |
1410 if (direction >= 0) { | |
1411 while (ptr_inputline >= inputline_offset + (char*)&inputLine + maxX) | |
1412 inputline_offset += 5; | |
1413 } | |
1414 } | |
1415 | |
44 | 1416 // process_key(key) |
1417 // Handle the pressed key, in the command line (bottom). | |
29 | 1418 int process_key(int key) |
24 | 1419 { |
1420 if (isprint(key)) { | |
1421 char tmpLine[INPUTLINE_LENGTH+1]; | |
1422 | |
1423 // Check the line isn't too long | |
1424 if (strlen(inputLine) >= INPUTLINE_LENGTH) | |
1425 return 0; | |
1426 | |
1427 // Insert char | |
1428 strcpy(tmpLine, ptr_inputline); | |
1429 *ptr_inputline++ = key; | |
1430 strcpy(ptr_inputline, tmpLine); | |
1431 check_offset(1); | |
1432 } else { | |
1433 switch(key) { | |
232 | 1434 case 8: // Ctrl-h |
1435 case 127: // Backspace too | |
24 | 1436 case KEY_BACKSPACE: |
1437 if (ptr_inputline != (char*)&inputLine) { | |
42 | 1438 char *c = --ptr_inputline; |
1439 for ( ; *c ; c++) | |
1440 *c = *(c+1); | |
24 | 1441 check_offset(-1); |
1442 } | |
1443 break; | |
238 | 1444 case KEY_DC:// Del |
24 | 1445 if (*ptr_inputline) |
1446 strcpy(ptr_inputline, ptr_inputline+1); | |
1447 break; | |
1448 case KEY_LEFT: | |
1449 if (ptr_inputline != (char*)&inputLine) { | |
1450 ptr_inputline--; | |
1451 check_offset(-1); | |
1452 } | |
1453 break; | |
1454 case KEY_RIGHT: | |
1455 if (*ptr_inputline) | |
1456 ptr_inputline++; | |
1457 check_offset(1); | |
1458 break; | |
98 | 1459 case 7: // Ctrl-g |
1460 scr_cancel_current_completion(); | |
1461 scr_end_current_completion(); | |
1462 check_offset(-1); | |
1463 break; | |
24 | 1464 case 9: // Tab |
98 | 1465 scr_handle_tab(); |
1466 check_offset(0); | |
24 | 1467 break; |
1468 case '\n': // Enter | |
29 | 1469 if (process_line(inputLine)) |
24 | 1470 return 255; |
173 | 1471 // Add line to history |
1472 scr_cmdhisto_addline(inputLine); | |
1473 cmdhisto_cur = NULL; | |
1474 // Reset the line | |
24 | 1475 ptr_inputline = inputLine; |
1476 *ptr_inputline = 0; | |
1477 inputline_offset = 0; | |
1478 break; | |
1479 case KEY_UP: | |
175 | 1480 { |
1481 const char *l = scr_cmdhisto_prev(inputLine, | |
1482 ptr_inputline-inputLine); | |
1483 if (l) { | |
1484 strcpy(inputLine, l); | |
1485 } | |
1486 } | |
24 | 1487 break; |
1488 case KEY_DOWN: | |
175 | 1489 { |
1490 const char *l = scr_cmdhisto_next(inputLine, | |
1491 ptr_inputline-inputLine); | |
1492 if (l) { | |
1493 strcpy(inputLine, l); | |
1494 } | |
1495 } | |
24 | 1496 break; |
1497 case KEY_PPAGE: | |
175 | 1498 scr_RosterUp(); |
24 | 1499 break; |
1500 case KEY_NPAGE: | |
175 | 1501 scr_RosterDown(); |
24 | 1502 break; |
1503 case KEY_HOME: | |
1504 case 1: | |
1505 ptr_inputline = inputLine; | |
1506 inputline_offset = 0; | |
1507 break; | |
1508 case KEY_END: | |
1509 case 5: | |
1510 for (; *ptr_inputline; ptr_inputline++) ; | |
1511 check_offset(1); | |
1512 break; | |
1513 case 21: // Ctrl-u | |
1514 strcpy(inputLine, ptr_inputline); | |
1515 ptr_inputline = inputLine; | |
1516 inputline_offset = 0; | |
1517 break; | |
1518 case KEY_EOL: | |
1519 case 11: // Ctrl-k | |
1520 *ptr_inputline = 0; | |
1521 break; | |
1522 case 16: // Ctrl-p | |
175 | 1523 scr_ScrollUp(); |
24 | 1524 break; |
1525 case 14: // Ctrl-n | |
175 | 1526 scr_ScrollDown(); |
24 | 1527 break; |
196 | 1528 case 17: // Ctrl-q |
236 | 1529 scr_RosterUnreadMessage(1); // next unread message |
196 | 1530 break; |
195 | 1531 case 20: // Ctrl-t |
1532 readline_transpose_chars(); | |
1533 break; | |
194 | 1534 case 23: // Ctrl-w |
195 | 1535 readline_backward_kill_word(); |
194 | 1536 break; |
24 | 1537 case 27: // ESC |
1538 currentWindow = NULL; | |
1539 chatmode = FALSE; | |
119 | 1540 if (current_buddy) |
1541 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); | |
24 | 1542 top_panel(chatPanel); |
1543 top_panel(inputPanel); | |
157 | 1544 update_panels(); |
24 | 1545 break; |
151 | 1546 case 12: // Ctrl-l |
1547 case KEY_RESIZE: | |
1548 scr_Resize(); | |
1549 break; | |
24 | 1550 default: |
1551 scr_LogPrint("Unkown key=%d", key); | |
232 | 1552 if (utf8_mode) |
1553 scr_LogPrint("WARNING: UTF-8 not yet supported!"); | |
24 | 1554 } |
1555 } | |
157 | 1556 if (completion_started && key != 9 && key != KEY_RESIZE) |
98 | 1557 scr_end_current_completion(); |
24 | 1558 mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset); |
1559 wclrtoeol(inputWnd); | |
1560 if (*ptr_inputline) { | |
1561 wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset); | |
1562 } | |
157 | 1563 if (!update_roster) { |
1564 //update_panels(); | |
1565 doupdate(); | |
1566 } | |
24 | 1567 return 0; |
1568 } |