Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send selected text (or a line) in TextMate2 to 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 am using R in terminal (MacOSX Mountain Lion) and I develop my code with TextMate2. With the previous version of TextMate (1.5.11) I used the following trick to send selected text or lines to my terminal window:

-> See How can I send selected text (or a line) in TextMate to R running on Terminal

This trick worked perfectly for me but I cannot figure out how to get a similar behaviour with TextMate2.

Any idea? I thank you in advance for your precious help.

like image 345
bhaibeka Avatar asked Dec 03 '12 14:12

bhaibeka


2 Answers

Actually based on a previous answer (How can I send selected text (or a line) in TextMate to R running on 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 screenchot below.

code and options for the new bundle The only problem is that when you select a chunk of text, the cursor is jumping to the first line of the document, which is a very irritating behaviour. Changing 'Input" from 'Line' to 'Selection' doe not solve the issue.

Any thoughts?

like image 93
bhaibeka Avatar answered Sep 24 '22 03:09

bhaibeka


This works for me, and it correctly goes to 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 bundle written by Hans-Jörg Bibiko. Set "scope 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 selection otherwise.

edit: it works perfectly with selections, but not with line. When you do not select text it just re-runs everything from the top of the file

edit2: solved, I wrote to Hans-Jörg Bibiko and he pointed me to the "Input" selection.

#!/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
like image 30
mic Avatar answered Sep 23 '22 03:09

mic