Add xterm-white.theme
[cmus.git] / cmdline.c
blob7fc654d02def85fd924e6a05960fec00add5b1e8
1 /*
2 * Copyright 2005 Timo Hirvonen
3 */
5 #include <cmdline.h>
7 #include <uchar.h>
8 #include <xmalloc.h>
10 struct cmdline cmdline;
12 void cmdline_init(void)
14 cmdline.blen = 0;
15 cmdline.clen = 0;
16 cmdline.bpos = 0;
17 cmdline.cpos = 0;
18 cmdline.size = 128;
19 cmdline.line = xnew(char, cmdline.size);
20 cmdline.line[0] = 0;
23 void cmdline_insert_ch(uchar ch)
25 int size;
27 size = u_char_size(ch);
28 if (cmdline.blen + size > cmdline.size) {
29 cmdline.size *= 2;
30 cmdline.line = xrenew(char, cmdline.line, cmdline.size);
32 memmove(cmdline.line + cmdline.bpos + size,
33 cmdline.line + cmdline.bpos,
34 cmdline.blen - cmdline.bpos + 1);
35 u_set_char_raw(cmdline.line, &cmdline.bpos, ch);
36 cmdline.cpos++;
37 cmdline.blen += size;
38 cmdline.clen++;
41 void cmdline_backspace(void)
43 int bpos, size;
45 if (cmdline.bpos == 0)
46 return;
48 bpos = cmdline.bpos;
49 u_prev_char_pos(cmdline.line, &bpos);
50 size = cmdline.bpos - bpos;
52 memmove(cmdline.line + bpos,
53 cmdline.line + cmdline.bpos,
54 cmdline.blen - cmdline.bpos + 1);
55 cmdline.bpos -= size;
56 cmdline.cpos--;
57 cmdline.blen -= size;
58 cmdline.clen--;
61 void cmdline_backspace_to_bol(void)
63 while (cmdline.bpos)
64 cmdline_backspace();
67 void cmdline_delete_ch(void)
69 uchar ch;
70 int size, bpos;
72 if (cmdline.bpos == cmdline.blen)
73 return;
74 bpos = cmdline.bpos;
75 u_get_char(cmdline.line, &bpos, &ch);
76 size = u_char_size(ch);
77 cmdline.blen -= size;
78 cmdline.clen--;
79 memmove(cmdline.line + cmdline.bpos,
80 cmdline.line + cmdline.bpos + size,
81 cmdline.blen - cmdline.bpos + 1);
84 void cmdline_set_text(const char *text)
86 int len = strlen(text);
88 if (len >= cmdline.size) {
89 while (len >= cmdline.size)
90 cmdline.size *= 2;
91 cmdline.line = xrenew(char, cmdline.line, cmdline.size);
93 memcpy(cmdline.line, text, len + 1);
94 cmdline.cpos = u_strlen(cmdline.line);
95 cmdline.bpos = len;
96 cmdline.clen = cmdline.cpos;
97 cmdline.blen = len;
100 void cmdline_clear(void)
102 cmdline.blen = 0;
103 cmdline.clen = 0;
104 cmdline.bpos = 0;
105 cmdline.cpos = 0;
106 cmdline.line[0] = 0;
109 void cmdline_clear_end(void)
111 cmdline.line[cmdline.bpos] = 0;
113 cmdline.clen = u_strlen(cmdline.line);
114 cmdline.blen = strlen(cmdline.line);
117 void cmdline_move_left(void)
119 if (cmdline.bpos > 0) {
120 cmdline.cpos--;
121 u_prev_char_pos(cmdline.line, &cmdline.bpos);
125 void cmdline_move_right(void)
127 if (cmdline.bpos < cmdline.blen) {
128 uchar ch;
130 u_get_char(cmdline.line, &cmdline.bpos, &ch);
131 cmdline.cpos++;
135 void cmdline_move_home(void)
137 cmdline.cpos = 0;
138 cmdline.bpos = 0;
141 void cmdline_move_end(void)
143 cmdline.cpos = cmdline.clen;
144 cmdline.bpos = cmdline.blen;