[ESS] Enter Stata pound sign thingies?

Brendan Halpin brendan.halpin at ul.ie
Sun Jun 15 14:40:44 CEST 2014


On Tue, May 20 2014, Ista Zahn wrote:

> Hi Paul,
>
> The ESS mechanism for running Stata code appears to be equivalent to
> entering that code in the command box and hitting enter. Changing the
> delimiter in that context doesn't work, as described in the stata
> documentation here: http://www.stata.com/manuals13/pdelimit.pdf .
>
> What stata appears to do when you highlight a section of code and "do"
> it is to copy the highlighted code to a temporary file and "do" that
> temporary file. Since emacs is programmable, you can implement this
> strategy easily enough. One way to go is to compose a macro using "C-x
> (", copy the selected region, save it to a file, switch to the stata
> console and run "do <path/to/tmpfile".  If you know a little elisp you
> can write a function rather than record a macro, but the macro
> approach will work and is easy even if you don't know elisp.

Ista is right both about Stata and the solution. I have adapted the
ess-eval-region defun to do it as follows. There is a slight problem in
that I can't manage to delete the temporary file -- deleting it after
executing process-send-string is too quick for Stata. Putting a
Stata command at the end of the block, to delete it after completing, is
better but will leave the temporary file in place if there is an error. 

In general however, halting on error in the body of the code is a
distinct advantage over ess-eval-region.

Brendan

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun delimit-do (start end toggle &optional clear message)
"Send code to the Stata process in a fashion similar to Stata's do-file editor.
Deals with batch-style comments and changed line delimiters."
(interactive "r\nP")
  ;;(untabify (point-min) (point-max))
  ;;(untabify start end); do we really need to save-excursion?
  (ess-force-buffer-current "Process to use: ")
  (message "Starting evaluation...")
  (setq message (or message "Eval region"))

  (save-excursion
    ;; don't send new lines (avoid screwing the debugger)
    (goto-char start)
    (skip-chars-forward "\n\t ")
    (setq start (point))

    (unless mark-active
      (ess-blink-region start end))

    ;; don't send new lines at the end (avoid screwing the debugger)
    (goto-char end)
    (skip-chars-backward "\n\t ")
    (setq end (point)))

  (let* (delimit
         (commands (buffer-substring-no-properties start end))
         (delimit-do-file (make-temp-file "delimit-do" nil ".do"))
         (proc (get-process ess-local-process-name))
         (visibly (if toggle (not ess-eval-visibly-p) ess-eval-visibly-p))
         (dev-p (process-get proc 'developer))
         (tb-p  (process-get proc 'tracebug)))

    ;; Go to the start of the section and look back for #delimit
    ;; if found set delimit unless the delimiter is not ";" 
    (goto-char start)
    (setq delimit (re-search-backward "^#delimit +\\(.+\\)$" nil t))
    (if delimit 
        (if (not (string-match ";" (match-string 1))) (setq delimit nil)))

    (with-temp-buffer
      (if clear (insert "clear\n"))
      (if delimit (insert "#delimit ;\n" 
                          commands
                          "\n#delimit cr\n")
        (insert commands "\n"))
      (write-file delimit-do-file nil)
      (kill-buffer (current-buffer)))

    ;; (cond
    ;;  (dev-p     (ess-developer-send-region proc start end visibly message tb-p))
    ;;  (tb-p      (ess-tracebug-send-region proc start end visibly message))
    ;;  (t         (ess-send-region proc start end visibly message))
    ;;  ))
    (process-send-string 
     (get-ess-process ess-current-process-name)
     (format "do %s \n" delimit-do-file))
    ;(delete-file delimit-do-file)
    )
  (if (and (fboundp 'deactivate-mark) ess-eval-deactivate-mark)
      (deactivate-mark))
  ;; return value
  (list start end))

-- 
Brendan Halpin, Head, Department of Sociology, University of Limerick, Ireland
Tel: w +353-61-213147  f +353-61-202569  h +353-61-338562;  Room F1-002 x 3147
mailto:brendan.halpin at ul.ie    ULSociology on Facebook: http://on.fb.me/fjIK9t
http://teaching.sociology.ul.ie/bhalpin/wordpress         twitter:@ULSociology



More information about the ESS-help mailing list