iota-editor 0.1.0

A simple text editor
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
//FIXME: Check unicode support
// stdlib dependencies
use std::cmp;
use std::collections::HashMap;
use std::path::PathBuf;
use std::fs::File;
use std::io::{Stdin, Read};
use std::convert::From;

// external dependencies
use gapbuffer::GapBuffer;

// local dependencies
use log::{Log, Change, LogEntry};
use utils::is_alpha_or_;
use input::Input;
use iterators::{Lines, Chars};
use textobject::{TextObject, Kind, Offset, Anchor};


#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum Mark {
    /// For keeping track of cursors.
    Cursor(usize),
    
    /// For using in determining some display of characters
    DisplayMark(usize),
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum WordEdgeMatch {
    Alphabet,
    Whitespace,
}

pub struct Buffer {
    /// Current buffers text
    text: GapBuffer<u8>,

    /// Table of marked indices in the text
    /// KEY: mark id => VALUE : (absolute index, line index)
    ///
    /// - absolute index is the offset from the start of the buffer
    /// - line index is the offset from the start of the current line
    marks: HashMap<Mark, (usize, usize)>,

    /// Transaction history (used for undo/redo)
    pub log: Log,

    /// Location on disk where the current buffer should be written
    pub file_path: Option<PathBuf>,
}

impl Buffer {
    /// Constructor for empty buffer.
    pub fn new() -> Buffer {
        Buffer {
            file_path: None,
            text: GapBuffer::new(),
            marks: HashMap::new(),
            log: Log::new(),
        }
    }

    /// Length of the text stored in this buffer.
    pub fn len(&self) -> usize {
        self.text.len() + 1
    }

    /// The x,y coordinates of a mark within the file. None if not a valid mark.
    pub fn get_mark_coords(&self, mark: Mark) -> Option<(usize, usize)> {
        if let Some(idx) = self.get_mark_idx(mark) {
            if let Some(line) = get_line(idx, &self.text) {
                Some((idx - line, (0..idx).filter(|i| -> bool { self.text[*i] == b'\n' })
                                               .count()))
            } else { None }
        } else { None }
    }

    /// The absolute index of a mark within the file. None if not a valid mark.
    pub fn get_mark_idx(&self, mark: Mark) -> Option<usize> {
        if let Some(&(idx, _)) = self.marks.get(&mark) {
            if idx < self.len() {
                Some(idx)
            } else { None }
        } else { None }
    }

    /// Creates an iterator on the text by chars.
    pub fn chars(&self) -> Chars {
        Chars {
            buffer: &self.text,
            idx: 0,
            forward: true,
        }
    }

    /// Creates an iterator on the text by chars that begins at the specified index.
    pub fn chars_from_idx(&self, idx: usize) -> Option<Chars> {
        if idx < self.len() {
            Some(Chars {
                buffer: &self.text,
                idx: idx,
                forward: true,
            })
        } else { None }
    }

    /// Creates an iterator on the text by chars that begins at the specified mark.
    pub fn chars_from(&self, mark: Mark) -> Option<Chars> {
        if let Some(&(idx, _)) = self.marks.get(&mark) {
            self.chars_from_idx(idx)
        } else { None }
    }

    /// Creates an iterator on the text by lines.
    pub fn lines(&self) -> Lines {
        Lines {
            buffer: &self.text,
            tail: 0,
            head: self.len()
        }
    }

    /// Creates an iterator on the text by lines that begins at the specified mark.
    pub fn lines_from(&self, mark: Mark) -> Option<Lines> {
        if let Some(&(idx, _)) = self.marks.get(&mark) {
            if idx < self.len() {
                Some(Lines {
                    buffer: &self.text,
                    tail: idx,
                    head: self.len(),
                })
            } else { None }
        } else { None }
    }

    /// Return the buffer index of a TextObject
    pub fn get_object_index(&self, obj: TextObject) -> Option<(usize, usize)> {
        match obj.kind {
            Kind::Char => self.get_char_index(obj.offset),
            Kind::Line(anchor) => self.get_line_index(obj.offset, anchor),
            Kind::Word(anchor) => self.get_word_index(obj.offset, anchor),
        }
    }

