/* BB Key Bindings User-defined key bindings go in config.h, which is created by running `make` (config.def.h is for keeping the defaults around, just in case) The basic structure is: (for the help menu) (whether to run in a full terminal window or silently, etc.) When the scripts are run, the following values are provided as environment variables: $@ (the list of arguments): the full paths of the selected files $BBCURSOR: the full name of the file under the cursor $BB_DEPTH: the number of `bb` instances deep (in case you want to run a shell and have that shell print something special in the prompt) $BBCMD: a file to which `bb` commands can be written (used internally) In order to modify bb's internal state, you can call `bb +cmd`, where "cmd" is one of the following commands (or a unique prefix of one): cd: Navigate to columns: Change which columns are visible, and in what order deselect: Deselect dots[:yes|:no] Toggle whether dotfiles are visible goto: Move the cursor to (changing directory if needed) jump: Jump to the mark associated with mark:[=] Associate with (or current dir, if blank) move: Move the cursor a numeric amount option:([=|~|!])+ Set options (see below) quit Quit bb refresh Refresh the file listing scroll: Scroll the view a numeric amount select: Select spread: Spread the selection state at the cursor toggle: Toggle the selection status of Currently supported options: 's': sort, one of (n)ame (s)ize (c)reation (m)odification (a)ccess (p)ermission (r)andom 'r': reverse-sort (boolean) '.': show dotfiles (boolean) 'i': interleave files and directories (boolean), when false, directories are always at the top '0'-'9': what to put in each of the (maximum of 10) columns (one of: [nscmap]) 'A'-'J': how wide to make each column (A -> column 0, etc.). 0 means minimum width, 1+ means divide the free space proportionally among all nonzero columns The postfix operator '!' sets an option to 0, '~' toggles an option, '=' assigns an option to the following character value, and no postfix operator sets an option to 1. Internally, bb will write the commands (NUL terminated) to $BBCMD, if $BBCMD is set, and read the file when file browsing resumes. These commands can also be passed to bb at startup, and will run immediately. E.g. `bb +col:n +sort:r .` will launch `bb` only showing the name column, randomly sorted. *Note: for numeric-based commands (like scroll), the number can be either an absolute value or a relative value (starting with '+' or '-'), and/or a percent (ending with '%'). Scrolling and moving, '%' means percent of screen height, and '%n' means percent of number of files (e.g. +50% means half a screen height down, and 100%n means the last file) */ #include "bterm.h" // Configurable options: #define KEY_DELAY 50 #define SCROLLOFF MIN(5, (termheight-4)/2) #define QUOTE(s) #s #define CMDFILE_FORMAT "/tmp/bb.XXXXXX" #define SORT_INDICATOR "↓" #define RSORT_INDICATOR "↑" #define SELECTED_INDICATOR "\033[33;1m★\033[0m" #define NOT_SELECTED_INDICATOR "\033[1m \033[0m" #define TITLE_COLOR "\033[32;1m" #define NORMAL_COLOR "\033[37m" #define CURSOR_COLOR "\033[1;30;43m" #define LINK_COLOR "\033[35m" #define DIR_COLOR "\033[34m" #define EXECUTABLE_COLOR "\033[31m" #define PIPE_SELECTION_TO " printf '%s\\n' \"$@\" | " #define PAUSE " read -n1 -p '\033[2mPress any key to continue...\033[0m\033[?25l'" #define NORMAL_TERM (1<<0) #define AT_CURSOR (1<<1) #define MAX_REBINDINGS 8 typedef struct { int keys[MAX_REBINDINGS+1]; const char *command; const char *description; int flags; } binding_t; // These commands will run at startup (before command-line arguments) extern const char *startupcmds[]; const char *startupcmds[] = { ////////////////////////////////////////////// // User-defined startup commands can go here ////////////////////////////////////////////// // Set some default marks: "+mark:0", "+mark:~=~", "+mark:h=~", "+mark:/=/", "+mark:c=~/.config", "+mark:l=~/.local", // Default column and sorting options: "+opt:0=s,1=m,2=p,3=n,s=n,D=1", NULL, // NULL-terminated array }; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdollar-in-identifier-extension" extern binding_t bindings[]; binding_t bindings[] = { ////////////////////////////////////////////////////////////////////////// // User-defined custom scripts can go here // Please note that these are sh scripts, not bash scripts, so bash-isms // won't work unless you make your script use `bash -c ""` ////////////////////////////////////////////////////////////////////////// {{'?'}, "bb -b | $PAGER -r", "Show the help menu", NORMAL_TERM}, {{KEY_CTRL_H}, "", "Figure out what key does"}, {{'q', 'Q'}, "+quit", "Quit"}, {{'k', KEY_ARROW_UP}, "+move:-1", "Move up"}, {{'j', KEY_ARROW_DOWN}, "+move:+1", "Move down"}, {{'h', KEY_ARROW_LEFT}, "+cd:..", "Go up a folder"}, {{'l', KEY_ARROW_RIGHT}, "test -d \"$BBCURSOR\" && bb \"+cd:$BBCURSOR\"", "Enter a folder"}, {{' ','v','V'}, "+toggle", "Toggle selection"}, {{'e'}, "$EDITOR \"$@\"", "Edit file in $EDITOR", NORMAL_TERM}, {{'\r', KEY_MOUSE_DOUBLE_LEFT}, #ifdef __APPLE__ QUOTE( if test -d "$BBCURSOR"; then bb "+cd:$BBCURSOR"; elif test -x "$BBCURSOR"; then "$BBCURSOR"; read -p 'Press any key to continue...' -n1; elif file -bI "$BBCURSOR" | grep '^\(text/\|inode/empty\)' >/dev/null; then $EDITOR "$BBCURSOR"; else open "$BBCURSOR"; fi )/*ENDQUOTE*/, #else QUOTE( if test -d "$BBCURSOR"; then bb "+cd:$BBCURSOR"; elif file -bi "$BBCURSOR" | grep '^\(text/\|inode/empty\)' >/dev/null; then $EDITOR "$BBCURSOR"; else xdg-open "$BBCURSOR"; fi )/*ENDQUOTE*/, #endif "Open file", NORMAL_TERM}, {{'f'}, "bb \"+g:`fzf`\"", "Fuzzy search for file", NORMAL_TERM}, {{'/'}, "bb \"+g:`ls -a|fzf`\"", "Fuzzy select file", NORMAL_TERM}, {{'L'}, PIPE_SELECTION_TO "$PAGER", "List all selected files", NORMAL_TERM}, {{'d', KEY_DELETE}, "rm -rfi \"$@\"; bb '+d:*' +r", "Delete files", AT_CURSOR}, {{'D'}, "rm -rf \"$@\"; bb '+d:*' +r", "Delete files without confirmation"}, {{'M'}, "mv -i \"$@\" .; bb '+d:*' +r", "Move files to current folder"}, {{'c'}, "cp -i \"$@\" .; bb +r", "Copy files to current folder"}, {{'C'}, "for f; do cp \"$f\" \"$f.copy\"; done; bb +r", "Clone files"}, {{'n'}, "name=`bb '?New file: '` && touch \"$name\"; bb +r \"+goto:$name\"", "New file"}, {{'N'}, "name=`bb '?New dir: '` && mkdir \"$name\"; bb +r \"+goto:$name\"", "New folder"}, {{'|'}, "cmd=`bb '?|'` && " PIPE_SELECTION_TO "sh -c \"$cmd\" && " PAUSE "; bb +r", "Pipe selected files to a command"}, {{':'}, "$SHELL -c \"`bb '?:'`\" -- \"$@\"; " PAUSE "; bb +refresh", "Run a command"}, {{'>'}, "$SHELL", "Open a shell", NORMAL_TERM}, {{'m'}, "read -n1 -p 'Mark: ' m && bb \"+mark:$m;$PWD\"", "Set mark"}, {{'\''}, "read -n1 -p 'Jump: ' j && bb \"+jump:$j\"", "Jump to mark"}, {{'r'}, QUOTE( bb '+deselect:*' +refresh; for f; do if renamed="$(dirname "$f")/$(bb '?Rename: ' "$(basename "$f")")" && test "$f" != "$renamed" && mv -i "$f" "$renamed"; then test $BBSELECTED && bb "+select:$renamed"; elif test $BBSELECTED; then bb "+select:$f"; fi done)/*ENDQUOTE*/, "Rename files", AT_CURSOR}, {{'R'}, QUOTE( if patt="`bb '?Rename pattern: ' 's/'`"; then true; else bb +r; exit; fi; if sed -E "$patt" /dev/null 2>/dev/null && bb \"+sel:$f\"; done", "Regex select files"}, {{'J'}, "+spread:+1", "Spread selection down"}, {{'K'}, "+spread:-1", "Spread selection up"}, {{'s'}, "read -n1 -p 'Sort \033[1m(a)\033[22mlphabetic " "\033[1m(s)\033[22mize \033[1m(m)\033[22modification \033[1m(c)\033[22mcreation " "\033[1m(a)\033[22maccess \033[1m(r)\033[22mandom \033[1m(p)\033[22mermissions:\033[0m ' sort " "&& bb \"+opt:s=$sort\"", "Sort by..."}, {{'#'}, "cols=`bb '?Set columns: '` && " "bb \"+opt:`echo $cols 0 | fold -w1 | sed 10q | nl -v0 -s= -w1 | paste -sd '\\0' -`\"", "Set columns"}, {{'.'}, "+opt:.~", "Toggle dotfiles"}, {{'g', KEY_HOME}, "+move:0", "Go to first file"}, {{'G', KEY_END}, "+move:100%n", "Go to last file"}, {{KEY_ESC}, "+deselect:*", "Clear selection"}, {{KEY_F5, KEY_CTRL_R}, "+refresh", "Refresh"}, {{KEY_CTRL_A}, "+select:*", "Select all files in current folder"}, {{KEY_PGDN}, "+scroll:+100%", "Page down"}, {{KEY_PGUP}, "+scroll:-100%", "Page up"}, {{KEY_CTRL_D}, "+scroll:+50%", "Half page down"}, {{KEY_CTRL_U}, "+scroll:-50%", "Half page up"}, {{KEY_MOUSE_WHEEL_DOWN}, "+scroll:+3", "Scroll down"}, {{KEY_MOUSE_WHEEL_UP}, "+scroll:-3", "Scroll up"}, {0}, // Array must be 0-terminated }; #pragma clang diagnostic pop // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1