nnn

mirror of https://github.com/jarun/nnn
Log | Files | Refs | README | LICENSE

nnn.h (7532B)


      1 /*
      2  * BSD 2-Clause License
      3  *
      4  * Copyright (C) 2014-2016, Lazaros Koromilas <lostd@2f30.org>
      5  * Copyright (C) 2014-2016, Dimitris Papastamos <sin@2f30.org>
      6  * Copyright (C) 2016-2026, Arun Prakash Jana <engineerarun@gmail.com>
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions are met:
     11  *
     12  * * Redistributions of source code must retain the above copyright notice, this
     13  *   list of conditions and the following disclaimer.
     14  *
     15  * * Redistributions in binary form must reproduce the above copyright notice,
     16  *   this list of conditions and the following disclaimer in the documentation
     17  *   and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #pragma once
     32 
     33 #include <curses.h>
     34 #include <wchar.h>
     35 
     36 #define CONTROL(c) ((c) & 0x1f)
     37 
     38 #ifndef ESC
     39 #define ESC (27)
     40 #endif
     41 
     42 #ifndef DEL
     43 #define DEL (127)
     44 #endif
     45 
     46 /* Supported actions */
     47 enum action {
     48 	SEL_BACK = 1,
     49 	SEL_OPEN,
     50 	SEL_NAV_IN,
     51 	SEL_NEXT,
     52 	SEL_PREV,
     53 	SEL_PGDN,
     54 	SEL_PGUP,
     55 	SEL_CTRL_D,
     56 	SEL_CTRL_U,
     57 	SEL_HOME,
     58 	SEL_END,
     59 	SEL_FIRST,
     60 	SEL_JUMP,
     61 	SEL_YOUNG,
     62 	SEL_CDHOME,
     63 	SEL_CDBEGIN,
     64 	SEL_CDLAST,
     65 	SEL_CDROOT,
     66 	SEL_BMOPEN,
     67 	SEL_REMOTE,
     68 	SEL_CYCLE,
     69 	SEL_CYCLER,
     70 	SEL_CTX1,
     71 	SEL_CTX2,
     72 	SEL_CTX3,
     73 	SEL_CTX4,
     74 	SEL_CTX5,
     75 	SEL_CTX6,
     76 	SEL_CTX7,
     77 	SEL_CTX8,
     78 	SEL_MARK,
     79 	SEL_BMARK,
     80 	SEL_FLTR,
     81 	SEL_MFLTR,
     82 	SEL_HIDDEN,
     83 	SEL_DETAIL,
     84 	SEL_STATS,
     85 	SEL_CHMODX,
     86 	SEL_ARCHIVE,
     87 	SEL_SORT,
     88 	SEL_REDRAW,
     89 	SEL_SEL,
     90 	SEL_SELMUL,
     91 	SEL_SELALL,
     92 	SEL_SELINV,
     93 	SEL_SELEDIT,
     94 	SEL_CP,
     95 	SEL_MV,
     96 	SEL_CPMVAS,
     97 	SEL_TRASH,
     98 	SEL_RM_RF,
     99 	SEL_OPENWITH,
    100 	SEL_NEW,
    101 	SEL_RENAME,
    102 	SEL_RENAMEMUL,
    103 	SEL_UMOUNT,
    104 	SEL_HELP,
    105 	SEL_AUTONEXT,
    106 	SEL_EDIT,
    107 	SEL_PLUGIN,
    108 	SEL_SELSIZE,
    109 	SEL_SHELL,
    110 	SEL_LAUNCH,
    111 	SEL_PROMPT,
    112 	SEL_LOCK,
    113 	SEL_SESSIONS,
    114 	SEL_EXPORT,
    115 	SEL_TIMETYPE,
    116 	SEL_PREVIEW,
    117 	SEL_QUITCTX,
    118 	SEL_QUITCD,
    119 	SEL_QUIT,
    120 	SEL_QUITERR,
    121 #ifndef NOMOUSE
    122 	SEL_CLICK,
    123 #endif
    124 	SEL_MAX,
    125 };
    126 
    127 /* Associate a pressed key to an action */
    128 struct key {
    129 	wint_t sym;      /* Key pressed */
    130 	enum action act; /* Action */
    131 };
    132 
    133 static struct key bindings[] = {
    134 	/* Back */
    135 	{ KEY_LEFT,       SEL_BACK },
    136 	{ 'h',            SEL_BACK },
    137 	/* Inside or select */
    138 	{ KEY_ENTER,      SEL_OPEN },
    139 	{ '\r',           SEL_OPEN },
    140 	/* Pure navigate inside */
    141 	{ KEY_RIGHT,      SEL_NAV_IN },
    142 	{ 'l',            SEL_NAV_IN },
    143 	/* Next */
    144 	{ 'j',            SEL_NEXT },
    145 	{ KEY_DOWN,       SEL_NEXT },
    146 	/* Previous */
    147 	{ 'k',            SEL_PREV },
    148 	{ KEY_UP,         SEL_PREV },
    149 	/* Page down */
    150 	{ KEY_NPAGE,      SEL_PGDN },
    151 	/* Page up */
    152 	{ KEY_PPAGE,      SEL_PGUP },
    153 	/* Ctrl+D */
    154 	{ CONTROL('D'),   SEL_CTRL_D },
    155 	/* Ctrl+U */
    156 	{ CONTROL('U'),   SEL_CTRL_U },
    157 	/* First entry */
    158 	{ KEY_HOME,       SEL_HOME },
    159 	{ 'g',            SEL_HOME },
    160 	{ CONTROL('A'),   SEL_HOME },
    161 	/* Last entry */
    162 	{ KEY_END,        SEL_END },
    163 	{ 'G',            SEL_END },
    164 	{ CONTROL('E'),   SEL_END },
    165 	/* Go to first file */
    166 	{ '\'',           SEL_FIRST },
    167 	/* Jump to an entry number/offset */
    168 	{ 'J',            SEL_JUMP },
    169 	{ CONTROL('Y'),   SEL_YOUNG },
    170 	/* HOME */
    171 	{ '~',            SEL_CDHOME },
    172 	/* Initial directory */
    173 	{ '@',            SEL_CDBEGIN },
    174 	/* Last visited dir */
    175 	{ '-',            SEL_CDLAST },
    176 	/* Go to / */
    177 	{ '`',            SEL_CDROOT },
    178 	/* Leader key */
    179 	{ 'b',            SEL_BMOPEN },
    180 	{ CONTROL('_'),   SEL_BMOPEN },
    181 	/* Connect to server over SSHFS */
    182 	{ 'c',            SEL_REMOTE },
    183 	/* Cycle contexts in forward direction */
    184 	{ '\t',           SEL_CYCLE },
    185 	/* Cycle contexts in reverse direction */
    186 	{ KEY_BTAB,       SEL_CYCLER },
    187 	/* Go to/create context N */
    188 	{ '1',            SEL_CTX1 },
    189 	{ '2',            SEL_CTX2 },
    190 	{ '3',            SEL_CTX3 },
    191 	{ '4',            SEL_CTX4 },
    192 	{ '5',            SEL_CTX5 },
    193 	{ '6',            SEL_CTX6 },
    194 	{ '7',            SEL_CTX7 },
    195 	{ '8',            SEL_CTX8 },
    196 	/* Mark a path to visit later */
    197 	{ ',',            SEL_MARK },
    198 	/* Create a bookmark */
    199 	{ 'B',            SEL_BMARK },
    200 	/* Filter */
    201 	{ '/',            SEL_FLTR },
    202 	/* Toggle filter mode */
    203 	{ CONTROL('N'),   SEL_MFLTR },
    204 	/* Toggle hide .dot files */
    205 	{ '.',            SEL_HIDDEN },
    206 	/* Detailed listing */
    207 	{ 'd',            SEL_DETAIL },
    208 	/* File details */
    209 	{ 'f',            SEL_STATS },
    210 	{ CONTROL('F'),   SEL_STATS },
    211 	/* Toggle executable status */
    212 	{ '*',            SEL_CHMODX },
    213 	/* Create archive */
    214 	{ 'z',            SEL_ARCHIVE },
    215 	/* Sort toggles */
    216 	{ 't',            SEL_SORT },
    217 	{ CONTROL('T'),   SEL_SORT },
    218 	/* Redraw window */
    219 	{ CONTROL('L'),   SEL_REDRAW },
    220 	/* Select current file path */
    221 	{ ' ',            SEL_SEL },
    222 	{ '+',            SEL_SEL },
    223 	/* Toggle select multiple files */
    224 	{ 'm',            SEL_SELMUL },
    225 	/* Select all files in current dir */
    226 	{ 'a',            SEL_SELALL },
    227 	/* Invert selection in current dir */
    228 	{ 'A',            SEL_SELINV },
    229 	/* List, edit selection */
    230 	{ 'E',            SEL_SELEDIT },
    231 	/* Copy from selection buffer */
    232 	{ 'p',            SEL_CP },
    233 	{ CONTROL('P'),   SEL_CP },
    234 	/* Move from selection buffer */
    235 	{ 'v',            SEL_MV },
    236 	{ CONTROL('V'),   SEL_MV },
    237 	/* Copy/move from selection buffer and rename */
    238 	{ 'w',            SEL_CPMVAS },
    239 	{ CONTROL('W'),   SEL_CPMVAS },
    240 	/* Delete from selection buffer */
    241 	{ 'x',            SEL_TRASH },
    242 	{ CONTROL('X'),   SEL_TRASH },
    243 	{ 'X',            SEL_RM_RF },
    244 	/* Open in a custom application */
    245 	{ 'o',            SEL_OPENWITH },
    246 	{ CONTROL('O'),   SEL_OPENWITH },
    247 	/* Create a new file */
    248 	{ 'n',            SEL_NEW },
    249 	/* Show rename prompt */
    250 	{ CONTROL('R'),   SEL_RENAME },
    251 	/* Rename contents of current dir */
    252 	{ 'r',            SEL_RENAMEMUL },
    253 	/* Disconnect a SSHFS mount point */
    254 	{ 'u',            SEL_UMOUNT },
    255 	/* Show help */
    256 	{ '?',            SEL_HELP },
    257 	/* Toggle auto-advance on file open */
    258 	{ CONTROL('J'),   SEL_AUTONEXT },
    259 	/* Edit in EDITOR */
    260 	{ 'e',            SEL_EDIT },
    261 	/* Run a plugin */
    262 	{ ';',            SEL_PLUGIN },
    263 	/* Show total size of listed selection */
    264 	{ 'S',            SEL_SELSIZE },
    265 	/* Run command */
    266 	{ '!',            SEL_SHELL },
    267 	{ CONTROL(']'),   SEL_SHELL },
    268 	/* Launcher */
    269 	{ '=',            SEL_LAUNCH },
    270 	/* Show command prompt */
    271 	{ ']',            SEL_PROMPT },
    272 	/* Lock screen */
    273 	{ '0',            SEL_LOCK },
    274 	/* Manage sessions */
    275 	{ 's',            SEL_SESSIONS },
    276 	/* Export list */
    277 	{ '>',            SEL_EXPORT },
    278 	/* Set time type */
    279 	{ 'T',            SEL_TIMETYPE },
    280 	/* Toggle preview pane */
    281 	{ 'P',            SEL_PREVIEW },
    282 	/* Quit a context */
    283 	{ 'q',            SEL_QUITCTX },
    284 	/* Change dir on quit */
    285 	{ CONTROL('G'),   SEL_QUITCD },
    286 	/* Quit */
    287 	{ CONTROL('Q'),   SEL_QUIT },
    288 	/* Quit with an error code */
    289 	{ 'Q',            SEL_QUITERR },
    290 #ifndef NOMOUSE
    291 	{ KEY_MOUSE,      SEL_CLICK },
    292 #endif
    293 };