ftblog

:: widerstand zwecklos ::
email jabber gpgkey
hackergotchi

May 17, 2007

exec_on_xclip

Filed under: computer -- 17:57

Vor kurzem hat mich nion auf die Idee gebracht, die zsh etwas mit dem Inhalt der
X11 Selection machen zu lassen. Heraus gekommen ist dabei ein widget für zle,
welches sich, glaube ich, an so ziemlich jeden Geschmack anpassen lässt.

So braucht man nur noch eine Taste(nkombination) drücken, und schon passiert
etwas mit dem String der in der Selection steht. Was? Das kann man nach Belieben
einstellen.

Zuholen gibt es den Code in meiner zsh Konfiguration (hier im tarball).

NAME
      exec_on_xclip - call commands based on the current X11 selection.

DESCRIPTION
      exec_on_xclip is a zle widget, that looks at the current state
      of the X11 selection buffer, looks up an appropriate action for
      it and calls the given command with the selection as the
      argument for the command.

      That way, you can start browsers on selected URIs, for example.

INVOCATION
      exec_on_xclip looks at the current commandline buffer, to
      determine, if it should do something special.

      Recognized strings:
        'help'
            Displays this help text.

        'conf'
            Dumps the current exec_on_xclip configuration.

        If a commandline buffer starting with help or conf ends
        with '|', the text that would be printed, will be
        displayed in the configured pager (less -Mr).

        Additionally, if exec_on_xclip sees a stagename in the
        commandline buffer, it will lookup an action exclusively from
        that stage instead of checking all stages.

SETUP
      To get exec_on_xclip running a few simple steps are required:

        1. copy the function file into your $fpath.
        2. call: autoload -U exec_on_xclip
        3. make it known to zle: zle -N exec_on_xclip
        4. bind it to a key:
            bindkey '^xb'   exec_on_xclip
            bindkey '^x^b'  exec_on_xclip

CONFIGURATION
      exec_on_xclip is configured via zsh's zstyle system.
      It uses the obvious namespace ':exec_on_xclip:*'.

      Global styles:
        For some styles, it does not make sense to define them
        in a fine-grained context, as they are only looked up
        in ':exec_on_xclip:*'; these are:

        + xclip
            The X11 selection tool to use.
            (default: xclip)

        + options
            Options for the X11 selection tool to print the selection
            buffer to stdout. (default: -o)

        + pager
            This help text, as well as the configuration dumping
            function of exec_on_xclip may output more text than
            fits on your screen, to improve usability, the texts
            may be viewed in a pager (see above). This style sets up
            the pager that will be used. (default: less)

        + stages
            Stages are used to group applications together (for
            example apps that open a new window in X11 and others,
            that work directly in the current terminal window).

            This style takes a list of stage names, that will
            be gone through one after another, until a matching
            application was found. The names have a special format,
            that must be followed:
              "number:name"

            The number is used to define the order the stages are used.
            "0:foo" is used earlier then "1:bar".

            Stage names, starting with x11 have a special effect:
            The command, that will be called is put into the background
            and is disowned.

            The default value for stages is: '0:x11' '1:nox'.

        + contexts
            The contexts, exec_on_xclip are completely user definable.
            Its value is an associative array, that maps names to
            patterns. These patterns are used to match the current
            X11 selection buffer. If a pattern matches the buffer,
            the context is set to the name that corresponds to the
            pattern (minus the priority expression).

            The format of a name pattern pair is as follows:
              "number:name"   "pattern"

            The "number:" part is what I refered to as "priority
            expression" above. Is serves the same reason as the
            similar part in stage names (see above).

            The contexts style is empty be default.

      Other styles:
        These styles control the way exec_on_xclip acts in various
        contexts.

        + app
            This is (beside contexts) the most important style, that
            you must set. It configures the application, that should
            be used in a given context.

            The app style is simply a string, that must contain the
            substring '_eoxc_', because that is the position
            where the X11 selection is filled in.

            No applications are defined by default.

        + clearterm
            If this boolean style is set in the current context, the
            terminal screen will be cleared by zle after the called
            application exits. (default: false)

        + operate
            This boolean style is an addition to the app style.
            If set to 'false', it let's you disable complete
            contexts (useful to disable complete stages, if you
            temporarily want to disable apps, that open new windows,
            for example). (default: true)

EXAMPLES
      To give you an idea, of how the configuration described above
      may be put together, here is a working example:

    zstyle ':exec_on_xclip:*'             contexts                      \
                                              "0:uri_http"  'http://*'  \
                                              "1:file_gz"   '(/|~)*.gz' \
                                              "2:file"      '(/|~)*'

    zstyle ':exec_on_xclip:*'             stages '0:nox' '1:x11'
    zstyle ':exec_on_xclip:*'             xclip "xclip"
    zstyle ':exec_on_xclip:*'             options "-o"
    zstyle ':exec_on_xclip:*'             pager "less -Mr"
    zstyle ':exec_on_xclip:x11:*'         operate false
    zstyle ':exec_on_xclip:nox:*'         clearterm true
  if   [[ -x $(which opera) ]] ; then
    zstyle ':exec_on_xclip:x11:uri_http'  app 'opera -remote openURL(_eoxc_)'
  elif [[ -x $(which firefox) ]] ; then
    zstyle ':exec_on_xclip:x11:uri_http'  app 'firefox -new-tab _eoxc_'
  else
    zstyle ':exec_on_xclip:x11:uri_http'  app false
  fi
    zstyle ':exec_on_xclip:*:uri_http'    app "${BROWSER:-w3m} _eoxc_"
    zstyle ':exec_on_xclip:*:file_gz'     app "zless _eoxc_"
    zstyle ':exec_on_xclip:*:file'        app "less _eoxc_"


BUGS
      Redrawing multi-line prompts may be broken.
      I do not think it is worth working around that.
      If you feel disturbed by that, set clearterm.

AUTHOR
      Frank Terbeck <ft@bewatermyfriend.org>
      (c) 2007
      Permission to use, copy, modify, and distribute this software
      for any purpose with or without fee is hereby granted,
      provided that the above copyright notice and this permission
      notice appear in all copies.

      THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
      WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
      WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
      THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
      CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
      FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN AN ACTION
      OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Powered by zblog
valid css | valid xhtml | utf-8 encoded | best viewed with anybrowser