6

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.

Community
  • 1
  • 1
bhaibeka
  • 91
  • 4

4 Answers4

3

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?

Community
  • 1
  • 1
bhaibeka
  • 91
  • 4
3

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
mic
  • 927
  • 8
  • 17
  • I've recently started having issues with this bundle item. When I select lines and run the bundle item, Textmate correctly skips to the next line but leaves the selection in place. I then need to select and deselect all to remove the selection. – altabq Apr 27 '20 at 12:22
1

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

First time it opens the R app and execute code. Subsequent times it just pastes the code and run it.

It's not exactly "send to terminal" but still works

0

I got it working with a few changes on bhaibeka's answer. Apparently $TM_LINE_NUMBER is empty and that makes the cursor jump to the first line of the document. By 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

The other problem is how to move the cursor to the end of the selection. I made it work by inserting the 'empty' output at the end of the selection (on the right panel: Output:"Insert After Input" and Format: "Text"). Probably this is not the most elegant way to do it but it works.