(ess-r-args-get): Do not use zap-to-char within defun,
[ess.git] / lisp / ess-mode.el
blobedc439a1f90419a5fdfc4b99f0ab11c3af95c6a0
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 (use-local-map ess-mode-map)
386 (set-syntax-table ess-mode-syntax-table)
388 ;; Keep <tabs> out of the code.
389 (make-local-variable 'indent-tabs-mode)
390 (setq indent-tabs-mode nil)
392 ;; (make-local-variable 'paragraph-start)
393 ;; (setq paragraph-start (concat "^$\\|" page-delimiter))
394 ;; (make-local-variable 'paragraph-separate)
395 ;; (setq paragraph-separate paragraph-start)
396 ;; (make-local-variable 'paragraph-ignore-fill-prefix)
397 ;; (setq paragraph-ignore-fill-prefix t)
398 ;; (make-local-variable 'indent-line-function)
399 ;; (setq indent-line-function 'ess-indent-line)
400 ;; (make-local-variable 'require-final-newline)
401 ;; (setq require-final-newline t)
402 ;; (make-local-variable 'comment-start)
403 ;; (setq comment-start "#")
404 ;; (make-local-variable 'comment-start-skip)
405 ;; (setq comment-start-skip "#+ *")
406 ;; (make-local-variable 'comment-column)
407 ;; (setq comment-column 40)
408 ;; (make-local-variable 'comment-indent-function)
409 ;; (setq comment-indent-function 'ess-comment-indent)
410 ;; (make-local-variable 'parse-sexp-ignore-comments)
411 ;; (setq parse-sexp-ignore-comments t)
412 ;; (ess-set-style ess-default-style)
413 ;; (make-local-variable 'ess-local-process-name)
414 ;; (make-local-variable 'ess-keep-dump-files)
415 (put 'ess-local-process-name 'permanent-local t) ; protect from RCS
416 (setq mode-line-process ;; AJR: in future, XEmacs will use modeline-process.
417 '(" [" (ess-local-process-name ess-local-process-name "none") "]"))
418 ;; SJE Tue 28 Dec 2004: do not attempt to load object name db.
419 ;; (ess-load-object-name-db-file)
420 (run-hooks 'ess-mode-hook)
421 (ess-write-to-dribble-buffer "\nFinished setting up ESS-mode.\n"))
423 ;;*;; User commands in ess-mode
425 ;;;*;;; Handy commands
427 (defun ess-execute-in-tb ()
428 "Like `ess-execute', but always evaluates in temp buffer."
429 (interactive)
430 (let ((ess-execute-in-process-buffer nil))
431 (call-interactively 'ess-execute)))
433 ;;;*;;; Buffer motion/manipulation commands
435 (defvar ess-set-function-start
436 ;; setAs, setGeneric; setMethod, setReplaceMethod, setGroupMethod
437 "^set[MGAR][Ma-z]+\\s-?("
440 ;; common R and S
441 (let*
442 ((Q "\\s\"") ; quote
443 (repl "\\(<-\\)?") ; replacement (function)
444 (Sym-0 "\\(\\sw\\|\\s_\\)") ; symbol
445 (Symb (concat Sym-0 "+"))
446 (xSymb (concat "\\[?\\[?" Sym-0 "*")); symbol / [ / [[ / [symbol / [[symbol
447 ;; FIXME: allow '%foo%' but only when quoted; don't allow [_0-9] at beg.
448 (_or_ "\\)\\|\\(") ; OR
449 (space "\\(\\s-\\|\n\\)*") ; white space
451 (part-1 (concat
452 "\\(" ;;--------outer Either-------
453 "\\(\\(" ; EITHER
454 Q xSymb repl Sym-0 "*" Q ; quote ([) (replacement) symbol quote
455 _or_
456 "\\(^\\|[ ]\\)" Symb ; (beginning of name) + Symb
457 "\\)\\)")) ; END EITHER OR
459 (set-S4-exp
460 (concat
461 "^set\\(As\\|Method\\|Generic\\|GroupMethod\\|ReplaceMethod\\)(" ; S4 ...
462 Q xSymb Q "," space
463 ;; and now often `` signature(......), : ''
464 ".*" ;; <<< FIXME ???
467 (part-2 (concat
468 "\\|" ;;--------outer Or ---------
469 set-S4-exp
470 "\\)" ;;--------end outer Either/Or-------
472 "\\(" space "\\s<.*\\s>\\)*" ; whitespace, comment
473 ;; FIXME: in principle we should skip 'definition *= *' here
474 space "function\\s-*(" ; whitespace, function keyword, parenthesis
477 ;; SJE: 2007-07-16 add to quieten byte-compiler.
478 (defvar ess-function-pattern nil
479 "Regexp to match the beginning of a function in S buffers.")
480 (defvar ess-R-function-pattern
481 (concat part-1
482 "\\s-*\\(<-\\|=\\)" ; whitespace, assign
483 part-2)
484 "The regular expression for matching the beginning of an R function.")
486 (defvar ess-S-function-pattern
487 (concat part-1
488 "\\s-*\\(<-\\|_\\|=\\)" ; whitespace, assign (incl. "_")
489 part-2)
490 "The regular expression for matching the beginning of an S function.")
492 ); {end let}
494 (defun ess-beginning-of-function (&optional no-error)
495 "Leave (and return) the point at the beginning of the current ESS function.
496 If the optional argument NO-ERROR is non-nil, the function returns nil when
497 it cannot find a function beginning."
498 (interactive)
499 (let ((init-point (point))
500 (in-set-S4 nil)
501 beg end done)
504 ;; Note that we must be sure that we are past the 'function (' text,
505 ;; such that ess-function-pattern is found in BACKwards later.
506 ;; In case we're sitting in a function or setMethod() header,
507 ;; we need to move further.
508 ;; But not too far! {wrongly getting into next function}
509 (if (search-forward "("
510 (ess-line-end-position 2) t); at most end of next line
511 (forward-char 1))
513 (setq end (point)); = init-point when nothing found
515 (ess-write-to-dribble-buffer
516 (format "ess-BEG-of-fun after 'search-FWD (': Ini-pt %d, (p)-Ini-pt = %d\n"
517 init-point (- end init-point)))
518 (if (and (> end 1)
519 (re-search-backward ;; in case of setMethod() etc ..
520 ess-set-function-start
521 ;; at most 1 line earlier {2 is too much: finds previous sometimes}
522 (+ 1 (ess-line-end-position -1)) t))
524 (progn ;; yes we *have* an S4 setMethod(..)-like
525 (setq in-set-S4 t
526 beg (point))
527 (ess-write-to-dribble-buffer
528 (format " set*() function start at position %d" beg))
529 ;; often need to move even further to have 'function(' to our left
530 ;; (if (search-forward "function" end t)
531 ;; (ess-write-to-dribble-buffer
532 ;; (format " -> 'function' already at pos %d\n" (point)))
533 ;; ;; else need to move further
534 (goto-char end)
535 ;; search 4 lines, we are pretty sure now:
536 (search-forward
537 "function" (ess-line-end-position 4) t)
538 ;; )
539 (search-forward "(" (ess-line-end-position) t)
541 ;; else: regular function; no set*Method(..)
542 (ess-write-to-dribble-buffer "ELSE not in setMethod() header ...\n")
545 (while (not done)
546 ;; Need this while loop to skip over local function definitions
548 ;; In the case of non-success, it is inefficiently
549 ;; going back in the buffer through all function definitions...
550 (unless (re-search-backward ess-function-pattern (point-min) t)
551 (goto-char init-point)
552 (if no-error
553 (setq done t beg nil)
554 ;; else [default]:
555 (error "Point is not in a function according to 'ess-function-pattern'.")
557 (unless done
558 (setq beg (point))
559 (ess-write-to-dribble-buffer
560 (format "\tMatch,Pt:(%d,%d),%d\n"
561 (match-beginning 0) (match-end 0) beg))
562 (setq in-set-S4 (looking-at ess-set-function-start))
563 (forward-list 1) ; get over arguments
565 ;; The following used to bomb "Unbalanced parentheses", n1, n2
566 ;; when the above (search-forward "(" ..) wasn't delimited :
567 (unless in-set-S4 (forward-sexp 1)) ; move over braces
568 ;;DBG (ess-write-to-dribble-buffer "|")
569 (setq end (point))
570 (goto-char beg)
571 ;; current function must begin and end around point
572 (setq done (and (>= end init-point) (<= beg init-point)))))
573 beg))
575 (defun ess-end-of-function (&optional beginning no-error)
576 "Leave the point at the end of the current ESS function.
577 Optional argument for location of beginning. Return '(beg end)."
578 (interactive)
579 (if beginning
580 (goto-char beginning)
581 (setq beginning (ess-beginning-of-function no-error)))
582 (if beginning
583 ;; *hack* only for S (R || S+): are we in setMethod(..) etc?
584 (let ((in-set-S4 (looking-at ess-set-function-start)))
585 (ess-write-to-dribble-buffer
586 (format "ess-END-of-fun: S4=%s, beginning = %d\n" in-set-S4 beginning))
587 (forward-list 1) ; get over arguments || whole set*(..)
588 (unless in-set-S4 (forward-sexp 1)) ; move over braces
589 ;;DBG (ess-write-to-dribble-buffer "ess-END-of-fun: found ok\n")
590 (list beginning (point))
592 ;; else: 'no-error': we are not in a function
593 nil))
595 ;;; Kurt's version, suggested 1997-03-06.
596 (defun ess-mark-function ()
597 "Put mark at end of ESS function, point at beginning."
598 (interactive)
599 (let ((beg (ess-beginning-of-function)))
600 (push-mark (point))
601 (ess-end-of-function beg)
602 (exchange-point-and-mark)))
604 ;; Donated by Stephen Eglen, 2001-08-29:
605 ;; This command is analogous to `narrow-to-defun' (elisp)
606 ;; and `py-narrow-to-defun' (python)."
607 (defun ess-narrow-to-defun ()
608 "Make text outside current function invisible.
609 If text is already narrowed, this is removed before narrowing to the
610 current function."
611 (interactive)
612 ;; if point is not in a function, ess-end-of-function catches the error.
613 (save-excursion
614 (widen)
615 (let* ((beg-end (ess-end-of-function)))
616 (narrow-to-region (nth 0 beg-end) (nth 1 beg-end)))))
618 ;;*;; Loading files
620 (defun ess-check-modifications nil
621 "Check whether loading this file would overwrite some ESS objects
622 which have been modified more recently than this file, and confirm
623 if this is the case."
624 ;; FIXME: this should really cycle through all top-level assignments in
625 ;; the buffer
626 (and (buffer-file-name) ess-filenames-map
627 (let ((sourcemod (nth 5 (file-attributes (buffer-file-name))))
628 (objname))
629 (save-excursion
630 (goto-char (point-min))
631 ;; Get name of assigned object, if we can find it
632 (setq objname
633 (and
634 (re-search-forward
635 "^\\s *\"?\\(\\(\\sw\\|\\s_\\)+\\)\"?\\s *[<_]"
638 (buffer-substring (match-beginning 1)
639 (match-end 1)))))
640 (and
641 sourcemod ; the file may have been deleted
642 objname ; may not have been able to
643 ; find name
644 (ess-modtime-gt (ess-object-modtime objname) sourcemod)
645 (not (y-or-n-p
647 (format
648 "The ESS object %s is newer than this file. Continue?"
649 objname)))
650 (error "Aborted")))))
652 (defun ess-check-source (fname)
653 "If file FNAME has an unsaved buffer, offer to save it.
654 Returns t if the buffer existed and was modified, but was not saved."
655 (let ((buff (get-file-buffer fname)))
656 ;; RMH: Corrections noted below are needed for C-c C-l to work
657 ;; correctly when issued from *S* buffer.
658 ;; The following barfs since
659 ;; 1. `if' does not accept a buffer argument, `not' does.
660 ;; 2. (buffer-file-name) is not necessarily defined for *S*
661 ;;(if buff
662 ;; (let ((deleted (not (file-exists-p (buffer-file-name)))))
663 ;; Next 2 lines are RMH's solution:
664 (if (not(not buff))
665 (let ((deleted (not (file-exists-p fname))))
666 (if (and deleted (not (buffer-modified-p buff)))
667 ;; Buffer has been silently deleted, so silently save
668 (save-excursion
669 (set-buffer buff)
670 (set-buffer-modified-p t)
671 (save-buffer))
672 (if (and (buffer-modified-p buff)
673 (or ess-mode-silently-save
674 (y-or-n-p
675 (format "Save buffer %s first? "
676 (buffer-name buff)))))
677 (save-excursion
678 (set-buffer buff)
679 (save-buffer))))
680 (buffer-modified-p buff)))))
682 (defun ess-parse-errors (showerr)
683 "Jump to error in last loaded ESS source file.
684 With prefix argument, only shows the errors ESS reported."
685 (interactive "P")
686 (ess-make-buffer-current)
687 (let ((errbuff (get-buffer ess-error-buffer-name)))
688 (if (not errbuff)
689 (error "You need to do a load first!")
690 (set-buffer errbuff)
691 (goto-char (point-max))
693 (re-search-backward
694 "^\\(Syntax error: .*\\) at line \\([0-9]*\\), file \\(.*\\)$"
697 (let* ((filename (buffer-substring (match-beginning 3) (match-end 3)))
698 (fbuffer (get-file-buffer filename))
699 (linenum
700 (string-to-number
701 (buffer-substring (match-beginning 2) (match-end 2))))
702 (errmess (buffer-substring (match-beginning 1) (match-end 1))))
703 (if showerr
704 (ess-display-temp-buffer errbuff)
705 (if fbuffer nil
706 (setq fbuffer (find-file-noselect filename))
707 (save-excursion
708 (set-buffer fbuffer)
709 (ess-mode)))
710 (pop-to-buffer fbuffer)
711 (goto-line linenum))
712 (princ errmess t))
713 (message "Not a syntax error.")
714 (ess-display-temp-buffer errbuff)))))
716 ;;*;; ESS code formatting/indentation
718 ;;;*;;; User commands
720 (defun ess-electric-brace (arg)
721 "Insert character and correct line's indentation."
722 (interactive "P")
723 (let (insertpos)
724 (if (and (not arg)
725 (eolp)
726 (or (save-excursion
727 (skip-chars-backward " \t")
728 (bolp))
729 (if ess-auto-newline (progn (ess-indent-line) (newline) t) nil)))
730 (progn
731 (insert last-command-char)
732 (ess-indent-line)
733 (if ess-auto-newline
734 (progn
735 (newline)
736 ;; (newline) may have done auto-fill
737 (setq insertpos (- (point) 2))
738 (ess-indent-line)))
739 (save-excursion
740 (if insertpos (goto-char (1+ insertpos)))
741 (delete-char -1))))
742 (if insertpos
743 (save-excursion
744 (goto-char insertpos)
745 (self-insert-command (prefix-numeric-value arg)))
746 (self-insert-command (prefix-numeric-value arg)))))
748 (defun ess-indent-command (&optional whole-exp)
749 "Indent current line as ESS code, or in some cases insert a tab character.
750 If `ess-tab-always-indent' is non-nil (the default), always indent
751 current line. Otherwise, indent the current line only if point is at
752 the left margin or in the line's indentation; otherwise insert a tab.
753 A numeric argument, regardless of its value, means indent rigidly all
754 the lines of the expression starting after point so that this line
755 becomes properly indented. The relative indentation among the lines
756 of the expression are preserved."
757 (interactive "P")
758 (if whole-exp
759 ;; If arg, always indent this line as S
760 ;; and shift remaining lines of expression the same amount.
761 (let ((shift-amt (ess-indent-line))
762 beg end)
763 (save-excursion
764 (if ess-tab-always-indent
765 (beginning-of-line))
766 (setq beg (point))
767 (backward-up-list 1)
768 (forward-list 1)
769 (setq end (point))
770 (goto-char beg)
771 (forward-line 1)
772 (setq beg (point)))
773 (if (> end beg)
774 (indent-code-rigidly beg end shift-amt)))
775 (if (and (not ess-tab-always-indent)
776 (save-excursion
777 (skip-chars-backward " \t")
778 (not (bolp))))
779 (insert-tab)
780 (ess-indent-line))))
782 (defun ess-indent-exp ()
783 "Indent each line of the ESS grouping following point."
784 (interactive)
785 (let ((indent-stack (list nil))
786 (contain-stack (list (point)))
787 (case-fold-search nil)
788 ;; restart
789 outer-loop-done innerloop-done state ostate
790 this-indent
791 last-sexp
792 last-depth
793 at-else at-brace
794 (opoint (point))
795 (next-depth 0))
796 (save-excursion
797 (forward-sexp 1))
798 (save-excursion
799 (setq outer-loop-done nil)
800 (while (and (not (eobp)) (not outer-loop-done))
801 (setq last-depth next-depth)
802 ;; Compute how depth changes over this line
803 ;; plus enough other lines to get to one that
804 ;; does not end inside a comment or string.
805 ;; Meanwhile, do appropriate indentation on comment lines.
806 (setq innerloop-done nil)
807 (while (and (not innerloop-done)
808 (not (and (eobp) (setq outer-loop-done t))))
809 (setq ostate state)
810 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
811 nil nil state))
812 (setq next-depth (car state))
813 (if (and (car (cdr (cdr state)))
814 (>= (car (cdr (cdr state))) 0))
815 (setq last-sexp (car (cdr (cdr state)))))
816 (if (or (nth 4 ostate))
817 (ess-indent-line))
818 (if (nth 4 state)
819 (and (ess-indent-line)
820 (setcar (nthcdr 4 state) nil)))
821 (if (or (nth 3 state))
822 (forward-line 1)
823 (setq innerloop-done t)))
824 (if (<= next-depth 0)
825 (setq outer-loop-done t))
826 (if outer-loop-done
828 ;; If this line had ..))) (((.. in it, pop out of the levels
829 ;; that ended anywhere in this line, even if the final depth
830 ;; doesn't indicate that they ended.
831 (while (> last-depth (nth 6 state))
832 (setq indent-stack (cdr indent-stack)
833 contain-stack (cdr contain-stack)
834 last-depth (1- last-depth)))
835 (if (/= last-depth next-depth)
836 (setq last-sexp nil))
837 ;; Add levels for any parens that were started in this line.
838 (while (< last-depth next-depth)
839 (setq indent-stack (cons nil indent-stack)
840 contain-stack (cons nil contain-stack)
841 last-depth (1+ last-depth)))
842 (if (null (car contain-stack))
843 (setcar contain-stack (or (car (cdr state))
844 (save-excursion (forward-sexp -1)
845 (point)))))
846 (forward-line 1)
847 (skip-chars-forward " \t")
848 (if (eolp)
850 (if (and (car indent-stack)
851 (>= (car indent-stack) 0))
852 ;; Line is on an existing nesting level.
853 ;; Lines inside parens are handled specially.
854 (if (/= (char-after (car contain-stack)) ?{)
855 (setq this-indent (car indent-stack))
856 ;; Line is at statement level.
857 ;; Is it a new statement? Is it an else?
858 ;; Find last non-comment character before this line
859 (save-excursion
860 (setq at-else (looking-at "else\\W"))
861 (setq at-brace (= (following-char) ?{))
862 (ess-backward-to-noncomment opoint)
863 (if (ess-continued-statement-p)
864 ;; Preceding line did not end in comma or semi;
865 ;; indent this line ess-continued-statement-offset
866 ;; more than previous.
867 (progn
868 (ess-backward-to-start-of-continued-exp (car contain-stack))
869 (setq this-indent
870 (+ ess-continued-statement-offset (current-column)
871 (if at-brace ess-continued-brace-offset 0))))
872 ;; Preceding line ended in comma or semi;
873 ;; use the standard indent for this level.
874 (if at-else
875 (progn (ess-backward-to-start-of-if opoint)
876 (setq this-indent (+ ess-else-offset
877 (current-indentation))))
878 (setq this-indent (car indent-stack))))))
879 ;; Just started a new nesting level.
880 ;; Compute the standard indent for this level.
881 (let ((val (ess-calculate-indent
882 (if (car indent-stack)
883 (- (car indent-stack))))))
884 (setcar indent-stack
885 (setq this-indent val))))
886 ;; Adjust line indentation according to its contents
887 (if (= (following-char) ?})
888 ;;(setq this-indent (- this-indent ess-indent-level)))
889 (setq this-indent (+ this-indent
890 (- ess-close-brace-offset ess-indent-level))))
891 (if (= (following-char) ?{)
892 (setq this-indent (+ this-indent ess-brace-offset)))
893 ;; Put chosen indentation into effect.
894 (or (= (current-column) this-indent)
895 (= (following-char) ?\#)
896 (progn
897 (delete-region (point) (progn (beginning-of-line) (point)))
898 (indent-to this-indent)))
899 ;; Indent any comment following the text.
900 (or (looking-at comment-start-skip)
901 (if (re-search-forward comment-start-skip
902 (save-excursion (end-of-line)
903 (point)) t)
904 (progn (indent-for-comment) (beginning-of-line))))))))))
905 ;; (message "Indenting ESS expression...done")
907 ;;;*;;; Support functions for indentation
909 (defun ess-comment-indent ()
910 (if (looking-at "###")
911 (current-column)
912 (if (looking-at "##")
913 (let ((tem (ess-calculate-indent)))
914 (if (listp tem) (car tem) tem))
915 (skip-chars-backward " \t")
916 (max (if (bolp) 0 (1+ (current-column)))
917 comment-column))))
919 (defun ess-indent-line ()
920 "Indent current line as ESS code.
921 Return the amount the indentation changed by."
922 (let ((indent (ess-calculate-indent nil))
923 beg shift-amt
924 (case-fold-search nil)
925 (pos (- (point-max) (point))))
926 (beginning-of-line)
927 (setq beg (point))
928 (cond ((eq indent nil)
929 (setq indent (current-indentation)))
931 (skip-chars-forward " \t")
932 (if (and ess-fancy-comments (looking-at "###"))
933 (setq indent 0))
934 (if (and ess-fancy-comments
935 (looking-at "#")
936 (not (looking-at "##")))
937 (setq indent comment-column)
938 (if (eq indent t) (setq indent 0))
939 (if (listp indent) (setq indent (car indent)))
940 (cond ((and (looking-at "else\\b")
941 (not (looking-at "else\\s_")))
942 (setq indent (save-excursion
943 (ess-backward-to-start-of-if)
944 (+ ess-else-offset (current-indentation)))))
945 ((= (following-char) ?})
946 (setq indent
947 (+ indent
948 (- ess-close-brace-offset ess-indent-level))))
949 ((= (following-char) ?{)
950 (setq indent (+ indent ess-brace-offset)))))))
951 (skip-chars-forward " \t")
952 (setq shift-amt (- indent (current-column)))
953 (if (zerop shift-amt)
954 (if (> (- (point-max) pos) (point))
955 (goto-char (- (point-max) pos)))
956 (delete-region beg (point))
957 (indent-to indent)
958 ;; If initial point was within line's indentation,
959 ;; position after the indentation.
960 ;; Else stay at same point in text.
961 (if (> (- (point-max) pos) (point))
962 (goto-char (- (point-max) pos))))
963 shift-amt))
965 (defun ess-calculate-indent (&optional parse-start)
966 "Return appropriate indentation for current line as ESS code.
967 In usual case returns an integer: the column to indent to.
968 Returns nil if line starts inside a string, t if in a comment."
969 (save-excursion
970 (beginning-of-line)
971 (let ((indent-point (point))
972 (case-fold-search nil)
973 state
974 containing-sexp)
975 (if parse-start
976 (goto-char parse-start)
977 (beginning-of-defun))
978 (while (< (point) indent-point)
979 (setq parse-start (point))
980 (setq state (parse-partial-sexp (point) indent-point 0))
981 (setq containing-sexp (car (cdr state))))
982 (cond ((or (nth 3 state) (nth 4 state))
983 ;; return nil or t if should not change this line
984 (nth 4 state))
985 ((null containing-sexp)
986 ;; Line is at top level. May be data or function definition,
987 (beginning-of-line)
988 (if (and (/= (following-char) ?\{)
989 (save-excursion
990 (ess-backward-to-noncomment (point-min))
991 (ess-continued-statement-p)))
992 ess-continued-statement-offset
993 0)) ; Unless it starts a function body
994 ((/= (char-after containing-sexp) ?{)
995 ;; line is expression, not statement:
996 ;; indent to just after the surrounding open.
997 (goto-char containing-sexp)
998 (let ((bol (save-excursion (beginning-of-line) (point))))
1000 ;; modified by shiba@isac 7.3.1992
1001 (cond ((and (numberp ess-expression-offset)
1002 (re-search-backward "[ \t]*expression[ \t]*" bol t))
1003 ;; This regexp match every "expression".
1004 ;; modified by shiba
1005 ;;(forward-sexp -1)
1006 (beginning-of-line)
1007 (skip-chars-forward " \t")
1008 ;; End
1009 (+ (current-column) ess-expression-offset))
1010 ((and (numberp ess-arg-function-offset)
1011 (re-search-backward
1012 "=[ \t]*\\s\"*\\(\\w\\|\\s_\\)+\\s\"*[ \t]*"
1013 bol t))
1014 (forward-sexp -1)
1015 (+ (current-column) ess-arg-function-offset))
1016 ;; "expression" is searched before "=".
1017 ;; End
1019 (progn (goto-char (1+ containing-sexp))
1020 (current-column))))))
1022 ;; Statement level. Is it a continuation or a new statement?
1023 ;; Find previous non-comment character.
1024 (goto-char indent-point)
1025 (ess-backward-to-noncomment containing-sexp)
1026 ;; Back up over label lines, since they don't
1027 ;; affect whether our line is a continuation.
1028 (while (eq (preceding-char) ?\,)
1029 (ess-backward-to-start-of-continued-exp containing-sexp)
1030 (beginning-of-line)
1031 (ess-backward-to-noncomment containing-sexp))
1032 ;; Now we get the answer.
1033 (if (ess-continued-statement-p)
1034 ;; This line is continuation of preceding line's statement;
1035 ;; indent ess-continued-statement-offset more than the
1036 ;; previous line of the statement.
1037 (progn
1038 (ess-backward-to-start-of-continued-exp containing-sexp)
1039 (+ ess-continued-statement-offset (current-column)
1040 (if (save-excursion (goto-char indent-point)
1041 (skip-chars-forward " \t")
1042 (eq (following-char) ?{))
1043 ess-continued-brace-offset 0)))
1044 ;; This line starts a new statement.
1045 ;; Position following last unclosed open.
1046 (goto-char containing-sexp)
1047 ;; Is line first statement after an open-brace?
1049 ;; If no, find that first statement and indent like it.
1050 (save-excursion
1051 (forward-char 1)
1052 (while (progn (skip-chars-forward " \t\n")
1053 (looking-at "#"))
1054 ;; Skip over comments following openbrace.
1055 (forward-line 1))
1056 ;; The first following code counts
1057 ;; if it is before the line we want to indent.
1058 (and (< (point) indent-point)
1059 (current-column)))
1060 ;; If no previous statement,
1061 ;; indent it relative to line brace is on.
1062 ;; For open brace in column zero, don't let statement
1063 ;; start there too. If ess-indent-level is zero,
1064 ;; use ess-brace-offset +
1065 ;; ess-continued-statement-offset instead.
1066 ;; For open-braces not the first thing in a line,
1067 ;; add in ess-brace-imaginary-offset.
1068 (+ (if (and (bolp) (zerop ess-indent-level))
1069 (+ ess-brace-offset ess-continued-statement-offset)
1070 ess-indent-level)
1071 ;; Move back over whitespace before the openbrace.
1072 ;; If openbrace is not first nonwhite thing on the line,
1073 ;; add the ess-brace-imaginary-offset.
1074 (progn (skip-chars-backward " \t")
1075 (if (bolp) 0 ess-brace-imaginary-offset))
1076 ;; If the openbrace is preceded by a parenthesized exp,
1077 ;; move to the beginning of that;
1078 ;; possibly a different line
1079 (progn
1080 (if (eq (preceding-char) ?\))
1081 (forward-sexp -1))
1082 ;; Get initial indentation of the line we are on.
1083 (current-indentation))))))))))
1085 (defun ess-continued-statement-p ()
1086 (let ((eol (point)))
1087 (save-excursion
1088 (cond ((memq (preceding-char) '(nil ?\, ?\; ?\} ?\{ ?\]))
1089 nil)
1090 ;; ((bolp))
1091 ((= (preceding-char) ?\))
1092 (forward-sexp -2)
1093 (looking-at "if\\b[ \t]*(\\|function\\b[ \t]*(\\|for\\b[ \t]*(\\|while\\b[ \t]*("))
1094 ((progn (forward-sexp -1)
1095 (and (looking-at "else\\b\\|repeat\\b")
1096 (not (looking-at "else\\s_\\|repeat\\s_"))))
1097 (skip-chars-backward " \t")
1098 (or (bolp)
1099 (= (preceding-char) ?\;)))
1101 (progn (goto-char eol)
1102 (skip-chars-backward " \t")
1103 (or (and (> (current-column) 1)
1104 (save-excursion (backward-char 1)
1105 (looking-at "[-:+*/_><=]")))
1106 (and (> (current-column) 3)
1107 (progn (backward-char 3)
1108 (looking-at "%[^ \t]%"))))))))))
1110 (defun ess-backward-to-noncomment (lim)
1111 (let (opoint stop)
1112 (while (not stop)
1113 (skip-chars-backward " \t\n\f" lim)
1114 (setq opoint (point))
1115 (beginning-of-line)
1116 (search-forward "#" opoint 'move)
1117 (skip-chars-backward " \t#")
1118 (setq stop (or (/= (preceding-char) ?\n) (<= (point) lim)))
1119 (if stop (point)
1120 (beginning-of-line)))))
1122 (defun ess-backward-to-start-of-continued-exp (lim)
1123 (if (= (preceding-char) ?\))
1124 (forward-sexp -1))
1125 (beginning-of-line)
1126 (if (<= (point) lim)
1127 (goto-char (1+ lim)))
1128 (skip-chars-forward " \t"))
1130 (defun ess-backward-to-start-of-if (&optional limit)
1131 "Move to the start of the last ``unbalanced'' if."
1132 (or limit (setq limit (save-excursion (beginning-of-defun) (point))))
1133 (let ((if-level 1)
1134 (case-fold-search nil))
1135 (while (not (zerop if-level))
1136 (backward-sexp 1)
1137 (cond ((looking-at "else\\b")
1138 (setq if-level (1+ if-level)))
1139 ((looking-at "if\\b")
1140 (setq if-level (1- if-level)))
1141 ((< (point) limit)
1142 (setq if-level 0)
1143 (goto-char limit))))))
1145 ;;;*;;; Predefined indentation styles
1147 (defun ess-set-style (&optional style quiet)
1148 "Set up the `ess-mode' style variables from the `ess-style' variable
1149 or if STYLE argument is given, use that. It makes the ESS indentation
1150 style variables buffer local."
1152 (interactive)
1153 (let ((ess-styles (mapcar 'car ess-style-alist)))
1154 (if (interactive-p)
1155 (setq style
1156 (let ((style-string ; get style name with completion
1157 (completing-read
1158 (format
1159 "Set ESS mode indentation style (default %s): "
1160 ess-default-style)
1161 (vconcat ess-styles)
1162 (function (lambda (arg) (memq arg ess-styles))))))
1163 (if (string-equal "" style-string)
1164 ess-default-style
1165 (intern style-string)))))
1166 (setq style (or style ess-style)) ; use ess-style if style is nil
1167 (make-local-variable 'ess-style)
1168 (if (memq style ess-styles)
1169 (setq ess-style style)
1170 (error (concat "Bad ESS style: " style)))
1171 (if (not quiet)
1172 (message "ESS-style: %s" ess-style))
1173 ; finally, set the indentation style variables making each one local
1174 (mapcar (function (lambda (ess-style-pair)
1175 (make-local-variable (car ess-style-pair))
1176 (set (car ess-style-pair)
1177 (cdr ess-style-pair))))
1178 (cdr (assq ess-style ess-style-alist)))
1179 ess-style))
1181 ;;*;; Creating and manipulating dump buffers
1183 ;;;*;;; The user command
1185 (defun ess-dump-object-into-edit-buffer (object)
1186 "Edit an ESS object in its own buffer.
1188 Without a prefix argument, this simply finds the file pointed to by
1189 `ess-source-directory'. If this file does not exist, or if a
1190 prefix argument is given, a dump() command is sent to the ESS process to
1191 generate the source buffer."
1192 (interactive
1193 (progn
1194 (ess-force-buffer-current "Process to dump from: ")
1195 (if (ess-ddeclient-p)
1196 (list (read-string "Object to edit: "))
1197 (ess-read-object-name "Object to edit: "))))
1199 (let* ((dirname (file-name-as-directory
1200 (if (stringp ess-source-directory)
1201 ess-source-directory
1202 (save-excursion
1203 (set-buffer
1204 (process-buffer (get-ess-process
1205 ess-local-process-name)))
1206 (ess-setq-vars-local ess-customize-alist)
1207 (apply ess-source-directory nil)))))
1208 (filename (concat dirname (format ess-dump-filename-template object)))
1209 (old-buff (get-file-buffer filename)))
1211 ;; If the directory doesn't exist, offer to create it
1212 (if (file-exists-p (directory-file-name dirname)) nil
1213 (if (y-or-n-p ; Approved
1214 (format "Directory %s does not exist. Create it? " dirname))
1215 (make-directory (directory-file-name dirname))
1216 (error "Directory %s does not exist." dirname)))
1218 ;; Three options:
1219 ;; (1) Pop to an existing buffer containing the file in question
1220 ;; (2) Find an existing file
1221 ;; (3) Create a new file by issuing a dump() command to S
1222 ;; Force option (3) if there is a prefix arg
1224 (if current-prefix-arg
1225 (ess-dump-object object filename)
1226 (if old-buff
1227 (progn
1228 (pop-to-buffer old-buff)
1229 (message "Popped to edit buffer."))
1230 ;; No current buffer containing desired file
1231 (if (file-exists-p filename)
1232 (progn
1233 (ess-find-dump-file-other-window filename)
1234 (message "Read %s" filename))
1235 ;; No buffer and no file
1236 (ess-dump-object object filename))))))
1238 (defun ess-dump-object (object filename)
1239 "Dump the ESS object OBJECT into file FILENAME."
1240 (let ((complete-dump-command (format inferior-ess-dump-command
1241 object filename)))
1242 (if (file-writable-p filename) nil
1243 (error "Can't dump %s as %f is not writeable." object filename))
1245 (if (ess-ddeclient-p)
1246 ;; ddeclient version
1247 (ess-dump-object-ddeclient object filename)
1249 ;; else: "normal", non-DDE behavior:
1251 ;; Make sure we start fresh
1252 (if (get-file-buffer filename)
1253 (kill-buffer (get-file-buffer filename)))
1255 (ess-command complete-dump-command)
1256 (message "Dumped in %s" filename)
1258 (ess-find-dump-file-other-window filename)
1260 ;; PD, 1Apr97
1261 ;;This ensures that the object gets indented according to ess-mode,
1262 ;;not as the R/S deparser does it. At the same time, it gets rid
1263 ;;of the mess generated by sending TAB characters to the readline
1264 ;;functions in R when you eval-buffer-*.
1265 (indent-region (point-min-marker) (point-max-marker) nil)
1266 (set-buffer-modified-p nil) ; no need to safe just because of indenting
1268 ;; Don't make backups for temporary files; it only causes clutter.
1269 ;; The ESS object itself is a kind of backup, anyway.
1270 (unless ess-keep-dump-files
1271 (make-local-variable 'make-backup-files)
1272 (setq make-backup-files nil))
1274 ;; Don't get confirmation to delete dumped files when loading
1275 (if (eq ess-keep-dump-files 'check)
1276 (setq ess-keep-dump-files nil))
1278 ;; Delete the file if necessary
1279 (if ess-delete-dump-files
1280 (delete-file (buffer-file-name))))))
1282 (defun ess-find-dump-file-other-window (filename)
1283 "Find ESS source file FILENAME in another window."
1285 (if (file-exists-p filename) nil
1286 (ess-write-to-dribble-buffer
1287 (format "%s does not exist. Bad dump, starting fresh." filename)))
1289 ;; Generate a buffer with the dumped data
1290 (find-file-other-window filename)
1291 (ess-mode ess-customize-alist)
1293 (auto-save-mode 1) ; Auto save in this buffer
1294 (setq ess-local-process-name ess-current-process-name)
1296 (if ess-function-template
1297 (progn
1298 (goto-char (point-max))
1299 (if (re-search-backward ess-dumped-missing-re nil t)
1300 (progn
1301 (replace-match ess-function-template t t)
1302 (set-buffer-modified-p nil) ; Don't offer to save if killed now
1303 (goto-char (point-min))
1304 (condition-case nil
1305 ;; This may fail if there are no opens
1306 (down-list 1)
1307 (error nil)))))))
1309 ;; AJR: XEmacs, makes sense to dump into "other frame".
1310 (defun ess-dump-object-into-edit-buffer-other-frame (object)
1311 "Edit an ESS object in its own frame."
1312 (switch-to-buffer-other-frame (ess-dump-object-into-edit-buffer object)))
1314 (provide 'ess-mode)
1316 \f ; Local variables section
1318 ;;; This file is automatically placed in Outline minor mode.
1319 ;;; The file is structured as follows:
1320 ;;; Chapters: ^L ;
1321 ;;; Sections: ;;*;;
1322 ;;; Subsections: ;;;*;;;
1323 ;;; Components: defuns, defvars, defconsts
1324 ;;; Random code beginning with a ;;;;* comment
1326 ;;; Local variables:
1327 ;;; mode: emacs-lisp
1328 ;;; outline-minor-mode: nil
1329 ;;; mode: outline-minor
1330 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
1331 ;;; End:
1333 ;;; ess-mode.el ends here