    /// Get the absolute index of a specific character in the buffer
    ///
    /// This character can be at an absolute position, or a postion relative
    /// to a given mark.
    ///
    /// The absolute offset is in the form (index, line_index) where:
    ///     index = the offset from the start of the buffer
    ///     line_index = the offset from the start of the current line
    ///
    /// ie: get the index of the 7th character after the cursor
    /// or: get the index of the 130th character from the start of the buffer
    fn get_char_index(&self, offset: Offset) -> Option<(usize, usize)> {
        let text = &self.text;

        match offset {
            // get the index of the char `offset` chars in front of `mark`
            //
            // ie: get the index of the char which is X chars in front of the MARK
            // or: get the index of the char which is 5 chars in front of the Cursor
            Offset::Forward(offset, from_mark) => {
                let last = self.len() - 1;
                if let Some(tuple)= self.marks.get(&from_mark) {
                    let (index, _) = *tuple;
                    let absolute_index = index + offset;
                    if absolute_index < last {
                        Some((absolute_index, absolute_index - get_line(absolute_index, text).unwrap()))
                    } else {
                        Some((last, last - get_line(last, text).unwrap()))
                    }
                } else {
                    None
                }
            }

            // get the index of the char `offset` chars before of `mark`
            //
            // ie: get the index of the char which is X chars before the MARK
            // or: get the index of the char which is 5 chars before the Cursor
            Offset::Backward(offset, from_mark) => {
                if let Some(tuple)= self.marks.get(&from_mark) {
                    let (index, _) = *tuple;
                    if index >= offset {
                        let absolute_index = index - offset;
                        Some((absolute_index, absolute_index - get_line(absolute_index, text).unwrap()))
                    } else {
                        None
                    }
                } else {
                    None
                }
            }

            // get the index of the char at position `offset` in the buffer
            //
            // ie: get the index of the 5th char in the buffer
            Offset::Absolute(absolute_char_offset) => {
                Some((absolute_char_offset, absolute_char_offset - get_line(absolute_char_offset, text).unwrap()))
            },
        }
    }

    /// Get the absolute index of a specific line in the buffer
    ///
    /// This line can be at an absolute position, or a postion relative
    /// to a given mark.
    ///
    /// The absolute offset is in the form (index, line_index) where:
    ///     index = the offset from the start of the buffer
    ///     line_index = the offset from the start of the current line
    ///
    /// The index is calculated based on a given Anchor. This Anchor determines
    /// where in the line the index is calculated. For instance, if you want
    /// the index of the start of the line, you would use Anchor::Start. If you
    /// are on the 5th char in a line, and want to get the index of the 5th char
    /// in another line, you can use Anchor::Same.
    ///
    /// ie: get the index of the middle of the 7th line after the cursor
    /// or: get the index of the start of the 130th line from the start of the buffer
    fn get_line_index(&self, offset: Offset, anchor: Anchor) -> Option<(usize, usize)> {
        match offset {
            Offset::Forward(offset, from_mark)  => { self.get_line_index_forward(anchor, offset, from_mark) }
            Offset::Backward(offset, from_mark) => { self.get_line_index_backward(anchor, offset, from_mark) }
            Offset::Absolute(line_number)       => { self.get_line_index_absolute(anchor, line_number) }
        }
    }

    /// Get the index of the line identified by line_number
    ///
    /// ie. Get the index of Anchor inside the 23th line in the buffer
    /// or: Get the index of the start of the 23th line
    fn get_line_index_absolute(&self, anchor: Anchor, line_number: usize) -> Option<(usize, usize)> {
        let text = &self.text;

        let nlines = (0..text.len()).filter(|i| text[*i] == b'\n')
                                    .take(line_number + 1)
                                    .collect::<Vec<usize>>();
        match anchor {
            Anchor::Start => {
                let end_offset = nlines[line_number - 1];
                let start = get_line(end_offset, text).unwrap();
                Some((start, 0))
            }

            Anchor::End => {
                let end_offset = nlines[line_number - 1];
                Some((end_offset, end_offset))
            }

            _ => {
                print!("Unhandled line anchor: {:?} ", anchor);
                None
            },
        }
    }


