995
|
1 /*
|
|
2 * Buffer management.
|
|
3 * Some of the functions are internal,
|
|
4 * and some are actually attached to user
|
|
5 * keys. Like everyone else, they set hints
|
|
6 * for the display system.
|
|
7 */
|
|
8 #include <stdio.h>
|
|
9 #include "ueed.h"
|
|
10
|
|
11 #ifndef OS9
|
|
12 itoa(buf, width, num)
|
|
13 register char buf[];
|
|
14 register int width;
|
|
15 register int num;
|
|
16 {
|
|
17 buf[width] = 0; /* End of string. */
|
|
18 while (num >= 10) { /* Conditional digits. */
|
|
19 buf[--width] = (num%10) + '0';
|
|
20 num /= 10;
|
|
21 }
|
|
22 buf[--width] = num + '0'; /* Always 1 digit. */
|
|
23 while (width != 0) /* Pad with blanks. */
|
|
24 buf[--width] = ' ';
|
|
25 }
|
|
26
|
|
27 /*
|
|
28 * The argument "text" points to
|
|
29 * a string. Append this line to the
|
|
30 * buffer list buffer. Handcraft the EOL
|
|
31 * on the end. Return TRUE if it worked and
|
|
32 * FALSE if you ran out of room.
|
|
33 */
|
|
34 addline(text)
|
|
35 char *text;
|
|
36 {
|
|
37 register LINE *lp;
|
|
38 register int i;
|
|
39 register int ntext;
|
|
40
|
|
41 ntext = strlen(text);
|
|
42 if ((lp=lalloc(ntext)) == NULL)
|
|
43 return (FALSE);
|
|
44 for (i=0; i<ntext; ++i)
|
|
45 lputc(lp, i, text[i]);
|
|
46 blistp->b_linep->l_bp->l_fp = lp; /* Hook onto the end */
|
|
47 lp->l_bp = blistp->b_linep->l_bp;
|
|
48 blistp->b_linep->l_bp = lp;
|
|
49 lp->l_fp = blistp->b_linep;
|
|
50 if (blistp->b_dotp == blistp->b_linep) /* If "." is at the end */
|
|
51 blistp->b_dotp = lp; /* move it to new line */
|
|
52 return (TRUE);
|
|
53 }
|
|
54 #endif
|
|
55
|
|
56 /*
|
|
57 * Look through the list of
|
|
58 * buffers. Return TRUE if there
|
|
59 * are any changed buffers. Buffers
|
|
60 * that hold magic internal stuff are
|
|
61 * not considered; who cares if the
|
|
62 * list of buffer names is hacked.
|
|
63 * Return FALSE if no buffers
|
|
64 * have been changed.
|
|
65 */
|
|
66 anycb()
|
|
67 {
|
|
68 register BUFFER *bp;
|
|
69
|
|
70 bp = bheadp;
|
|
71 while (bp != NULL) {
|
|
72 if ((bp->b_flag&BFTEMP)==0 && (bp->b_flag&BFCHG)!=0)
|
|
73 return (TRUE);
|
|
74 bp = bp->b_bufp;
|
|
75 }
|
|
76 return (FALSE);
|
|
77 }
|
|
78
|
|
79 /*
|
|
80 * Find a buffer, by name. Return a pointer
|
|
81 * to the BUFFER structure associated with it. If
|
|
82 * the named buffer is found, but is a TEMP buffer (like
|
|
83 * the buffer list) conplain. If the buffer is not found
|
|
84 * and the "cflag" is TRUE, create it. The "bflag" is
|
|
85 * the settings for the flags in in buffer.
|
|
86 */
|
|
87 BUFFER *bfind(bname, cflag, bflag)
|
|
88 register char *bname;
|
|
89 {
|
|
90 register BUFFER *bp;
|
|
91 register LINE *lp;
|
|
92
|
|
93 bp = bheadp;
|
|
94 while (bp != NULL) {
|
|
95 if (strcmp(bname, bp->b_bname) == 0) {
|
|
96 if ((bp->b_flag&BFTEMP) != 0) {
|
|
97 mlwrite("Cannot select builtin buffer");
|
|
98 return (NULL);
|
|
99 }
|
|
100 return (bp);
|
|
101 }
|
|
102 bp = bp->b_bufp;
|
|
103 }
|
|
104 if (cflag != FALSE) {
|
|
105 if ((bp=(BUFFER *)malloc(sizeof(BUFFER))) == NULL)
|
|
106 return (NULL);
|
|
107 if ((lp=lalloc(0)) == NULL) {
|
|
108 free((char *) bp);
|
|
109 return (NULL);
|
|
110 }
|
|
111 bp->b_bufp = bheadp;
|
|
112 bheadp = bp;
|
|
113 bp->b_dotp = lp;
|
|
114 bp->b_doto =
|
|
115 bp->b_markp =
|
|
116 bp->b_marko =
|
|
117 bp->b_nwnd = 0;
|
|
118 bp->b_linep = lp;
|
|
119 bp->b_flag = bflag;
|
|
120 strcpy(bp->b_fname, "");
|
|
121 strcpy(bp->b_bname, bname);
|
|
122 lp->l_fp = lp;
|
|
123 lp->l_bp = lp;
|
|
124 }
|
|
125 return (bp);
|
|
126 }
|
|
127
|
|
128 /*
|
|
129 * This routine blows away all of the text
|
|
130 * in a buffer. If the buffer is marked as changed
|
|
131 * then we ask if it is ok to blow it away; this is
|
|
132 * to save the user the grief of losing text. The
|
|
133 * window chain is nearly always wrong if this gets
|
|
134 * called; the caller must arrange for the updates
|
|
135 * that are required. Return TRUE if everything
|
|
136 * looks good.
|
|
137 */
|
|
138 bclear(bp)
|
|
139 register BUFFER *bp;
|
|
140 {
|
|
141 register LINE *lp;
|
|
142 register int s;
|
|
143
|
|
144 if ((bp->b_flag&BFTEMP) == 0 /* Not scratch buffer. */
|
|
145 && (bp->b_flag&BFCHG) != 0 /* Something changed */
|
|
146 && (s=mlyesno("Discard changes")) != TRUE)
|
|
147 return (s);
|
|
148 bp->b_flag &= ~BFCHG; /* Not changed */
|
|
149 while ((lp=lforw(bp->b_linep)) != bp->b_linep)
|
|
150 lfree(lp);
|
|
151 bp->b_dotp = bp->b_linep; /* Fix "." */
|
|
152 bp->b_doto =
|
|
153 bp->b_markp = /* Invalidate "mark" */
|
|
154 bp->b_marko = 0;
|
|
155 return (TRUE);
|
|
156 }
|
|
157
|