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--2004 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>
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)
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.
29 ;; Code for editing ESS source code.
33 \f ; Requires and autoloads
37 ;;; AJR: THIS IS GROSS AND DISGUSTING (but I wrote it).
38 ;;; MM: and I had to add all other 'ess-eval-*** ...
39 ;;; >>> why not just do the obvious instead of all these ? Namely,
40 ;;; (require 'ess-inf)
41 ;;; ------------------ ?
42 ;;; AJR: The reason is that we ONLY need to load ess-inf for the
43 ;;; functions which are interactive in nature. We don't want to load
44 ;;; it when we are only editing.
46 (autoload 'ess-mode-minibuffer-map
"ess-inf" "" nil
'keymap
)
47 (autoload 'ess-read-object-name
"ess-inf" "" nil
)
48 (autoload 'ess-list-object-completions
"ess-inf" "" nil
)
50 (autoload 'ess-eval-linewise
"ess-inf" "" nil
)
51 (autoload 'ess-eval-paragraph
"ess-inf" "" nil
)
52 (autoload 'ess-eval-region
"ess-inf" "" nil
)
53 (autoload 'ess-eval-buffer
"ess-inf" "" nil
)
54 (autoload 'ess-eval-function
"ess-inf" "" nil
)
55 (autoload 'ess-eval-line
"ess-inf" "" nil
)
56 (autoload 'ess-eval-line-and-step
"ess-inf" "" nil
)
57 (autoload 'ess-eval-region-and-go
"ess-inf" "" nil
)
58 (autoload 'ess-eval-buffer-and-go
"ess-inf" "" nil
)
59 (autoload 'ess-eval-function-and-go
"ess-inf" "" nil
)
60 (autoload 'ess-eval-line-and-go
"ess-inf" "" nil
)
62 (autoload 'ess-load-file
"ess-inf" "" nil
)
63 (autoload 'ess-switch-process
"ess-inf" "" nil
)
64 (autoload 'ess-switch-to-ESS
"ess-inf" "" nil
)
65 (autoload 'ess-request-a-process
"ess-inf" "" nil
)
66 (autoload 'get-ess-process
"ess-inf" "" nil
)
67 (autoload 'ess-command
"ess-inf" "" nil
)
68 (autoload 'ess-create-temp-buffer
"ess-inf" "" nil
)
69 (autoload 'ess-display-temp-buffer
"ess-inf" "" nil
)
70 (autoload 'ess-force-buffer-current
"ess-inf" "" nil
)
71 (autoload 'ess-make-buffer-current
"ess-inf" "" nil
)
72 (autoload 'ess-modtime-gt
"ess-inf" "" nil
)
73 (autoload 'ess-object-modtime
"ess-inf" "" nil
)
74 (autoload 'ess-quit
"ess-inf" "" nil
)
77 (defun ess-line-end-position (&optional N
)
84 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
87 ;;;; * The major mode ess-mode
88 ;;;; * Commands for ess-mode
89 ;;;; * Code evaluation commands
90 ;;;; * Indenting code and commands
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 ;;*;; Major mode definition
97 (if (featurep 'xemacs
)
99 (setq ess-eval-map
(make-keymap))
100 ;; else code for GNU Emacs
101 (setq ess-eval-map
(make-sparse-keymap)))
103 (define-key ess-eval-map
"\C-r" 'ess-eval-region
)
104 (define-key ess-eval-map
"\M-r" 'ess-eval-region-and-go
)
105 (define-key ess-eval-map
"\C-b" 'ess-eval-buffer
)
106 (define-key ess-eval-map
"\M-b" 'ess-eval-buffer-and-go
)
107 (define-key ess-eval-map
"\C-f" 'ess-eval-function
)
108 (define-key ess-eval-map
"\M-f" 'ess-eval-function-and-go
)
109 (define-key ess-eval-map
"\C-x" 'ess-eval-function
)
110 (define-key ess-eval-map
"\C-n" 'ess-eval-line-and-step
)
111 (define-key ess-eval-map
"\C-j" 'ess-eval-line
)
112 (define-key ess-eval-map
"\M-j" 'ess-eval-line-and-go
))
118 (if (featurep 'xemacs
)
119 (progn ;; Code for XEmacs
120 (setq ess-mode-map
(make-keymap))
121 (set-keymap-parent ess-mode-map text-mode-map
)) ;; was comint?!?
122 ;; else code for GNU Emacs
123 (setq ess-mode-map
(make-sparse-keymap)))
125 ;; By popular demand:
126 (define-key ess-mode-map
"\C-m" 'newline-and-indent
); = [RETURN]
128 (define-key ess-mode-map
"\C-c\C-r" 'ess-eval-region
)
129 (define-key ess-mode-map
"\C-c\M-r" 'ess-eval-region-and-go
)
130 (define-key ess-mode-map
"\C-c\C-b" 'ess-eval-buffer
)
131 (define-key ess-mode-map
"\C-c\M-b" 'ess-eval-buffer-and-go
)
132 (define-key ess-mode-map
"\C-c\C-f" 'ess-eval-function
)
133 (define-key ess-mode-map
"\C-c\M-f" 'ess-eval-function-and-go
)
134 (define-key ess-mode-map
"\M-\C-x" 'ess-eval-function
)
135 (define-key ess-mode-map
"\C-c\C-n" 'ess-eval-line-and-step
)
136 (define-key ess-mode-map
"\C-c\C-j" 'ess-eval-line
)
137 (define-key ess-mode-map
"\C-c\M-j" 'ess-eval-line-and-go
)
138 (define-key ess-mode-map
"\M-\C-a" 'ess-beginning-of-function
)
139 (define-key ess-mode-map
"\M-\C-e" 'ess-end-of-function
)
140 (define-key ess-mode-map
"\C-xnd" 'ess-narrow-to-defun
)
141 (define-key ess-mode-map
"\C-c\C-y" 'ess-switch-to-ESS
)
142 (define-key ess-mode-map
"\C-c\C-z" 'ess-switch-to-end-of-ESS
)
143 (define-key ess-mode-map
"\C-c\C-l" 'ess-load-file
)
144 (define-key ess-mode-map
"\C-c\C-v" 'ess-display-help-on-object
)
145 (define-key ess-mode-map
"\C-c\C-d" 'ess-dump-object-into-edit-buffer
)
146 ;(define-key ess-mode-map "\C-c5\C-d"'ess-dump-object-into-edit-buffer-other-frame)
147 (define-key ess-mode-map
"\C-c\C-s" 'ess-switch-process
) ; use a
148 ; different process for the buffer.
149 (define-key ess-mode-map
"\C-c\C-t" 'ess-execute-in-tb
)
150 (define-key ess-mode-map
"\C-c\t" 'ess-complete-object-name
)
151 (define-key ess-mode-map
"\M-\t" 'comint-replace-by-expanded-filename
)
152 (define-key ess-mode-map
"\M-?" 'ess-list-object-completions
)
153 ;; wrong here (define-key ess-mode-map "\C-c\C-k" 'ess-request-a-process)
154 (define-key ess-mode-map
"\C-c\C-k" 'ess-force-buffer-current
)
155 (define-key ess-mode-map
"\C-c`" 'ess-parse-errors
) ; \C-x reserved!
156 (define-key ess-mode-map
"{" 'ess-electric-brace
)
157 (define-key ess-mode-map
"}" 'ess-electric-brace
)
158 (define-key ess-mode-map
"\e\C-h" 'ess-mark-function
)
159 (define-key ess-mode-map
"\e\C-q" 'ess-indent-exp
)
160 (define-key ess-mode-map
"\177" 'backward-delete-char-untabify
)
161 (define-key ess-mode-map
"\t" 'ess-indent-command
)
162 (define-key ess-mode-map
"\C-c\C-q" 'ess-quit
)
163 (define-key ess-mode-map
"\C-c\C-e" ess-eval-map
))
165 (require 'noweb-mode
)
167 ess-mode-menu ess-mode-map
168 "Menu for use in `ess-mode'."
170 ["What is this? (beta)" ess-mouse-me t
]
171 ["Load file" ess-load-file t
]
173 ["Eval buffer" ess-eval-buffer-and-go t
]
174 ["Eval region" ess-eval-region-and-go t
]
175 ["Eval function" ess-eval-function-and-go t
]
176 ["Eval line" ess-eval-line-and-go t
]
177 ["Eval chunk" ess-eval-chunk-and-go noweb-mode
]
178 ["Eval thread" ess-eval-thread-and-go noweb-mode
]
179 ["About" (ess-goto-info "Evaluating code") t
]
182 ["Eval buffer" ess-eval-buffer t
]
183 ["Eval region" ess-eval-region t
]
184 ["Eval function" ess-eval-function t
]
185 ["Enter expression" ess-execute-in-tb t
]
186 ["Eval line" ess-eval-line t
]
187 ["Eval line & step" ess-eval-line-and-step t
]
188 ["Eval chunk" ess-eval-chunk noweb-mode
]
189 ["Eval thread" ess-eval-thread noweb-mode
]
190 ["About" (ess-goto-info "Evaluating code") t
]
193 ["Edit new object" ess-dump-object-into-edit-buffer t
]
194 ["Goto end of ESS buffer" ess-switch-to-end-of-ESS t
]
195 ["Switch to ESS buffer" ess-switch-to-ESS t
]
196 ["Beginning of function" ess-beginning-of-function t
]
197 ["End of function" ess-end-of-function t
]
200 ["Backward list" backward-list t
]
201 ["Forward list" forward-list t
]
202 ["Next parenthesis" down-list t
]
203 ["Enclosing parenthesis" backward-up-list t
]
204 ["Backward sexp" backward-sexp t
]
205 ["Forward sexp" forward-sexp t
]
206 ["About" (Info-goto-node "(Emacs)Lists") t
]
209 ["Complete Filename" comint-replace-by-expanded-filename t
]
210 ["Complete Object" ess-complete-object-name t
]
211 ["Kill sexp" kill-sexp t
]
212 ["Mark function" ess-mark-function t
]
213 ["Indent expression" ess-indent-exp t
]
214 ["Indent line" ess-indent-command t
]
216 ["About" (ess-goto-info "Edit buffer") t
]
219 ;; SJE - :help not yet recognised in XEmacs.
220 ["R" R t
] ;; :help "Start a new R process" :active t
221 ["S" S t
] ;; :help "Start a new S process" :active t
222 ["Sqpe" Sqpe ess-microsoft-p
] ;; :help "Start a new Sqpe process" :active t
223 ["S+6-exisiting" S
+6-existing ess-microsoft-p
] ;; :help "Access an existing S process" :active t
224 ["SAS" SAS-menu t
] ;; :help "Start a new SAS process" :active t
225 ;; The following menu item "Other" is a place-holder that will
226 ;; be replaced with the other versions of R and Sqpe that can be run.
227 ;; See `ess-r-versions-create' and ess-site.el
229 ["No other R or Sqpe versions" nil nil
])
231 (ess-goto-info "Starting up") t
]
232 ;; :help "Read about starting a new ESS process" :active t]
234 ["Switch Process" ess-switch-process t
]
236 ["Describe" describe-mode t
]
237 ["About editing" (ess-goto-info "Editing") t
]
238 ["Read ESS info" (ess-goto-info "") t
]
239 ["Send bug report" ess-submit-bug-report t
]
243 "Start SAS from the menu."
246 ;; replace with other choices for starting SAS under XEmacs?
247 (error "SAS cannot be started this way in ESS on Windows.")
250 (defun ess-mode-xemacs-menu ()
251 "Hook to install `ess-mode' menu for XEmacs (w/ easymenu)."
253 (easy-menu-add ess-mode-menu
)
254 (easy-menu-remove ess-mode-menu
)))
256 (if (featurep 'xemacs
)
257 (add-hook 'ess-mode-hook
'ess-mode-xemacs-menu
))
259 (defun ess-mode (&optional alist proc-name
)
260 "Major mode for editing ESS source.
261 Optional arg ALIST describes how to customize the editing mode.
262 Optional arg PROC-NAME is name of associated inferior process.
266 Extra binding to note: 'ESC C-\\' indent-region.
268 Entry to this mode runs the hooks in ess-mode-hook.
270 You can send text to the inferior ESS process from other buffers containing
272 `ess-eval-region' sends the current region to the ESS process.
273 `ess-eval-buffer' sends the current buffer to the ESS process.
274 `ess-eval-function' sends the current function to the ESS process.
275 `ess-eval-line' sends the current line to the ESS process.
276 `ess-beginning-of-function' and `ess-end-of-function' move the point to
277 the beginning and end of the current ESS function.
278 `ess-switch-to-ESS' switches the current buffer to the ESS process buffer.
279 `ess-switch-to-end-of-ESS' switches the current buffer to the ESS process
280 buffer and puts point at the end of it.
282 `ess-eval-region-and-go', `ess-eval-buffer-and-go',
283 `ess-eval-function-and-go', and `ess-eval-line-and-go' switch to the S
284 process buffer after sending their text.
286 `ess-load-file' sources a file of commands to the ESS process.
288 \\[ess-indent-command] indents for ESS code.
289 \\[backward-delete-char-untabify] converts tabs to spaces as it moves back.
290 Comments are indented in a similar way to Emacs-lisp mode:
291 `###' beginning of line
292 `##' the same level of indentation as the code
293 `#' the same column on the right, or to the right of such a
294 column if that is not possible.(default value 40).
295 \\[indent-for-comment] command automatically inserts such a
296 `#' in the right place, or aligns such a comment if it is
298 \\[ess-indent-exp] command indents each line of the ESS grouping following point.
300 Variables controlling indentation style:
301 `ess-tab-always-indent'
302 Non-nil means TAB in ESS mode should always reindent the current line,
303 regardless of where in the line point is when the TAB command is used.
305 Non-nil means automatically newline before and after braces inserted in S
308 Indentation of ESS statements within surrounding block.
309 The surrounding block's indentation is the indentation of the line on
310 which the open-brace appears.
311 `ess-continued-statement-offset'
312 Extra indentation given to a substatement, such as the then-clause of an
313 if or body of a while.
314 `ess-continued-brace-offset'
315 Extra indentation given to a brace that starts a substatement.
316 This is in addition to ess-continued-statement-offset.
318 Extra indentation for line if it starts with an open brace.
319 `ess-arg-function-offset'
320 Extra indent for internal substatements of function `foo' that called
321 in `arg=foo(...)' form.
322 If not number, the statements are indented at open-parenthesis following
324 `ess-expression-offset'
325 Extra indent for internal substatements of `expression' that specified
326 in `obj <- expression(...)' form.
327 If not number, the statements are indented at open-parenthesis following
329 `ess-brace-imaginary-offset'
330 An open brace following other text is treated as if it were
331 this far to the right of the start of its line.
333 Extra indentation for line if it starts with `else'.
334 `ess-close-brace-offset'
335 Extra indentation for closing braces.
337 Non-nil means distinguish between #, ##, and ### for indentation.
339 Furthermore, \\[ess-set-style] command enables you to set up predefined ess-mode
340 indentation style. At present, predefined style are `BSD', `GNU', `K&R', `C++',
341 `CLB' (quoted from C language style)."
343 (kill-all-local-variables) ;; NOTICE THIS!
344 (ess-setq-vars-local alist
)
345 (ess-write-to-dribble-buffer
346 (format "(ess-mode-1): ess-language=%s, ess-dialect=%s buf=%s \n"
350 ;; (ess-write-to-dribble-buffer
351 ;; (format "(ess-mode-1.2): ess-process= %s \n"
352 ;; (ess-local-process-name ess-local-process-name "none")))
353 (ess-write-to-dribble-buffer
354 (format "(ess-mode-1.5): alist=%s \n"
356 (setq major-mode
'ess-mode
)
357 (setq mode-name
(concat "ESS[" ess-language
"]")) ; was ess-dialect
358 ;; The following line does the next 20 or so :-).
359 (ess-write-to-dribble-buffer
360 (format "(ess-mode-1.6): editing-alist=%s \n"
361 ess-mode-editing-alist
))
362 (ess-setq-vars-local ess-mode-editing-alist
)
364 (use-local-map ess-mode-map
)
365 (set-syntax-table ess-mode-syntax-table
)
367 ;; Keep <tabs> out of the code.
368 (make-local-variable 'indent-tabs-mode
)
369 (setq indent-tabs-mode nil
)
371 ;; (make-local-variable 'paragraph-start)
372 ;; (setq paragraph-start (concat "^$\\|" page-delimiter))
373 ;; (make-local-variable 'paragraph-separate)
374 ;; (setq paragraph-separate paragraph-start)
375 ;; (make-local-variable 'paragraph-ignore-fill-prefix)
376 ;; (setq paragraph-ignore-fill-prefix t)
377 ;; (make-local-variable 'indent-line-function)
378 ;; (setq indent-line-function 'ess-indent-line)
379 ;; (make-local-variable 'require-final-newline)
380 ;; (setq require-final-newline t)
381 ;; (make-local-variable 'comment-start)
382 ;; (setq comment-start "#")
383 ;; (make-local-variable 'comment-start-skip)
384 ;; (setq comment-start-skip "#+ *")
385 ;; (make-local-variable 'comment-column)
386 ;; (setq comment-column 40)
387 ;; (make-local-variable 'comment-indent-function)
388 ;; (setq comment-indent-function 'ess-comment-indent)
389 ;; (make-local-variable 'parse-sexp-ignore-comments)
390 ;; (setq parse-sexp-ignore-comments t)
391 ;; (ess-set-style ess-default-style)
392 ;; (make-local-variable 'ess-local-process-name)
393 ;; (make-local-variable 'ess-keep-dump-files)
394 (put 'ess-local-process-name
'permanent-local t
) ; protect from RCS
395 (setq mode-line-process
;; AJR: in future, XEmacs will use modeline-process.
396 '(" [" (ess-local-process-name ess-local-process-name
"none") "]"))
398 (ess-load-object-name-db-file)
399 (run-hooks 'ess-mode-hook
)
400 (ess-write-to-dribble-buffer "\nFinished setting up ESS-mode.\n"))
402 ;;*;; User commands in ess-mode
404 ;;;*;;; Handy commands
406 (defun ess-execute-in-tb ()
407 "Like `ess-execute', but always evaluates in temp buffer."
409 (let ((ess-execute-in-process-buffer nil
))
410 (call-interactively 'ess-execute
)))
412 ;;;*;;; Buffer motion/manipulation commands
414 (defun ess-beginning-of-function ()
415 "Leave (and return) the point at the beginning of the current ESS function."
417 (let ((init-point (point))
419 ;;DBG (ess-write-to-dribble-buffer "ess-BEG-of-fun:")
420 ;; in case we're sitting in a function header:
421 (if (search-forward "(" (ess-line-end-position 2) t
); at most end of next line
424 (if (re-search-backward ess-function-pattern
(point-min) t
)
426 (goto-char init-point
)
427 (error "Point is not in a function."))
430 ;;DBG (ess-write-to-dribble-buffer
431 ;;DBG (format "Match,Pt:(%d,%d),%d" (match-beginning 0)(match-end 0) beg))
432 (forward-list 1) ; get over arguments
433 ;;DBG (ess-write-to-dribble-buffer ":")
434 ;; The following used to bomb "Unbalanced parentheses", n1, n2
435 ;; when the above (search-forward "(" ..) wasn't delimited :
436 (forward-sexp 1) ; move over braces
437 ;;DBG (ess-write-to-dribble-buffer "|")
440 ;; current function must begin and end around point
441 (setq done
(and (>= end init-point
) (<= beg init-point
))))
442 ;;DBG (ess-write-to-dribble-buffer (format "found beg=%d\n" beg))
445 (defun ess-end-of-function (&optional beginning
)
446 "Leave the point at the end of the current ESS function.
447 Optional argument for location of beginning. Return '(beg end)."
450 (goto-char beginning
)
451 (setq beginning
(ess-beginning-of-function)))
452 (forward-list 1) ; get over arguments
453 (forward-sexp 1) ; move over braces
454 ;;DBG (ess-write-to-dribble-buffer "ess-END-of-fun: found ok\n")
455 (list beginning
(point))
458 ;;; Kurt's version, suggested 970306.
459 (defun ess-mark-function ()
460 "Put mark at end of ESS function, point at beginning."
462 (let ((beg (ess-beginning-of-function)))
464 (ess-end-of-function beg
)
465 (exchange-point-and-mark)))
467 ;; Donated by Stephen Eglen, 2001-08-29:
468 ;; This command is analogous to `narrow-to-defun' (elisp)
469 ;; and `py-narrow-to-defun' (python)."
470 (defun ess-narrow-to-defun ()
471 "Make text outside current function invisible.
472 If text is already narrowed, this is removed before narrowing to the
475 ;; if point is not in a function, ess-end-of-function catches the error.
478 (let* ((beg-end (ess-end-of-function)))
479 (narrow-to-region (nth 0 beg-end
) (nth 1 beg-end
)))))
483 (defun ess-check-modifications nil
484 "Check whether loading this file would overwrite some ESS objects
485 which have been modified more recently than this file, and confirm
486 if this is the case."
487 ;; FIXME: this should really cycle through all top-level assignments in
489 (and (buffer-file-name) ess-filenames-map
490 (let ((sourcemod (nth 5 (file-attributes (buffer-file-name))))
493 (goto-char (point-min))
494 ;; Get name of assigned object, if we can find it
498 "^\\s *\"?\\(\\(\\sw\\|\\s_\\)+\\)\"?\\s *[<_]"
501 (buffer-substring (match-beginning 1)
504 sourcemod
; the file may have been deleted
505 objname
; may not have been able to
507 (ess-modtime-gt (ess-object-modtime objname
) sourcemod
)
511 "The ESS object %s is newer than this file. Continue?"
513 (error "Aborted")))))
515 (defun ess-check-source (fname)
516 "If file FNAME has an unsaved buffer, offer to save it.
517 Returns t if the buffer existed and was modified, but was not saved."
518 (let ((buff (get-file-buffer fname
)))
519 ;; RMH: Corrections noted below are needed for C-c C-l to work
520 ;; correctly when issued from *S* buffer.
521 ;; The following barfs since
522 ;; 1. `if' does not accept a buffer argument, `not' does.
523 ;; 2. (buffer-file-name) is not necessarily defined for *S*
525 ;; (let ((deleted (not (file-exists-p (buffer-file-name)))))
526 ;; Next 2 lines are RMH's solution:
528 (let ((deleted (not (file-exists-p fname
))))
529 (if (and deleted
(not (buffer-modified-p buff
)))
530 ;; Buffer has been silently deleted, so silently save
533 (set-buffer-modified-p t
)
535 (if (and (buffer-modified-p buff
)
536 (or ess-mode-silently-save
538 (format "Save buffer %s first? "
539 (buffer-name buff
)))))
543 (buffer-modified-p buff
)))))
545 (defun ess-parse-errors (showerr)
546 "Jump to error in last loaded ESS source file.
547 With prefix argument, only shows the errors ESS reported."
549 (ess-make-buffer-current)
550 (let ((errbuff (get-buffer ess-error-buffer-name
)))
552 (error "You need to do a load first!")
554 (goto-char (point-max))
557 "^\\(Syntax error: .*\\) at line \\([0-9]*\\), file \\(.*\\)$"
560 (let* ((filename (buffer-substring (match-beginning 3) (match-end 3)))
561 (fbuffer (get-file-buffer filename
))
562 (linenum (string-to-int (buffer-substring (match-beginning 2) (match-end 2))))
563 (errmess (buffer-substring (match-beginning 1) (match-end 1))))
565 (ess-display-temp-buffer errbuff
)
567 (setq fbuffer
(find-file-noselect filename
))
571 (pop-to-buffer fbuffer
)
574 (message "Not a syntax error.")
575 (ess-display-temp-buffer errbuff
)))))
577 ;;*;; ESS code formatting/indentation
579 ;;;*;;; User commands
581 (defun ess-electric-brace (arg)
582 "Insert character and correct line's indentation."
588 (skip-chars-backward " \t")
590 (if ess-auto-newline
(progn (ess-indent-line) (newline) t
) nil
)))
592 (insert last-command-char
)
597 ;; (newline) may have done auto-fill
598 (setq insertpos
(- (point) 2))
601 (if insertpos
(goto-char (1+ insertpos
)))
605 (goto-char insertpos
)
606 (self-insert-command (prefix-numeric-value arg
)))
607 (self-insert-command (prefix-numeric-value arg
)))))
609 (defun ess-indent-command (&optional whole-exp
)
610 "Indent current line as ESS code, or in some cases insert a tab character.
611 If `ess-tab-always-indent' is non-nil (the default), always indent
612 current line. Otherwise, indent the current line only if point is at
613 the left margin or in the line's indentation; otherwise insert a tab.
614 A numeric argument, regardless of its value, means indent rigidly all
615 the lines of the expression starting after point so that this line
616 becomes properly indented. The relative indentation among the lines
617 of the expression are preserved."
620 ;; If arg, always indent this line as S
621 ;; and shift remaining lines of expression the same amount.
622 (let ((shift-amt (ess-indent-line))
625 (if ess-tab-always-indent
635 (indent-code-rigidly beg end shift-amt
)))
636 (if (and (not ess-tab-always-indent
)
638 (skip-chars-backward " \t")
643 (defun ess-indent-exp ()
644 "Indent each line of the ESS grouping following point."
646 (let ((indent-stack (list nil
))
647 (contain-stack (list (point)))
648 (case-fold-search nil
)
650 outer-loop-done innerloop-done state ostate
660 (setq outer-loop-done nil
)
661 (while (and (not (eobp)) (not outer-loop-done
))
662 (setq last-depth next-depth
)
663 ;; Compute how depth changes over this line
664 ;; plus enough other lines to get to one that
665 ;; does not end inside a comment or string.
666 ;; Meanwhile, do appropriate indentation on comment lines.
667 (setq innerloop-done nil
)
668 (while (and (not innerloop-done
)
669 (not (and (eobp) (setq outer-loop-done t
))))
671 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
673 (setq next-depth
(car state
))
674 (if (and (car (cdr (cdr state
)))
675 (>= (car (cdr (cdr state
))) 0))
676 (setq last-sexp
(car (cdr (cdr state
)))))
677 (if (or (nth 4 ostate
))
680 (and (ess-indent-line)
681 (setcar (nthcdr 4 state
) nil
)))
682 (if (or (nth 3 state
))
684 (setq innerloop-done t
)))
685 (if (<= next-depth
0)
686 (setq outer-loop-done t
))
689 ;; If this line had ..))) (((.. in it, pop out of the levels
690 ;; that ended anywhere in this line, even if the final depth
691 ;; doesn't indicate that they ended.
692 (while (> last-depth
(nth 6 state
))
693 (setq indent-stack
(cdr indent-stack
)
694 contain-stack
(cdr contain-stack
)
695 last-depth
(1- last-depth
)))
696 (if (/= last-depth next-depth
)
697 (setq last-sexp nil
))
698 ;; Add levels for any parens that were started in this line.
699 (while (< last-depth next-depth
)
700 (setq indent-stack
(cons nil indent-stack
)
701 contain-stack
(cons nil contain-stack
)
702 last-depth
(1+ last-depth
)))
703 (if (null (car contain-stack
))
704 (setcar contain-stack
(or (car (cdr state
))
705 (save-excursion (forward-sexp -
1)
708 (skip-chars-forward " \t")
711 (if (and (car indent-stack
)
712 (>= (car indent-stack
) 0))
713 ;; Line is on an existing nesting level.
714 ;; Lines inside parens are handled specially.
715 (if (/= (char-after (car contain-stack
)) ?
{)
716 (setq this-indent
(car indent-stack
))
717 ;; Line is at statement level.
718 ;; Is it a new statement? Is it an else?
719 ;; Find last non-comment character before this line
721 (setq at-else
(looking-at "else\\W"))
722 (setq at-brace
(= (following-char) ?
{))
723 (ess-backward-to-noncomment opoint
)
724 (if (ess-continued-statement-p)
725 ;; Preceding line did not end in comma or semi;
726 ;; indent this line ess-continued-statement-offset
727 ;; more than previous.
729 (ess-backward-to-start-of-continued-exp (car contain-stack
))
731 (+ ess-continued-statement-offset
(current-column)
732 (if at-brace ess-continued-brace-offset
0))))
733 ;; Preceding line ended in comma or semi;
734 ;; use the standard indent for this level.
736 (progn (ess-backward-to-start-of-if opoint
)
737 (setq this-indent
(+ ess-else-offset
738 (current-indentation))))
739 (setq this-indent
(car indent-stack
))))))
740 ;; Just started a new nesting level.
741 ;; Compute the standard indent for this level.
742 (let ((val (ess-calculate-indent
743 (if (car indent-stack
)
744 (- (car indent-stack
))))))
746 (setq this-indent val
))))
747 ;; Adjust line indentation according to its contents
748 (if (= (following-char) ?
})
749 ;;(setq this-indent (- this-indent ess-indent-level)))
750 (setq this-indent
(+ this-indent
751 (- ess-close-brace-offset ess-indent-level
))))
752 (if (= (following-char) ?
{)
753 (setq this-indent
(+ this-indent ess-brace-offset
)))
754 ;; Put chosen indentation into effect.
755 (or (= (current-column) this-indent
)
756 (= (following-char) ?\
#)
758 (delete-region (point) (progn (beginning-of-line) (point)))
759 (indent-to this-indent
)))
760 ;; Indent any comment following the text.
761 (or (looking-at comment-start-skip
)
762 (if (re-search-forward comment-start-skip
763 (save-excursion (end-of-line)
765 (progn (indent-for-comment) (beginning-of-line))))))))))
766 ;; (message "Indenting ESS expression...done")
768 ;;;*;;; Support functions for indentation
770 (defun ess-comment-indent ()
771 (if (looking-at "###")
773 (if (looking-at "##")
774 (let ((tem (ess-calculate-indent)))
775 (if (listp tem
) (car tem
) tem
))
776 (skip-chars-backward " \t")
777 (max (if (bolp) 0 (1+ (current-column)))
780 (defun ess-indent-line ()
781 "Indent current line as ESS code.
782 Return the amount the indentation changed by."
783 (let ((indent (ess-calculate-indent nil
))
785 (case-fold-search nil
)
786 (pos (- (point-max) (point))))
789 (cond ((eq indent nil
)
790 (setq indent
(current-indentation)))
792 (skip-chars-forward " \t")
793 (if (and ess-fancy-comments
(looking-at "###"))
795 (if (and ess-fancy-comments
797 (not (looking-at "##")))
798 (setq indent comment-column
)
799 (if (eq indent t
) (setq indent
0))
800 (if (listp indent
) (setq indent
(car indent
)))
801 (cond ((and (looking-at "else\\b")
802 (not (looking-at "else\\s_")))
803 (setq indent
(save-excursion
804 (ess-backward-to-start-of-if)
805 (+ ess-else-offset
(current-indentation)))))
806 ((= (following-char) ?
})
809 (- ess-close-brace-offset ess-indent-level
))))
810 ((= (following-char) ?
{)
811 (setq indent
(+ indent ess-brace-offset
)))))))
812 (skip-chars-forward " \t")
813 (setq shift-amt
(- indent
(current-column)))
814 (if (zerop shift-amt
)
815 (if (> (- (point-max) pos
) (point))
816 (goto-char (- (point-max) pos
)))
817 (delete-region beg
(point))
819 ;; If initial point was within line's indentation,
820 ;; position after the indentation.
821 ;; Else stay at same point in text.
822 (if (> (- (point-max) pos
) (point))
823 (goto-char (- (point-max) pos
))))
826 (defun ess-calculate-indent (&optional parse-start
)
827 "Return appropriate indentation for current line as ESS code.
828 In usual case returns an integer: the column to indent to.
829 Returns nil if line starts inside a string, t if in a comment."
832 (let ((indent-point (point))
833 (case-fold-search nil
)
837 (goto-char parse-start
)
838 (beginning-of-defun))
839 (while (< (point) indent-point
)
840 (setq parse-start
(point))
841 (setq state
(parse-partial-sexp (point) indent-point
0))
842 (setq containing-sexp
(car (cdr state
))))
843 (cond ((or (nth 3 state
) (nth 4 state
))
844 ;; return nil or t if should not change this line
846 ((null containing-sexp
)
847 ;; Line is at top level. May be data or function definition,
849 (if (and (/= (following-char) ?\
{)
851 (ess-backward-to-noncomment (point-min))
852 (ess-continued-statement-p)))
853 ess-continued-statement-offset
854 0)) ; Unless it starts a function body
855 ((/= (char-after containing-sexp
) ?
{)
856 ;; line is expression, not statement:
857 ;; indent to just after the surrounding open.
858 (goto-char containing-sexp
)
859 (let ((bol (save-excursion (beginning-of-line) (point))))
861 ;; modified by shiba@isac 7.3.1992
862 (cond ((and (numberp ess-expression-offset
)
863 (re-search-backward "[ \t]*expression[ \t]*" bol t
))
864 ;; This regexp match every "expression".
868 (skip-chars-forward " \t")
870 (+ (current-column) ess-expression-offset
))
871 ((and (numberp ess-arg-function-offset
)
873 "=[ \t]*\\s\"*\\(\\w\\|\\s_\\)+\\s\"*[ \t]*"
876 (+ (current-column) ess-arg-function-offset
))
877 ;; "expression" is searched before "=".
880 (progn (goto-char (1+ containing-sexp
))
881 (current-column))))))
883 ;; Statement level. Is it a continuation or a new statement?
884 ;; Find previous non-comment character.
885 (goto-char indent-point
)
886 (ess-backward-to-noncomment containing-sexp
)
887 ;; Back up over label lines, since they don't
888 ;; affect whether our line is a continuation.
889 (while (eq (preceding-char) ?\
,)
890 (ess-backward-to-start-of-continued-exp containing-sexp
)
892 (ess-backward-to-noncomment containing-sexp
))
893 ;; Now we get the answer.
894 (if (ess-continued-statement-p)
895 ;; This line is continuation of preceding line's statement;
896 ;; indent ess-continued-statement-offset more than the
897 ;; previous line of the statement.
899 (ess-backward-to-start-of-continued-exp containing-sexp
)
900 (+ ess-continued-statement-offset
(current-column)
901 (if (save-excursion (goto-char indent-point
)
902 (skip-chars-forward " \t")
903 (eq (following-char) ?
{))
904 ess-continued-brace-offset
0)))
905 ;; This line starts a new statement.
906 ;; Position following last unclosed open.
907 (goto-char containing-sexp
)
908 ;; Is line first statement after an open-brace?
910 ;; If no, find that first statement and indent like it.
913 (while (progn (skip-chars-forward " \t\n")
915 ;; Skip over comments following openbrace.
917 ;; The first following code counts
918 ;; if it is before the line we want to indent.
919 (and (< (point) indent-point
)
921 ;; If no previous statement,
922 ;; indent it relative to line brace is on.
923 ;; For open brace in column zero, don't let statement
924 ;; start there too. If ess-indent-level is zero,
925 ;; use ess-brace-offset +
926 ;; ess-continued-statement-offset instead.
927 ;; For open-braces not the first thing in a line,
928 ;; add in ess-brace-imaginary-offset.
929 (+ (if (and (bolp) (zerop ess-indent-level
))
930 (+ ess-brace-offset ess-continued-statement-offset
)
932 ;; Move back over whitespace before the openbrace.
933 ;; If openbrace is not first nonwhite thing on the line,
934 ;; add the ess-brace-imaginary-offset.
935 (progn (skip-chars-backward " \t")
936 (if (bolp) 0 ess-brace-imaginary-offset
))
937 ;; If the openbrace is preceded by a parenthesized exp,
938 ;; move to the beginning of that;
939 ;; possibly a different line
941 (if (eq (preceding-char) ?\
))
943 ;; Get initial indentation of the line we are on.
944 (current-indentation))))))))))
946 (defun ess-continued-statement-p ()
949 (cond ((memq (preceding-char) '(nil ?\
, ?\
; ?\} ?\{ ?\]))
952 ((= (preceding-char) ?\
))
954 (looking-at "if\\b[ \t]*(\\|function\\b[ \t]*(\\|for\\b[ \t]*(\\|while\\b[ \t]*("))
955 ((progn (forward-sexp -
1)
956 (and (looking-at "else\\b\\|repeat\\b")
957 (not (looking-at "else\\s_\\|repeat\\s_"))))
958 (skip-chars-backward " \t")
960 (= (preceding-char) ?\
;)))
962 (progn (goto-char eol
)
963 (skip-chars-backward " \t")
964 (or (and (> (current-column) 1)
965 (save-excursion (backward-char 1)
966 (looking-at "[-:+*/_><=]")))
967 (and (> (current-column) 3)
968 (progn (backward-char 3)
969 (looking-at "%[^ \t]%"))))))))))
971 (defun ess-backward-to-noncomment (lim)
974 (skip-chars-backward " \t\n\f" lim
)
975 (setq opoint
(point))
977 (search-forward "#" opoint
'move
)
978 (skip-chars-backward " \t#")
979 (setq stop
(or (/= (preceding-char) ?
\n) (<= (point) lim
)))
981 (beginning-of-line)))))
983 (defun ess-backward-to-start-of-continued-exp (lim)
984 (if (= (preceding-char) ?\
))
988 (goto-char (1+ lim
)))
989 (skip-chars-forward " \t"))
991 (defun ess-backward-to-start-of-if (&optional limit
)
992 "Move to the start of the last ``unbalanced'' if."
993 (or limit
(setq limit
(save-excursion (beginning-of-defun) (point))))
995 (case-fold-search nil
))
996 (while (not (zerop if-level
))
998 (cond ((looking-at "else\\b")
999 (setq if-level
(1+ if-level
)))
1000 ((looking-at "if\\b")
1001 (setq if-level
(1- if-level
)))
1004 (goto-char limit
))))))
1006 ;;;*;;; Predefined indentation styles
1008 (defun ess-set-style (&optional style quiet
)
1009 "Set up the `ess-mode' style variables from the `ess-style' variable
1010 or if STYLE argument is given, use that. It makes the ESS indentation
1011 style variables buffer local."
1014 (let ((ess-styles (mapcar 'car ess-style-alist
)))
1017 (let ((style-string ; get style name with completion
1020 "Set ESS mode indentation style (default %s): "
1022 (vconcat ess-styles
)
1023 (function (lambda (arg) (memq arg ess-styles
))))))
1024 (if (string-equal "" style-string
)
1026 (intern style-string
)))))
1027 (setq style
(or style ess-style
)) ; use ess-style if style is nil
1028 (make-local-variable 'ess-style
)
1029 (if (memq style ess-styles
)
1030 (setq ess-style style
)
1031 (error (concat "Bad ESS style: " style
)))
1033 (message "ESS-style: %s" ess-style
))
1034 ; finally, set the indentation style variables making each one local
1035 (mapcar (function (lambda (ess-style-pair)
1036 (make-local-variable (car ess-style-pair
))
1037 (set (car ess-style-pair
)
1038 (cdr ess-style-pair
))))
1039 (cdr (assq ess-style ess-style-alist
)))
1042 ;;*;; Creating and manipulating dump buffers
1044 ;;;*;;; The user command
1046 (defun ess-dump-object-into-edit-buffer (object)
1047 "Edit an ESS object in its own buffer.
1049 Without a prefix argument, this simply finds the file pointed to by
1050 `ess-source-directory'. If this file does not exist, or if a
1051 prefix argument is given, a dump() command is sent to the ESS process to
1052 generate the source buffer."
1056 (ess-force-buffer-current "Process to dump from: ")
1057 (ess-read-object-name "Object to edit: ")))
1058 (let* ((dirname (file-name-as-directory
1059 (if (stringp ess-source-directory
)
1060 ess-source-directory
1063 (process-buffer (get-ess-process
1064 ess-local-process-name
)))
1065 (ess-setq-vars-local ess-customize-alist
)
1066 (apply ess-source-directory nil
)))))
1067 (filename (concat dirname
(format ess-dump-filename-template object
)))
1068 (old-buff (get-file-buffer filename
)))
1070 ;; If the directory doesn't exist, offer to create it
1071 (if (file-exists-p (directory-file-name dirname
)) nil
1072 (if (y-or-n-p ; Approved
1073 (format "Directory %s does not exist. Create it? " dirname
))
1074 (make-directory (directory-file-name dirname
))
1075 (error "Directory %s does not exist." dirname
)))
1078 ;; (1) Pop to an existing buffer containing the file in question
1079 ;; (2) Find an existing file
1080 ;; (3) Create a new file by issuing a dump() command to S
1081 ;; Force option (3) if there is a prefix arg
1083 (if current-prefix-arg
1084 (ess-dump-object object filename
)
1087 (pop-to-buffer old-buff
)
1088 (message "Popped to edit buffer."))
1089 ;; No current buffer containing desired file
1090 (if (file-exists-p filename
)
1092 (ess-find-dump-file-other-window filename
)
1093 (message "Read %s" filename
))
1094 ;; No buffer and no file
1095 (ess-dump-object object filename
))))))
1097 (defun ess-dump-object (object filename
)
1098 "Dump the ESS object OBJECT into file FILENAME."
1099 (let ((complete-dump-command (format inferior-ess-dump-command
1101 (if (file-writable-p filename
) nil
1102 (error "Can't dump %s as %f is not writeable." object filename
))
1104 ;; Make sure we start fresh
1105 (if (get-file-buffer filename
)
1106 (or (kill-buffer (get-file-buffer filename
))
1107 (error "Aborted.")))
1109 (ess-command complete-dump-command
)
1110 (message "Dumped in %s" filename
)
1112 (ess-find-dump-file-other-window filename
)
1115 ;;This ensures that the object gets indented according to ess-mode,
1116 ;;not as the R/S deparser does it. At the same time, it gets rid
1117 ;;of the mess generated by sending TAB characters to the readline
1118 ;;functions in R when you eval-buffer-*.
1119 (indent-region (point-min-marker) (point-max-marker) nil
)
1121 ;; Don't make backups for temporary files; it only causes clutter.
1122 ;; The ESS object itself is a kind of backup, anyway.
1123 (if ess-keep-dump-files nil
1124 (make-local-variable 'make-backup-files
)
1125 (setq make-backup-files nil
))
1127 ;; Don't get confirmation to delete dumped files when loading
1128 (if (eq ess-keep-dump-files
'check
)
1129 (setq ess-keep-dump-files nil
))
1131 ;; Delete the file if necessary
1132 (if ess-delete-dump-files
1133 (delete-file (buffer-file-name)))))
1135 (defun ess-find-dump-file-other-window (filename)
1136 "Find ESS source file FILENAME in another window."
1138 (if (file-exists-p filename
) nil
1139 (ess-write-to-dribble-buffer
1140 (format "%s does not exist. Bad dump, starting fresh." filename
)))
1142 ;; Generate a buffer with the dumped data
1143 (find-file-other-window filename
)
1144 (ess-mode ess-customize-alist
)
1146 (auto-save-mode 1) ; Auto save in this buffer
1147 (setq ess-local-process-name ess-current-process-name
)
1149 (if ess-function-template
1151 (goto-char (point-max))
1152 (if (re-search-backward ess-dumped-missing-re nil t
)
1154 (replace-match ess-function-template t t
)
1155 (set-buffer-modified-p nil
) ; Don't offer to save if killed now
1156 (goto-char (point-min))
1158 ;; This may fail if there are no opens
1162 (defun ess-dump-object-into-edit-buffer-other-frame (object)
1163 "Edit an ESS object in its own frame."
1164 (switch-to-buffer-other-frame (ess-dump-object-into-edit-buffer object
)))
1168 \f ; Local variables section
1170 ;;; This file is automatically placed in Outline minor mode.
1171 ;;; The file is structured as follows:
1174 ;;; Subsections: ;;;*;;;
1175 ;;; Components: defuns, defvars, defconsts
1176 ;;; Random code beginning with a ;;;;* comment
1178 ;;; Local variables:
1179 ;;; mode: emacs-lisp
1180 ;;; outline-minor-mode: nil
1181 ;;; mode: outline-minor
1182 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
1185 ;;; ess-mode.el ends here