├── .gitignore ├── bin ├── picker ├── symbol-search ├── zqantara └── haico ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .aider* 2 | .tags 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /bin/picker: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -euo pipefail 4 | 5 | # Show help message 6 | show_help() { 7 | cat <<'EOF' 8 | picker - A minimal TUI picker for selecting from piped input 9 | 10 | USAGE: 11 | picker [--help] 12 | 13 | DESCRIPTION: 14 | Reads lines from stdin and presents them as a numbered list for interactive 15 | selection. The selected line is printed to stdout. 16 | 17 | Full TUI compatibility: works perfectly inside editors like Helix and other 18 | terminal applications that redirect stdin/stdout. 19 | Press 'q' at any time to cancel without selecting. 20 | 21 | EXAMPLES: 22 | # Basic usage 23 | ls | picker 24 | 25 | # Use with other commands 26 | git branch | picker | xargs git checkout 27 | 28 | # Use as fallback in scripts 29 | command | fzf || command | picker 30 | 31 | OPTIONS: 32 | --help Show this help message and exit 33 | 34 | EXIT STATUS: 35 | 0 A line was successfully selected 36 | 1 Invalid selection or error occurred 37 | 130 Canceled by user (pressed 'q') 38 | EOF 39 | } 40 | 41 | # Function to check if stdin/stdout is a TTY and open /dev/tty if needed 42 | setup_tty() { 43 | # Check if stdin is a TTY 44 | if [[ -t 0 ]]; then 45 | # stdin is a TTY, use it directly 46 | INPUT_FD=0 47 | else 48 | # stdin is redirected (e.g., from Helix), open /dev/tty for input 49 | exec 3/dev/tty 61 | OUTPUT_FD=4 62 | fi 63 | } 64 | 65 | # Read all input into an array 66 | read_input() { 67 | # Read piped stdin into a global array 68 | PICKER_ITEMS=() 69 | local line 70 | while IFS= read -r line; do 71 | PICKER_ITEMS+=("$line") 72 | done 73 | } 74 | 75 | # Save and restore terminal settings 76 | save_terminal_state() { 77 | if [[ "$INPUT_FD" != "0" ]] || [[ "$OUTPUT_FD" != "1" ]]; then 78 | # We're using /dev/tty, need to manage its state 79 | SAVED_STTY=$(stty -g /dev/null) || SAVED_STTY="" 80 | fi 81 | } 82 | 83 | restore_terminal_state() { 84 | if [[ -n "${SAVED_STTY:-}" ]]; then 85 | stty "$SAVED_STTY" /dev/null || true 86 | fi 87 | } 88 | 89 | # Display numbered lines and prompt for selection 90 | display_and_select() { 91 | # Save terminal state before modifying it 92 | save_terminal_state 93 | 94 | # Set up terminal for interactive use when using /dev/tty 95 | if [[ "$INPUT_FD" != "0" ]]; then 96 | # Put terminal in raw mode for proper input handling 97 | stty raw -echo /dev/null || true 98 | fi 99 | 100 | # Clear screen and display numbered lines 101 | printf '\033[2J\033[H' >&"$OUTPUT_FD" 102 | 103 | for i in "${!PICKER_ITEMS[@]}"; do 104 | printf '%3d: %s\r\n' $((i + 1)) "${PICKER_ITEMS[i]}" >&"$OUTPUT_FD" 105 | done 106 | 107 | printf '\r\nSelect a line number (1-%d) [q to cancel]: ' "${#PICKER_ITEMS[@]}" >&"$OUTPUT_FD" 108 | 109 | # Read selection from appropriate input 110 | local selection="" 111 | local char 112 | 113 | # Read input character by character until enter is pressed 114 | while IFS= read -r -n1 char <&"$INPUT_FD"; do 115 | if [[ "$char" == $'\r' ]] || [[ "$char" == $'\n' ]] || [[ "$char" == "" ]]; then 116 | printf '\r\n' >&"$OUTPUT_FD" 117 | break 118 | elif [[ "$char" == 'q' ]]; then 119 | # User canceled with 'q' 120 | printf '\r\n' >&"$OUTPUT_FD" 121 | exit 130 122 | elif [[ "$char" =~ [0-9] ]]; then 123 | selection+="$char" 124 | printf '%s' "$char" >&"$OUTPUT_FD" 125 | elif [[ "$char" == $'\177' ]] || [[ "$char" == $'\b' ]]; then 126 | # Backspace 127 | if [[ -n "$selection" ]]; then 128 | selection="${selection%?}" 129 | printf '\b \b' >&"$OUTPUT_FD" 130 | fi 131 | fi 132 | done 133 | 134 | # Restore terminal state 135 | restore_terminal_state 136 | 137 | # Validate selection 138 | if [[ "$selection" =~ ^[0-9]+$ ]] && ((selection >= 1 && selection <= ${#PICKER_ITEMS[@]})); then 139 | # Print selected line to stdout (original stdout, not TTY) 140 | printf '%s\n' "${PICKER_ITEMS[$((selection - 1))]}" 141 | else 142 | printf 'Invalid selection\n' >&2 143 | exit 1 144 | fi 145 | } 146 | 147 | # Cleanup function 148 | cleanup() { 149 | restore_terminal_state 150 | # Close file descriptors if they were opened 151 | [[ "${INPUT_FD:-}" != "0" ]] && exec 3<&- 2>/dev/null || true 152 | [[ "${OUTPUT_FD:-}" != "1" ]] && exec 4>&- 2>/dev/null || true 153 | } 154 | 155 | # Set up signal handlers for cleanup 156 | trap cleanup EXIT INT TERM 157 | 158 | # Main execution 159 | main() { 160 | # Check for help flag 161 | if [[ "${1:-}" == "--help" ]] || [[ "${1:-}" == "-h" ]]; then 162 | show_help 163 | exit 0 164 | fi 165 | 166 | setup_tty 167 | read_input 168 | display_and_select 169 | } 170 | 171 | main "$@" 172 | -------------------------------------------------------------------------------- /bin/symbol-search: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Symbol search script that uses readtags, ripgrep, or grep to find symbols and pipe to fzf 4 | 5 | set -euo pipefail 6 | 7 | # Default values 8 | USE_RG=false 9 | USE_GREP=false 10 | TAGS_FILES=() 11 | PICKER_CMD="auto" 12 | 13 | # Function to show help 14 | show_help() { 15 | cat <<'EOF' 16 | SYMBOL-SEARCH - Symbol search tool using readtags, ripgrep, or grep with fzf 17 | 18 | SYNOPSIS 19 | echo "symbol_prefix" | symbol-search [OPTIONS] 20 | 21 | DESCRIPTION 22 | This script searches for symbols starting with a given prefix using various 23 | search methods (readtags, ripgrep, or grep) and presents the results via fzf. 24 | The symbol prefix is read from stdin. 25 | 26 | OPTIONS 27 | --rg 28 | Use ripgrep to search for symbols in the current directory and subdirectories. 29 | Searches case-insensitively and respects gitignore files. Only extracts 30 | matching symbol names, not entire lines. 31 | 32 | --grep 33 | Use grep to search for symbols in the current directory and subdirectories. 34 | Searches case-insensitively and only extracts matching symbol names. 35 | 36 | --tags TAGS_FILE 37 | Use readtags to search in the specified tags file. This option can be 38 | specified multiple times to search in multiple tags files. 39 | 40 | --picker [COMMAND] 41 | Specify the picker command to use for selection. If no command is provided, 42 | defaults to 'fzf' if available, otherwise falls back to 'picker'. 43 | You can specify any command that accepts input from stdin. 44 | 45 | --help 46 | Show this help message and exit. 47 | 48 | Multiple search methods can be combined. At least one search method must be 49 | specified. 50 | 51 | BEHAVIOR 52 | - All searches are case-insensitive 53 | - Results are deduplicated before being passed to fzf 54 | - Respects gitignore and ignores common directories (.git, .venv, node_modules, etc.) 55 | - Preserves hidden files like .editorconfig, .stylua.toml 56 | - Only symbol names are extracted, not full lines (for ripgrep and grep) 57 | 58 | EXAMPLES 59 | # Search using ripgrep only 60 | echo "func" | symbol-search --rg 61 | 62 | # Search using multiple tags files 63 | echo "MyClass" | symbol-search --tags .tags --tags tags 64 | 65 | # Combine all search methods 66 | echo "var" | symbol-search --rg --grep --tags .tags 67 | 68 | DEPENDENCIES 69 | - fzf (for interactive selection, falls back to picker script if not available) 70 | - picker script (fallback for fzf, should be in PATH) 71 | - ripgrep (if using --rg) 72 | - readtags (if using --tags, part of universal-ctags) 73 | - Standard POSIX tools (grep, find, awk, sort) 74 | 75 | EXIT CODES 76 | 0 Success 77 | 1 Error (no matches found, invalid arguments, missing dependencies) 78 | EOF 79 | } 80 | 81 | # Parse command line arguments 82 | while [[ $# -gt 0 ]]; do 83 | case $1 in 84 | --help) 85 | show_help 86 | exit 0 87 | ;; 88 | --rg) 89 | USE_RG=true 90 | shift 91 | ;; 92 | --grep) 93 | USE_GREP=true 94 | shift 95 | ;; 96 | --tags) 97 | if [[ -n "${2:-}" ]]; then 98 | TAGS_FILES+=("$2") 99 | shift 2 100 | else 101 | echo "Error: --tags requires a file argument" >&2 102 | exit 1 103 | fi 104 | ;; 105 | --picker) 106 | if [[ -n "${2:-}" ]]; then 107 | PICKER_CMD="$2" 108 | shift 2 109 | else 110 | shift 1 111 | fi 112 | ;; 113 | *) 114 | echo "Unknown option: $1" >&2 115 | echo "Usage: $0 [--rg] [--grep] [--tags TAGS_FILE]... [--picker [COMMAND]]" >&2 116 | echo "Try '$0 --help' for more information." >&2 117 | exit 1 118 | ;; 119 | esac 120 | done 121 | 122 | # Check if at least one search method is specified 123 | if [[ "$USE_RG" == false && "$USE_GREP" == false && ${#TAGS_FILES[@]} -eq 0 ]]; then 124 | echo "Error: At least one search method must be specified (--rg, --grep, or --tags)" >&2 125 | exit 1 126 | fi 127 | 128 | # Read symbol prefix from stdin 129 | symbol_prefix=$(cat) 130 | 131 | if [[ -z "$symbol_prefix" ]]; then 132 | echo "Error: No symbol prefix provided" >&2 133 | exit 1 134 | fi 135 | 136 | # Variable to collect all results 137 | all_results="" 138 | 139 | # Function to search with readtags 140 | search_readtags() { 141 | local tags_file="$1" 142 | if [[ -f "$tags_file" ]]; then 143 | local readtags_results 144 | if readtags_results=$(readtags -ip -t "$tags_file" - "$symbol_prefix" 2>/dev/null); then 145 | readtags_results=$(echo "$readtags_results" | awk -F"\t" '{print $1}') 146 | if [[ -n "$readtags_results" ]]; then 147 | all_results="${all_results}${readtags_results}"$'\n' 148 | fi 149 | fi 150 | fi 151 | } 152 | 153 | # Function to search with ripgrep 154 | search_ripgrep() { 155 | # Search for symbols case-insensitively, respecting gitignore, ignoring common dirs 156 | local rg_results 157 | if rg_results=$(rg -i --hidden \ 158 | --glob='!.git/**' --glob='!.venv/**' --glob='!node_modules/**' \ 159 | --glob='!__pycache__/**' --glob='!.pytest_cache/**' \ 160 | --glob='!build/**' --glob='!dist/**' --glob='!target/**' \ 161 | --only-matching --no-heading --no-line-number --no-filename \ 162 | "\b${symbol_prefix}\w*" . 2>/dev/null); then 163 | if [[ -n "$rg_results" ]]; then 164 | all_results="${all_results}${rg_results}"$'\n' 165 | fi 166 | fi 167 | } 168 | 169 | # Function to search with grep 170 | search_grep() { 171 | # Use find to get files, respecting common ignore patterns, then grep 172 | local find_results 173 | local grep_results 174 | 175 | if find_results=$(find . -type f \ 176 | -not -path './.git/*' \ 177 | -not -path './.venv/*' \ 178 | -not -path './node_modules/*' \ 179 | -not -path './__pycache__/*' \ 180 | -not -path './.pytest_cache/*' \ 181 | -not -path './build/*' \ 182 | -not -path './dist/*' \ 183 | -not -path './target/*' \ 184 | -not -name '*.pyc' \ 185 | -not -name '*.o' \ 186 | -not -name '*.so' \ 187 | -not -name '*.dylib' \ 188 | -not -name '*.dll' \ 189 | 2>/dev/null); then 190 | if [[ -n "$find_results" ]]; then 191 | if grep_results=$(echo "$find_results" | xargs grep -aiIoh "\b${symbol_prefix}\w*" 2>/dev/null); then 192 | if [[ -n "$grep_results" ]]; then 193 | all_results="${all_results}${grep_results}"$'\n' 194 | fi 195 | fi 196 | fi 197 | fi 198 | } 199 | 200 | # Perform searches based on options 201 | if [[ ${#TAGS_FILES[@]} -gt 0 ]]; then 202 | for tags_file in "${TAGS_FILES[@]}"; do 203 | search_readtags "$tags_file" 204 | done 205 | fi 206 | 207 | if [[ "$USE_RG" == true ]]; then 208 | search_ripgrep 209 | fi 210 | 211 | if [[ "$USE_GREP" == true ]]; then 212 | search_grep 213 | fi 214 | 215 | # Resolve picker command if set to auto 216 | if [[ "$PICKER_CMD" == "auto" ]]; then 217 | if command -v fzf >/dev/null 2>&1; then 218 | PICKER_CMD="fzf" 219 | elif command -v picker >/dev/null 2>&1; then 220 | PICKER_CMD="picker" 221 | else 222 | echo "Error: Neither fzf nor picker script is available" >&2 223 | echo "Please install fzf or ensure the picker script is in your PATH" >&2 224 | exit 1 225 | fi 226 | fi 227 | 228 | # Function to use the selected picker 229 | use_picker() { 230 | local deduplicated_results 231 | deduplicated_results=$(echo "$all_results" | sed '/^\s*$/d' | sort | uniq) 232 | 233 | if command -v "$PICKER_CMD" >/dev/null 2>&1; then 234 | echo "$deduplicated_results" | "$PICKER_CMD" 235 | else 236 | echo "Error: Specified picker command '$PICKER_CMD' is not available" >&2 237 | exit 1 238 | fi 239 | } 240 | 241 | # Deduplicate results and use appropriate picker 242 | if [[ -n "$all_results" ]]; then 243 | use_picker 244 | else 245 | echo "No matches found for: $symbol_prefix" >&2 246 | exit 1 247 | fi 248 | -------------------------------------------------------------------------------- /bin/zqantara: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | USE_TAB=false 6 | USE_FLOATING=false 7 | USE_PANE=false 8 | NAME="" 9 | PROGRAM="" 10 | POS="" 11 | FINAL_RETURN=true 12 | 13 | show_help() { 14 | cat <<'EOF' 15 | zqantara (Zellij REPL Bridge) 16 | Named after the Arabic "qantara" meaning bridge - like how bridges connect distant places, 17 | this script bridges the gap between your Helix editor and REPL environments in Zellij. 18 | 19 | This script provides REPL interaction between Helix editor and Zellij by routing 20 | selected content to floating windows or tabs. 21 | 22 | It accepts content from stdin and sends it to designated Zellij tabs or floating 23 | windows using bracketed paste mode. 24 | 25 | Requirements: 26 | - Zellij must be running with the session active 27 | - Target REPL programs should support bracketed paste mode 28 | 29 | The script supports three main modes: 30 | 1. Tab mode: Routes content to named tabs, creating them if they don't exist 31 | 2. Floating mode: Routes content to floating windows by position or creates new ones 32 | 3. Pane mode: Routes content to panes in specified positions, creating them if needed 33 | 34 | Usage: 35 | # Send to existing tab or create new one: 36 | echo 'print(42)' | ./zqantara --tab --name python --program 'ipython --simple-prompt' 37 | 38 | # Send to current floating window: 39 | echo 'ls -la' | ./zqantara --floating --pos current 40 | 41 | # Send to floating window below current: 42 | echo 'ls -la' | ./zqantara --floating --pos down 43 | 44 | # Create new floating window: 45 | ./zqantara --floating --program 'python3' 46 | 47 | # Send to pane in the upper left position: 48 | echo 'print(42)' | ./zqantara --pane --pos "up left" 49 | 50 | # Create new pane with program (right direction): 51 | ./zqantara --pane --pos right --program 'python3' 52 | 53 | # Create new stacked pane: 54 | ./zqantara --pane --pos stacked --program 'python3' 55 | 56 | Options: 57 | --tab Use tab mode. 58 | --floating Use floating window mode. 59 | --pane Use pane mode. 60 | --name Tab name (required for --tab mode). 61 | --pos Position specification: 62 | For --pane mode: 63 | - With --program: 'right', 'down', or 'stacked' 64 | - Without --program: space-separated directions like 'up left' or 'down right right' 65 | For --floating mode (without --program): 66 | - 'current': use current floating window without switching focus 67 | - 'up'/'down': switch focus up/down, supports repetition like 'down down' 68 | --program Program to launch (e.g., 'ipython --simple-prompt'). 69 | --final-return Control the final return behavior after sending bracketed paste: 70 | - true (default): send final return immediately 71 | - false: don't send final return 72 | - number: send final return after delay (in seconds, e.g., 0.1) 73 | NOTE: You generally do not need to think about this option. 74 | Only change this value if you encounter issues: 75 | - Code cannot be executed by REPL (requires manual return): try 0.1 76 | - REPL shows two prompts instead of one: try false 77 | Examples: Claude Code (use 0.1), radian (use false) 78 | --help Show this help message and exit. 79 | 80 | Note: --tab, --floating, and --pane are mutually exclusive. 81 | EOF 82 | } 83 | 84 | while [[ $# -gt 0 ]]; do 85 | case $1 in 86 | --tab) 87 | USE_TAB=true 88 | shift 89 | ;; 90 | --floating) 91 | USE_FLOATING=true 92 | shift 93 | ;; 94 | --pane) 95 | USE_PANE=true 96 | shift 97 | ;; 98 | --name) 99 | NAME="$2" 100 | shift 2 101 | ;; 102 | --program) 103 | PROGRAM="$2" 104 | shift 2 105 | ;; 106 | --pos) 107 | POS="$2" 108 | shift 2 109 | ;; 110 | --final-return) 111 | FINAL_RETURN="$2" 112 | shift 2 113 | ;; 114 | --help) 115 | show_help 116 | exit 0 117 | ;; 118 | *) 119 | echo "Unknown option: $1" >&2 120 | exit 1 121 | ;; 122 | esac 123 | done 124 | 125 | validate_args() { 126 | # Count active modes 127 | local mode_count=0 128 | 129 | if [[ "$USE_TAB" == true ]]; then 130 | mode_count=$((mode_count + 1)) 131 | elif [[ "$USE_FLOATING" == true ]]; then 132 | mode_count=$((mode_count + 1)) 133 | elif [[ "$USE_PANE" == true ]]; then 134 | mode_count=$((mode_count + 1)) 135 | fi 136 | 137 | # Check that only one mode is used 138 | if [[ $mode_count -gt 1 ]]; then 139 | echo "Error: --tab, --floating, and --pane are mutually exclusive" >&2 140 | exit 1 141 | fi 142 | 143 | # Check that at least one mode is specified 144 | if [[ $mode_count -eq 0 ]]; then 145 | echo "Error: One of --tab, --floating, or --pane must be specified" >&2 146 | exit 1 147 | fi 148 | 149 | # Validate tab mode requirements 150 | if [[ "$USE_TAB" == true && -z "$NAME" ]]; then 151 | echo "Error: --name is required when using --tab" >&2 152 | exit 1 153 | fi 154 | 155 | # Validate that --name is only used with --tab 156 | if [[ ("$USE_FLOATING" == true || "$USE_PANE" == true) && -n "$NAME" ]]; then 157 | echo "Error: --name can only be used with --tab" >&2 158 | exit 1 159 | fi 160 | 161 | # Validate floating mode requirements 162 | if [[ "$USE_FLOATING" == true ]]; then 163 | if [[ -z "$PROGRAM" && -z "$POS" ]]; then 164 | echo "Error: For floating windows, either --program or --pos is required" >&2 165 | exit 1 166 | fi 167 | 168 | # When creating new floating window with --program, --pos must not be specified 169 | if [[ -n "$PROGRAM" && -n "$POS" ]]; then 170 | echo "Error: When creating new floating window with --program, --pos must not be specified" >&2 171 | exit 1 172 | fi 173 | 174 | # When using --pos without --program, validate position values for floating mode 175 | if [[ -z "$PROGRAM" && -n "$POS" ]]; then 176 | # Check that POS only contains valid directions for floating mode 177 | local invalid_word="" 178 | for word in $POS; do 179 | case "$word" in 180 | up | down | current) ;; # Valid directions for floating mode 181 | left | right) 182 | echo "Error: --pos does not support 'left' and 'right' directions in --floating mode" >&2 183 | exit 1 184 | ;; 185 | *) 186 | invalid_word="$word" 187 | break 188 | ;; 189 | esac 190 | done 191 | 192 | if [[ -n "$invalid_word" ]]; then 193 | echo "Error: Invalid direction '$invalid_word' in --pos '$POS' for floating mode. Valid directions: up, down, current" >&2 194 | exit 1 195 | fi 196 | fi 197 | fi 198 | 199 | # Validate pane mode requirements 200 | if [[ "$USE_PANE" == true ]]; then 201 | if [[ -z "$POS" ]]; then 202 | echo "Error: --pos is required when using --pane" >&2 203 | exit 1 204 | fi 205 | 206 | # When creating new pane with --program, validate position values 207 | if [[ -n "$PROGRAM" ]]; then 208 | case "$POS" in 209 | right | down | stacked) ;; # Valid for new pane creation 210 | *) 211 | echo "Error: When creating new pane with --program, --pos can only be 'right', 'down', or 'stacked'" >&2 212 | exit 1 213 | ;; 214 | esac 215 | else 216 | # When switching focus to existing pane, validate that POS contains only valid direction words 217 | local invalid_word="" 218 | for word in $POS; do 219 | case "$word" in 220 | left | right | up | down) ;; # Valid direction words 221 | *) 222 | invalid_word="$word" 223 | break 224 | ;; 225 | esac 226 | done 227 | 228 | if [[ -n "$invalid_word" ]]; then 229 | echo "Error: Invalid direction '$invalid_word' in --pos '$POS'. Valid directions: left, right, up, down" >&2 230 | exit 1 231 | fi 232 | fi 233 | fi 234 | 235 | # Validate that --pos is only used with --pane or --floating 236 | if [[ "$USE_TAB" == true && -n "$POS" ]]; then 237 | echo "Error: --pos can only be used with --pane or --floating" >&2 238 | exit 1 239 | fi 240 | 241 | # Validate --final-return value 242 | if [[ "$FINAL_RETURN" != "true" && "$FINAL_RETURN" != "false" ]]; then 243 | # Check if it's a valid positive number (integer or decimal) 244 | if ! [[ "$FINAL_RETURN" =~ ^[0-9]*\.?[0-9]+$ ]]; then 245 | echo "Error: --final-return must be 'true', 'false', or a positive number" >&2 246 | exit 1 247 | fi 248 | fi 249 | } 250 | 251 | send_bracketed_paste() { 252 | local content="$1" 253 | 254 | zellij action write 27 91 50 48 48 126 255 | zellij action write-chars "$content" 256 | zellij action write 13 27 91 50 48 49 126 257 | 258 | # Handle final return based on FINAL_RETURN 259 | if [[ "$FINAL_RETURN" == "false" ]]; then 260 | # Don't send final return 261 | : 262 | elif [[ "$FINAL_RETURN" == "true" ]]; then 263 | # Send final return immediately 264 | zellij action write 13 265 | else 266 | # Send final return with delay 267 | sleep "$FINAL_RETURN" 268 | zellij action write 13 269 | fi 270 | } 271 | 272 | handle_tab_mode() { 273 | local existing_tabs 274 | existing_tabs=$(zellij action query-tab-names) 275 | 276 | if echo "$existing_tabs" | grep -q "^$NAME$"; then 277 | zellij action go-to-tab-name "$NAME" 278 | 279 | if [[ -t 0 ]]; then 280 | return 0 281 | fi 282 | 283 | local stdin_content 284 | stdin_content=$(cat) 285 | if [[ -n "$stdin_content" ]]; then 286 | send_bracketed_paste "$stdin_content" 287 | fi 288 | else 289 | if [[ -n "$PROGRAM" ]]; then 290 | zellij action new-tab --name "$NAME" 291 | zellij action go-to-tab-name "$NAME" 292 | zellij action write-chars "$PROGRAM" 293 | zellij action write 13 294 | else 295 | echo "Error: Tab '$NAME' does not exist and no --program specified" >&2 296 | exit 1 297 | fi 298 | fi 299 | } 300 | 301 | handle_floating_mode() { 302 | if [[ -n "$PROGRAM" ]]; then 303 | zellij action new-pane --floating --cwd "$(pwd)" --close-on-exit -- $PROGRAM 304 | return 0 305 | fi 306 | 307 | if [[ -n "$POS" ]]; then 308 | zellij action toggle-floating-panes 309 | 310 | # Handle focus switching based on --pos directions 311 | for direction in $POS; do 312 | case "$direction" in 313 | current) 314 | # Do nothing - stay at current position 315 | ;; 316 | up | down) 317 | zellij action move-focus "$direction" 318 | ;; 319 | esac 320 | done 321 | 322 | if [[ ! -t 0 ]]; then 323 | local stdin_content 324 | stdin_content=$(cat) 325 | if [[ -n "$stdin_content" ]]; then 326 | send_bracketed_paste "$stdin_content" 327 | fi 328 | fi 329 | fi 330 | } 331 | 332 | handle_pane_mode() { 333 | if [[ -n "$PROGRAM" ]]; then 334 | # Create new pane with program 335 | case "$POS" in 336 | right | down) 337 | zellij action new-pane --direction "$POS" --cwd "$(pwd)" --close-on-exit -- $PROGRAM 338 | ;; 339 | stacked) 340 | zellij action new-pane --stacked --cwd "$(pwd)" --close-on-exit -- $PROGRAM 341 | ;; 342 | esac 343 | return 0 344 | fi 345 | 346 | # Switch to existing pane and send code 347 | # Move focus according to the specified directions 348 | for direction in $POS; do 349 | zellij action move-focus "$direction" 350 | done 351 | 352 | # Send stdin content if available 353 | if [[ ! -t 0 ]]; then 354 | local stdin_content 355 | stdin_content=$(cat) 356 | if [[ -n "$stdin_content" ]]; then 357 | send_bracketed_paste "$stdin_content" 358 | fi 359 | fi 360 | } 361 | 362 | validate_args 363 | 364 | # Check if zellij is running and we're inside a session 365 | if [[ -z "$ZELLIJ_SESSION_NAME" ]]; then 366 | echo "Error: Not running inside a zellij session. Please start zellij first." >&2 367 | exit 1 368 | fi 369 | 370 | if [[ "$USE_TAB" == true ]]; then 371 | handle_tab_mode 372 | elif [[ "$USE_FLOATING" == true ]]; then 373 | handle_floating_mode 374 | elif [[ "$USE_PANE" == true ]]; then 375 | handle_pane_mode 376 | fi 377 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Helix + Zellij + AI + REPL Workflow 2 | 3 | This repository provides a complete workflow integration for modern development 4 | using Helix editor, Zellij terminal multiplexer, AI CLI apps, and REPL 5 | environments. The workflow is built around three scripts with minimal 6 | dependencies: 7 | 8 | ## Overview 9 | 10 | - **`haico`** - AI-powered code completion for Helix editor supporting OpenAI, 11 | Gemini, Codestral and Claude providers 12 | - **`zqantara`** - REPL bridge between Helix and Zellij with bracketed paste 13 | mode support for all Zellij window types 14 | - **`symbol-search`** - Workspace-wide symbol (word) completion using command line tools 15 | 16 | This workflow enables seamless integration between: 17 | 18 | - Code editing in Helix with AI-powered completions 19 | - REPL interaction for Python, R, Shell, and other languages 20 | - AI CLI applications like Aider and Claude Code 21 | - Multiplexing: Manage multiple REPLs and processes efficiently using Zellij's tabs, panes, and floating windows. 22 | 23 | All scripts are designed with minimal dependencies and focus on providing 24 | efficient, keyboard-driven workflows. 25 | 26 | > **💡 Tip**: Use `haico --help`, `zqantara --help`, and `symbol-search --help` to view the complete 27 | > documentation of all supported command-line arguments and options. 28 | 29 | ## Installation 30 | 31 | 1. Clone this repository 32 | 2. Copy the scripts from the `bin/` folder (`haico`, `zqantara`, `symbol-search`, and `picker`) to your PATH 33 | 3. Set up the required API keys for `haico` 34 | 4. Configure Helix with the provided key bindings 35 | 5. Ensure Zellij is installed and running 36 | 37 | ## Video Showcase 38 | 39 | See the workflow in action: 40 | 41 | ### AI Code Completion 42 | 43 | https://github.com/user-attachments/assets/f1d1c159-94da-4fd9-8ea7-986738e7ac33 44 | 45 | ### Claude Code Integration 46 | 47 | https://github.com/user-attachments/assets/dc7085c1-6a74-4943-b417-879697996b09 48 | 49 | ### Zellij Tab REPL 50 | 51 | https://github.com/user-attachments/assets/7a585708-d6b7-4eaf-83c2-5ea4c411b815 52 | 53 | ### Zellij Pane REPL 54 | 55 | https://github.com/user-attachments/assets/cfbe4f99-937d-4130-b8ae-937d4ad58fda 56 | 57 | ### Zellij Floating REPL 58 | 59 | https://github.com/user-attachments/assets/9f644b01-cbdc-4ac1-bdfd-079ab54d2592 60 | 61 | ## haico - Helix AI COmpletion 62 | 63 | `haico` provides AI-powered code completion that integrates seamlessly with 64 | Helix editor, offering intelligent code suggestions using various AI providers. 65 | 66 | ### Features 67 | 68 | - Multi-provider support: Works with OpenAI, Gemini, Codestral, FIM, and Claude APIs 69 | - Recommended model: `gemini-2.0-flash` or `codestral` for optimal performance and speed 70 | - Minimal dependencies: Only requires `jq` and standard Unix tools 71 | - Context-aware: Understands cursor position, file context, and programming language 72 | - Two-step workflow: Designed to work around Helix's limitations with buffer content and cursor position handling 73 | 74 | ### Dependencies 75 | 76 | - `jq` - JSON processor (only external dependency) 77 | - Standard Unix tools (`curl`, `grep`, `awk`, `sed`) 78 | 79 | ### Setup 80 | 81 | Set the appropriate API key based on your chosen provider: 82 | 83 | ```bash 84 | # For Gemini (recommended) 85 | export GEMINI_API_KEY="your-api-key" 86 | 87 | # For OpenAI 88 | export OPENAI_API_KEY="your-api-key" 89 | export OPENAI_BASE_URL="https://openrouter.ai/api/v1" # optional, for custom endpoints 90 | 91 | # For Claude 92 | export ANTHROPIC_API_KEY="your-api-key" 93 | ``` 94 | 95 | ### Helix Integration 96 | 97 | Add these key bindings to your Helix configuration: 98 | 99 | ```toml 100 | [keys.insert] 101 | # long completion 102 | A-y = [ 103 | "save_selection", 104 | "select_all", 105 | ":pipe-to haico --prepare --file ~/.cache/haico/data.txt", 106 | "jump_backward", 107 | """:insert-output \ 108 | haico \ 109 | --cursor-line %{cursor_line} \ 110 | --cursor-column %{cursor_column} \ 111 | --language %{language} \ 112 | --buffer-name %{buffer_name} \ 113 | --max-tokens 128 \ 114 | --file ~/.cache/haico/data.txt 115 | """, 116 | "align_view_center" 117 | ] 118 | 119 | # short (single-line) completion 120 | "A-'" = [ 121 | "save_selection", 122 | "select_all", 123 | ":pipe-to haico --prepare --file ~/.cache/haico/data.txt", 124 | "jump_backward", 125 | """:insert-output \ 126 | haico \ 127 | --provider codestral \ 128 | --cursor-line %{cursor_line} \ 129 | --cursor-column %{cursor_column} \ 130 | --language %{language} \ 131 | --buffer-name %{buffer_name} \ 132 | --file ~/.cache/haico/data.txt \ 133 | --stop '["\\n"]' 134 | """, 135 | "align_view_center" 136 | ] 137 | ``` 138 | 139 | **Note**: Claude models do not support newline/whitespace as stop sequences, so 140 | single-line completion is not available with Claude. 141 | 142 | ## zqantara - Zellij REPL Bridge 143 | 144 | Named after the Arabic "qantara" meaning bridge, `zqantara` bridges the gap 145 | between your Helix editor and REPL environments in Zellij, enabling seamless 146 | code execution and AI CLI tool integration. 147 | 148 | ### Features 149 | 150 | - Bracketed paste mode support\*\*: **Essential feature** for REPL integration utility 151 | - Complete Zellij mode support: Works with all Zellij window types: 152 | - Tab mode: Routes content to named tabs, creating them if needed 153 | - Floating window mode: Routes content to floating windows by position 154 | - Pane mode: Routes content to panes in specified positions 155 | - Zero dependencies: Uses only standard Unix tools (`grep`, `bash`) 156 | - Handle REPL creation: This script can also handle creating new 157 | tabs/panes/windows with specified programs 158 | - CLI integration: Can also used for for Non REPL CLI tools like yazi, lazygit, 159 | Aider and Claude Code 160 | 161 | ### Dependencies 162 | 163 | - Zero external dependencies - uses only standard Unix tools 164 | 165 | ### Helix Integration 166 | 167 | #### Opening REPL Programs and AI Tools 168 | 169 | ```toml 170 | # Open in tabs 171 | [keys.normal.space.o] 172 | g = ":sh zqantara --tab --name lazygit --program lazygit" 173 | p = ":sh zqantara --tab --name ipython --program 'uv run ipython'" 174 | r = ":sh zqantara --tab --name radian --program radian" 175 | c = ":sh zqantara --tab --name aichat --program 'aichat -s'" 176 | C = ":sh zqantara --tab --name claude --program claude" 177 | a = ":sh zqantara --tab --name aider --program aider" 178 | 179 | # Open in floating windows 180 | [keys.normal.space.o.f] 181 | p = ":sh zqantara --floating --program 'uv run ipython'" 182 | t = ":sh zqantara --floating --program $SHELL" 183 | c = ":sh zqantara --floating --program aichat" 184 | 185 | # Open in panes (embedded windows) 186 | [keys.normal.space.o.w] 187 | p = ":sh zqantara --pane --pos down --program ipython" 188 | t = ":sh zqantara --pane --pos right --program $SHELL" 189 | c = ":sh zqantara --pane --pos stacked --program aichat" 190 | ``` 191 | 192 | #### Sending Content to REPLs and AI Tools 193 | 194 | ```toml 195 | [keys.normal.space.space] 196 | # Send to named tabs 197 | p = ":pipe-to zqantara --tab --name ipython" 198 | r = ":pipe-to zqantara --final-return false --tab --name radian" 199 | c = ":pipe-to zqantara --tab --name aichat" 200 | C = ":pipe-to zqantara --final-return 0.1 --tab --name claude" 201 | t = ":pipe-to zqantara --tab --name shell" 202 | 203 | # Send to floating windows by position 204 | # 'current' means use current floating window without switching position 205 | "1" = ":pipe-to zqantara --floating --pos current" 206 | "2" = ":pipe-to zqantara --floating --pos up" 207 | "3" = ":pipe-to zqantara --floating --pos down" 208 | 209 | # Send to panes by position 210 | "l" = ":pipe-to zqantara --pane --pos 'down right'" 211 | "h" = ":pipe-to zqantara --pane --pos 'down left'" 212 | 213 | [keys.select.space.space] 214 | # Send to named tabs 215 | p = ":pipe-to zqantara --tab --name ipython" 216 | r = ":pipe-to zqantara --final-return false --tab --name radian" 217 | c = ":pipe-to zqantara --tab --name aichat" 218 | C = ":pipe-to zqantara --final-return 0.1 --tab --name claude" 219 | t = ":pipe-to zqantara --tab --name shell" 220 | 221 | # Send to floating windows by position 222 | # 'current' means use current floating window without switching position 223 | "1" = ":pipe-to zqantara --floating --pos current" 224 | "2" = ":pipe-to zqantara --floating --pos up" 225 | "3" = ":pipe-to zqantara --floating --pos down" 226 | 227 | # Send to panes by position 228 | "l" = ":pipe-to zqantara --pane --pos 'down right'" 229 | "h" = ":pipe-to zqantara --pane --pos 'down left'" 230 | ``` 231 | 232 | ### Recommended Zellij Configuration 233 | 234 | If you're using a terminal emulator that supports the Kitty keyboard protocol 235 | (such as Kitty or Ghostty), I recommend add the following line to your 236 | `config.kdl`: 237 | 238 | ```kdl 239 | support_kitty_keyboard_protocol false 240 | ``` 241 | 242 | Without this configuration, certain Helix keybindings that require `Alt+Shift` 243 | combinations will not work properly. This includes keys like `Alt+C` (typed as 244 | `Alt+Shift+c`) and `Alt+*` (typed as `Alt+Shift+8`). For technical details 245 | about this issue, see Zellij Issue #4148. 246 | 247 | ### Current Limitations 248 | 249 | Due to Zellij's API limitations, the script cannot retrieve pane names or floating window names: 250 | 251 | - Content can only be sent to floating windows by **position** (not by name) 252 | - Content can only be sent to panes by **position** (not by name) 253 | - Cannot accurately target a specific pane/floating window by its desired name 254 | 255 | ### Why we need Bracketed Paste Mode? 256 | 257 |
258 | 259 | Bracketed-paste mode wraps pasted text in escape sequences, letting terminals 260 | distinguish it from typed input. Most modern REP Ls and command-line 261 | applications support this feature, which offers several advantages: 262 | 263 | - Ensures multi-line code blocks are pasted as a single unit 264 | - Prevent REPLs from misinterpreting pasted newlines or special characters. 265 | - Maintains proper indentation and formatting 266 | 267 |
268 | 269 | ## symbol-search - Workspace Symbol (Word) Completion 270 | 271 | `symbol-search` provides workspace-wide symbol (word) completion for Helix editor 272 | through command line tools integration. While it can work with only standard 273 | Unix tools, it's designed to leverage advanced command line utilities like 274 | `universal-ctags`, `ripgrep`, and `fzf` for better user experience. 275 | 276 | It supports multiple search backends: `universal-ctags` for tag-based symbol 277 | indexing, `ripgrep` for fast text searching, and `grep` as a fallback. 278 | 279 | The script gracefully falls back to standard Unix tools using the included 280 | `picker` script when `fzf` are unavailable. 281 | 282 | ### Features 283 | 284 | - **Adaptive toolchain**: Leverages ripgrep, fzf, and universal-ctags when 285 | available; falls back to grep and the dependency-free `picker` script 286 | - **Workspace-wide completion**: Symbol search across the entire project, not 287 | limited to the current file 288 | - **Zero external dependencies**: Complete functionality using only standard 289 | Unix tools when needed 290 | 291 | ### Comparison with simple-completion-language-server 292 | 293 | | Feature | symbol-search | simple-completion-language-server | 294 | | ---------------- | ------------------------------------------------ | ---------------------------------------------------- | 295 | | **Dependencies** | Standard Unix tools or established CLI utilities | Requires Rust toolchain for compilation | 296 | | **Installation** | Ready-to-use shell script | Binary compilation required (Nix packages available) | 297 | | **Scope** | Workspace-wide symbol completion | Files opened in editor session | 298 | | **Integration** | External picker via `:pipe` command | Native LSP auto-completion | 299 | | **Features** | Symbol completion only | Symbol + snippet completion, more LSP features | 300 | | **Portability** | Works anywhere with basic Unix tools | Requires pre-built binary or compilation | 301 | 302 | ### Helix Integration 303 | 304 | Configure symbol completion with these polished key bindings: 305 | 306 | ```toml 307 | [keys.insert.C-x] 308 | # Macro ensures proper word selection before symbol search 309 | "C-x" = "@hmiwa" 310 | 311 | [keys.normal] 312 | "C-x" = [ 313 | ":pipe symbol-search --rg", 314 | # Use grep if ripgrep is unavailable 315 | # ":pipe symbol-search --grep", 316 | ] 317 | 318 | # If `fzf` is unavailable and the fallback picker script is used, add `:redraw` 319 | # to ensure the screen is properly refreshed after the command executes. 320 | # "C-x" = [":pipe symbol-search --rg", ":redraw"] 321 | 322 | [keys.select] 323 | "C-x" = [ 324 | ":pipe symbol-search --rg", 325 | ] 326 | 327 | # If `fzf` is unavailable and the fallback picker script is used, add `:redraw` 328 | # to ensure the screen is properly refreshed after the command executes. 329 | # "C-x" = [":pipe symbol-search --rg", ":redraw"] 330 | ``` 331 | 332 | The insert mode configuration uses a macro (`@hmiwa`) to ensure 333 | complete word selection under the cursor before triggering symbol search. This 334 | approach provides more reliable completion context than command sequences, 335 | which cannot accurately select word boundaries (`miw` operation). 336 | 337 | ## Complete Workflow 338 | 339 | This setup provides a comprehensive development environment: 340 | 341 | 1. Code in Helix with AI-powered completions via `haico` 342 | 2. Execute code in REPLs via `zqantara` with proper bracketed paste 343 | 3. Access AI tools like Aider and Claude Code through organized Zellij sessions 344 | 4. Manage multiple CLI Apps with tabs, panes, and floating windows 345 | 5. Maintain context across all tools with minimal context switching 346 | -------------------------------------------------------------------------------- /bin/haico: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -euo pipefail 4 | 5 | # Function to display help information 6 | show_help() { 7 | cat <<'EOF' 8 | haico (Helix AI COmpletion) 9 | This script provides AI-based code completion for Helix editor using various AI providers (OpenAI, Claude, Gemini). 10 | 11 | It accepts file content from stdin or a file and prepares a formatted string for an AI model. 12 | 13 | Requirements: 14 | - API key environment variable must be set based on provider: 15 | * Gemini: GEMINI_API_KEY 16 | * OpenAI: OPENAI_API_KEY (optionally set OPENAI_BASE_URL for custom endpoints) 17 | * Claude: ANTHROPIC_API_KEY 18 | * Codestral: CODESTRAL_API_KEY (optionally set CODESTRAL_BASE_URL for custom endpoints) 19 | - jq command must be available for JSON processing 20 | 21 | Local AI Setup: 22 | - For Ollama: Use --provider openai-fim, set OPENAI_BASE_URL=http://localhost:11434/v1 23 | Recommended model: qwen2.5-coder:3b 24 | - For llama.cpp: Use --provider openai-fim, set OPENAI_BASE_URL=http://localhost:8012/v1 25 | Recommended model: qwen2.5-coder:3b 26 | - For DeepSeek: Use --provider openai-fim, set OPENAI_BASE_URL=https://api.deepseek.com/beta 27 | 28 | Note: The --provider openai-fim uses the legacy completion API (rather than the 29 | modern Chat Completions API) and is strongly recommended for use with custom 30 | endpoints (Ollama, llama.cpp, DeepSeek) rather than OpenAI's default models. 31 | 32 | The --prepare flag exists because Helix cannot pass both the buffer content AND the current 33 | cursor position (line/column) in a single call. This requires a two-step workflow: 34 | 1. First call: Use --prepare to store the buffer content verbatim to a temporary file 35 | 2. Second call: Read the stored file and format it with the cursor position for AI completion 36 | 37 | The --wait flag is needed because Helix shell commands (pipe-to and run-shell-command) are 38 | asynchronous. In a two-step workflow, the second step might execute before the first step 39 | has finished writing the required data file. Adding a wait ensures proper sequencing. 40 | 41 | Usage: 42 | # Read from stdin: 43 | cat file.txt | ./haico [options] 44 | 45 | # Read from cache file: 46 | ./haico --file ~/.cache/haico/buffer_content.txt [options] 47 | 48 | # Prepare mode (store stdin to file): 49 | cat file.txt | ./haico --prepare --file /path/to/buffer.txt 50 | 51 | Options: 52 | --cursor-line Cursor line number (1-based). Defaults to the last line. 53 | --cursor-column Cursor column number (1-based). Defaults to the last column. 54 | --system-prompt The system prompt for AI completion. Defaults to standard completion prompt. 55 | --language The programming language of the content. 56 | --buffer-name The filename of the content. 57 | --file Read buffer content from file instead of stdin. 58 | --prepare Store stdin content verbatim to --file (requires --file). 59 | --wait Wait for specified seconds before execution. Defaults to 0.1. 60 | Set to 0 to disable the waiting period. 61 | --provider AI provider to use: openai, claude, gemini, openai-fim, or codestral. Defaults to gemini. 62 | --model AI model to use. Defaults vary by provider: 63 | gemini: gemini-2.0-flash 64 | openai: gpt-4.1-nano 65 | claude: claude-3-5-haiku-20241022 66 | openai-fim: gpt-3.5-turbo-instruct 67 | codestral: codestral-latest 68 | --max-tokens Maximum output tokens. Defaults to 256. 69 | --thinking-tokens Thinking tokens for reasoning. Defaults to 0. Generally recommended to keep at 0. 70 | --stop Stop sequences as JSON array (e.g., '["\n", "END"]'). Defaults to none. 71 | Note: Be careful with shell escaping rules. 72 | --help Show this help message and exit. 73 | EOF 74 | } 75 | 76 | # --- Argument Parsing --- 77 | cursor_line="" 78 | cursor_column="" 79 | system_prompt="Complete the code, comment, or prose based on the provided context. 80 | Insert the completion at the \`\` marker. 81 | Return only the generated text, without markdown code fences, and respect the original indentation. 82 | 83 | Examples: 84 | 85 | Input: 86 | python 87 | 88 | def fibonacci(n): 89 | if n <= 1: 90 | return n 91 | return 92 | 93 | Output: 94 | fibonacci(n-1) + fibonacci(n-2) 95 | 96 | Input: 97 | javascript 98 | 99 | const users = [ 100 | { name: 'Alice', age: 30 }, 101 | { name: 'Bob', age: 25 } 102 | ]; 103 | 104 | const names = users.map( 105 | 106 | Output: 107 | user => user.name); 108 | 109 | Input: 110 | rust 111 | 112 | fn main() { 113 | let mut vec = Vec::new(); 114 | vec.push(1); 115 | vec.push(2); 116 | 117 | for item in 118 | 119 | Output: 120 | &vec { 121 | println!(\"{}\", item); 122 | } 123 | 124 | Important: Do not repeat code that appears before the cursor position. Only 125 | provide the completion text that should be inserted at the cursor location." 126 | language="" 127 | buffer_name="" 128 | input_file="" 129 | prepare_mode=false 130 | wait_seconds="0.1" 131 | provider="gemini" 132 | model="" 133 | max_tokens="256" 134 | thinking_tokens="0" 135 | stop_sequences="" 136 | 137 | while [[ $# -gt 0 ]]; do 138 | case "$1" in 139 | --cursor-line) 140 | cursor_line="$2" 141 | shift 2 142 | ;; 143 | --cursor-column) 144 | cursor_column="$2" 145 | shift 2 146 | ;; 147 | --system-prompt) 148 | system_prompt="$2" 149 | shift 2 150 | ;; 151 | --language) 152 | language="$2" 153 | shift 2 154 | ;; 155 | --buffer-name) 156 | buffer_name="$2" 157 | shift 2 158 | ;; 159 | --file) 160 | input_file="$2" 161 | shift 2 162 | ;; 163 | --prepare) 164 | prepare_mode=true 165 | shift 166 | ;; 167 | --wait) 168 | wait_seconds="$2" 169 | shift 2 170 | ;; 171 | --provider) 172 | provider="$2" 173 | shift 2 174 | ;; 175 | --model) 176 | model="$2" 177 | shift 2 178 | ;; 179 | --max-tokens) 180 | max_tokens="$2" 181 | shift 2 182 | ;; 183 | --thinking-tokens) 184 | thinking_tokens="$2" 185 | shift 2 186 | ;; 187 | --stop) 188 | stop_sequences="$2" 189 | shift 2 190 | ;; 191 | --help) 192 | show_help 193 | exit 0 194 | ;; 195 | *) 196 | echo "Unknown option: $1" >&2 197 | exit 1 198 | ;; 199 | esac 200 | done 201 | 202 | # --- Set default model based on provider if not specified --- 203 | if [[ -z "$model" ]]; then 204 | case "$provider" in 205 | "gemini") 206 | model="gemini-2.0-flash" 207 | ;; 208 | "openai") 209 | model="gpt-4.1-nano" 210 | ;; 211 | "claude") 212 | model="claude-3-5-haiku-20241022" 213 | ;; 214 | "openai-fim") 215 | model="gpt-3.5-turbo-instruct" 216 | ;; 217 | "codestral") 218 | model="codestral-latest" 219 | ;; 220 | esac 221 | fi 222 | 223 | if [[ "$prepare_mode" == true && -z "$input_file" ]]; then 224 | echo "Error: --prepare requires --file to be specified." >&2 225 | exit 1 226 | fi 227 | 228 | # --- Handle wait parameter --- 229 | if [[ -n "$wait_seconds" && "$wait_seconds" != "0" ]]; then 230 | if [[ ! "$wait_seconds" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then 231 | echo "Error: --wait must be a positive number." >&2 232 | exit 1 233 | fi 234 | sleep "$wait_seconds" 235 | fi 236 | 237 | # --- Handle prepare mode --- 238 | if [[ "$prepare_mode" == true ]]; then 239 | # In prepare mode, store stdin content verbatim to the specified file 240 | mkdir -p "$(dirname "$input_file")" 241 | cat >"$input_file" 242 | exit 0 243 | fi 244 | 245 | # --- Read content from stdin or file --- 246 | if [[ -n "$input_file" ]]; then 247 | if [[ ! -f "$input_file" ]]; then 248 | echo "Error: File '$input_file' does not exist." >&2 249 | exit 1 250 | fi 251 | content=$(cat "$input_file") 252 | else 253 | content=$(cat) 254 | fi 255 | 256 | # --- Set default cursor position if not provided --- 257 | if [[ -z "$cursor_line" ]]; then 258 | if [[ -z "$content" ]]; then 259 | cursor_line=1 260 | else 261 | # Count lines; awk is reliable for this. 262 | cursor_line=$(printf "%s" "$content" | awk 'END{print NR}') 263 | fi 264 | fi 265 | 266 | if [[ -z "$cursor_column" ]]; then 267 | if [[ -z "$content" ]]; then 268 | cursor_column=1 269 | else 270 | # Get the line content for the cursor line 271 | line_content=$(printf "%s" "$content" | sed -n "${cursor_line}p") 272 | # Column is 1-based, so length + 1 is the position after the line 273 | cursor_column=$(($(printf "%s" "$line_content" | wc -m) + 1)) 274 | fi 275 | fi 276 | 277 | # --- Split content at cursor position --- 278 | # Awk is used to split the file content into two parts based on the cursor. 279 | # HACK: Bash command substitution strips trailing newlines. When cursor is at 280 | # column 1, we need to preserve the newline before cursor, so we use a special 281 | # marker and remove it later with ${%} expansion syntax. 282 | 283 | newline_marker="羣" 284 | 285 | cursor_before_content=$(printf "%s" "$content" | 286 | awk -v line="$cursor_line" -v col="$cursor_column" -v newline_marker="$newline_marker" ' 287 | BEGIN { ORS = "" } 288 | NR < line { print $0; print "\n" } 289 | NR == line { print substr($0, 1, col - 1) } 290 | NR == line && col == 1 { print newline_marker } 291 | ') 292 | 293 | cursor_after_content=$(printf "%s" "$content" | awk -v line="$cursor_line" -v col="$cursor_column" ' 294 | BEGIN { ORS = "" } 295 | NR == line { print substr($0, col) } 296 | NR > line { print "\n"; print $0 } 297 | ') 298 | 299 | # --- Prepare the formatted output --- 300 | formatted_content="" 301 | if [[ -n "$language" ]]; then 302 | formatted_content+="${language}\n" 303 | fi 304 | if [[ -n "$buffer_name" ]]; then 305 | formatted_content+="${buffer_name}\n" 306 | fi 307 | 308 | if [[ -n "$formatted_content" ]]; then 309 | # Add a blank line separator if we have metadata. 310 | formatted_content+="\n" 311 | fi 312 | 313 | formatted_content+="${cursor_before_content%"$newline_marker"}${cursor_after_content}" 314 | 315 | # --- Create cache directory if it doesn't exist --- 316 | cache_dir="$HOME/.cache/haico" 317 | mkdir -p "$cache_dir" 318 | 319 | # --- Clean up old cache files --- 320 | [ -f "$cache_dir/text.xml" ] && rm "$cache_dir/text.xml" 321 | 322 | # --- Output results --- 323 | printf "%s" "$formatted_content" >"$cache_dir/text.xml" 324 | 325 | # --- Clean up input file if it was provided and we're not in prepare mode --- 326 | if [[ -n "$input_file" && "$prepare_mode" != true ]]; then 327 | rm -f "$input_file" 328 | fi 329 | 330 | # --- Provider Functions --- 331 | gemini_request() { 332 | # Check if GEMINI_API_KEY is set 333 | if [[ -z "${GEMINI_API_KEY:-}" ]]; then 334 | echo "Error: GEMINI_API_KEY environment variable is not set." >&2 335 | exit 1 336 | fi 337 | 338 | # Prepare JSON payload for Gemini API 339 | local json_payload 340 | json_payload=$(jq -n \ 341 | --arg system_prompt "$system_prompt" \ 342 | --arg user_content "$formatted_content" \ 343 | --argjson max_tokens "$max_tokens" \ 344 | --argjson thinking_tokens "$thinking_tokens" \ 345 | --arg stop_sequences "$stop_sequences" \ 346 | '{ 347 | "contents": [ 348 | { 349 | "parts": [ 350 | { 351 | "text": $user_content 352 | } 353 | ] 354 | } 355 | ], 356 | "systemInstruction": { 357 | "parts": [ 358 | { 359 | "text": $system_prompt 360 | } 361 | ] 362 | }, 363 | "generationConfig": ( 364 | { 365 | "maxOutputTokens": $max_tokens, 366 | "thinkingConfig": { 367 | "thinkingBudget": $thinking_tokens 368 | } 369 | } + ( 370 | if $stop_sequences != "" then 371 | {"stopSequences": ($stop_sequences | fromjson)} 372 | else 373 | {} 374 | end 375 | ) 376 | ) 377 | }') 378 | 379 | # Make API request to Gemini 380 | local response 381 | response=$(curl -s -X POST \ 382 | "https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent" \ 383 | -H "x-goog-api-key: $GEMINI_API_KEY" \ 384 | -H "Content-Type: application/json" \ 385 | -d "$json_payload") 386 | 387 | # Check if curl failed 388 | if [[ $? -ne 0 ]]; then 389 | echo "Error: Failed to make API request to Gemini." >&2 390 | exit 1 391 | fi 392 | 393 | # Extract and return the generated text 394 | local extracted_text 395 | extracted_text=$(echo "$response" | jq -r '.candidates[0].content.parts[0].text // empty') 396 | if [[ -z "$extracted_text" && -n "$response" ]]; then 397 | echo "$response" >&2 398 | exit 1 399 | fi 400 | printf "%s" "$extracted_text" 401 | } 402 | 403 | openai_request() { 404 | # Check if OPENAI_API_KEY is set 405 | if [[ -z "${OPENAI_API_KEY:-}" ]]; then 406 | echo "Error: OPENAI_API_KEY environment variable is not set." >&2 407 | exit 1 408 | fi 409 | 410 | # Set OpenAI base URL, defaulting to official API if not set 411 | local openai_base_url 412 | openai_base_url="${OPENAI_BASE_URL:-https://api.openai.com/v1}" 413 | 414 | # Prepare JSON payload for OpenAI API 415 | local json_payload 416 | json_payload=$(jq -n \ 417 | --arg system_prompt "$system_prompt" \ 418 | --arg user_content "$formatted_content" \ 419 | --arg model "$model" \ 420 | --argjson max_tokens "$max_tokens" \ 421 | --arg stop_sequences "$stop_sequences" \ 422 | '{ 423 | "model": $model, 424 | "messages": [ 425 | { 426 | "role": "system", 427 | "content": $system_prompt 428 | }, 429 | { 430 | "role": "user", 431 | "content": $user_content 432 | } 433 | ], 434 | "max_tokens": $max_tokens 435 | } + ( 436 | if $stop_sequences != "" then 437 | {"stop": ($stop_sequences | fromjson)} 438 | else 439 | {} 440 | end 441 | )') 442 | 443 | # Make API request to OpenAI 444 | local response 445 | response=$(curl -s -X POST \ 446 | "${openai_base_url}/chat/completions" \ 447 | -H "Authorization: Bearer $OPENAI_API_KEY" \ 448 | -H "Content-Type: application/json" \ 449 | -d "$json_payload") 450 | 451 | # Check if curl failed 452 | if [[ $? -ne 0 ]]; then 453 | echo "Error: Failed to make API request to OpenAI." >&2 454 | exit 1 455 | fi 456 | 457 | # Extract and return the generated text 458 | local extracted_text 459 | extracted_text=$(echo "$response" | jq -r '.choices[0].message.content // empty') 460 | if [[ -z "$extracted_text" && -n "$response" ]]; then 461 | echo "$response" >&2 462 | exit 1 463 | fi 464 | printf "%s" "$extracted_text" 465 | } 466 | 467 | claude_request() { 468 | # Check if ANTHROPIC_API_KEY is set 469 | if [[ -z "${ANTHROPIC_API_KEY:-}" ]]; then 470 | echo "Error: ANTHROPIC_API_KEY environment variable is not set." >&2 471 | exit 1 472 | fi 473 | 474 | # Prepare JSON payload for Claude API 475 | local json_payload 476 | json_payload=$(jq -n \ 477 | --arg system_prompt "$system_prompt" \ 478 | --arg user_content "$formatted_content" \ 479 | --arg model "$model" \ 480 | --argjson max_tokens "$max_tokens" \ 481 | --arg stop_sequences "$stop_sequences" \ 482 | '{ 483 | "model": $model, 484 | "max_tokens": $max_tokens, 485 | "system": $system_prompt, 486 | "messages": [ 487 | { 488 | "role": "user", 489 | "content": $user_content 490 | } 491 | ] 492 | } + ( 493 | if $stop_sequences != "" then 494 | {"stop_sequences": ($stop_sequences | fromjson)} 495 | else 496 | {} 497 | end 498 | )') 499 | 500 | # Make API request to Claude 501 | local response 502 | response=$(curl -s -X POST \ 503 | "https://api.anthropic.com/v1/messages" \ 504 | -H "x-api-key: $ANTHROPIC_API_KEY" \ 505 | -H "Content-Type: application/json" \ 506 | -H "anthropic-version: 2023-06-01" \ 507 | -d "$json_payload") 508 | 509 | # Check if curl failed 510 | if [[ $? -ne 0 ]]; then 511 | echo "Error: Failed to make API request to Claude." >&2 512 | exit 1 513 | fi 514 | 515 | # Extract and return the generated text 516 | local extracted_text 517 | extracted_text=$(echo "$response" | jq -r '.content[0].text // empty') 518 | if [[ -z "$extracted_text" && -n "$response" ]]; then 519 | echo "$response" >&2 520 | exit 1 521 | fi 522 | printf "%s" "$extracted_text" 523 | } 524 | 525 | # Shared function for FIM-style requests (openai-fim and codestral) 526 | fim_request() { 527 | local provider_name="$1" 528 | local endpoint_path="$2" 529 | local response_path="$3" 530 | local api_key="$4" 531 | local base_url="$5" 532 | 533 | # Check if API key is set 534 | if [[ -z "$api_key" ]]; then 535 | echo "Error: API key for $provider_name is not set." >&2 536 | exit 1 537 | fi 538 | 539 | # Use existing cursor split content, removing newline marker from before content 540 | local prompt_part="${cursor_before_content%"$newline_marker"}" 541 | local suffix_part="$cursor_after_content" 542 | 543 | # Prepare JSON payload for FIM API 544 | local json_payload 545 | json_payload=$(jq -n \ 546 | --arg prompt "$prompt_part" \ 547 | --arg suffix "$suffix_part" \ 548 | --arg model "$model" \ 549 | --argjson max_tokens "$max_tokens" \ 550 | --arg stop_sequences "$stop_sequences" \ 551 | '{ 552 | "model": $model, 553 | "prompt": $prompt, 554 | "suffix": $suffix, 555 | "max_tokens": $max_tokens 556 | } + ( 557 | if $stop_sequences != "" then 558 | {"stop": ($stop_sequences | fromjson)} 559 | else 560 | {} 561 | end 562 | )') 563 | 564 | # Make API request 565 | local response 566 | response=$(curl -s -X POST \ 567 | "${base_url}${endpoint_path}" \ 568 | -H "Authorization: Bearer $api_key" \ 569 | -H "Content-Type: application/json" \ 570 | -d "$json_payload") 571 | 572 | # Check if curl failed 573 | if [[ $? -ne 0 ]]; then 574 | echo "Error: Failed to make API request to $provider_name." >&2 575 | exit 1 576 | fi 577 | 578 | # Extract and return the generated text using the specified response path 579 | local extracted_text 580 | extracted_text=$(echo "$response" | jq -r "$response_path // empty") 581 | if [[ -z "$extracted_text" && -n "$response" ]]; then 582 | echo "$response" >&2 583 | exit 1 584 | fi 585 | printf "%s" "$extracted_text" 586 | } 587 | 588 | openai_fim_request() { 589 | if [[ -z "${OPENAI_API_KEY:-}" ]]; then 590 | echo "Error: OPENAI_API_KEY environment variable is not set." >&2 591 | exit 1 592 | fi 593 | local openai_base_url="${OPENAI_BASE_URL:-https://api.openai.com/v1}" 594 | fim_request "OpenAI FIM" "/completions" ".choices[0].text" "$OPENAI_API_KEY" "$openai_base_url" 595 | } 596 | 597 | codestral_request() { 598 | if [[ -z "${CODESTRAL_API_KEY:-}" ]]; then 599 | echo "Error: CODESTRAL_API_KEY environment variable is not set." >&2 600 | exit 1 601 | fi 602 | local codestral_base_url="${CODESTRAL_BASE_URL:-https://codestral.mistral.ai/v1}" 603 | fim_request "Codestral" "/fim/completions" ".choices[0].message.content" "$CODESTRAL_API_KEY" "$codestral_base_url" 604 | } 605 | 606 | # --- Make API request based on provider --- 607 | case "$provider" in 608 | "gemini") 609 | generated_text=$(gemini_request) 610 | ;; 611 | "openai") 612 | generated_text=$(openai_request) 613 | ;; 614 | "claude") 615 | generated_text=$(claude_request) 616 | ;; 617 | "openai-fim") 618 | generated_text=$(openai_fim_request) 619 | ;; 620 | "codestral") 621 | generated_text=$(codestral_request) 622 | ;; 623 | *) 624 | echo "Error: Unsupported provider '$provider'. Supported providers: openai, claude, gemini, openai-fim, codestral." >&2 625 | exit 1 626 | ;; 627 | esac 628 | 629 | # Check if we got a valid response 630 | if [[ -z "$generated_text" ]]; then 631 | echo "Error: No valid response from $provider API." >&2 632 | exit 1 633 | fi 634 | 635 | # Output the generated text with trimmed whitespace 636 | echo -e "$generated_text" | sed '1s/^[[:space:]]*//' | sed '$s/[[:space:]]*$//' 637 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------