diff --git a/.sackrc b/.sackrc index e831912..8c9e14c 100644 --- a/.sackrc +++ b/.sackrc @@ -2,7 +2,7 @@ # # @author Sampson L. Chen (sampson-chen / slchen) # @date 2012-10-14 -# +# # @description # - Configuration file for sack @@ -22,7 +22,7 @@ sack__dev_null=/dev/null # Determine the OS sack__OS=$(uname) -# @todo: add support for editors other than 'vim', 'gvim' and 'emacs' +# @todo: add support for editors other than 'vim', 'gvim', 'emacs' and 'scite' sack__default_editor=vim # # # # # # sack profiles # # # # # # diff --git a/sack b/sack index 9f2fa45..e312060 100755 --- a/sack +++ b/sack @@ -5,18 +5,18 @@ # @author: Sampson L. Chen (sampson-chen / slchen) # @date: 2012-10-14 # -# @description: +# @description: # s(hortcut)-ack - a faster way to use ack (or grep)! -# +# # For more details: # https://github.com/sampson-chen/sack -# +# # @dependencies: # - ack (http://betterthangrep.com/) # - tee # - awk # - sed (GNU sed) -# +# # @version: # 1.0 # @@ -26,6 +26,8 @@ # @usage: # - for additional usage information, see README.md +set +x + # ============================================================== # ============================================================== @@ -190,28 +192,62 @@ create_shortcut_cmd_vscode() { chmod +x $sack__shortcut_cmd_path } +# Open shortcut using SciTE (https://www.scintilla.org/SciTE.html) +# First try to locate a running instance, and use the first if more than one is available +# If no running instances are found spawn a new one +create_shortcut_cmd_scite() { + sack__shortcut_cmd_path="${sack__shortcut_path}/${sack__shortcut_cmd}" + echo "#!/bin/bash" > "${sack__shortcut_cmd_path}" + echo '# Search for a running scite instance, use the first found' >> "${sack__shortcut_cmd_path}" + echo 'PIDOF=$(which pidof)' >> "${sack__shortcut_cmd_path}" + echo 'SCITE_PID=""' >> "${sack__shortcut_cmd_path}" + echo 'if [[ $? -eq 0 ]]; then' >> "${sack__shortcut_cmd_path}" + echo ' # Have pidof' >> "${sack__shortcut_cmd_path}" + echo ' SCITE_PID=$(${PIDOF} -s -n scite)' >> "${sack__shortcut_cmd_path}" + echo ' if [[ $? -ne 0 ]]; then' >> "${sack__shortcut_cmd_path}" + echo ' SCITE_PID=$(${PIDOF} -s -n SciTE)' >> "${sack__shortcut_cmd_path}" + echo ' fi' >> "${sack__shortcut_cmd_path}" + echo 'fi' >> "${sack__shortcut_cmd_path}" + + echo "sack__scite_shortcut=\$(sed -n \"\$1p\" < $sack__shortcut_file)" >> "${sack__shortcut_cmd_path}" + echo "fp=\$(echo \"\${sack__scite_shortcut}\" | awk '{ print \$2 }')" >> "$sack__shortcut_cmd_path" + echo "ln=\$(echo \"\${sack__scite_shortcut}\" | awk '{ print \$1 }')" >> "$sack__shortcut_cmd_path" + #echo "ln=\`echo \$sack__scite_shortcut | awk '{ print \$1 }'\`" >> $sack__shortcut_cmd_path + echo 'if [[ "${SCITE_PID}" == "" ]]; then' >> "${sack__shortcut_cmd_path}" + echo ' # No running instance found, spawn a new one' >> "${sack__shortcut_cmd_path}" + echo " ${sack__default_editor} \"-open:\${fp}\" \"-goto:\${ln}\"" >> $sack__shortcut_cmd_path + echo 'else' >> "${sack__shortcut_cmd_path}" + echo " # Use scite director extension to open the file" >> $sack__shortcut_cmd_path + echo " SCITE_PIPE=\$(printf "/tmp/SciTE.%d.in" \${SCITE_PID})" >> $sack__shortcut_cmd_path + echo " echo \"open:\${fp}\" >> \"\${SCITE_PIPE}\"" >> $sack__shortcut_cmd_path + echo " echo \"goto:\${ln}\" >> \"\${SCITE_PIPE}\"" >> $sack__shortcut_cmd_path + echo 'fi' >> "${sack__shortcut_cmd_path}" + + chmod +x $sack__shortcut_cmd_path +} + # Switch to a different profile switchprofile() { # Get the specified profile name from cmdline sack__selected_profile=$1 - # Try to find the corresponding profile entry that matches the name EXACTLY + # Try to find the corresponding profile entry that matches the name EXACTLY # with an entry in config file - # If there are multiple profile names that match exactly, - # (i.e. in the case where currrent profile is already the one we want + # If there are multiple profile names that match exactly, + # (i.e. in the case where currrent profile is already the one we want # to switch to, use the first match) # then use only the first entry (i.e. the first 3 lines) sack__switch_to_profile_raw=$(cat $sack__config_path | grep --line-regexp -A 2 "sack__profile_name=$sack__selected_profile" | head -3) - + # If we can't find an exact match with grep, bail - if [[ -z $sack__switch_to_profile_raw ]]; then + if [[ -z $sack__switch_to_profile_raw ]]; then echo "" echo "===> Error: profile \"$sack__selected_profile\" is not found" echo "" echo "To create a new profile, do:" echo " sack -anp PROFILE_NAME" echo " sack --addnewprofile ReviewBoard" - return 1 + return 1 fi # Get the flags for this profile entry @@ -280,7 +316,7 @@ renameprofile() { echo "===> Renaming for \"no_profile\" is disallowed!" return 1 fi - + # Process the information that need to be written back to the config file # into a temporary file so we don't have I/O conflict cat $sack__config_path | awk -v replace_flag="sack__profile_name=$sack__profile_name" -v new_profile_name=$sack__new_profile_name '{ @@ -296,7 +332,7 @@ renameprofile() { echo "" echo "===> sack: renamed profile <===" - echo "Profile renamed from \"$sack__profile_name\" to: \"$sack__new_profile_name\"" + echo "Profile renamed from \"$sack__profile_name\" to: \"$sack__new_profile_name\"" } setflags() { @@ -316,10 +352,10 @@ setflags() { echo " sack -sp PROFILE_NAME" return 1 fi - + # Get the search directory for this profile entry sack__profile_preset_directory=$(echo "$sack__current_profile_raw" | grep "sack__profile_preset_directory" | sed -e "s/sack__profile_preset_directory=//") - + # Process the information that need to be written back to the config file # into a temporary file so we don't have I/O conflict cat $sack__config_path | awk -v replace_flag="sack__profile_name=$sack__profile_name" -v new_profile_preset_flags="$sack__new_flags" -v profile_preset_directory="$sack__profile_preset_directory" 'BEGIN {do_replace=0}; @@ -344,7 +380,7 @@ setflags() { echo "" echo "===> sack: set flags <===" - echo "Preset flags for profile \"$sack__profile_name\" now set to: $sack__new_flags" + echo "Preset flags for profile \"$sack__profile_name\" now set to: $sack__new_flags" } setdirectory() { @@ -352,7 +388,7 @@ setdirectory() { # Validate the list of all directories passed in for sack__new_directory in $sack__new_directories - do + do # Validate that it is a valid directory path if [[ ! -d "$sack__new_directory" ]]; then echo "" @@ -376,7 +412,7 @@ setdirectory() { echo " sack -sp PROFILE_NAME" return 1 fi - + # Get the preset flags for this profile entry sack__profile_preset_flags=$(echo "$sack__current_profile_raw" | grep "sack__profile_preset_flags" | sed -e "s/sack__profile_preset_flags=//") @@ -403,8 +439,8 @@ setdirectory() { mv $sack__config_path.tmp $sack__config_path echo "" - echo "===> sack: set directory(s) <===" - echo "Preset directory(s) for profile \"$sack__profile_name\" now set to: \"$sack__new_directories\"" + echo "===> sack: set directory\(s\) <===" + echo "Preset directory\(s\) for profile \"$sack__profile_name\" now set to: \"$sack__new_directories\"" } addnewprofile() { @@ -420,7 +456,7 @@ addnewprofile() { echo "===> Must not add new profile with name \"no_profile\"!" return 1 fi - + # Process the information that need to be written back to the config file # into a temporary file so we don't have I/O conflict cat $sack__config_path | awk -v add_profile_name=$sack__add_profile_name '{ @@ -466,11 +502,11 @@ listprofiles() { } } };' - + } # ============================================= -# ================ Script Main ================ +# ================ Script Main ================ # ============================================= # Where to find the config file @@ -553,7 +589,7 @@ if [[ "$sack__profile_name" == "no_profile" ]]; then # are displayed as absolute paths, so that they can be shared across # multiple terminal / shell sessions sack__flags="" - sack__cwd=$(pwd) + sack__cwd=$(pwd) else sack__flags="$sack__profile_preset_flags" sack__cwd="$sack__profile_preset_directory" @@ -576,13 +612,13 @@ echo "" $sack__default_tool $sack__color_param $sack__flags "$@" "$sack__cwd" | tee >$sack__dev_null >(display_shortcuts) >(process_shorcut_paths | remove_escaped_chars > $sack__shortcut_file) # Show usage instructions for first-time users; this can be turned off -if [[ $sack__show_instructions -eq 0 ]]; then +if [[ "$sack__show_instructions" -eq "0" ]]; then # do nothing echo "" -else +else echo "" echo "===== Use: \"$sack__shortcut_cmd #\" to go directly to the search result!" - echo "===== Example: ( user@linux:~$ $sack__shortcut_cmd 25 )" + echo "===== Example: \( user@linux:~$ $sack__shortcut_cmd 25 \)" echo "" fi @@ -592,6 +628,8 @@ elif [ $sack__default_editor == 'rsub' ]; then create_shortcut_cmd_rsub elif [ $sack__default_editor == 'code' ]; then create_shortcut_cmd_vscode +elif [ $sack__default_editor == 'scite' ]; then + create_shortcut_cmd_scite else echo "Error: unsupported default editor specified in sack__default_editor" fi