-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
pen-support.el
420 lines (359 loc) · 13.8 KB
/
pen-support.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
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
(defmacro defset (symbol value &optional documentation)
"Instead of doing a defvar and a setq, do this. [[http://ergoemacs.org/emacs/elisp_defvar_problem.html][ergoemacs.org/emacs/elisp_defvar_problem.html]]"
`(progn (defvar ,symbol ,documentation)
(setq ,symbol ,value)))
(defun string-empty-or-nil-p (s)
(or (not s)
(string-empty-p s)))
(defun string-not-empty-nor-nil-p (s)
(not (string-empty-or-nil-p s)))
(defun string-first-nonnil-nonempty-string (&rest ss)
"Get the first non-nil string."
(let ((result))
(catch 'bbb
(dolist (p ss)
(if (string-not-empty-nor-nil-p p)
(progn
(setq result p)
(throw 'bbb result)))))
result))
(defun cwd ()
"Gets the current working directory"
(interactive)
(substring (shut-up-c (pwd)) 10))
(defun get-dir ()
"Gets the directory of the current buffer's file. But this could be different from emacs' working directory.
Takes into account the current file name."
(shut-up-c
(let ((filedir (if buffer-file-name
(file-name-directory buffer-file-name)
(file-name-directory (cwd)))))
(if (s-blank? filedir)
(cwd)
filedir))))
(defmacro shut-up-c (&rest body)
"This works for c functions where shut-up does not."
`(progn (let* ((inhibit-message t))
,@body)))
(defun pen-q (&rest strings)
(let ((print-escape-newlines t))
(s-join " " (mapcar 'prin1-to-string strings))))
(defalias 'pen-q 'e/escape-string)
(defun e/chomp (str)
"Chomp (remove tailing newline from) STR."
(replace-regexp-in-string "\n\\'" "" str))
(defalias 'chomp 'e/chomp)
(defun get-string-from-file (filePath)
"Return filePath's file content."
(with-temp-buffer
(insert-file-contents filePath)
(buffer-string)))
(defun str (thing)
"Converts object or string to an unformatted string."
(if thing
(if (stringp thing)
(substring-no-properties thing)
(progn
(setq thing (format "%s" thing))
(set-text-properties 0 (length thing) nil thing)
thing))
""))
(defun sh-notty (cmd &optional stdin dir exit_code_var detach b_no_unminimise output_buffer b_unbuffer chomp b_output-return-code)
"Runs command in shell and return the result.
This appears to strip ansi codes.
\(sh) does not."
(interactive)
(if (not cmd)
(setq cmd "false"))
(if (not dir)
(setq dir (get-dir)))
(let ((default-directory dir))
(if (not b_no_unminimise)
(setq cmd (umn cmd)))
(if b_unbuffer
(setq cmd (concat "unbuffer -p " cmd)))
(if (or
(and (variable-p 'sh-update)
(eval 'sh-update))
(>= (prefix-numeric-value current-global-prefix-arg) 16))
(setq cmd (concat "upd " cmd)))
(setq tf (make-temp-file "elisp_bash"))
(setq tf_exit_code (make-temp-file "elisp_bash_exit_code"))
(let ((exps
(sh-construct-exports
(-filter 'identity
(list (list "PATH" (getenv "PATH"))
(if (and (variable-p 'sh-update) (eval 'sh-update))
(list "UPDATE" "y")))))))
(setq final_cmd (concat exps "; ( cd " (pen-q dir) "; " cmd "; echo -n $? > " tf_exit_code " ) > " tf)))
(if detach
(setq final_cmd (concat "trap '' HUP; unbuffer bash -c " (pen-q final_cmd) " &")))
(shut-up-c
(if (not stdin)
(progn
(shell-command final_cmd output_buffer))
(with-temp-buffer
(insert stdin)
(shell-command-on-region (point-min) (point-max) final_cmd output_buffer))))
(setq output (get-string-from-file tf))
(if chomp
(setq output (chomp output)))
(progn
(defset b_exit_code (get-string-from-file tf_exit_code)))
(if b_output-return-code
(setq output (str b_exit_code)))
output))
(defalias 'sn 'sh-notty)
(defun snc (cmd &optional stdin)
"sn chomp"
(chomp (sn cmd stdin)))
(defun slugify (input &optional joinlines length)
"Slugify input"
(interactive)
(let ((slug
(if joinlines
(sn "tr '\n' - | slugify" input)
(sn "slugify" input))))
(if length
(substring slug 0 (- length 1))
slug)))
(defun fz-completion-second-of-tuple-annotation-function (s)
(let ((item (assoc s minibuffer-completion-table)))
(when item
;; (concat " # " (second item))
(cond
((listp item) (concat " # " (second item)))
((stringp item) "")
(t "")))))
(cl-defun cl-fz (list &key prompt &key full-frame &key initial-input &key must-match &key select-only-match &key hist-var &key add-props)
(if (and (not hist-var)
(sor prompt))
(setq hist-var (intern (concat "histvar-fz-" (slugify prompt)))))
(setq prompt (sor prompt ":"))
(if (not (re-match-p " $" prompt))
(setq prompt (concat prompt " ")))
(if (eq (type-of list) 'symbol)
(cond
((variable-p 'clojure-mode-funcs) (setq list (eval list)))
((fboundp 'clojure-mode-funcs) (setq list (funcall list)))))
(if (stringp list)
(setq list (split-string list "\n")))
(if (and select-only-match (eq (length list) 1))
(car list)
(progn
(setq prompt (or prompt ":"))
(let ((helm-full-frame full-frame)
(completion-extra-properties nil))
(if add-props
(setq completion-extra-properties
(append
completion-extra-properties
add-props)))
(if (and (listp (car list)))
(setq completion-extra-properties
(append
'(:annotation-function fz-completion-second-of-tuple-annotation-function)
completion-extra-properties)))
(completing-read prompt list nil must-match initial-input hist-var)))))
(defun fz (list &optional input b_full-frame prompt must-match select-only-match add-props)
(cl-fz
list
:initial-input input
:full-frame b_full-frame
:prompt prompt
:must-match must-match
:select-only-match select-only-match
:add-props add-props))
(defun selected ()
(or
(use-region-p)
(evil-visual-state-p)))
(defun glob (pattern &optional dir)
(split-string (cl-sn (concat "glob -b " (pen-q pattern) " 2>/dev/null") :stdin nil :dir dir :chomp t) "\n"))
(defun new-buffer-from-string (&optional contents bufname mode nodisplay)
"Create a new untitled buffer from a string."
(interactive)
(if (not bufname)
(setq bufname "*untitled*"))
(let ((buffer (generate-new-buffer bufname)))
(set-buffer-major-mode buffer)
(if (not nodisplay)
(display-buffer buffer '(display-buffer-same-window . nil)))
(with-current-buffer buffer
(if contents (insert (str contents)))
(beginning-of-buffer)
(if mode (call-function mode)))
buffer))
(defalias 'nbfs 'new-buffer-from-string)
(defun new-buffer-from-o (o)
(new-buffer-from-string
(if (stringp o)
o
(pp-to-string o))))
(defun etv (o)
"Returns the object. This is a way to see the contents of a variable while not interrupting the flow of code.
Example:
(message (etv \"shane\"))"
(new-buffer-from-o o)
o)
(defun flatten-once (list-of-lists)
(apply #'append list-of-lists))
(defun filter-selected-region-through-function (fun)
(let* ((start (if (selected) (region-beginning) (point-min)))
(end (if (selected) (region-end) (point-max)))
(doreverse (and (selected) (< (point) (mark))))
(removed (delete-and-extract-region start end))
(replacement (str (ptw fun (str removed))))
(replacement-len (length (str replacement))))
(if buffer-read-only (new-buffer-from-string replacement)
(progn
(insert replacement)
(let ((end-point (point))
(start-point (- (point) replacement-len)))
(push-mark start-point)
(goto-char end-point)
(setq deactivate-mark nil)
(activate-mark)
(if doreverse
(call-interactively 'cua-exchange-point-and-mark)))))))
(defalias 'filter-selection 'filter-selected-region-through-function)
(defmacro ntimes (n &rest body)
(cons 'progn (flatten-once
(cl-loop for i from 1 to n collect body))))
(defun pen-selected-text ()
"Just give me the selected text as a string. If it's empty, then nothing was selected. region-active-p does not work for evil selection."
(interactive)
(cond
((or (region-active-p) (eq evil-state 'visual))
(str (buffer-substring (region-beginning) (region-end))))
(iedit-mode
(iedit-current-occurrence-string))))
(defun xc (&optional s silent)
"emacs kill-ring, xclip copy
when s is nil, return current contents of clipboard
when s is a string, set the clipboard to s"
(interactive)
(if (and
s
(not (stringp s)))
(setq s (pps s)))
(if (not (empty-string-p s))
(kill-new s)
(if (selected-p)
(progn
(setq s (selection))
(call-interactively 'kill-ring-save))))
(if s
(if (not silent) (message "%s" (concat "Copied: " s)))
(progn
(shell-command-to-string "xsel --clipboard --output"))))
(defun pen-ivy-completing-read (prompt collection
&optional predicate require-match initial-input
history def inherit-input-method)
"This is like ivy-completing-read but it does not escape +"
(let ((handler
(and (< ivy-completing-read-ignore-handlers-depth (minibuffer-depth))
(assq this-command ivy-completing-read-handlers-alist))))
(if handler
(let ((completion-in-region-function #'completion--in-region)
(ivy-completing-read-ignore-handlers-depth (1+ (minibuffer-depth))))
(funcall (cdr handler)
prompt collection
predicate require-match
initial-input history
def inherit-input-method))
;; See the doc of `completing-read'.
(when (consp history)
(when (numberp (cdr history))
(setq initial-input (nth (1- (cdr history))
(symbol-value (car history)))))
(setq history (car history)))
(when (consp def)
(setq def (car def)))
(let ((str (ivy-read
prompt collection
:predicate predicate
:require-match (when (and collection require-match)
require-match)
:initial-input (cond ((consp initial-input)
(car initial-input))
(t
initial-input))
:preselect def
:def def
:history history
:keymap nil
:dynamic-collection ivy-completing-read-dynamic-collection
:extra-props '(:caller ivy-completing-read)
:caller (if (and collection (symbolp collection))
collection
this-command))))
(if (string= str "")
(or def "")
str)))))
(defun completing-read-hist (prompt &optional initial-input histvar default-value)
"read-string but with history."
(if (not histvar)
(setq histvar (intern (concat "completing-read-hist-" (slugify prompt)))))
(setq prompt (sor prompt ":"))
(if (not (re-match-p " $" prompt))
(setq prompt (concat prompt " ")))
(initvar histvar)
(if (and (not initial-input)
(listp histvar))
(setq initial-input (first histvar)))
(eval `(progn
(let ((inhibit-quit t))
(or (with-local-quit
(let ((completion-styles
'(basic))
(s (str (pen-ivy-completing-read ,prompt ,histvar nil nil initial-input ',histvar nil))))
(setq ,histvar (seq-uniq ,histvar 'string-equal))
s))
"")))))
(defalias 'read-string-hist 'completing-read-hist)
(defun vector2list (v)
(append v nil))
(defun region-or-buffer-string ()
(interactive)
(if (or (region-active-p) (eq evil-state 'visual))
(str (buffer-substring (region-beginning) (region-end)))
(str (buffer-substring (point-min) (point-max)))))
(defun detect-language (&optional detect buffer-not-selection)
"Returns the language of the buffer or selection."
(interactive)
(let ((lang
(if (not detect)
(s-replace-regexp "-mode$" "" (current-major-mode-string))
(str (language-detection-string
(if buffer-not-selection
(buffer-string)
(region-or-buffer-string)))))))
(if (string-equal "rustic" lang) (setq lang "rust"))
(if (string-equal "clojurec" lang) (setq lang "clojure"))
lang))
(defun mode-to-lang (&optional modesym)
(if (not modesym)
(setq modesym major-mode))
(s-replace-regexp "-mode$" "" (symbol-name modesym)))
(defun lang-to-mode (&optional langstr)
(if (not langstr)
(setq langstr (detect-language)))
(intern (concat langstr "-mode")))
(defun get-ext-for-lang (langstr)
(get-ext-for-mode (lang-to-mode langstr)))
(defun get-ext-for-mode (&optional m)
(interactive)
(if (not m) (setq m major-mode))
(cond ((eq major-mode 'json-mode) "json")
((eq major-mode 'python-mode) "py")
((eq major-mode 'fundamental-mode) "txt")
(t (try (let ((result (e/chomp (s-replace-regexp "^\." "" (scrape "\\.[a-z0-9A-Z]+" (car (rassq m auto-mode-alist)))))))
(setq result (cond ((string-equal result "pythonrc") "py")
(t result)))
(if (called-interactively-p)
(message result)
result))
"txt"))))
(defalias 'get-path-ext-from-mode-alist 'get-ext-for-mode)
(provide 'pen-support)