Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
2701 views
1
;;; ess-developer.el --- Developer mode for R.
2
3
;; Copyright (C) 2011-2012 V. Spinu, A.J. Rossini, Richard M. Heiberger, Martin
4
;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
5
6
;; Author: Vitalie Spinu
7
;; Created: 12-11-2011
8
;; Maintainer: ESS-core <[email protected]>
9
10
;; Keywords: languages, tools
11
12
;; This file is part of ESS.
13
14
;; This file is free software; you can redistribute it and/or modify
15
;; it under the terms of the GNU General Public License as published by
16
;; the Free Software Foundation; either version 2, or (at your option)
17
;; any later version.
18
19
;; This file is distributed in the hope that it will be useful,
20
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
;; GNU General Public License for more details.
23
24
;; A copy of the GNU General Public License is available at
25
;; http://www.r-project.org/Licenses/
26
27
;;; Commentary:
28
29
;; see apropriate documentation section of ESS user manual
30
31
;;; Code:
32
33
;; (require 'ess-site) ;; need to assigne the keys in the map
34
35
(defgroup ess-developer nil
36
"ESS: developer."
37
:group 'ess
38
:prefix "ess-developer-")
39
40
(defface ess-developer-indicator-face
41
'((((class grayscale)) (:background "DimGray"))
42
(((class color) (background light))
43
(:foreground "red4" :bold t ))
44
(((class color) (background dark))
45
(:foreground "deep sky blue" :bold t )))
46
"Face to highlight mode line process name when developer mode is on."
47
:group 'ess-developer)
48
49
(defcustom ess-developer-packages nil
50
"List of names of R packages you develop.
51
Use `ess-developer-add-package' to modify interactively this
52
list. "
53
:group 'ess-developer
54
:type 'list)
55
56
(defcustom ess-developer-load-package-command "library(devtools)\nload_all('%s')\n"
57
"Command issued by `ess-developer-load-package'.
58
%s is subsituted with the user supplied directory."
59
:group 'ess-developer
60
:type 'string)
61
62
(defvar ess-developer-root-file "DESCRIPTION"
63
"If this file is present in the directory, it is considered a
64
project root.")
65
66
;; (defcustom ess-developer-force-attach nil
67
;; "If non-nill all the packages listed in `ess-developer-packages' should be attached
68
;; when ess-developer mode is turned on."
69
;; :group 'ess-developer
70
;; :type 'boolean)
71
72
(defcustom ess-developer-enter-hook nil
73
"Normal hook run on entering `ess-developer' mode."
74
:group 'ess-developer
75
:type 'hook)
76
77
(defcustom ess-developer-exit-hook nil
78
"Normal hook run on exiting `ess-developer' mode."
79
:group 'ess-developer
80
:type 'hook)
81
82
(defcustom ess-developer-activate-in-package t
83
"If non-nil, ess-developer is automatically toggled in files
84
within package directory."
85
:group 'ess-developer
86
:type 'boolean)
87
88
(defcustom ess-developer-load-on-add-commands '(("library" . "library(%n)")
89
("load_all" . "library(devtools)\nload_all('%d')"))
90
"Alist of available load commands what are proposed for loading
91
on `ess-developer-add-package'.
92
93
%n is replaced with package name,
94
%d is replaced with package directory.
95
96
See also `ess-developer-load-package' for related functionality."
97
:group 'ess-developer
98
:type 'alist)
99
100
(defvar ess-developer--load-hist nil)
101
102
(defun ess-developer-add-package (&optional attached-only)
103
"Add a package to `ess-developer-packages' list.
104
With prefix argument only choose from among attached packages."
105
(interactive "P")
106
(ess-force-buffer-current)
107
(let* ((packs (ess-get-words-from-vector
108
(format "print(unique(c(.packages(), %s)), max=1e6)\n"
109
(if attached-only "NULL" ".packages(TRUE)") nil t)))
110
(cur-pack (ess-developer--get-package-name))
111
(sel (ess-completing-read "Add package" packs nil nil nil nil
112
(unless (member cur-pack ess-developer-packages)
113
cur-pack)))
114
(check-attached (format ".ess_package_attached('%s')\n" sel)))
115
(unless (ess-boolean-command check-attached)
116
(let* ((fn (if (> (length ess-developer-load-on-add-commands) 1)
117
(ess-completing-read "Package not loaded. Use"
118
(mapcar 'car ess-developer-load-on-add-commands) nil t
119
nil 'ess-developer--load-hist
120
(car ess-developer--load-hist))
121
(caar ess-developer-load-on-add-commands)))
122
(cmd (cdr (assoc fn ess-developer-load-on-add-commands))))
123
(setq cmd (replace-regexp-in-string "%n" sel cmd))
124
(when (string-match-p "%d" cmd)
125
(let ((dir (read-directory-name
126
"Package: " (ess-developer--get-package-path sel) nil t nil)))
127
(setq cmd (replace-regexp-in-string "%d" dir cmd))))
128
(ess-eval-linewise (concat cmd "\n")))
129
(ess-wait-for-process)
130
(when (not (ess-boolean-command check-attached))
131
(error "Package '%s' could not be added" sel)))
132
(setq ess-developer-packages
133
(ess-uniq-list (append ess-developer-packages (list sel))))
134
;; turn developer in all files from selected package
135
(ess-developer-activate-in-package sel 'all)
136
(message "You are developing: %s" ess-developer-packages)))
137
138
(defun ess-developer-remove-package ()
139
"Remove packages from `ess-developer-packages' list; defaults to *ALL*."
140
(interactive)
141
(unless ess-developer-packages
142
(error "Nothing to remove, 'ess-developer-packages' is empty"))
143
(let ((sel (ess-completing-read "Remove package(s)"
144
(append ess-developer-packages (list "*ALL*"))
145
nil t nil nil "*ALL*")))
146
(if (equal "*ALL*" sel)
147
(progn
148
(setq ess-developer-packages nil)
149
(ess-developer-deactivate-in-package nil 'all)
150
(message "Removed *ALL* packages from the `ess-developer-packages' list."))
151
(setq ess-developer-packages (delete sel ess-developer-packages))
152
(ess-developer-deactivate-in-package sel 'all)
153
(message "Removed package '%s' from the `ess-developer-packages' list"
154
(propertize sel 'face 'font-lock-function-name-face)))))
155
156
(defun ess-developer-send-region-fallback (proc beg end visibly &optional message tracebug func)
157
(if tracebug
158
(ess-tracebug-send-region proc beg end visibly message t)
159
(ess-send-region proc beg end visibly message)))
160
161
(defun ess-developer-source-current-file (&optional filename)
162
"Ask for namespace to source the current file into.
163
If *current* is selected just invoke source('file_name'),
164
otherwise call devSource."
165
(interactive)
166
(ess-force-buffer-current "R process to use: ")
167
(unless ess-developer
168
(error "Ess-developer mode is not active"))
169
(if (not (or filename
170
buffer-file-name))
171
(error "Buffer '%s' doesn't visit a file" (buffer-name (current-buffer)))
172
(let* ((filename (or filename buffer-file-name))
173
(file (file-name-nondirectory filename))
174
(env (ess-completing-read (format "devSource '%s' into" file)
175
(append ess-developer-packages (list "*current*" )) nil t))
176
(comm (if (equal env "*current*")
177
(format "source(file=\"%s\", local=F)\n cat(\"Sourced file '%s' into\", capture.output(environment()), '\n')" filename file)
178
(format ".essDev_source(source='%s',package='%s')" filename env))))
179
(when (buffer-modified-p) (save-buffer))
180
(message "devSourcing '%s' ..." file)
181
(ess-developer--command comm 'ess-developer--propertize-output))))
182
183
(defun ess-developer-send-function (proc beg end name &optional visibly message tracebug)
184
(save-excursion
185
(if (null ess-developer-packages)
186
(error "`ess-developer-packages' is empty (add packages with C-c C-t C-a).")
187
(if (null name)
188
(error "Oops, could not find function name (probably a regexp bug)")
189
(let ((nms (ess-get-words-from-vector "loadedNamespaces()\n"))
190
(dev-packs ess-developer-packages)
191
assigned-p ns)
192
;; such a kludge
193
(if (string-match-p ess-set-function-start (concat name "("))
194
(ess-developer-send-region proc beg end visibly message tracebug)
195
(if tracebug (ess-tracebug-set-last-input proc))
196
(while (and (setq ns (pop dev-packs))
197
(not assigned-p))
198
(when (and (member ns nms) ;;todo: try to load the package if not loaded
199
(ess-boolean-command
200
(format "as.character(exists('%s', envir=asNamespace('%s'), mode='function', inherits=FALSE))\n"
201
name ns)))
202
(ess-developer-devSource beg end ns message)
203
(setq assigned-p t)))
204
(unless assigned-p
205
(ess-developer-send-region-fallback proc beg end visibly message tracebug))))))))
206
207
(defvar ess-developer--hist nil)
208
209
(defun ess-developer-send-region (proc beg end &optional visibly message tracebug)
210
"Ask for for the package and devSource region into it."
211
(let* ((all-packs (append ess-developer-packages (list "*current*" )))
212
(default (car (member (car ess-developer--hist) all-packs)))
213
(package
214
(ess-completing-read "devEval into" all-packs
215
nil t nil 'ess-developer--hist default)))
216
(message (if message (format "dev%s ..." message)))
217
(if (equal package "*current*")
218
(ess-developer-send-region-fallback proc beg end visibly message tracebug)
219
;; else, (ignore VISIBLY here)
220
(ess-developer-devSource beg end package message))))
221
222
(defun ess-developer-devSource (beg end package &optional message)
223
(let* ((ess-eval-command
224
(format ".essDev.eval(\"%s\", package=\"%s\", file=\"%s\")" "%s" package "%f"))
225
(ess-eval-visibly-command ess-eval-command)
226
(ess-eval-visibly-noecho-command ess-eval-command))
227
(if message (message message))
228
(ess-developer--command (ess--make-source-refd-command beg end)
229
'ess-developer--propertize-output)))
230
231
(defun ess-developer--command (comm &optional propertize-func)
232
"Evaluate the command and popup a message with the output if succed.
233
On error insert the error at the end of the inferior-ess buffer.
234
235
PROPERTIZE-FUNC is a function called with the output buffer being
236
current. usually used to manipulate the output, for example to
237
propertize output text.
238
"
239
(setq comm (format "eval({cat(\"\\n\")\n%s\ncat(\"!@OK@!\")})\n" comm))
240
(let ((buff (get-buffer-create " *ess-command-output*"))
241
out)
242
(ess-command comm buff nil nil 0.1)
243
(with-current-buffer buff
244
(goto-char (point-min))
245
(delete-region (point) (min (point-max) ;; delete + + +
246
(1+ (point-at-eol))))
247
(goto-char (point-max))
248
(if (re-search-backward "!@OK@!" nil t)
249
(progn
250
(when (fboundp propertize-func)
251
(save-excursion (funcall propertize-func)))
252
(message "%s" (buffer-substring (point-min) (max (point-min)
253
(1- (point))))))
254
(message "%s" (buffer-substring-no-properties (point-min) (point-max)))))))
255
256
(defun ess-developer--propertize-output ()
257
(goto-char (point-min))
258
(while (re-search-forward "\\(FUN\\|CLS\\METH\\)\\[" nil t)
259
(put-text-property (match-beginning 1) (match-end 1)
260
'face 'font-lock-function-name-face))
261
(goto-char (point-min))
262
(while (re-search-forward "\\([^ \t]+\\):" nil t)
263
(put-text-property (match-beginning 1) (match-end 1)
264
'face 'font-lock-keyword-face)))
265
266
(defvar ess-developer--pack-name nil)
267
(make-variable-buffer-local 'ess-developer--pack-name)
268
269
(defun ess-developer--get-package-path (&optional pack-name)
270
"Get the root of R package that contains current directory.
271
Root is determined by locating `ess-developer-root-file'.
272
273
If PACK-NAME is given, iterate over default-directories of all
274
open R files till package with name pack-name is found. If not
275
found, return nil."
276
(if pack-name
277
(let ((bl (buffer-list))
278
path bf)
279
(while (and (setq bf (pop bl))
280
(not path))
281
(when (buffer-local-value 'ess-dialect bf)
282
(with-current-buffer bf
283
(setq path (ess-developer--get-package-path))
284
(unless ess-developer--pack-name
285
(setq ess-developer--pack-name ;; cache locally
286
(ess-developer--get-package-name path)))
287
(unless (equal ess-developer--pack-name pack-name)
288
(setq path nil)))))
289
path)
290
(let ((path default-directory)
291
opath package)
292
(while (and path
293
(not package)
294
(not (equal path opath)))
295
(if (file-exists-p (expand-file-name ess-developer-root-file path))
296
(setq package path)
297
(setq opath path
298
path (file-name-directory (directory-file-name path)))))
299
package)))
300
301
302
(defun ess-developer--get-package-name (&optional path)
303
"Find package name in path. Parses DESCRIPTION file in PATH (R
304
specific so far). PATH defaults to `default-directory'"
305
(when (setq path (or path (ess-developer--get-package-path)))
306
(let ((file (expand-file-name ess-developer-root-file path))
307
(case-fold-search t))
308
(when (file-exists-p file)
309
(with-temp-buffer
310
(insert-file-contents file)
311
(goto-char (point-min))
312
(re-search-forward "package: \\(.*\\)")
313
(match-string 1))))))
314
315
(defun ess-developer-activate-in-package (&optional package all)
316
"Activate developer if current file is part of a package which
317
is registered in `ess-developer-packages'.
318
319
If PACKAGE is given, activate only if current file is part of the
320
PACKAGE, `ess-developer-packages' is ignored in this case.
321
322
If ALL is non-nil, perform activation in all R buffers.
323
324
This function does nothing if `ess-developer-activate-in-package'
325
is nil. "
326
(when ess-developer-activate-in-package
327
(if all
328
(dolist (bf (buffer-list))
329
(with-current-buffer bf
330
(ess-developer-activate-in-package package)))
331
(let ((pack (ess-developer--get-package-name)))
332
(when (and buffer-file-name
333
pack
334
(not ess-developer)
335
(if package
336
(equal pack package)
337
(member pack ess-developer-packages)))
338
(ess-developer t))))))
339
340
(defun ess-developer-deactivate-in-package (&optional package all)
341
"Deactivate developer if current file is part of the R package.
342
343
If PACKAGE is given, deactivate only if current package is
344
PACKAGE.
345
346
If ALL is non-nil, deactivate in all open R buffers."
347
(if all
348
(dolist (bf (buffer-list))
349
(with-current-buffer bf
350
(ess-developer-deactivate-in-package package)))
351
(let ((pack (ess-developer--get-package-name)))
352
(when (and ess-developer
353
(or (null package)
354
(equal pack package)))
355
(ess-developer -1)))))
356
357
(defun ess-developer-load-package ()
358
"Interface to load_all function from devtools package."
359
(interactive)
360
(ess-force-buffer-current)
361
(let ((package (ess-developer--get-package-path)))
362
(unless (and package ess-developer)
363
;; ask only when not obvious
364
(setq package
365
(read-directory-name "Package: " package nil t nil)))
366
(unless (file-exists-p (expand-file-name ess-developer-root-file package))
367
(error "Not a valid package. No '%s' found in `%s'."
368
ess-developer-root-file package))
369
(message "Loading %s" (abbreviate-file-name package))
370
(ess-eval-linewise
371
(format ess-developer-load-package-command package))))
372
373
(defvar ess-developer nil
374
"Non nil in buffers where developer mode is active")
375
(make-variable-buffer-local 'ess-developer)
376
377
;; Since the ESSR package, this one is not needed:
378
;; (defun ess-developer--inject-source-maybe ()
379
;; ;; puting this into ESSR.R makes loading very slow
380
;; ;; when ESSR is a package, this should go away
381
;; (let ((devR-file (concat (file-name-directory ess-etc-directory)
382
;; "ess-developer.R")))
383
;; (unless (ess-boolean-command
384
;; "exists('.essDev_source', envir = .ESSR_Env)\n")
385
;; (unless (file-exists-p devR-file)
386
;; (error "Cannot locate 'ess-developer.R' file"))
387
;; (message "Injecting ess-developer code ...")
388
;; (ess--inject-code-from-file devR-file)
389
;; (unless (ess-boolean-command "exists('.essDev_source', envir = .ESSR_Env)\n")
390
;; (error "Could not source ess-developer.R. Please investigate the output of *ess-command-output* buffer for errors")))))
391
392
(defun ess-developer (&optional val)
393
"Toggle on/off ess-developer functionality.
394
If optional VAL is non-negative, turn on the developer mode. If
395
VAL is negative turn it off."
396
(interactive)
397
(when (eq val t) (setq val 1))
398
(let ((ess-dev (if (numberp val)
399
(if (< val 0) nil t)
400
(not ess-developer))))
401
(if ess-dev
402
(progn
403
(run-hooks 'ess-developer-enter-hook)
404
(if ess-developer-packages
405
(message "You are developing: %s" ess-developer-packages)
406
(message "Developer is on (add packages with C-c C-t a)")))
407
(run-hooks 'ess-developer-exit-hook)
408
(message "%s developer is off" (if (get-buffer-process (current-buffer))
409
"Global"
410
"Local")))
411
412
(setq ess-developer ess-dev))
413
(force-window-update))
414
415
(defalias 'ess-toggle-developer 'ess-developer)
416
417
418
419
;;; MODELINE
420
421
(defvar ess-developer--local-indicator
422
'(""
423
(:eval
424
;; process has priority
425
(if (and (ess-process-live-p)
426
(ess-get-process-variable 'ess-developer))
427
(propertize " D" 'face 'ess-developer-indicator-face)
428
(if ess-developer
429
(propertize " d" 'face 'ess-developer-indicator-face)
430
"")))))
431
(put 'ess-developer--local-indicator 'risky-local-variable t)
432
433
(defun ess-developer-setup-modeline ()
434
(add-to-list 'ess--local-mode-line-process-indicator
435
'ess-developer--local-indicator 'append))
436
437
438
439
;;; HOOKS
440
441
(add-hook 'R-mode-hook 'ess-developer-activate-in-package)
442
(add-hook 'R-mode-hook 'ess-developer-setup-modeline)
443
(add-hook 'inferior-ess-mode-hook 'ess-developer-setup-modeline)
444
445
(provide 'ess-developer)
446
;;; ess-developer.el ends here
447
448