Compare commits

...

2 Commits

Author SHA1 Message Date
xfnw 14ad178349 add x and X commands
not originally planned, but would be nice to have a counterpart to i/I
2023-11-21 20:02:12 -05:00
xfnw 9685d31af6 factor out general region application from insert
'send the selected lines through this function' seems like a
common pattern
2023-11-21 19:59:53 -05:00
2 changed files with 37 additions and 5 deletions

View File

@ -40,5 +40,7 @@ implementation status of planned features:
- [x] s - ce-command-reg-replace
- [ ] t - ce-command-copy
- [x] w - ce-command-write
- [x] x - ce-command-chop
- [x] X - ce-command-chop-beg
- [x] 0 - ce-command-number

40
ce.cl
View File

@ -30,6 +30,8 @@
(#\s . ce-command-reg-replace)
(#\t . ce-command-copy)
(#\w . ce-command-write)
(#\x . ce-command-chop)
(#\X . ce-command-chop-beg)
(#\0 . ce-command-number)
(#\1 . ce-command-number)
(#\2 . ce-command-number)
@ -69,10 +71,21 @@
"return nil if numeric or newline"
(not (or (digit-char-p c) (char= #\Newline c))))
(defun string-or (a b)
"or but first being empty is equivalent to nil"
(or (if (string= "" a) nil a) b))
(defun ce-mod (num div)
"modulus but handle zero"
(if (= 0 div) 0 (mod num div)))
(defun ce-apply-region (fun)
"apply fun to each line of region"
(let ((mlen (list-length buffer)))
(let ((in (ce-mod inpoint mlen)) (out (1+ (ce-mod outpoint mlen))))
(let ((new (mapcar fun (subseq buffer in out))))
(ce-replace-lines in out new)))))
(defun ce-push-line (index line)
"push a line into the buffer at index"
(if (= 0 index) ; index 0 is special as buffer is a singly linked list
@ -347,11 +360,8 @@ specific command. the recognized commands are as follows:
"helper for insert and insert-beg"
`(progn
(ce-reset-input)
(let ((inp (read-line)) (mlen (list-length buffer)))
(let ((in (ce-mod inpoint mlen)) (out (1+ (ce-mod outpoint mlen))))
(let ((new (mapcar (lambda (x) (concat ,a ,b))
(subseq buffer in out))))
(ce-replace-lines in out new))))))
(let ((inp (read-line)))
(ce-apply-region (lambda (x) (concat ,a ,b))))))
(defun ce-command-insert (&optional c)
"add to the end of each line in region"
@ -423,6 +433,26 @@ specific command. the recognized commands are as follows:
:if-does-not-exist :create)
(format out "~{~a~%~}" buffer))))
(defmacro ce-build-chop (fin)
"helper for chop and chop-beg"
`(progn
(ce-reset-input)
(let ((inp (parse-integer (string-or (read-line) "1"))))
(ce-apply-region (lambda (x)
(let ((llen (length x)))
(if (< llen inp)
x ,fin)))))))
(defun ce-command-chop (&optional c)
"chop off argument characters from end of each line in region"
(declare (ignore c))
(ce-build-chop (subseq x 0 (- llen inp))))
(defun ce-command-chop-beg (&optional c)
"chop off argument characters from start of each line in region"
(declare (ignore c))
(ce-build-chop (subseq x inp)))
(defun ce-command-number (c)
"input a number"
(if (or (= 0 newpoint) (= 2 newpoint))