Better handling of w32-shell-execute.
[ess.git] / lisp / ess-mode.el
blobebdcabec353f2b0f0227cbc13347c5cdf3ab2b10
1 ;;; ess-mode.el --- Support for editing ESS source code
3 ;; Copyright (C) 1989-1994 Doug Bates, Ed Kademan, Frank Ritter, David Smith.
4 ;; Copyright (C) 1997--2005 A.J. Rossini, Rich M. Heiberger, Martin
5 ;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
7 ;; Original Author: David Smith <dsmith@stats.adelaide.edu.au>
8 ;; Created: 7 Jan 1994
9 ;; Maintainers: ESS-core <ESS-core@stat.math.ethz.ch>
11 ;; This file is part of ESS
13 ;; This file is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; This file is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;;; Commentary:
29 ;; Code for editing ESS source code.
31 ;;; Code:
33 \f ; Requires and autoloads
35 (require 'ess)
37 ;;; AJR: THIS IS GROSS AND DISGUSTING (but I wrote it).
38 ;;; MM: and I had to add all other 'ess-eval-*** ...
39 ;;; >>> why not just do the obvious instead of all these ? Namely,
40 ;;; (require 'ess-inf)
41 ;;; ------------------ ?
42 ;;; AJR: The reason is that we ONLY need to load ess-inf for the
43 ;;; functions which are interactive in nature. We don't want to load
44 ;;; it when we are only editing.
46 (autoload 'ess-mode-minibuffer-map "ess-inf" "" nil 'keymap)
47 (autoload 'ess-read-object-name "ess-inf" "" nil)
48 (autoload 'ess-list-object-completions "ess-inf" "" nil)
50 (autoload 'ess-eval-linewise "ess-inf" "" nil)
51 (autoload 'ess-eval-paragraph "ess-inf" "" nil)
52 (autoload 'ess-eval-region "ess-inf" "" nil)
53 (autoload 'ess-eval-buffer "ess-inf" "" nil)
54 (autoload 'ess-eval-function "ess-inf" "" nil)
55 (autoload 'ess-eval-line "ess-inf" "" nil)
56 (autoload 'ess-eval-line-and-step "ess-inf" "" nil)
57 (autoload 'ess-eval-region-and-go "ess-inf" "" nil)
58 (autoload 'ess-eval-buffer-and-go "ess-inf" "" nil)
59 (autoload 'ess-eval-function-and-go "ess-inf" "" nil)
60 (autoload 'ess-eval-line-and-go "ess-inf" "" nil)
62 (autoload 'ess-load-file "ess-inf" "" nil)
63 (autoload 'ess-switch-process "ess-inf" "" nil)
64 (autoload 'ess-switch-to-ESS "ess-inf" "" nil)
65 (autoload 'ess-request-a-process "ess-inf" "" nil)
66 (autoload 'get-ess-process "ess-inf" "" nil)
67 (autoload 'ess-command "ess-inf" "" nil)
68 (autoload 'ess-create-temp-buffer "ess-inf" "" nil)
69 (autoload 'ess-display-temp-buffer "ess-inf" "" nil)
70 (autoload 'ess-force-buffer-current "ess-inf" "" nil)
71 (autoload 'ess-make-buffer-current "ess-inf" "" nil)
72 (autoload 'ess-modtime-gt "ess-inf" "" nil)
73 (autoload 'ess-object-modtime "ess-inf" "" nil)
74 (autoload 'ess-quit "ess-inf" "" nil)
77 (defun ess-line-end-position (&optional N)
78 "return the 'point' at the end of N lines. N defaults to 1, i.e., current line."
79 (save-excursion
80 (end-of-line N)
81 (point)))
84 \f ; ESS mode
85 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
86 ;;;; In this section:
87 ;;;;
88 ;;;; * The major mode ess-mode
89 ;;;; * Commands for ess-mode
90 ;;;; * Code evaluation commands
91 ;;;; * Indenting code and commands
92 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 ;;*;; Major mode definition
96 (if ess-eval-map
97 nil
98 (if (featurep 'xemacs)
99 ;; Code for XEmacs
100 (setq ess-eval-map (make-keymap))
101 ;; else code for GNU Emacs
102 (setq ess-eval-map (make-sparse-keymap)))
104 (define-key ess-eval-map "\C-r" 'ess-eval-region)
105 (define-key ess-eval-map "\M-r" 'ess-eval-region-and-go)
106 (define-key ess-eval-map "\C-b" 'ess-eval-buffer)
107 (define-key ess-eval-map "\M-b" 'ess-eval-buffer-and-go)
108 (define-key ess-eval-map "\C-f" 'ess-eval-function)
109 (define-key ess-eval-map "\M-f" 'ess-eval-function-and-go)
110 (define-key ess-eval-map "\C-x" 'ess-eval-function)
111 (define-key ess-eval-map "\C-n" 'ess-eval-line-and-step)
112 (define-key ess-eval-map "\C-j" 'ess-eval-line)
113 (define-key ess-eval-map "\M-j" 'ess-eval-line-and-go))
116 (if ess-mode-map
119 (if (featurep 'xemacs)
120 (progn ;; Code for XEmacs
121 (setq ess-mode-map (make-keymap))
122 (set-keymap-parent ess-mode-map text-mode-map)) ;; was comint?!?
123 ;; else code for GNU Emacs
124 (setq ess-mode-map (make-sparse-keymap)))
126 ;; By popular demand:
127 (define-key ess-mode-map "\C-m" 'newline-and-indent); = [RETURN]
129 (define-key ess-mode-map "\C-c\C-r" 'ess-eval-region)
130 (define-key ess-mode-map "\C-c\M-r" 'ess-eval-region-and-go)
131 (define-key ess-mode-map "\C-c\C-b" 'ess-eval-buffer)
132 (define-key ess-mode-map "\C-c\M-b" 'ess-eval-buffer-and-go)
133 (define-key ess-mode-map "\C-c\C-f" 'ess-eval-function)
134 (define-key ess-mode-map "\C-c\M-f" 'ess-eval-function-and-go)
135 (define-key ess-mode-map "\M-\C-x" 'ess-eval-function)
136 (define-key ess-mode-map "\C-c\C-n" 'ess-eval-line-and-step)
137 (define-key ess-mode-map "\C-c\C-j" 'ess-eval-line)
138 (define-key ess-mode-map "\C-c\M-j" 'ess-eval-line-and-go)
139 ;; the next three can only work in S/R - mode {FIXME}
140 (define-key ess-mode-map "\M-\C-a" 'ess-beginning-of-function)
141 (define-key ess-mode-map "\M-\C-e" 'ess-end-of-function)
142 (define-key ess-mode-map "\C-xnd" 'ess-narrow-to-defun)
143 (define-key ess-mode-map "\C-c\C-y" 'ess-switch-to-ESS)
144 (define-key ess-mode-map "\C-c\C-z" 'ess-switch-to-end-of-ESS)
145 (define-key ess-mode-map "\C-c\C-l" 'ess-load-file)
146 (define-key ess-mode-map "\C-c\C-v" 'ess-display-help-on-object)
147 (define-key ess-mode-map "\C-c\C-d" 'ess-dump-object-into-edit-buffer)
148 ;(define-key ess-mode-map "\C-c5\C-d"'ess-dump-object-into-edit-buffer-other-frame)
149 (define-key ess-mode-map "\C-c\C-s" 'ess-switch-process) ; use a
150 ; different process for the buffer.
151 (define-key ess-mode-map "\C-c\C-t" 'ess-execute-in-tb)
152 (define-key ess-mode-map "\C-c\t" 'ess-complete-object-name)
153 (define-key ess-mode-map "\M-\t" 'comint-replace-by-expanded-filename)
154 (define-key ess-mode-map "\M-?" 'ess-list-object-completions)
155 ;; wrong here (define-key ess-mode-map "\C-c\C-k" 'ess-request-a-process)
156 (define-key ess-mode-map "\C-c\C-k" 'ess-force-buffer-current)
157 (define-key ess-mode-map "\C-c`" 'ess-parse-errors) ; \C-x reserved!
158 (define-key ess-mode-map "{" 'ess-electric-brace)
159 (define-key ess-mode-map "}" 'ess-electric-brace)
160 (define-key ess-mode-map "\e\C-h" 'ess-mark-function)
161 (define-key ess-mode-map "\e\C-q" 'ess-indent-exp)
162 (define-key ess-mode-map "\177" 'backward-delete-char-untabify)
163 (define-key ess-mode-map "\t" 'ess-indent-command)
164 (define-key ess-mode-map "\C-c\C-q" 'ess-quit)
165 (define-key ess-mode-map "\C-c\C-e" ess-eval-map))
167 (require 'noweb-mode)
168 (easy-menu-define
169 ess-mode-menu ess-mode-map
170 "Menu for use in `ess-mode'."
171 '("ESS" ; ESS-mode
172 ["What is this? (beta)" ess-mouse-me t]
173 ["Load file" ess-load-file t]
174 ("Eval and Go"
175 ["Eval buffer" ess-eval-buffer-and-go t]
176 ["Eval region" ess-eval-region-and-go t]
177 ["Eval function" ess-eval-function-and-go t]
178 ["Eval line" ess-eval-line-and-go t]
179 ["Eval chunk" ess-eval-chunk-and-go noweb-mode]
180 ["Eval thread" ess-eval-thread-and-go noweb-mode]
181 ["About" (ess-goto-info "Evaluating code") t]
183 ("ESS Eval"
184 ["Eval buffer" ess-eval-buffer t]
185 ["Eval region" ess-eval-region t]
186 ["Eval function" ess-eval-function t]
187 ["Enter expression" ess-execute-in-tb t]
188 ["Eval line" ess-eval-line t]
189 ["Eval line & step" ess-eval-line-and-step t]
190 ["Eval chunk" ess-eval-chunk noweb-mode]
191 ["Eval thread" ess-eval-thread noweb-mode]
192 ["About" (ess-goto-info "Evaluating code") t]
194 ("Motion..."
195 ["Edit new object" ess-dump-object-into-edit-buffer t]
196 ["Goto end of ESS buffer" ess-switch-to-end-of-ESS t]
197 ["Switch to ESS buffer" ess-switch-to-ESS t]
198 ["Beginning of function" ess-beginning-of-function t]
199 ["End of function" ess-end-of-function t]
201 ("ESS list..."
202 ["Backward list" backward-list t]
203 ["Forward list" forward-list t]
204 ["Next parenthesis" down-list t]
205 ["Enclosing parenthesis" backward-up-list t]
206 ["Backward sexp" backward-sexp t]
207 ["Forward sexp" forward-sexp t]
208 ["About" (Info-goto-node "(Emacs)Lists") t]
210 ("ESS Edit"
211 ["Complete Filename" comint-replace-by-expanded-filename t]
212 ["Complete Object" ess-complete-object-name t]
213 ["Kill sexp" kill-sexp t]
214 ["Mark function" ess-mark-function t]
215 ["Indent expression" ess-indent-exp t]
216 ["Indent line" ess-indent-command t]
217 ["Toggle Auto-Fill Mode" auto-fill-mode t]
218 ["Undo" undo t]
219 ["About" (ess-goto-info "Edit buffer") t]
221 ("Start Process"
222 ;; SJE - :help not yet recognised in XEmacs.
223 ["R" R t] ;; :help "Start a new R process" :active t
224 ["S" S t] ;; :help "Start a new S process" :active t
225 ["Sqpe" Sqpe ess-microsoft-p] ;; :help "Start a new Sqpe process" :active t
226 ["S+6-exisiting" S+6-existing ess-microsoft-p] ;; :help "Access an existing S process" :active t
227 ["SAS" SAS-menu t] ;; :help "Start a new SAS process" :active t
228 ;; The following menu item "Other" is a place-holder that will
229 ;; be replaced with the other versions of R and Sqpe that can be run.
230 ;; See `ess-r-versions-create' and ess-site.el
231 ("Other"
232 ["No other R or Sqpe versions" nil nil])
233 ["About"
234 (ess-goto-info "Starting up") t]
235 ;; :help "Read about starting a new ESS process" :active t]
237 ["Switch Process" ess-switch-process t]
238 "------"
239 ["Describe" describe-mode t]
240 ["About editing" (ess-goto-info "Editing") t]
241 ["Read ESS info" (ess-goto-info "") t]
242 ["Send bug report" ess-submit-bug-report t]
245 (defun SAS-menu ()
246 "Start SAS from the menu."
247 (interactive)
248 (if ess-microsoft-p
249 ;; replace with other choices for starting SAS under XEmacs?
250 (error "SAS cannot be started this way in ESS on Windows.")
251 (SAS)))
253 (defun ess-mode-xemacs-menu ()
254 "Hook to install `ess-mode' menu for XEmacs (w/ easymenu)."
255 (if 'ess-mode
256 (easy-menu-add ess-mode-menu)
257 (easy-menu-remove ess-mode-menu)))
259 (if (featurep 'xemacs)
260 (add-hook 'ess-mode-hook 'ess-mode-xemacs-menu))
262 (defun ess-mode (&optional alist proc-name)
263 "Major mode for editing ESS source.
264 Optional arg ALIST describes how to customize the editing mode.
265 Optional arg PROC-NAME is name of associated inferior process.
267 \\{ess-mode-map}
269 Extra binding to note: 'ESC C-\\' indent-region.
271 Entry to this mode runs the hooks in ess-mode-hook.
273 You can send text to the inferior ESS process from other buffers containing
274 ESS source.
275 `ess-eval-region' sends the current region to the ESS process.
276 `ess-eval-buffer' sends the current buffer to the ESS process.
277 `ess-eval-function' sends the current function to the ESS process.
278 `ess-eval-line' sends the current line to the ESS process.
279 `ess-beginning-of-function' and `ess-end-of-function' move the point to
280 the beginning and end of the current ESS function.
281 `ess-switch-to-ESS' switches the current buffer to the ESS process buffer.
282 `ess-switch-to-end-of-ESS' switches the current buffer to the ESS process
283 buffer and puts point at the end of it.
285 `ess-eval-region-and-go', `ess-eval-buffer-and-go',
286 `ess-eval-function-and-go', and `ess-eval-line-and-go' switch to the S
287 process buffer after sending their text.
289 `ess-load-file' sources a file of commands to the ESS process.
291 \\[ess-indent-command] indents for ESS code.
292 \\[backward-delete-char-untabify] converts tabs to spaces as it moves back.
293 Comments are indented in a similar way to Emacs-lisp mode:
294 `###' beginning of line
295 `##' the same level of indentation as the code
296 `#' the same column on the right, or to the right of such a
297 column if that is not possible.(default value 40).
298 \\[indent-for-comment] command automatically inserts such a
299 `#' in the right place, or aligns such a comment if it is
300 already inserted.
301 \\[ess-indent-exp] command indents each line of the ESS grouping following point.
303 Variables controlling indentation style:
304 `ess-tab-always-indent'
305 Non-nil means TAB in ESS mode should always reindent the current line,
306 regardless of where in the line point is when the TAB command is used.
307 `ess-auto-newline'
308 Non-nil means automatically newline before and after braces inserted in S
309 code.
310 `ess-indent-level'
311 Indentation of ESS statements within surrounding block.
312 The surrounding block's indentation is the indentation of the line on
313 which the open-brace appears.
314 `ess-continued-statement-offset'
315 Extra indentation given to a substatement, such as the then-clause of an
316 if or body of a while.
317 `ess-continued-brace-offset'
318 Extra indentation given to a brace that starts a substatement.
319 This is in addition to ess-continued-statement-offset.
320 `ess-brace-offset'
321 Extra indentation for line if it starts with an open brace.
322 `ess-arg-function-offset'
323 Extra indent for internal substatements of function `foo' that called
324 in `arg=foo(...)' form.
325 If not number, the statements are indented at open-parenthesis following
326 `foo'.
327 `ess-expression-offset'
328 Extra indent for internal substatements of `expression' that specified
329 in `obj <- expression(...)' form.
330 If not number, the statements are indented at open-parenthesis following
331 `expression'.
332 `ess-brace-imaginary-offset'
333 An open brace following other text is treated as if it were
334 this far to the right of the start of its line.
335 `ess-else-offset'
336 Extra indentation for line if it starts with `else'.
337 `ess-close-brace-offset'
338 Extra indentation for closing braces.
339 `ess-fancy-comments'
340 Non-nil means distinguish between #, ##, and ### for indentation.
342 Furthermore, \\[ess-set-style] command enables you to set up predefined ess-mode
343 indentation style. At present, predefined style are `BSD', `GNU', `K&R', `C++',
344 `CLB' (quoted from C language style)."
345 (interactive)
346 (kill-all-local-variables) ;; NOTICE THIS!
347 (ess-setq-vars-local alist)
348 (ess-write-to-dribble-buffer
349 (format "(ess-mode-1): ess-language=%s, ess-dialect=%s buf=%s \n"
350 ess-language
351 ess-dialect
352 (current-buffer)))
353 ;; (ess-write-to-dribble-buffer
354 ;; (format "(ess-mode-1.2): ess-process= %s \n"
355 ;; (ess-local-process-name ess-local-process-name "none")))
356 (ess-write-to-dribble-buffer
357 (format "(ess-mode-1.5): alist=%s \n"
358 alist))
359 (setq major-mode 'ess-mode)
360 (setq mode-name (concat "ESS[" ess-language "]")) ; was ess-dialect
361 ;; The following line does the next 20 or so :-).
362 (ess-write-to-dribble-buffer
363 (format "(ess-mode-1.6): editing-alist=%s \n"
364 ess-mode-editing-alist))
365 (ess-setq-vars-local ess-mode-editing-alist)
367 (use-local-map ess-mode-map)
368 (set-syntax-table ess-mode-syntax-table)
370 ;; Keep <tabs> out of the code.
371 (make-local-variable 'indent-tabs-mode)
372 (setq indent-tabs-mode nil)
374 ;; (make-local-variable 'paragraph-start)
375 ;; (setq paragraph-start (concat "^$\\|" page-delimiter))
376 ;; (make-local-variable 'paragraph-separate)
377 ;; (setq paragraph-separate paragraph-start)
378 ;; (make-local-variable 'paragraph-ignore-fill-prefix)
379 ;; (setq paragraph-ignore-fill-prefix t)
380 ;; (make-local-variable 'indent-line-function)
381 ;; (setq indent-line-function 'ess-indent-line)
382 ;; (make-local-variable 'require-final-newline)
383 ;; (setq require-final-newline t)
384 ;; (make-local-variable 'comment-start)
385 ;; (setq comment-start "#")
386 ;; (make-local-variable 'comment-start-skip)
387 ;; (setq comment-start-skip "#+ *")
388 ;; (make-local-variable 'comment-column)
389 ;; (setq comment-column 40)
390 ;; (make-local-variable 'comment-indent-function)
391 ;; (setq comment-indent-function 'ess-comment-indent)
392 ;; (make-local-variable 'parse-sexp-ignore-comments)
393 ;; (setq parse-sexp-ignore-comments t)
394 ;; (ess-set-style ess-default-style)
395 ;; (make-local-variable 'ess-local-process-name)
396 ;; (make-local-variable 'ess-keep-dump-files)
397 (put 'ess-local-process-name 'permanent-local t) ; protect from RCS
398 (setq mode-line-process ;; AJR: in future, XEmacs will use modeline-process.
399 '(" [" (ess-local-process-name ess-local-process-name "none") "]"))
400 ;; SJE Tue 28 Dec 2004: do not attempt to load object name db.
401 ;; (ess-load-object-name-db-file)
402 (run-hooks 'ess-mode-hook)
403 (ess-write-to-dribble-buffer "\nFinished setting up ESS-mode.\n"))
405 ;;*;; User commands in ess-mode
407 ;;;*;;; Handy commands
409 (defun ess-execute-in-tb ()
410 "Like `ess-execute', but always evaluates in temp buffer."
411 (interactive)
412 (let ((ess-execute-in-process-buffer nil))
413 (call-interactively 'ess-execute)))
415 ;;;*;;; Buffer motion/manipulation commands
417 (defvar ess-set-function-start
418 ;; setAs, setGeneric; setMethod, setReplaceMethod, setGroupMethod
419 "^set[MGAR][Ma-z]+\\s-?("
422 ;; common R and S
423 (let*
424 ((Q "\\s\"") ; quote
425 (repl "\\(<-\\)?") ; replacement (function)
426 (Sym-0 "\\(\\sw\\|\\s_\\)") ; symbol
427 (Symb (concat Sym-0 "+"))
428 (xSymb (concat "\\[?\\[?" Sym-0 "*")); symbol / [ / [[ / [symbol / [[symbol
429 ;; FIXME: allow '%foo%' but only when quoted; don't allow [_0-9] at beg.
430 (_or_ "\\)\\|\\(") ; OR
431 (space "\\(\\s-\\|\n\\)*") ; white space
433 (part-1 (concat
434 "\\(" ;;--------outer Either-------
435 "\\(\\(" ; EITHER
436 Q xSymb repl Sym-0 "*" Q ; quote ([) (replacement) symbol quote
437 _or_
438 "\\(^\\|[ ]\\)" Symb ; (beginning of name) + Symb
439 "\\)\\)")) ; END EITHER OR
441 (set-S4-exp
442 (concat
443 "^set\\(As\\|Method\\|Generic\\|GroupMethod\\|ReplaceMethod\\)(" ; S4 ...
444 Q xSymb Q "," space
445 ;; and now often `` signature(......), : ''
446 ".*" ;; <<< FIXME ???
449 (part-2 (concat
450 "\\|" ;;--------outer Or ---------
451 set-S4-exp
452 "\\)" ;;--------end outer Either/Or-------
454 "\\(" space "\\s<.*\\s>\\)*" ; whitespace, comment
455 space "function\\s-*(" ; whitespace, function keyword, parenthesis
459 (defvar ess-R-function-pattern
460 (concat part-1
461 "\\s-*\\(<-\\|=\\)" ; whitespace, assign
462 part-2)
463 "The regular expression for matching the beginning of an R function.")
465 (defvar ess-S-function-pattern
466 (concat part-1
467 "\\s-*\\(<-\\|_\\|=\\)" ; whitespace, assign (incl. "_")
468 part-2)
469 "The regular expression for matching the beginning of an S function.")
471 ); {end let}
473 (defun ess-beginning-of-function ()
474 "Leave (and return) the point at the beginning of the current ESS function."
475 (interactive)
476 (let ((init-point (point))
477 (in-set-S4 nil)
478 beg end done)
480 ;; Note that we must be sure that we are past the 'function (' text,
481 ;; such that ess-function-pattern is found in BACKwards later.
482 ;; In case we're sitting in a function or setMethod() header,
483 ;; we need to move further.
484 ;; But not too far! {wrongly getting into next function}
485 (if (search-forward "("
486 (ess-line-end-position 2) t); at most end of next line
487 (forward-char 1))
489 (setq end (point)); = init-point when nothing found
490 (ess-write-to-dribble-buffer
491 (format "ess-BEG-of-fun after 'search-FWD (': Ini-pt %d, (p)-Ini-pt = %d\n"
492 init-point (- end init-point)))
493 (if (re-search-backward ;; in case of setMethod() etc ..
494 ess-set-function-start
495 ;; at most one line earlier {2 is too much: finds previous sometimes}
496 (+ 1 (ess-line-end-position -1)) t)
497 (progn ;; yes we *have* an S4 setMethod(..)-like
498 (setq in-set-S4 t
499 beg (point))
500 (ess-write-to-dribble-buffer
501 (format " set*() function start at position %d" beg))
502 ;; often need to move even further to have 'function(' to our left
503 ;; (if (search-forward "function" end t)
504 ;; (ess-write-to-dribble-buffer
505 ;; (format " -> 'function' already at pos %d\n" (point)))
506 ;; ;; else need to move further
507 (goto-char end)
508 ;; search 4 lines, we are pretty sure now:
509 (search-forward
510 "function" (ess-line-end-position 4) t)
511 ;; )
512 (search-forward "(" (ess-line-end-position) t)
514 ;; else: regular function; no set*Method(..)
515 (ess-write-to-dribble-buffer "ELSE not in setMethod() header ...\n")
518 (while (not done)
519 ;; Need this while loop to skip over local function definitions
521 ;; In the case of non-success, it is inefficient;
522 ;; going back in the buffer through all function definitions...
523 (if (re-search-backward ess-function-pattern (point-min) t)
525 (goto-char init-point)
526 (error "Point is not in a function according to 'ess-function-pattern'."))
527 (setq beg (point))
528 (ess-write-to-dribble-buffer
529 (format "\tMatch,Pt:(%d,%d),%d\n" (match-beginning 0)(match-end 0) beg))
530 (setq in-set-S4 (looking-at ess-set-function-start))
531 (forward-list 1) ; get over arguments
533 ;; The following used to bomb "Unbalanced parentheses", n1, n2
534 ;; when the above (search-forward "(" ..) wasn't delimited :
535 (unless in-set-S4 (forward-sexp 1)) ; move over braces
536 ;;DBG (ess-write-to-dribble-buffer "|")
537 (setq end (point))
538 (goto-char beg)
539 ;; current function must begin and end around point
540 (setq done (and (>= end init-point) (<= beg init-point))))
541 beg))
543 (defun ess-end-of-function (&optional beginning)
544 "Leave the point at the end of the current ESS function.
545 Optional argument for location of beginning. Return '(beg end)."
546 (interactive)
547 (if beginning
548 (goto-char beginning)
549 (setq beginning (ess-beginning-of-function)))
550 ;; *hack* only for S (R || S+): are we in setMethod(..) etc?
551 (let ((in-set-S4 (looking-at ess-set-function-start)))
552 (ess-write-to-dribble-buffer
553 (format "ess-END-of-fun: S4=%s, beginning = %d\n" in-set-S4 beginning))
554 (forward-list 1) ; get over arguments || whole set*(..)
555 (unless in-set-S4 (forward-sexp 1)) ; move over braces
556 ;;DBG (ess-write-to-dribble-buffer "ess-END-of-fun: found ok\n")
557 (list beginning (point))
560 ;;; Kurt's version, suggested 970306.
561 (defun ess-mark-function ()
562 "Put mark at end of ESS function, point at beginning."
563 (interactive)
564 (let ((beg (ess-beginning-of-function)))
565 (push-mark (point))
566 (ess-end-of-function beg)
567 (exchange-point-and-mark)))
569 ;; Donated by Stephen Eglen, 2001-08-29:
570 ;; This command is analogous to `narrow-to-defun' (elisp)
571 ;; and `py-narrow-to-defun' (python)."
572 (defun ess-narrow-to-defun ()
573 "Make text outside current function invisible.
574 If text is already narrowed, this is removed before narrowing to the
575 current function."
576 (interactive)
577 ;; if point is not in a function, ess-end-of-function catches the error.
578 (save-excursion
579 (widen)
580 (let* ((beg-end (ess-end-of-function)))
581 (narrow-to-region (nth 0 beg-end) (nth 1 beg-end)))))
583 ;;*;; Loading files
585 (defun ess-check-modifications nil
586 "Check whether loading this file would overwrite some ESS objects
587 which have been modified more recently than this file, and confirm
588 if this is the case."
589 ;; FIXME: this should really cycle through all top-level assignments in
590 ;; the buffer
591 (and (buffer-file-name) ess-filenames-map
592 (let ((sourcemod (nth 5 (file-attributes (buffer-file-name))))
593 (objname))
594 (save-excursion
595 (goto-char (point-min))
596 ;; Get name of assigned object, if we can find it
597 (setq objname
598 (and
599 (re-search-forward
600 "^\\s *\"?\\(\\(\\sw\\|\\s_\\)+\\)\"?\\s *[<_]"
603 (buffer-substring (match-beginning 1)
604 (match-end 1)))))
605 (and
606 sourcemod ; the file may have been deleted
607 objname ; may not have been able to
608 ; find name
609 (ess-modtime-gt (ess-object-modtime objname) sourcemod)
610 (not (y-or-n-p
612 (format
613 "The ESS object %s is newer than this file. Continue?"
614 objname)))
615 (error "Aborted")))))
617 (defun ess-check-source (fname)
618 "If file FNAME has an unsaved buffer, offer to save it.
619 Returns t if the buffer existed and was modified, but was not saved."
620 (let ((buff (get-file-buffer fname)))
621 ;; RMH: Corrections noted below are needed for C-c C-l to work
622 ;; correctly when issued from *S* buffer.
623 ;; The following barfs since
624 ;; 1. `if' does not accept a buffer argument, `not' does.
625 ;; 2. (buffer-file-name) is not necessarily defined for *S*
626 ;;(if buff
627 ;; (let ((deleted (not (file-exists-p (buffer-file-name)))))
628 ;; Next 2 lines are RMH's solution:
629 (if (not(not buff))
630 (let ((deleted (not (file-exists-p fname))))
631 (if (and deleted (not (buffer-modified-p buff)))
632 ;; Buffer has been silently deleted, so silently save
633 (save-excursion
634 (set-buffer buff)
635 (set-buffer-modified-p t)
636 (save-buffer))
637 (if (and (buffer-modified-p buff)
638 (or ess-mode-silently-save
639 (y-or-n-p
640 (format "Save buffer %s first? "
641 (buffer-name buff)))))
642 (save-excursion
643 (set-buffer buff)
644 (save-buffer))))
645 (buffer-modified-p buff)))))
647 (defun ess-parse-errors (showerr)
648 "Jump to error in last loaded ESS source file.
649 With prefix argument, only shows the errors ESS reported."
650 (interactive "P")
651 (ess-make-buffer-current)
652 (let ((errbuff (get-buffer ess-error-buffer-name)))
653 (if (not errbuff)
654 (error "You need to do a load first!")
655 (set-buffer errbuff)
656 (goto-char (point-max))
658 (re-search-backward
659 "^\\(Syntax error: .*\\) at line \\([0-9]*\\), file \\(.*\\)$"
662 (let* ((filename (buffer-substring (match-beginning 3) (match-end 3)))
663 (fbuffer (get-file-buffer filename))
664 (linenum
665 (string-to-number
666 (buffer-substring (match-beginning 2) (match-end 2))))
667 (errmess (buffer-substring (match-beginning 1) (match-end 1))))
668 (if showerr
669 (ess-display-temp-buffer errbuff)
670 (if fbuffer nil
671 (setq fbuffer (find-file-noselect filename))
672 (save-excursion
673 (set-buffer fbuffer)
674 (ess-mode)))
675 (pop-to-buffer fbuffer)
676 (goto-line linenum))
677 (princ errmess t))
678 (message "Not a syntax error.")
679 (ess-display-temp-buffer errbuff)))))
681 ;;*;; ESS code formatting/indentation
683 ;;;*;;; User commands
685 (defun ess-electric-brace (arg)
686 "Insert character and correct line's indentation."
687 (interactive "P")
688 (let (insertpos)
689 (if (and (not arg)
690 (eolp)
691 (or (save-excursion
692 (skip-chars-backward " \t")
693 (bolp))
694 (if ess-auto-newline (progn (ess-indent-line) (newline) t) nil)))
695 (progn
696 (insert last-command-char)
697 (ess-indent-line)
698 (if ess-auto-newline
699 (progn
700 (newline)
701 ;; (newline) may have done auto-fill
702 (setq insertpos (- (point) 2))
703 (ess-indent-line)))
704 (save-excursion
705 (if insertpos (goto-char (1+ insertpos)))
706 (delete-char -1))))
707 (if insertpos
708 (save-excursion
709 (goto-char insertpos)
710 (self-insert-command (prefix-numeric-value arg)))
711 (self-insert-command (prefix-numeric-value arg)))))
713 (defun ess-indent-command (&optional whole-exp)
714 "Indent current line as ESS code, or in some cases insert a tab character.
715 If `ess-tab-always-indent' is non-nil (the default), always indent
716 current line. Otherwise, indent the current line only if point is at
717 the left margin or in the line's indentation; otherwise insert a tab.
718 A numeric argument, regardless of its value, means indent rigidly all
719 the lines of the expression starting after point so that this line
720 becomes properly indented. The relative indentation among the lines
721 of the expression are preserved."
722 (interactive "P")
723 (if whole-exp
724 ;; If arg, always indent this line as S
725 ;; and shift remaining lines of expression the same amount.
726 (let ((shift-amt (ess-indent-line))
727 beg end)
728 (save-excursion
729 (if ess-tab-always-indent
730 (beginning-of-line))
731 (setq beg (point))
732 (backward-up-list 1)
733 (forward-list 1)
734 (setq end (point))
735 (goto-char beg)
736 (forward-line 1)
737 (setq beg (point)))
738 (if (> end beg)
739 (indent-code-rigidly beg end shift-amt)))
740 (if (and (not ess-tab-always-indent)
741 (save-excursion
742 (skip-chars-backward " \t")
743 (not (bolp))))
744 (insert-tab)
745 (ess-indent-line))))
747 (defun ess-indent-exp ()
748 "Indent each line of the ESS grouping following point."
749 (interactive)
750 (let ((indent-stack (list nil))
751 (contain-stack (list (point)))
752 (case-fold-search nil)
753 ;; restart
754 outer-loop-done innerloop-done state ostate
755 this-indent
756 last-sexp
757 last-depth
758 at-else at-brace
759 (opoint (point))
760 (next-depth 0))
761 (save-excursion
762 (forward-sexp 1))
763 (save-excursion
764 (setq outer-loop-done nil)
765 (while (and (not (eobp)) (not outer-loop-done))
766 (setq last-depth next-depth)
767 ;; Compute how depth changes over this line
768 ;; plus enough other lines to get to one that
769 ;; does not end inside a comment or string.
770 ;; Meanwhile, do appropriate indentation on comment lines.
771 (setq innerloop-done nil)
772 (while (and (not innerloop-done)
773 (not (and (eobp) (setq outer-loop-done t))))
774 (setq ostate state)
775 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
776 nil nil state))
777 (setq next-depth (car state))
778 (if (and (car (cdr (cdr state)))
779 (>= (car (cdr (cdr state))) 0))
780 (setq last-sexp (car (cdr (cdr state)))))
781 (if (or (nth 4 ostate))
782 (ess-indent-line))
783 (if (nth 4 state)
784 (and (ess-indent-line)
785 (setcar (nthcdr 4 state) nil)))
786 (if (or (nth 3 state))
787 (forward-line 1)
788 (setq innerloop-done t)))
789 (if (<= next-depth 0)
790 (setq outer-loop-done t))
791 (if outer-loop-done
793 ;; If this line had ..))) (((.. in it, pop out of the levels
794 ;; that ended anywhere in this line, even if the final depth
795 ;; doesn't indicate that they ended.
796 (while (> last-depth (nth 6 state))
797 (setq indent-stack (cdr indent-stack)
798 contain-stack (cdr contain-stack)
799 last-depth (1- last-depth)))
800 (if (/= last-depth next-depth)
801 (setq last-sexp nil))
802 ;; Add levels for any parens that were started in this line.
803 (while (< last-depth next-depth)
804 (setq indent-stack (cons nil indent-stack)
805 contain-stack (cons nil contain-stack)
806 last-depth (1+ last-depth)))
807 (if (null (car contain-stack))
808 (setcar contain-stack (or (car (cdr state))
809 (save-excursion (forward-sexp -1)
810 (point)))))
811 (forward-line 1)
812 (skip-chars-forward " \t")
813 (if (eolp)
815 (if (and (car indent-stack)
816 (>= (car indent-stack) 0))
817 ;; Line is on an existing nesting level.
818 ;; Lines inside parens are handled specially.
819 (if (/= (char-after (car contain-stack)) ?{)
820 (setq this-indent (car indent-stack))
821 ;; Line is at statement level.
822 ;; Is it a new statement? Is it an else?
823 ;; Find last non-comment character before this line
824 (save-excursion
825 (setq at-else (looking-at "else\\W"))
826 (setq at-brace (= (following-char) ?{))
827 (ess-backward-to-noncomment opoint)
828 (if (ess-continued-statement-p)
829 ;; Preceding line did not end in comma or semi;
830 ;; indent this line ess-continued-statement-offset
831 ;; more than previous.
832 (progn
833 (ess-backward-to-start-of-continued-exp (car contain-stack))
834 (setq this-indent
835 (+ ess-continued-statement-offset (current-column)
836 (if at-brace ess-continued-brace-offset 0))))
837 ;; Preceding line ended in comma or semi;
838 ;; use the standard indent for this level.
839 (if at-else
840 (progn (ess-backward-to-start-of-if opoint)
841 (setq this-indent (+ ess-else-offset
842 (current-indentation))))
843 (setq this-indent (car indent-stack))))))
844 ;; Just started a new nesting level.
845 ;; Compute the standard indent for this level.
846 (let ((val (ess-calculate-indent
847 (if (car indent-stack)
848 (- (car indent-stack))))))
849 (setcar indent-stack
850 (setq this-indent val))))
851 ;; Adjust line indentation according to its contents
852 (if (= (following-char) ?})
853 ;;(setq this-indent (- this-indent ess-indent-level)))
854 (setq this-indent (+ this-indent
855 (- ess-close-brace-offset ess-indent-level))))
856 (if (= (following-char) ?{)
857 (setq this-indent (+ this-indent ess-brace-offset)))
858 ;; Put chosen indentation into effect.
859 (or (= (current-column) this-indent)
860 (= (following-char) ?\#)
861 (progn
862 (delete-region (point) (progn (beginning-of-line) (point)))
863 (indent-to this-indent)))
864 ;; Indent any comment following the text.
865 (or (looking-at comment-start-skip)
866 (if (re-search-forward comment-start-skip
867 (save-excursion (end-of-line)
868 (point)) t)
869 (progn (indent-for-comment) (beginning-of-line))))))))))
870 ;; (message "Indenting ESS expression...done")
872 ;;;*;;; Support functions for indentation
874 (defun ess-comment-indent ()
875 (if (looking-at "###")
876 (current-column)
877 (if (looking-at "##")
878 (let ((tem (ess-calculate-indent)))
879 (if (listp tem) (car tem) tem))
880 (skip-chars-backward " \t")
881 (max (if (bolp) 0 (1+ (current-column)))
882 comment-column))))
884 (defun ess-indent-line ()
885 "Indent current line as ESS code.
886 Return the amount the indentation changed by."
887 (let ((indent (ess-calculate-indent nil))
888 beg shift-amt
889 (case-fold-search nil)
890 (pos (- (point-max) (point))))
891 (beginning-of-line)
892 (setq beg (point))
893 (cond ((eq indent nil)
894 (setq indent (current-indentation)))
896 (skip-chars-forward " \t")
897 (if (and ess-fancy-comments (looking-at "###"))
898 (setq indent 0))
899 (if (and ess-fancy-comments
900 (looking-at "#")
901 (not (looking-at "##")))
902 (setq indent comment-column)
903 (if (eq indent t) (setq indent 0))
904 (if (listp indent) (setq indent (car indent)))
905 (cond ((and (looking-at "else\\b")
906 (not (looking-at "else\\s_")))
907 (setq indent (save-excursion
908 (ess-backward-to-start-of-if)
909 (+ ess-else-offset (current-indentation)))))
910 ((= (following-char) ?})
911 (setq indent
912 (+ indent
913 (- ess-close-brace-offset ess-indent-level))))
914 ((= (following-char) ?{)
915 (setq indent (+ indent ess-brace-offset)))))))
916 (skip-chars-forward " \t")
917 (setq shift-amt (- indent (current-column)))
918 (if (zerop shift-amt)
919 (if (> (- (point-max) pos) (point))
920 (goto-char (- (point-max) pos)))
921 (delete-region beg (point))
922 (indent-to indent)
923 ;; If initial point was within line's indentation,
924 ;; position after the indentation.
925 ;; Else stay at same point in text.
926 (if (> (- (point-max) pos) (point))
927 (goto-char (- (point-max) pos))))
928 shift-amt))
930 (defun ess-calculate-indent (&optional parse-start)
931 "Return appropriate indentation for current line as ESS code.
932 In usual case returns an integer: the column to indent to.
933 Returns nil if line starts inside a string, t if in a comment."
934 (save-excursion
935 (beginning-of-line)
936 (let ((indent-point (point))
937 (case-fold-search nil)
938 state
939 containing-sexp)
940 (if parse-start
941 (goto-char parse-start)
942 (beginning-of-defun))
943 (while (< (point) indent-point)
944 (setq parse-start (point))
945 (setq state (parse-partial-sexp (point) indent-point 0))
946 (setq containing-sexp (car (cdr state))))
947 (cond ((or (nth 3 state) (nth 4 state))
948 ;; return nil or t if should not change this line
949 (nth 4 state))
950 ((null containing-sexp)
951 ;; Line is at top level. May be data or function definition,
952 (beginning-of-line)
953 (if (and (/= (following-char) ?\{)
954 (save-excursion
955 (ess-backward-to-noncomment (point-min))
956 (ess-continued-statement-p)))
957 ess-continued-statement-offset
958 0)) ; Unless it starts a function body
959 ((/= (char-after containing-sexp) ?{)
960 ;; line is expression, not statement:
961 ;; indent to just after the surrounding open.
962 (goto-char containing-sexp)
963 (let ((bol (save-excursion (beginning-of-line) (point))))
965 ;; modified by shiba@isac 7.3.1992
966 (cond ((and (numberp ess-expression-offset)
967 (re-search-backward "[ \t]*expression[ \t]*" bol t))
968 ;; This regexp match every "expression".
969 ;; modified by shiba
970 ;;(forward-sexp -1)
971 (beginning-of-line)
972 (skip-chars-forward " \t")
973 ;; End
974 (+ (current-column) ess-expression-offset))
975 ((and (numberp ess-arg-function-offset)
976 (re-search-backward
977 "=[ \t]*\\s\"*\\(\\w\\|\\s_\\)+\\s\"*[ \t]*"
978 bol t))
979 (forward-sexp -1)
980 (+ (current-column) ess-arg-function-offset))
981 ;; "expression" is searched before "=".
982 ;; End
984 (progn (goto-char (1+ containing-sexp))
985 (current-column))))))
987 ;; Statement level. Is it a continuation or a new statement?
988 ;; Find previous non-comment character.
989 (goto-char indent-point)
990 (ess-backward-to-noncomment containing-sexp)
991 ;; Back up over label lines, since they don't
992 ;; affect whether our line is a continuation.
993 (while (eq (preceding-char) ?\,)
994 (ess-backward-to-start-of-continued-exp containing-sexp)
995 (beginning-of-line)
996 (ess-backward-to-noncomment containing-sexp))
997 ;; Now we get the answer.
998 (if (ess-continued-statement-p)
999 ;; This line is continuation of preceding line's statement;
1000 ;; indent ess-continued-statement-offset more than the
1001 ;; previous line of the statement.
1002 (progn
1003 (ess-backward-to-start-of-continued-exp containing-sexp)
1004 (+ ess-continued-statement-offset (current-column)
1005 (if (save-excursion (goto-char indent-point)
1006 (skip-chars-forward " \t")
1007 (eq (following-char) ?{))
1008 ess-continued-brace-offset 0)))
1009 ;; This line starts a new statement.
1010 ;; Position following last unclosed open.
1011 (goto-char containing-sexp)
1012 ;; Is line first statement after an open-brace?
1014 ;; If no, find that first statement and indent like it.
1015 (save-excursion
1016 (forward-char 1)
1017 (while (progn (skip-chars-forward " \t\n")
1018 (looking-at "#"))
1019 ;; Skip over comments following openbrace.
1020 (forward-line 1))
1021 ;; The first following code counts
1022 ;; if it is before the line we want to indent.
1023 (and (< (point) indent-point)
1024 (current-column)))
1025 ;; If no previous statement,
1026 ;; indent it relative to line brace is on.
1027 ;; For open brace in column zero, don't let statement
1028 ;; start there too. If ess-indent-level is zero,
1029 ;; use ess-brace-offset +
1030 ;; ess-continued-statement-offset instead.
1031 ;; For open-braces not the first thing in a line,
1032 ;; add in ess-brace-imaginary-offset.
1033 (+ (if (and (bolp) (zerop ess-indent-level))
1034 (+ ess-brace-offset ess-continued-statement-offset)
1035 ess-indent-level)
1036 ;; Move back over whitespace before the openbrace.
1037 ;; If openbrace is not first nonwhite thing on the line,
1038 ;; add the ess-brace-imaginary-offset.
1039 (progn (skip-chars-backward " \t")
1040 (if (bolp) 0 ess-brace-imaginary-offset))
1041 ;; If the openbrace is preceded by a parenthesized exp,
1042 ;; move to the beginning of that;
1043 ;; possibly a different line
1044 (progn
1045 (if (eq (preceding-char) ?\))
1046 (forward-sexp -1))
1047 ;; Get initial indentation of the line we are on.
1048 (current-indentation))))))))))
1050 (defun ess-continued-statement-p ()
1051 (let ((eol (point)))
1052 (save-excursion
1053 (cond ((memq (preceding-char) '(nil ?\, ?\; ?\} ?\{ ?\]))
1054 nil)
1055 ;; ((bolp))
1056 ((= (preceding-char) ?\))
1057 (forward-sexp -2)
1058 (looking-at "if\\b[ \t]*(\\|function\\b[ \t]*(\\|for\\b[ \t]*(\\|while\\b[ \t]*("))
1059 ((progn (forward-sexp -1)
1060 (and (looking-at "else\\b\\|repeat\\b")
1061 (not (looking-at "else\\s_\\|repeat\\s_"))))
1062 (skip-chars-backward " \t")
1063 (or (bolp)
1064 (= (preceding-char) ?\;)))
1066 (progn (goto-char eol)
1067 (skip-chars-backward " \t")
1068 (or (and (> (current-column) 1)
1069 (save-excursion (backward-char 1)
1070 (looking-at "[-:+*/_><=]")))
1071 (and (> (current-column) 3)
1072 (progn (backward-char 3)
1073 (looking-at "%[^ \t]%"))))))))))
1075 (defun ess-backward-to-noncomment (lim)
1076 (let (opoint stop)
1077 (while (not stop)
1078 (skip-chars-backward " \t\n\f" lim)
1079 (setq opoint (point))
1080 (beginning-of-line)
1081 (search-forward "#" opoint 'move)
1082 (skip-chars-backward " \t#")
1083 (setq stop (or (/= (preceding-char) ?\n) (<= (point) lim)))
1084 (if stop (point)
1085 (beginning-of-line)))))
1087 (defun ess-backward-to-start-of-continued-exp (lim)
1088 (if (= (preceding-char) ?\))
1089 (forward-sexp -1))
1090 (beginning-of-line)
1091 (if (<= (point) lim)
1092 (goto-char (1+ lim)))
1093 (skip-chars-forward " \t"))
1095 (defun ess-backward-to-start-of-if (&optional limit)
1096 "Move to the start of the last ``unbalanced'' if."
1097 (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
1098 (let ((if-level 1)
1099 (case-fold-search nil))
1100 (while (not (zerop if-level))
1101 (backward-sexp 1)
1102 (cond ((looking-at "else\\b")
1103 (setq if-level (1+ if-level)))
1104 ((looking-at "if\\b")
1105 (setq if-level (1- if-level)))
1106 ((< (point) limit)
1107 (setq if-level 0)
1108 (goto-char limit))))))
1110 ;;;*;;; Predefined indentation styles
1112 (defun ess-set-style (&optional style quiet)
1113 "Set up the `ess-mode' style variables from the `ess-style' variable
1114 or if STYLE argument is given, use that. It makes the ESS indentation
1115 style variables buffer local."
1117 (interactive)
1118 (let ((ess-styles (mapcar 'car ess-style-alist)))
1119 (if (interactive-p)
1120 (setq style
1121 (let ((style-string ; get style name with completion
1122 (completing-read
1123 (format
1124 "Set ESS mode indentation style (default %s): "
1125 ess-default-style)
1126 (vconcat ess-styles)
1127 (function (lambda (arg) (memq arg ess-styles))))))
1128 (if (string-equal "" style-string)
1129 ess-default-style
1130 (intern style-string)))))
1131 (setq style (or style ess-style)) ; use ess-style if style is nil
1132 (make-local-variable 'ess-style)
1133 (if (memq style ess-styles)
1134 (setq ess-style style)
1135 (error (concat "Bad ESS style: " style)))
1136 (if (not quiet)
1137 (message "ESS-style: %s" ess-style))
1138 ; finally, set the indentation style variables making each one local
1139 (mapcar (function (lambda (ess-style-pair)
1140 (make-local-variable (car ess-style-pair))
1141 (set (car ess-style-pair)
1142 (cdr ess-style-pair))))
1143 (cdr (assq ess-style ess-style-alist)))
1144 ess-style))
1146 ;;*;; Creating and manipulating dump buffers
1148 ;;;*;;; The user command
1150 (defun ess-dump-object-into-edit-buffer (object)
1151 "Edit an ESS object in its own buffer.
1153 Without a prefix argument, this simply finds the file pointed to by
1154 `ess-source-directory'. If this file does not exist, or if a
1155 prefix argument is given, a dump() command is sent to the ESS process to
1156 generate the source buffer."
1157 (interactive
1158 (progn
1159 (require 'ess-inf)
1160 (ess-force-buffer-current "Process to dump from: ")
1161 (ess-read-object-name "Object to edit: ")))
1162 (let* ((dirname (file-name-as-directory
1163 (if (stringp ess-source-directory)
1164 ess-source-directory
1165 (save-excursion
1166 (set-buffer
1167 (process-buffer (get-ess-process
1168 ess-local-process-name)))
1169 (ess-setq-vars-local ess-customize-alist)
1170 (apply ess-source-directory nil)))))
1171 (filename (concat dirname (format ess-dump-filename-template object)))
1172 (old-buff (get-file-buffer filename)))
1174 ;; If the directory doesn't exist, offer to create it
1175 (if (file-exists-p (directory-file-name dirname)) nil
1176 (if (y-or-n-p ; Approved
1177 (format "Directory %s does not exist. Create it? " dirname))
1178 (make-directory (directory-file-name dirname))
1179 (error "Directory %s does not exist." dirname)))
1181 ;; Three options:
1182 ;; (1) Pop to an existing buffer containing the file in question
1183 ;; (2) Find an existing file
1184 ;; (3) Create a new file by issuing a dump() command to S
1185 ;; Force option (3) if there is a prefix arg
1187 (if current-prefix-arg
1188 (ess-dump-object object filename)
1189 (if old-buff
1190 (progn
1191 (pop-to-buffer old-buff)
1192 (message "Popped to edit buffer."))
1193 ;; No current buffer containing desired file
1194 (if (file-exists-p filename)
1195 (progn
1196 (ess-find-dump-file-other-window filename)
1197 (message "Read %s" filename))
1198 ;; No buffer and no file
1199 (ess-dump-object object filename))))))
1201 (defun ess-dump-object (object filename)
1202 "Dump the ESS object OBJECT into file FILENAME."
1203 (let ((complete-dump-command (format inferior-ess-dump-command
1204 object filename)))
1205 (if (file-writable-p filename) nil
1206 (error "Can't dump %s as %f is not writeable." object filename))
1208 ;; Make sure we start fresh
1209 (if (get-file-buffer filename)
1210 (or (kill-buffer (get-file-buffer filename))
1211 (error "Aborted.")))
1213 (ess-command complete-dump-command)
1214 (message "Dumped in %s" filename)
1216 (ess-find-dump-file-other-window filename)
1218 ;; PD, 1Apr97
1219 ;;This ensures that the object gets indented according to ess-mode,
1220 ;;not as the R/S deparser does it. At the same time, it gets rid
1221 ;;of the mess generated by sending TAB characters to the readline
1222 ;;functions in R when you eval-buffer-*.
1223 (indent-region (point-min-marker) (point-max-marker) nil)
1224 (set-buffer-modified-p nil); no need to safe just because of indenting
1226 ;; Don't make backups for temporary files; it only causes clutter.
1227 ;; The ESS object itself is a kind of backup, anyway.
1228 (unless ess-keep-dump-files
1229 (make-local-variable 'make-backup-files)
1230 (setq make-backup-files nil))
1232 ;; Don't get confirmation to delete dumped files when loading
1233 (if (eq ess-keep-dump-files 'check)
1234 (setq ess-keep-dump-files nil))
1236 ;; Delete the file if necessary
1237 (if ess-delete-dump-files
1238 (delete-file (buffer-file-name)))))
1240 (defun ess-find-dump-file-other-window (filename)
1241 "Find ESS source file FILENAME in another window."
1243 (if (file-exists-p filename) nil
1244 (ess-write-to-dribble-buffer
1245 (format "%s does not exist. Bad dump, starting fresh." filename)))
1247 ;; Generate a buffer with the dumped data
1248 (find-file-other-window filename)
1249 (ess-mode ess-customize-alist)
1251 (auto-save-mode 1) ; Auto save in this buffer
1252 (setq ess-local-process-name ess-current-process-name)
1254 (if ess-function-template
1255 (progn
1256 (goto-char (point-max))
1257 (if (re-search-backward ess-dumped-missing-re nil t)
1258 (progn
1259 (replace-match ess-function-template t t)
1260 (set-buffer-modified-p nil) ; Don't offer to save if killed now
1261 (goto-char (point-min))
1262 (condition-case nil
1263 ;; This may fail if there are no opens
1264 (down-list 1)
1265 (error nil)))))))
1267 ;; AJR: XEmacs, makes sense to dump into "other frame".
1268 (defun ess-dump-object-into-edit-buffer-other-frame (object)
1269 "Edit an ESS object in its own frame."
1270 (switch-to-buffer-other-frame (ess-dump-object-into-edit-buffer object)))
1272 (provide 'ess-mode)
1274 \f ; Local variables section
1276 ;;; This file is automatically placed in Outline minor mode.
1277 ;;; The file is structured as follows:
1278 ;;; Chapters: ^L ;
1279 ;;; Sections: ;;*;;
1280 ;;; Subsections: ;;;*;;;
1281 ;;; Components: defuns, defvars, defconsts
1282 ;;; Random code beginning with a ;;;;* comment
1284 ;;; Local variables:
1285 ;;; mode: emacs-lisp
1286 ;;; outline-minor-mode: nil
1287 ;;; mode: outline-minor
1288 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
1289 ;;; End:
1291 ;;; ess-mode.el ends here