How to send selected text (or string) to TextMate2 on R running on terminal

As you know, version 2 of TextMate is on the way, and the current development version is very promising: https://github.com/textmate/textmate/blob/master/README.md

In my case, I use R in the terminal (MacOSX Mountain Lion), and I am developing my code using TextMate2. In a previous version of TextMate (1.5.11), I used the following trick to send selected text or lines to a terminal window:

-> See How to send selected text (or string) to TextMate in R running on terminal

This trick worked fine for me, but I can't figure out how to achieve similar behavior with TextMate2.

Any idea? I thank you for your precious help.

+6
source share
4 answers

This works for me, and it is right at the end of the selection. I just added the osascript part in the previous answer and put it in the code that was in the original R package written by Hans-Jörg Bibiko. Set the "area selector" to "source.r" and "output" to "discard". Set "Input" to "line" and it does what I need: send the line if nothing is selected and send the choice otherwise.

edit: it works fine with selection, but not with line. When you do not select text, it just restarts everything from the top of the file

edit2: decided, I wrote to Hans-Jörg Bibiko, and he pointed me to the choice of "Entrance".

#!/usr/bin/env bash # input is selection or document rawText="$(cat | sed 's/ / /g;')" curDir='' if [[ ${#TM_DIRECTORY} -gt 0 ]]; then curDir="$TM_DIRECTORY" fi osascript -e 'on run(theCode)' \ -e ' tell application "Terminal"' \ -e ' do script theCode in window 1' \ -e ' end tell' \ -e 'end run' -- "$rawText" if [ "$TM_LINE_NUMBER" != "" ]; then "$TM_MATE" -l "$(($TM_LINE_NUMBER+1)):1000000" elif [[ $TM_SELECTION =~ [1-9][0-9]*:?[0-9]*-([1-9][0-9]*):?[0-9]* ]]; then # Regular Selection "$TM_MATE" -l "$((${BASH_REMATCH[1]}+1)):1000000" elif [[ $TM_SELECTION =~ [1-9][0-9]*:?[0-9]*x([1-9][0-9]*):?[0-9]* ]]; then # Block (option) selection "$TM_MATE" -l "$((${BASH_REMATCH[1]}+1)):1000000" else "$TM_MATE" fi 
+3
source

In fact, based on the previous answer ( How to send selected text (or string) to TextMate in R running on a terminal ), I created my own Bundle in TextMate 2 using the following code:

 #!/bin/bash source "$TM_SUPPORT_PATH/lib/bash_init.sh" # might not be necessary # input is selection or document rawText="$(cat | sed 's/ / /g;')" osascript -e 'on run(theCode)' \ -e ' tell application "Terminal"' \ -e ' do script theCode in window 1' \ -e ' end tell' \ -e 'end run' -- "$rawText" open "txmt://open?line=$(($TM_LINE_NUMBER+1))&column=1000000" & 

See the screen shot below.

code and options for the new bundle The only problem is that when you select a piece of text, the cursor moves to the first line of the document, which is very annoying. Changing the “input” from “Line” to “Choice” does not solve the problem.

Any thoughts?

+2
source

A bit of an indirect answer: I use the R package in Textmate 2 (which also worked in Textmate 1). Just select the lines you want to run, and "Send selection to / R App" (I attached it to the R command, but I'm not sure if this is the original binding)

First, he opens the R application and executes the code. In the following moments, he simply inserts the code and runs it.

It’s not exactly “send to terminal”, but it works anyway

+1
source

I got a job with a few changes in the bahibeki answer. Apparently $ TM_LINE_NUMBER is empty, and this forces the cursor to jump to the first line of the document. Getting rid of the last line, it solves part of the problem.

 #!/bin/bash [[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh" rawText="`cat`" osascript -e 'on run(theCode)' \ -e ' tell application "Terminal"' \ -e ' do script theCode in window 1' \ -e ' end tell' \ -e 'end run' -- "$rawText" > /dev/null 

Another problem is how to move the cursor to the end of the selection. I launched it by inserting the "empty" output at the end of the selection (in the right pane: Output: "Insert After Input" and Format: "Text"). This is probably not the most elegant way to do this, but it works.

0
source

All Articles