File: input.c

package info (click to toggle)
oleo 1.6-16
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,640 kB
  • ctags: 3,139
  • sloc: ansic: 39,221; yacc: 1,737; sh: 362; makefile: 88
file content (359 lines) | stat: -rw-r--r-- 9,056 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*	Copyright (C) 1993 Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this software; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "global.h"
#include "input.h"
#include "cmd.h"
#include "window.h"
#include "io-abstract.h"


/* In the functions below, we only ever deal with one input_view at a 
 * time.  By convention, call a pointer to it THIS_IV and use these
 * macros: 
 */
#define Redraw_needed 		(this_iv->redraw_needed)
#define Prompt_metric 		(this_iv->prompt_metric)
#define Input_metric 		(this_iv->input_metric)
#define Keymap_prompt 		(this_iv->keymap_prompt)
#define Expanded_keymap_prompt 	(this_iv->expanded_keymap_prompt)
#define Prompt_wid 		(this_iv->prompt_wid)
#define Must_fix_input 		(this_iv->must_fix_input)
#define Input_area 		(this_iv->input_area)
#define Prompt 			(this_iv->prompt)
#define Visibility_begin 	(this_iv->visibility_begin)
#define Visibility_end 		(this_iv->visibility_end)
#define Current_info		(this_iv->current_info)
#define Info_pos		(this_iv->info_pos)
#define Info_redraw_needed	(this_iv->info_redraw_needed)
#define Vis_wid			(this_iv->vis_wid)
#define Input_cursor 		(this_iv->input_cursor)



#define Max(A,B)  ((A) < (B) ? (B) : (A))
#define Min(A,B)  ((A) > (B) ? (B) : (A))


/* Decide if a keymap prompt should be displayed.  If so, 
 * return the prompt, else 0.
 */

static char * 
desired_keymap_prompt (struct input_view * this_iv)
{
  int map = cur_keymap;
  if (the_cmd_frame->cmd && (the_cmd_arg.style == &keyseq_style))
    map = the_cmd_arg.val.key.cmd.code;
  return ((map >= 0) ? map_prompts[map] : 0);
}




/* In WID columns, find the first char in STR that can
 * be displayed while still leaving POS visible.  METRIC
 * converts str->cols.
 */

static int
find_vis_begin (int * wid_used, int wid,
		char * str, int pos, text_measure metric)
{
  int used;			/* How many cols allocated? */
  if (str[pos])
    used = metric (&str[pos], 1);
  else
    /* POS could be just after the end of STR, in which case pretend
     * that the character there is a SPC. 
     */
    used = metric (" ", 1);

  while (pos && (used < wid))
    {
      int next;
      next = metric (&str[pos - 1], 1);
      if (used + next > wid)
	break;
      else
	{
	  --pos;
	  used += next;
	}
    }
  if (wid_used)
    *wid_used = used;
  return pos;
}



/* Find the last visible character...  -1 if none are vis. */

static int
find_vis_end (int * wid_used, int wid, char * str, int start, text_measure
	      metric) 
{
  int used = metric (&str[start], 1);	/* How many cols allocated? */
  int pos = start;
  int max = strlen (str) - 1;

  while ((pos < max) && (used < wid))
    {
      int next;
      next = metric (&str[pos + 1], 1);
      if (used + next > wid)
	break;
      else
	{
	  ++pos;
	  used += next;
	}
    }
  if (wid_used)
    *wid_used = used;
  return pos;
}

static void
set_vis_wid (struct input_view * this_iv)
{
  Vis_wid = (Input_metric (Input_area->buf + Visibility_begin,
			     Visibility_end - Visibility_begin)
	       + ((Input_cursor > Visibility_end)
		  ? Input_metric (" ", 1)
		  : 0));
}


/* This recomputes the input area parameters of an input_view, attempting to 
 * center the cursor. 
 */

static void
iv_reset_input (struct input_view * this_iv)
{
  char * km = desired_keymap_prompt (this_iv);
  if (km && (km == Keymap_prompt))
    return;
  Redraw_needed = FULL_REDRAW;
  Keymap_prompt = km;
  if (km)
    {
      /* A keymap prompt should be displayed. */
      Expanded_keymap_prompt = expand_prompt (Keymap_prompt);
      Prompt_wid = Prompt_metric (Expanded_keymap_prompt,
				  strlen(Expanded_keymap_prompt));
      Must_fix_input = 1;
      Visibility_begin = Visibility_end = 0;
      Input_area = 0;
      Vis_wid = Input_metric (" ", 1);
      Prompt = 0;
      Input_cursor = 0;
    }
  else
    {
      if (Expanded_keymap_prompt)
	{
	  ck_free (Expanded_keymap_prompt);
	  Expanded_keymap_prompt = 0;
	}
      if (!the_cmd_frame->cmd || the_cmd_arg.is_set || !the_cmd_arg.do_prompt)
	{
	  Prompt_wid = 0;
	  Must_fix_input = 0;
	  Input_area = 0;
	  Vis_wid = 0;
	  Prompt = 0;
	  Visibility_begin = Visibility_end = 0;
	  Input_cursor = 0;
	}
      else
	{
	  Prompt_wid = Prompt_metric (the_cmd_arg.expanded_prompt,
				       strlen (the_cmd_arg.expanded_prompt));
	  Must_fix_input = 0;
	  Input_area = &the_cmd_arg.text;
	  Prompt = the_cmd_arg.expanded_prompt;

	  if ((scr_cols - Prompt_wid) < Input_metric ("M", 1))
	    Prompt += find_vis_begin (&Prompt_wid,
				      scr_cols - Input_metric("M", 1),
				      Prompt, strlen(Prompt) - 1,
				      Prompt_metric);
	  
	  {
	    int wid_avail = scr_cols - Prompt_wid;
	    Visibility_begin =
	      find_vis_begin (0, wid_avail / 2, Input_area->buf,
			      the_cmd_arg.cursor, Input_metric);
	    Visibility_end =
	      find_vis_end (0, wid_avail,
			    Input_area->buf, Visibility_begin,
			    Input_metric); 
	  }

	  Input_cursor = the_cmd_arg.cursor;
	  set_vis_wid (this_iv);
	}
    }
}





