-
Notifications
You must be signed in to change notification settings - Fork 1
/
r-org-utils.el
566 lines (490 loc) · 17 KB
/
r-org-utils.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
;;; r-org-utils.el --- Utilities for working with R, org-mode and ess.
;; Author: Fred Gruber <fgruber@email.com>
;; Created: 2023-07-16
;; Updated: 2023-12-09
;; Version: 0.1
;; Package-Requires: ((emacs "28.0") (ess "18.10") (org "9.5.5"))
;; This package provides utilities to work with R, orgmode, and ess~
;;; Code:
(defvar r-org-utils-jupyter-prefix "jupyter-repl[python 3.11.4]-")
(defvar r-org-utils-show_plot_next_to_r_process nil)
(defvar r-org-utils-python-function-default "print")
;;(defvar r-org-utils-fit-page-docview-only nil)
(defun r-org-utils-plot-region-or-paragraph(&optional arg)
"execute region or paragraph and save tmp plot to pdf. Then open windows to show pdf"
(interactive)
(let* (
(fname (concat (make-temp-file "plot_") ".pdf"))
(width (or arg 7))
)
(progn
(if (use-region-p)
(ess-eval-linewise (add-pdf-to-rcode (buffer-substring (region-beginning) (region-end)) fname width width))
(progn (ess-eval-linewise (add-pdf-to-rcode (thing-at-point 'paragraph) fname width width)))
)
;; (with-help-window "*plots*"
;; (find-ssfile-at-point)
;; )
;;(sleep-for 0.1)
(ess-wait-for-process)
(if r-org-utils-show_plot_next_to_r_process
(ess-switch-to-end-of-ESS)
)
(if (window-in-direction 'right)
(progn
(select-window (window-in-direction 'right))
(find-file fname)
)
(progn
(split-window-right)
(select-window (window-in-direction 'right))
(find-file fname)
)
)
;;(split-window-right)
;;(windmove-right)
(sleep-for 0.1)
(if (equal major-mode 'doc-view-mode)
(doc-view-fit-page-to-window)
)
)
)
)
;;(define-key ess-mode-map (kbd "C-c g") 'r-org-utils-plot-region-or-paragraph)
(define-key ess-mode-map (kbd "C-M-g") 'r-org-utils-plot-region-or-paragraph)
(defun add-pdf-to-rcode(rcomm fname &optional width height)
"add pdf(tmpfile) and dev.off() to R command"
(let* (
(newc (concat "pdf('" fname "',width=" (number-to-string width) ",height=" (number-to-string height) ")\n" rcomm "\n dev.off()"))
)
(eval newc)
)
)
;;(add-pdf-to-rcode "asdf" ";" 7 7)
(defun r-org-utils-convert-saveRDS-to-readRDS ()
"Converts saveRDS string in kill ring readRDS."
(interactive)
(let ((orig-str (current-kill 0 t));;(buffer-substring (region-beginning) (region-end))
(new-str ""))
(if (string-match "saveRDS(\\(.*\\),.*file=\\(.*[.]RDS.*\\))" orig-str)
(progn
(setq new-str (concat "if (!exists(\"" (match-string 1 orig-str) "\"))\n"
" " (match-string 1 orig-str) " = readRDS(" (match-string 2 orig-str) ")"))
(insert new-str))
(message "Selected string is not in the correct format."))))
(defun r-org-utils-convert-save-to-load ()
"Converts selected save string to load format."
(interactive)
(let ((orig-str (current-kill 0 t));;(buffer-substring (region-beginning) (region-end))
(new-str ""))
(if (string-match "save(\\(.*\\),.*file.*=\\(.*\\))" orig-str)
(progn
(setq new-str (concat "if (!exists(\"" (match-string 1 orig-str) "\"))\n"
" load(\"" (match-string 2 orig-str) "\")"))
(insert new-str))
(message "String in kill ring is not in the correct format."))))
(defun toggle-true-false ()
"Toggle the word at point between TRUE and FALSE."
(interactive)
(let ((bounds (bounds-of-thing-at-point 'word)))
(when bounds
(let* ((word (buffer-substring-no-properties (car bounds) (cdr bounds)))
(replacement (if (string= word "TRUE") "FALSE" "TRUE")))
(delete-region (car bounds) (cdr bounds))
(insert replacement)))))
(define-key ess-mode-map (kbd "C-c t") 'toggle-true-false)
(define-key org-mode-map (kbd "C-c t") 'toggle-true-false)
(defun toggle-true-false-python ()
"Toggle the word at point between TRUE and FALSE."
(interactive)
(let ((bounds (bounds-of-thing-at-point 'word)))
(when bounds
(let* ((word (buffer-substring-no-properties (car bounds) (cdr bounds)))
(replacement (if (string= word "True") "False" "True")))
(delete-region (car bounds) (cdr bounds))
(insert replacement)))))
(define-key python-mode-map (kbd "C-c t") 'toggle-true-false-python)
(define-key elpy-mode-map (kbd "C-c t") 'toggle-true-false-python)
(defun r-org-utils-copy-last-output-line ()
(interactive)
(save-window-excursion
(ess-switch-to-end-of-ESS)
(comint-previous-prompt 1)
(move-end-of-line 1)
(forward-char 1)
(push-mark)
(move-end-of-line 1)
(kill-ring-save (region-beginning) (region-end))
)
(comment-indent)
(yank)
)
(define-key ess-mode-map (kbd "C-c l") 'r-org-utils-copy-last-output-line)
(defun r-org-utils-copy-last-output-line-python ()
(interactive)
(save-window-excursion
(elpy-shell-switch-to-shell)
(comint-previous-prompt 1)
(move-end-of-line 1)
(forward-char 1)
(push-mark)
(move-end-of-line 1)
(kill-ring-save (region-beginning) (region-end))
)
(comment-indent)
(yank)
)
(define-key elpy-mode-map (kbd "C-c l") 'r-org-utils-copy-last-output-line-python)
(defun r-org-utils-elpy-eval-string (fun)
(interactive)
(save-window-excursion
(elpy-shell-switch-to-shell)
(end-of-buffer)
(insert fun)
(comint-send-input)
)
)
(defun r-org-utils-add-print (func)
"enclose line in print(). This is useful in python orgmode blocks"
(interactive (list (read-string "Function to add: " r-org-utils-python-function-default)))
(setq r-org-utils-python-function-default func)
(save-excursion
(beginning-of-line-text)
(insert (concat func "("))
(end-of-line)
(insert ")")
)
)
(define-key python-mode-map (kbd "C-c r") 'r-org-utils-add-print)
(defun r-org-utils-evaluate-r-function (func &optional arg)
"Add an R-style function call around the word at point to the kill ring, and evaluate it in the default R session. Use C-u to bring back the first line of the output and put it in comment. Use C-u C-u to used %$% instead of %>%"
(interactive "sEnter R function name: \np")
(let ((word (thing-at-point 'symbol)))
(if word
(let (
(text (format "%s %%>%% %s" word func))
(text2 (format "%s %%$%% %s" word func))
)
(if (equal arg 16)
(progn
(message "arg==16")
(kill-new text2)
(ess-eval-linewise text2)
)
(progn
(kill-new text)
(ess-eval-linewise text)
)
)
(if (equal arg 4)
(progn
;;(sleep-for 0.1)
(ess-wait-for-process)
(r-org-utils-copy-last-output-line)
)
)
)
(message "No word at point")
)
)
)
(define-key ess-mode-map (kbd "C-c f") 'r-org-utils-evaluate-r-function)
(defun r-org-utils-evaluate-python-function (func &optional arg)
"Add a function call around the word at point to the kill ring, and evaluate it in the default R session. Use C-u to bring back the first line of the output and put it in comment. Use C-u C-u to used %$% instead of %>%-"
(interactive "sEnter python function name: \np")
(let ((word (thing-at-point 'symbol)))
(if word
(let (
(text2 (format "%s(%s)" func word))
(text (format "%s.%s" word func))
)
(if (equal arg 16)
(progn
;;(message "arg==16")
;;(kill-new text2)
(r-org-utils-elpy-eval-string text2)
)
(progn
;;(kill-new text)
(r-org-utils-elpy-eval-string text)
)
)
(if (equal arg 4)
(progn
(sleep-for 0.2)
;;(message "arg==4")
(r-org-utils-copy-last-output-line-python)
)
)
)
(message "No word at point")
)
)
)
(define-key elpy-mode-map (kbd "C-c f") 'r-org-utils-evaluate-python-function)
(define-key python-mode-map (kbd "C-c f") 'r-org-utils-evaluate-python-function)
(defvar session_name "*R*")
(defun set-ess-R-process()
"set the process to session stored in session_name variable"
(when (string= "R" lang)
(message "Setting R session to %s" session_name)
(setq ess-local-process-name (process-name (get-buffer-process session_name)))
)
(when (string= "python" lang)
(if (string= session_name "none")
(setq session_name "Global")
)
(message "Setting python session to %s" session_name)
(r-org-utils-elpy-shell-set-local-shell2 session_name)
(if (bound-and-true-p ob-reticulate-mode)
(progn
(message "in reticulate mode activating`' repl")
;; (python-shell-send-string "repl_python()")
;;(r-org-utils-elpy-eval-string "repl_python()")
(save-window-excursion
(elpy-shell-switch-to-shell)
(end-of-buffer)
(insert "repl_python()")
;;(comint-send-input)
(inferior-ess-send-input)
(inferior-python-mode)
)
;;(python-shell-send-string python-shell-eval-setup-code)
;;(r-org-utils-elpy-eval-string python-shell-eval-setup-code)
;;(inferior-ess-send-input)
;;(ess-eval-linewise "repl_python()")
;;(r-org-utils-elpy-eval-string "repl_python()")
;; (save-window-excursion
;; (elpy-shell-switch-to-shell)
;; (python-remote)
;; )
)
)
;;(setq python-shell-buffer-name session_name)
)
(when (string= "jupyter-python" lang)
(message "Setting python session to %s" session_name)
(r-org-utils-elpy-shell-set-local-shell2 session_name)
;;(setq python-shell-buffer-name session_name)
)
)
(defun get-org-current-rsession()
"When you are in an org file get the current R session based on global header, subtree, property or source code :session property whichever is relevant. Save it to variable session_name"
(interactive)
(let*
((mylist (org-babel-get-src-block-info))
(prop (nth 2 mylist))
)
(progn
(setq lang (nth 0 mylist))
(when (string= "R" lang)
(setq session_name (cdr (assq :session prop)))
(message "R session name: %s" session_name)
(set-other-window-func session_name)
)
(when (string= "python" lang)
(setq session_name (cdr (assq :session prop)))
(message "Setting other window to Python session name: %s" session_name)
(set-other-window-func session_name)
;; (if (bound-and-true-p ob-reticulate-mode)
;; (progn
;; (org-babel-execute:R
;; "repl_python()"
;; params
;; )
;; (org-babel-execute:R
;; (python-shell-eval-setup-code)
;; nil
;; )
;; )
;; )
)
(when (string= "jupyter-python" lang)
(setq session_name (cdr (assq :session prop)))
(setq session_name (concat r-org-utils-jupyter-prefix session_name))
(message "Jupyter session name: %s" session_name)
(set-other-window-func session_name)
)
)
)
)
(advice-add 'org-edit-special :before #'get-org-current-rsession)
(advice-add 'org-edit-special :after #'set-ess-R-process)
(defun reticulate-cleanup-after()
"if ob-reticulate is active exit python repl"
(if (bound-and-true-p ob-reticulate-mode)
(progn
(advice-add
#'org-babel-execute:python :around #'ob-reticulate-advice)
)
)
)
(defun reticulate-cleanup-before()
"if ob-reticulate is active exit python repl"
(if (bound-and-true-p ob-reticulate-mode)
(progn
(if (string="python" lang)
(progn
(message "exiting python repl")
(save-window-excursion
(elpy-shell-switch-to-shell)
(inferior-ess-r-mode)
(setq comint-output-filter-functions '(ansi-color-process-output comint-postoutput-scroll-to-bottom comint-watch-for-password-prompt))
(insert "exit")
(comint-send-input)
)
;;(r-org-utils-elpy-eval-string "exit")
;;(python-shell-send-string "exit")
)
)
)
)
)
(advice-add 'org-edit-src-exit :before #'reticulate-cleanup-before)
;;(advice-remove 'org-edit-src-exit #'reticulate-cleanup-before)
;;(advice-add 'org-edit-src-exit :after #'reticulate-cleanup-after)
;;(advice-remove 'org-edit-src-exit #'reticulate-cleanup-after)
(defun set-other-window-func(&optional arg)
"set the other window"
(unless arg (setq arg nil))
;;(message "arg=%s" arg)
(if arg
(setq other-window-scroll-buffer
(get-buffer arg))
(progn
(let*
((window_names (mapcar (lambda (w) (buffer-name (window-buffer w)))
(window-list)))
(cwindow (list (buffer-name (window-buffer))))
(windows_names_filt (cl-set-difference window_names cwindow))
)
(progn
(message "windows_names_filt: %s" windows_names_filt)
(setq other-window-scroll-buffer
(get-buffer (completing-read "Select other window: " windows_names_filt)))
)
)
)
)
)
(defun set-other-window()
"set other windows to selected window"
(interactive)
(set-other-window-func)
)
(defun automatic-other-window-selection()
"Return the default behavior of other window"
(interactive)
(setq other-window-scroll-buffer nil)
)
(defun r-org-utils-set-other-to-ess-process()
"set the other window to the R process associated to ess buffer"
(interactive)
(let*
(
(cprocess ess-local-process-name)
)
(progn
(message "Associated buffer is %s" cprocess)
(set-other-window-func (process-buffer (get-process cprocess)))
)
)
)
(advice-add 'ess-switch-process :after #'r-org-utils-set-other-to-ess-process)
(defun add-exists-condition-above ()
(interactive)
(save-excursion
(let ((word (thing-at-point 'symbol))) ;; Get the word at point
(beginning-of-line-text) ;; Move to the beginning of the line
(open-line 1)
(insert (format "if(!exists(\"%s\"))" word))
(forward-line 1) ;; Move to the line above
(indent-according-to-mode) ;; indent
)
)
)
(defun get-python-inferior-buffers ()
"Get a list of buffer names with a python inferior session."
(let (buffer-list)
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(when (eq major-mode 'inferior-python-mode)
(push (buffer-name buffer) buffer-list))))
buffer-list))
(defun r-org-utils-elpy-shell-set-local-shell2 (&optional shell-name)
"Associate the current buffer to a specific shell.
small modification of elpy-shell-set-local-shell function in elpy. Removed prefix in shell name.
Meaning that the code from the current buffer will be sent to this shell.
If SHELL-NAME is not specified, ask with completion for a shell name.
If SHELL-NAME is \"Global\", associate the current buffer to the main python
shell (often \"*Python*\" shell)."
(interactive)
(let* ((current-shell-name (if (local-variable-p 'python-shell-buffer-name)
(progn
(string-match "\\(.*?\\)"
python-shell-buffer-name)
(match-string 1 python-shell-buffer-name))
"Global"))
(shell-names (get-python-inferior-buffers))
;; (shell-names (cl-loop
;; for buffer in (buffer-list)
;; for buffer-name = (substring-no-properties (buffer-name buffer))
;; if (string-match "\\*.*py\\[\\(.*?\\)\\]\\*" buffer-name)
;; collect (match-string 1 buffer-name)))
(candidates (remove current-shell-name
(delete-dups
(append (list (buffer-name) "Global")
shell-names))))
(prompt (format "Shell name (current: %s): " current-shell-name))
(shell-name (or shell-name (completing-read prompt candidates)))
(shell-name2 (replace-regexp-in-string "\\*" "" shell-name))
)
(if (string= shell-name2 "Global")
(kill-local-variable 'python-shell-buffer-name)
(setq-local python-shell-buffer-name (format "%s" shell-name2)))
)
)
(define-key python-mode-map (kbd "C-c C-s") 'r-org-utils-elpy-shell-set-local-shell2)
(define-key elpy-mode-map (kbd "C-c C-s") 'r-org-utils-elpy-shell-set-local-shell2)
(define-key python-mode-map (kbd "C-c s") 'elpy-rgrep-symbol)
(define-key elpy-mode-map (kbd "C-c s") 'elpy-rgrep-symbol)
(defun python-remote()
(interactive)
"convert python running on a shell into an python inferior so you can use elpy"
(end-of-buffer)
(insert python-shell-eval-setup-code)
(comint-send-input)
(comint-send-input)
(end-of-buffer)
(insert python-shell-eval-file-setup-code)
(comint-send-input)
(comint-send-input)
(end-of-buffer)
(inferior-python-mode)
)
(define-skeleton insertpipe_python
"insert R pipe operator"
""
" >> "
)
(define-key python-mode-map (kbd "C-.") 'insertpipe_python)
(define-key elpy-mode-map (kbd "C-.") 'insertpipe_python)
(define-key inferior-python-mode-map (kbd "C-.") 'insertpipe_python)
(define-skeleton insertpipe
"insert R pipe operator"
""
" %>% "
)
(define-key ess-mode-map (kbd "C-.") 'insertpipe)
(define-key inferior-ess-mode-map (kbd "C-.") 'insertpipe)
(define-key minibuffer-mode-map (kbd "C-.") 'insertpipe)
;;(global-set-key (kbd "C-.") 'insertpipe)
(define-skeleton insertpipe2
"insert R pipe operator"
""
" %$% "
)
(define-key ess-mode-map (kbd "C-,") 'insertpipe2)
(define-key inferior-ess-mode-map (kbd "C-,") 'insertpipe2)
(provide 'r-org-utils)
;;; r-org-utils.el ends here