File: tooledit.c

package info (click to toggle)
mush 7.2.5unoff2-15
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 1,684 kB
  • ctags: 1,338
  • sloc: ansic: 21,932; sh: 795; csh: 87; makefile: 48
file content (317 lines) | stat: -rw-r--r-- 9,136 bytes parent folder | download | duplicates (6)
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
/* @(#)tooledit.c	(c) copyright	2/14/90 (Dan Heller) */

/*
 * intercept events in the compose window for auto-
 *	positioning and tilde command recognition.
 */
#include "mush.h"

static short dat_bentarrow[] = {
    0x007F, 0x007F, 0x007F, 0x0007, 0x0407, 0x0C07, 0x1C07, 0x3807, 
    0x7FFF, 0xFFFF, 0x7FFF, 0x3800, 0x1C00, 0x0C00, 0x0400, 0x0000
};
mpr_static(bent_arrow, 16, 16, 1, dat_bentarrow);
Cursor bentarrow;

extern void do_send(), do_edit();

/* Return the byte position in the textsw of the header specified */
Textsw_index
header_position(textsw, str)
Textsw textsw;
char *str;
{
    char buf[256];
    register char *p = buf, *p2;
    int contd_hdr = 0, add_newline = 0;
    Textsw_index pos = 0L, ret_pos = 0L;

    buf[0] = 0;
    for (;;) {
	/* get a line at a time from the textsw */
	(void) window_get(textsw, TEXTSW_CONTENTS, pos, buf, 256);
	if (p = index(buf, '\n'))
	    *p = 0;
	else
	    add_newline++;
	p = buf;
	skipspaces(0);
	if (!*p) /* newline alone -- end of headers */
	    break;
	pos += strlen(buf) + 1; /* advance position to next line */
	if (*p != ' ' && *p != '\t') {
	    contd_hdr = 0;
	    /* strcmp ignoring case */
	    for (p2 = str; *p && *p2 && lower(*p2) == lower(*p); ++p, ++p2)
		;
	    /* MATCH is true if p2 is at the end of str and *p is ':' */
	    if (*p2 || *p != ':') {
		if (!*p2 && isspace(*any(p, ": \t"))) {
		    /* Not a legal or continued header */
		    pos -= strlen(buf) + 1; /* go back to beginning of line */
		    break;
		}
		continue;
	    } else {
		contd_hdr = 1;
		ret_pos = pos - 1;
	    }
	} else if (!contd_hdr)
	    continue;
    }
    if (!ret_pos) {
	/* coudn't find the header -- add it */
	window_set(textsw, TEXTSW_INSERTION_POINT, pos, NULL);
	p = buf;
	if (add_newline)
	    *p++ = '\n', pos--;
	for (p2 = str; *p2; ++p2) {
	    if (p2 == str || p2[-1] == '-')
		*p++ = upper(*p2);
	    else
		*p++ = *p2;
	}
	*p++ = ':', *p++ = ' ', *p++ = '\n', *p = 0;
	textsw_insert(textsw, buf, strlen(buf));
	ret_pos = pos + strlen(buf) - 1;
    }
    return ret_pos;
}

/* position_flags indicates which header to go to when uses tilde commands */
static u_long position_flags;
static char *tilde_hdrs[] = {
#define POSITION_TO	ULBIT(0)
    "to",
#define POSITION_SUBJ	ULBIT(1)
    "subject",
#define POSITION_CC	ULBIT(2)
    "cc",
#define POSITION_BCC	ULBIT(3)
    "bcc",
#define POSITION_FCC	ULBIT(4)
    "fcc"
};
#define POSITION_ALL \
    ((POSITION_TO) | (POSITION_SUBJ) | (POSITION_CC) | (POSITION_BCC))
#define POSITION_END	ULBIT(5)
#define TOTAL_POSITIONS	6

/*
 * position_flags identifies which header is requested by the calling func.
 * use header_position to find the position of the header associated with
 * with the flags.
 */