/* This is called strategicly from the command loop and whenever
 * the input area is changed by beginning a complex command.
 * It may do nothing, though if the input area appeas to have changed
 * it will recompute its appearence.
 *
 * now it also updates the info_fields of the input view.
 */

void
iv_fix_input (struct input_view * this_iv)
{
  char * km_prompt = desired_keymap_prompt (this_iv);

  if (Keymap_prompt && (Keymap_prompt == km_prompt))

    Must_fix_input = 1;		/* Do nothing, keymap prompt has precedence */

  else if (Must_fix_input
	   || (Keymap_prompt != km_prompt)
	   || ((the_cmd_frame->cmd
		&& (the_cmd_arg.do_prompt && !the_cmd_arg.is_set))
	       ? ((Input_area != &the_cmd_arg.text)
		  || (Prompt != the_cmd_arg.expanded_prompt)
		  || (Input_cursor != the_cmd_arg.cursor))
	       : (Input_area || Prompt_wid)))
    iv_reset_input (this_iv);

  if (the_cmd_frame->cmd
      && ((the_cmd_arg.prompt_info != Current_info)
	  || (the_cmd_arg.info_line != Info_pos)))
    {
      Current_info = the_cmd_arg.prompt_info;
      Info_pos = the_cmd_arg.info_line;
      Info_redraw_needed = 1;
    }
  else if (Current_info
	   && (!the_cmd_frame->cmd || !the_cmd_arg.prompt_info))
    {
      Current_info = 0;
      io_repaint ();
    }
	   
}


/* Incremental updates:
 * For simple edits it is not necessary to redraw the entire line.
 * These schedule incremental updating.
 */


void
iv_move_cursor (struct input_view * this_iv)
{
  if (   Must_fix_input
      || (Visibility_begin > the_cmd_arg.cursor)
      || (   ((Visibility_end + 1) < the_cmd_arg.cursor)
	  || (((Visibility_end + 1) == the_cmd_arg.cursor)
	      && ((Vis_wid + Prompt_wid) < scr_cols))))

    Must_fix_input = 1;
  else if (Redraw_needed != FULL_REDRAW)
    {
      if (   (Redraw_needed == NO_REDRAW)
	  || ((Redraw_needed > Input_cursor)
	      && (Redraw_needed > the_cmd_arg.cursor)))
	{
	  Input_cursor = the_cmd_arg.cursor;
	  set_vis_wid (this_iv);
	}
      else
	{
	  Redraw_needed = Min (Input_cursor, the_cmd_arg.cursor);
	  set_vis_wid (this_iv);
	}
    }
}

void
iv_erase (struct input_view * this_iv, int len)
{
  if (Must_fix_input
      || (the_cmd_arg.cursor <= Visibility_begin))
    {
      Must_fix_input = 1;
      return;
    }
  else if (Redraw_needed != FULL_REDRAW)
    {
      if ((Redraw_needed == NO_REDRAW) || (Redraw_needed > the_cmd_arg.cursor))
	{
	  Redraw_needed = the_cmd_arg.cursor;
	  Visibility_end = find_vis_end (0, scr_cols - Prompt_wid,
					 Input_area->buf, Visibility_begin,
					 Input_metric);
	  Input_cursor = the_cmd_arg.cursor;
	}
      else
	Redraw_needed = the_cmd_arg.cursor;
      set_vis_wid (this_iv);
    }
}


void
iv_insert (struct input_view * this_iv, int len)
{
  if (!Must_fix_input)
    {
      int new_end;
      int cursor_past_end = (Input_cursor + len == strlen (Input_area->buf));
      
      new_end = find_vis_end (0, scr_cols - Prompt_wid,
			      Input_area->buf, Visibility_begin,
			      Input_metric);
      
      if ((new_end + cursor_past_end) < (Input_cursor + len))
	Must_fix_input = 1;
      else
	{
	  if (   (Redraw_needed != FULL_REDRAW)
	      && ((Redraw_needed == NO_REDRAW) || (Redraw_needed > Input_cursor)))
	    Redraw_needed = Input_cursor;
	  Input_cursor += len;
	  Visibility_end = new_end;
	  set_vis_wid (this_iv);
	}
    }
}
	   
void
iv_over (struct input_view * this_iv, int len)
{
  iv_insert (this_iv, len);
}