aboutsummaryrefslogtreecommitdiff
path: root/config_cmd.h.in
blob: 7c4a9f4672562131e25f2777173b536f683c5229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// DFM - Dylan's File Manager - Command Configuration file.
//
// Commands can be created using the FM_CMD macro which declares a function
// that holds a struct fm_cmd and calls fm_cmd().
//
// struct fm_cmd {
//   cut prompt;          // Prompt text.
//   cut left;            // Text left of cursor.
//   cut right;           // Text right of cursor.
//   fm_key_press press;  // Callback on press.
//   fm_key_enter enter;  // Callback on enter.
//   u32 config;          // Configuration.
// };
//
// The config field supports the following options:
//
// CMD_BG           - Run the command in the background.
// CMD_CONFLICT     - Prompt on file conflicts.
// CMD_MUT          - Hint that the command might mutate directory entries.
// CMD_EXEC         - Skip the interactive prompt and execute the command.
// CMD_MARK_DIR     - Command must run in the mark directory.
// CMD_NOT_MARK_DIR - Command must not run in the mark directory.
// CMD_STDIN        = Feed file under cursor to command via stdin.
// CMD_FILE_CURSOR  = Ignore marks and add the name under the cursor to input.
// CMD_EXEC_MARK    = Skip interactive prompt only if marks exist..
// CMD_EXEC_ROOT    = Skip interactive prompt even if root.
!!
## Shell cript can be embedded within dfm by using the embed command.
## The script will be run in $SHELL as the argument following '-c'.
##
##  FM_CMD(cmd_bulk_rename,
##   .prompt = CUT_NULL,
##   .left   = CUT($(embed script/bulk-rename)),
##   .enter  = fm_cmd_run_sh,
##   .config = CMD_MARK_DIR | CMD_MUT | CMD_EXEC,
## )
##
## Another useful example is embedding your opener script.
##
## FM_CMD(cmd_opener,
##   .prompt = CUT_NULL,
##   .left   = CUT($(embed script/opener_ext)),
##   .enter  = fm_cmd_run_sh,
##   .config = CMD_MARK_DIR | CMD_EXEC,
## )
##
## Shell can also be typed directly but its cumbersone to add \\n to the end of
## each line and escape backslashes and double quotes.
##
##  .left   = CUT("echo \\"$@\\"\\n"),
!!
// If the FM_CMD system is too limiting you can define your own functions and
// bind them to keys. The same function signature is used in navigation and
// input modes.
//
// static void
// my_custom_fn(struct fm *p)
// {
//   // Go wild.
// }
//
// Refer to dfm.c for more information.
//

FM_CMD(cmd_exec,
  .prompt = CUT(":"),
  .enter  = fm_cmd_run,
  .config = CMD_MUT,
)

FM_CMD(cmd_exec_sh,
  .prompt = CUT(":!"),
  .enter  = fm_cmd_run_sh,
  .config = CMD_MUT | CMD_STDIN,
)

FM_CMD(cmd_exec_stdin,
  .prompt = CUT(":<"),
  .enter  = fm_cmd_run,
  .config = CMD_MUT | CMD_STDIN,
)

FM_CMD(cmd_exec_open,
  .prompt = CUT(":"),
  .right  = CUT(" %m"),
  .enter  = fm_cmd_run,
)

FM_CMD(cmd_exec_open_bg,
  .prompt = CUT(":"),
  .right  = CUT(" %m &"),
  .enter  = fm_cmd_run
)

FM_CMD(cmd_link,
  .prompt = CUT(":"),
  .left   = CUT("ln -sf %m %d"),
  .enter  = fm_cmd_run,
  .config = CMD_NOT_MARK_DIR | CMD_MUT | CMD_EXEC_MARK | CMD_CONFLICT,
)

