File: editline.c

package info (click to toggle)
saoimage 1.19-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 3,256 kB
  • ctags: 3,610
  • sloc: ansic: 36,050; makefile: 215; sh: 11
file content (182 lines) | stat: -rw-r--r-- 5,521 bytes parent folder | download | duplicates (5)
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
#ifndef lint
static char SccsId[] = "%W%  %G%";
#endif

/* Module:	editline.c (Editor Line)
 * Purpose:	Manipulate the popup editor character string
 * Subroutine:	insert_chars()			returns: void
 * Subroutine:	delete_chars_backward()		returns: void
 * Subroutine:	delete_chars_forward()		returns: void
 * Subroutine:	next_word_end()			returns: int
 * Subroutine:	last_word_end()			returns: int
 * Copyright:	1989 Smithsonian Astrophysical Observatory
 *		You may do anything you like with this file except remove
 *		this copyright.  The Smithsonian Astrophysical Observatory
 *		makes no representations about the suitability of this
 *		software for any purpose.  It is provided "as is" without
 *		express or implied warranty.
 * Modified:	{0} Michael VanHilst	initial version		  4 July 1989
 *		{n} <who> -- <does what> -- <when>
 */

#include <stdio.h>
#include <X11/Xlib.h>
#include "hfiles/edit.h"

/*
 * Subroutine:	insert_chars
 * Purpose:	Insert new characters into the edit line
 * Returns:	Number of characters inserted
 */
void insert_chars ( edit, str, len )
     EditStruct *edit;
     char *str;
     int len;
{
  int i, j;
  void redraw_after_insert();

  /* check for end of buffer */
  if( (edit->char_cnt + len) > edit->max_char_cnt ) {
    (void)fprintf(stderr, "Line buffer full!\n");
    if( edit->char_cnt < edit->max_char_cnt ) {
      len = edit->max_char_cnt - edit->char_cnt;
    } else
      return;
  }
  /* move later characters forward */
  for( i = edit->char_cnt, j = i + len;
       i >= edit->active_position; i--, j-- ) {
    edit->charsz[j] = edit->charsz[i];
    edit->string[j] = edit->string[i];
  }
  /* insert new characters */
  for( j = 0, ++i; j < len; j++, i++ ) {
    edit->string[i] = str[j];
    edit->charsz[i] = XTextWidth(edit->fontstruct, &str[j], 1);
    edit->pixlen[i+1] = edit->pixlen[i] + edit->charsz[i];
  }
  edit->char_cnt += len;
  edit->active_position += len;
  /* update running lengths */
  for( i--; i <= edit->char_cnt; i++ )
    edit->pixlen[i+1] = edit->pixlen[i] + edit->charsz[i];
  redraw_after_insert(edit, len);
}

/*
 * Subroutine:	delete_chars_backward
 * Purpose:	Delete characters before the active position
 */
void delete_chars_backward ( edit, len )
     EditStruct *edit;
     int len;
{
  int i, j;
  void redraw_after_delete();

  /* delete before beginning of line? */
  if( len > edit->active_position ) {
    (void)fprintf(stderr, "Beginning of line!\n");
    if( edit->active_position > 0 )
      len = edit->active_position;
    else
      return;
  }
  /* move later characters backward */
  for( i = edit->active_position, j = i - len;
       i <= edit->char_cnt; i++, j++ ) {
    edit->charsz[j] = edit->charsz[i];
    edit->string[j] = edit->string[i];
  }
  edit->char_cnt -= len;
  edit->active_position -= len;
  /* update running lengths */
  for( i=edit->active_position; i<=edit->char_cnt; i++ )
    edit->pixlen[i+1] = edit->pixlen[i] + edit->charsz[i];
  redraw_after_delete(edit);
}

/*
 * Subroutine:	delete_chars_forward
 * Purpose:	Delete characters from the active position forward
 */
void delete_chars_forward ( edit, len )
     EditStruct *edit;
     int len;
{
  int i, j;
  void redraw_after_delete();

  /* delete beyond end of line? */
  if( (edit->active_position + len) > edit->char_cnt ) {
    if( edit->active_position < edit->char_cnt )
      len = edit->char_cnt - edit->active_position;
    else
      return;
  }
  /* move later characters backward */
  for( j = edit->active_position, i = j + len;
       i <= edit->char_cnt; i++, j++ ) {
    edit->charsz[j] = edit->charsz[i];
    edit->string[j] = edit->string[i];
  }
  edit->char_cnt -= len;
  /* update running lengths */
  for( i=edit->active_position; i<=edit->char_cnt; i++ )
    edit->pixlen[i+1] = edit->pixlen[i] + edit->charsz[i];    
  redraw_after_delete(edit);
}

/*
 * Subroutine:	next_word_end
 * Returns:	The distance from the active position to the next space
 *		following a character (or the end of the line)
 */
int next_word_end ( edit )
     EditStruct *edit;
{
  int i;
  for( i=edit->active_position;
       (i < edit->char_cnt) && ((edit->string[i] == ' ') ||
				  (edit->string[i] == '/') ||
				  (edit->string[i] == ',') ||
				  (edit->string[i] == '.') ||
				  (edit->string[i] == ';') ||
				  (edit->string[i] == ':')); i++ );
  /* string[char_cnt]==' ', so loop will stop there, if nowhere else */
  if( i < edit->char_cnt ) {
    while( (edit->string[i] != ' ') && (edit->string[i] != '/') &&
	   (edit->string[i] != ',') && (edit->string[i] != '.') &&
	   (edit->string[i] != ';') && (edit->string[i] != ':') )
      i++;
  }
  return( i - edit->active_position );
}

/*
 * Subroutine:	last_word_start
 * Returns:	The distance from the active position to the first character
 *		following a space found in a backward search (or the start of
 *		the line)
 */
int last_word_start ( edit )
     EditStruct *edit;
{
  int i;
  if( edit->active_position == 0 )
    return( 0 );
  for( i = edit->active_position - 1;
       (i >= 0) && ((edit->string[i] == ' ') || (edit->string[i] == '/') ||
		    (edit->string[i] == ',') || (edit->string[i] == '.') ||
		    (edit->string[i] == ';') || (edit->string[i] == ':'));
      i-- );
  if( i >= 0 ) {
    while( (i >= 0) &&
	   ((edit->string[i] != ' ') && (edit->string[i] != '/') &&
	    (edit->string[i] != ',') && (edit->string[i] != '.') &&
	    (edit->string[i] != ';') && (edit->string[i] != ':')) )
      i--;
  }
  return( (i+1) - edit->active_position );
}