Giter Site home page Giter Site logo

nvim-colorizer.lua's Introduction

colorizer.lua

luadoc

A high-performance color highlighter for Neovim which has no external dependencies! Written in performant Luajit.

Demo.gif

Demo.mp4

Installation and Usage

Requires Neovim >= 0.4.0 and set termguicolors (I'm looking into relaxing these constraints). If you don't have true color for your terminal or are unsure, read this excellent guide.

Use your plugin manager or clone directly into your runtimepath.

Plug 'norcalli/nvim-colorizer.lua'

As long as you have malloc() and free() on your system, this will work. Which includes Linux, OSX, and Windows.

One line setup. This will create an autocmd for FileType * to highlight every filetype. NOTE: You should add this line after/below where your plugins are setup.

lua require'colorizer'.setup()

Why another highlighter?

Mostly, RAW SPEED.

This has no external dependencies, which means you install it and it just works. Other colorizers typically were synchronous and slow, as well. Being written with performance in mind and leveraging the excellent LuaJIT and a handwritten parser, updates can be done in real time. There are plugins such as hexokinase which have good performance, but it has some difficulty with becoming out of sync. The downside is that this only works for Neovim, and that will never change.

Additionally, having a Lua API that's available means users can use this as a library to do custom highlighting themselves.

Customization

  DEFAULT_OPTIONS = {
	RGB      = true;         -- #RGB hex codes
	RRGGBB   = true;         -- #RRGGBB hex codes
	names    = true;         -- "Name" codes like Blue
	RRGGBBAA = false;        -- #RRGGBBAA hex codes
	rgb_fn   = false;        -- CSS rgb() and rgba() functions
	hsl_fn   = false;        -- CSS hsl() and hsla() functions
	css      = false;        -- Enable all CSS features: rgb_fn, hsl_fn, names, RGB, RRGGBB
	css_fn   = false;        -- Enable all CSS *functions*: rgb_fn, hsl_fn
	-- Available modes: foreground, background
	mode     = 'background'; -- Set the display mode.
  }

MODES:

  • foreground: sets the foreground text color.
  • background: sets the background text color.

For basic setup, you can use a command like the following.

-- Attaches to every FileType mode
require 'colorizer'.setup()

-- Attach to certain Filetypes, add special configuration for `html`
-- Use `background` for everything else.
require 'colorizer'.setup {
  'css';
  'javascript';
  html = {
    mode = 'foreground';
  }
}

-- Use the `default_options` as the second parameter, which uses
-- `foreground` for every mode. This is the inverse of the previous
-- setup configuration.
require 'colorizer'.setup({
  'css';
  'javascript';
  html = { mode = 'background' };
}, { mode = 'foreground' })

-- Use the `default_options` as the second parameter, which uses
-- `foreground` for every mode. This is the inverse of the previous
-- setup configuration.
require 'colorizer'.setup {
  '*'; -- Highlight all files, but customize some others.
  css = { rgb_fn = true; }; -- Enable parsing rgb(...) functions in css.
  html = { names = false; } -- Disable parsing "names" like Blue or Gray
}

-- Exclude some filetypes from highlighting by using `!`
require 'colorizer'.setup {
  '*'; -- Highlight all files, but customize some others.
  '!vim'; -- Exclude vim from highlighting.
  -- Exclusion Only makes sense if '*' is specified!
}

For lower level interface, see the LuaDocs for API details or use :h colorizer.lua once installed.

Commands

|:ColorizerAttachToBuffer|

Attach to the current buffer and start highlighting with the settings as
specified in setup (or the defaults).

If the buffer was already attached (i.e. being highlighted), the settings will
be reloaded with the ones from setup. This is useful for reloading settings
for just one buffer.

|:ColorizerDetachFromBuffer|

Stop highlighting the current buffer (detach).

|:ColorizerReloadAllBuffers|

Reload all buffers that are being highlighted with new settings from the setup
settings (or the defaults). Shortcut for ColorizerAttachToBuffer on every
buffer.

|:ColorizerToggle|

Toggle highlighting of the current buffer.

Caveats

If the file you are editing has no filetype, the plugin won't be attached, as it relies on AutoCmd to do so. You can still make it work by running the following command: :ColorizerAttachToBuffer

See this comment for more information.

TODO

  • Add more display modes?
  • Use a more space efficient trie implementation.
  • Create a COMMON_SETUP which does obvious things like enable rgb_fn for css

nvim-colorizer.lua's People

Contributors

akinsho avatar filipekiss avatar jcrd avatar kwshi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nvim-colorizer.lua's Issues

Highlight only full word matches

Might wanna check if they are full words or surround by only things like ', " or ; or space ... or even check if not surrounded by alphaneumeric

Allow disabling `#RGB` codes and consider excluding partial matches

It can match aggressively for words like matcher#add or #define. I could exclude these patterns from the matching as well by using a frontier pattern for no trailing characters as well. I can't see a reason not to do that, but while I'm at it, it would be very easy to add an exclusion option for fine grained control.

Bug: ffi module not found

Neovim Version: 0.4.3
Lua Version: 5.3

E5105: Error while calling lua chunk: ...k/minpac/start/nvim-colorizer.lua/lua/colorizer/trie.lua:16: module 'ffi' not found: no field package.preload['ffi']
no file '/data/data/com.termux/files/home/.config/nvim/lua/ffi.lua'
no file '/data/data/com.termux/files/home/.config/nvim/lua/ffi/init.lua'

Consider adding support for non true color environments like 256 colors.

The tricky thing here is that what we would be showing wouldn't be accurate if we attempted to approximate true RGB codes which aren't exact matches to your colorscheme. Even the 216 color cube could be overridden, but that would be unlikely considering most people only modify the 16 colors.

Therefore, there are 2 approaches possible here:

  • Try to approximate values with a setting like approximate_true_colors which makes it very clear what it's doing
  • Only highlight values we know to be 100% correct.

I don't have a use-case for this, so I need feedback from the community to see what would be useful. Currently, would recommend disabling colorizer if termguicolors isn't set with

if &termguicolors
lua require'colorizer'.setup()
endif

runtimepath issue, colorizer.lua not found.

Hello,

I have an ~/.config/nvim/init.vim with the following content:

set runtimepath^=~/.vim runtimepath+=~/.vim/after
let &packpath = &runtimepath
source ~/.vimrc

I do that since I want to share ~/.vimrc between Vim and Neovim.

In ~/.vimrc I have:

if empty(glob('~/.vim/autoload/plug.vim'))
  silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif
call plug#begin('~/.vim/plugged')

if has("nvim")
Plug 'norcalli/nvim-colorizer.lua'
lua require'colorizer'.setup()
endif

call plug#end()

With that configuation, Neovim is failing to start with the following error:

no field package.preload['colorizer']
no file '/Users/bluz71/.vim/lua/colorizer.lua'
no file '/Users/bluz71/.vim/lua/colorizer/init.lua'
no file '/Users/bluz71/.config/nvim/lua/colorizer.lua'
no file '/Users/bluz71/.config/nvim/lua/colorizer/init.lua'
no file '/Users/bluz71/.local/share/nvim/site/lua/colorizer.lua'
no file '/Users/bluz71/.local/share/nvim/site/lua/colorizer/init.lua'

Ideally it should try and lookup /Users/bluz71/.vim/plugged/nvim-colorizer.lua/lua/colorizer.lua.

The &rtp does contain the path component /Users/bluz71/.vim/plugged/nvim-colorizer.lua/.

Is there something you should be doing at your end to extend lookup? Or is there something I need to do at my end?

Cheers.

Bug: no field package.preload['colorizer']

For those using minpac and having the no field package.preload['colorizer'] problem.
I used to use Vim-Plug, and I suspect that minpac has different order of loading plugins.
So, if you put lua require'colorizer'.setup() in your init.vim, it's gonna throw an error, because plugins are not loaded yet, I guess. Thus, it has to be placed in the after directory:
mkdir -p ~/.config/nvim/after/plugin && echo "lua require'colorizer'.setup()" >> ~/.config/nvim/after/plugin/colorizer.vim.
Works for me :p

Need of Luajit, steps to install it

Describe the bug
getting module 'ffi' not found as I do not have installed lua in system.

Expected behavior
Do not have external dependencies

Operating System:
POP OS 19.10

Neovim Version:
NVIM v0.5.0-dev

Colorizer Version:
aa5377f040907c5be86499195529c16cb9f5264bAdditional context

Bug: alpha values in hexadecimal notation

Describe the bug
CSS4+ specifically will support alpha values in hex format now as well. Where both #ff000077 and #f007 would display a red at around 46.7% opacity.

To Reproduce
Try using #ff000077 and #f007

Expected behavior
As the background can be trickier to get that one would expect (background none) and readability would suffer with opacity, I would expect to see the RGB values with 100% alpha.

Screenshots
If applicable, add screenshots to help explain your problem.

Operating System:
Currently Nix in Arch

Neovim Version:
0.4.4

Support lowercase color names?

I anticipate that people might want this feature. Adding it as a configurable option will bring additional complexity, so I'm thinking of making it enabled by default.

Is there any good use case for not highlighting lowercase names and only doing uppercase?

Bug: Colors disappear after sourcing a colorscheme file

Describe the bug
Edit a vim colorscheme file with some hex color codes and use colorizer. Source the file with :so%. Colorization disappears and it cannot be re-enabled in anyway.

To Reproduce

  • Goto a vim colorscheme file with hex color values
  • use :ColorizerAttachToBuffer
  • load colorscheme with :so%

Expected behavior
Colors don't disappear. Or atleast should be able to use :ColorizerReloadAllBuffers to fix it.

Operating System:
Debian 10

Neovim Version:
0.4.2

Colorizer Version:
Paste the output of
35f1aad

Doesn't work for *.conf filetypes (and maybe others?)

I installed your plugin and was working with a *.conf file and noticed the colors weren't working. I thought I was messing the configuration, but it works when I'm on a *.sass file. Here's a gist with both files and a minimal.vimrc for reproduction.

Great plugin, btw!

Sass File Conf File
image image

How to set foreground colorization for all file types?

So with the information in the README it is not clear to me how to set foreground coloring as default for all filetypes. I tried:

lua require'colorizer'.setup('*'; {mode = 'foreground'};)

But that only yields:

E5107: Error loading lua [string ":lua"]:1: ')' expected near ';'

Removing the '*'; yields "Invalid option for filetype mode".

Sadly the README only shows how to switch the mode for certain filetypes, but I would like this for all.

Any advice?

Unable to customize '*'

require 'colorizer'.setup ({
  '*';
  css = { names = true; };
}, {
  names = false;
  rgb_fn = true;
  hsl_fn = true;
})

I tried doing this. I am hoping to get non name highlights everywhere and every kind of highlight in css.

I am still getting name highlight in all buffers.

I don't know much lua. But I tried something like this. Did not work either.

config = {}
config['*'] = { names = false }
config['css'] = { names = true }
require 'colorizer'.setup (config, {
  rgb_fn = true;
  hsl_fn = true;
})

Remapping <C-a> and <C-x> possible?

ive already used these shortcuts in my config so it would be awesome if i could remap them to something else. its a great feature which sets this plugin apart even more from the rest.

went threw all the documentations and searched the plugin file for the mappings but came up empty.

is there a way i could change these shortcuts?

Bug: "attempt to call field 'create_namespace' (a nil value)"

Describe the bug
When enabling the plugin it causes neovim to crash

To Reproduce
init.vim:

:let mapleader = ";"

:set number hlsearch tabstop=2 shiftwidth=2 expandtab
:set termguicolors
:syntax on
:set bg=dark

:map ' $
:noremap! jk <esc>
:noremap! <esc> <nop>
:nnoremap <leader>ev :vsplit $MYVIMRC<cr>
:nnoremap <leader>sv :source $MYVIMRC<cr>

call plug#begin()
Plug 'morhetz/gruvbox'

Plug 'norcalli/nvim-colorizer.lua'

call plug#end()
lua require 'colorizer'.setup()
:colorscheme gruvbox

terminal: termite

Expected behavior
For it to work and not crash

Operating System:
Ubuntu 18.04

Neovim Version:
0.2.2

Colorizer Version:
479bec1d73abd2b4f596b1d2c03c4f1f8e36ab76

Additional context
Output on startup:

Error detected while processing /home/<user>/.config/nvim/init.vim:
line   20:
E5105: Error while calling lua chunk: ...config/nvim/plugged/nvim-colorizer.lua/lua/colorizer.lua:326: attempt to call field 'create_namespace' (a nil value)
Press ENTER or type command to continue

Bug: lua require'colorizer'.setup() has no effect

First, thanks for the plugin.
I was using Hexokinase before, but didn't want to upgrade to the new version due to the required dependencies.

I have a question regarding setup.

Description
I have installed the plugin with minpac.
My init.vim sources a file where I load all plugins:

source ~/.config/nvim/plugins.vim

I load nvim-colorizer.lua in the plugins.vim file:

call minpac#init()
call minpac#add('norcalli/nvim-colorizer.lua')

lua require'colorizer'.setup()

When I open a css file, I see no color highlighting.
If I call :ColorizerAttachToBuffer or :ColorizerToggle manually from within the buffer, I get syntax highlighting.

:messages doesn't print errors.

I'm unsure on how to debug this. Is there a command or a lua function I can call?

Expected behavior
nvim-colorizer.lua should highlight as soon as NeoVim enters the buffer, without having to call a command.

Operating System:
Linux 4.19.85-1-MANJARO x86_64 GNU/Linux

Neovim Version:
NVIM v0.4.3
Build type: Release
LuaJIT 2.0.5

Colorizer Version:
cfe8c495452272ed88da29a6babd906c483accf4

Errors when passing options

So I'm trying to do this plugin/colorizer.vim

lua require 'colorizer'.setup ({
  '*';
}, {
  mode = 'foreground';
  rgb_fn = true;
})

But when I open neovim, I get this error

|| Error detected while processing
/Users/ahmed/.dotfiles/files/.vim/plugin/colorizer.vim|5| 
|| E5104: Error while creating lua chunk: [string "<VimL compiled string>"]:1: unexpected symbol near '<eof>'
|| line    6:
|| E20: Mark not set
|| line    7:
|| E20: Mark not set
|| line    8:
|| E492: Not an editor command:   html = { mode = 'background' };
|| line    9:
|| E492: Not an editor command: }, { mode = 'foreground' })

