-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSH.el
More file actions
executable file
·122 lines (111 loc) · 3.34 KB
/
SH.el
File metadata and controls
executable file
·122 lines (111 loc) · 3.34 KB
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
(defun SH-hook ()
(define-key sh-mode-map [(ctrl return)] #'SH-SH)
(define-key sh-mode-map [(meta return)] #'SH-SHnew))
(add-hook 'sh-mode-hook 'SH-hook)
(defun SH-SH (&optional NEW)
"h"
(interactive)
(unless (eq (count-windows) 2)
(delete-other-windows)
(split-window-right))
(let (beg1 end1 toSend1 cb1 p1)
(when (region-active-p)
(set 'beg1 (region-beginning))
(set 'end1 (region-end)))
(unless (region-active-p)
(beginning-of-line)
(set 'beg1 (point))
(end-of-line)
(set 'end1 (point)))
(set 'toSend1 (buffer-substring-no-properties beg1 end1))
(unless (string-suffix-p "\n" toSend1)
(set 'toSend1 (concat toSend1 "\n")))
(set 'cb1 (current-buffer))
(set 'p1 (SH-get-shell-process NEW))
(display-buffer-use-some-window (process-buffer p1) '(("side" . "right")))
(with-current-buffer (process-buffer p1)
(goto-char (point-max))
(insert toSend1)
(set-marker (process-mark p1) (point-max))
(process-send-string p1 toSend1)
(goto-char (point-max)))
(switch-to-buffer cb1)
(ess-next-code-line)))
(defun SH-SHnew ()
"h"
(interactive)
(SH-SH t))
(defun SH-get-shell-process (&optional NEW)
"h"
(interactive)
;; p1 = process to return
;; lShProc = list of shell processes
(let (p1
lShProc)
(when NEW
(set 'p1 (SH-new-shell-process)))
(unless p1
(mapc (lambda (x)
(when (string-match "shell" (process-name x))
(push x lShProc)))
(process-list))
(set 'p1
(car
(sort lShProc #'SH-X-more-recent))))
(unless p1
(set 'p1 (SH-new-shell-process)))
p1))
(defun SH-new-shell-process ()
"h"
(interactive)
(let (p1)
(set 'p1 (get-buffer-process
(shell
(generate-new-buffer "*shell*"))))
p1))
(defun SH-X-more-recent (X Y)
"
Returns t if the `process-buffer' associated with X is higher on the `buffer-list' than that for Y.
That is, X has more recently been active (display or selected)."
(interactive)
(<
(cl-position (process-buffer X) (buffer-list))
(cl-position (process-buffer Y) (buffer-list))))
;; (defun SH-newer-process (x y)
;; "h"
;; (interactive)
;; (time-less-p
;; (cdr (assoc 'start (process-attributes (process-id y))))
;; (cdr (assoc 'start (process-attributes (process-id x))))))
;; function taken from ess package
(defun ess-next-code-line (&optional arg)
"Move ARG lines of code forward (backward if ARG is negative).
Skips past all empty and comment lines. Default for ARG is 1.
On success, return 0. Otherwise, go as far as possible and return -1."
(interactive "p")
(or arg (setq arg 1))
(beginning-of-line)
(let ((n 0)
(inc (if (> arg 0) 1 -1)))
(while (and (/= arg 0) (= n 0))
(setq n (forward-line inc)); n=0 is success
(while (and (= n 0)
(looking-at "\\s-*\\($\\|\\s<\\)"))
(setq n (forward-line inc)))
(setq arg (- arg inc)))
n))
;; (setq shelllist (process-shell))
;; (setq shelln (length shelllist))
;; (if (eq shelln 0)
;; (progn (shell)
;; (switch-to-buffer cbuf)
;; (setq outpr (get-process "shell"))
;; (sleep-for 0.5)))
;; (if (eq shelln 1)
;; (setq outpr (get-process (elt shelllist 0))))
;; (if (> shelln 1)
;; (progn
;; (setq proc (completing-read "Send code to:" shelllist nil t (elt shelllist 0)))
;; (setq outpr (get-process proc))))
;; outpr)
(provide 'SH)