| 1 | #!/bin/bash |
|---|
| 2 | |
|---|
| 3 | # set -x |
|---|
| 4 | |
|---|
| 5 | # |
|---|
| 6 | # starts an editor (asynchronous) |
|---|
| 7 | # |
|---|
| 8 | # if the environment variable ARB_TEXTEDIT is set (to the name of an editor) that editor will be used as default. |
|---|
| 9 | # Example setting for bash users (add to ~/.bashrc): |
|---|
| 10 | # |
|---|
| 11 | # export ARB_TEXTEDIT="gedit" |
|---|
| 12 | # |
|---|
| 13 | # If ARB_TEXTEDIT is set to a value not listed in X_EDITORS below, |
|---|
| 14 | # it is assumed that the editor needs a console window to run. |
|---|
| 15 | # |
|---|
| 16 | # known editors (most preferable first): |
|---|
| 17 | X_EDITORS="xed gedit kate kwrite textedit xedit xdg-open xemacs emacs" |
|---|
| 18 | CONSOLE_EDITORS="vim vi editor edit" |
|---|
| 19 | ALL_EDITORS="$X_EDITORS $CONSOLE_EDITORS" |
|---|
| 20 | |
|---|
| 21 | show_envar_notes() { |
|---|
| 22 | echo "Note: Please set the environment variable \$ARB_TEXTEDIT to the" |
|---|
| 23 | echo " name of your preferred text editor, by adding a line like" |
|---|
| 24 | echo " ARB_TEXTEDIT=gedit" |
|---|
| 25 | echo " into your ~/.bashrc (or similar if not using bash)." |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | exit_with_message() { |
|---|
| 29 | local MSG="$1" ; shift |
|---|
| 30 | arb_message "Error running arb_textedit: $MSG (see console for further information)" |
|---|
| 31 | exit 1 |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | if [ -z "$ARB_TEXTEDIT" ]; then |
|---|
| 35 | echo '$ARB_TEXTEDIT is not defined - looking for known editors ..' |
|---|
| 36 | |
|---|
| 37 | # try to find one of the editors listed above |
|---|
| 38 | for EDITOR in $ALL_EDITORS; do |
|---|
| 39 | if [ -z "$ARB_TEXTEDIT" ]; then |
|---|
| 40 | if [ -x "`which $EDITOR`" ]; then |
|---|
| 41 | ARB_TEXTEDIT=$EDITOR |
|---|
| 42 | fi |
|---|
| 43 | fi |
|---|
| 44 | done |
|---|
| 45 | |
|---|
| 46 | if [ -z "$ARB_TEXTEDIT" ]; then |
|---|
| 47 | echo "Error: ARB could not detect an editor that can be used to edit textfiles." |
|---|
| 48 | show_envar_notes |
|---|
| 49 | echo "You may as well install any of the editors known by ARB:" |
|---|
| 50 | echo " $X_EDITORS" |
|---|
| 51 | exit_with_message "Failed to auto-detect a text editor\n(set environment variable ARB_TEXTEDIT to the name of your editor, e.g. 'ARB_TEXTEDIT=gedit')" |
|---|
| 52 | fi |
|---|
| 53 | echo "Using '$ARB_TEXTEDIT' as text editor." |
|---|
| 54 | else |
|---|
| 55 | echo "Using user-defined '$ARB_TEXTEDIT' as text editor." |
|---|
| 56 | fi |
|---|
| 57 | |
|---|
| 58 | if [ -z "$1" ]; then |
|---|
| 59 | echo "Usage: arb_textedit filename" |
|---|
| 60 | echo "Edits a file using $ARB_TEXTEDIT" |
|---|
| 61 | exit_with_message "missing argument" |
|---|
| 62 | else |
|---|
| 63 | IS_X_EDITOR=0 |
|---|
| 64 | for EDI in $X_EDITORS; do |
|---|
| 65 | if [ "$EDI" = "$ARB_TEXTEDIT" ]; then |
|---|
| 66 | IS_X_EDITOR=1 |
|---|
| 67 | fi |
|---|
| 68 | done |
|---|
| 69 | |
|---|
| 70 | ERRMSG="Error running text-editor '$ARB_TEXTEDIT' - please check ARB_TEXTEDIT" |
|---|
| 71 | |
|---|
| 72 | if [ "$IS_X_EDITOR" = "1" ]; then |
|---|
| 73 | echo "[ editor command: $ARB_TEXTEDIT \"$1\" & ]" |
|---|
| 74 | ($ARB_TEXTEDIT "$1" || arb_message "$ERRMSG") & |
|---|
| 75 | else |
|---|
| 76 | if [ -z "$ARB_XCMD" ]; then |
|---|
| 77 | if [ -z "$ARB_XTERM" ]; then |
|---|
| 78 | ARB_XTERM="xterm -sl 1000 -sb -geometry 130x20" |
|---|
| 79 | fi |
|---|
| 80 | ARB_XCMD="$ARB_XTERM -e" |
|---|
| 81 | fi |
|---|
| 82 | echo "[ editor command: $ARB_XCMD $ARB_TEXTEDIT \"$1\" & ]" |
|---|
| 83 | ($ARB_XCMD $ARB_TEXTEDIT "$1" || arb_message "$ERRMSG" ) & |
|---|
| 84 | fi |
|---|
| 85 | fi |
|---|
| 86 | |
|---|
| 87 | |
|---|