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