But this works fine

lua require 'colorizer'.setup ()

Bug: wrong highlight RRGGBBAA

Describe the bug
Not correctly represent the colors RRGGBBAA

To Reproduce

require'colorizer'.setup(
{'*';},
{
  RGB      = true;         -- #RGB hex codes
  RRGGBB   = true;         -- #RRGGBB hex codes
  names    = true;         -- "Name" codes like Blue
  RRGGBBAA = true;         -- #RRGGBBAA hex codes
  rgb_fn   = true;         -- CSS rgb() and rgba() functions
  hsl_fn   = true;         -- CSS hsl() and hsla() functions
  css      = true;         -- Enable all CSS features: rgb_fn, hsl_fn, names, RGB, RRGGBB
  css_fn   = true;         -- Enable all CSS *functions*: rgb_fn, hsl_fn
})

Expected behavior
Right color

Screenshots
0qr0quvadxr61

With another plugin:
3omFb5f

Operating System:
Debian 10.9

Neovim Version:
e.g. 0.5.0

Colorizer Version:
36c610a

Additional context
st terminal and tmux

Treesitter issue

hey- ive an issue with treesitter and colorizer. i dont know one which side the issue is so i try it here first.

ive set colorizer up that it shows the colors in a block in the background. now this always worked fine. however- since ive treesitter installed treesitters colors are more important than colorizers colors. so lets say ive a hex code for a blue color. but that part of the syntax gets also colored blue from the theme. colorizer now doesnt change the text color and ive a blue text color from the theme with a blue background from colorizer. that means i dont see the color value anymore.

