You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

608 lines
22 KiB

  1. #!/bin/bash
  2. # The iTerm2 customizations fall under the following license:
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. # -- BEGIN ITERM2 CUSTOMIZATIONS --
  18. if [[ "$ITERM_ENABLE_SHELL_INTEGRATION_WITH_TMUX""$TERM" != screen && "$ITERM_SHELL_INTEGRATION_INSTALLED" = "" && "$-" == *i* && "$TERM" != linux && "$TERM" != dumb ]]; then
  19. if shopt extdebug | grep on > /dev/null; then
  20. echo "iTerm2 Shell Integration not installed."
  21. echo ""
  22. echo "Your shell has 'extdebug' turned on."
  23. echo "This is incompatible with shell integration."
  24. echo "Find 'shopt -s extdebug' in bash's rc scripts and remove it."
  25. return 0
  26. fi
  27. ITERM_SHELL_INTEGRATION_INSTALLED=Yes
  28. # Saved copy of your PS1. This is used to detect if the user changes PS1
  29. # directly. ITERM_PREV_PS1 will hold the last value that this script set PS1 to
  30. # (including various custom escape sequences).
  31. ITERM_PREV_PS1="$PS1"
  32. # The following chunk of code, bash-preexec.sh, is licensed like this:
  33. # The MIT License
  34. #
  35. # Copyright (c) 2015 Ryan Caloras and contributors (see https://github.com/rcaloras/bash-preexec)
  36. #
  37. # Permission is hereby granted, free of charge, to any person obtaining a copy
  38. # of this software and associated documentation files (the "Software"), to deal
  39. # in the Software without restriction, including without limitation the rights
  40. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  41. # copies of the Software, and to permit persons to whom the Software is
  42. # furnished to do so, subject to the following conditions:
  43. #
  44. # The above copyright notice and this permission notice shall be included in
  45. # all copies or substantial portions of the Software.
  46. #
  47. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  48. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  49. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  50. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  51. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  52. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  53. # THE SOFTWARE.
  54. # Wrap bash-preexec.sh in a function so that, if it exits early due to having
  55. # been sourced elsewhere, it doesn't exit our entire script.
  56. _install_bash_preexec () {
  57. # -- BEGIN BASH-PREEXEC.SH --
  58. #!/bin/bash
  59. #
  60. # bash-preexec.sh -- Bash support for ZSH-like 'preexec' and 'precmd' functions.
  61. # https://github.com/rcaloras/bash-preexec
  62. #
  63. #
  64. # 'preexec' functions are executed before each interactive command is
  65. # executed, with the interactive command as its argument. The 'precmd'
  66. # function is executed before each prompt is displayed.
  67. #
  68. # Author: Ryan Caloras (ryan@bashhub.com)
  69. # Forked from Original Author: Glyph Lefkowitz
  70. #
  71. # V0.3.7
  72. #
  73. # -- END ITERM2 CUSTOMIZATIONS --
  74. # bash-preexec.sh -- Bash support for ZSH-like 'preexec' and 'precmd' functions.
  75. # https://github.com/rcaloras/bash-preexec
  76. #
  77. #
  78. # 'preexec' functions are executed before each interactive command is
  79. # executed, with the interactive command as its argument. The 'precmd'
  80. # function is executed before each prompt is displayed.
  81. #
  82. # Author: Ryan Caloras (ryan@bashhub.com)
  83. # Forked from Original Author: Glyph Lefkowitz
  84. #
  85. # V0.3.7
  86. #
  87. # General Usage:
  88. #
  89. # 1. Source this file at the end of your bash profile so as not to interfere
  90. # with anything else that's using PROMPT_COMMAND.
  91. #
  92. # 2. Add any precmd or preexec functions by appending them to their arrays:
  93. # e.g.
  94. # precmd_functions+=(my_precmd_function)
  95. # precmd_functions+=(some_other_precmd_function)
  96. #
  97. # preexec_functions+=(my_preexec_function)
  98. #
  99. # 3. Consider changing anything using the DEBUG trap or PROMPT_COMMAND
  100. # to use preexec and precmd instead. Preexisting usages will be
  101. # preserved, but doing so manually may be less surprising.
  102. #
  103. # Note: This module requires two Bash features which you must not otherwise be
  104. # using: the "DEBUG" trap, and the "PROMPT_COMMAND" variable. If you override
  105. # either of these after bash-preexec has been installed it will most likely break.
  106. # Avoid duplicate inclusion
  107. if [[ "${__bp_imported:-}" == "defined" ]]; then
  108. return 0
  109. fi
  110. __bp_imported="defined"
  111. # Should be available to each precmd and preexec
  112. # functions, should they want it. $? and $_ are available as $? and $_, but
  113. # $PIPESTATUS is available only in a copy, $BP_PIPESTATUS.
  114. # TODO: Figure out how to restore PIPESTATUS before each precmd or preexec
  115. # function.
  116. __bp_last_ret_value="$?"
  117. BP_PIPESTATUS=("${PIPESTATUS[@]}")
  118. __bp_last_argument_prev_command="$_"
  119. __bp_inside_precmd=0
  120. __bp_inside_preexec=0
  121. # Fails if any of the given variables are readonly
  122. # Reference https://stackoverflow.com/a/4441178
  123. __bp_require_not_readonly() {
  124. local var
  125. for var; do
  126. if ! ( unset "$var" 2> /dev/null ); then
  127. echo "iTerm2 Shell Integration:bash-preexec requires write access to ${var}" >&2
  128. return 1
  129. fi
  130. done
  131. }
  132. # Remove ignorespace and or replace ignoreboth from HISTCONTROL
  133. # so we can accurately invoke preexec with a command from our
  134. # history even if it starts with a space.
  135. __bp_adjust_histcontrol() {
  136. local histcontrol
  137. histcontrol="${HISTCONTROL//ignorespace}"
  138. # Replace ignoreboth with ignoredups
  139. if [[ "$histcontrol" == *"ignoreboth"* ]]; then
  140. histcontrol="ignoredups:${histcontrol//ignoreboth}"
  141. fi;
  142. export HISTCONTROL="$histcontrol"
  143. }
  144. # This variable describes whether we are currently in "interactive mode";
  145. # i.e. whether this shell has just executed a prompt and is waiting for user
  146. # input. It documents whether the current command invoked by the trace hook is
  147. # run interactively by the user; it's set immediately after the prompt hook,
  148. # and unset as soon as the trace hook is run.
  149. __bp_preexec_interactive_mode=""
  150. __bp_trim_whitespace() {
  151. local var=$@
  152. var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
  153. var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
  154. echo -n "$var"
  155. }
  156. # This function is installed as part of the PROMPT_COMMAND;
  157. # It sets a variable to indicate that the prompt was just displayed,
  158. # to allow the DEBUG trap to know that the next command is likely interactive.
  159. __bp_interactive_mode() {
  160. __bp_preexec_interactive_mode="on";
  161. }
  162. # This function is installed as part of the PROMPT_COMMAND.
  163. # It will invoke any functions defined in the precmd_functions array.
  164. __bp_precmd_invoke_cmd() {
  165. # Save the returned value from our last command, and from each process in
  166. # its pipeline. Note: this MUST be the first thing done in this function.
  167. __bp_last_ret_value="$?" BP_PIPESTATUS=("${PIPESTATUS[@]}")
  168. # Don't invoke precmds if we are inside an execution of an "original
  169. # prompt command" by another precmd execution loop. This avoids infinite
  170. # recursion.
  171. if (( __bp_inside_precmd > 0 )); then
  172. return
  173. fi
  174. local __bp_inside_precmd=1
  175. # Invoke every function defined in our function array.
  176. local precmd_function
  177. for precmd_function in "${precmd_functions[@]}"; do
  178. # Only execute this function if it actually exists.
  179. # Test existence of functions with: declare -[Ff]
  180. if type -t "$precmd_function" 1>/dev/null; then
  181. __bp_set_ret_value "$__bp_last_ret_value" "$__bp_last_argument_prev_command"
  182. # Quote our function invocation to prevent issues with IFS
  183. "$precmd_function"
  184. fi
  185. done
  186. }
  187. # Sets a return value in $?. We may want to get access to the $? variable in our
  188. # precmd functions. This is available for instance in zsh. We can simulate it in bash
  189. # by setting the value here.
  190. __bp_set_ret_value() {
  191. return ${1:-}
  192. }
  193. __bp_in_prompt_command() {
  194. local prompt_command_array
  195. IFS=';' read -ra prompt_command_array <<< "$PROMPT_COMMAND"
  196. local trimmed_arg
  197. trimmed_arg=$(__bp_trim_whitespace "${1:-}")
  198. local command
  199. for command in "${prompt_command_array[@]:-}"; do
  200. local trimmed_command
  201. trimmed_command=$(__bp_trim_whitespace "$command")
  202. # Only execute each function if it actually exists.
  203. if [[ "$trimmed_command" == "$trimmed_arg" ]]; then
  204. return 0
  205. fi
  206. done
  207. return 1
  208. }
  209. # This function is installed as the DEBUG trap. It is invoked before each
  210. # interactive prompt display. Its purpose is to inspect the current
  211. # environment to attempt to detect if the current command is being invoked
  212. # interactively, and invoke 'preexec' if so.
  213. __bp_preexec_invoke_exec() {
  214. # Save the contents of $_ so that it can be restored later on.
  215. # https://stackoverflow.com/questions/40944532/bash-preserve-in-a-debug-trap#40944702
  216. __bp_last_argument_prev_command="${1:-}"
  217. # Don't invoke preexecs if we are inside of another preexec.
  218. if (( __bp_inside_preexec > 0 )); then
  219. return
  220. fi
  221. local __bp_inside_preexec=1
  222. # Checks if the file descriptor is not standard out (i.e. '1')
  223. # __bp_delay_install checks if we're in test. Needed for bats to run.
  224. # Prevents preexec from being invoked for functions in PS1
  225. if [[ ! -t 1 && -z "${__bp_delay_install:-}" ]]; then
  226. return
  227. fi
  228. if [[ -n "${COMP_LINE:-}" ]]; then
  229. # We're in the middle of a completer. This obviously can't be
  230. # an interactively issued command.
  231. return
  232. fi
  233. if [[ -z "${__bp_preexec_interactive_mode:-}" ]]; then
  234. # We're doing something related to displaying the prompt. Let the
  235. # prompt set the title instead of me.
  236. return
  237. else
  238. # If we're in a subshell, then the prompt won't be re-displayed to put
  239. # us back into interactive mode, so let's not set the variable back.
  240. # In other words, if you have a subshell like
  241. # (sleep 1; sleep 2)
  242. # You want to see the 'sleep 2' as a set_command_title as well.
  243. if [[ 0 -eq "${BASH_SUBSHELL:-}" ]]; then
  244. __bp_preexec_interactive_mode=""
  245. fi
  246. fi
  247. if __bp_in_prompt_command "${BASH_COMMAND:-}"; then
  248. # If we're executing something inside our prompt_command then we don't
  249. # want to call preexec. Bash prior to 3.1 can't detect this at all :/
  250. __bp_preexec_interactive_mode=""
  251. return
  252. fi
  253. local this_command
  254. this_command=$(
  255. export LC_ALL=C
  256. HISTTIMEFORMAT= builtin history 1 | sed '1 s/^ *[0-9][0-9]*[* ] //'
  257. )
  258. # Sanity check to make sure we have something to invoke our function with.
  259. if [[ -z "$this_command" ]]; then
  260. return
  261. fi
  262. # If none of the previous checks have returned out of this function, then
  263. # the command is in fact interactive and we should invoke the user's
  264. # preexec functions.
  265. # Invoke every function defined in our function array.
  266. local preexec_function
  267. local preexec_function_ret_value
  268. local preexec_ret_value=0
  269. for preexec_function in "${preexec_functions[@]:-}"; do
  270. # Only execute each function if it actually exists.
  271. # Test existence of function with: declare -[fF]
  272. if type -t "$preexec_function" 1>/dev/null; then
  273. __bp_set_ret_value ${__bp_last_ret_value:-}
  274. # Quote our function invocation to prevent issues with IFS
  275. "$preexec_function" "$this_command"
  276. preexec_function_ret_value="$?"
  277. if [[ "$preexec_function_ret_value" != 0 ]]; then
  278. preexec_ret_value="$preexec_function_ret_value"
  279. fi
  280. fi
  281. done
  282. # Restore the last argument of the last executed command, and set the return
  283. # value of the DEBUG trap to be the return code of the last preexec function
  284. # to return an error.
  285. # If `extdebug` is enabled a non-zero return value from any preexec function
  286. # will cause the user's command not to execute.
  287. # Run `shopt -s extdebug` to enable
  288. __bp_set_ret_value "$preexec_ret_value" "$__bp_last_argument_prev_command"
  289. }
  290. __bp_install() {
  291. # Exit if we already have this installed.
  292. if [[ "${PROMPT_COMMAND:-}" == *"__bp_precmd_invoke_cmd"* ]]; then
  293. return 1;
  294. fi
  295. trap '__bp_preexec_invoke_exec "$_"' DEBUG
  296. # Preserve any prior DEBUG trap as a preexec function
  297. local prior_trap=$(sed "s/[^']*'\(.*\)'[^']*/\1/" <<<"${__bp_trap_string:-}")
  298. unset __bp_trap_string
  299. if [[ -n "$prior_trap" ]]; then
  300. eval '__bp_original_debug_trap() {
  301. '"$prior_trap"'
  302. }'
  303. preexec_functions+=(__bp_original_debug_trap)
  304. fi
  305. # Adjust our HISTCONTROL Variable if needed.
  306. __bp_adjust_histcontrol
  307. # Issue #25. Setting debug trap for subshells causes sessions to exit for
  308. # backgrounded subshell commands (e.g. (pwd)& ). Believe this is a bug in Bash.
  309. #
  310. # Disabling this by default. It can be enabled by setting this variable.
  311. if [[ -n "${__bp_enable_subshells:-}" ]]; then
  312. # Set so debug trap will work be invoked in subshells.
  313. set -o functrace > /dev/null 2>&1
  314. shopt -s extdebug > /dev/null 2>&1
  315. fi;
  316. # Install our hooks in PROMPT_COMMAND to allow our trap to know when we've
  317. # actually entered something.
  318. PROMPT_COMMAND="__bp_precmd_invoke_cmd; __bp_interactive_mode"
  319. # Add two functions to our arrays for convenience
  320. # of definition.
  321. precmd_functions+=(precmd)
  322. preexec_functions+=(preexec)
  323. # Since this function is invoked via PROMPT_COMMAND, re-execute PC now that it's properly set
  324. eval "$PROMPT_COMMAND"
  325. }
  326. # Sets our trap and __bp_install as part of our PROMPT_COMMAND to install
  327. # after our session has started. This allows bash-preexec to be included
  328. # at any point in our bash profile. Ideally we could set our trap inside
  329. # __bp_install, but if a trap already exists it'll only set locally to
  330. # the function.
  331. __bp_install_after_session_init() {
  332. # Make sure this is bash that's running this and return otherwise.
  333. if [[ -z "${BASH_VERSION:-}" ]]; then
  334. return 1;
  335. fi
  336. # bash-preexec needs to modify these variables in order to work correctly
  337. # if it can't, just stop the installation
  338. __bp_require_not_readonly PROMPT_COMMAND HISTCONTROL HISTTIMEFORMAT || return
  339. # If there's an existing PROMPT_COMMAND capture it and convert it into a function
  340. # So it is preserved and invoked during precmd.
  341. if [[ -n "$PROMPT_COMMAND" ]]; then
  342. eval '__bp_original_prompt_command() {
  343. '"$PROMPT_COMMAND"'
  344. }'
  345. precmd_functions+=(__bp_original_prompt_command)
  346. fi
  347. # Installation is finalized in PROMPT_COMMAND, which allows us to override the DEBUG
  348. # trap. __bp_install sets PROMPT_COMMAND to its final value, so these are only
  349. # invoked once.
  350. # It's necessary to clear any existing DEBUG trap in order to set it from the install function.
  351. # Using \n as it's the most universal delimiter of bash commands
  352. PROMPT_COMMAND=$'\n__bp_trap_string="$(trap -p DEBUG)"\ntrap DEBUG\n__bp_install\n'
  353. }
  354. # Run our install so long as we're not delaying it.
  355. if [[ -z "$__bp_delay_install" ]]; then
  356. __bp_install_after_session_init
  357. fi;
  358. # -- END BASH-PREEXEC.SH --
  359. }
  360. _install_bash_preexec
  361. unset -f _install_bash_preexec
  362. # -- BEGIN ITERM2 CUSTOMIZATIONS --
  363. # We don't care about whitespace, but users care about not changing their histcontrol variables.
  364. # We overwrite the upstream __bp_adjust_histcontrol function whcih gets called from the next
  365. # PROMPT_COMMAND invocation.
  366. function __bp_adjust_histcontrol() {
  367. true
  368. }
  369. function iterm2_begin_osc {
  370. printf "\033]"
  371. }
  372. function iterm2_end_osc {
  373. printf "\007"
  374. }
  375. function iterm2_print_state_data() {
  376. iterm2_begin_osc
  377. printf "1337;RemoteHost=%s@%s" "$USER" "$iterm2_hostname"
  378. iterm2_end_osc
  379. iterm2_begin_osc
  380. printf "1337;CurrentDir=%s" "$PWD"
  381. iterm2_end_osc
  382. iterm2_print_user_vars
  383. }
  384. # Usage: iterm2_set_user_var key value
  385. function iterm2_set_user_var() {
  386. iterm2_begin_osc
  387. printf "1337;SetUserVar=%s=%s" "$1" $(printf "%s" "$2" | base64 | tr -d '\n')
  388. iterm2_end_osc
  389. }
  390. if [ -z "$(type -t iterm2_print_user_vars)" ] || [ "$(type -t iterm2_print_user_vars)" != function ]; then
  391. # iterm2_print_user_vars is not already defined. Provide a no-op default version.
  392. #
  393. # Users can write their own version of this function. It should call
  394. # iterm2_set_user_var but not produce any other output.
  395. function iterm2_print_user_vars() {
  396. true
  397. }
  398. fi
  399. function iterm2_prompt_prefix() {
  400. iterm2_begin_osc
  401. printf "133;D;\$?"
  402. iterm2_end_osc
  403. }
  404. function iterm2_prompt_mark() {
  405. iterm2_begin_osc
  406. printf "133;A"
  407. iterm2_end_osc
  408. }
  409. function iterm2_prompt_suffix() {
  410. iterm2_begin_osc
  411. printf "133;B"
  412. iterm2_end_osc
  413. }
  414. function iterm2_print_version_number() {
  415. iterm2_begin_osc
  416. printf "1337;ShellIntegrationVersion=14;shell=bash"
  417. iterm2_end_osc
  418. }
  419. # If hostname -f is slow on your system, set iterm2_hostname before sourcing this script.
  420. if [ -z "${iterm2_hostname:-}" ]; then
  421. iterm2_hostname=$(hostname -f 2>/dev/null)
  422. # some flavors of BSD (i.e. NetBSD and OpenBSD) don't have the -f option
  423. if [ $? -ne 0 ]; then
  424. iterm2_hostname=$(hostname)
  425. fi
  426. fi
  427. # Runs after interactively edited command but before execution
  428. __iterm2_preexec() {
  429. # Save the returned value from our last command
  430. __iterm2_last_ret_value="$?"
  431. iterm2_begin_osc
  432. printf "133;C;"
  433. iterm2_end_osc
  434. # If PS1 still has the value we set it to in iterm2_preexec_invoke_cmd then
  435. # restore it to its original value. It might have changed if you have
  436. # another PROMPT_COMMAND (like liquidprompt) that modifies PS1.
  437. if [ -n "${ITERM_ORIG_PS1+xxx}" -a "$PS1" = "$ITERM_PREV_PS1" ]
  438. then
  439. export PS1="$ITERM_ORIG_PS1"
  440. fi
  441. iterm2_ran_preexec="yes"
  442. __bp_set_ret_value "$__iterm2_last_ret_value" "$__bp_last_argument_prev_command"
  443. }
  444. function __iterm2_precmd () {
  445. __iterm2_last_ret_value="$?"
  446. # Work around a bug in CentOS 7.2 where preexec doesn't run if you press
  447. # ^C while entering a command.
  448. if [[ -z "${iterm2_ran_preexec:-}" ]]
  449. then
  450. __iterm2_preexec ""
  451. fi
  452. iterm2_ran_preexec=""
  453. # This is an iTerm2 addition to try to work around a problem in the
  454. # original preexec.bash.
  455. # When the PS1 has command substitutions, this gets invoked for each
  456. # substitution and each command that's run within the substitution, which
  457. # really adds up. It would be great if we could do something like this at
  458. # the end of this script:
  459. # PS1="$(iterm2_prompt_prefix)$PS1($iterm2_prompt_suffix)"
  460. # and have iterm2_prompt_prefix set a global variable that tells precmd not to
  461. # output anything and have iterm2_prompt_suffix reset that variable.
  462. # Unfortunately, command substitutions run in subshells and can't
  463. # communicate to the outside world.
  464. # Instead, we have this workaround. We save the original value of PS1 in
  465. # $ITERM_ORIG_PS1. Then each time this function is run (it's called from
  466. # PROMPT_COMMAND just before the prompt is shown) it will change PS1 to a
  467. # string without any command substitutions by doing eval on ITERM_ORIG_PS1. At
  468. # this point ITERM_PREEXEC_INTERACTIVE_MODE is still the empty string, so preexec
  469. # won't produce output for command substitutions.
  470. # The first time this is called ITERM_ORIG_PS1 is unset. This tests if the variable
  471. # is undefined (not just empty) and initializes it. We can't initialize this at the
  472. # top of the script because it breaks with liquidprompt. liquidprompt wants to
  473. # set PS1 from a PROMPT_COMMAND that runs just before us. Setting ITERM_ORIG_PS1
  474. # at the top of the script will overwrite liquidprompt's PS1, whose value would
  475. # never make it into ITERM_ORIG_PS1. Issue 4532. It's important to check
  476. # if it's undefined before checking if it's empty because some users have
  477. # bash set to error out on referencing an undefined variable.
  478. if [ -z "${ITERM_ORIG_PS1+xxx}" ]
  479. then
  480. # ITERM_ORIG_PS1 always holds the last user-set value of PS1.
  481. # You only get here on the first time iterm2_preexec_invoke_cmd is called.
  482. export ITERM_ORIG_PS1="$PS1"
  483. fi
  484. # If you want to generate PS1 dynamically from PROMPT_COMMAND, the best way
  485. # to do it is to define a function named iterm2_generate_ps1 that sets PS1.
  486. # Issue 5964. Other shells don't have this issue because they don't need
  487. # such extremes to get precmd and preexec.
  488. if [ -n "$(type -t iterm2_generate_ps1)" ] && [ "$(type -t iterm2_generate_ps1)" = function ]; then
  489. iterm2_generate_ps1
  490. fi
  491. if [[ "$PS1" != "$ITERM_PREV_PS1" ]]
  492. then
  493. export ITERM_ORIG_PS1="$PS1"
  494. fi
  495. # Get the value of the prompt prefix, which will change $?
  496. \local iterm2_prompt_prefix_value="$(iterm2_prompt_prefix)"
  497. # Add the mark unless the prompt includes '$(iterm2_prompt_mark)' as a substring.
  498. if [[ $ITERM_ORIG_PS1 != *'$(iterm2_prompt_mark)'* && x$ITERM2_SQUELCH_MARK = x ]]
  499. then
  500. iterm2_prompt_prefix_value="$iterm2_prompt_prefix_value$(iterm2_prompt_mark)"
  501. fi
  502. # Send escape sequences with current directory and hostname.
  503. iterm2_print_state_data
  504. # Reset $? to its saved value, which might be used in $ITERM_ORIG_PS1.
  505. __bp_set_ret_value "$__iterm2_last_ret_value" "$__bp_last_argument_prev_command"
  506. # Set PS1 to various escape sequences, the user's preferred prompt, and more escape sequences.
  507. export PS1="\[$iterm2_prompt_prefix_value\]$ITERM_ORIG_PS1\[$(iterm2_prompt_suffix)\]"
  508. # Save the value we just set PS1 to so if the user changes PS1 we'll know and we can update ITERM_ORIG_PS1.
  509. export ITERM_PREV_PS1="$PS1"
  510. __bp_set_ret_value "$__iterm2_last_ret_value" "$__bp_last_argument_prev_command"
  511. }
  512. # Install my functions
  513. preexec_functions+=(__iterm2_preexec)
  514. precmd_functions+=(__iterm2_precmd)
  515. iterm2_print_state_data
  516. iterm2_print_version_number
  517. fi
  518. # -- END ITERM2 CUSTOMIZATIONS --