370648f64a272e3fa0f7e9964010ddbbfabac554
[oweals/cde.git] / cde / lib / DtTerm / TermPrim / TermPrimFunction.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 #ifndef lint
24 #ifdef  VERBOSE_REV_INFO
25 static char rcs_id[] = "$XConsortium: TermPrimFunction.c /main/1 1996/04/21 19:17:27 drk $";
26 #endif  /* VERBOSE_REV_INFO */
27 #endif  /* lint */
28
29 /*                                                                      *
30  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
31  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
32  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
33  * (c) Copyright 1993, 1994 Novell, Inc.                                *
34  */
35
36 #include "TermHeader.h"
37 #include "TermPrimDebug.h"
38 #include "TermPrimP.h"
39 #include "TermPrimData.h"
40 #include "TermPrimFunction.h"
41 #include "TermPrimSetPty.h"
42
43 /******************************************************************************
44  *
45  *   ####   #####                   #       ######
46  *  #    #  #    #                  #       #
47  *  #       #    #   ###            #       #####    ###
48  *  #       #####    ###            #       #        ###
49  *  #    #  #   #     #             #       #         #
50  *   ####   #    #   #              ######  #        #
51  * 
52  * 
53  *  #####    ####                    #####    ##    #####
54  *  #    #  #                          #     #  #   #    #
55  *  #####    ####    ###               #    #    #  #####
56  *  #    #       #   ###               #    ######  #    #
57  *  #    #  #    #    #                #    #    #  #    #
58  *  #####    ####    #                 #    #    #  #####
59  */
60
61 void
62 _DtTermPrimFuncLF(Widget w, int count, FunctionSource functionSource)
63 {
64     DtTermPrimitiveWidget          tw = (DtTermPrimitiveWidget)w;
65     struct termData    *tpd = tw->term.tpd;
66     
67     /*
68     ** move the insert point...
69     */
70     if (++tpd->cursorRow >= tw->term.rows)
71     {
72         /*
73         ** scroll one line...
74         */
75         (void) _DtTermPrimScrollText(w, 1);
76         tpd->cursorRow = tw->term.rows - 1;
77     }
78     (void) _DtTermPrimFillScreenGap(w);
79     return;
80 }
81
82 void
83 _DtTermPrimFuncBackspace(Widget w, int count, FunctionSource functionSource)
84 {
85     DtTermPrimitiveWidget          tw = (DtTermPrimitiveWidget)w;
86     struct termData    *tpd = tw->term.tpd;
87
88     if (tpd->cursorColumn > 0) {
89         (void) tpd->cursorColumn--;
90     }
91     return;
92 }
93
94 void
95 _DtTermPrimFuncCR(Widget w, int count, FunctionSource functionSource)
96 {
97     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
98     struct termData *tpd = tw->term.tpd;
99
100     tpd->cursorColumn = tpd->leftMargin;
101     return;
102 }
103
104 \f
105 /*** CURSOR MOTION ************************************************************
106  * 
107  *   ####   #    #  #####    ####    ####   #####
108  *  #    #  #    #  #    #  #       #    #  #    #
109  *  #       #    #  #    #   ####   #    #  #    #
110  *  #       #    #  #####        #  #    #  #####
111  *  #    #  #    #  #   #   #    #  #    #  #   #
112  *   ####    ####   #    #   ####    ####   #    #
113  * 
114  * 
115  *  #    #   ####    #####     #     ####   #    #
116  *  ##  ##  #    #     #       #    #    #  ##   #
117  *  # ## #  #    #     #       #    #    #  # #  #
118  *  #    #  #    #     #       #    #    #  #  # #
119  *  #    #  #    #     #       #    #    #  #   ##
120  *  #    #   ####      #       #     ####   #    #
121  */
122
123 void
124 _DtTermPrimFuncNextLine(Widget w, int count, FunctionSource functionSource)
125 {
126     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
127     struct termData *tpd = tw->term.tpd;
128
129     /* move down...
130      */
131     (void) _DtTermPrimCursorOff(w);
132     while (count-- > 0) {
133         if (tpd->cursorRow < tw->term.rows - 1) {
134             (void) tpd->cursorRow++;
135         } else {
136             tpd->cursorRow = 0;
137         }
138     }
139     return;
140 }
141
142
143 void
144 _DtTermPrimFuncPreviousLine(Widget w, int count, FunctionSource functionSource)
145 {
146     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
147     struct termData *tpd = tw->term.tpd;
148
149     /* move up...
150      */
151     (void) _DtTermPrimCursorOff(w);
152     while (count-- > 0) {
153         if (tpd->cursorRow > 0) {
154             tpd->cursorRow--;
155         } else {
156             tpd->cursorRow = tw->term.rows - 1;
157         }
158     }
159     return;
160 }
161
162 void
163 _DtTermPrimFuncBackwardCharacter(Widget w, int count, FunctionSource functionSource)
164 {
165     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
166     struct termData *tpd = tw->term.tpd;
167
168     /* move left...
169      */
170     (void) _DtTermPrimCursorOff(w);
171     while (count-- > 0) {
172         if (tpd->cursorColumn > 0) {
173             (void) tpd->cursorColumn--;
174         } else {
175             /* past left edge -- wrap up... */
176             tpd->cursorColumn = tw->term.columns - 1;
177             if (tpd->cursorRow > 0) {
178                 (void) tpd->cursorRow--;
179             } else {
180                 /* past first line -- wrap to bottom of screen... */
181                 tpd->cursorRow = tw->term.rows - 1;
182             }
183         }
184     }
185     return;
186 }
187
188 void
189 _DtTermPrimFuncForwardCharacter(Widget w, int count, FunctionSource functionSource)
190 {
191     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
192     struct termData *tpd = tw->term.tpd;
193
194     /* move right...
195      */
196     (void) _DtTermPrimCursorOff(w);
197     while (count-- > 0) {
198         if (tpd->cursorColumn < tw->term.columns - 1) {
199             (void) tpd->cursorColumn++;
200         } else {
201             /* past right edge -- wrap... */
202             tpd->cursorColumn = 0;
203             if (tpd->cursorRow < tw->term.rows - 1) {
204                 (void) tpd->cursorRow++;
205             } else {
206                 /* past last line -- wrap to top of screen... */
207                 tpd->cursorRow = 0;
208             }
209         }
210     }
211     return;
212 }
213
214
215 void
216 _DtTermPrimFuncReturn(Widget w, int count, FunctionSource functionSource)
217 {
218     Debug('i', fprintf(stderr,
219             ">>_DtTermPrimFuncReturn: not yet implemented\n"));
220 }
221
222
223 void
224 _DtTermPrimFuncTab(Widget w, int count, FunctionSource functionSource)
225 {
226     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
227     struct termData *tpd = tw->term.tpd;
228     TermBuffer tBuffer = tpd->termBuffer;
229     short nextTab;
230
231     (void) _DtTermPrimCursorOff(w);
232
233     if (tpd->cursorColumn < tpd->leftMargin) {
234         /* move to left margin on current line... */
235         tpd->cursorColumn = tpd->leftMargin;
236     } else {
237         /* Move to the next tab stop.  Note that this cursor motion is
238          * similar to the right arrow cursor motion in that
239          * it doesn't set the line length until a character is entered.
240          */
241         /* DKS: this code currently enforces tabs at every 8 character
242          * positions.  It needs to be worked on to support the margins/tabs
243          * buffer...
244          */
245         nextTab = _DtTermPrimBufferGetNextTab(tBuffer, tpd->cursorColumn);
246
247         /* check to see if we are past the end of the line... */
248         if ((nextTab <= 0) || (nextTab >= tpd->rightMargin)) {
249             /* wrap to next line...
250              */
251             tpd->cursorColumn = tpd->leftMargin;
252
253             /* check to see if we scrolled off the bottom of the screen... */
254             if (++tpd->cursorRow >= tw->term.rows) {
255                 /* scroll one line...
256                  */
257                 (void)_DtTermPrimScrollText(w, 1);
258                 tpd->cursorRow = tw->term.rows - 1;
259             }
260             /* fill any screen gap... */
261             if (tpd->topRow + tpd->cursorRow >= tpd->lastUsedRow) {
262                 (void) _DtTermPrimFillScreenGap(w);
263             }
264         } else {
265             tpd->cursorColumn = nextTab;
266         }
267     }
268     return;
269 }
270
271
272 \f
273 /*** TAB FUNCTIONS ************************************************************
274  * 
275  *   #####    ##    #####
276  *     #     #  #   #    #
277  *     #    #    #  #####
278  *     #    ######  #    #
279  *     #    #    #  #    #
280  *     #    #    #  #####
281  * 
282  * 
283  *  ######  #    #  #    #   ####    #####     #     ####   #    #   ####
284  *  #       #    #  ##   #  #    #     #       #    #    #  ##   #  #
285  *  #####   #    #  # #  #  #          #       #    #    #  # #  #   ####
286  *  #       #    #  #  # #  #          #       #    #    #  #  # #       #
287  *  #       #    #  #   ##  #    #     #       #    #    #  #   ##  #    #
288  *  #        ####   #    #   ####      #       #     ####   #    #   ####
289  */
290
291 void
292 _DtTermPrimFuncTabSet(Widget w, int count, FunctionSource functionSource)
293 {
294     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
295     struct termData *tpd = tw->term.tpd;
296
297     (void) _DtTermPrimBufferSetTab(tpd->termBuffer, tpd->cursorColumn);
298 }
299
300 void
301 _DtTermPrimFuncTabClear(Widget w, int count, FunctionSource functionSource)
302 {
303     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
304     struct termData *tpd = tw->term.tpd;
305
306     (void) _DtTermPrimBufferClearTab(tpd->termBuffer, tpd->cursorColumn);
307 }
308
309 void
310 _DtTermPrimFuncTabClearAll(Widget w, int count, FunctionSource functionSource)
311 {
312     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
313     struct termData *tpd = tw->term.tpd;
314
315     (void) _DtTermPrimBufferClearAllTabs(tpd->termBuffer);
316 }
317
318 \f
319 /*** REDRAW DISPLAY ***********************************************************
320  * 
321  *  #    #    ##    #####    ####      #    #    #
322  *  ##  ##   #  #   #    #  #    #     #    ##   #
323  *  # ## #  #    #  #    #  #          #    # #  #
324  *  #    #  ######  #####   #  ###     #    #  # #
325  *  #    #  #    #  #   #   #    #     #    #   ##
326  *  #    #  #    #  #    #   ####      #    #    #
327  * 
328  * 
329  *  ######  #    #  #    #   ####    #####     #     ####   #    #   ####
330  *  #       #    #  ##   #  #    #     #       #    #    #  ##   #  #
331  *  #####   #    #  # #  #  #          #       #    #    #  # #  #   ####
332  *  #       #    #  #  # #  #          #       #    #    #  #  # #       #
333  *  #       #    #  #   ##  #    #     #       #    #    #  #   ##  #    #
334  *  #        ####   #    #   ####      #       #     ####   #    #   ####
335  */
336 void
337 _DtTermPrimFuncMarginSetLeft(Widget w, int count, FunctionSource functionSource)
338 {
339     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
340     struct termData *tpd = tw->term.tpd;
341
342     if (tpd->cursorColumn < tpd->rightMargin) {
343         tpd->leftMargin = tpd->cursorColumn;
344     } else {
345         _DtTermPrimBell(w);
346     }
347     return;
348 }
349
350 void
351 _DtTermPrimFuncMarginSetRight(Widget w, int count, FunctionSource functionSource)
352 {
353     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
354     struct termData *tpd = tw->term.tpd;
355
356     if (tpd->cursorColumn > tpd->leftMargin) {
357         tpd->rightMargin = tpd->cursorColumn;
358     } else {
359         _DtTermPrimBell(w);
360     }
361     return;
362 }
363
364 void
365 _DtTermPrimFuncMarginClear(Widget w, int count, FunctionSource functionSource)
366 {
367     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
368     struct termData *tpd = tw->term.tpd;
369
370     tpd->leftMargin = 0;
371     tpd->rightMargin = tw->term.columns - 1;
372     return;
373 }
374
375 \f
376 /*** REDRAW DISPLAY ***********************************************************
377  * 
378  *  #####   ######  #####   #####     ##    #    #
379  *  #    #  #       #    #  #    #   #  #   #    #
380  *  #    #  #####   #    #  #    #  #    #  #    #
381  *  #####   #       #    #  #####   ######  # ## #
382  *  #   #   #       #    #  #   #   #    #  ##  ##
383  *  #    #  ######  #####   #    #  #    #  #    #
384  * 
385  * 
386  *  #####      #     ####   #####   #         ##     #   #
387  *  #    #     #    #       #    #  #        #  #     # #
388  *  #    #     #     ####   #    #  #       #    #     #
389  *  #    #     #         #  #####   #       ######     #
390  *  #    #     #    #    #  #       #       #    #     #
391  *  #####      #     ####   #       ######  #    #     #
392  */
393
394 void
395 _DtTermPrimFuncRedrawDisplay(Widget w, int count, FunctionSource functionSource)
396 {
397     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
398     struct termData *tpd = tw->term.tpd;
399
400     (void) XClearArea(XtDisplay(w), XtWindow(w), 0, 0, 0, 0, True);
401     return;
402 }