is there anything u could change that colorizer forces certain text colors depending on the colors of highlighted part?

tried sourcing colorizer after treesitter but that didnt work out

Feature Request: Configuration of color name colors

At the moment the colors shown behind color names (Red, Gray, Yellow, etc) is hardcoded and doesn't really reflect what those colors would ever look like for me.

I would suggest having the colors either respect the terminal colorscheme if that is somehow accessible or to just let me adjust those colors manually to match whatever colorscheme I want

image

Bug: Can't seem to install on Fedora 33 Silverblue

Describe the bug
I'm using Fedora 33 Silverblue and the default Fedora 33 Toolbox image, I have neovim 0.4.4 installed, it comes with luajit bundled. When executing any of this plugins lua commands, I get a stack overflow error. I have been unable to probe further and see what code is actually executing.

To Reproduce
I have tested this with a clean config, only installing the plugin using dein, and then attempting to both execute the easy setup function (lua require'colorizer'.setup()) and also attempting to invoke it with the vim commands (which show up as being available).

Expected behavior
it to work.

Screenshots

Error detected while processing /var/home/abdul/.dotfiles/nvim/init.vim:
line   33:
E5105: Error while calling lua chunk: stack overflow
Press ENTER or type command to continue

Operating System:
Fedora Silverblue 33

