edward-contrib

Extra commands for the extensible edward text editor

git clone https://git.8pit.net/edward-contrib.git

 1(import (scheme base)
 2        (srfi 1)
 3        (srfi 14)
 4        (chicken process)
 5
 6        (edward cli)
 7        (edward parse)
 8        (edward util)
 9        (edward ed cmd)
10        (edward ed addr)
11        (edward ed posix)
12        (edward ed editor))
13
14(include "util.scm")
15
16;;;;
17;; The pipe command.
18;;;;
19
20(define (exec-pipe editor range cmd)
21  (let* ((proc (process cmd))
22         (output (process-output-port proc))
23         (lines (editor-get-lines editor range)))
24    (write-string (lines->string lines) output)
25    (close-output-port output)
26    (let* ((input (process-input-port proc))
27           (recv (port->lines input)))
28      (close-input-port input)
29      (exec-delete editor range)
30      (exec-insert editor (car range) (car recv)))))
31
32(define-file-cmd (pipe exec-pipe (make-range))
33  (parse-cmd-char #\|)
34  (parse-token (char-set-complement (char-set #\newline))))
35
36;;;;
37;; The ctags command.
38;;;;
39
40(define (exec-tag editor name)
41  (let* ((cmd (string-append "!" "readtags -n - " name))
42         (out (car (read-from cmd))))
43    (if (null? out)
44      (editor-error editor (string-append "tag not found: " name))
45      (let* ((tags (parse-tags out))
46             (tag  (select-tag tags)))
47        (unless (equal? (tag-file tag) (text-editor-filename editor))
48          (%exec-edit editor (tag-file tag)))
49        (let* ((addrlst (parse parse-addrs (tag-regex tag)))
50               (lpair   (addrlst->lpair editor addrlst)))
51          (exec-print editor lpair))))))
52
53(define-file-cmd (tag exec-tag)
54  (parse-cmd-char #\T)
55  (parse-token char-set:graphic))
56
57;;;;
58;; The FZF command.
59;;;;
60
61(define (exec-fzf editor)
62  (let* ((cmd (string-append "!" "fzf"))
63         (out (read-from cmd)))
64    (unless (null? (car out))
65      (%exec-edit editor (caar out)))))
66
67(define-file-cmd (fzf exec-fzf)
68  (parse-cmd-char #\F))
69
70;;;;
71;; The scroll command.
72;;;;
73
74(define (exec-scroll editor start %amount)
75  (let* ((amount (if %amount
76                   %amount
77                   (max 0 (- (terminal-rows) 2)))) ;; -2 for cur/nxt prompt
78         (end    (addr->line editor (make-addr
79                                      (cons 'nth-line
80                                            (min
81                                              (editor-lines editor)
82                                              (+ start amount)))))))
83    (exec-print editor (cons start end))))
84
85(define-file-cmd (scroll exec-scroll (make-addr '(current-line) '(+1)))
86  (parse-cmd-char #\z)
87  (parse-optional parse-digits))
88
89;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
90
91(cond-expand
92  ((or chicken-script compiling)
93   (edward-main))
94  (else #t))