Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow generic 'first line match' across all syntaxes #1600

Open
Grazfather opened this issue Mar 27, 2021 · 7 comments
Open

Allow generic 'first line match' across all syntaxes #1600

Grazfather opened this issue Mar 27, 2021 · 7 comments
Labels
feature-request New feature or request

Comments

@Grazfather
Copy link

Suggested by @keith-hall in #160

The idea is to read the syntax from the first line of input, like a shebang, emacs syntax hint, or vim modeline.

Ideally this wouldn't have to literally be on the first line, but for technical/performance reasons it probably must.

@mmahmoudian
Copy link
Contributor

This is very essential. Most config files don't have extension (e.g vimrc, .zshrc, .bashrc) and their format is typically defined by their first line as explained by OP. I hope this feature gets more traction. Also having this feature would make the integration of bat easier in TUI file managers. For example in vifm I have the following which passes the vifmrc (*rc) to bat, but bat just show it as a plain text even when it has the " vim: filetype=vim :

fileviewer *.R,*.md,*.sh,*.py,*.yml,*.toml,*.el,*.ini,*.json,*rc
    \ env -uCOLORTERM 
    \ bat 
        \ --force-colorization 
        \ --line-range=:%ph 
        \ --terminal-width=%pw 
        \ --wrap='never' 
        \ --pager='never' 
        \ --style='plain' 
        \ --tabs=2 
        \ %c

image

@Freed-Wu
Copy link
Contributor

Excuse me.
How can we define the map of shebang and syntax? I know bat -m'*.tlu:Lua a.tlu can map tlu extension to Lua. However can we map a shebang to a syntax? Like:

#! /usr/bin/env texlua
...

Then we use bat -Mtexlua:Lua can view the file with the highlight of Lua?

Thanks.

@krishnakumarg1984
Copy link

I am very interested in this first-line match request. Have there been any further thoughts on this?

@glyh
Copy link

glyh commented Feb 4, 2022

actually vim modelines can occur at the last line of the file... And consider this common lisp & bash hybrid script, how would you highlight it?

#!/bin/sh
#|-*- mode:lisp -*-|#
#|
exec ros -Q -- $0 "$@"
|#
(progn ;;init forms
  (ros:ensure-asdf)
  #+quicklisp(ql:quickload '() :silent t))
  

(defpackage :ros.script.test.3852975384
  (:use :cl))
(in-package :ros.script.test.3852975384)

(defun main (&rest argv)
  (declare (ignorable argv)))
;;; vim: set ft=lisp lisp:

@Grazfather
Copy link
Author

Similar:

#!/usr/bin/env bb
#_" -*- mode: clojure; -*-"

@eggbean
Copy link

eggbean commented Oct 24, 2023

Please implement this!

@mmahmoudian
Copy link
Contributor

actually vim modelines can occur at the last line of the file... And consider this common lisp & bash hybrid script, how would you highlight it?

#!/bin/sh
#|-*- mode:lisp -*-|#
#|
exec ros -Q -- $0 "$@"
|#
(progn ;;init forms
  (ros:ensure-asdf)
  #+quicklisp(ql:quickload '() :silent t))
  

(defpackage :ros.script.test.3852975384
  (:use :cl))
(in-package :ros.script.test.3852975384)

(defun main (&rest argv)
  (declare (ignorable argv)))
;;; vim: set ft=lisp lisp:

I don't see much of bash here apart from the shebang, but that said, as far as I understand this thread is about syntax highlighting, and therefore we should focus only on what the user wants to read rather than how it should be executed. For this reason my suggestions are as follows:

  1. Vim or Emacs modelines have higher priority compared to shebang

  2. If a file has both Vim and Emacs lines and if there is a discrepancy between them, give priority to the one that appears first in the file

So imho the code needs to check the first few lines and the last few lines of the given file (if it is not binary) for Vim, Emacs, and shebang, then apply the priorities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants