Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/clipboard place holders #37

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ else
endif

deb:
ifneq ($(shell id -u), 0)
@echo "You must be root to perform this action."
exit 1
ifeq ($(FAKEROOTKEY),)
@echo fakerooting
@fakeroot make deb
else
rm -rf build
mkdir -p build/$(GLOBALLOC)/nautilus-python
Expand All @@ -85,6 +85,7 @@ else
find build/ -type d -exec chmod 0755 {} \;
find build/ -type f -exec chmod 0644 {} \;
chmod +x build/$(GLOBALLOC)/actions-for-nautilus-configurator/start-configurator.sh
endif
dpkg-deb -Z gzip --build build dist/actions-for-nautilus_$(VERSION)_all.deb
lintian dist/actions-for-nautilus_$(VERSION)_all.deb
endif

9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ if you want to see the sample configuration working properly:
* `gedit` - the standard Gnome editor - you probably already have this
* `gnome-terminal` - the standard Gnome terminal emulator (for now) - you probably have
this too.
* `xclip` - a command line tool for managing the X clipboards
* `xclip` - a command line tool for managing the X clipboards
* `zenity` - a Gnome UI toolkit for shell scripts

Again, these can be installed using your platform package manager as shown above.
Expand All @@ -140,6 +140,11 @@ It is also possible that the semantics of the more complex command structures re
upon shell features that, if you are not using BASH as your system shell, will not
work for you.

(Note that there are a number of programs that allow you to manage the X clipboards
from the command line - `xsel`, `xclipboard`, `xcutsel`... - all are usable with this
extension - particularly `xsel`. However for the sample configuration we had to pick one,
so `xclip` was chosen).

### The Gnome Terminal "No Close" profile
When executing the `gnome-terminal` command, the sample configuration references a
`gnome-terminal` profile named "No Close".
Expand Down Expand Up @@ -546,6 +551,8 @@ the same semantics:
| `%s` | the URI scheme from the URI of the first selected item (e.g. `file`) | ANY |
| `%u` | the URI of the first selected item (e.g. `file:///home/me/my-first-dir/my-second-dir/my-file.txt`) | SINGULAR |
| `%U` | space-separated list of the `%u` values of all selected items | PLURAL |
| `%v` | the text content of the system selection | ANY |
| `%V` | the text content of the system clipboard | ANY |
| `%w` | the basename of the first selected item without it's extension (e.g. `my-file`) | SINGULAR |
| `%W` | space-separated list of the `%w` values of all selected items | PLURAL |
| `%x` | the extension of the first selected item without it's extension (e.g. `txt`) | SINGULAR |
Expand Down
10 changes: 10 additions & 0 deletions configurator/command-line-help.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ <h1 id="place-holders">Place Holders</h1>
<td>space-separated list of the <code>%u</code> values of all selected items</td>
<td>PLURAL</td>
</tr>
<tr>
<td><code>%v</code></td>
<td>The textual content of the system selection (as opposed to the content of the clipboard. Analogous to clicking the middle mouse button). <em>Beware: </em>If the text contains quotes and you are using the system shell, parameter quoting may be corrupted.</td>
<td>ANY</td>
</tr>
<tr>
<td><code>%V</code></td>
<td>The textual content of the system clipboard (as opposed to simply selected text. Analogous to hitting CTRL-V). <em>Beware: </em>If the text contains quotes and you are using the system shell, parameter quoting may be corrupted.</td>
<td>ANY</td>
</tr>
<tr>
<td><code>%w</code></td>
<td>the basename of the first selected item without it&#39;s extension (e.g. <code>my-file</code>)
Expand Down
2 changes: 1 addition & 1 deletion configurator/sample-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{
"type": "command",
"label": "Test all place holders",
"command_line": "%O printf '%%s\\n' '#b' %b '#B' %B '#c' %c '#d' %d '#D' %D '#f' %f '#F' %F '#h' %h '#m' %m '#M' %M '#n' %n '#o' %o '#O' %O '#p' %p '#s' %s '#u' %u '#U' %U '#w' %w '#W' %W '#x' %x '#X' %X | zenity --title \"Test all place holders\" --text-info",
"command_line": "%O printf '%%s\\n' '#v' %v '#V' %V '#b' %b '#B' %B '#c' %c '#d' %d '#D' %D '#f' %f '#F' %F '#h' %h '#m' %m '#M' %M '#n' %n '#o' %o '#O' %O '#p' %p '#s' %s '#u' %u '#U' %U '#w' %w '#W' %W '#x' %x '#X' %X | zenity --title \"Test all place holders\" --text-info",
"use_shell": true
},
{
Expand Down
Binary file modified dist/actions-for-nautilus_1.6.0_all.deb
Binary file not shown.
52 changes: 52 additions & 0 deletions extensions/actions-for-nautilus/afn_clipboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#
# I have two implementations because the original used xclip and I wanted
# to preserve that in the sources as the "reference" for semantics while
# we shake out the feature in general.
#
# However, there really isn't a need for a user switch between the GTK
# implementation and the xclip implementation - if GTK is proved to be
# entirely consistent it will be the actual implementation, avoiding a
# dependency on xclip (even though that remains a recommended additional
# tool.
#
# Additionally The GTK implementation has the advantage of internally
# sorting out whether the content of the target clipboard is text-serializable
# without relying on the caller (us) having to trap and process errors.
#
IMPL = "gtk" # "gtk" or "xclip"

if IMPL == "gtk":
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk

global _gtk_clipboard
_gtk_clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
global _gtk_selection
_gtk_selection = Gtk.Clipboard.get(Gdk.SELECTION_PRIMARY)
else:
import os

def _get_from_gtk_clipboard(clipboard):
return clipboard.wait_for_text()

def _get_from_xclip(clipboard):
try:
paste_str = os.popen(f"xclip -out -selection {clipboard}").read()
return "" if not isinstance(paste_str, str) else paste_str
except Exception as error:
print("Clipboard load error", error)
return ""

def get_from_clipboard():
if IMPL == "gtk":
return _get_from_gtk_clipboard(_gtk_clipboard)
else:
return _get_from_xclip("clipboard")

def get_from_selection():
if IMPL == "gtk":
return _get_from_gtk_clipboard(_gtk_selection)
else:
return _get_from_xclip("primary")
12 changes: 11 additions & 1 deletion extensions/actions-for-nautilus/afn_place_holders.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
###
### Place holder replacement functions
###
import re, afn_config
import re, afn_config, afn_clipboard, os

###
### Exported functions and values
Expand Down Expand Up @@ -66,6 +66,14 @@ def _expand_percent_p(index, files, escape):
def _expand_percent_s(index, files, escape):
return files[0]["uri"].scheme

def _expand_percent_v(index, files, escape):
v = afn_clipboard.get_from_selection()
return "" if v is None else v.replace(" ","\\ ") if escape else v

def _expand_percent_V(index, files, escape):
V = afn_clipboard.get_from_clipboard()
return "" if V is None else V.replace(" ","\\ ") if escape else V

#
# SINGULAR (per index)
#
Expand Down Expand Up @@ -142,6 +150,8 @@ def _file_name_extension(basename):
"s": { "f": _expand_percent_s, "behavior": -1},
"u": { "f": _expand_percent_u, "behavior": SINGULAR},
"U": { "f": _expand_percent_U, "behavior": PLURAL},
"v": { "f": _expand_percent_v, "behavior": -1},
"V": { "f": _expand_percent_V, "behavior": -1},
"w": { "f": _expand_percent_w, "behavior": SINGULAR},
"W": { "f": _expand_percent_W, "behavior": PLURAL},
"x": { "f": _expand_percent_x, "behavior": SINGULAR},
Expand Down