forked from 11111000000/pen.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pen-filters.el
159 lines (138 loc) · 5.29 KB
/
pen-filters.el
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
;; $MYGIT/semiosis/pen.el/scripts/filters
(defmacro pen-flash-region (&rest body)
`(let ((fg "#00aa00")
(bg "#006600")
(fg_blue "#5555ff")
(bg_blue "#2222bb")
(fg_purple "#ff55ff")
(bg_purple "#bb22bb")
(fg_limegreen "#55ff55")
(bg_limegreen "#22bb22")
(fg_yellow "#ffff55")
(bg_yellow "#bbbb22")
(fg_orange "#ffA500")
(bg_orange "#996200"))
(set-face-foreground 'region fg_limegreen)
(set-face-background 'region bg_limegreen)
(sit-for 0.05)
(let ((r ,@body))
(set-face-foreground 'region fg)
(set-face-background 'region bg)
r)))
(defun pen-replace-region (s)
(replace-region s))
(defun pen-region-filter (func)
"Apply the function to the selected region. The function must accept a string and return a string."
(let ((rstart (if (region-active-p) (region-beginning) (point-min)))
(rend (if (region-active-p) (region-end) (point-max))))
(let ((res
(pen-flash-region
(ptw func (buffer-substring rstart rend)))))
(pen-replace-region res))))
(defun pen-region-pipe (cmd)
(interactive (list (read-string "shell filter:")))
"pipe region through shell command"
(if (not (sor cmd))
(setq cmd "pen-tm filter"))
(pen-region-filter (lambda (input) (pen-sn (concat cmd) input))))
(defun pen-fwfzf ()
"This will pipe the selection into fzf filters, replacing the original region. If no region is selected, then the entire buffer is passed read only."
(interactive)
(if (region-active-p)
(if (>= (prefix-numeric-value current-prefix-arg) 4)
(pen-region-pipe "pen-tm filter")
(pen-region-pipe (chomp (esed " #.*" ""
(fz
(cat
(f-join pen-penel-directory
"config/filters.sh"))
nil nil "pen-fwfzf: ")))))
(pen-nil (pen-sn (concat "pen-tm -f -S -tout nw -noerror " (pen-q "f filter-with-fzf") " &") (buffer-string)))))
(defun pen-nwp (&optional cmd input nw_args)
"Runs command in a new window with input"
(interactive)
(if (not cmd)
(setq cmd "zsh"))
(pen-sn (concat "pen-tm -tout -S nw " nw_args " " (pen-q cmd) " &") input (get-dir)))
(defun rt-from-region ()
"Start an pen-rtcmd with the current region as input"
(interactive)
(if mark-active
(pen-nwp "pen-rtcmd awk '{$2 = gensub("$", " |", "g", $2); print $0}'" (pen-selected-text))))
(defun fzf-on-buffer ()
(interactive)
(shell-command-on-region
(point-min) (point-max)
(concat "pen-tm -S -tout nw 'fzf --sync | pen-tm -S -soak -tout nw v'")))
;; cat "$PENELD/config/filters.sh"
(require 'f)
(defun pen-filter-shellscript (script)
"This will pipe the selection into fzf filters,\nreplacing the original region. If no region is\nselected, then the entire buffer is passed\nread only."
(interactive (list (fz (cat (f-join pen-penel-directory "config" "filters.sh")) nil nil "pen-filter-shellscript: ")))
(if (region-active-p)
(pen-region-pipe
(chomp
(replace-regexp-in-string
" #.*" ""
script)))))
(defun pen-fi-join (arg)
"Indent by prefix arg"
(interactive "P")
(progn (if (not arg) (setq arg ""))
(pen-region-pipe (concat "pen-str join " (str arg)))))
(defun pen-fi-indent (arg)
"Indent by prefix arg"
(interactive "P")
;; (pen-ns (str arg))
(progn (if (not arg) (setq arg 4))
(pen-region-pipe (concat "pen-indent " (str arg)))))
(defun pen-fi-unindent (arg)
"Indent by prefix arg"
(interactive "P")
;; (pen-ns (str arg))
(progn
(if (not arg) (setq arg 4))
(pen-region-pipe (concat "pen-indent -" (str arg)))))
(defun pen-fi-org-indent (arg)
"Indent by prefix arg"
(interactive "P")
;; (pen-ns (str arg))
(cond
((and (equal major-mode 'org-mode)
(string-match-p "\\`\\*"
(if mark-active
(pen-selected-text)
(thing-at-point 'line))))
(pen-region-pipe "pen-orgindent"))
((and (equal major-mode 'org-mode)
(string-match-p "\\` *[-+]"
(if mark-active
(pen-selected-text)
(thing-at-point 'line))))
(pen-region-pipe (pen-cmd "pen-indent" "2")))
(t (progn (if (not arg) (setq arg 4))
(pen-region-pipe (concat "pen-indent " (str arg)))))))
(defun pen-fi-org-unindent (arg)
"Indent by prefix arg"
(interactive "P")
;; (pen-ns (str arg))
(cond
((and (equal major-mode 'org-mode)
(not arg)
(string-match-p "\\`\\*"
(if mark-active
(pen-selected-text)
(thing-at-point 'line))))
(pen-region-pipe "pen-orgindent -1"))
((and (equal major-mode 'org-mode)
(not arg))
(if (string-match-p "\\` *[-+]"
(if mark-active
(pen-selected-text)
(thing-at-point 'line)))
(pen-region-pipe "pen-indent -2")
(call-interactively #'org-run-babel-template-hydra)))
(t (progn
(if (not arg) (setq arg 4))
(pen-region-pipe (concat "pen-indent -" (str arg)))))))
(provide 'pen-filters)