← Back to team overview

zim-wiki team mailing list archive

Re: Wrap command tool

 

On Sat, Apr 28, 2012 at 4:29 AM, kwoodham@xxxxxxxxx <kwoodham@xxxxxxxxx> wrote:
> Hi all,
>
> I know just enough python to get strangled by it - but here goes...
>
> I've been using a Autokey script under Linux for a while - decided I
> would try my hand at porting it to a Zim Custom tool.
>
> I like to use the verbatim format for some text references - just to
> set it off from my personal text.  So in Autokey it's done by copying
> the highlighted text into a string, performing the wrap on the string
> (which may span multiple lines) and pasting the wrapped text back
> using keyboard.send_key from the Autokey library (overwriting the
> unwrapped text).  Works well.
>
> I've managed to do the same thing in a wrap.py script that I can run
> on the python command line, giving it a long string as an argument.
> For instance the following wraps the string (whole words) at 20
> characters or less (I use 100 in Zim text window):
>
> import textwrap
> import sys
> text = sys.argv[1]
> text_lines =text.splitlines()
> for line in text_lines:
>    wrap_line = textwrap.fill(line,20)
>    print wrap_line+'\n'
>
> And can run it as:
>
> $python wrap.py "This is a very long line that I need to have over 100
> characters in in order to see it wrap, I think we are coming close to
> 100 characters"
> This is a very long
> line that I need to
> have over 100
> characters in in
> order to see it
> wrap, I think we are
> coming close to 100
> characters
>
> The question that I have is that I'm no sure what mods are necessary
> to run this as a Zim custom tool: I'm guessing something line a Wrap
> tool mapped to a command like "wrap.py %t" - but how do I (a) delete
> the existing (unwrapped) text and (b) get the wrapped text back into
> the Zim window?

First step is to read the text from the file and write it back. If you
use "wrap.py %f" you get a file name as argument and you can modify it
(see python introduction for reading & writing files). After the
script is done, zim will reload the page from this file.

However, if I read your mail correctly, you want to format only the
selected text, leaving other text alone. This is not really possible
at the moment. Would need to add a new code to the custom tool engine
in order to make that possible.

Regards,

Jaap


References