BufPreWrite hook dependent on buffer content


The Problem

I want markdown files to get autoformatted on safe.
For that, I want to use pandoc. Since I have markdown files with pandoc’s YAML markdown header and latex content, I need to consider that.
Since the workaround needed for this creates redundant newlines in the beginning of the file (see github isse), I want to adress this by either deleting theses newlines or by changing the formatcmd depending on file content.

Why not use prettier for formatting?

I would like to use --reference-links with pandoc, which creates references for all the links in the document at the bottom of the file.

The Question

Depending on which way I choose, I want to do one of the following:

  • (a) delete three newlines in the beginning of the file or after the YAML header
  • (b) check the file content for a specific line within the YAML header

The problem is that either way, I’m not sure how to approach this since the YAML header starts with --- but ends with either --- or ....
Also, it normally is at the beginning of the file but it doesn’t have to by definition.

So the only way I can come up with for matching this is some sort of complicated PCRE regex, wich wouldn’t be very portable at all.

Maybe soneone else has another idea?

Hi basbebe,
It’s not everything but it may get you started. The top one I got from @dmerejkowsky. The other one I just winged it. Have fun. Bye :wave:

# on every `:write` a hook like this can be done for every file type.
hook global BufWritePost .* %{
  nop %sh{ pandoc --flags "${kak_hook_param}" }
# or on a specific file type you can run something like `pandoc --flags`
hook global WinSetOption filetype=latex %{
  hook window BufWritePost .* %{ sh%{ pdflatex -halt-on-error -file-line-error -output-format=pdf -synctex=1 [...] } }

Hey basbebe, forgot I had this. You could swap it out for pandoc or yaml parser…

hook global WinSetOption filetype=kotlin %{
    # <https://discuss.kakoune.com/t/what-is-the-right-way-to-do-format-on-write/922/2>
    set-option window lintcmd 'ktlint --relative --verbose --editorconfig="$HOME/.editorconfig"'
    set-option window formatcmd 'ktlint --format--relative'

Bye buddy :wave:

Might actually have a solution for you basbebe that I came across today. Source adapted:
nficano 2019, permissions, dotfiles/bin/, viewed 05 March 2021, dotfiles/permissions at master · nficano/dotfiles · GitHub

#/ Shows the permissions of a file or directory in octal form.
#/    permissions /path/to/file/or/dir
#/    permissions /usr/bin    # drwxr-xr-x 755 /usr/bin

usage () {
  grep '^#/' < "$0" | cut -c4-

As the line begin is always known then the problem is only the line end. We can look for simple alternation pattern and pipe the result as above. Also consider the sed utility, a play ground can be found here https://sed.js.org/.

grep '^---.*(---|....)' | utility

Unix utilities are not one off my strengths, so it’s an idea not proof of concept.

If that was no help to you at all, see you tomorrow. Bye :wave:.