Neovim Version:
0.4.4

Colorizer Version:
36c610a

Cursor color

Hi!

I just came around your plugin via your reddit post (I think it's yours) and I have a question:

Say I have the color #FFFFFF. When the cursor hovers on this text, I can't see it. Would it be possible to make nvim-colorizer.lua change the highlighting groups nCursor and / or Cursor so the cursor will be visible in such edge cases?

Bug: Colorizer stop working when switching out and back

Describe the bug
Colorizer / highlighting color code, stop working when I switch out of the nvim window and go back

Configuration.

require 'colorizer'.setup {
  css = { rgb_fn = true; };
  scss = { rgb_fn = true; };
  sass = { rgb_fn = true; };
  stylus = { rgb_fn = true; };
  vim = { names = true; };
  tmux = { names = false; };
  'javascript';
  'javascriptreact';
  'typescript';
  'cpp';
  'typescriptreact';
  html = {
    mode = 'foreground';
  }
}

Expected behavior
A clear and concise description of what you expected to happen.
Color highlighting is on regadless
Screenshots
If applicable, add screenshots to help explain your problem.

Operating System:
e.g. Ubuntu 16.04

Neovim Version:
e.g. 0.4.2

Colorizer Version:
Current

E5105: Error while calling lua chunk

E5105: Error while calling lua chunk: ...ocal/share/nvim/plugged/nvim-colorizer.lua//lua/nvim.lua:96: attempt to call field 'tbl_flatten' (a nil value)

Installed this with vim-plug and added lua require'colorizer'.setup() to init.vim.

NVIM v0.4.0-dev

Bug: Index out of bounds

Sometimes shows this message when open a file:

Error executing lua callback: ~/.vim/plugged/nvim-colorizer.lua//lua/colorizer.lua:518: Index out of bounds

I'm using the last version from master.

error

i really tried to make it work before submitting

i am using lunarVim
i added the module to my packer list and i created a folder with colorizer and i put in inside it init.lua with a line

require('colorizer').setup()

and it's not working and also it gives me a big red warning

E5113: Error while calling lua chunk: /home/d7eeem/.config/nvim/init.lua:23: module 'colorizer' not found:
        no field package.preload['colorizer']
        no file './colorizer.lua'
        no file '/home/runner/work/neovim/neovim/.deps/usr/share/luajit-2.1.0-beta3/colorizer.lua'
        no file '/usr/local/share/lua/5.1/colorizer.lua'
        no file '/usr/local/share/lua/5.1/colorizer/init.lua'
        no file '/home/runner/work/neovim/neovim/.deps/usr/share/lua/5.1/colorizer.lua'
        no file '/home/runner/work/neovim/neovim/.deps/usr/share/lua/5.1/colorizer/init.lua'
        no file './colorizer.so'
        no file '/usr/local/lib/lua/5.1/colorizer.so'
        no file '/home/runner/work/neovim/neovim/.deps/usr/lib/lua/5.1/colorizer.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
Press ENTER or type command to continue

How to load disabled?

I'd like to only enable the plugin on demand, but I don't know how to disable it by default. I use a regular init.vim.

Bug:NVim waits for enter key press after launch.

Describe the bug
NVim waits for enter key press after launch after installing this plugin.

To Reproduce
Install plugin and add minimal lua setup line.

Expected behavior
Enter the buffer edit mode,

Screenshots
If applicable, add screenshots to help explain your problem.

Operating System:
Win10 20h2

Neovim Version:
latest nightly: NVIM v0.5.0-dev+967-g7afd4526f
Build type: Release
LuaJIT 2.1.0-beta3

Colorizer Version:
Paste the output of

find ~/.config/nvim/ -type d -name 'nvim-colorizer.lua' -exec git rev-parse HEAD \;

Additional context
Add any other context about the problem here.

Feature Request: allow to configure named colors

Hello,

I'd like to change the colors used by the plugin to colorize things like "DarkBlue". Is it possible to somehow define the color that is used for each color name as hex code?

Here is an example:
image
How would make the colors on the left and right of each line be the same in this case?

Bug: Highlighted text is incorrect upon change

Describe the bug
Upon changing a word that is highlighted (see screenshots for better explanation) the color seems to overlap the proceeding text.

To Reproduce
Navigate to a highlighted word in normal mode, execute ciw then u. Colors now highlight the incorrect text.

Expected behavior
Plugin should continue to highlight the correct text

Screenshots
ciw
https://i.imgur.com/u9FXchC.png

u
https://i.imgur.com/mgPm2oP.png

Operating System:
MacOS

Neovim Version:
v0.5.0-b0196586d

Colorizer Version:
master / 35f1aad99c4d03217bcc80a2e16efe3ba74379a4

Also almost forgot, thanks for the great plugin. I love it :)

Allow excluding files from the `setup()` function

Possible syntax choices here are:

  • Using ! such as setup({'*', !vim})
  • Passing an option such as setup {'*', vim = {exclude = true}}
  • Both of the above.

I'm leaning towards the first one as long as people realize that !* would make little to no sense. The utility of an option would only be in the case of configuring it dynamically, which I don't think people would do.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.