Klipper Actions (was Re: Is it normal that text in the clipboard gets lost?)

Stephen Dowdy sdowdy at ucar.edu
Fri Jun 7 03:29:36 BST 2013

Klipper doesn't just do one thing, it does many things and can serve
as a generalized launcher, text-processor, or simple selection
buffer clipboard history manager. It sort-of does it all well, but
also is buggy.

This response is about using klipper for the text-processing
and generalized launcher operations...

FWIW, I've been trying to make 'klipper' do a bit of the old
SunView/OpenWindows TextSW stuff through its "Actions" facility.

This works pretty well, but there seems to be a bug, where if
i select (usually) a URL, klipper becomes unresponsive for exactly
10 seconds.  It's System Tray icon becomes random garbage also
during that 10 seconds.  sometimes, it'll ultimately put the URL
in the klipboard history, but that URL will not be available in
the paste buffer. (have to manually select it again in the
clipboard history)  That's pretty annoying.

Anyway, back to using Actions to emulate TextSW from SunView...

In SunView/OpenWindows, If you had a file
"${HOME}/.text_extras_menu} it could contain stuff like:

    "Sort"                  MENU
            "Alphabetic"            sort
            "Numeric"               sort -n
    "Sort"                  END
    "Capitalize"            MENU
            "abcd -> ABCD"  capitalize -u
            "ABCD -> abcd"  capitalize -l
            "abcd -> Abcd"  capitalize -c
    "Capitalize"            END
    "Shift/Expand/Compress Lines"           MENU
            "Right" shift_lines -t 1
            "Left"  shift_lines -t -1
            "Tabs->Spaces"          expand
            "Spaces->Tabs"          unexpand
    "Shift/Expand/Compress Lines"           END
    "Pretty-print C"        indent -st
    "Brackets"              MENU
            "Insert"        MENU
                    " ( )"  insert_brackets ( )
                    " [ ]"  insert_brackets \[ \]
                    " { }"  insert_brackets { }
                    "`` ''" insert_brackets \" \"
            "Insert"        END
            "Remove"        MENU
                    " ( )"  remove_brackets ( )
                    " [ ]"  remove_brackets \[ \]
                    " { }"  remove_brackets { }
                    "`` ''" remove_brackets \" \"
            "Remove"        END
    "Brackets"              END

So, in any Text Widget from sunview/openview/cde/...
You could select text, Context Click (Right Mouse) select one of
these popup menu items and your selection would be sent to the
specified command as STDIN, and the STDOUT would replace your
selection in the text widget.

This is something i REALLY miss.

However, with "Klipper" actions, you can not only have it
use Regex matching to define what Actions are relevant to
a particular selection, but you can specify NO regex for
the Action, and use a bunch of commands which accept
STDIN and possibly use the builtin "Replace Clipboard"
Output Handling option, or use Ignore and an external
clipboard manager like 'xclip' or 'xsel'.

So, It's not as spiffy as the builtin TextSW stuff, and
takes a couple extra steps, but you can:

    Select Text in a text area
    <CTRL><ALT><R> to bring up Klipper's Action Popup
      (or, automatic if regex matches and "Enable Clipboard Actions"
       is checked - this can get annoying, though)
    Select your Action from the Popup
    Then <CTRL><V> in your text area to replace the
    selection with your action's output, which is
    now in the clipboard history's current slot.

To setup Actions, you need to:

    Left Mouse the Klipper icon (scissors)
    select "Configure Klipper"
    select the "Actions" tab
    Select "Add Action"

- put in a QregExp regex expression if you desire, or leave blank to match anything.
- Deselect Automatic if you don't want it to automatically match
- Put a descriptive name in for the class of commands you will allow for that type of data.
- Then "Add Command" for each command you want.
- Click on the "new command" and put a SHELL command in place.
- You can use "%s" to be replaced with the current contents of the clipboard, but i prefer to use 'xclip -o' or 'xsel -o' to pull the selection out, rather than putting on the command line with "%s" because shell quoting and accidental shell parse failures are not fun.  Then pipe the 'xclip -o' to any other set of commands that produce your desired output, and select "Output Handling" = "Replace Clipboard" if you are processing text, or "Ignore" if you're just acting on it.  
- Put in a useful description so it's obvious.

But, here's some snippets from my klipperrc:

    Description=Debian Bug Report
    Number of commands=1

    Commandline[$e]=bug="%s"; konqueror dbug:$${bug##\\#}
    Description=Launch konqueror with Debian Bug shortcut

That action has 1 command assigned to it.  It regex matches any
series of digits, possibly prefixed by a '#' and gives you the
option to launch konqueror to view a Debian bug report by that
number (stripping the '#' if necessary.)  'dbug' is a standard
Debian (at least) web-shortcut that is:
(see: kcmshell4 ebrowsing)

    Description=Text Conversions
    Number of commands=5

    Commandline[$e]=xclip -o | sed -e 's/^/    /'
    Description=Indent 4 spaces

    Commandline[$e]=xclip -o | tr '[[:lower:]]' '[[:upper:]]'

    Commandline[$e]=xclip -o | sed -e 's/[[:space:]]\\+$$//'
    Description=Trim Trailing Spaces (konsole bug)

    Commandline[$e]=xclip -o | par h1 w68
    Description=PAR wrap 68

    Commandline[$e]=xclip -o | par h1 w78
    Description=PAR wrap 78

Action 1 has NO Regexp, but has 5 commands you can invoke
on arbitrary text.

The first dumps the context of the current selection buffer (xclip -o)
and runs it via 'sed' to insert 4 spaces at the front of every line.
It Replaces the contents of the selection buffer (Output=1) with the
STDOUT of that command.  the second command uppercases everything,
the 3rd trims spaces off Konsole's EXCRUTIATINGLY PAINFUL and
PERSISTENT BUG of padding extra spaces on the end of lines (yes,
i'm aware of the hackaround in 4.10+ of "remove all spaces from
line endings", but i'm still on 4.8.4, etc...)
The last two use 'par' to reflow to 68 or 78 characters / line with a
hanging indent of 1.

You can also have something like:

    Description=Generated Content
    Number of commands=1

    Commandline[$e]=pwgen -1 16
    Description=Generate 16char random password

This doesn't even use the selection buffer contents initially,
but creates a random 16-char password you can PASTE somewhere ;)

Stephen Dowdy  -  Systems Administrator  -  NCAR/RAL
303.497.2869   -  sdowdy at ucar.edu        -  http://www.ral.ucar.edu/~sdowdy/

This message is from the kde mailing list.
Account management:  https://mail.kde.org/mailman/listinfo/kde.
Archives: http://lists.kde.org/.
More info: http://www.kde.org/faq.html.

More information about the kde mailing list