]> pd.if.org Git - pd_readline/blob - funcs.c
3623220228d841bc52735d64f52f04e18f665323
[pd_readline] / funcs.c
1
2
3 /*  funcs.c                                          */ 
4 /*  Functions to handle cursor movement for          */ 
5 /*  pd_readline.                                     */      
6 /*  This code is released to the public domain.      */ 
7 /*  "Share and enjoy...."  ;)                        */  
8 /*  See the UNLICENSE file for details.              */ 
9
10
11 #include <string.h>   
12 #include <stdio.h> 
13 #include <stdlib.h> 
14 #include "pd_readline.h"  
15
16 /* Note - make the up and down funcs return a buffer.     */ 
17 /* A line from the history file can be put into the       */ 
18 /* array member of the buffer.                            */ 
19 /* Also test for the top and bottom of the history file.  */     
20
21 /*  Error function.  */ 
22 void error(void)
23
24    printf("Error \n");  
25 }       
26
27
28
29 /*  Display a buffer  */ 
30 void show(buf b)
31
32    printf("%s", b.array);                       
33 }       
34
35
36
37 /* Enter a char into a buffer and display the buffer array.  */ 
38 buf set(buf b, int i)
39
40   if ( (b.index < 80)  )  
41     { 
42            b.array[b.index] = i;        
43            b.index += 1 ;
44     } 
45   else
46     { 
47            memset(&b.array[0], 0, sizeof(b.array) ); 
48            b.array[0] = i;
49            b.index += 1 ;               
50     }           
51   
52   show(b); 
53   return b;     
54         
55
56
57
58 /*  Return a line from hist.  */ 
59 buf get(hist h)
60
61    buf b; 
62    memcpy(&b.array[0], h.array[h.curindex], 80); 
63    return b;            
64 }        
65
66
67
68 /* Move up in history list. */ 
69 hist up(hist h) 
70
71         
72   buf b;          
73         
74   if ( (h.curindex > 0) )  
75     { 
76                 h.curindex -= 1; 
77                 memset(&b.array[0], 0, sizeof(b.array) ); 
78                 memcpy(&b.array[0], h.array[h.curindex], 80);   
79                 show(b);                
80         return h;               
81     }    
82      
83   else error();  
84   
85
86
87
88 /* Move down in history list. */ 
89 hist down(hist h) 
90
91         
92   buf b;          
93         
94   if ( (h.curindex < 19) )  
95     { 
96                 h.curindex += 1; 
97                 memset(&b.array[0], 0, sizeof(b.array) ); 
98                 memcpy(&b.array[0], h.array[h.curindex], 80); 
99                 show(b);    
100         return h;                       
101     }  
102     
103   else error();     
104   
105
106
107
108 /* Move cursor to left. */ 
109 buf left(buf b) 
110 {
111
112
113
114
115
116 /* Move cursor to right. */ 
117 buf right(buf b) 
118
119
120
121
122
123
124 /* Delete a char. */ 
125 buf delch(buf b) 
126
127
128
129
130
131
132 /* Insert a char. */ 
133 buf insch(buf b) 
134
135
136
137
138
139
140 void enter(void) 
141
142         printf("Enter ");               
143 }       
144
145
146 /* Find if a number is in a given range. */ 
147 int range(int rstart, int rend, int i)
148 {
149    if ( (rstart <= i) && (i <= rend) ) return 1;
150    else return 0;       
151         
152 }       
153
154
155 /* Assign a type depending on the range that a  */ 
156 /* number is in.                                */ 
157 int type(int i)
158
159    int ret; 
160    
161         if ( range(0, 9, i) == 1 )       ret = 1; 
162    else if ( range(10, 10, i) == 1 )     ret = 2; 
163    else if ( range(27, 27, i) == 1 )     ret = 3; 
164    else if ( range(32, 126, i) == 1 )    ret = 4;          
165    else if ( range(127, 127, i) == 1 )   ret = 5;
166    else ret = 6;           
167     
168    return ret; 
169    
170 }     
171         
172
173 /*  Function for special key combinations  */ 
174 /*  (Ctrl, Alt, function keys.             */ 
175 void spec(hist h)
176
177         
178   int j = getch();  
179     
180        if ( ( j == 65 ) )  up(h); 
181   else if ( ( j == 66 ) )  down(h); 
182   else if ( ( j == 67 ) )  printf("Right "); 
183   else if ( ( j == 68 ) )  printf("Left "); 
184                 
185 }       
186
187
188
189
190