wavpack: Pass correct sample count to format_samples()
[cmus.git] / cmdline.c
blobe942b446dcf6c62add0736387b75fdf62039761e
1 /*
2 * Copyright 2005 Timo Hirvonen
3 */
5 #include "cmdline.h"
6 #include "uchar.h"
7 #include "xmalloc.h"
9 struct cmdline cmdline;
11 void cmdline_init(void)
13 cmdline.blen = 0;
14 cmdline.clen = 0;
15 cmdline.bpos = 0;
16 cmdline.cpos = 0;
17 cmdline.size = 128;
18 cmdline.line = xnew(char, cmdline.size);
19 cmdline.line[0] = 0;
22 void cmdline_insert_ch(uchar ch)
24 int size;
26 size = u_char_size(ch);
27 if (cmdline.blen + size > cmdline.size) {
28 cmdline.size *= 2;
29 cmdline.line = xrenew(char, cmdline.line, cmdline.size);
31 memmove(cmdline.line + cmdline.bpos + size,
32 cmdline.line + cmdline.bpos,
33 cmdline.blen - cmdline.bpos + 1);
34 u_set_char_raw(cmdline.line, &cmdline.bpos, ch);
35 cmdline.cpos++;
36 cmdline.blen += size;
37 cmdline.clen++;
40 void cmdline_backspace(void)
42 int bpos, size;
44 if (cmdline.bpos == 0)
45 return;
47 bpos = cmdline.bpos;
48 u_prev_char_pos(cmdline.line, &bpos);
49 size = cmdline.bpos - bpos;
51 memmove(cmdline.line + bpos,
52 cmdline.line + cmdline.bpos,
53 cmdline.blen - cmdline.bpos + 1);
54 cmdline.bpos -= size;
55 cmdline.cpos--;
56 cmdline.blen -= size;
57 cmdline.clen--;
60 void cmdline_backspace_to_bol(void)
62 while (cmdline.bpos)
63 cmdline_backspace();
66 void cmdline_delete_ch(void)
68 uchar ch;
69 int size, bpos;
71 if (cmdline.bpos == cmdline.blen)
72 return;
73 bpos = cmdline.bpos;
74 u_get_char(cmdline.line, &bpos, &ch);
75 size = u_char_size(ch);
76 cmdline.blen -= size;
77 cmdline.clen--;
78 memmove(cmdline.line + cmdline.bpos,
79 cmdline.line + cmdline.bpos + size,
80 cmdline.blen - cmdline.bpos + 1);
83 void cmdline_set_text(const char *text)
85 int len = strlen(text);
87 if (len >= cmdline.size) {
88 while (len >= cmdline.size)
89 cmdline.size *= 2;
90 cmdline.line = xrenew(char, cmdline.line, cmdline.size);
92 memcpy(cmdline.line, text, len + 1);
93 cmdline.cpos = u_strlen(cmdline.line);
94 cmdline.bpos = len;
95 cmdline.clen = cmdline.cpos;
96 cmdline.blen = len;
99 void cmdline_clear(void)
101 cmdline.blen = 0;
102 cmdline.clen = 0;
103 cmdline.bpos = 0;
104 cmdline.cpos = 0;
105 cmdline.line[0] = 0;
108 void cmdline_clear_end(void)
110 cmdline.line[cmdline.bpos] = 0;
112 cmdline.clen = u_strlen(cmdline.line);
113 cmdline.blen = strlen(cmdline.line);
116 void cmdline_move_left(void)
118 if (cmdline.bpos > 0) {
119 cmdline.cpos--;
120 u_prev_char_pos(cmdline.line, &cmdline.bpos);
124 void cmdline_move_right(void)
126 if (cmdline.bpos < cmdline.blen) {
127 uchar ch;
129 u_get_char(cmdline.line, &cmdline.bpos, &ch);
130 cmdline.cpos++;
134 void cmdline_move_home(void)
136 cmdline.cpos = 0;
137 cmdline.bpos = 0;
140 void cmdline_move_end(void)
142 cmdline.cpos = cmdline.clen;
143 cmdline.bpos = cmdline.blen;