🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.readers
Article #265142

Re: [dialog] Can I set dialog to edit in Notepad++

#265142
From: Bernd Rose
Date: Sun, 15 Aug 2021 12:23
207 lines
9036 bytes
Am Wed, 11th Aug 2021 17:00:23 +0200, bill wrote:

[Use external editor (Notepad++) for editing follow-ups]
> On Wed, 11 Aug 2021 06:05:21 +0200, Bernd Rose wrote:
>> It is possible per script [...]. But the whole process would 
>> be somewhat difficult to implement and probably not seem seamless
>> enough for a good usage experience.
> 
> I'm not a script writer so unless the script exists (or a similar modifiable
> script exists)

All approaches we discussed until now, are either not to your liking
or, IMHO, rather dangerous. - But you got me curious...   ;-)

Although the script capabilities of 40tude Dialog are extraordinary, they
still have their limitations. Often it is possible to circumvent problems
by calling system function. In your case, this would rather complicate
things, though.

Of all Event scripts, only OnBeforeSendingMessage deals to some degree with
the composition window. Actually, it rather deals with a text message, right
after the Composition window is disposed off, but before this message is 
sent. This makes it possible, to add some checks and changes right before 
sending. But everything altered will be either sent without 40tude Dialog
doing additional checks; or it will be discarded and +/- lost.

The same is true for Message scripts. All objects and their messages, which
are accessible this way, target (only!) the main Dialog window. The Compose
window is out of the scope of Messages scripts.

Even Custom scripts can only be assigned hotkeys of the main Dialog window,
and /not/ be executed from Composition window.

So, everything dealing with calling an external editor has to be initiated
from the main 40tude Dialog Window. This also means, it is not possible 
(without diving in the innermost system functions for process and window
enumeration and the like) to open the follow-up inside the Composition 
window, save the text from there, edit it externally and come back to that
Composition window for reloading the altered text and final processing.

The way I chose is:
1) Load currently selected message (cf. Article pane) to memory buffer
2) Add quote chars in front of each line of the buffer. (Usually, this
   step would be done by 40tude Dialog while loading the text into the 
   Composition window. But, unfortunately, we are about to circumvent 
   this step right now.)
3) Save the buffer to disk
4) Open Notepad++ (or another editor) with the saved temporary file
5) Block 40tude Dialog with a message box
6) Wait for the message box to be closed after the file has been edited,
   externally, and saved back to disk.
7) Fire up the 40tude Composition window (as follow-up to the still current
   message)
8) Reload the file from disk into the Composition window, replacing the
   whole text, save the first two lines (intro plus one empty line) by
   stuffing the keyboard buffer. (= The only way without Windows system
   functions to access the Composition window per script.)

Now the text is ready for final editing (inserting wrap override characters,
adding a suitable signature, and the like). Afterwards, the message can be 
sent (or aborted), normally.

The script itself can be created as Custom script (to be assigned to a
keyboard shortcut) or as a Message script. Only few alterations have to be
done, to go from one to the other. Both methods performed well during my
tests. It should be clear, though, that the script performs only the most
basic error checks. Therefore, it may fail at any time. (More refined
error testing would preoccupy far more hours, then I care to spend on this 
matter.) - You have been warned!   ;-)

And now for the script: Following is the Message script variant. I chose
to assign it to a mouse middle button double click inside the Article pane.
This way, it is clear, that it targets the current article.

To load it, choose the "Message scripts" tab from 40tude Dialog Scripting
Editor, create a new script and select:

Control: BODYEDIT
Message: WM_MBUTTONDBLCLK

Then replace the script stub with the following code. Adjust the entries
in the "const" section at the top of the script to your preferences and
system settings. Then save and compile the script, and you are ready to go.

Have fun!
Bernd

--------------------------------------------------------------------------

// Script for opening the current message in an external editor 
// and getting the edited text back as Follow-Up into the 40tude 
// Composition Window
//
// Adjust settings in the 'const' section on top of the script
//
// Author: Bernd Rose | Version: 2021-08-15 | Public Domain

program BODYEDIT_WM_MBUTTONDBLCLK;
uses Forms, StdCtrls, Textfile;

const
  // Edit settings here:
  ExtEditor  = 'C:\Program Files\Notepad++\Notepad++.exe';
  TmpFile    = 'C:\Temp\dialog.txt';
  bSkipIntro = True;
  QuoteChrs  = '>|';
  QuoteChr   = '>';
  // End of editable section

procedure OnWMessage(var Msg:TMessage;var handled:boolean);

var
  TmpForm: TForm;
  TmpMemo: TMemo;
  TextF:   Textfile;
  i:       Integer;

