MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame^] | 1 | // MorgenGrauen MUDlib |
| 2 | // |
| 3 | // util/ex.c -- ex compatible editor |
| 4 | // |
| 5 | // $Id: ex.c 9142 2015-02-04 22:17:29Z Zesstra $ |
| 6 | |
| 7 | #pragma strong_types |
| 8 | #pragma save_types |
| 9 | #pragma pedantic |
| 10 | #pragma range_check |
| 11 | #pragma no_clone |
| 12 | |
| 13 | inherit "/std/util/input"; |
| 14 | |
| 15 | private nosave mapping ctrl = ([]); |
| 16 | private nosave mixed buf; |
| 17 | |
| 18 | #include <sys_debug.h> |
| 19 | #undef ARGS |
| 20 | #include <wizlevels.h> |
| 21 | #include <player.h> |
| 22 | |
| 23 | #define MODE 0 |
| 24 | # define CMD 0 |
| 25 | # define INP 1 |
| 26 | #define TEXT 1 |
| 27 | #define LINE 2 |
| 28 | #define FUNC 3 |
| 29 | #define ARGS 4 |
| 30 | #define FLAG 5 |
| 31 | # define NUM 1 |
| 32 | #define CHG 6 |
| 33 | |
| 34 | #define YANK "@YANK" |
| 35 | |
| 36 | int evaluate(string in); |
| 37 | |
| 38 | varargs int error(string msg, string arg) |
| 39 | { |
| 40 | if(stringp(arg)) write(arg+": "+msg+"\n"); |
| 41 | else write(msg+"\n"); |
| 42 | } |
| 43 | |
| 44 | string substitute(string s, string regex, string n, string extra) |
| 45 | { |
| 46 | mixed del; int i; |
| 47 | if((i = sizeof(del = regexplode(s, regex))) < 2) return s; |
| 48 | if(member(extra, 'g') == -1) i = 1; |
| 49 | else i -= 2; |
| 50 | while(i>0) { |
| 51 | del[i] = n; |
| 52 | i -= 2; |
| 53 | } |
| 54 | return implode(del, ""); |
| 55 | } |
| 56 | |
| 57 | // saveText() -- save edited text |
| 58 | // |
| 59 | int saveText() |
| 60 | { |
| 61 | string text; |
| 62 | text = implode(ctrl[buf][TEXT][1..], "\n")+"\n"; |
| 63 | error(sprintf("%O, %d Zeilen, %d Zeichen", |
| 64 | buf ? buf : "", sizeof(ctrl[buf][TEXT])-1, |
| 65 | sizeof(text))); |
| 66 | if(ctrl[buf][FUNC]) apply(ctrl[buf][FUNC], ctrl[buf][ARGS], text); |
| 67 | ctrl = m_copy_delete(ctrl, buf); |
| 68 | ctrl = m_copy_delete(ctrl, buf+"!"); |
| 69 | buf = 0; |
| 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | // cmdPrompt() -- create command prompt |
| 74 | // |
| 75 | string cmdPrompt(int mode, mixed text, int line, int maxl) |
| 76 | { |
| 77 | return ":"; |
| 78 | } |
| 79 | |
| 80 | // inputPrompt() -- create input prompt |
| 81 | // |
| 82 | string inputPrompt() |
| 83 | { |
| 84 | if(ctrl[buf][FLAG] & NUM) return sprintf("[%02d] ", ctrl[buf][LINE]); |
| 85 | return ""; |
| 86 | } |
| 87 | |
| 88 | string AddNum(string s) |
| 89 | { |
| 90 | string r; |
| 91 | r = inputPrompt()+s; |
| 92 | ctrl[buf][LINE]++; |
| 93 | return r; |
| 94 | } |
| 95 | |
| 96 | void showLines(int from, int to) |
| 97 | { |
| 98 | write(implode(map(ctrl[buf][TEXT][from..to], #'AddNum), "\n")+"\n"); |
| 99 | ctrl[buf][LINE] = from; |
| 100 | } |
| 101 | |
| 102 | // commandMode() -- handle commands |
| 103 | // |
| 104 | int commandMode(string in) |
| 105 | { |
| 106 | int from, to, swap; |
| 107 | string cmd; |
| 108 | |
| 109 | if(!in) return 0; |
| 110 | if(in[0..1] == "se") { |
| 111 | ctrl[buf][FLAG] ^= NUM; |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | if(sscanf(in, "%d,%d%s", from, to, cmd) != 3) |
| 116 | if(sscanf(in, "%d%s", from, cmd) != 2) |
| 117 | if(!stringp(in)) return error("Kein Editorkommando", in); |
| 118 | else { cmd = in; from = to = ctrl[buf][LINE]; } |
| 119 | else to = from; |
| 120 | |
| 121 | if(from < 0) from = sizeof(ctrl[buf][TEXT])-1+from; |
| 122 | if(to < 1) to = sizeof(ctrl[buf][TEXT])-1+to; |
| 123 | if(to && to < from) return error("Erste Adresse groesser als die Zweite"); |
| 124 | if(from) ctrl[buf][LINE] = from; |
| 125 | if(from > sizeof(ctrl[buf][TEXT])-1 || |
| 126 | to > sizeof(ctrl[buf][TEXT])-1) |
| 127 | return error("Nicht so viele Zeilen im Speicher"); |
| 128 | if(!to) to = from; |
| 129 | switch(cmd[0]) |
| 130 | { |
| 131 | case 'h': |
| 132 | write("ex: Kommandohilfe\n\n" |
| 133 | "Alle Kommandos funktionieren nach folgendem Prinzip:\n" |
| 134 | "Adresse Kommando Argumente\n" |
| 135 | "Adressen werden gebildet durch: ZeileVon,ZeileBis\n" |
| 136 | "(ZeileBis kann weggelassen werden, samt Komma)\n" |
| 137 | "Kommandos:\n" |
| 138 | " q,x -- Editor verlassen\n" |
| 139 | " r -- Datei einfuegen\n" |
| 140 | " r<datei>\n" |
| 141 | " a -- Text hinter der aktuellen Zeile einfuegen\n" |
| 142 | " i -- Text vor der aktuellen Zeile einfuegen\n" |
| 143 | " d -- Zeilen loeschen\n" |
| 144 | " y -- Zeilen zwischenspeichern\n" |
| 145 | " pu -- Zwischengespeicherte Zeilen einfuegen\n" |
| 146 | " s -- Substitution von Text in Zeilen\n" |
| 147 | " s/AlterText/NeuerText/\n" |
| 148 | " p,l -- Zeilen anzeigen\n" |
| 149 | " z -- Eine Seite anzeigen\n"); |
| 150 | break; |
| 151 | case 'q': |
| 152 | if(ctrl[buf][CHG]) |
| 153 | if(!(sizeof(cmd) > 1 && cmd[1]=='!')) |
| 154 | return error("Datei wurde geaendert! Abbrechen mit q!"); |
| 155 | else ctrl[buf] = ctrl[buf+"!"]; |
| 156 | case 'x': |
| 157 | if(saveText()) return 1; |
| 158 | case 'r': |
| 159 | if(!IS_WIZARD(this_player())) |
| 160 | return error("Kommando gesperrt", cmd[0..0]); |
| 161 | if(file_size(cmd[1..]) < 0) |
| 162 | return error("Datei nicht gefunden", "\""+cmd[1..]+"\""); |
| 163 | ctrl[buf][TEXT] = ctrl[buf][TEXT][0..from] |
| 164 | + explode(read_file(cmd[1..]), "\n") |
| 165 | + ctrl[buf][TEXT][(from == to ? to+1 : to)..]; |
| 166 | ctrl[buf][CHG] = 1; |
| 167 | break; |
| 168 | case 'a': |
| 169 | ctrl[buf][MODE] = INP; |
| 170 | write("Texteingabe: (. beendet zum Kommandomodus, ** beendet ganz)\n"); |
| 171 | input(#'inputPrompt, 0, #'evaluate); |
| 172 | ctrl[buf][CHG] = 1; |
| 173 | return 1; |
| 174 | case 'i': |
| 175 | ctrl[buf][MODE] = INP; |
| 176 | ctrl[buf][LINE]--; |
| 177 | write("Texteingabe: (. beendet zum Kommandomodus, ** beendet ganz)\n"); |
| 178 | input(#'inputPrompt, 0, #'evaluate); |
| 179 | ctrl[buf][CHG] = 1; |
| 180 | return 1; |
| 181 | case 'd': |
| 182 | ctrl[buf][TEXT][from..to] = ({}); |
| 183 | ctrl[buf][CHG] = 1; |
| 184 | break; |
| 185 | case 'y': |
| 186 | ctrl[YANK] = ctrl[buf][TEXT][from..to]; |
| 187 | if(to - from) error(to-from+1+" Zeilen gespeichert"); |
| 188 | break; |
| 189 | case 's': |
| 190 | { |
| 191 | mixed reg, new, extra, scmd; |
| 192 | if(sizeof(scmd = old_explode(cmd[1..], cmd[1..1])) < 2) |
| 193 | return error("Substitution fehlgeschlagen"); |
| 194 | reg = scmd[0]; new = scmd[1]; |
| 195 | if(sizeof(scmd) > 2) extra = scmd[2]; |
| 196 | else extra = ""; |
| 197 | ctrl[buf][TEXT][from..to] = map(ctrl[buf][TEXT][from..to], |
| 198 | #'substitute, |
| 199 | reg, new, extra); |
| 200 | showLines(from, to); |
| 201 | ctrl[buf][CHG] = 1; |
| 202 | break; |
| 203 | } |
| 204 | case 'z': |
| 205 | showLines(ctrl[buf][LINE], |
| 206 | ctrl[buf][LINE]+this_player()->QueryProp(P_SCREENSIZE)); |
| 207 | ctrl[buf][LINE] += this_player()->QueryProp(P_SCREENSIZE); |
| 208 | break; |
| 209 | case 'p': |
| 210 | if(sizeof(cmd) > 1 && cmd[1] == 'u') |
| 211 | { |
| 212 | if(!pointerp(ctrl[YANK])) return error("Keine Zeilen im Speicher"); |
| 213 | if(from >= ctrl[buf][LINE]) ctrl[buf][TEXT] += ctrl[YANK]; |
| 214 | else |
| 215 | ctrl[buf][TEXT][0..from] + ctrl[YANK] + ctrl[buf][TEXT][from+1..]; |
| 216 | ctrl[buf][LINE] += sizeof(ctrl[YANK]); |
| 217 | ctrl[YANK] = 0; |
| 218 | showLines(ctrl[buf][LINE], ctrl[buf][LINE]); |
| 219 | break; |
| 220 | } |
| 221 | case 'l': |
| 222 | default: |
| 223 | if(!from) |
| 224 | { |
| 225 | error("Kein Editorkommando", sprintf("%c", cmd[0])); |
| 226 | return 0; |
| 227 | } |
| 228 | showLines(from, to); |
| 229 | } |
| 230 | input(#'cmdPrompt, 0, #'evaluate); |
| 231 | return 1; |
| 232 | } |
| 233 | |
| 234 | // inputMode() -- handle input mode commands |
| 235 | // |
| 236 | int inputMode(string in) |
| 237 | { |
| 238 | switch(in) |
| 239 | { |
| 240 | case ".": |
| 241 | ctrl[buf][MODE] = CMD; |
| 242 | ctrl[buf][LINE]--; |
| 243 | input(#'cmdPrompt, 0, #'evaluate); |
| 244 | break; |
| 245 | case "**": |
| 246 | return saveText(); |
| 247 | default: |
| 248 | if(!in) /* do nothing now */; |
| 249 | if(ctrl[buf][LINE] < sizeof(ctrl[buf][TEXT])-1) |
| 250 | ctrl[buf][TEXT] = ctrl[buf][TEXT][0..ctrl[buf][LINE]-1] + ({ in }) |
| 251 | + ctrl[buf][TEXT][ctrl[buf][LINE]..]; |
| 252 | else ctrl[buf][TEXT] += ({ in }); |
| 253 | ctrl[buf][LINE]++; |
| 254 | input(#'inputPrompt, 0, #'evaluate); |
| 255 | break; |
| 256 | } |
| 257 | return 1; |
| 258 | } |
| 259 | |
| 260 | // evaluate() -- evaluate input (in) in context (ctrl[buf]) |
| 261 | // |
| 262 | int evaluate(string in) |
| 263 | { |
| 264 | switch(ctrl[buf][MODE]) |
| 265 | { |
| 266 | case INP: return inputMode(in); |
| 267 | case CMD: return commandMode(in); |
| 268 | default : return 0; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void StartEX(string in, mixed c) |
| 273 | { |
| 274 | if(!in || in == "j" || in == "J") ctrl[buf] = c; |
| 275 | else if(in && (in != "n" || in != "N")) |
| 276 | return 0; |
| 277 | ctrl[buf+"!"] = ctrl[buf]; |
| 278 | input(#'cmdPrompt, 0, #'evaluate); |
| 279 | } |
| 280 | |
| 281 | varargs int ex(mixed text, closure func, mixed fargs, string buffer) |
| 282 | { |
| 283 | int c, l; |
| 284 | mixed ct; |
| 285 | if(!text) text = ""; |
| 286 | c = sizeof(text); |
| 287 | l = sizeof(text = explode(text, "\n")) - 1; |
| 288 | ct = ({ CMD, text, 0, func, fargs, 0, 0}); |
| 289 | if(!ctrl[buffer]) StartEX(0, ct); |
| 290 | else input(sprintf("Es existiert bereits Text! Verwerfen? [j]"), |
| 291 | 0, #'StartEX, ct); |
| 292 | return 1; |
| 293 | } |