-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathess.el
297 lines (248 loc) · 8.82 KB
/
ess.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
;;; ess.el --- R setup -*- lexical-binding: t; -*-
;; setup projectile
(with-eval-after-load 'projectile
(add-to-list 'projectile-project-root-files "DESCRIPTION"))
;;; main ess setup
(use-package ess
:mode (("\\.[rR]\\'" . ess-r-mode))
:commands R ess-r-mode
:init (require 'ess-site)
:config
(setq ess-offset-continued 'straight
ess-nuke-trailing-whitespace-p t
ess-style 'RStudio
ess-eval-visibly 'nowait
ess-use-flymake nil)
;; most people say this is annoying -- I need to figure out what it is eventually
(setq ess-smart-S-assign-key nil)
;; ess don't ask for startup directory
(setq ess-ask-for-ess-directory nil
ess-startup-directory 'default-directory)
(add-hook 'ess-r-mode-hook #'display-fill-column-indicator-mode)
;; from here: https://github.com/SteveLane/dot-emacs/blob/master/ess-config.el
;; Add in company-mode helpers
;; (defun my-ess-company-hook ()
;; ;; ensure company-R-library is in ESS backends
;; (make-variable-buffer-local 'company-backends)
;; (cl-delete-if (lambda (x) (and (eq (car-safe x) 'company-R-args))) company-backends)
;; (add-to-list 'company-backends
;; '(company-R-args company-R-objects company-R-library
;; company-dabbrev-code :separate)))
;; (add-hook 'ess-mode-hook #'my-ess-company-hook)
;;
;; (defun jds~ess-capf-hook ()
;; ;; ensure company-R-library is in ESS backends
;; (make-variable-buffer-local 'completion-at-point-functions)
;; (add-to-list 'company-backends (mapcar #'cape-company-to-capf
;; '(#'company-R-args
;; #'company-R-objects
;; #'company-R-library)))
;; (add-to-list 'company-backends
;; '(company-R-args company-R-objects company-R-library
;; company-dabbrev-code :separate)))
;; (add-hook 'ess-mode-hook #'my-ess-company-hook)
;; this also makes heading section headers available in consult-outline
;; (add-hook 'ess-mode-hook
;; '(lambda ()
;; (outline-minor-mode)
;; (setq outline-regexp ".*----$")))
;; turn off fancy comments which are really annoying
(defun dont-like-fancy ()
(setcdr (assoc 'ess-indent-with-fancy-comments (cdr (assoc 'DEFAULT ess-style-alist))) nil))
(add-hook 'ess-mode-hook 'dont-like-fancy)
(add-hook 'ess-r-mode-hook 'dont-like-fancy)
;; add dumb-jump as the default xref-backend
(with-eval-after-load 'dumb-jump
(add-hook 'ess-r-mode-hook
(lambda ()
;; (add-hook 'xref-backend-functions #'eglot-xref-backend -100 'local)
(add-hook 'xref-backend-functions #'dumb-jump-xref-activate -90 'local)
)))
;; better display-buffer defaulsts
;; (add-to-list 'display-buffer-alist '("^\\*R Dired"
;; (display-buffer-reuse-window display-buffer-in-side-window)
;; (side . right)
;; (slot . -1)
;; (window-width . 0.33)
;; (reusable-frames . nil)))
;; (add-to-list 'display-buffer-alist '("^\\*R"
;; (display-buffer-reuse-window display-buffer-in-side-window)
;; (side . right)
;; (slot . -1)
;; (window-width . 0.5)
;; (reusable-frames . nil)))
;; ;; (add-to-list 'display-buffer-alist '("^\\*Help"
;; ;; (display-buffer-reuse-window display-buffer-in-side-window)
;; ;; (side . right)
;; ;; (slot . 1)
;; ;; (window-width . 0.33)
;; ;; (reusable-frames . nil)))
;; (add-to-list 'display-buffer-alist '("^R_x11"
;; (display-buffer-reuse-window display-buffer-in-side-window)
;; (side . right)
;; (slot . 1)
;; (window-width . 0.33)
;; (reusable-frames . nil)))
;; dont ask for startup directory just starup in wherever the script is.
;; (setq ess-startup-directory nil
;; ess-ask-for-ess-directory nil)
)
;;; setup polymode
(use-package markdown-mode
:mode
(("README\\.md\\'" . gfm-mode)
("\\.md\\'" . markdown-mode)
("\\.markdown\\'" . markdown-mode))
:init
(setq markdown-command "markdown"))
(use-package polymode
:ensure markdown-mode
:ensure poly-R
:ensure poly-noweb
:config
(add-to-list 'auto-mode-alist '("\\.Rnw" . poly-noweb+r-mode))
(add-to-list 'auto-mode-alist '("\\.rnw" . poly-noweb+r-mode))
(add-to-list 'auto-mode-alist '("\\.Rmd" . poly-markdown+r-mode)))
(use-package poly-markdown
:ensure polymode
:defer t)
;; poly-R
(use-package poly-R
:ensure polymode
:ensure poly-markdown
:ensure poly-noweb
:defer t)
;; Add yaml to markdown an .yml files
(use-package yaml-mode
:mode (("\\.yml\\'" . yaml-mode)))
;;; ess-breakerofchains
(use-package ess-breakerofchains
:straight (ess-breakerofchains
:type git :host github :repo "jsilve24/ess-breakerofchains"))
(general-define-key
:keymaps 'ess-r-mode-map
:states '(insert normal)
"<C-S-return>" #'ess-boc-break-chain)
;;; ess add library import
(defun jds/ess-add-library-import (library-name)
"Add a 'library' import statement for LIBRARY-NAME to the current R script buffer.
The function searches backwards from the end of the buffer
for the regular expression '^library(.*)$', which matches a
'library' import statement. If a match is found, the function
moves the point to the end of the matched line and inserts a
newline and a 'library' import statement for LIBRARY-NAME.
If no match is found, the function opens a new line above the
first line in the buffer and inserts a 'library' import statement
for LIBRARY-NAME.
The function raises an error if the current buffer is not an
R script buffer (i.e. the major mode is not `ess-r-mode`)."
(interactive "sLibrary name: ")
(if (eq major-mode 'ess-r-mode)
(let ((library-import-regexp "^library(.*)$")
(proc (ess-get-process))
(fstring (format "library(%s)\n" library-name)))
(save-excursion
(goto-char (point-max))
(if (re-search-backward library-import-regexp nil t)
(progn
(goto-char (match-beginning 0))
(match-string-no-properties 0)
(end-of-line)
(newline)
(insert fstring))
(goto-char (point-min))
(beginning-of-line)
(newline)
(forward-line -1)
(insert fstring)))
(if proc
(ess-send-string proc fstring)))
(error "Buffer is not in ess-r-mode")))
;;; key bindings
(general-define-key
:keymaps 'inferior-ess-mode-map
"C-l" #'comint-clear-buffer)
(jds/localleader-def
:keymaps '(ess-mode-map inferior-ess-mode-map)
"<tab>" '(ess-switch-to-inferior-or-script-buffer :wk "REPL-script switch")
"vs" #'r/df-sample-small
"vm" #'r/df-sample-medium
"vl" #'r/df-sample-large
"l" #'jds/ess-add-library-import
;; predefined keymaps
"h" 'ess-doc-map
"x" 'ess-extra-map
"p" 'ess-r-package-dev-map
"q" 'ess-dev-map
"oo" #'ess-roxy-update-entry
"oc" #'ess-roxy-toggle-roxy-region
"ot" #'ess-roxy-preview-text
"ow" #'ess-roxy-preview-HTML
"on" #'ess-roxy-next-entry
"op" #'ess-roxy-previous-entry
"oh" #'ess-roxy-hide-all
"cc" #'r/clear-environment)
(general-define-key
:keymaps '(polymode-mode-map markdown-mode-map)
"C-S-i" #'r/rmd-insert-chunk)
(jds/localleader-def
:keymaps '(polymode-mode-map markdown-mode-map)
"rc" #'r/rmd-insert-chunk
"rr" #'r/rmd-render
"m" #'r/rmd-render
"rd" #'r/draft-rmd
"re" #'polymode-eval-chunk
"rn" #'polymode-next-chunk)
(jds/localleader-def
:keymaps 'ess-mode-map
"'" #'org-edit-src-exit
"k" #'org-edit-src-abort)
(general-define-key
:keymaps 'ess-mode-map
:states 'i
[C-return] #'ess-eval-line-and-step)
(general-define-key
:keymaps 'ess-mode-map
:states '(n m)
[C-return] #'ess-eval-function-or-paragraph-and-step)
(general-define-key
:keymaps 'ess-mode-map
:states 'v
;; leave point where it was originally
[C-return] #'(lambda (start end vis) (interactive "r\nP")
(let* ((mark (mark)))
(ess-eval-region start end vis)
(goto-char mark))))
;; (map! (:after ess-help
;; (:map ess-help-mode-map
;; :n "q" #'kill-current-buffer
;; :n "Q" #'ess-kill-buffer-and-go
;; :n "K" #'ess-display-help-on-object
;; :n "go" #'ess-display-help-in-browser
;; :n "gO" #'ess-display-help-apropos
;; :n "gv" #'ess-display-vignettes
;; :m "]]" #'ess-skip-to-next-section
;; :m "[[" #'ess-skip-to-previous-section)
;; (:map ess-doc-map
;; "h" #'ess-display-help-on-object
;; "p" #'ess-R-dv-pprint
;; "t" #'ess-R-dv-ctable
;; [up] #'comint-next-input
;; [down] #'comint-previous-input
;; :i [C-return] #'ess-eval-line-and-step
;; :nom [C-return] #'ess-eval-function-or-paragraph-and-step
;; :v [C-return] #'ess-eval-region))
;; (:after ess
;; :map ess-mode-map
;; :localleader
;; "'" #'R
;; [backtab] #'ess-switch-process
;; ;; noweb
;; :prefix "c"
;; "C" #'ess-eval-chunk-and-go
;; "c" #'ess-eval-chunk
;; "d" #'ess-eval-chunk-and-step
;; "m" #'ess-noweb-mark-chunk
;; "p" #'ess-noweb-previous-chunk
;; "n" #'ess-noweb-next-chunk)))
(provide 'config-ess)