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