static void
go_to_next_pos(textsw)
Textsw textsw;
{
    Textsw_index pos;
    int i = 0;

    while (i < TOTAL_POSITIONS && isoff(position_flags, ULBIT(i)))
	i++;
    if (i == TOTAL_POSITIONS)
	return;
    if (i < ArraySize(tilde_hdrs))
	pos = header_position(textsw, tilde_hdrs[i]);
    else
	pos = (Textsw_index)window_get(textsw, TEXTSW_LENGTH);
    turnoff(position_flags, ULBIT(i));
    if (!position_flags)
	/* restore old cursor */
	window_set(textsw,WIN_CURSOR, window_get(mfprint_sw, WIN_CURSOR), NULL);
    else
	window_set(textsw, WIN_CURSOR, bentarrow, NULL);
    window_set(textsw, TEXTSW_INSERTION_POINT, pos, NULL);
    textsw_normalize_view(textsw, (Textsw_index)0);
}

tilde_from_menu(item, value, event)
Panel_item item;
int value;
Event	*event;
{
    Textsw textsw = (Textsw)panel_get(panel_get(item, PANEL_PARENT_PANEL),
	PANEL_CLIENT_DATA);
    if (value == 0 || event_id(event) == MS_LEFT)
	position_flags = POSITION_ALL;
    else
	turnon(position_flags, ULBIT(value - 1));
    panel_set_value(item, 0);
    go_to_next_pos(textsw);
}

/*
 * This interpose function is here to parse for tilde escapes.
 * Note: this is a (currently) undocumented feature and is intended
 * as an accelerator for advanced users.  Supported tilde escapes
 * are: t,s,c,b,x,e and v.
 */
Notify_value
edit_msg_textwin(textsw, event, arg, type)
Textsw	textsw;
Event	*event;
Notify_arg	arg;
Notify_event_type	type;
{
    char buf[2];
    static char do_tilde;
    Textsw_index pos;

    if (do_tilde == 1 && event_is_ascii(event) &&
	    /* make sure we are going to catch this switch */
	    index("bschetv", event_id(event))) {
	textsw_erase(textsw,
	    (unsigned)window_get(textsw, TEXTSW_INSERTION_POINT)-1,
	    (unsigned)window_get(textsw, TEXTSW_INSERTION_POINT));
	switch (event_id(event)) {
	    case 'h':
		turnon(position_flags, POSITION_ALL);
	    when 't':
		turnon(position_flags, POSITION_TO);
	    when 's':
		turnon(position_flags, POSITION_SUBJ);
	    when 'c':
		turnon(position_flags, POSITION_CC);
	    when 'b':
		turnon(position_flags, POSITION_BCC);
	    when 'e' : case 'v' : {
		/* shouldn't use global -- hack for now */
		extern Panel_item edit_item;
		do_edit(edit_item);
		return NOTIFY_DONE;
	    }
	}
	do_tilde = 0;
	go_to_next_pos(textsw);
	return NOTIFY_DONE;
    }
    do_tilde = 0;
    /* check to see if this is a potential tilde escape */
    if (event_id(event) == *escape) {
	/* get previous character entered */
	pos = (Textsw_index)window_get(textsw, TEXTSW_INSERTION_POINT);
	if (pos > 0)
	    (void) window_get(textsw, TEXTSW_CONTENTS, pos-1, buf, 1);
	/* test to see if ~ came at the beginning of a line */
	if (pos < 1 || buf[0] == '\n')
	    do_tilde = 1;
    }
    /* check for auto-next-header .. e.g. when you hit CR on To: go to Subj:
     * special case backspace keys since textsw_start_of_display_line() has
     * a bug where it gets the line # wrong when backspacing.
     */
    if (position_flags != 0L && ID != CTRL('H') && ID != 127) {
	Notify_value val;
	if (ID == '\n' || ID == '\r') {
	    go_to_next_pos(textsw);
	    return NOTIFY_DONE; /* don't process event */
	}
	/* we're still processing this header -- continue to do so unless
	 * the event in question changes the line# of the insertion point.
	 * first get current position...
	 */
	pos = (Textsw_index)window_get(textsw, TEXTSW_INSERTION_POINT);
	/* now let the event be processed... */
	val = notify_next_event_func(textsw, event, arg, type);
	/* see if the line # for the new insertion point has changed. */
	if (textsw_start_of_display_line(textsw, pos) !=
	    textsw_start_of_display_line(textsw,
		(Textsw_index)window_get(textsw, TEXTSW_INSERTION_POINT))) {
	    /* the event (mouse button, ACTION_??), changed the line # */
	    position_flags = 0L; /* disable auto-next-header */
	    /* restore cursor */
	    window_set(textsw,
		WIN_CURSOR, window_get(mfprint_sw, WIN_CURSOR),
		NULL);
	}
	return val;
    }
    return notify_next_event_func(textsw, event, arg, type);
}

