kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

comment.kak (7421B)


      1 # Line comments
      2 # If the language has no line comments, set to ''
      3 declare-option -docstring "characters inserted at the beginning of a commented line" \
      4     str comment_line '#'
      5 
      6 # Block comments
      7 declare-option -docstring "characters inserted before a commented block" \
      8     str comment_block_begin
      9 declare-option -docstring "characters inserted after a commented block" \
     10     str comment_block_end
     11 
     12 # Default comments for all languages
     13 hook global BufSetOption filetype=asciidoc %{
     14     set-option buffer comment_line '//'
     15     set-option buffer comment_block_begin '////'
     16     set-option buffer comment_block_end '////'
     17 }
     18 
     19 hook global BufSetOption filetype=(c|cpp|dart|gluon|go|hjson|java|javascript|json5|kdl|objc|odin|php|pony|protobuf|ron|rust|sass|scala|scss|swift|typescript|typst|vala|groovy) %{
     20     set-option buffer comment_line '//'
     21     set-option buffer comment_block_begin '/*'
     22     set-option buffer comment_block_end '*/'
     23 }
     24 
     25 hook global BufSetOption filetype=(cabal|haskell|moon|idris|elm|dhall|purescript) %{
     26     set-option buffer comment_line '--'
     27     set-option buffer comment_block_begin '{-'
     28     set-option buffer comment_block_end '-}'
     29 }
     30 
     31 hook global BufSetOption filetype=clojure %{
     32     set-option buffer comment_line '#_'
     33     set-option buffer comment_block_begin '(comment '
     34     set-option buffer comment_block_end ')'
     35 }
     36 
     37 hook global BufSetOption filetype=janet %{
     38     set-option buffer comment_line '#'
     39     set-option buffer comment_block_begin '(comment '
     40     set-option buffer comment_block_end ')'
     41 }
     42 
     43 hook global BufSetOption filetype=coffee %{
     44     set-option buffer comment_block_begin '###'
     45     set-option buffer comment_block_end '###'
     46 }
     47 
     48 hook global BufSetOption filetype=(capnp|conf|hyprlang|ssh) %{
     49     set-option buffer comment_line '#'
     50 }
     51 
     52 hook global BufSetOption filetype=css %{
     53     set-option buffer comment_line ''
     54     set-option buffer comment_block_begin '/*'
     55     set-option buffer comment_block_end '*/'
     56 }
     57 
     58 hook global BufSetOption filetype=d %{
     59     set-option buffer comment_line '//'
     60     set-option buffer comment_block_begin '/+'
     61     set-option buffer comment_block_end '+/'
     62 }
     63 
     64 hook global BufSetOption filetype=(fennel|gas|ini) %{
     65     set-option buffer comment_line ';'
     66 }
     67 
     68 hook global BufSetOption filetype=haml %{
     69     set-option buffer comment_line '-#'
     70 }
     71 
     72 hook global BufSetOption filetype=(html|xml) %{
     73     set-option buffer comment_line ''
     74     set-option buffer comment_block_begin '<!--'
     75     set-option buffer comment_block_end '-->'
     76 }
     77 
     78 hook global BufSetOption filetype=julia %{
     79     set-option buffer comment_block_begin '#='
     80     set-option buffer comment_block_end '=#'
     81 }
     82 
     83 hook global BufSetOption filetype=(erlang|latex|mercury) %{
     84     set-option buffer comment_line '%'
     85 }
     86 
     87 hook global BufSetOption filetype=ledger %{
     88     set-option buffer comment_line ';'
     89 }
     90 
     91 hook global BufSetOption filetype=(lisp|scheme) %{
     92     set-option buffer comment_line ';'
     93     set-option buffer comment_block_begin '#|'
     94     set-option buffer comment_block_end '|#'
     95 }
     96 
     97 hook global BufSetOption filetype=lua %{
     98     set-option buffer comment_line '--'
     99     set-option buffer comment_block_begin '--[['
    100     set-option buffer comment_block_end ']]'
    101 }
    102 
    103 hook global BufSetOption filetype=markdown %{
    104     set-option buffer comment_line ''
    105     set-option buffer comment_block_begin '[//]: # "'
    106     set-option buffer comment_block_end '"'
    107 }
    108 
    109 hook global BufSetOption filetype=nix %{
    110     set-option buffer comment_block_begin '/*'
    111     set-option buffer comment_block_end '*/'
    112 }
    113 
    114 hook global BufSetOption filetype=(ocaml|coq) %{
    115     set-option buffer comment_line ''
    116     set-option buffer comment_block_begin '(* '
    117     set-option buffer comment_block_end ' *)'
    118 }
    119 
    120 hook global BufSetOption filetype=((free|object)?pascal|delphi) %{
    121     set-option buffer comment_line '//'
    122     set-option buffer comment_block_begin '{'
    123     set-option buffer comment_block_end '}'
    124 }
    125 
    126 hook global BufSetOption filetype=perl %{
    127     set-option buffer comment_block_begin '#['
    128     set-option buffer comment_block_end ']'
    129 }
    130 
    131 hook global BufSetOption filetype=(pug|zig|cue|hare|gleam) %{
    132     set-option buffer comment_line '//'
    133 }
    134 
    135 hook global BufSetOption filetype=python %{
    136     set-option buffer comment_block_begin "'''"
    137     set-option buffer comment_block_end "'''"
    138 }
    139 
    140 hook global BufSetOption filetype=r %{
    141     set-option buffer comment_line '#'
    142 }
    143 
    144 hook global BufSetOption filetype=ragel %{
    145     set-option buffer comment_line '%%'
    146     set-option buffer comment_block_begin '%%{'
    147     set-option buffer comment_block_end '}%%'
    148 }
    149 
    150 hook global BufSetOption filetype=ruby %{
    151     set-option buffer comment_block_begin '^begin='
    152     set-option buffer comment_block_end '^=end'
    153 }
    154 
    155 hook global BufSetOption filetype=sql %{
    156     set-option buffer comment_line '--'
    157     set-option buffer comment_block_begin '/*'
    158     set-option buffer comment_block_end '*/'
    159 }
    160 
    161 define-command comment-block -docstring '(un)comment selections using block comments' %{
    162     evaluate-commands %sh{
    163         if [ -z "${kak_opt_comment_block_begin}" ] || [ -z "${kak_opt_comment_block_end}" ]; then
    164             echo "fail \"The 'comment_block' options are empty, could not comment the selection\""
    165         fi
    166     }
    167     evaluate-commands -save-regs '"/' -draft %{
    168         # Keep non-empty selections
    169         execute-keys <a-K>\A\s*\z<ret>
    170 
    171         try %{
    172             # Assert that the selection has been commented
    173             set-register / "\A\Q%opt{comment_block_begin}\E.*\Q%opt{comment_block_end}\E\n*\z"
    174             execute-keys "s<ret>"
    175             # Uncomment it
    176             set-register / "\A\Q%opt{comment_block_begin}\E|\Q%opt{comment_block_end}\E\n*\z"
    177             execute-keys s<ret>d
    178         } catch %{
    179             # Comment the selection
    180             set-register '"' "%opt{comment_block_begin}"
    181             execute-keys -draft P
    182             set-register '"' "%opt{comment_block_end}"
    183             execute-keys p
    184         }
    185     }
    186 }
    187 
    188 define-command comment-line -docstring '(un)comment selected lines using line comments' %{
    189     evaluate-commands %sh{
    190         if [ -z "${kak_opt_comment_line}" ]; then
    191             echo "fail \"The 'comment_line' option is empty, could not comment the line\""
    192         fi
    193     }
    194     evaluate-commands -save-regs '"/' -draft %{
    195         # Select the content of the lines, without indentation
    196         execute-keys <a-s>gi<a-l>
    197 
    198         try %{
    199             # Keep non-empty lines
    200             execute-keys <a-K>\A\s*\z<ret>
    201         }
    202 
    203         try %{
    204             set-register / "\A\Q%opt{comment_line}\E\h?"
    205 
    206             try %{
    207                 # See if there are any uncommented lines in the selection
    208                 execute-keys -draft <a-K><ret>
    209 
    210                 # There are uncommented lines, so comment everything
    211                 set-register '"' "%opt{comment_line} "
    212                 align-selections-left
    213                 execute-keys P
    214             } catch %{
    215                 # All lines were commented, so uncomment everything
    216                 execute-keys s<ret>d
    217             }
    218         }
    219     }
    220 }
    221 
    222 define-command align-selections-left -docstring 'extend selections to the left to align with the leftmost selected column' %{
    223     evaluate-commands %sh{
    224         leftmost_column=$(echo "$kak_selections_desc" | tr ' ' '\n' | cut -d',' -f1 | cut -d'.' -f2 | sort -n | head -n1)
    225         aligned_selections=$(echo "$kak_selections_desc" | sed -E "s/\.[0-9]+,/.$leftmost_column,/g")
    226         echo "select $aligned_selections"
    227     }
    228 }