    fn get_line_index_backward(&self, anchor: Anchor, offset: usize, from_mark: Mark) -> Option<(usize, usize)> {
        let text = &self.text;

        if let Some(tuple) = self.marks.get(&from_mark) {
            let (index, line_index) = *tuple;
            let nlines = (0..index).rev().filter(|i| text[*i] == b'\n')
                                         .take(offset + 1)
                                         .collect::<Vec<usize>>();

            match anchor {
                // Get the index of the start of the desired line
                Anchor::Start => {
                    // if this is the first line in the buffer
                    if nlines.len() == 0 {
                        return Some((0, 0))
                    }
                    let start_offset = cmp::min(line_index + nlines[offset] + 1, nlines[offset]);
                    Some((start_offset + 1, 0))
                }

                // ie. If the current line_index is 5, then the line_index
                // returned will be the fifth index from the start of the
                // desired line.
                Anchor::Same => {
                    if offset == 0 {
                        Some((0, 0)) // going to start of the first line
                    } else if offset == nlines.len() {
                        Some((cmp::min(line_index, nlines[0]), line_index))
                    } else if offset > nlines.len() {
                        Some((0, 0)) // trying to move up from the first line
                    } else {
                        Some((cmp::min(line_index + nlines[offset] + 1, nlines[offset-1]), line_index))
                    }
                }

                _ => {
                    print!("Unhandled line anchor: {:?} ", anchor);
                    None
                },
            }
        } else {
            None
        }
    }

    fn get_line_index_forward(&self, anchor: Anchor, offset: usize, from_mark: Mark) -> Option<(usize, usize)> {
        let text = &self.text;
        let last = self.len() - 1;

        if let Some(tuple) = self.marks.get(&from_mark) {
            let (index, line_index) = *tuple;
            let nlines = (index..text.len()).filter(|i| text[*i] == b'\n')
                                            .take(offset + 1)
                                            .collect::<Vec<usize>>();

            match anchor {
                // Get the same index as the current line_index
                //
                // ie. If the current line_index is 5, then the line_index
                // returned will be the fifth index from the start of the
                // desired line.
                Anchor::Same => {
                    if offset == nlines.len() {
                        Some((cmp::min(line_index + nlines[offset-1] + 1, last), line_index))
                    } else {
                        if offset > nlines.len() {
                            Some((last, last - get_line(last, text).unwrap()))
                        } else {
                            Some((cmp::min(line_index + nlines[offset-1] + 1, nlines[offset]), line_index))
                        }
                    }
                }

                // Get the index of the end of the desired line
                Anchor::End => {
                    // if this is the last line in the buffer
                    if nlines.len() == 0 {
                        return Some((last, offset))
                    }
                    let end_offset = cmp::min(line_index + nlines[offset] + 1, nlines[offset]);
                    Some((end_offset, end_offset - get_line(end_offset, text).unwrap()))
                }

                _ => {
                    print!("Unhandled line anchor: {:?} ", anchor);
                    None
                },
            }
        } else {
            None
        }
    }

    fn get_word_index(&self, offset: Offset, anchor: Anchor) -> Option<(usize, usize)> {
        match offset {
            Offset::Forward(nth_word, from_mark)  => { self.get_word_index_forward(anchor, nth_word, from_mark) }
            Offset::Backward(nth_word, from_mark) => { self.get_word_index_backward(anchor, nth_word, from_mark) }
            Offset::Absolute(word_number)         => { self.get_word_index_absolute(anchor, word_number) }
        }
    }

    fn get_word_index_forward(&self, anchor: Anchor, nth_word: usize, from_mark: Mark) -> Option<(usize, usize)> {
        let text = &self.text;
        let last = self.len() - 1;
        // TODO: use anchor to determine this
        let edger = WordEdgeMatch::Whitespace;


        if let Some(tuple) = self.marks.get(&from_mark) {
            let (index, _) = *tuple;
            match anchor {
                Anchor::Start => {
                    // move to the start of nth_word from the mark
                    if let Some(new_index) = get_words(index, nth_word, edger, text) {
                        Some((new_index, new_index - get_line(new_index, text).unwrap()))
                    } else {
                        Some((last, last - get_line(last, text).unwrap()))
                    }
                }

                _ => {
                    print!("Unhandled word anchor: {:?} ", anchor);
                    Some((last, last - get_line(last, text).unwrap()))
                }
            }
        } else {
            None
        }
    }

