marvinkreis/rofi-file-browser-extended

Feature request: Add Option to copy file to clipboard

Closed this issue · 5 comments

Not sure if this is possible but I think it would be really useful to have an option to copy and paste/drag the file(just like ctrl+c ctrl+v/drag n drop) from a file explorer

Hi, I'd like to try to implement this feature as this would be helpful for me.
@winiciuscota what exact functionality do you have in mind? I thought about reserving ctrl + Return to copy the path.

@marvinkreis do you have any pointers where you would implement this? I just started digging through the code and my C is a littly rusty ;)

Hi @pianoslum. First of all, thanks for contributing. I appreciate it :)

Since I haven't responded to this issue yet: Unfortunately, drag and drop isn't possible with rofi's custom mode API.
The way to implement this would be to assign one of rofi's available keys to a copy (or paste) action.
From what I've gathered, kb-custom-* and kb-accept-alt are keys that can be used for custom modes.
(Maybe others are available now, I haven't checked this in a while.)

Anyways, the key bindings are defined here:

/* Keys for custom key bindings. */
typedef enum FBKey {
KB_CUSTOM_1, KB_CUSTOM_2, KB_CUSTOM_3, KB_CUSTOM_4, KB_CUSTOM_5,
KB_CUSTOM_6, KB_CUSTOM_7, KB_CUSTOM_8, KB_CUSTOM_9, KB_CUSTOM_10,
KB_CUSTOM_11, KB_CUSTOM_12, KB_CUSTOM_13, KB_CUSTOM_14, KB_CUSTOM_15,
KB_CUSTOM_16, KB_CUSTOM_17, KB_CUSTOM_18, KB_CUSTOM_19,
KB_ACCEPT_ALT,
KEY_NONE, // no key is assigned to an action
KEY_UNSUPPORTED // key is not a supported FBKey
} FBKey;
typedef struct {
/* Only KB_CUSTOM_* and KB_ACCEPT_ALT supported. */
/* Key for custom program prompt. */
FBKey open_custom_key;
/* Key for opening file without closing. */
FBKey open_multi_key;
/* Key for toggling hidden files. */
FBKey toggle_hidden_key;
} FileBrowserKeyData;

(rofi -show keys will show your current bindings, which is very helpful for troubleshooting.)

To implement the actual copying part, you'd check for the key press somewhere here:

/* Toggle hidden files with toggle_hidden_key. */
} else if ( key == kd->toggle_hidden_key ) {
fd->show_hidden = ! fd->show_hidden;
load_files ( fd );
retv = RELOAD_DIALOG;

Pretty much just create another else case and check if the configured key is pressed.
Then copy (or paste) the file there.

Finally, to make it configurable, add it as an option here:

/* Set key bindings. */
char *open_custom_key_str = str_arg_or_default ( "-file-browser-open-custom-key", NULL, pd );
char *open_multi_key_str = str_arg_or_default ( "-file-browser-open-multi-key", NULL, pd );
char *toggle_hidden_key_str = str_arg_or_default ( "-file-browser-toggle-hidden-key", NULL, pd );
set_key_bindings ( open_custom_key_str, open_multi_key_str, toggle_hidden_key_str, &pd->key_data );
g_free ( open_custom_key_str );
g_free ( open_multi_key_str );
g_free ( toggle_hidden_key_str );

And extend the key binding validation here (shouldn't be much more that adding it to the arrays and changing the 3s to the new number of key bindings, I think):
void set_key_bindings (
char *open_custom_key_str,
char* open_multi_key_str,
char* toggle_hidden_key_str,
FileBrowserKeyData *kd )
{
kd->open_custom_key = OPEN_CUSTOM_KEY;
kd->open_multi_key = OPEN_MULTI_KEY;
kd->toggle_hidden_key = TOGGLE_HIDDEN_KEY;
FBKey *keys[] = { &kd->open_custom_key,
&kd->open_multi_key,
&kd->toggle_hidden_key };
char *names[] = { "open-custom",
"open-multi",
"toggle-hidden" };
char *params[] = { open_custom_key_str,
open_multi_key_str,
toggle_hidden_key_str };
for ( int i = 0; i < 3; i++ ) {
if ( params[i] != NULL ) {
*keys[i] = get_key_for_name ( params[i] );
if ( *keys[i] == KEY_UNSUPPORTED ) {
print_err ( "Could not match key \"%s\". Disabling key binding for \"%s\". "
"Supported keys are \"kb-accept-alt\", \"kb-custom-[1-19]\" and \"none\" "
"(disables the key binding).\n", params[i], names[i] );
*keys[i] = KEY_NONE;
}
}
}
for ( int i = 0; i < 3; i++ ) {
if ( *keys[i] != KEY_NONE ) {
for ( int j = 0; j < 3; j++ ) {
if ( i != j && *keys[i] == *keys[j] ) {
*keys[j] = KEY_NONE;
char *key_name = get_name_of_key ( *keys[i] );
print_err ( "Detected key binding clash. Both \"%s\" and \"%s\" use \"%s\". "
"Disabling \"%s\".\n", names[i], names[j], key_name, names[j]);
g_free ( key_name );
}
}
}
}
}

Thanks a lot.
After digging around a bit, I think that integrating copying to clipboard in the C-code is much too cumbersome.

I found a little workaround to achieve the same basic functionality:
Create a small bash script copyFilePath basically consisting of echo "$1" | xclip -r and put it somewhere in the path.
The you can either override the standard command (if you only want to copy) or use this command in the open-custom-list.

A little bit more elegant solution would be to offer a 2nd open-command that can be freely configured like the primary one (and set to aforementioned script for the path-copy-functionality).

I could update the Readme about my workaround and/or provide the patch for a second open-command.

What do you think?

Hmm, yeah. I googled a bit, and clipboard manipulation in C indeed seems very cumbersome.

I like the open-custom workaround though. If you already have an idea about what to write in the README, I'd be happy to use it. Otherwise, I'll just write 1 or 2 sentences about it.

As for the 2nd open-command: I already have plans to change the key bindings to map commands to keys instead of the other way around. This would make it possible, to map different open-commands to keys, e.g.:

  • kb-custom-1 -> toggle hidden files
  • kb-accept-entry -> open with xdg-open
  • kb-accept-alt -> open with copyFilePath

But I don't know when I'll get around to implementing this, so might as well change the README for now.

wow, I forgot I had created this issue. Really sorry about that.
I managed to create exactly the solution I wanted shortly after by integrating rofi-file-browser-extended to Dragon-drag-and-drop

The gist with the scripts to copy-n-paste and drag-n-drop: https://gist.github.com/winiciuscota/5790f7b6134518555d7d6b1426298abe

Drag and Drop
drag-n-drop

Copy and Paste(Tested on dolphin only)
copy-n-paste-2