MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Plugins (https://www.mobileread.com/forums/forumdisplay.php?f=268)
-   -   Plugin to insert text at current cursor position (https://www.mobileread.com/forums/showthread.php?t=339023)

MandyBrigwell 04-24-2021 03:25 PM

Plugin to insert text at current cursor position
 
Is it possible to simply insert text at the current cursor position?

I'd like to insert the current date and time and a few other bits and pieces, essentially using Sigil as a fancy journal system. I can handle formatting the inserted text myself, but if someone can clue me in as to how I can simply insert 'Hello World!' at the cursor position, I'd be very grateful.

I have tried with the documentation, but I'm struggling somewhat… the learning curve is a little steep, and although I've downloaded a number of example plug-ins for tips, I've come up short.

DiapDealer 04-24-2021 03:32 PM

The Sigil plugin framework doesn't currently provide any way to make the current cursor position available to third-party plugins.

MandyBrigwell 04-24-2021 03:55 PM

Well, that's that idea scuppered, then! Thank you for your help, though.

KevinH 04-24-2021 05:14 PM

Simply have the plugin copy whatever you want to insert at the cursor to your system clipboard and once there you can paste it at the cursor or anywhere else.

Should work.

Doitsu 04-24-2021 05:47 PM

Quote:

Originally Posted by MandyBrigwell (Post 4115874)
I'd like to insert the current date and time and a few other bits and pieces, essentially using Sigil as a fancy journal system.

As KevinH has already suggested, having the plugin copy text to the clipboard would be the easiest solution and takes only 3 lines of code. Here's a proof of concept plugin:

Code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from datetime import datetime
from PyQt5.Qt import QApplication

# main routine   
def run(bk):
    app = QApplication(sys.argv)
    date_time = str(datetime.now())
    app.clipboard().setText(date_time)
    print('{} copied to the clipboard.'.format(date_time))
    return 0

def main():
    print('I reached main when I should not have\n')
    return -1

if __name__ == "__main__":
    sys.exit(main())


MandyBrigwell 04-25-2021 08:33 AM

Oh, thank you—that's great. I'll have a play around.

Thanks!

DiapDealer 04-25-2021 09:09 AM

Sorry. I assumed the insertion would need to be done by the plugin.

Another possibility that occurs to me (that would allow a plugin to do the actual insertion) would be using a clip to insert something in the html that is (or includes) an extremely unique placeholder; which can then be replaced by a plugin with a simple text replacement.

fxp33 08-16-2021 06:32 AM

Could we have a plugin that paste the html from the clipboard at the cursor position ?

(really useful when pasting theater and poetry: keeping the li or br are a necessity. I really miss that option from the first versions of Sigil)

Turtle91 08-16-2021 08:39 AM

If it is already in the clipboard, then why doesn’t ctrl-v work??

KevinH 08-16-2021 09:12 AM

Quote:

Originally Posted by fxp33 (Post 4146806)
Could we have a plugin that paste the html from the clipboard at the cursor position ?

(really useful when pasting theater and poetry: keeping the li or br are a necessity. I really miss that option from the first versions of Sigil)

But that is the default for pasting in CodeView. I copy and paste snippets of html code all of the time.

Where are you trying to paste your html and how exactly as you trying to paste it (what menu or keyboard shortcut). Where did you copy your html from?

If you copy it from a different app, that app may not be properly copying the text to your clipboard as it may try to interpret the html as rich text.

DiapDealer 08-16-2021 10:05 AM

I think they're talking about the option you got to paste rich text or plain text when pasting into Book View from something like Word. Like you now do when pasting into PageEdit from Word.

KevinH 08-16-2021 10:33 AM

So instead of converting the Word doc to html using one of the existing plugins and importing it they instead want to copy the Word screen and somehow convert just it into html and paste that into Sigil?

As you said, they can paste it into PageEdit and get some semblance of html back, but using a Word import plugin or convert their doc into html and load that and use copy and paste.

DiapDealer 08-16-2021 11:59 AM

Oh, I agree. For quick snippets of rich text in the clipboard, there's PageEdit (which was intended to replace Book View functionality anyway), for full documents, there's conversion and/or plugins.

Turtle91 08-16-2021 01:58 PM

The only “paste” plug-in function I could see me needing, would be the ability to keep the last x number of items in the clipboard and then choose which one I wanted to paste.

I regularly have 2-3 different items that I wish I could switch between when copy/pasting as I’m going through a book.

DiapDealer 08-16-2021 02:10 PM

Quote:

Originally Posted by Turtle91 (Post 4146920)
The only “paste” plug-in function I could see me needing, would be the ability to keep the last x number of items in the clipboard and then choose which one I wanted to paste.

I regularly have 2-3 different items that I wish I could switch between when copy/pasting as I’m going through a book.

That sounds an awful lot like the built-in clipboard history feature. :blink:

Turtle91 08-16-2021 03:23 PM

Quote:

Originally Posted by DiapDealer (Post 4146921)
That sounds an awful lot like the built-in clipboard history feature. :blink:




Wait....Wut??? :eek:


Please don't tell me I missed another function that Sigil does so well already.....




:rofl:

DNSB 08-16-2021 10:20 PM

On a Windows computer, pressing windows-V give you a history to paste from. This works in Sigil and most other programs.

KevinH 08-16-2021 10:32 PM

Or as DiapDealer said use Sigil's Edit/Paste From Clipboard History... menu item or key shortcut on any platform. You can highlight any clipboard history item to paste or use the delete key to remove it from the history.

DiapDealer 08-16-2021 10:50 PM

It also saves the history between Sigil sessions (the preference setting determines how many are saved between sessions)

fxp33 09-04-2021 07:25 AM

Pasting
 
Hi all,

Maybe far from the original post title, but as the conversation was on the pasting topic I continue here.

The process is:
  1. copy from a web page
  2. go to sigil
  3. ctrl+v
  4. choose plain text
  5. or choose html

Here is what I meant... in images: https://www.youtube.com/watch?v=NAXWuLJN3lI

Hope this helps
Cheers

KevinH 09-04-2021 08:53 AM

Just use PageEdit with Sigil. It will do exactly what you want.

Quote:

Originally Posted by fxp33 (Post 4151602)
Hi all,

Maybe far from the original post title, but as the conversation was on the pasting topic I continue here.

The process is:
  1. copy from a web page
  2. go to sigil
  3. ctrl+v
  4. choose plain text
  5. or choose html

Here is what I meant... in images: https://www.youtube.com/watch?v=NAXWuLJN3lI

Hope this helps
Cheers


fxp33 09-04-2021 01:21 PM

Quote:

Originally Posted by KevinH (Post 4151617)
Just use PageEdit with Sigil. It will do exactly what you want.

Well, using a 160Mo external editor was not my idea of a simple pasting option 😉

I hoped more for an addition of ten lines of code:

javascript
Code:

document.addEventListener('paste', function (evt) {
  document.getElementById("plain").textContent = evt.clipboardData.getData('text/plain');
  document.getElementById("html").textContent = evt.clipboardData.getData('text/html');
}

Python gtk
Code:

#!/usr/bin/env python
import gtk
print (gtk.Clipboard().wait_for_contents('text/html')).data


qt
Code:

void DropArea::paste()
{
    const QClipboard *clipboard = QApplication::clipboard();
    const QMimeData *mimeData = clipboard->mimeData();

    if (mimeData->hasImage()) {
        setPixmap(qvariant_cast<QPixmap>(mimeData->imageData()));
    } else if (mimeData->hasHtml()) {
        setText(mimeData->html());
        setTextFormat(Qt::RichText);
    } else if (mimeData->hasText()) {
        setText(mimeData->text());
        setTextFormat(Qt::PlainText);
    } else {
        setText(tr("Cannot display data"));
    }


KevinH 09-04-2021 02:00 PM

Huh? That code exists in PageEdit and it can easily be launched and run. It is meant to be integrated into Sigil which has an easy way to launch it.

Sigil's CodeView editor is not a browser window and always pastes html/text as its base property since it is an xhtml code editor where copy and paste of code snippets are always done. Almost all copies made in CodeView typically includes html tags or partial html as text. You can not paste into a CodeView formatted rich text or images. You are confusing the target (a pure text code editor vs a browser like dom editor (Old BookView and PageEdit).

PageEdit is exactly like old Sigil's BookView and pasting formatted text into the PageEdit browser window is handled as you would expect.

Doitsu 09-04-2021 02:15 PM

1 Attachment(s)
@fxp33

If the feature is that important to you, why not use a plugin? The following code should do the trick:

Spoiler:
Code:

# -*- coding: utf-8 -*-
import sys
from PyQt5.Qt import QApplication

def run(bk):
    app = QApplication(sys.argv)
    clipboard = QApplication.clipboard()
    mime_data = clipboard.mimeData()
   
    if mime_data.hasHtml():
        app.clipboard().setText(mime_data.html())

    return 0

def main():
  print ("I reached main when I should not have\n")
  return -1

if __name__ == "__main__":
  sys.exit(main())



Just assign it one of the 10 plugin shortcuts and a corresponding keyboard shortcut, and you're good to go.

However, you might want to consider using PageEdit instead, because it'll significantly clean up the messy HTML code that some apps will copy to the clipboard.

fxp33 09-05-2021 12:09 PM

Quote:

Originally Posted by Doitsu (Post 4151675)
@fxp33
Just assign it one of the 10 plugin shortcuts and a corresponding keyboard shortcut, and you're good to go.

Hi Doitsu,

Yep, the plugin was my intention... and this is precisely why I was hoping I could get a hint on "how to insert text at cursor position" in this thread. I opened a lot of plugins to try and understand how to do that... 😉

Thanks a lot for packaging and rewriting the code into a plugin... And of course, I will indeed, check with pageEdit 👍

Best regards
François

fxp33 09-05-2021 12:37 PM

Quote:

Originally Posted by KevinH (Post 4151672)
Huh? That code exists in PageEdit and it can easily be launched and run. It is meant to be integrated into Sigil which has an easy way to launch it.

PageEdit is exactly like old Sigil's BookView and pasting formatted text into the PageEdit browser window is handled as you would expect.

Yes Kevin, I see that I can have:
  • raw text: with a paste in Sigil
  • original html: with all the code and classes (PageEdit ctrl+v)
  • structured text: linefeed are kept with p and br (PageEdit, Edit menu, paste, select "no")

What I can't figure out is why all the buttons from pageEdit have been added to Sigil (bald, italic, images, special caracters and even cut, paste) except this pasting option. PageEdit seems therefor very redundant to Sigil, and I find it to be a very big "plugin" for "just pasting".

But as you and Doitsu mentioned: it does not just paste, it cleans the code too when choosing "plain text" by adding "p" and "br". Which I understand is not just 10 lines of code.

Thanks for your reply, this helped me try new things with pageEdit. 👍
Regards
François

KevinH 09-05-2021 12:50 PM

PageEdit is not a plugin. It is separate editing program app that allows limited WYSIWYG editing similar to what the old BookView did in some ways. It can be easily launched from within Sigil by setting PageEdit as the External XHTML editor in Sigil's Preferences.


All times are GMT -4. The time now is 08:32 PM.

Powered by: vBulletin
Copyright ©2000 - 3.8.5, Jelsoft Enterprises Ltd.
MobileRead.com is a privately owned, operated and funded community.