/*
 * start the compose textsw.  This is here because we need position_flags
 * and the tilde-bits to set the insertion point at the To: line if
 * do_position is true.
 */
void
start_textsw_edit(textsw, do_position)
Textsw textsw;
{
    extern char *hfile;
    char *file = (char *)window_get(textsw, TEXTSW_CLIENT_DATA);
    Textsw_index first, last, to_index;
    int		i;

    strdup(file, hfile);
#ifdef SUN_4_0 /* SunOS 4.0+ */
    window_set(textsw,
	TEXTSW_CLIENT_DATA,		file,
	TEXTSW_FILE_CONTENTS,		hfile,
	TEXTSW_READ_ONLY,		FALSE,
	TEXTSW_STORE_CHANGES_FILE,	FALSE,
	NULL);
#else /* SUN_4_0 */
    textsw_load_file(textsw, hfile, 1, 0, 0);
    window_set(textsw,
	TEXTSW_CLIENT_DATA,		file,
	TEXTSW_READ_ONLY,		FALSE,
	TEXTSW_STORE_CHANGES_FILE,	FALSE,
	NULL);
#endif /* SUN_4_0 */
    position_flags = 0L;
    if (do_position) {
	turnon(position_flags, POSITION_TO);
	if (do_set(set_options, "ask") || do_set(set_options, "asksub"))
	    turnon(position_flags, POSITION_SUBJ);
	if (do_set(set_options, "askcc"))
	    turnon(position_flags, POSITION_CC);
    }
    turnon(position_flags, POSITION_END);
    go_to_next_pos(textsw);
    (void) unlink(hfile);
    xfree(hfile), hfile = NULL;
}

/*ARGSUSED*/
void
do_edit(item, value, event)
Panel_item item;
int value;
register Event *event;
{
    int argc;
    char *file, **argv, *edit, cmd[MAXPATHLEN];
    Panel_item next;
    Panel panel = (Panel)panel_get(item, PANEL_PARENT_PANEL);
    Textsw textsw = (Textsw)panel_get(panel, PANEL_CLIENT_DATA);

    file = (char *)window_get(textsw, TEXTSW_CLIENT_DATA);
    if (textsw_store_file(textsw, file, 0, 0)) {
	error("Can't start editor");
	return;
    }
    if ((!(edit = do_set(set_options, "visual")) || !*edit) &&
	(!(edit = do_set(set_options, "editor")) || !*edit))
	edit = DEF_EDITOR;
    (void) sprintf(cmd, "%s %s", edit, file);
    argc = 0;
    if (!(argv = mk_argv(cmd, &argc, FALSE))) {
	unlink(file);
	return;
    }
    if (tool_edit_letter(textsw, argv) > -1) {
	/* skip first panel item */
	item = (Panel_item) panel_get(panel, PANEL_FIRST_ITEM);
	for (item = (Panel_item) panel_get(item, PANEL_NEXT_ITEM);
	     item; item = next) {
	     next = (Panel_item) panel_get(item, PANEL_NEXT_ITEM);
	     (void) panel_set(item, PANEL_SHOW_ITEM, FALSE, NULL);
	}
	position_flags = 0L;
	window_set(textsw,WIN_CURSOR, window_get(mfprint_sw,WIN_CURSOR), NULL);
    }
    free_vec(argv);
}