add-ed 0.9.0-alpha

Embeddable pure rust editor based on ED
Documentation
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
use crate::Ed;
use crate::buffer::{Buffer, verify_selection, verify_index, verify_line};
use crate::ui::{UI, DummyUI};
use crate::error_consts::*;

mod parse_selection;
use parse_selection::*;
mod parse_expressions;
use parse_expressions::*;
mod parse_path;
use parse_path::*;
mod parse_flags;
use parse_flags::*;


/// The horrifying piece that is command parsing and execution.
///
/// I tried to break it up, but since all commands require different subsequent
/// parsing it is a lost cause.
/// If someone manages to do it, a PR is more than welcome.
///
/// Important things to remember if modifying this are:
/// * If taking input, verify everything you have first. Nothing is more
///   annoying than entering a paragraph of text to be informed that the given
///   index doesn't exist...
/// * Forbid input you don't handle. This should prevent accidentally force
///   exiting with ',Q file.txt' because you pressed 'Q' instead of 'W'.
pub fn run<B: Buffer>(state: &mut Ed<'_,B>, ui: &mut dyn UI, command: &str)
  -> Result<bool, &'static str>
{
  // Declare flags for printing after the command has been executed.
  let mut p = false;
  let mut n = false;
  let mut l = false;

  // Parse out the command index and the selection
  let (cmd_i, selection) = parse_selection(command)?;

  // Use the cmd_i to get a clean selection  
  // Match the command and act upon it
  let ret = match command[cmd_i..].trim().chars().next() {
    // No command is valid. It updates selection and prints
    None => {
      // Get and update the selection.
      let sel = interpret_selection(selection, state.selection, state.buffer)?;
      verify_selection(state.buffer, sel)?;
      state.selection = Some(sel);
      p = true; // Default command is 'p'
      Ok(false)
    },
    Some(ch) => {
      let clean = {
        let mut x = 1;
        while ! command.is_char_boundary(cmd_i + x) { x += 1; }
        &command[cmd_i + x ..].trim()
      };
      match ch {
        // Quit commands
        'q' | 'Q' => {
          if selection.is_some() { return Err(SELECTION_FORBIDDEN); }
          parse_flags(clean, "")?;
          if state.buffer.saved() || ch == 'Q' {
            Ok(true)
          }
          else {
            Err(UNSAVED_CHANGES)
          }
        }
        // Help commands
        'h' => {
          if selection.is_some() { return Err(SELECTION_FORBIDDEN); }
          // If 'help' was entered, print held
          if clean == &"elp" {
            ui.print(state.see_state(), HELP_TEXT)?;
          }
          // Else no flags accepted and print last error
          else {
            parse_flags(clean, "")?;
            ui.print(state.see_state(), state.error.unwrap_or(NO_ERROR))?;
          }
          Ok(false)
        },
        'H' => {
          if selection.is_some() { return Err(SELECTION_FORBIDDEN); }
          parse_flags(clean, "")?;
          state.print_errors = !state.print_errors; // Toggle the setting
          Ok(false)
        }
        // Non-editing commands
        '#' => {
          // Get and update selection (none given gives no change)
          if selection.is_some() {
            let sel = interpret_selection(selection, state.selection, state.buffer)?;
            verify_selection(state.buffer, sel)?;
            state.selection = Some(sel);
          }
          Ok(false)
        },
        '=' => { // Print selection (can set selection)
          let sel = interpret_selection(selection, state.selection, state.buffer)?;
          verify_selection(state.buffer, sel)?;
          state.selection = Some(sel);
          ui.print(state.see_state(), &format!("({},{})", sel.0, sel.1) )?;
          Ok(false)
        },
        // File commands
        'f' => { // Set or print filename
          if selection.is_some() { return Err(SELECTION_FORBIDDEN); }
          match parse_path(clean) {
            None => { // Print current filename
              if state.path.len() == 0 {
                ui.print(state.see_state(), NO_FILE)?;
              }
              else {
                ui.print(state.see_state(), &state.path)?;
              }
            },
            Some(x) => { // Set new filename
              state.path = x.to_string();
            }
          }
          Ok(false)
        }
        'e' | 'E' | 'r' => {
          // Read the selection if 'r', else error on any selection and return 0 on none (Lone default == no input)
          let index = 
            if ch == 'r' {
              let i = interpret_selection(selection, state.selection, state.buffer)?.1;
              Ok(Some(i))
            }
            else {
              if selection.is_none() {
                Ok(None)
              }
              else {
                Err(SELECTION_FORBIDDEN)
              }
            }?;
          // Only 'e' cares if the buffer is saved
          if !state.buffer.saved() && (ch == 'e') {
            Err(UNSAVED_CHANGES)
          }
          else {
            // Get the path (cutting of the command char and the trailing newline)
            let path = parse_path(clean);
            let path = path.unwrap_or(&state.path);
            // Read the data from the file
            let datalen = state.buffer.read_from(path, index, ch == 'E')?;
            if ch != 'r' {
              state.path = path.to_string();
            }
            let index = index.unwrap_or(1);
            state.selection = Some((index, index + datalen));
            Ok(false)
          }
        },
        'w' | 'W' => {
          // Since 'w' and 'W' should default to the whole buffer rather than previous selection
          // they get some custom code here
          let sel = match selection {
            // If selection given we interpret it
            // (When explicit selection is whole buffer we change it to None to signal that)
            Some(s) => {
              let inter = interpret_selection(Some(s), state.selection, state.buffer)?;
              if inter == (1, state.buffer.len()) {
                None
              } else {
                Some(inter)
              }
            },
            // If no selection defaults to selecting the whole buffer
            None => None,
          };

          // If not wq, parse path
          let (q, path) = if clean != &"q" {
            (false, parse_path(clean).unwrap_or(&state.path))
          }
          // If wq, use current file path
          else {
            (true, &state.path[..])
          };
          // If the 'q' flag is set the whole buffer must be selected
          if q && sel.is_some() { return Err(UNSAVED_CHANGES); }
          // Write it into the file (append if 'W')
          let append = ch == 'W';
          state.buffer.write_to(sel, path, append)?;
          // If given path now contains only the whole buffer, update state.file
          // If selection was given, save that selection
          match sel {
            None => {
              if !append { state.path = path.to_string(); }
            },
            Some(s) => {
              state.selection = Some((s.0, s.1));
            },
          }
          Ok(q)
        }
        // Print commands
        'p' | 'n' | 'l' => {
          let sel = interpret_selection(selection, state.selection, state.buffer)?;
          verify_selection(state.buffer, sel)?;
          // Get the flags
          let mut flags = parse_flags(&command[cmd_i..], "pnl")?;
          // Set the global print flags (safe to unwrap since parse_flags never removes a key)
          p = flags.remove(&'p').unwrap();
          n = flags.remove(&'n').unwrap();
          l = flags.remove(&'l').unwrap();
          state.selection = Some(sel);
          Ok(false)
        }
        'z' | 'Z' => {
          // Depending on forward or backward we use start or end of selection as starting point
          let sel = interpret_selection(selection, state.selection, state.buffer)?;
          let index = if ch == 'z' {
            sel.1
          } else {
            sel.0
          };
          verify_index(state.buffer, index)?;
          // Parse the arguments to see how many lines to scroll
          let nr_end = clean.find( | c: char | !c.is_numeric() ).unwrap_or(clean.len());
          let nr = if nr_end == 0 {
            3 // 3 is the default from ed, no reason to change
          } else {
            clean[.. nr_end].parse::<usize>().map_err(|_| INTEGER_PARSE)?
          };
          // Check what isn't numeric for flags
          let mut flags = parse_flags(&clean[nr_end ..], "pnl")?;
          p = true; // This command should print, so p always true
          n = flags.remove(&'n').unwrap();
          l = flags.remove(&'l').unwrap();
          // The real purpose is to update the selection, do that
          let new_sel = if ch == 'z' {
            // Gracefully handle overrunning bufferlen
            let mut start = index + 1;
            let mut end = index + nr;
            if start > state.buffer.len() { start = state.buffer.len(); }
            if end > state.buffer.len() { end = state.buffer.len(); }
            (start, end)
          } else {
            // Gracefully handle going under 0
            // (If we end up under 1 that is handled by print logic below)
            (index.saturating_sub(1 + nr), index.saturating_sub(1))
          };
          // Verify selection before applying. Probably only fails if buffer is empty.
          verify_selection(state.buffer, new_sel)?;
          // If all is well we set it and trust the p,n,l flag catcher to print for us
          state.selection = Some(new_sel);
          Ok(false)
        }
        // Basic editing commands
        'a' | 'i' | 'A' | 'I' => {
          let sel = interpret_selection(selection, state.selection, state.buffer)?;
          let mut flags = parse_flags(clean, "pnl")?;
          p = flags.remove(&'p').unwrap();
          n = flags.remove(&'n').unwrap();
          l = flags.remove(&'l').unwrap();
          match ch {
            'a' => verify_index(state.buffer, sel.1)?,
            'i' => verify_index(state.buffer, sel.0.saturating_sub(1))?,
            'A' => verify_line(state.buffer, sel.1)?,
            'I' => verify_line(state.buffer, sel.0)?,
            _ => { panic!("Unreachable code reached"); }
          }
          // Now that we have checked that the command is valid, get input
          // This is done so we don't drop text input, that would be annoying
          let tmp = ui.get_input(
            state.see_state(),
            '.',
            #[cfg(feature = "initial_input_data")]
            None,
          )?;
          // Input conversion, to follow buffer api
          let mut input = tmp.iter().map(|string| &string[..]);
          // Run the actual command and save returned selection to state
          state.selection = if input.len() != 0 {
            let index = if ch == 'a' || ch == 'A' {
              sel.1
            }
            else {
              sel.0.saturating_sub(1)
            };
            let start = index + 1; // since buffer.insert puts input after index
            let end = start + input.len() - 1; // Subtract for inclusive select
            state.buffer.insert(&mut input, index)?;
            // In the case of 'a', 'i' that is all
            // 'A' and 'I' need a join
            match ch {
              // For 'A' we join the first input line with its preceeding, and thus reduce selection with 1
              'A' => {
                // That the next line exists is checked by check_line on sel.1 above
                state.buffer.join((index, index + 1))?;
                // This offsets start and end of sel by -1
                Some((start.saturating_sub(1), end.saturating_sub(1)))
              },
              // For 'I' we do the same with the last input line and the following line, requiring no selection change
              'I' => {
                // That next line exists is checked by check_line on sel.0 above
                state.buffer.join((end, end + 1))?;
                Some((start,end))
              },
              // 'a' and 'i' need only pass out start and end
              'a' | 'i' => {
                Some((start, end))
              },
              _ => { panic!("Unreachable code reached"); }
            }
          }
          // If no input is given, keep old selection
          else {
            state.selection
          };
          Ok(false)
        },
        'c' => {
          let sel = interpret_selection(selection, state.selection, state.buffer)?;
          verify_selection(state.buffer, sel)?;
          let mut flags = parse_flags(clean, "pnl")?;
          p = flags.remove(&'p').unwrap();
          n = flags.remove(&'n').unwrap();
          l = flags.remove(&'l').unwrap();
          let input = ui.get_input(
            state.see_state(),
            '.',
            #[cfg(feature = "initial_input_data")]
            None,
          )?;
          let inputlen = input.len();
          let mut input = input.iter().map(|string| &string[..]);
          state.buffer.change(&mut input, sel)?;
          state.selection = if inputlen != 0 {
            Some((sel.0, sel.0 + inputlen - 1))
          }
          else {
            // Equivalent to delete, so use same post-selection logic
            if state.buffer.len() == 0 { None }
            else { Some((sel.0.saturating_sub(1), sel.0.saturating_sub(1))) }
          };
          Ok(false)
        },
        'C' => {
          #[cfg(feature = "initial_input_data")]
          {
            let sel = interpret_selection(selection, state.selection, state.buffer)?;
            let mut flags = parse_flags(clean, "pnl")?;
            p = flags.remove(&'p').unwrap();
            n = flags.remove(&'n').unwrap();
            l = flags.remove(&'l').unwrap();
            // Before getting input, get the selected area
            let selected = state.buffer.get_selection(sel)?.map(|s| s.to_string()).collect();
            // Then feed that to the input function as initial contents of the input buffer
            let tmp = ui.get_input(
              state.see_state(),
              '.',
              Some(selected),
            )?;
            let inputlen = input.len();
            let mut input = input.iter().map(|string| &string[..]);
            state.buffer.change(&mut input, sel)?;
            state.selection = if inputlen != 0 {
              Some((sel.0, sel.0 + inputlen - 1))
            }
            else {
              // Equivalent to delete, so use same post-selection logic
              if state.buffer.len() == 0 { None }
              else { Some((sel.0.saturating_sub(1), sel.0.saturating_sub(1))) }
            };
            Ok(false)
          }
          #[cfg(not(feature = "initial_input_data"))]
          {
            Err(UNDEFINED_COMMAND)
          }
        },
        'd' => { // Cut
          let sel = interpret_selection(selection, state.selection, state.buffer)?;
          // Since selection after execution can be 0 it isn't allowed to auto print after
          parse_flags(clean, "")?;
          state.buffer.cut(sel)?;
          // Try to figure out a selection after the deletion
          state.selection = 
            if state.buffer.len() == 0 { None }
            else {Some((sel.0.saturating_sub(1), sel.0.saturating_sub(1)))}
          ;
          Ok(false)
        },
        'y' => { // Copy to clipboard
          let sel = interpret_selection(selection, state.selection, state.buffer)?;
          let mut flags = parse_flags(clean, "pnl")?;
          state.buffer.copy(sel)?;
          // Save the selection and export the flags
          state.selection = Some(sel);
          p = flags.remove(&'p').unwrap();
          n = flags.remove(&'n').unwrap();
          l = flags.remove(&'l').unwrap();
          Ok(false)
        },
        'x' | 'X' => { // Append/prepend (respectively) clipboard contents to selection
          let sel = interpret_selection(selection, state.selection, state.buffer)?;
          let mut flags = parse_flags(clean, "pnl")?;
          p = flags.remove(&'p').unwrap();
          n = flags.remove(&'n').unwrap();
          l = flags.remove(&'l').unwrap();
          // Append or prepend based on command
          let index = 
            if ch == 'X' { sel.0.saturating_sub(1) }
            else { sel.1 }
          ;
          let length = state.buffer.paste(index)?;
          state.selection =
            if length != 0 {
              Some((index + 1, index + length))
            }
            else { None }
          ;
          Ok(false)
        },
        // Advanced editing commands
        'k' | 'K' => { // Tag first (k) or last (K) line in selection
          let sel = interpret_selection(selection, state.selection, state.buffer)?;
          // Expect only the tag, no flags
          if clean.len() > 1 { return Err(INVALID_TAG); }
          let index = if ch == 'k' { sel.0 } else { sel.1 };
          state.buffer.tag_line(index, clean.chars().next().unwrap_or('\0'))?;
          state.selection = Some(sel);
          Ok(false)
        },
        'm' | 't' => {
          // Parse the target index, then the flags if any
          let (ind_end, ind) = parse_index(&clean)?;
          let index = interpret_index(
            ind.unwrap_or(Ind::BufferLen),
            state.buffer,
            state.selection.map(|s| s.1),
          )?;
          let mut flags = parse_flags(&clean[ind_end..], "pnl")?;
          p = flags.remove(&'p').unwrap();
          n = flags.remove(&'n').unwrap();
          l = flags.remove(&'l').unwrap();
          // Calculate the selection
          let selection = interpret_selection(selection, state.selection, state.buffer)?;
          let move_size = selection.1 - selection.0; // Beware, is actually 1 less than move size due to inclusive bounds
          // Note that we subtract/add one to index to exclude index itself
          let new_sel = if ch == 'm' && selection.1 < index {
            // If moving forward detract moved lines from resulting selection
            (index - move_size, index)
          } else {
            (index + 1, index + move_size + 1)
          };
          // Make the change
          if ch == 'm' {
            state.buffer.mov(selection, index)?;
          }
          else {
            state.buffer.mov_copy(selection, index)?;
          }
          // Update the selection
          state.selection = Some(new_sel);
          Ok(false)
        }
        'j' => {
          // Calculate the selection
          let selection = interpret_selection(selection, state.selection, state.buffer)?;
          let mut flags = parse_flags(clean, "pnl")?;
          p = flags.remove(&'p').unwrap();
          n = flags.remove(&'n').unwrap();
          l = flags.remove(&'l').unwrap();
          state.buffer.join(selection)?;
          state.selection = Some((selection.0, selection.0)); // Guaranteed to exist, but may be wrong.
          Ok(false)
        }    
        // Pattern commands
        's' => {
          let selection = interpret_selection(selection, state.selection, state.buffer)?;
          // switch based on if clean was given or not
          if clean.len() == 0 {
            // This means we use the arguments stored in state.s_args
            match &state.s_args {
              None => return Err(NO_PRIOR_S),
              Some((pattern, replacement, global)) => {
                let end = state.buffer.search_replace((pattern, replacement), selection, *global)?;
                state.selection = if end < selection.0 {
                  if end == 0 {
                    None
                  } else {
                    Some((end, end))
                  }
                } else {
                  Some((selection.0, end))
                };
              }
            }
          }
          else {
            let expressions = parse_expressions(clean)?;
            if expressions.len() != 3 { return Err(EXPRESSION_TOO_SHORT); }
            let mut flags = parse_flags(&(expressions[2]), "gpnl")?;
            let g = flags.remove(&'g').unwrap();
            p = flags.remove(&'p').unwrap();
            n = flags.remove(&'n').unwrap();
            l = flags.remove(&'l').unwrap();
            let end = state.buffer.search_replace((&expressions[0], &expressions[1]), selection, g)?;
            state.selection = if end < selection.0 {
              if end == 0 {
                None
              } else {
                Some((end, end))
              }
            } else {
              Some((selection.0, end))
            };
            // If that was valid we save all the arguments to support lone 's'
            state.s_args = Some((expressions[0].to_string(), expressions[1].to_string(), g));
          }
          Ok(false)
        },
        'g' | 'v' => {
          let selection = interpret_selection(selection, state.selection, state.buffer)?;
          // Since this command may take input we need to check just as carefully as with a, i, c
          verify_selection(state.buffer, selection)?;
          let mut expressions = parse_expressions(clean)?;
          if expressions.len() < 2 { return Err(EXPRESSION_TOO_SHORT); }
          // We first try to mark all matching lines, to tell if there is any issue
          state.buffer.mark_matching(&expressions[0], selection, ch == 'v')?;
          // Then we get the script to run against them, if not already given
          // First grab commands given on command line
          let mut commands: Vec<String> = expressions.split_off(1).iter().map(|s| s.to_string()).collect();
          // If the last command in that list is not empty it means the list was not terminated, so we take more from input
          if commands.last().map(|s| s.trim()) != Some("") {
            // expressions.len() would be 0 if no char, so safe to unwrap
            let mut input = ui.get_input(
              state.see_state(),
              clean.chars().next().unwrap(),
              #[cfg(feature = "initial_input_data")]
              None,
            )?;
            commands.append(&mut input);
          }
          else {
            // If the last command was empty we should pop it, since it will otherwise cause an unexpected print
            commands.pop();
          }
          // After command collection we get the matching lines to run them at and do so
          while let Some(index) = state.buffer.get_marked()? {
            // Use dummy UI to recurse while supporting text input
            let mut dummy = DummyUI{
              input: commands.iter().map(|s| s.clone()).collect(),
              print_ui: Some(ui),
            };
            state.selection = Some((index, index));
            state.run_macro(&mut dummy)?;
          }
          Ok(false)
        },
        'G' | 'V' => {
          let selection = interpret_selection(selection, state.selection, state.buffer)?;
          // Since this command takes input we need to check just as carefully as with a, i, c
          verify_selection(state.buffer, selection)?;
          let expressions = parse_expressions(clean)?;
          if expressions.len() != 2 { return Err(EXPRESSION_TOO_SHORT); }
          if expressions[1].len() != 0 && expressions[1] != "\n" { return Err(UNDEFINED_FLAG); }
          // Mark first, to check if the expression is valid
          state.buffer.mark_matching(&expressions[0], selection, ch == 'V')?;
          // With all data gathered we fetch and iterate over the lines
          while let Some(index) = state.buffer.get_marked()? {
            // Print the line, so the user knows what they are changing
            ui.print_selection(state.see_state(), (index, index), false, false)?;
            // Get input and create dummy-ui with it
            // expressions.len() == 2 implies that a separator was given
            let input = ui.get_input(
              state.see_state(),
              clean.chars().next().unwrap(),
              #[cfg(feature = "initial_input_data")]
              None,
            )?;
            let mut dummy = DummyUI{
              input: input.into(),
              print_ui: Some(ui),
            };
            state.selection = Some((index, index));
            state.run_macro(&mut dummy)?;
          }
          Ok(false)
        },
        _cmd => {
          Err(UNDEFINED_COMMAND)
        }
      }
    }
  }?;
  
  // If print flags are set, print
  if p | n | l {
    if let Some(sel) = state.selection {
      ui.print_selection(state.see_state(), sel, n, l)?;
    }
    else {
      Err(SELECTION_EMPTY)?
    }
  }

  Ok(ret)
}