Linux program - reverse highlighted text

323 Views Asked by At

I want to develop a program that will run on the background of my Ubuntu 12.04 so when some text is selected in some textbox of any running program and some key-combination is pressed (like ctrl-F12), the text will be cut, reversed, and pasted in the same place.

I know some programs that do it on windows.

It is useful in some programs and web-pages that do not support right-to-left languages like arabic and hebrew - the letters are printed from left to right so the text becomes reversed.

More specifically I need it in Prezi that has this kind of bug in their embedded flash editor (I thought about writing a chrome-plugin, but I don't think such plugin can manipulate the selected text inside flash objects).

Do you know if such a program exist? Where should I start reading in order to develop a program with such capabilities (manipulate selected text in other programs)?

Thanks

2

There are 2 best solutions below

3
On

My partial solution - A python script that reverses the clipboard text:

#!/usr/bin/env python
from gi.repository import Gtk, Gdk

clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
# if you want to take the selected text without copying, set primary to:
# Gtk.Clipboard.get(Gdk.SELECTION_PRIMARY)
# (doesn't always work)
primary = clipboard 

def run():
    text = primary.wait_for_text()
    if not isinstance(text, str):
        return
    reversed_text = u''.join(reversed(text.decode('utf-8')))
    clipboard.set_text(reversed_text, -1)
    clipboard.store()

if __name__ == '__main__':
    run()

Then define a keyboard shortcut to run the script (I used alt-Insert), and reverse selected the by copying it to the clipboard, call the script and paste it back (ctrl-Insert, alt-Insert, shift-Insert).

I'm still looking for a better solution, so I could use a single keyboard shortcut without overriding the clipboard.

1
On

Below is an input method for Emacs that will type text in reverse:

(defun reverse-input-method (key)
  (if buffer-read-only
      (list key)
    (if (setq new key)
        (list new ?\2) ;; ?\2 == backwards char
      (list key ?\2))))

(defun reverse-input-activate (&optional arg)
  "Activate reverse-im input method.
With arg, activate reverse-im input method if and only if arg is
positive.

While this input method is active, the variable
`input-method-function' is bound to the function
`reverse-input-method'."
  (if (and arg
           (< (prefix-numeric-value arg) 0))
      (unwind-protect
          (progn
            (quail-hide-guidance)
            (quail-delete-overlays)
            (setq describe-current-input-method-function nil))
        (kill-local-variable 'input-method-function))
    (setq inactivate-current-input-method-function 'reverse-input-inactivate)
    (setq describe-current-input-method-function 'reversr-input-help)
    (quail-delete-overlays)
    (if (eq (selected-window) (minibuffer-window))
        (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer))
    (set (make-local-variable 'input-method-function)
         'reverse-input-method)))

(defun reverse-input-inactivate ()
  "Inactivate reverse-im input method."
  (interactive)
  (reverse-input-activate -1))

(defun reverse-input-help ()
  (interactive)
  (with-output-to-temp-buffer "*Help*"
    (princ "Inserts text in reverse order.")))

(provide 'reverse-im)

(register-input-method "reverse-im" "UTF-8" 'reverse-input-activate "<<"
                       "Input method for \"typing characters in reverse\".")

You could save it in, for example, ~/.emacs.d/reverse-im/reverse-im.el and then add these lines to .emacs

(add-to-list 'load-path (expand-file-name "~/.emacs.d/reverse-im/"))
(require 'reverse-im)

Then use KeySnail Firefox plugin to call emacs when you need to edit text (you'd need to set your text editor in .bashrc or whatever you use to store your shell variables to emacsclient and use K2Emacs plugin of KeySnail, or modify your .keysnail.js to invoke Emacs when you need that.

There's similar plugin for Vim called Vimperator, but I didn't use it.