Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ set -g @prefix_highlight_output_prefix '< '
set -g @prefix_highlight_output_suffix ' >'
```

#### Change option scope (default g - global)

Follows same rules as `set-option`

**Special case**: To set scope to session use `'-'`, because by default session scope used when no param is provided.

```tmux.conf
set -g @prefix_highlight_option_scope '-'
```

### License

[MIT](LICENSE)
18 changes: 13 additions & 5 deletions prefix_highlight.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set -e
place_holder="\#{prefix_highlight}"

# Possible configurations
option_scope_config='@prefix_highlight_option_scope'
fg_color_config='@prefix_highlight_fg'
bg_color_config='@prefix_highlight_bg'
output_prefix='@prefix_highlight_output_prefix'
Expand All @@ -22,7 +23,7 @@ empty_attr_config='@prefix_highlight_empty_attr'
empty_has_affixes='@prefix_highlight_empty_has_affixes'

tmux_option() {
local -r value=$(tmux show-option -gqv "$1")
local -r value=$(tmux show-option -Aqv "$1")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why yet, but this broke my status bar. And it's not just me: #32

Changing the A back to g in the flags fixed everything (I don't understand why), but I think it also takes away the contribution of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carlocab Hi. Very strange. -A should be inheriting all options. Maybe we should rollback this PR for now

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Been debugging this a little. Pretty sure #30 is related. I'm no C developer but I've been reading the tmux source code for a while to try and make sense of the issue and I think I might have understood enough.

There's a function in cmd-show-options.c called cmd_show_options_exec which is what runs when you do something like tmux show-options -gqv status-left or tmux show-options -Aqv status-left. One of the first things this function does is to check the scope of the option like this.

scope = options_scope_from_name(args, window, name, target, &oo, &cause);

The &oo there is the options data which the function will later search to find the status-left or status-right value. Over in the implementation of options_scope_from_name in options.c, this &oo struct is actually overwritten in one of two ways depending on the flags. If the g flag is used, then global_s_options is written to &oo which I think is the full global config from .tmux.conf. Otherwise, in the else branch, s->options is written to &oo, which I think is only the session options.

So if I'm understanding this code correctly, then removing the g flag from the call to show-option causes it to ignore the global config and use only the session config. It's weird though because I can run either version of the command manually in the terminal and get the right result. But it does look like a revert might be the way to go here!

Had a lot of fun digging into this tonight BTW. I hope some of what I came up with is actually correct, or maybe even useful if I'm lucky!

Copy link

@henrycatalinismith henrycatalinismith Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have sent a quick revert PR over at #33. I'm loving how much I'm learning about tmux from all this @imomaliev and you've inspired me to try and learn a bit more about sessions at some point. Might be fun to see if I can figure out a way to make this feature work someday. For all I know though we could have tripped up on a bug in tmux itself here 😇

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the issue is that calling tmux show-option -Aqv status-left returns nothing when run from a shell script using run-shell in tmux.conf.

I did this a few times in my tmux.conf, and this gave me the following:

# Begining of tmux.conf
Using -g
[#{session_name}]
Using -A

# Before calling tpm
Using -g
#{?client_prefix,#[fg=#{@my-prefix-colour}],#{?pane_in_mode,#[fg=#{@copy-colour}],#{?pane_synchronized,#[fg=#{@sync-colour}],}}}#[reverse] #{=10:session_name} #[default]#{?client_prefix,#[fg=#{@my-prefix-colour}],#{?pane_in_mode,#[fg=#{@copy-colour}],#{?pane_synchronized,#[fg=#{@sync-colour}],}}}
Using -A

# After calling tpm
Using -g
#{?client_prefix,#[fg=#{@my-prefix-colour}],#{?pane_in_mode,#[fg=#{@copy-colour}],#{?pane_synchronized,#[fg=#{@sync-colour}],}}}#[reverse] #{=10:session_name} #[default]#{?client_prefix,#[fg=#{@my-prefix-colour}],#{?pane_in_mode,#[fg=#{@copy-colour}],#{?pane_synchronized,#[fg=#{@sync-colour}],}}}
Using -A

Could be a tmux bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you report this to tmux?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet. I intended to poke at it a bit more to try to pin down the problem more precisely (e.g. is it just -A vs -g, or is it from the combination with -qv? Is it just status options that are affected?), but I just haven't managed to find the time yet.

local -r default="$2"

if [ -n "$value" ]; then
Expand All @@ -36,6 +37,14 @@ format_style() {
echo "#[${1}]" | sed -e 's/,/]#[/g'
}

interpolate() {
local -r option=$1
local -r replacement=$2
local -r option_scope=${3#-}
local -r option_value=$(tmux_option "$option")
tmux set-option -"$option_scope"q "$option" "${option_value/$place_holder/$replacement}"
}

# Defaults
default_fg='colour231'
default_bg='colour04'
Expand All @@ -49,6 +58,7 @@ default_empty_prompt=''

main() {
local -r \
option_scope=$(tmux_option "$option_scope_config" "g") \
fg_color=$(tmux_option "$fg_color_config" "$default_fg") \
bg_color=$(tmux_option "$bg_color_config" "$default_bg") \
show_copy_mode=$(tmux_option "$show_copy_config" "off") \
Expand Down Expand Up @@ -94,11 +104,9 @@ main() {

local -r highlight="#{?client_prefix,$prefix_mode,$fallback}#[default]"

local -r status_left_value="$(tmux_option "status-left")"
tmux set-option -gq "status-left" "${status_left_value/$place_holder/$highlight}"
interpolate "status-left" "$highlight" "$option_scope"

local -r status_right_value="$(tmux_option "status-right")"
tmux set-option -gq "status-right" "${status_right_value/$place_holder/$highlight}"
interpolate "status-right" "$highlight" "$option_scope"
}

main
Copy link
Contributor

@MunifTanjim MunifTanjim Jun 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@imomaliev if you're stilll interested in knowing why this was problematic, this main function only runs once when the tmux server starts up (because that's when the .tmux.conf file is sourced). So, setting status-left or status-right as local session option will only set the status for the first session after the server starts. All the subsequent sessions will have no effect of this plugin. It'll work again if you manually source .tmux.conf in each of those sessions.