delta icon indicating copy to clipboard operation
delta copied to clipboard

🚀 Provide a map-syntax feature, like bat does

Open ahmedelgabri opened this issue 5 years ago • 18 comments

As far as I understand, delta uses bat for its syntax highlighting, etc… And bat supports having a config file (bat --config-file /path/to/file or by setting BAT_CONFIG_PATH), I'm interested in the second case BAT_CONFIG_PATH.

In this file you can have some file mapping like this for example:

--map-syntax '.gitconfig.local:Git Config'
--map-syntax '.zsh*:Bourne Again Shell (bash)'
--map-syntax '.vimrc.local:VimL'
--map-syntax 'vimrc:VimL'

But it seems like this is not being picked up by the bat version used by delta

ahmedelgabri avatar Sep 18 '20 22:09 ahmedelgabri

Hi @ahmedelgabri, thanks for this, but delta doesn't use bat for syntax highlighting. So, currently at least, delta doesn't look at the bat config file. So, perhaps this is best turned into a feature request for map-syntax functionality in delta? I'll have to think about whether we should go down the path of honoring the bat config map-syntax entries. Although in practice I know one would almost always want the same mappings in both, it may not make sense to link the two applications like that -- what do you think? There is one point of shared configuration at the moment -- delta honors the BAT_THEME env var.

dandavison avatar Sep 18 '20 22:09 dandavison

If delta honors BAT_THEME already why not honor the config too. The config can actually contain the theme too.

ahmedelgabri avatar Sep 18 '20 22:09 ahmedelgabri

Unless you plan not to use bat, I don’t see why you shouldn’t pick up it’s config. Yes it will couple the projects together but bat can be used as a library already so it’s not like you are going to be tied to an external app. It can be a direct dependency of the project which you can swap later if you want.

ahmedelgabri avatar Sep 18 '20 22:09 ahmedelgabri

I found this confusing too, when trying to work out how to map my ~/.vim/vimrc syntax to VimL, which worked as expected for bat ~/.vim/vimrc but not git diff ~/.vim/vimrc.

+1 for some form of map-syntax for delta. I don't understand where delta is using bat and where it is not, as a new user it seems like the bat config file would have been used here. Clearly not, but if parsing the bat config file is an option, that might make things "just work" for a lot of users. For 2 of us, at least :smile:

nickspoons avatar Oct 18 '20 22:10 nickspoons

I don't understand where delta is using bat and where it is not, as a new user it seems like the bat config file would have been used here

I agree with this, the readme mentions over and over how delta supports everything bat does so I just assumed that it uses bat as a dependency and the config would work. In particular I'm also interested in having the equivalent of --map-syntax 'vimrc:VimL'

SuperCuber avatar Oct 29 '20 12:10 SuperCuber

Thanks for the design input here @ahmedelgabri @nickspoons @SuperCuber. On the one hand the way the documentation has mentioned bat has caused some confusion, and there is probably no need for delta to support BAT_PAGER for example, seeing as we have DELTA_PAGER and PAGER. But on the other hand Delta does absolutely make use of the syntax and theme assets from the bat project, so perhaps that's an area where they should be coupled at the level of configuration files?

cc @MarcoIeni @Kr1ss-XD any thoughts here?

ref #116 "Make use of bat as a library"

dandavison avatar Nov 06 '20 15:11 dandavison

On the one hand I think it would be nice if delta could parse the bat configuration file and make use of the settings in it, to have a single place where syntax- and color-schemes would be defined.

Nonetheless, I do find it really useful that I can define different settings for

a) standalone use of bat, and b) syntax highlighting with delta.

Hence my suggestion would be to either

  • use bat's config file, but only as a default for the delta.<xyz> options in gitconfig that we currently have, so that its options can be overridden, or
  • introduce an additional option like delta.batconfig or similar, so one can have a "delta-specific" bat configuration in addition to the default one for general use of bat.

If it comes to one of these two alternatives, I'd prefer the first one. It would cause less of a change compared to the current status.

Kr1ss-XD avatar Nov 06 '20 17:11 Kr1ss-XD

I definitely agree we need to make map-syntax functionality available for delta: the situation with the vim config files sounds really annoying :/

The syntax of the bat config file is simply strings representing bat command line options. So one option would be to undertake to read that but only respect certain entries such as --map-syntax. However, delta and bat have similar and partially overlapping sets of command line options. Delta can't honor all the entries in the bat config file and I worry that partial honoring of the bat config file will be the source of even more confusion. @ahmedelgabri what do you think?

I'm honestly not sure yet what's best here, so do keep the opinions coming! What we're talking about is configuring the usage of the binary sublime syntax and theme definitions that both delta and bat use. Other than --map-syntax and BAT_THEME (aka --syntax-theme in delta and --theme in bat) are there any other points of configuration that we should be considering in this conversation?

Nonetheless, I do find it really useful that I can define different settings

@Kr1ss-XD I'm guessing you wouldn't want to map syntax differently in bat vs delta, right?

