forked from myuhe/auto-complete-acr.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanything-R.el
230 lines (192 loc) · 9.16 KB
/
anything-R.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
;;; anything-R.el --- anything-sources and some utilities for GNU R.
;; Copyright (C) 2010 myuhe <yuhei.maeda_at_gmail.com>
;; Author: <yuhei.maeda_at_gmail.com>
;; Keywords: convenience,anything, GNU R
;; 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, or (at your option)
;; any later version.
;; This file 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; It is necessary to Some Anything and ESS Configurations for using R before
;;; Installation:
;;
;; Put the anything-R.el, anything.el and ESS to your
;; load-path.
;; Add to .emacs:
;; (require 'anything-R)
;;
;;; Command:
;; `anything-for-R'
;; Anything sources defined :
;; `anything-c-source-R-help' (manage help function)
;; `anything-c-source-R-local' (manage object))
;; `anything-c-source-R-localpkg' (manage local packages)
;; `anything-c-source-R-repospkg' (manage repository packages)
;;; Code:
(require 'anything)
(require 'ess-site)
(defvar anything-R-default-limit
anything-candidate-number-limit)
(defvar anything-R-help-limit
anything-R-default-limit)
(defvar anything-R-local-limit
anything-R-default-limit)
(defvar anything-R-localpkg-limit
anything-R-default-limit)
(defvar anything-R-repospkg-limit
anything-R-default-limit)
(defun anything-R-cmd-head10 (obj-name)
(ess-execute (concat "head(" obj-name ", n = 10)\n") nil (concat "R head: " obj-name)))
(defun anything-R-cmd-head100 (obj-name)
(ess-execute (concat "head(" obj-name ", n = 100)\n") nil (concat "R head: " obj-name)))
(defun anything-R-cmd-tail (obj-name)
(ess-execute (concat "tail(" obj-name ", n = 10)\n") nil (concat "R tail: " obj-name)))
(defun anything-R-cmd-str (obj-name)
(ess-execute (concat "str(" obj-name ")\n") nil (concat "R str: " obj-name)))
(defun anything-R-cmd-summary (obj-name)
(ess-execute (concat "summary(" obj-name ")\n") nil (concat "R summary: " obj-name)))
(defun anything-R-cmd-print (obj-name)
(ess-execute (concat "print(" obj-name ")\n") nil (concat "R object: " obj-name)))
(defun anything-R-cmd-dput (obj-name)
(ess-execute (concat "dput(" obj-name ")\n") nil (concat "R dput: " obj-name)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;anything-c-source-R-help
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq anything-c-source-R-help
`((name . "R objects / help")
(init . (lambda ()
(ess-force-buffer-current "Process to load into: ")
(let ((ess-proc ess-local-process-name))
(if ess-proc
(condition-case nil
(with-current-buffer (anything-candidate-buffer 'local)
(insert
(mapconcat 'identity (ess-get-object-list ess-proc) "\n"))))
(error nil)))))
(candidates-in-buffer)
(candidate-number-limit . ,anything-R-help-limit)
(action
("help" . ess-display-help-on-object)
("head (10)" . anything-R-cmd-head10)
("head (100)" . anything-R-cmd-head100)
("tail" . anything-R-cmd-tail)
("str" . anything-R-cmd-str)
("summary" . anything-R-cmd-summary)
("view source" . anything-R-cmd-print)
("dput" . anything-R-cmd-dput))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; anything-c-source-R-local
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq anything-c-source-R-local
`((name . "R local objects")
(init . (lambda ()
;; this grabs the process name associated with the buffer
(setq anything-c-ess-local-process-name ess-local-process-name)
;; this grabs the buffer for later use
(setq anything-c-ess-buffer (current-buffer))))
(candidates . (lambda ()
(let (buf)
(condition-case nil
(with-temp-buffer
(progn
(setq buf (current-buffer))
(with-current-buffer anything-c-ess-buffer
(ess-command "print(ls.str(all.names = TRUE), max.level=0)\n" buf))
(split-string (buffer-string) "\n" t)))
(error nil)))))
(candidate-number-limit . ,anything-R-local-limit)
(display-to-real . (lambda (obj-name) (car (split-string obj-name " : " t))))
(action
("str" . anything-R-cmd-str)
("summary" . anything-R-cmd-summary)
("head (10)" . anything-R-cmd-head10)
("head (100)" . anything-R-cmd-head100)
("tail" . anything-R-cmd-tail)
("print" . anything-R-cmd-print)
("dput" . anything-R-cmd-dput))
(volatile)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; func for action
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun anything-ess-marked-install (candidate)
(dolist (i (anything-marked-candidates))
(ess-execute (concat "install.packages(\"" i "\")\n") t)))
(defun anything-ess-marked-remove (candidate)
(dolist (i (anything-marked-candidates))
(ess-execute (concat "remove.packages(\"" i "\")\n") t)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; anything-c-source-R-localpkg
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq anything-c-source-R-localpkg
`((name . "R-local-packages")
(init . (lambda ()
;; this grabs the process name associated with the buffer
(setq anything-c-ess-local-process-name ess-local-process-name)
;; this grabs the buffer for later use
(setq anything-c-ess-buffer (current-buffer))))
(candidates . (lambda ()
(let (buf)
(condition-case nil
(with-temp-buffer
(progn
(setq buf (current-buffer))
(with-current-buffer anything-c-ess-buffer
(ess-command "writeLines(paste('', sort(.packages(all.available=TRUE)), sep=''))\n" buf))
(split-string (buffer-string) "\n" t)))
(error nil)))))
(candidate-number-limit . ,anything-R-localpkg-limit)
(action
("load packages" . (lambda(obj-name)
(ess-execute (concat "library(" obj-name ")\n") t )))
("remove packages" . (lambda(obj-name)
(ess-execute (concat "remove.packages(\"" obj-name "\")\n") t)))
("remove marked packages" . anything-ess-marked-remove))
(volatile)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; anything-c-source-R-repospkg
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq anything-c-source-R-repospkg
`((name . "R-repos-packages")
(init . (lambda ()
;; this grabs the process name associated with the buffer
(setq anything-c-ess-local-process-name ess-local-process-name)
;; this grabs the buffer for later use
(setq anything-c-ess-buffer (current-buffer))))
(candidates . (lambda ()
(let (buf)
(condition-case nil
(with-temp-buffer
(progn
(setq buf (current-buffer))
(with-current-buffer anything-c-ess-buffer
(ess-command "writeLines(paste('', rownames(available.packages(contriburl=contrib.url(\"http://cran.md.tsukuba.ac.jp/\"))), sep=''))\n" buf))
;; (ess-command "writeLines(paste('', sort(.packages(all.available=TRUE)), sep=''))\n" buf))
(split-string (buffer-string) "\n" t)))
(error nil)))))
(candidate-number-limit . ,anything-R-repospkg-limit)
(action
("install packages" . (lambda(obj-name)
(ess-execute (concat "install.packages(\"" obj-name "\")\n") t)))
("install marked packages" . anything-ess-marked-install))
(volatile)))
(defcustom anything-for-R-list '(anything-c-source-R-help
anything-c-source-R-local
anything-c-source-R-repospkg
anything-c-source-R-localpkg)
"Your prefered sources to GNU R."
:type 'list
:group 'anything-R)
(defun anything-for-R ()
"Preconfigured `anything' for GNU R."
(interactive)
(anything-other-buffer anything-for-R-list "*anything for GNU R*"))
(provide 'anything-R)