1 /*
2   Copyright (c) 2005-2013 uim Project https://github.com/uim/uim
3 
4   All rights reserved.
5 
6   Redistribution and use in source and binary forms, with or
7   without modification, are permitted provided that the
8   following conditions are met:
9 
10   1. Redistributions of source code must retain the above
11      copyright notice, this list of conditions and the
12      following disclaimer.
13   2. Redistributions in binary form must reproduce the above
14      copyright notice, this list of conditions and the
15      following disclaimer in the documentation and/or other
16      materials provided with the distribution.
17   3. Neither the name of authors nor the names of its
18      contributors may be used to endorse or promote products
19      derived from this software without specific prior written
20      permission.
21 
22   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 
37 #include "preedit.h"
38 
39 preedit *
create_preedit()40 create_preedit()
41 {
42   preedit *pe;
43   pe = uim_malloc(sizeof(preedit));
44   pe->valid = 0;
45   pe->head = pe->tail = NULL;
46 
47   return pe;
48 }
49 
50 void
add_preedit(preedit * pe,int attr,const char * str)51 add_preedit(preedit *pe, int attr, const char *str)
52 {
53   preedit_buffer *pb;
54 
55   pe->valid = 1;
56 
57   pb = uim_malloc(sizeof(preedit_buffer));
58 
59   if (pe->head == NULL) {
60 	pe->head = pb;
61 	pe->tail = pb;
62   } else {
63 	pe->tail->next = pb;
64 	pe->tail = pb;
65   }
66 
67   if (strlen(str) > 0) {
68 	pb->str = uim_strdup(str);
69 	pe->length += strlen(str);
70   } else {
71 	pb->str = NULL;
72   }
73 
74   pb->attr = attr;
75   pb->next = NULL;
76 }
77 
78 
79 void
clear_preedit(preedit * pe)80 clear_preedit(preedit *pe)
81 {
82   preedit_buffer *p, *ptmp;
83 
84   pe->valid = 0;
85 
86   p = pe->head;
87 
88   while (p) {
89 	ptmp = p;
90 	p = p->next;
91 	free(ptmp->str);
92 	free(ptmp);
93   }
94 
95   pe->head = pe->tail = NULL;
96   pe->length = 0;
97 }
98 
99 
100 
101 int
show_preedit(preedit * pe)102 show_preedit(preedit *pe)
103 {
104   preedit_buffer *p;
105 
106   p = pe->head;
107 
108   if (p == NULL || pe->length == 0) {
109 	a_printf(" ( e ) ");
110 	return 0;
111   }
112 
113   a_printf("( p ");
114 
115   while (p) {
116 	a_printf(" ( ");
117 	if (p->attr & UPreeditAttr_Reverse)
118 	  a_putchar('r');
119 	if (p->attr & UPreeditAttr_UnderLine)
120 	  a_putchar('u');
121 	if (p->attr & UPreeditAttr_Cursor)
122 	  a_putchar('c');
123 	if (p->attr & UPreeditAttr_Separator)
124 	  a_putchar('s');
125 
126 	a_putchar('t');
127 	a_putchar(' ');
128 
129 	output_with_escape(p->str);
130 
131 	a_printf(" ) ");
132 
133 	p = p->next;
134   }
135   a_printf(" ) ");
136 
137   return 1;
138 }
139 
140 
141 
142 int
show_preedit_force(preedit * pe)143 show_preedit_force(preedit *pe)
144 {
145   if (! show_preedit(pe))
146 	a_printf("( p (t \"\") ) ");
147 
148   return 1;
149 }
150