dandavison avatar Nov 06 '20 20:11 dandavison

I'm guessing you wouldn't want to map syntax differently in bat vs delta, right?

No, I only find different color themes convenient. Sorry if I wasn't precise.

Kr1ss-XD avatar Nov 06 '20 20:11 Kr1ss-XD

cc @ryuta69 who's also been thinking about ways to make command line configuration and on-the-fly config changes more convenient.

No, I only find different color themes convenient. Sorry if I wasn't precise.

Not at all; very helpful.

dandavison avatar Nov 06 '20 21:11 dandavison

The syntax of the bat config file is simply strings representing bat command line options. So one option would be to undertake to read that but only respect certain entries such as --map-syntax. However, delta and bat have similar and partially overlapping sets of command line options. Delta can't honor all the entries in the bat config file and I worry that partial honoring of the bat config file will be the source of even more confusion. @ahmedelgabri what do you think?

For me, what I only want from delta to respect from bat is only the syntax mapping, the rest doesn't make sense to support (at least for me) I don't want to configure two places/tools to do the same thing if one of them consumes the other already.

ahmedelgabri avatar Nov 06 '20 21:11 ahmedelgabri

cc @MarcoIeni @Kr1ss-XD any thoughts here?

I am not an heavy delta user, so I don't have any particular opinion about this, but I will keep this discussion in mind while trying to solve #116

marcoieni avatar Nov 06 '20 22:11 marcoieni

I would love to see the --map-syntax feature in delta, but until it gets there, a way to add your syntax mapping is to compile the syntaxes.bin file using bat. I did the following assuming bat was already installed to give files with a Rnw extension Latex syntax highlighting:

# Clone bat to get the syntax files it uses
git clone --recursive https://github.com/sharkdp/bat
# Make the default bat config dir
mkdir -p "$(bat --config-dir)/syntaxes"
# Copy the syntax dir for the language you want to extend
cp -r bat/assets/syntaxes/01_Packages/LaTeX "$(bat --config-dir)/syntaxes/"
# Add the extension you want to add to the list of "file_extensions:" at the top of the file (Rnw for me)
nvim "$(bat --config-dir)/syntaxes/LaTeX/LaTeX.sublime-syntax"
# Build the bat cache
bat cache --build

Now I see "Rnw" under LaTeX when I run delta --list-languages and I get LaTeX syntax highlighting with Rnw files. If you have constantly changing file extensions, much of this could be automated with some shell scripts and clever use of sed. Hope this helps.

bamonroe avatar Feb 01 '21 09:02 bamonroe

I'd be fine with different options in delta vs bat, and with copying things from my bat config to my delta config.

Ideally the argument names would be the same so I could just copy the --map-syntax lines into my git config, but it's not the end of the world.

Pulling some options from the bat config seems possible, but would also be confusing if only some options were respected.

gibfahn avatar Feb 02 '21 11:02 gibfahn

I agree with @gibfahn. I don't care if I have to maintain two separate sets of configuration (I already do), I just want the option to exist in delta so I can make use of it.

djmattyg007 avatar Jun 28 '21 04:06 djmattyg007

I would also like some way to customize the syntax detection, based on the file extension. For example, I'd want *.ftn90 files to be recognized as "Fortran (Modern)", so I would like to be able to put something to that effect in my .gitconfig .


As an aside, delta would be even more amazing if it could use the diff Git attribute mechanism to choose which language syntax to apply. Meaning, if I already have in my .gitattributes:

*.ftn90    diff=fortran

then delta would recognize those files as Fortran without me having to configure delta itself. One could even think of a further fall back to GitHub's and GitLab's special attributes for syntax highlighting:

  • https://github.com/github/linguist/blob/master/docs/overrides.md
  • https://docs.gitlab.com/ee/user/project/highlighting.html

phil-blain avatar Jan 20 '22 20:01 phil-blain

Those sound like good suggestions @phil-blain. In the absence of any actual work on this I just want to point out that it is possible to work around this by

  1. Install bat
  2. Copy the target syntax definition into ~/.config/bat/syntaxes/ with a new name. I.e. something like
 cp bat/assets/syntaxes/02_Extra/Fortran/grammars/FortranModern.sublime-syntax ~/.config/bat/syntaxes/Fortran90.sublime-syntax
  1. Modify the file in ~/.config/bat/syntaxes so that it has the desired file suffix
  2. Run bat cache --build

~~However. Please note that delta is not compatible with the binary assets produced by the newest version of bat. See https://github.com/dandavison/delta/issues/895. So either use an older version of bat < v0.19.0, or use my experimental delta branch: https://github.com/dandavison/delta/pull/903~~

If you have a recent bat version, then this requires delta >= 0.12.1.

dandavison avatar Jan 20 '22 20:01 dandavison

Thanks a lot for that suggestion, that's really great!

phil-blain avatar Jan 20 '22 21:01 phil-blain