begin
  TmpForm            := TForm.Create(Nil);
  TmpMemo            := TMemo.Create(TmpForm);
  TmpMemo.parent     := TmpForm;
  TmpMemo.WordWrap   := True;
  TmpMemo.Scrollbars := ssBoth;

  lockdisplay;

  try
    TmpMemo.Clear;
    ADo('ArticlePane');
    ADo('SelectAll');
    Ado('Copy');
    TmpMemo.PasteFromClipboard;

    // Insert quotation char in the first position of every line
    begin
      i := 0;
      while i <= TmpMemo.Lines.count - 1 do
        begin
          // Check for empty lines (need special treatment)
          if TmpMemo.Lines[i] <> '' then
            if Pos(TmpMemo.Lines[i][1], QuoteChrs) > 0 then
              begin
                // Quote an already quoted line
                TmpMemo.Lines[i] := QuoteChr + TmpMemo.Lines[i];
              end
            else
              begin
                // New quotes are padded with a space character
                TmpMemo.Lines[i] := QuoteChr + ' ' + TmpMemo.Lines[i];
              end
          else
            // Append empty line, as well, to get correct line endings
            TmpMemo.Lines[i] := QuoteChr + TmpMemo.Lines[i];
          i := i + 1;
        end;
    end;

    AssignFile(TextF, TmpFile);
    Rewrite(TextF);
    TextWrite(TextF, TmpMemo.text);
    CloseFile(TextF);

    //PostKey( key: Word; Shift, Alt, Ctrl, Left, Right, Middle, Double,specialkey: Boolean);
    PostKey(36, false, false, true, false, false, false, false, false);

    unlockdisplay;

    FileExecute(0,ExtEditor,'open',TmpFile,'',1,0);
                              
    // Access to 40tude Dialog is suspended until return from external editor
    if Application.MessageBox('Hit <OK> when the external edit process is finished.', 
      'Waiting...', 65) = 1 then
      begin
        TmpMemo.Clear;
        TmpMemo.Lines.LoadFromFile(TmpFile);
        TmpMemo.SelectAll;
        TmpMemo.CopyToClipboard;

        ADoLater('FollowUpToUsenetMessage');

        if bSkipIntro then
          begin
            // Go to top, then 2 lines down and select message text to the end
            PostKey(36, false, false, true, false, false, false, false, false);
            PostKey(40, false, false, false, false, false, false, false, false);
            PostKey(40, false, false, false, false, false, false, false, false);
            PostKey(35, true, false, true, false, false, false, false, true);
          end
        else
          begin
            // Select all
            PostKey(65, false, false, true, false, false, false, false, false);
          end;

        // Paste Clipboard content (overwriting selection); then go to top
        PostKey(86, false, false, true, false, false, false, false, false);
        PostKey(36, false, false, true, false, false, false, false, false);
      end;

 finally
   TmpForm.free;
 end;

end;

begin
end.
Technical Details
Message-ID: <2cf4nyzcfoti.dlg@b.rose.tmpbox.news.arcor.de>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!apf1.newsdeef.eu!not-for-mail
References: <sevelu$1qrk$1@gioia.aioe.org> <1dtmjo6u9vzl1$.dlg@b.rose.tmpbox.news.arcor.de> <sf0olp$qvr$1@gioia.aioe.org>

Show Full Headers
Complete Article Headers
Message-ID: <2cf4nyzcfoti.dlg@b.rose.tmpbox.news.arcor.de>
Subject: Re: [dialog] Can I set dialog to edit in Notepad++
Date: Sun, 15 Aug 2021 14:23:41 +0200
From: Bernd Rose <b.rose.tmpbox@arcor.de>
References: <sevelu$1qrk$1@gioia.aioe.org> <1dtmjo6u9vzl1$.dlg@b.rose.tmpbox.news.arcor.de> <sf0olp$qvr$1@gioia.aioe.org>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!apf1.newsdeef.eu!not-for-mail
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Injection-Info: solani.org; logging-data="2154"; mail-complaints-to="abuse@news.solani.org"
User-Agent: 40tude_Dialog/2.0.15.41 (2787fb44.66.493)
Newsgroups: news.software.readers
Mime-Version: 1.0
X-User-Id: eJwNyskBwCAIBMCWzMKCloMc/ZcQf/MYin2WrkZTDifbmsRslVtEbCfy1kAGsWadbhbek6zpCFV6iN0J0LDs1GafBX1ypPz6rRpZ
Cancel-Lock: sha1:0tSrHXKKUbayORGJl/qDt19ObJQ=