00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include "compat.h"
00047
00048
00049
00050
00051 #include "sys.h"
00052 #include <stdlib.h>
00053 #include "el.h"
00054
00055
00056
00057
00058 el_protected int
00059 hist_init(EditLine_t* el) {
00060 el->fHistory.fFun = NULL;
00061 el->fHistory.fRef = NULL;
00062 el->fHistory.fBuf = (char*) el_malloc(EL_BUFSIZ);
00063 el->fHistory.fSz = EL_BUFSIZ;
00064
00065 if (el->fHistory.fBuf == NULL) {
00066 return -1;
00067 }
00068 el->fHistory.fLast = el->fHistory.fBuf;
00069 return 0;
00070 }
00071
00072
00073
00074
00075
00076 el_protected void
00077 hist_end(EditLine_t* el) {
00078 el_free((ptr_t) el->fHistory.fBuf);
00079 el->fHistory.fBuf = NULL;
00080 }
00081
00082
00083
00084
00085
00086 el_protected int
00087 hist_set(EditLine_t* el, HistFun_t fun, ptr_t ptr) {
00088 el->fHistory.fRef = ptr;
00089 el->fHistory.fFun = fun;
00090 return 0;
00091 }
00092
00093
00094
00095
00096
00097
00098 el_protected ElAction_t
00099 hist_get(EditLine_t* el) {
00100 const char* hp;
00101 int h;
00102
00103 if (el->fHistory.fEventNo == 0) {
00104 (void) strncpy(el->fLine.fBuffer, el->fHistory.fBuf,
00105 el->fHistory.fSz);
00106 ElColor_t* col = el->fLine.fBufColor;
00107
00108 for (size_t i = 0; i < (size_t) el->fHistory.fSz; ++i) {
00109 col[i] = -1;
00110 }
00111 el->fLine.fLastChar = el->fLine.fBuffer +
00112 (el->fHistory.fLast - el->fHistory.fBuf);
00113
00114 #ifdef KSHVI
00115
00116 if (el->fMap.fType == MAP_VI) {
00117 el->fLine.fCursor = el->fLine.fBuffer;
00118 } else
00119 #endif
00120 el->fLine.fCursor = el->fLine.fLastChar;
00121
00122 return CC_REFRESH;
00123 }
00124
00125 if (el->fHistory.fRef == NULL) {
00126 return CC_ERROR;
00127 }
00128
00129 hp = HIST_FIRST(el);
00130
00131 if (hp == NULL) {
00132 return CC_ERROR;
00133 }
00134
00135 for (h = 1; h < el->fHistory.fEventNo; h++) {
00136 if ((hp = HIST_NEXT(el)) == NULL) {
00137 el->fHistory.fEventNo = h;
00138 return CC_ERROR;
00139 }
00140 }
00141 (void) strncpy(el->fLine.fBuffer, hp,
00142 (size_t) (el->fLine.fLimit - el->fLine.fBuffer));
00143 ElColor_t* col = el->fLine.fBufColor;
00144
00145 for (size_t i = 0; i < (size_t) (el->fLine.fLimit - el->fLine.fBuffer); ++i) {
00146 col[i] = -1;
00147 }
00148 el->fLine.fLastChar = el->fLine.fBuffer + strlen(el->fLine.fBuffer);
00149
00150 if (el->fLine.fLastChar > el->fLine.fBuffer) {
00151 if (el->fLine.fLastChar[-1] == '\n') {
00152 el->fLine.fLastChar--;
00153 }
00154
00155 if (el->fLine.fLastChar[-1] == ' ') {
00156 el->fLine.fLastChar--;
00157 }
00158
00159 if (el->fLine.fLastChar < el->fLine.fBuffer) {
00160 el->fLine.fLastChar = el->fLine.fBuffer;
00161 }
00162 }
00163 #ifdef KSHVI
00164
00165 if (el->fMap.fType == MAP_VI) {
00166 el->fLine.fCursor = el->fLine.fBuffer;
00167 } else
00168 #endif
00169 el->fLine.fCursor = el->fLine.fLastChar;
00170
00171 return CC_REFRESH;
00172 }
00173
00174
00175
00176
00177
00178 el_protected int
00179
00180 hist_list(EditLine_t* el, int , const char** ) {
00181 const char* str;
00182
00183 if (el->fHistory.fRef == NULL) {
00184 return -1;
00185 }
00186
00187 for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el)) {
00188 (void) fprintf(el->fOutFile, "%d %s%s",
00189 el->fHistory.fEv.fNum, str,
00190 (NULL == strstr(str, "\n")) ? "\n" : ""
00191
00192
00193
00194
00195
00196
00197
00198 );
00199 }
00200
00201 return 0;
00202 }
00203
00204
00205
00206
00207
00208
00209 el_protected int
00210
00211 hist_enlargebuf(EditLine_t* el, size_t oldsz, size_t newsz) {
00212 char* newbuf;
00213
00214 newbuf = (char*) realloc(el->fHistory.fBuf, newsz);
00215
00216 if (!newbuf) {
00217 return 0;
00218 }
00219
00220 (void) memset(&newbuf[oldsz], '\0', newsz - oldsz);
00221
00222 el->fHistory.fLast = newbuf +
00223 (el->fHistory.fLast - el->fHistory.fBuf);
00224 el->fHistory.fBuf = newbuf;
00225 el->fHistory.fSz = newsz;
00226
00227 return 1;
00228 }