    fn get_word_index_backward(&self, anchor: Anchor, nth_word: usize, from_mark: Mark) -> Option<(usize, usize)> {
        let text = &self.text;
        // TODO: use anchor to determine this
        let edger = WordEdgeMatch::Whitespace;


        if let Some(tuple) = self.marks.get(&from_mark) {
            let (index, _) = *tuple;
            match anchor {
                Anchor::Start => {
                    // move to the start of the nth_word before the mark
                    if let Some(new_index) = get_words_rev(index, nth_word, edger, text) {
                        Some((new_index, new_index - get_line(new_index, text).unwrap()))
                    } else {
                        Some((0, 0))
                    }
                }

                _ => {
                    print!("Unhandled word anchor: {:?} ", anchor);
                    None
                },
            }
        } else {
            None
        }
    }

    fn get_word_index_absolute(&self, anchor: Anchor, word_number: usize) -> Option<(usize, usize)> {
        let text = &self.text;
        // TODO: use anchor to determine this
        let edger = WordEdgeMatch::Whitespace;


        match anchor {
            Anchor::Start => {
                let new_index = get_words(0, word_number - 1, edger, text).unwrap();

                Some((new_index, new_index - get_line(new_index, text).unwrap()))
            }

            _ => {
                print!("Unhandled word anchor: {:?} ", anchor);
                None
            }
        }
    }

    /// Returns the status text for this buffer.
    pub fn status_text(&self) -> String {
        match self.file_path {
            Some(ref path)  =>  format!("[{}] ", path.display()),
            None            =>  format!("untitled "),
        }
    }

    /// Sets the mark to the location of a given TextObject, if it exists.
    /// Adds a new mark or overwrites an existing mark.
    pub fn set_mark_to_object(&mut self, mark: Mark, obj: TextObject) {
        if let Some(tuple) = self.get_object_index(obj) {
            self.marks.insert(mark, tuple);
        }
    }

    /// Sets the mark to a given absolute index. Adds a new mark or overwrites an existing mark.
    pub fn set_mark(&mut self, mark: Mark, idx: usize) {
        if let Some(line) = get_line(idx, &self.text) {
            if let Some(tuple) = self.marks.get_mut(&mark) {
                *tuple = (idx, idx - line);
                return;
            }
            self.marks.insert(mark, (idx, idx - line));
        }
    }

    // Remove the chars in the range from start to end
    pub fn remove_range(&mut self, start: usize, end: usize) -> Option<Vec<u8>> {
        let text = &mut self.text;
        let mut transaction = self.log.start(start);
        let mut vec = (start..end)
            .rev()
            .filter_map(|idx| text.remove(idx).map(|ch| (idx, ch)))
            .inspect(|&(idx, ch)| transaction.log(Change::Remove(idx, ch), idx))
            .map(|(_, ch)| ch)
            .collect::<Vec<u8>>();
        vec.reverse();
        Some(vec)
    }

    // Remove the chars between mark and object
    pub fn remove_from_mark_to_object(&mut self, mark: Mark, object: TextObject) -> Option<Vec<u8>> {
        if let Some(&(mark_idx, _)) = self.marks.get(&mark) {
            let object_index = self.get_object_index(object);

            if let Some((obj_idx, _)) = object_index {
                if mark_idx != obj_idx {
                    let (start, end) = if mark_idx < obj_idx { (mark_idx, obj_idx) } else { (obj_idx, mark_idx) };
                    return self.remove_range(start, end);
                }
            }
        }
        None
    }

    pub fn remove_object(&mut self, object: TextObject) -> Option<Vec<u8>> {
        let object_start = TextObject { kind: object.kind.with_anchor(Anchor::Start), offset: object.offset };
        let object_end = TextObject { kind: object.kind.with_anchor(Anchor::End), offset: object.offset };

        let start = self.get_object_index(object_start);
        let end = self.get_object_index(object_end);

        if let (Some((start_index, _)), Some((end_index, _))) = (start, end) {
            return self.remove_range(start_index, end_index);
        }
        None
    }

    /// Insert a char at the mark.
    pub fn insert_char(&mut self, mark: Mark, ch: u8) {
        if let Some(&(idx, _)) = self.marks.get(&mark) {
            self.text.insert(idx, ch);
            let mut transaction = self.log.start(idx);
            transaction.log(Change::Insert(idx, ch), idx);
        }
    }

