mini-change in (ess-dump-to-src); two days ago
[ess.git] / lisp / essl-s.el
blobd45325be56cfd8ca3f66de5b0ba2771e0f569d14
1 ;;; essl-s.el --- Support for editing S source code
3 ;; Copyright (C) 1989-1997 D. Bates, Kademan, Ritter, D.M. Smith, K. Hornik,
4 ;; R.M. Heiberger, M. Maechler, and A.J. Rossini.
5 ;; Copyright (C) 1998-2005 A.J. Rossini, Rich M. Heiberger, Martin
6 ;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
8 ;; Original Author: A.J. Rossini <rossini@biostat.washington.edu>
9 ;; Created: 26 Aug 1997
10 ;; Maintainers: ESS-core <ESS-core@stat.math.ethz.ch>
12 ;; This file is part of ESS (Emacs Speaks Statistics).
14 ;; This file is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
19 ;; This file is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to
26 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;;; Commentary:
30 ;; Code for general editing S source code (specializes to S, S+, R).
32 ;;; Code:
34 \f ; Requires and autoloads
36 (ess-message "[essl-s:] (def** ) only ...")
38 \f ; Configuration variables
40 (defvar S-syntax-table nil "Syntax table for S code.")
41 (if S-syntax-table
42 nil
43 (setq S-syntax-table (make-syntax-table))
44 (modify-syntax-entry ?\\ "\\" S-syntax-table)
45 (modify-syntax-entry ?+ "." S-syntax-table)
46 (modify-syntax-entry ?- "." S-syntax-table)
47 (modify-syntax-entry ?= "." S-syntax-table)
48 (modify-syntax-entry ?% "." S-syntax-table)
49 (modify-syntax-entry ?< "." S-syntax-table)
50 (modify-syntax-entry ?> "." S-syntax-table)
51 (modify-syntax-entry ?& "." S-syntax-table)
52 (modify-syntax-entry ?| "." S-syntax-table)
53 (modify-syntax-entry ?\' "\"" S-syntax-table)
54 (modify-syntax-entry ?\" "\"" S-syntax-table)
55 (modify-syntax-entry ?# "<" S-syntax-table) ; open comment
56 (modify-syntax-entry ?\n ">" S-syntax-table) ; close comment
57 ;;(modify-syntax-entry ?. "w" S-syntax-table) ; "." used in S obj names
58 (modify-syntax-entry ?. "_" S-syntax-table) ; see above/below,
59 ; plus consider separation.
60 (modify-syntax-entry ?$ "_" S-syntax-table); foo$comp = 1 symbol(completion)
61 (modify-syntax-entry ?@ "_" S-syntax-table); foo@slot = 1 symbol(completion)
62 (modify-syntax-entry ?_ "." S-syntax-table)
63 (modify-syntax-entry ?* "." S-syntax-table)
64 (modify-syntax-entry ?< "." S-syntax-table)
65 (modify-syntax-entry ?> "." S-syntax-table)
66 (modify-syntax-entry ?/ "." S-syntax-table))
68 (defvar R-editing-alist
69 '((paragraph-start . (concat "\\s-*$\\|" page-delimiter))
70 (paragraph-separate . (concat "\\s-*$\\|" page-delimiter))
71 (paragraph-ignore-fill-prefix . t)
72 (require-final-newline . t)
73 (comment-start . "#")
74 (comment-add . 1)
75 (comment-start-skip . "#+ *")
76 (comment-column . 40)
77 ;;(comment-indent-function . 'S-comment-indent)
78 ;;(ess-comment-indent . 'S-comment-indent)
79 ;;(ess-indent-line . 'S-indent-line)
80 ;;(ess-calculate-indent . 'S-calculate-indent)
81 (indent-line-function . 'S-indent-line)
82 (parse-sexp-ignore-comments . t)
83 (ess-set-style . ess-default-style)
84 (ess-local-process-name . nil)
85 ;;(ess-keep-dump-files . 'ask)
86 (ess-mode-syntax-table . S-syntax-table)
87 ;; For Changelog add, require ' ' before <- : "attr<-" is a function name :
88 (add-log-current-defun-header-regexp . "^\\(.+\\)\\s-+<-[ \t\n]*function")
89 (font-lock-defaults . '(ess-R-mode-font-lock-keywords
90 nil nil ((?\. . "w") (?\_ . "w"))))
92 "General options for R source files.")
94 (defvar S-editing-alist
95 ;; copy the R-list and modify :
96 (let ((S-alist (copy-alist R-editing-alist)))
97 (setcdr (assoc 'font-lock-defaults S-alist)
98 (quote '(ess-S-mode-font-lock-keywords nil nil ((?\. . "w")))))
99 ;; ^^ extra quote is needed - why?
100 S-alist)
101 "General options for editing S and S+ source files.")
103 (defvar inferior-S-language-start
104 '(concat "options("
105 "STERM='" ess-STERM "'"
106 (if ess-editor (concat ", editor='" ess-editor "'"))
107 (if ess-pager (concat ", pager='" ess-pager "', help.pager='" ess-pager "'"))
108 ")")
109 "S language expression for startup -- default for all S dialects.")
111 (defconst S-common-cust-alist
112 '((ess-language . "S")
113 (inferior-ess-exit-command . "q()\n")
114 (inferior-ess-language-start . (eval inferior-S-language-start))
115 ;;harmful for shell-mode's C-a: -- but "necessary" for ESS-help(?) :
116 (comint-use-prompt-regexp-instead-of-fields . t) ;; emacs 21 and up
118 "S-language common settings for all <dialect>-customize-alist s"
121 (defconst S+common-cust-alist
122 (append
123 '((ess-suffix . "S")
124 (ess-mode-syntax-table . S-syntax-table)
125 (ess-help-sec-regex . ess-help-S+-sec-regex)
126 (ess-help-sec-keys-alist . ess-help-S+sec-keys-alist)
127 (ess-change-sp-regexp . ess-S+-change-sp-regexp)
128 (ess-cmd-delay . (if (featurep 'xemacs); needs much less delay
129 (* 0.1 ess-S+-cmd-delay)
130 ess-S+-cmd-delay))
131 (ess-function-pattern . ess-S-function-pattern)
132 (ess-function-template . " <- \n#\nfunction()\n{\n\n}\n")
133 (ess-dump-filename-template . (ess-replace-regexp-in-string
134 "S$" ess-suffix ; in the one from custom:
135 ess-dump-filename-template-proto))
137 (ess-mode-editing-alist . S-editing-alist)
139 (ess-dumped-missing-re
140 . "\\(\\(<-\\|=\\)\nDumped\n\\'\\)\\|\\(\\(<-\\|=\\)\\(\\s \\|\n\\)*\\'\\)")
141 (ess-syntax-error-re
142 . "\\(Syntax error: .*\\) at line \\([0-9]*\\), file \\(.*\\)$")
143 (ess-retr-lastvalue-command
144 . ".Last.value <- get(\".ess.lvsave\",frame=0)\n")
145 (ess-save-lastvalue-command
146 . "assign(\".ess.lvsave\",.Last.value,frame=0)\n")
148 (inferior-ess-objects-command . inferior-Splus-objects-command)
149 (inferior-ess-font-lock-keywords . inferior-ess-S-font-lock-keywords)
150 (inferior-ess-primary-prompt . "[a-zA-Z0-9() ]*> ?")
151 (inferior-ess-secondary-prompt . "+ ?")
153 (ess-editor . S-editor)
154 (ess-pager . S-pager)
156 S-common-cust-alist)
157 "Common settings for all S+<*>-customize-alist s"
160 ;;; Changes from S to S-PLUS 3.x. (standard S3 should be in essl-s!).
162 (defconst ess-help-S+sec-keys-alist
163 '((?a . "ARGUMENTS:")
164 (?b . "BACKGROUND:")
165 (?B . "BUGS:")
166 (?d . "DESCRIPTION:")
167 (?D . "DETAILS:")
168 (?e . "EXAMPLES:")
169 (?n . "NOTE:")
170 (?O . "OPTIONAL ARGUMENTS:")
171 (?R . "REQUIRED ARGUMENTS:")
172 (?r . "REFERENCES:")
173 (?s . "SEE ALSO:")
174 (?S . "SIDE EFFECTS:")
175 (?u . "USAGE:")
176 (?v . "VALUE:"))
177 "Alist of (key . string) pairs for use in section searching.")
178 ;;; `key' indicates the keystroke to use to search for the section heading
179 ;;; `string' in an S help file. `string' is used as part of a
180 ;;; regexp-search, and so specials should be quoted.
182 ;; S ver.3 (NOT S-Plus)
183 (defconst ess-help-S3-sec-keys-alist
184 '((?a . "ARGUMENTS:")
185 (?b . "BACKGROUND:")
186 (?B . "BUGS:")
187 (?d . "DESCRIPTION:")
188 (?D . "DETAILS:")
189 (?e . "EXAMPLES:")
190 (?n . "NOTE:")
191 (?r . "REFERENCES:")
192 (?s . "SEE ALSO:")
193 (?S . "SIDE EFFECTS:")
194 (?u . "USAGE:")
195 (?v . "VALUE:"))
196 "Help section keys for S ver.3.")
198 ;; S ver.4 (NOT S-Plus)
199 (defconst ess-help-S4-sec-keys-alist
200 '((?a . "ARGUMENTS:")
201 (?b . "BACKGROUND:")
202 (?B . "BUGS:")
203 (?d . "DESCRIPTION:")
204 (?D . "DETAILS:")
205 (?e . "EXAMPLES:")
206 (?n . "NOTE:")
207 (?r . "REFERENCES:")
208 (?s . "SEE ALSO:")
209 (?S . "SIDE EFFECTS:")
210 (?u . "USAGE:")
211 (?v . "VALUE:"))
212 "Help section keys for S4.")
214 ;; R
215 (defconst ess-help-R-sec-keys-alist
216 '((?a . "\\s *Arguments:")
217 (?d . "\\s *Description:")
218 (?D . "\\s *Details:")
219 (?e . "\\s *Examples:")
220 (?n . "\\s *Note:")
221 (?r . "\\s *References:")
222 (?s . "\\s *See Also:")
223 (?u . "\\s *Usage:")
224 (?v . "\\s *Value[s]?") ;
226 "Alist of (key . string) pairs for use in help section searching.")
229 (defconst ess-help-S+-sec-regex "^[A-Z. ---]+:$"
230 "Reg(ular) Ex(pression) of section headers in help file.")
232 (defconst ess-help-R-sec-regex "^[A-Z][a-z].+:$"
233 "Reg(ular) Ex(pression) of section headers in help file.")
235 ;;; S-mode extras of Martin Maechler, Statistik, ETH Zurich.
236 ;;; See also ./ess-utils.el
238 (defvar ess-function-outline-file
239 (concat ess-etc-directory "/function-outline.S")
240 "The file name of the ess-function outline that is to be inserted at point,
241 when \\<ess-mode-map>\\[ess-insert-function-outline] is used.
242 Placeholders (substituted `at runtime'): $A$ for `Author', $D$ for `Date'.")
244 ;; Use the user's own ~/S/emacs-fun.outline if (s)he has one : ---
245 (let ((outline-file (concat (getenv "HOME") "/S/function-outline.S")))
246 (if (file-exists-p outline-file)
247 (setq ess-function-outline-file outline-file)))
249 \f ; Function Definitions
251 (defun S-comment-indent ()
252 "Indentation for S comments."
254 (if (looking-at "###")
255 (current-column)
256 (if (looking-at "##")
257 (let ((tem (S-calculate-indent)))
258 (if (listp tem) (car tem) tem))
259 (skip-chars-backward " \t")
260 (max (if (bolp) 0 (1+ (current-column)))
261 comment-column))))
263 (defun S-indent-line ()
264 "Indent current line as S code.
265 Return the amount the indentation changed by."
266 (let ((indent (S-calculate-indent nil))
267 beg shift-amt
268 (case-fold-search nil)
269 (pos (- (point-max) (point))))
270 (beginning-of-line)
271 (setq beg (point))
272 (cond ((eq indent nil)
273 (setq indent (current-indentation)))
275 (skip-chars-forward " \t")
276 (if (and ess-fancy-comments (looking-at "###"))
277 (setq indent 0))
278 (if (and ess-fancy-comments
279 (looking-at "#")
280 (not (looking-at "##")))
281 (setq indent comment-column)
282 (if (eq indent t) (setq indent 0))
283 (if (listp indent) (setq indent (car indent)))
284 (cond ((and (looking-at "else\\b")
285 (not (looking-at "else\\s_")))
286 (setq indent (save-excursion
287 (ess-backward-to-start-of-if)
288 (+ ess-else-offset (current-indentation)))))
289 ((= (following-char) ?})
290 (setq indent
291 (+ indent
292 (- ess-close-brace-offset ess-indent-level))))
293 ((= (following-char) ?{)
294 (setq indent (+ indent ess-brace-offset)))))))
295 (skip-chars-forward " \t")
296 (setq shift-amt (- indent (current-column)))
297 (if (zerop shift-amt)
298 (if (> (- (point-max) pos) (point))
299 (goto-char (- (point-max) pos)))
300 (delete-region beg (point))
301 (indent-to indent)
302 ;; If initial point was within line's indentation,
303 ;; position after the indentation.
304 ;; Else stay at same point in text.
305 (if (> (- (point-max) pos) (point))
306 (goto-char (- (point-max) pos))))
307 shift-amt))
309 (defun S-calculate-indent (&optional parse-start)
310 "Return appropriate indentation for current line as S code.
311 In usual case returns an integer: the column to indent to.
312 Returns nil if line starts inside a string, t if in a comment."
313 (save-excursion
314 (beginning-of-line)
315 (let ((indent-point (point))
316 (case-fold-search nil)
317 state
318 containing-sexp)
319 (if parse-start
320 (goto-char parse-start)
321 (beginning-of-defun))
322 (while (< (point) indent-point)
323 (setq parse-start (point))
324 (setq state (parse-partial-sexp (point) indent-point 0))
325 (setq containing-sexp (car (cdr state))))
326 (cond ((or (nth 3 state) (nth 4 state))
327 ;; return nil or t if should not change this line
328 (nth 4 state))
329 ((null containing-sexp)
330 ;; Line is at top level. May be data or function definition,
331 (beginning-of-line)
332 (if (and (/= (following-char) ?\{)
333 (save-excursion
334 (ess-backward-to-noncomment (point-min))
335 (ess-continued-statement-p)))
336 ess-continued-statement-offset
337 0)) ; Unless it starts a function body
338 ((/= (char-after containing-sexp) ?{)
339 ;; line is expression, not statement:
340 ;; indent to just after the surrounding open.
341 (goto-char containing-sexp)
342 (let ((bol (save-excursion (beginning-of-line) (point))))
344 ;; modified by shiba@isac 7.3.1992
345 (cond ((and (numberp ess-expression-offset)
346 (re-search-backward "[ \t]*expression[ \t]*" bol t))
347 ;; This regexp match every "expression".
348 ;; modified by shiba
349 ;;(forward-sexp -1)
350 (beginning-of-line)
351 (skip-chars-forward " \t")
352 ;; End
353 (+ (current-column) ess-expression-offset))
354 ((and (numberp ess-arg-function-offset)
355 (re-search-backward
356 "=[ \t]*\\s\"*\\(\\w\\|\\s_\\)+\\s\"*[ \t]*"
359 (forward-sexp -1)
360 (+ (current-column) ess-arg-function-offset))
361 ;; "expression" is searched before "=".
362 ;; End
365 (progn (goto-char (1+ containing-sexp))
366 (current-column))))))
368 ;; Statement level. Is it a continuation or a new statement?
369 ;; Find previous non-comment character.
370 (goto-char indent-point)
371 (ess-backward-to-noncomment containing-sexp)
372 ;; Back up over label lines, since they don't
373 ;; affect whether our line is a continuation.
374 (while (eq (preceding-char) ?\,)
375 (ess-backward-to-start-of-continued-exp containing-sexp)
376 (beginning-of-line)
377 (ess-backward-to-noncomment containing-sexp))
378 ;; Now we get the answer.
379 (if (ess-continued-statement-p)
380 ;; This line is continuation of preceding line's statement;
381 ;; indent ess-continued-statement-offset more than the
382 ;; previous line of the statement.
383 (progn
384 (ess-backward-to-start-of-continued-exp containing-sexp)
385 (+ ess-continued-statement-offset (current-column)
386 (if (save-excursion (goto-char indent-point)
387 (skip-chars-forward " \t")
388 (eq (following-char) ?{))
389 ess-continued-brace-offset 0)))
390 ;; This line starts a new statement.
391 ;; Position following last unclosed open.
392 (goto-char containing-sexp)
393 ;; Is line first statement after an open-brace?
395 ;; If no, find that first statement and indent like it.
396 (save-excursion
397 (forward-char 1)
398 (while (progn (skip-chars-forward " \t\n")
399 (looking-at "#"))
400 ;; Skip over comments following openbrace.
401 (forward-line 1))
402 ;; The first following code counts
403 ;; if it is before the line we want to indent.
404 (and (< (point) indent-point)
405 (current-column)))
406 ;; If no previous statement,
407 ;; indent it relative to line brace is on.
408 ;; For open brace in column zero, don't let statement
409 ;; start there too. If ess-indent-level is zero, use
410 ;; ess-brace-offset + ess-continued-statement-offset
411 ;; instead.
412 ;; For open-braces not the first thing in a line,
413 ;; add in ess-brace-imaginary-offset.
414 (+ (if (and (bolp) (zerop ess-indent-level))
415 (+ ess-brace-offset ess-continued-statement-offset)
416 ess-indent-level)
417 ;; Move back over whitespace before the openbrace.
418 ;; If openbrace is not first nonwhite thing on the line,
419 ;; add the ess-brace-imaginary-offset.
420 (progn (skip-chars-backward " \t")
421 (if (bolp) 0 ess-brace-imaginary-offset))
422 ;; If the openbrace is preceded by a parenthesized exp,
423 ;; move to the beginning of that;
424 ;; possibly a different line
425 (progn
426 (if (eq (preceding-char) ?\))
427 (forward-sexp -1))
428 ;; Get initial indentation of the line we are on.
429 (current-indentation))))))))))
431 (defun ess-insert-function-outline ()
432 "Insert an S function definition `outline' at point.
433 Uses the file given by the variable `ess-function-outline-file'."
434 (interactive)
435 (let ((oldpos (point)))
436 (save-excursion
437 (insert-file-contents ess-function-outline-file)
438 (if (search-forward "$A$" nil t)
439 (replace-match (user-full-name) 'not-upcase 'literal))
440 (goto-char oldpos)
441 (if (search-forward "$D$" nil t)
442 (replace-match (ess-time-string 'clock) 'not-upcase 'literal)))
443 (goto-char (1+ oldpos))))
445 ;;*;; S/R Pretty-Editing
447 (defun ess-fix-comments (&optional dont-query verbose)
448 "Fix ess-mode buffer so that single-line comments start with at least `##'."
449 (interactive "P")
450 (ess-replace-regexp-dump-to-src "^\\([ \t]*#\\)\\([^#]\\)"
451 "\\1#\\2" dont-query verbose))
453 (defun ess-dump-to-src (&optional dont-query verbose)
454 "Make the changes in an S - dump() file to improve human readability."
455 (interactive "P")
456 (ess-replace-regexp-dump-to-src "^\"\\([a-z.][a-z.0-9]*\\)\" *<-\n"
457 "\n\\1 <- "
458 dont-query verbose 'ensure-ess))
460 (defun ess-num-var-round (&optional dont-query verbose)
461 "Is VERY useful for dump(.)'ed numeric variables; ROUND some of them by
462 replacing endings of 000000*.. and 999999*. Martin Maechler"
463 (interactive "P")
464 (save-excursion
465 (goto-char (point-min))
467 (let ((num 0)
468 (str "")
469 (rgxp "000000+[1-9]?[1-9]?\\>")
470 (to ""))
471 (if dont-query
472 (ess-rep-regexp rgxp to nil nil verbose)
473 (query-replace-regexp rgxp to nil))
475 (while (< num 9)
476 (setq str (concat (int-to-string num) "999999+[0-8]*"))
477 (if (and (numberp verbose) (> verbose 1))
478 (message (format "\nregexp: '%s'" str)))
479 (goto-char (point-min))
480 (ess-rep-regexp str (int-to-string (1+ num))
481 'fixedcase 'literal verbose)
482 (setq num (1+ num))))))
484 (defun ess-fix-dot (before-chars &optional dont-query verbose)
485 "Remove trailing decimal '.' (\"dot\"), before BEFORE; typically from S-plus"
486 ;; typically, before-chars = "]:" or more
487 (ess-replace-regexp-dump-to-src
488 (concat "\\([0-9]\\)\\.\\( *[" before-chars "]\\)")
489 ;; 111 ^
490 "\\1\\2" dont-query verbose))
492 (defun ess-fix-dot-1 (&optional do-query verbose)
493 "Remove trailing decimal '.' (\"dot\"), before ':' or ']', i.e.,
494 in cases where it's ugly and nonsense. DO-QUERY(prefix) asks before replacing."
495 (interactive "P")
496 (ess-fix-dot "]:" (not do-query) verbose))
498 (defun ess-fix-dot-more (&optional dont-query verbose)
499 "Remove trailing decimal '.' (\"dot\", typically from S+) in more cases
500 than `ess-fix-dot-1'."
501 (interactive "P")
502 (ess-fix-dot-1 nil verbose)
503 (ess-fix-dot ",)" dont-query verbose))
505 (defun ess-fix-EQ-assign (&optional dont-query verbose not-all)
506 "Replace \"=\" by \"<-\" in places where it 'might make sense', e.g.,
507 for function assignments and lines not ending in \",\".
508 Be *careful* for list()s of functions and when argument not-all is
509 nil (as by default) !"
510 ;;TODO: "in the few places we can be very sure.."
511 ;;---- is hard in general: local functions: ok; but functions in
512 ;; list(a = function(x) abs(x), b= function(y) bound(y)) *NOT* ok!
513 (interactive "P")
514 (ess-replace-regexp-dump-to-src
515 "^\\( *[a-z.][_a-z.0-9]*\\) *= *\\(function *(\\)"
516 "\\1 <- \\2" dont-query verbose)
518 (unless not-all
519 ;; "too" aggressive {proposing to replace function argument specs}:
520 (ess-replace-regexp-dump-to-src ;; all those *not* ending in ","
521 ;; including Mat[ i, ] = ...,
522 ;; but not `names(x) = "..."' for that is "confused" with plot(x=x,..)
523 "^\\( *[a-z.][][, \"_a-z.0-9]*\\) *= *\\([a-z.0-9({]\\(.*[^,]\\)? *$\\)"
524 "\\1 <- \\2" nil ;; always query - often has many "false positives"
525 verbose)
528 ;;; All of the above three :
529 (defun ess-MM-fix-src (&optional dont-query verbose)
530 "Clean up ess-source code which has been produced by dump(..), and other
531 code typically produced by other tools. Produces more readable code,
532 and one that is well formatted in emacs ess-mode."
533 (interactive "P")
534 ;; each of the following does a save-excursion:
535 (ess-dump-to-src dont-query)
536 (ess-fix-comments dont-query)
537 (ess-num-var-round dont-query verbose)
538 (ess-fix-dot-more dont-query verbose)
539 (ess-fix-EQ-assign dont-query verbose 'not-all)
542 (defun ess-fix-miscellaneous (&optional from verbose)
543 "Fix Miscellaneous S/R `ill-formation's from current \\[point].
544 Particularly use \"<-\"and put spaces around operators."
545 (interactive "d\nP"); Defaults: point and prefix (C-u)
546 (save-excursion
548 (if (string= ess-dialect "R")
549 (progn
550 (require 'essd-r)
551 (R-fix-T-F from (not verbose))))
553 ;; former C and matlab programmers leave trailing ";" :
554 (goto-char from) (ess-rep-regexp "; *$" "" nil 'literal verbose)
555 (goto-char from) (ess-rep-regexp ";\\( *\\)#" "\\1#" nil 'literal verbose)
557 ;;from R 1.9.x "_" is valid in names; here assume no initial / trailing '_'
558 (goto-char from) (ess-rep-regexp " +_ *" " <- " nil 'literal verbose)
559 (goto-char from) (ess-rep-regexp "_ +" " <- " nil 'literal verbose)
561 ;; ensure space around "<-" ---- but only replace if necessary:
562 (goto-char from)
563 (ess-rep-regexp "\\([^< \t\n]\\)\\(<<?-\\)" "\\1 \\2" nil nil verbose)
564 (goto-char from)(ess-rep-regexp "<-\\([^ \t\n]\\)" "<- \\1" nil nil verbose)
565 ;; ensure space around "<" (not in "<-","<=","<<-") and ">" (not ">=") :
566 (goto-char from);; --> " <", care with "->":
567 (ess-rep-regexp "\\([^-< \t\n]\\)\\([<>]\\)" "\\1 \\2" nil nil verbose)
568 ;; ">" -> "> " , for "<", don't split "<-" nor "<<-":
569 (goto-char from)
570 (ess-rep-regexp "\\(>=?\\)\\([^= \t\n]\\)" "\\1 \\2" nil nil verbose)
571 (goto-char from)
572 (ess-rep-regexp "\\(<=?\\)\\([^-<= \t\n]\\)" "\\1 \\2" nil nil t)
574 ;; -- ensure space around "=", "==", "!=" :
575 (goto-char from) ;; --> " ="
576 (ess-rep-regexp "\\([^=!<> ]\\)\\([=!]?\\)=" "\\1 \\2=" nil nil verbose)
577 (goto-char from) (ess-rep-regexp "=\\([^= ]\\)" "= \\1" nil nil verbose)
579 (goto-char from) ;; add a space between "{" and surrounding ..char:
580 (ess-rep-regexp "{\\([.A-Za-z()]\\)" "{ \\1" 'fix nil verbose)
581 (ess-rep-regexp "\\([()]\\){" "\\1 {" 'fix nil verbose)
582 (goto-char from) ;; add a space between "}" and a preceding wordchar:
583 (ess-rep-regexp "\\([A-Za-z0-9()]\\)}" "\\1 }" 'fix nil verbose)
584 (ess-space-around "else" from verbose)
586 ;; add a newline and indent before a "}"
587 ;; --- IFF there's NO "{" or "#" AND some NON-white text on the same line:
588 ;;D (if verbose (message "\t R-fix-misc..: Hard.. '}'"))
589 (goto-char from)
590 (ess-rep-regexp "^\\([^#{\n]*[^#{ \t\n]+[ \t]*\\)}[ \t]*$"
591 "\\1\n}" 'fix nil verbose)
594 (defun ess-smart-underscore ()
595 "Smart \"_\" key: insert `ess-S-assign', unless in string/comment.
596 If the underscore key is pressed a second time, the assignment
597 operator is removed and replaced by the underscore. `ess-S-assign',
598 typically \" <- \", can be customized. In ESS modes other than R/S,
599 an underscore is always inserted. "
600 (interactive)
601 ;;(insert (if (ess-inside-string-or-comment-p (point)) "_" ess-S-assign))
602 (if (or
603 (ess-inside-string-or-comment-p (point))
604 (not (equal ess-language "S")))
605 (insert "_")
606 ;; Else one keypress produces ess-S-assign; a second keypress will delete
607 ;; ess-S-assign and instead insert _
608 ;; Rather than trying to count a second _ keypress, just check whether
609 ;; the current point is preceded by ess-S-assign.
610 (let ((assign-len (length ess-S-assign)))
611 (if (and
612 (>= (point) (+ assign-len (point-min))) ;check that we can move back
613 (save-excursion
614 (backward-char assign-len)
615 (looking-at ess-S-assign)))
616 ;; If we are currently looking at ess-S-assign, replace it with _
617 (progn
618 (delete-backward-char assign-len)
619 (insert "_"))
620 (delete-horizontal-space)
621 (insert ess-S-assign)))))
623 (defun ess-toggle-underscore (force)
624 "Set the \"_\" (underscore) key to \\[ess-smart-underscore] or back to \"_\".
625 Toggle the current definition, unless FORCE is non-nil, where
626 \\[ess-smart-underscore] is set unconditionally.
628 Using \"C-q _\" will always just insert the underscore character."
629 (interactive "P")
630 (require 'ess-mode)
631 (require 'ess-inf)
632 (let ((uscore (lookup-key ess-mode-map "_")))
633 (if (and uscore
634 ;; (stringp uscore) (string= uscore ess-S-assign)
635 (not force))
636 (progn
637 (define-key ess-mode-map "_" nil); 'self-insert-command
638 (define-key inferior-ess-mode-map "_" nil))
639 ;; else : "force" or uscore is "nil", i.e. default
640 (define-key ess-mode-map "_" 'ess-smart-underscore)
641 (define-key inferior-ess-mode-map "_" 'ess-smart-underscore))))
643 ;; NOTA BENE: "_" is smart *by default* :
644 ;; ----- The user can always customize `ess-S-assign' ...
645 (ess-toggle-underscore 'force-to-S-assign)
647 (defun ess-add-MM-keys ()
648 "Define MM's user keys, currently \\<ess-mode-map>\\[ess-insert-function-outline], and
649 \\<inferior-ess-mode-map>\\[ess-execute-screen-options]."
650 (interactive)
651 (require 'ess-mode); typically unnecessary
652 (require 'ess-inf); dito
653 (define-key ess-mode-map "\C-cf" 'ess-insert-function-outline)
654 (define-key inferior-ess-mode-map "\C-cw" 'ess-execute-screen-options)
658 (defun ess-dump-args-and-go (Sfunc) ; &optional buff)
659 "Dump the function name, with arguments, to a buffer for editing.
661 Currently, this needs to:
662 1. set the buffer to the right mode, with the right settings
663 2. format the statement,
664 3. c/function/Sfunc/
665 and I need to relearn emacs lisp (but I had to, anyway."
667 (interactive "sFunction ? ")
668 (let* ((buffname "ess-complete.R")
669 (buf (ess-execute (format "args(%s)" Sfunc) t buffname)))
670 (pop-to-buffer buf)
671 (message "here yet?")
672 (while (search-forward "function" nil t)
673 (replace-match Sfunc nil t))
674 (ess-setq-vars-local ess-customize-alist); (current-buffer))
675 (setq major-mode 'ess-mode)
676 (use-local-map ess-mode-map)
677 (set-syntax-table ess-mode-syntax-table)
680 (add-hook 'ess-mode-hook
681 (lambda ()
682 (set (make-local-variable 'fill-nobreak-predicate)
683 'ess-inside-string-p)
684 (set (make-local-variable 'normal-auto-fill-function)
685 'ess-do-auto-fill)))
687 (provide 'essl-s)
689 \f ; Local variables section
691 ;;; This file is automatically placed in Outline minor mode.
692 ;;; The file is structured as follows:
693 ;;; Chapters: ^L ;
694 ;;; Sections: ;;*;;
695 ;;; Subsections: ;;;*;;;
696 ;;; Components: defuns, defvars, defconsts
697 ;;; Random code beginning with a ;;;;* comment
699 ;;; Local variables:
700 ;;; mode: emacs-lisp
701 ;;; outline-minor-mode: nil
702 ;;; mode: outline-minor
703 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
704 ;;; End:
706 ;;; essl-s.el ends here