Search code examples
themespromptfishomf

How to override a fish_prompt function from a omf theme


I lost some time on it so I'm creating this Q&A.

I'm using an omf theme and I would like to make a change in the theme prompt. Unfortunately, the change I want to do is not possible by setting the theme config variables.

I tried to edit fish_prompt function using funced fish_prompt; funcsave fish_prompt but if I do so the theme is not loaded anymore so I can't use the functions defined within the theme. The same happens if I just create a fish_prompt function in my config.fish.


Solution

  • tldr

    add these lines before you define your own fish_prompt function

    source $OMF_PATH/init.fish
    
    # Read current theme
    test -f $OMF_CONFIG/theme
      and read -l theme < $OMF_CONFIG/theme
      or set -l theme default
    
    set -l theme_functions_path {$OMF_CONFIG,$OMF_PATH}/themes*/$theme/functions/
    for conf in $theme_functions_path/*.fish
      source $conf
    end
    

    explanation

    When omf is loaded, it add the theme functions files to the $fish_function_path. (source code)

    According to fish documentation

    When fish needs to load a function, it searches through any directories in the list variable $fish_function_path for a file with a name consisting of the name of the function plus the suffix .fish and loads the first it finds.

    What the documentation doesn't explicitly say is if the function is already defined (in config.fish, for example) it will not try to load from $fish_function_path.

    So the issue is, when you creates your own fish_prompt function, it shadows the .../<theme>/functions/fish_prompt.fish.

    What you need to do to work around it is force loading the theme function file before you redefine it. For example:

    # Read current theme
    test -f $OMF_CONFIG/theme
      and read -l theme < $OMF_CONFIG/theme
      or set -l theme default
    
    set -l theme_functions_path {$OMF_CONFIG,$OMF_PATH}/themes*/$theme/functions/fish_prompt.fish
    for conf in $theme_functions_path
      source $conf
    end
    
    function fish_prompt
      # prompt_theme_foo
      # prompt_theme_bar
    end
    

    Be sure to run it after omf init.fish is loaded.
    You can assure by sourcing omf manually source $OMF_PATH/init.fish or be sure omf.fish is in an alphabetical order lesser than your file.