    /// Redo most recently undone action.
    pub fn redo(&mut self) -> Option<&LogEntry> {
        if let Some(transaction) = self.log.redo() {
            commit(transaction, &mut self.text);
            Some(transaction)
        } else { None }
    }

    /// Undo most recently performed action.
    pub fn undo(&mut self) -> Option<&LogEntry> {
        if let Some(transaction) = self.log.undo() {
            commit(transaction, &mut self.text);
            Some(transaction)
        } else { None }
    }

}


// This is a bit of a hack to get around an error I was getting when
// implementing From<R: Read> for Buffer with From<PathBuf> for Buffer.
// The compiler was telling me this was a conflicting implementation even
// though Read is not implemented for PathBuf. Changing R: Read to
// R: Read + BufferFrom fixes the error.
//
// TODO: investigate this further - possible compiler bug?
trait BufferFrom {}
impl BufferFrom for Stdin {}
impl BufferFrom for File {}

impl From<PathBuf> for Buffer {
    fn from(path: PathBuf) -> Buffer {
        if let Ok(file) = File::open(&path) {
            let mut buff = Buffer::from(file);
            buff.file_path = Some(path);
            buff
        } else { Buffer::new() }
    }
}

impl<R: Read + BufferFrom> From<R> for Buffer {
    fn from(mut reader: R) -> Buffer {
        let mut buff = Buffer::new();
        let mut contents = String::new();
        if let Ok(_) = reader.read_to_string(&mut contents) {
            buff.text.extend(contents.bytes());
        }
        buff
    }
}

impl From<Input> for Buffer {
    fn from(input: Input) -> Buffer {
        match input {
            Input::Filename(path) => {
                match path {
                    Some(path) => Buffer::from(PathBuf::from(path)),
                    None       => Buffer::new(),
                }
            },
            Input::Stdin(reader) => {
                Buffer::from(reader)
            }
        }
    }
}


impl WordEdgeMatch {
    /// If c1 -> c2 is the start of a word.
    /// If end of word matching is wanted then pass the chars in reversed.
    fn is_word_edge(&self, c1: &u8, c2: &u8) -> bool {
        // FIXME: unicode support - issue #69
        match (self, *c1 as char, *c2 as char) {
            (_, '\n', '\n') => true, // Blank lines are always counted as a word
            (&WordEdgeMatch::Whitespace, c1, c2) => c1.is_whitespace() && !c2.is_whitespace(),
            (&WordEdgeMatch::Alphabet, c1, c2) if c1.is_whitespace() => !c2.is_whitespace(),
            (&WordEdgeMatch::Alphabet, c1, c2) if is_alpha_or_(c1) => !is_alpha_or_(c2) && !c2.is_whitespace(),
            (&WordEdgeMatch::Alphabet, c1, c2) if !is_alpha_or_(c1) => is_alpha_or_(c2) && !c2.is_whitespace(),
            (&WordEdgeMatch::Alphabet, _, _) => false,
        }
    }
}

fn get_words(mark: usize, n_words: usize, edger: WordEdgeMatch, text: &GapBuffer<u8>) -> Option<usize> {
    (mark + 1..text.len() - 1)
        .filter(|idx| edger.is_word_edge(&text[*idx - 1], &text[*idx]))
        .take(n_words)
        .last()
}

fn get_words_rev(mark: usize, n_words: usize, edger: WordEdgeMatch, text: &GapBuffer<u8>) -> Option<usize> {
    (1..mark)
        .rev()
        .filter(|idx| edger.is_word_edge(&text[*idx - 1], &text[*idx]))
        .take(n_words)
        .last()
}

/// Returns the index of the first character of the line the mark is in.
/// Newline prior to mark (EXCLUSIVE) + 1.
fn get_line(mark: usize, text: &GapBuffer<u8>) -> Option<usize> {
    let val = cmp::min(mark, text.len());
    (0..val + 1).rev().filter(|idx| *idx == 0 || text[*idx - 1] == b'\n')
                           .take(1)
                           .next()
}

/// Returns the index of the newline character at the end of the line mark is in.
/// Newline after mark (INCLUSIVE).
fn get_line_end(mark: usize, text: &GapBuffer<u8>) -> Option<usize> {
    let val = cmp::min(mark, text.len());
    (val..text.len()+1).filter(|idx| *idx == text.len() || text[*idx] == b'\n')
                            .take(1)
                            .next()
}

