Code

use glib regex for list_window_jump.
[ncmpc.git] / src / list_window.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "list_window.h"
21 #include "config.h"
22 #include "options.h"
23 #include "charset.h"
24 #include "match.h"
25 #include "command.h"
26 #include "colors.h"
27 #include "paint.h"
28 #include "screen_message.h"
29 #include "i18n.h"
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
36 extern void screen_bell(void);
38 struct list_window *
39 list_window_init(WINDOW *w, unsigned width, unsigned height)
40 {
41         struct list_window *lw;
43         lw = g_malloc0(sizeof(*lw));
44         lw->w = w;
45         lw->cols = width;
46         lw->rows = height;
47         lw->range_selection = false;
48         return lw;
49 }
51 void
52 list_window_free(struct list_window *lw)
53 {
54         assert(lw != NULL);
56         g_free(lw);
57 }
59 void
60 list_window_reset(struct list_window *lw)
61 {
62         lw->selected = 0;
63         lw->range_selection = false;
64         lw->range_base = 0;
65         lw->start = 0;
66 }
68 static unsigned
69 list_window_validate_index(const struct list_window *lw, unsigned i)
70 {
71         if (lw->length == 0)
72                 return 0;
73         else if (i >= lw->length)
74                 return lw->length - 1;
75         else
76                 return i;
77 }
79 static void
80 list_window_check_selected(struct list_window *lw)
81 {
82         lw->selected = list_window_validate_index(lw, lw->selected);
84         if(lw->range_selection)
85                 lw->range_base =
86                         list_window_validate_index(lw, lw->range_base);
87 }
89 /**
90  * Scroll after the cursor was moved, the list was changed or the
91  * window was resized.
92  */
93 static void
94 list_window_check_origin(struct list_window *lw)
95 {
96         int start = lw->start;
98         if ((unsigned) options.scroll_offset * 2 >= lw->rows)
99                 // Center if the offset is more than half the screen
100                 start = lw->selected - lw->rows / 2;
101         else {
102                 if (lw->selected < lw->start + options.scroll_offset)
103                         start = lw->selected - options.scroll_offset;
105                 if (lw->selected >= lw->start + lw->rows - options.scroll_offset)
106                         start = lw->selected - lw->rows + 1 + options.scroll_offset;
107         }
109         if (start + lw->rows > lw->length)
110                 start = lw->length - lw->rows;
112         if (start < 0 || lw->length == 0)
113                 start = 0;
115         lw->start = start;
118 void
119 list_window_resize(struct list_window *lw, unsigned width, unsigned height)
121         lw->cols = width;
122         lw->rows = height;
124         list_window_check_origin(lw);
127 void
128 list_window_set_length(struct list_window *lw, unsigned length)
130         if (length == lw->length)
131                 return;
133         lw->length = length;
135         list_window_check_selected(lw);
136         list_window_check_origin(lw);
139 void
140 list_window_center(struct list_window *lw, unsigned n)
142         if (n > lw->rows / 2)
143                 lw->start = n - lw->rows / 2;
144         else
145                 lw->start = 0;
147         if (lw->start + lw->rows > lw->length) {
148                 if (lw->rows < lw->length)
149                         lw->start = lw->length - lw->rows;
150                 else
151                         lw->start = 0;
152         }
155 void
156 list_window_set_cursor(struct list_window *lw, unsigned i)
158         lw->range_selection = false;
159         lw->selected = i;
161         list_window_check_selected(lw);
162         list_window_check_origin(lw);
165 void
166 list_window_move_cursor(struct list_window *lw, unsigned n)
168         lw->selected = n;
170         list_window_check_selected(lw);
171         list_window_check_origin(lw);
174 void
175 list_window_fetch_cursor(struct list_window *lw)
177         if (lw->start > 0 &&
178             lw->selected < lw->start + options.scroll_offset)
179                 list_window_move_cursor(lw, lw->start + options.scroll_offset);
180         else if (lw->start + lw->rows < lw->length &&
181                  lw->selected > lw->start + lw->rows - 1 - options.scroll_offset)
182                 list_window_move_cursor(lw, lw->start + lw->rows - 1 - options.scroll_offset);
185 void
186 list_window_get_range(const struct list_window *lw,
187                       struct list_window_range *range)
189         if (lw->length == 0) {
190                 /* empty list - no selection */
191                 range->start = 0;
192                 range->end = 0;
193         } else if (lw->range_selection) {
194                 /* a range selection */
195                 if (lw->range_base < lw->selected) {
196                         range->start = lw->range_base;
197                         range->end = lw->selected + 1;
198                 } else {
199                         range->start = lw->selected;
200                         range->end = lw->range_base + 1;
201                 }
202         } else {
203                 /* no range, just the cursor */
204                 range->start = lw->selected;
205                 range->end = lw->selected + 1;
206         }
209 static void
210 list_window_next(struct list_window *lw)
212         if (lw->selected + 1 < lw->length)
213                 list_window_move_cursor(lw, lw->selected + 1);
214         else if (options.list_wrap)
215                 list_window_move_cursor(lw, 0);
218 static void
219 list_window_previous(struct list_window *lw)
221         if (lw->selected > 0)
222                 list_window_move_cursor(lw, lw->selected - 1);
223         else if (options.list_wrap)
224                 list_window_move_cursor(lw, lw->length - 1);
227 static void
228 list_window_top(struct list_window *lw)
230         if (lw->start == 0)
231                 list_window_move_cursor(lw, lw->start);
232         else
233                 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
234                         list_window_move_cursor(lw, lw->start + lw->rows / 2);
235                 else
236                         list_window_move_cursor(lw, lw->start + options.scroll_offset);
239 static void
240 list_window_middle(struct list_window *lw)
242         if (lw->length >= lw->rows)
243                 list_window_move_cursor(lw, lw->start + lw->rows / 2);
244         else
245                 list_window_move_cursor(lw, lw->length / 2);
248 static void
249 list_window_bottom(struct list_window *lw)
251         if (lw->length >= lw->rows)
252                 if ((unsigned) options.scroll_offset * 2 >= lw->rows)
253                         list_window_move_cursor(lw, lw->start + lw->rows / 2);
254                 else
255                         if (lw->start + lw->rows == lw->length)
256                                 list_window_move_cursor(lw, lw->length - 1);
257                         else
258                                 list_window_move_cursor(lw, lw->start + lw->rows - 1 - options.scroll_offset);
259         else
260                 list_window_move_cursor(lw, lw->length - 1);
263 static void
264 list_window_first(struct list_window *lw)
266         list_window_move_cursor(lw, 0);
269 static void
270 list_window_last(struct list_window *lw)
272         if (lw->length > 0)
273                 list_window_move_cursor(lw, lw->length - 1);
274         else
275                 list_window_move_cursor(lw, 0);
278 static void
279 list_window_next_page(struct list_window *lw)
281         if (lw->rows < 2)
282                 return;
283         if (lw->selected + lw->rows < lw->length)
284                 list_window_move_cursor(lw, lw->selected + lw->rows - 1);
285         else
286                 list_window_last(lw);
289 static void
290 list_window_previous_page(struct list_window *lw)
292         if (lw->rows < 2)
293                 return;
294         if (lw->selected > lw->rows - 1)
295                 list_window_move_cursor(lw, lw->selected - lw->rows + 1);
296         else
297                 list_window_first(lw);
300 static void
301 list_window_scroll_up(struct list_window *lw, unsigned n)
303         if (lw->start > 0) {
304                 if (n > lw->start)
305                         lw->start = 0;
306                 else
307                         lw->start -= n;
309                 list_window_fetch_cursor(lw);
310         }
313 static void
314 list_window_scroll_down(struct list_window *lw, unsigned n)
316         if (lw->start + lw->rows < lw->length)
317         {
318                 if ( lw->start + lw->rows + n > lw->length - 1)
319                         lw->start = lw->length - lw->rows;
320                 else
321                         lw->start += n;
323                 list_window_fetch_cursor(lw);
324         }
327 static void
328 list_window_paint_row(WINDOW *w, unsigned width, bool selected,
329                       const char *text)
331         row_paint_text(w, width, COLOR_LIST,
332                        selected, text);
335 void
336 list_window_paint(const struct list_window *lw,
337                   list_window_callback_fn_t callback,
338                   void *callback_data)
340         bool show_cursor = !lw->hide_cursor &&
341                 (!options.hardware_cursor || lw->range_selection);
342         struct list_window_range range;
344         if (show_cursor)
345                 list_window_get_range(lw, &range);
347         for (unsigned i = 0; i < lw->rows; i++) {
348                 const char *label;
350                 wmove(lw->w, i, 0);
352                 if (lw->start + i >= lw->length) {
353                         wclrtobot(lw->w);
354                         break;
355                 }
357                 label = callback(lw->start + i, callback_data);
358                 assert(label != NULL);
360                 list_window_paint_row(lw->w, lw->cols,
361                                       show_cursor &&
362                                       lw->start + i >= range.start &&
363                                       lw->start + i < range.end,
364                                       label);
365         }
367         row_color_end(lw->w);
369         if (options.hardware_cursor && lw->selected >= lw->start &&
370             lw->selected < lw->start + lw->rows) {
371                 curs_set(1);
372                 wmove(lw->w, lw->selected - lw->start, 0);
373         }
376 void
377 list_window_paint2(const struct list_window *lw,
378                    list_window_paint_callback_t paint_callback,
379                    void *callback_data)
381         bool show_cursor = !lw->hide_cursor &&
382                 (!options.hardware_cursor || lw->range_selection);
383         struct list_window_range range;
385         if (show_cursor)
386                 list_window_get_range(lw, &range);
388         for (unsigned i = 0; i < lw->rows; i++) {
389                 bool selected;
391                 wmove(lw->w, i, 0);
393                 if (lw->start + i >= lw->length) {
394                         wclrtobot(lw->w);
395                         break;
396                 }
398                 selected = show_cursor &&
399                         lw->start + i >= range.start &&
400                         lw->start + i < range.end;
402                 paint_callback(lw->w, lw->start + i, i, lw->cols,
403                                selected, callback_data);
404         }
406         if (options.hardware_cursor && lw->selected >= lw->start &&
407             lw->selected < lw->start + lw->rows) {
408                 curs_set(1);
409                 wmove(lw->w, lw->selected - lw->start, 0);
410         }
413 bool
414 list_window_find(struct list_window *lw,
415                  list_window_callback_fn_t callback,
416                  void *callback_data,
417                  const char *str,
418                  bool wrap,
419                  bool bell_on_wrap)
421         unsigned i = lw->selected + 1;
422         const char *label;
424         assert(str != NULL);
426         do {
427                 while (i < lw->length) {
428                         label = callback(i, callback_data);
429                         assert(label != NULL);
431                         if (match_line(label, str)) {
432                                 list_window_move_cursor(lw, i);
433                                 return true;
434                         }
435                         if (wrap && i == lw->selected)
436                                 return false;
437                         i++;
438                 }
439                 if (wrap) {
440                         if (i == 0) /* empty list */
441                                 return 1;
442                         i=0; /* first item */
443                         if (bell_on_wrap) {
444                                 screen_bell();
445                         }
446                 }
447         } while (wrap);
449         return false;
452 bool
453 list_window_rfind(struct list_window *lw,
454                   list_window_callback_fn_t callback,
455                   void *callback_data,
456                   const char *str,
457                   bool wrap,
458                   bool bell_on_wrap)
460         int i = lw->selected - 1;
461         const char *label;
463         assert(str != NULL);
465         if (lw->length == 0)
466                 return false;
468         do {
469                 while (i >= 0) {
470                         label = callback(i, callback_data);
471                         assert(label != NULL);
473                         if (match_line(label, str)) {
474                                 list_window_move_cursor(lw, i);
475                                 return true;
476                         }
477                         if (wrap && i == (int)lw->selected)
478                                 return false;
479                         i--;
480                 }
481                 if (wrap) {
482                         i = lw->length - 1; /* last item */
483                         if (bell_on_wrap) {
484                                 screen_bell();
485                         }
486                 }
487         } while (wrap);
489         return false;
492 #ifdef NCMPC_MINI
493 bool
494 list_window_jump(struct list_window *lw,
495                  list_window_callback_fn_t callback,
496                  void *callback_data,
497                  const char *str)
499         unsigned i;
500         const char *label;
502         assert(str != NULL);
504         for (i = 0; i < lw->length; i++) {
505                 label = callback(i, callback_data);
506                 assert(label != NULL);
508                 if (g_ascii_strncasecmp(label, str, strlen(str)) == 0) {
509                         list_window_move_cursor(lw, i);
510                         return true;
511                 }
512         }
513         return false;
515 #else
516 bool
517 list_window_jump(struct list_window *lw,
518                  list_window_callback_fn_t callback,
519                  void *callback_data,
520                  const char *str)
522         unsigned i;
523         const char *label;
524         GRegex *regex;
526         assert(str != NULL);
528         regex = compile_regex(str, options.jump_prefix_only);
529         if (regex == NULL)
530                 return false;
532         for (i = 0; i < lw->length; i++) {
533                 label = callback(i, callback_data);
534                 assert(label != NULL);
536                 if (match_regex(regex, label)) {
537                         g_regex_unref(regex);
538                         list_window_move_cursor(lw, i);
539                         return true;
540                 }
541         }
542         g_regex_unref(regex);
543         return false;
545 #endif
547 /* perform basic list window commands (movement) */
548 bool
549 list_window_cmd(struct list_window *lw, command_t cmd)
551         switch (cmd) {
552         case CMD_LIST_PREVIOUS:
553                 list_window_previous(lw);
554                 break;
555         case CMD_LIST_NEXT:
556                 list_window_next(lw);
557                 break;
558         case CMD_LIST_TOP:
559                 list_window_top(lw);
560                 break;
561         case CMD_LIST_MIDDLE:
562                 list_window_middle(lw);
563                 break;
564         case CMD_LIST_BOTTOM:
565                 list_window_bottom(lw);
566                 break;
567         case CMD_LIST_FIRST:
568                 list_window_first(lw);
569                 break;
570         case CMD_LIST_LAST:
571                 list_window_last(lw);
572                 break;
573         case CMD_LIST_NEXT_PAGE:
574                 list_window_next_page(lw);
575                 break;
576         case CMD_LIST_PREVIOUS_PAGE:
577                 list_window_previous_page(lw);
578                 break;
579         case CMD_LIST_RANGE_SELECT:
580                 if(lw->range_selection)
581                 {
582                         screen_status_printf(_("Range selection disabled"));
583                         list_window_set_cursor(lw, lw->selected);
584                 }
585                 else
586                 {
587                         screen_status_printf(_("Range selection enabled"));
588                         lw->range_base = lw->selected;
589                         lw->range_selection = true;
590                 }
591                 break;
592         case CMD_LIST_SCROLL_UP_LINE:
593                 list_window_scroll_up(lw, 1);
594                 break;
595         case CMD_LIST_SCROLL_DOWN_LINE:
596                 list_window_scroll_down(lw, 1);
597                 break;
598         case CMD_LIST_SCROLL_UP_HALF:
599                 list_window_scroll_up(lw, (lw->rows - 1) / 2);
600                 break;
601         case CMD_LIST_SCROLL_DOWN_HALF:
602                 list_window_scroll_down(lw, (lw->rows - 1) / 2);
603                 break;
604         default:
605                 return false;
606         }
608         return true;
611 bool
612 list_window_scroll_cmd(struct list_window *lw, command_t cmd)
614         switch (cmd) {
615         case CMD_LIST_SCROLL_UP_LINE:
616         case CMD_LIST_PREVIOUS:
617                 if (lw->start > 0)
618                         lw->start--;
619                 break;
621         case CMD_LIST_SCROLL_DOWN_LINE:
622         case CMD_LIST_NEXT:
623                 if (lw->start + lw->rows < lw->length)
624                         lw->start++;
625                 break;
627         case CMD_LIST_FIRST:
628                 lw->start = 0;
629                 break;
631         case CMD_LIST_LAST:
632                 if (lw->length > lw->rows)
633                         lw->start = lw->length - lw->rows;
634                 else
635                         lw->start = 0;
636                 break;
638         case CMD_LIST_NEXT_PAGE:
639                 lw->start += lw->rows - 1;
640                 if (lw->start + lw->rows > lw->length) {
641                         if (lw->length > lw->rows)
642                                 lw->start = lw->length - lw->rows;
643                         else
644                                 lw->start = 0;
645                 }
646                 break;
648         case CMD_LIST_PREVIOUS_PAGE:
649                 if (lw->start > lw->rows)
650                         lw->start -= lw->rows;
651                 else
652                         lw->start = 0;
653                 break;
655         case CMD_LIST_SCROLL_UP_HALF:
656                 if (lw->start > (lw->rows - 1) / 2)
657                         lw->start -= (lw->rows - 1) / 2;
658                 else
659                         lw->start = 0;
660                 break;
662         case CMD_LIST_SCROLL_DOWN_HALF:
663                 lw->start += (lw->rows - 1) / 2;
664                 if (lw->start + lw->rows > lw->length) {
665                         if (lw->length > lw->rows)
666                                 lw->start = lw->length - lw->rows;
667                         else
668                                 lw->start = 0;
669                 }
670                 break;
672         default:
673                 return false;
674         }
676         return true;
679 #ifdef HAVE_GETMOUSE
680 bool
681 list_window_mouse(struct list_window *lw, unsigned long bstate, int y)
683         assert(lw != NULL);
685         /* if the even occurred above the list window move up */
686         if (y < 0) {
687                 if (bstate & BUTTON3_CLICKED)
688                         list_window_first(lw);
689                 else
690                         list_window_previous_page(lw);
691                 return true;
692         }
694         /* if the even occurred below the list window move down */
695         if ((unsigned)y >= lw->length) {
696                 if (bstate & BUTTON3_CLICKED)
697                         list_window_last(lw);
698                 else
699                         list_window_next_page(lw);
700                 return true;
701         }
703         return false;
705 #endif