FM_CMD(cmd_remove,
  .prompt = CUT(":"),
  .left   = CUT("rm -rf %m"),
  .enter  = fm_cmd_run,
  .config = CMD_MARK_DIR | CMD_MUT | CMD_EXEC_MARK,
)

FM_CMD(cmd_copy,
  .prompt = CUT(":"),
  .left   = CUT("cp -Rf %m %d"),
  .enter  = fm_cmd_run,
  .config = CMD_NOT_MARK_DIR | CMD_MUT | CMD_EXEC_MARK | CMD_CONFLICT,
)

FM_CMD(cmd_move,
  .prompt = CUT(":"),
  .left   = CUT("mv -f %m %d"),
  .enter  = fm_cmd_run,
  .config = CMD_NOT_MARK_DIR | CMD_MUT | CMD_EXEC_MARK | CMD_CONFLICT,
)

FM_CMD(cmd_rename,
  .prompt = CUT(":"),
  .left   = CUT("mv -f %f "),
  .enter  = fm_cmd_run,
  .config = CMD_FILE_CURSOR | CMD_MUT,
)

FM_CMD(cmd_chmod,
  .prompt = CUT(":"),
  .left   = CUT("chmod"),
  .right  = CUT(" %m"),
  .enter  = fm_cmd_run,
  .config = CMD_MUT,
)

FM_CMD(cmd_chown,
  .prompt = CUT(":"),
  .left   = CUT("chown"),
  .right  = CUT(" %m"),
  .enter  = fm_cmd_run,
  .config = CMD_MUT,
)

FM_CMD(cmd_copy_clipboard,
  .prompt = CUT(":"),
  .left   = get_env("DFM_COPYER", DFM_COPYER),
  .enter  = fm_cmd_run,
  .config = CMD_EXEC | CMD_STDIN,
)

FM_CMD(cmd_cd,
  .prompt = CUT(":cd "),
  .enter  = fm_cmd_cd,
)

FM_CMD(cmd_touch,
  .prompt = CUT(":"),
  .left   = CUT("touch "),
  .enter  = fm_cmd_run,
  .config = CMD_MUT,
)

FM_CMD(cmd_mkdir,
  .prompt = CUT(":"),
  .left   = CUT("mkdir -p "),
  .enter  = fm_cmd_run,
  .config = CMD_MUT,
)

FM_CMD(cmd_trash,
  .prompt = CUT(":"),
  .left   = get_env("DFM_TRASH", DFM_TRASH),
  .right  = CUT(" %m"),
  .enter  = fm_cmd_run,
  .config = CMD_MARK_DIR | CMD_MUT | CMD_EXEC_MARK,
)

FM_CMD(cmd_bulk_rename,
  .prompt = CUT_NULL,
  .left   = CUT($(embed script/bulk-rename)),
  .enter  = fm_cmd_run_sh,
  .config = CMD_MARK_DIR | CMD_MUT | CMD_EXEC,
)

FM_CMD(cmd_editor,
  .prompt = CUT_NULL,
  .left   = CUT("kak %m"),
  .enter  = fm_cmd_run,
  .config = CMD_MUT | CMD_EXEC,
)

#define PBUFS 256
/* If you have paths longer than 256 chars I can't help you */

static void
cmd_gd(struct fm *p)
{
  char pbuf[PBUFS];
  FILE *fzf = popen("fd -t d -u | fzf --algo=v1", "r");
  fgets(pbuf, PBUFS, fzf);
  // Not sure why, but the -1 on strlen is required
  if (pbuf[0] == '/')
    fm_path_cd(p, pbuf, strlen(pbuf)-1);
  else
    fm_path_cd_relative(p, pbuf, strlen(pbuf)-1);
  pclose(fzf);
}

//FM_CMD(cmd_gd,
//  .prompt = CUT_NULL,
//  .left   = CUT("fd -t d -u | fzf --algo=v1"),
//  .enter  = fm_cmd_cd,
//  .config = CMD_MUT | CMD_EXEC,
//)