/// Performs a transaction on the passed in buffer.
fn commit(transaction: &LogEntry, text: &mut GapBuffer<u8>) {
    for change in transaction.changes.iter() {
        match change {
            &Change::Insert(idx, ch) => {
                text.insert(idx, ch);
            }
            &Change::Remove(idx, _) => {
                text.remove(idx);
            }
        }
    }
}

#[cfg(test)]
mod test {

    use buffer::{Buffer, Mark};
    use textobject::{TextObject, Offset, Kind, Anchor};

    fn setup_buffer(testcase: &'static str) -> Buffer {
        let mut buffer = Buffer::new();
        buffer.text.extend(testcase.bytes());
        buffer.set_mark(Mark::Cursor(0), 0);
        buffer
    }

    #[test]
    fn move_mark_char_right() {
        let mut buffer = setup_buffer("Some test content");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Char,
            offset: Offset::Forward(1, mark),
        };

        buffer.set_mark_to_object(mark, obj);
        
        assert_eq!(buffer.marks.get(&mark).unwrap(), &(1, 1));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (1, 0));
    }

    #[test]
    fn move_mark_char_left() {
        let mut buffer = setup_buffer("Some test content");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Char,
            offset: Offset::Backward(1, mark),
        };

        buffer.set_mark(mark, 3);
        buffer.set_mark_to_object(mark, obj);
        
        assert_eq!(buffer.marks.get(&mark).unwrap(), &(2, 2));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (2, 0));
    }
    
    #[test]
    fn move_mark_five_chars_right() {
        let mut buffer = setup_buffer("Some test content");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Char,
            offset: Offset::Forward(5, mark),
        };

        buffer.set_mark_to_object(mark, obj);
        
        assert_eq!(buffer.marks.get(&mark).unwrap(), &(5, 5));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (5, 0));
    }

    #[test]
    fn move_mark_line_down() {
        let mut buffer = setup_buffer("Some test content\nwith new\nlines!");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Line(Anchor::Same),
            offset: Offset::Forward(1, mark),
        };

        buffer.set_mark_to_object(mark, obj);
        
        assert_eq!(buffer.marks.get(&mark).unwrap(), &(18, 0));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (0, 1));
    }

    #[test]
    fn move_mark_line_up() {
        let mut buffer = setup_buffer("Some test content\nnew lines!");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Line(Anchor::Same),
            offset: Offset::Backward(1, mark),
        };

        buffer.set_mark(mark, 18);
        buffer.set_mark_to_object(mark, obj);
        
        assert_eq!(buffer.marks.get(&mark).unwrap(), &(0, 0));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (0, 0));
    }

    #[test]
    fn move_mark_two_lines_down() {
        let mut buffer = setup_buffer("Some test content\nwith new\nlines!");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Line(Anchor::Same),
            offset: Offset::Forward(2, mark),
        };

        buffer.set_mark_to_object(mark, obj);
        
        assert_eq!(buffer.marks.get(&mark).unwrap(), &(27, 0));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (0, 2));
    }

    #[test]
    fn move_mark_line_down_to_shorter_line() {
        let mut buffer = setup_buffer("Some test content\nwith new\nlines!");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Line(Anchor::Same),
            offset: Offset::Forward(1, mark),
        };

        buffer.set_mark(mark, 15);
        buffer.set_mark_to_object(mark, obj);

        assert_eq!(buffer.marks.get(&mark).unwrap(), &(26, 15));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (8, 1));

        let obj = TextObject {
            kind: Kind::Line(Anchor::Same),
            offset: Offset::Backward(1, mark),
        };
        buffer.set_mark_to_object(mark, obj);
        
        assert_eq!(buffer.marks.get(&mark).unwrap(), &(15, 15));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (15, 0));
    }

    #[test]
    fn move_mark_two_words_right() {
        let mut buffer = setup_buffer("Some test content\nwith new\nlines!");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Word(Anchor::Start),
            offset: Offset::Forward(2, mark),
        };

        buffer.set_mark_to_object(mark, obj);

        assert_eq!(buffer.marks.get(&mark).unwrap(), &(10, 10));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (10, 0));
    }

    #[test]
    fn move_mark_two_words_left() {
        let mut buffer = setup_buffer("Some test content\nwith new\nlines!");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Word(Anchor::Start),
            offset: Offset::Backward(2, mark),
        };

        buffer.set_mark(mark, 18);
        buffer.set_mark_to_object(mark, obj);

        assert_eq!(buffer.marks.get(&mark).unwrap(), &(5, 5));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (5, 0));
    }

    #[test]
    fn move_mark_move_word_left_at_start_of_buffer() {
        let mut buffer = setup_buffer("Some test content\nwith new\nlines!");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Word(Anchor::Start),
            offset: Offset::Backward(1, mark),
        };

        buffer.set_mark(mark, 5);
        buffer.set_mark_to_object(mark, obj);

        assert_eq!(buffer.marks.get(&mark).unwrap(), &(0, 0));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (0, 0));
    }

    #[test]
    fn move_mark_move_word_right_past_end_of_buffer() {
        let mut buffer = setup_buffer("Some test content\nwith new\nlines!");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Word(Anchor::Start),
            offset: Offset::Forward(8, mark),
        };

        buffer.set_mark(mark, 28);
        buffer.set_mark_to_object(mark, obj);

        assert_eq!(buffer.marks.get(&mark).unwrap(), &(33, 6));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (6, 2));
    }

    #[test]
    fn move_mark_second_word_in_buffer() {
        let mut buffer = setup_buffer("Some test content\nwith new\nlines!");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Word(Anchor::Start),
            offset: Offset::Absolute(2),
        };

        buffer.set_mark(mark, 18);
        buffer.set_mark_to_object(mark, obj);

        assert_eq!(buffer.marks.get(&mark).unwrap(), &(5, 5));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (5, 0));
    }

    #[test]
    fn move_mark_fifth_word_in_buffer() {
        let mut buffer = setup_buffer("Some test content\nwith new\nlines!");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Word(Anchor::Start),
            offset: Offset::Absolute(5),
        };

        buffer.set_mark(mark, 18);
        buffer.set_mark_to_object(mark, obj);

        assert_eq!(buffer.marks.get(&mark).unwrap(), &(23, 5));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (5, 1));
    }

    #[test]
    fn move_mark_second_line_in_buffer() {
        let mut buffer = setup_buffer("Some test content\nwith new\nlines!");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Line(Anchor::Start),
            offset: Offset::Absolute(2),
        };

        buffer.set_mark_to_object(mark, obj);

        assert_eq!(buffer.marks.get(&mark).unwrap(), &(18, 0));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (0, 1));
    }

    #[test]
    fn move_mark_second_char_in_buffer() {
        let mut buffer = setup_buffer("Some test content\nwith new\nlines!");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Char,
            offset: Offset::Absolute(2),
        };

        buffer.set_mark(mark, 18);
        buffer.set_mark_to_object(mark, obj);

        assert_eq!(buffer.marks.get(&mark).unwrap(), &(2, 2));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (2, 0));
    }

    #[test]
    fn move_mark_end_of_line() {
        let mut buffer = setup_buffer("Some test content\nwith new\nlines!");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Line(Anchor::End),
            offset: Offset::Forward(0, mark),
        };

        buffer.set_mark(mark, 19);
        buffer.set_mark_to_object(mark, obj);

        assert_eq!(buffer.marks.get(&mark).unwrap(), &(26, 8));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (8, 1));
    }

    #[test]
    fn move_mark_start_of_line() {
        let mut buffer = setup_buffer("Some test content\nwith new\nlines!");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Line(Anchor::Start),
            offset: Offset::Backward(0, mark),
        };

        buffer.set_mark(mark, 19);
        buffer.set_mark_to_object(mark, obj);

        assert_eq!(buffer.marks.get(&mark).unwrap(), &(18, 0));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (0, 1));
    }

    #[test]
    fn move_mark_past_last_line() {
        let mut buffer = setup_buffer("Some test content\n");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Line(Anchor::Same),
            offset: Offset::Forward(6, mark),
        };

        buffer.set_mark_to_object(mark, obj);

        assert_eq!(buffer.marks.get(&mark).unwrap(), &(18, 0));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (0, 1));
    }

    #[test]
    fn move_mark_line_up_middle_of_file() {
        let mut buffer = setup_buffer("Some\ntest\ncontent");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Line(Anchor::Same),
            offset: Offset::Backward(1, mark),
        };

        buffer.set_mark(mark, 10);
        buffer.set_mark_to_object(mark, obj);

        assert_eq!(buffer.marks.get(&mark).unwrap(), &(5, 0));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (0, 1));
    }

    #[test]
    fn move_mark_line_up_past_first_line() {
        let mut buffer = setup_buffer("Some\ntest\ncontent");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Line(Anchor::Same),
            offset: Offset::Backward(1, mark),
        };

        buffer.set_mark_to_object(mark, obj);

        assert_eq!(buffer.marks.get(&mark).unwrap(), &(0, 0));
        assert_eq!(buffer.get_mark_coords(mark).unwrap(), (0, 0));
    }

    #[test]
    fn test_insert() {
        let mut buffer = setup_buffer("");
        buffer.insert_char(Mark::Cursor(0), b'A');
        assert_eq!(buffer.len(), 2);
        assert_eq!(buffer.lines().next().unwrap(), [b'A']);
    }

    #[test]
    fn test_remove() {
        let mut buffer = setup_buffer("ABCD");
        let mark = Mark::Cursor(0);
        let obj = TextObject {
            kind: Kind::Char,
            offset: Offset::Forward(1, mark)
        };
        buffer.remove_from_mark_to_object(mark, obj);

        assert_eq!(buffer.len(), 4);
        assert_eq!(buffer.lines().next().unwrap(), [b'B', b'C', b'D']);
    }

    #[test]
    fn test_set_mark() {
        let mut buffer = setup_buffer("Test");
        buffer.set_mark(Mark::Cursor(0), 2);

        assert_eq!(buffer.get_mark_idx(Mark::Cursor(0)).unwrap(), 2);
    }

    #[test]
    fn test_to_lines() {
        let buffer = setup_buffer("Test\nA\nTest");
        let mut lines = buffer.lines();

        assert_eq!(lines.next().unwrap(), [b'T',b'e',b's',b't',b'\n']);
        assert_eq!(lines.next().unwrap(), [b'A',b'\n']);
        assert_eq!(lines.next().unwrap(), [b'T',b'e',b's',b't']);
    }

    #[test]
    fn test_to_lines_from() {
        let mut buffer = setup_buffer("Test\nA\nTest");
        buffer.set_mark(Mark::Cursor(0), 6);
        let mut lines = buffer.lines_from(Mark::Cursor(0)).unwrap();

        assert_eq!(lines.next().unwrap(), [b'\n']);
        assert_eq!(lines.next().unwrap(), [b'T',b'e',b's',b't']);
    }

    #[test]
    fn test_to_chars() {
        let mut buffer = setup_buffer("Test𐍈Test");
        buffer.set_mark(Mark::Cursor(0), 0);
        let mut chars = buffer.chars();
        assert!(chars.next().unwrap() == 'T');
        assert!(chars.next().unwrap() == 'e');
        assert!(chars.next().unwrap() == 's');
        assert!(chars.next().unwrap() == 't');
        assert!(chars.next().unwrap() == '𐍈');
        assert!(chars.next().unwrap() == 'T');
    }

    #[test]
    fn test_to_chars_from() {
        let mut buffer = setup_buffer("Test𐍈Test");
        buffer.set_mark(Mark::Cursor(0), 2);
        let mut chars = buffer.chars_from(Mark::Cursor(0)).unwrap();
        assert!(chars.next().unwrap() == 's');
        assert!(chars.next().unwrap() == 't');
        assert!(chars.next().unwrap() == '𐍈');
    }

    #[test]
    fn test_to_chars_rev() {
        // 𐍈 encodes as utf8 in 4 bytes... we need a solution for buffer offsets by byte/char
        let mut buffer = setup_buffer("Test𐍈Test");
        buffer.set_mark(Mark::Cursor(0), 8);
        let mut chars = buffer.chars_from(Mark::Cursor(0)).unwrap().reverse();
        assert!(chars.next().unwrap() == 'T');
        assert!(chars.next().unwrap() == '𐍈');
        assert!(chars.next().unwrap() == 't');
    }

}