-
Notifications
You must be signed in to change notification settings - Fork 3
/
ess-r-insert-obj.el
476 lines (405 loc) · 18.6 KB
/
ess-r-insert-obj.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
;;; ess-r-insert-obj.el --- Insert objects in ESS-R -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2020 Shuguang Sun <shuguang79@qq.com>
;; Author: Shuguang Sun <shuguang79@qq.com>
;; Created: 2019/04/06
;; Version: 1.0
;; URL: https://github.com/ShuguangSun/ess-r-insert-obj
;; Package-Requires: ((emacs "26.1") (ess "18.10.1"))
;; Keywords: tools
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package contains utilities to help insert variable (column) names or
;; values in ESS-R. The listing is as follows.
;; ## Utils:
;; To help insert Data.frame-like object:
;; - M-x ess-r-insert-obj-dt-name
;; To help insert Variable (Column) name: with `C-u C-u`, it prompts for the dt
;; name to search in.
;; - M-x ess-r-insert-obj-col-name
;; - M-x ess-r-insert-obj-col-name-all
;; To help insert values: with `C-u C-u`, it prompts for the dt name to search
;; in, or with `C-u`, it prompts for column/variable name to search in.
;; - M-x ess-r-insert-obj-value
;; - M-x ess-r-insert-obj-value-all
;; ## Customization
;; ### ess-r-insert-obj-complete-backend-list
;; - jsonlite
;; ### ess-r-insert-obj-read-string
;; - ess-completing-read (default)
;; - completing-read
;; - ido-completing-read
;; - ivy-completing-read
;;; Code:
(eval-when-compile (require 'cl-lib))
(eval-when-compile (require 'cl-generic))
(require 'ess-inf)
(require 'ess-rdired)
(require 'ess-r-mode)
(require 'ess-r-completion)
(require 'subr-x)
(require 'json)
(defvar ess-r-insert-obj-complete-backend-list
(list 'jsonlite)
"List of backends to read completion list.")
(defcustom ess-r-insert-obj-current-complete-backend 'jsonlite
"The backend to complete data.
From R data to Emacs list."
:type `(choice ,@(mapcar (lambda (x)
`(const :tag ,(symbol-name x) ,x))
ess-r-insert-obj-complete-backend-list)
(symbol :tag "Other"))
:group 'ess-r-insert-obj)
(defcustom ess-r-insert-obj-read-string 'ess-completing-read
"The function used to completing read."
:type `(choice (const :tag "ESS" ess-completing-read)
(const :tag "basic" completing-read)
(const :tag "ido" ido-completing-read)
(const :tag "ivy" ivy-completing-read)
(function :tag "Other"))
:group 'ess-r-insert-obj)
(defvar-local ess-r-insert-obj-object nil
"The candidate for completion.")
(defvar-local ess-r-insert-obj-dt-candidate nil
"The candidate for completion from which dt.")
(defvar-local ess-r-insert-obj-col-candidate nil
"The candidate for completion from which column (variable).")
(defvar-local ess-r-insert-obj-candidate nil
"The candidate for completion.")
(cl-defgeneric ess-r-insert-obj-do-complete-data (backend str)
"Completing input.
Argument BACKEND Backend to dispatch, i.e.,
the `ess-r-insert-obj-current-complete-backend'.
Argument STR R script to run.")
;;; jsonlite
(cl-defmethod ess-r-insert-obj-do-complete-data ((_backend (eql jsonlite)) &optional dataframe)
"To get the list for completing in data frame.
Optional argument DATAFRAME name of data.frame-like object."
(let ((cmd
(concat
"jsonlite::toJSON("
(format "c(list(%1$s = names(%1$s)), lapply(%1$s, function(x) as.character(unique(x))))"
(or dataframe ess-r-insert-obj-object))
")\n")))
(json-read-from-string (ess-string-command cmd))))
;;; Utility
(defun ess-r-insert-obj-get-objects ()
"Get the list of data.frame-like objects (is.list) for completion."
(let* ((call1 "ls(.GlobalEnv)[c(sapply(ls(.GlobalEnv), function(x) {is.list(eval(parse(text = x)))}))]")
(cmd (concat call1 "\n")))
(setq ess-r-insert-obj-dt-candidate (ess-get-words-from-vector cmd))
ess-r-insert-obj-dt-candidate))
(defun ess-r-insert-obj-set-object ()
"Set the object for completion."
(interactive "P")
(unless (and ;; (string= "R" ess-dialect)
ess-local-process-name)
(user-error "Not in an R buffer with attached process"))
(let* ((buf (current-buffer))
(proc-name (buffer-local-value 'ess-local-process-name buf))
(proc (get-process proc-name))
(objs (ess-r-insert-obj-get-objects)))
(when objs
;; (ess-read-object-name "obj" )
(setq ess-r-insert-obj-object
(funcall ess-r-insert-obj-read-string "Object:" objs nil t))
(when (and proc-name proc
(not (process-get proc 'busy)))
(setq ess-r-insert-obj-candidate
(ess-r-insert-obj-do-complete-data ess-r-insert-obj-current-complete-backend))))))
(defun ess-r-insert-obj--previous-complete-object (prop)
"Search for the object.
Argument PROP text property, i.e., dt-insert, col-insert."
(let (prop-value)
(while (progn
(goto-char (previous-single-char-property-change (point) prop))
(not (or (setq prop-value (get-text-property (point) prop))
(eobp)
(bobp)))))
prop-value))
;;;###autoload
(defun ess-r-insert-obj-dt-name ()
"Insert name of data.frame-like object."
(interactive)
(unless (and ;; (string= "R" ess-dialect)
ess-local-process-name)
(user-error "Not in an R buffer with attached process"))
(let* ((possible-completions (ess-r-get-rcompletions))
(token-string (or (car possible-completions) ""))
(start (- (point) (length token-string)))
(end (point))
(buf (current-buffer))
(proc-name (buffer-local-value 'ess-local-process-name buf))
(proc (get-process proc-name))
(objs (ess-r-insert-obj-get-objects))
dt-insert)
(setq ess-r-insert-obj-object
(funcall ess-r-insert-obj-read-string
"data.frame: " objs
nil t token-string))
(when (and proc-name proc
(not (process-get proc 'busy)))
(setq ess-r-insert-obj-candidate
(ess-r-insert-obj-do-complete-data ess-r-insert-obj-current-complete-backend)))
(setq dt-insert ess-r-insert-obj-object)
(delete-region start end)
;; propertize
(insert (propertize dt-insert 'dt-insert dt-insert))))
;;;###autoload
(defun ess-r-insert-obj-col-name ()
"Insert column/variable name.
If called with a prefix, prompt for a data.frame-like object to search in.
With two \\[universal-argument] prefixes (i.e., when `current-prefix-arg' is (16)),
prompt for a data.frame-like object to search in."
(interactive)
(unless (and ;; (string= "R" ess-dialect)
ess-local-process-name)
(user-error "Not in an R buffer with attached process"))
(let* ((buf (current-buffer))
(proc-name (buffer-local-value 'ess-local-process-name buf))
(proc (get-process proc-name))
dt-insert)
(when (or (equal current-prefix-arg '(16))
(null (save-excursion
(save-restriction
(setq dt-insert (ess-r-insert-obj--previous-complete-object 'dt-insert))))))
;; force refresh
(let ((objs (ess-r-insert-obj-get-objects)))
(setq ess-r-insert-obj-object
(funcall ess-r-insert-obj-read-string
"data.frame: " objs
nil t))
(when (and proc-name proc
(not (process-get proc 'busy)))
(setq ess-r-insert-obj-candidate
(ess-r-insert-obj-do-complete-data ess-r-insert-obj-current-complete-backend))))
(setq dt-insert ess-r-insert-obj-object))
(when dt-insert
(let ((objs (append
(if (assq (intern dt-insert)
ess-r-insert-obj-candidate)
(alist-get (intern dt-insert)
ess-r-insert-obj-candidate)
(alist-get (intern (replace-regexp-in-string
"`" "" dt-insert))
ess-r-insert-obj-candidate))
nil))
(obj " ")
objs2
obj-list)
(if current-prefix-arg
(progn
(while (not (equal obj ""))
(setq obj (funcall ess-r-insert-obj-read-string
(format "Column (%s), C-j to finish"
(mapconcat 'identity
(setq objs2 (nreverse objs2))
", "))
objs))
(unless (equal obj "")
(setq objs (delete obj objs))
(cl-pushnew obj obj-list)
(cl-pushnew obj objs2)))
(unless (null obj-list)
(insert (propertize (mapconcat 'identity (delete-dups (nreverse obj-list)) ", ")
'dt-insert dt-insert))))
(let* ((possible-completions (ess-r-get-rcompletions))
(token-string (or (car possible-completions) ""))
(start (- (point) (length token-string)))
(end (point))
com)
(setq com
(funcall ess-r-insert-obj-read-string
"Column: " objs
nil t token-string))
(delete-region start end)
(insert (propertize com 'dt-insert dt-insert))))))))
;;;###autoload
(defun ess-r-insert-obj-col-name-all ()
"Insert names of all column/variable name.
If called with a prefix, prompt for a data.frame-like object to search in.
With two \\[universal-argument] prefixes (i.e., when `current-prefix-arg' is (16)),
prompt for a data.frame-like object to search in."
(interactive)
(unless (and ;; (string= "R" ess-dialect)
ess-local-process-name)
(user-error "Not in an R buffer with attached process"))
(let* ((buf (current-buffer))
(proc-name (buffer-local-value 'ess-local-process-name buf))
(proc (get-process proc-name))
dt-insert)
(when (or (equal current-prefix-arg '(16))
(null (save-excursion
(save-restriction
(setq dt-insert (ess-r-insert-obj--previous-complete-object 'dt-insert))))))
;; force refresh
(let ((objs (ess-r-insert-obj-get-objects)))
(setq ess-r-insert-obj-object
(funcall ess-r-insert-obj-read-string
"data.frame: " objs
nil t))
(when (and proc-name proc
(not (process-get proc 'busy)))
(setq ess-r-insert-obj-candidate
(ess-r-insert-obj-do-complete-data ess-r-insert-obj-current-complete-backend))))
(setq dt-insert ess-r-insert-obj-object))
(when dt-insert
(let* ((obj-list (append
(if (assq (intern ess-r-insert-obj-object)
ess-r-insert-obj-candidate)
(alist-get (intern ess-r-insert-obj-object)
ess-r-insert-obj-candidate)
(alist-get (intern (replace-regexp-in-string
"`" "" ess-r-insert-obj-object))
ess-r-insert-obj-candidate))
nil)))
(insert (propertize (mapconcat 'identity
(delete-dups obj-list) ", ")
'dt-insert dt-insert))))))
;;;###autoload
(defun ess-r-insert-obj-value ()
"Insert variable value.
If called with a prefix, prompt for a data.frame-like object or
column/variable to search in.
With a \\[universal-argument] prefix (i.e., when `current-prefix-arg' is (4)),
prompt for a column/variable object to search in.
With two \\[universal-argument] prefixes (i.e., when `current-prefix-arg' is (16)),
prompt for a data.frame-like object to search in."
(interactive)
(unless (and ;; (string= "R" ess-dialect)
ess-local-process-name)
(user-error "Not in an R buffer with attached process"))
(let* ((buf (current-buffer))
(proc-name (buffer-local-value 'ess-local-process-name buf))
(proc (get-process proc-name))
dt-insert
col-insert)
(when (or (equal current-prefix-arg '(16))
(null (save-excursion
(save-restriction
(setq dt-insert (ess-r-insert-obj--previous-complete-object 'dt-insert))))))
(let* ((objs (ess-r-insert-obj-get-objects)))
(setq ess-r-insert-obj-object
(funcall ess-r-insert-obj-read-string
"data.frame: " objs
nil t))
(when (and proc-name proc
(not (process-get proc 'busy)))
(setq ess-r-insert-obj-candidate
(ess-r-insert-obj-do-complete-data ess-r-insert-obj-current-complete-backend))))
(setq dt-insert ess-r-insert-obj-object))
(when (or current-prefix-arg
(and dt-insert
(null (save-excursion
(save-restriction
(setq col-insert (ess-r-insert-obj--previous-complete-object 'col-insert)))))))
(setq col-insert
(funcall ess-r-insert-obj-read-string
"Column: "
(append
(if (assq (intern dt-insert)
ess-r-insert-obj-candidate)
(alist-get (intern dt-insert)
ess-r-insert-obj-candidate)
(alist-get (intern (replace-regexp-in-string
"`" "" dt-insert))
ess-r-insert-obj-candidate))
nil)
nil t)))
(when (and dt-insert col-insert)
(let* ((possible-completions (ess-r-get-rcompletions))
(token-string (or (car possible-completions) ""))
(start (- (point) (length token-string)))
(end (point))
com)
(setq com
(funcall ess-r-insert-obj-read-string
"Value: "
(delq nil (delete-dups (append
(if (assq (intern col-insert)
ess-r-insert-obj-candidate)
(alist-get (intern col-insert)
ess-r-insert-obj-candidate)
(alist-get (intern (replace-regexp-in-string
"`" "" col-insert))
ess-r-insert-obj-candidate))
nil)))
nil t token-string))
(delete-region start end)
(insert (propertize (format "\"%s\"" com)
'dt-insert dt-insert
'col-insert col-insert))))))
;;;###autoload
(defun ess-r-insert-obj-value-all ()
"Insert all variable values.
If called with a prefix, prompt for a data.frame-like object or
column/variable to search in.
With a \\[universal-argument] prefix (i.e., when `current-prefix-arg' is (4)),
prompt for a column/variable object to search in.
With two \\[universal-argument] prefixes (i.e., when `current-prefix-arg' is (16)),
prompt for a data.frame-like object to search in."
(interactive)
(unless (and ;; (string= "R" ess-dialect)
ess-local-process-name)
(user-error "Not in an R buffer with attached process"))
(let* ((buf (current-buffer))
(proc-name (buffer-local-value 'ess-local-process-name buf))
(proc (get-process proc-name))
dt-insert
col-insert)
(when (or (equal current-prefix-arg '(16))
(null (save-excursion
(save-restriction
(setq dt-insert (ess-r-insert-obj--previous-complete-object 'dt-insert))))))
(let* ((objs (ess-r-insert-obj-get-objects)))
(setq ess-r-insert-obj-object
(funcall ess-r-insert-obj-read-string
"data.frame: " objs
nil t))
(when (and proc-name proc
(not (process-get proc 'busy)))
(setq ess-r-insert-obj-candidate
(ess-r-insert-obj-do-complete-data ess-r-insert-obj-current-complete-backend))))
(setq dt-insert ess-r-insert-obj-object))
(when (or current-prefix-arg
(and dt-insert
(null (save-excursion
(save-restriction
(setq col-insert (ess-r-insert-obj--previous-complete-object 'col-insert)))))))
(setq col-insert
(funcall ess-r-insert-obj-read-string
"Column: "
(append
(if (assq (intern dt-insert)
ess-r-insert-obj-candidate)
(alist-get (intern dt-insert)
ess-r-insert-obj-candidate)
(alist-get (intern (replace-regexp-in-string
"`" "" dt-insert))
ess-r-insert-obj-candidate))
nil)
nil t)))
(when (and dt-insert col-insert)
(let* ((obj-list (append
(if (assq (intern col-insert)
ess-r-insert-obj-candidate)
(alist-get (intern col-insert)
ess-r-insert-obj-candidate)
(alist-get (intern (replace-regexp-in-string
"`" "" col-insert))
ess-r-insert-obj-candidate))
nil)))
(insert (propertize (mapconcat 'identity
(delete-dups obj-list) ", ")
'dt-insert dt-insert
'col-insert col-insert))))))
(provide 'ess-r-insert-obj)
;;; ess-r-insert-obj.el ends here