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