• Login
Community
  • Login

Is there a way to center text like this?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 4 Posters 1.4k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H
    Hello313241
    last edited by Oct 26, 2020, 5:13 PM

    7Q6DmtA.jpg

    Tnx for your help!

    M 1 Reply Last reply Oct 26, 2020, 5:36 PM Reply Quote 0
    • M
      Michael Vincent @Hello313241
      last edited by Oct 26, 2020, 5:36 PM

      @Hello313241

      Not default. Notepad++ is a text editor with no concept of a right margin like a word processor. That said, you can add a guideline for a right column (i.e., “margin”) and with a scripting language, you can create a “justify” script to left, right or center justify text based on the right column you select (of a default one).

      Here is a “PerlScript ” example:

      #!perl
      
      use strict;
      use warnings;
      use Getopt::Long qw(:config no_ignore_case);
      use Pod::Usage;
      use Win32::Mechanize::NotepadPlusPlus ':main';
      
      my $justify = 'l';
      my $width   = editor->getEdgeColumn();
      if ( $width == 0 ) {
          $width = 80;
      }
      
      sub justify {
          $justify = $_[0];
          $width = $_[1] if ( $_[1] =~ /^\d+$/ );
      }
      GetOptions(
          'c|center:s' => \&justify,
          'l|left'     => \&justify,
          'r|right:s'  => \&justify,
          'help!'      => sub { pod2usage( -verbose => 1 ) },
          'man!'       => sub { pod2usage( -verbose => 2 ) }
      ) or pod2usage( -verbose => 0 );
      
      my $txt = editor->getSelText();
      if ( $txt eq "\0" or length($txt) < 1 ) {
          pod2usage( -verbose => 0, -message => "$0: select text to justify" );
      }
      
      my $eol = ( "\r\n", "\r", "\n" )[editor->getEOLMode()];
      my @lines = split /$eol/, editor->getSelText();
      
      editor->beginUndoAction();
      
      for my $line (@lines) {
          chomp $line;
          $line =~ s/^\s*//;
          $line =~ s/\s*$//;
      
          my $space = 0;
          if ( $justify eq "c" ) {
              $space = int( ( $width - length($line) ) / 2 );
          } elsif ( $justify eq "r" ) {
              $space = int(   $width - length($line) );
          }
      
          $space = 0 if ( $space < 0 );
          editor->replaceSel( " " x $space . "$line$eol" );
      }
      
      editor->endUndoAction();
      
      __END__
      
      =head1 NAME
      
      Justify - Justify text.
      
      =head1 SYNOPSIS
      
       justify [options]
      
      =head1 DESCRIPTION
      
      Make a selection in Notepad++ to justify text left, right or center.
      Width for center and right is determined by SCI_GETEDGECOLUMN.  If 0, 
      default 80 assumed.
      
      =head1 OPTIONS
      
       -c [width]   Center justify.  Optional width. [Default]
       -l           Left justify.
       -r [width]   Right justify.  Optional width.
      
       --help       Print Options and Arguments.
       --man        Print complete man page.
      
      =head1 LICENSE
      
      This software is released under the same terms as Perl itself.
      If you don't know what that means visit L<http://perl.com/>.
      
      =head1 AUTHOR
      
      Copyright (c) 2020 Michael Vincent
      
      L<http://www.VinsWorld.com>
      
      All rights reserved
      
      =cut
      
      1 Reply Last reply Reply Quote 3
      • G
        guy038
        last edited by guy038 Oct 27, 2020, 6:03 PM Oct 26, 2020, 8:25 PM

        Hello, @hello313241, @michael-vincent, and All,

        Seemingly, the center feature should take the zoom factor in account ! Indeed, for instance, if a text block is centered for default zoom, it will not be centered if you zoom in or out !

        So here is a regex method which needs 5 consecutive search /replacement, described below. Luckily, you just can save these S/R in a macro and even give it a shortcut to trigger it after selection of some text ;-)

        For instance, on my laptop, with default zoom ( Ctrl + Numpad / ) and using the monospaced font Consolas, I can write 140 character per line. As you’ll probably get an other number, simply insert the appropriate number of space characters in the Replace zone of the second S/R !

        So, to center a line(s) selection we need the different regex S/R, below :

        • The first S/R deletes any leading and trailing blank characters in each line of the selection

          • SEARCH ^\h+|\h+$

          • REPLACE Leave EMPTY

        • The second S/R appends 140 space chars at end of each line of the selection

          • SEARCH $

          • REPLACE A string of 140 SPACE characters

        • The third S/R deletes any range of space characters present after position 140 :

          • SEARCH (?-s)^.{140}\K\x20+

          • REPLACE Leave EMPTY

        • The fourth S/R, where the centering operation occurs, surrounds each effective text with half the number of space chars of each line of the selection :

          • SEARCH (?-s)^(.*[^ \r\n])(\x20+)\2\x20?$

          • REPLACE \2\1\2

        • Finally, the fifth S/R simply trims all the trailing space characters of each line of the selection :

          • SEARCH \h+$

          • REPLACE Leave EMPTY


        As promised, here is the macro, which runs, successively, these 5 regex S/R on each line of your selection :

                <Macro name="Center Selected Lines" Ctrl="no" Alt="no" Shift="no" Key="0">
                    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                    <Action type="3" message="1601" wParam="0" lParam="0" sParam="^\h+|\h+$" />
                    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                    <Action type="3" message="1602" wParam="0" lParam="0" sParam="" />
                    <Action type="3" message="1702" wParam="0" lParam="640" sParam="" />
                    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
        
                    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                    <Action type="3" message="1601" wParam="0" lParam="0" sParam="$" />
                    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                    <Action type="3" message="1602" wParam="0" lParam="0" sParam="                                                                                                                                            " />
                    <Action type="3" message="1702" wParam="0" lParam="640" sParam="" />
                    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
        
                    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                    <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?-s)^.{140}\K\x20+" />
                    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                    <Action type="3" message="1602" wParam="0" lParam="0" sParam="" />
                    <Action type="3" message="1702" wParam="0" lParam="640" sParam="" />
                    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
        
                    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                    <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?-s)^(.*[^ \r\n])(\x20+)\2\x20?$" />
                    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                    <Action type="3" message="1602" wParam="0" lParam="0" sParam="\2\1\2" />
                    <Action type="3" message="1702" wParam="0" lParam="640" sParam="" />
                    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
        
                    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                    <Action type="3" message="1601" wParam="0" lParam="0" sParam="\h+$" />
                    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                    <Action type="3" message="1602" wParam="0" lParam="0" sParam="" />
                    <Action type="3" message="1702" wParam="0" lParam="640" sParam="" />
                    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                </Macro>
        

        Remark : In the second block, note the line <Action type="3" message="1602" wParam="0" lParam="0" sParam="...................... />" which contains 140 space characters. Adapt the number of spaces as needed, depending of your screen configuration and of the N++ margin(s), font, and zoom factor used !

        • Close any N++ instance

        • Insert this new macro in the <Macros>...........</Macros> section of your active Shortcut.xml file

        • Restart Notepad++

        • Do a normal selection of some text and… enjoy !

        Best Regards,

        guy038

        1 Reply Last reply Reply Quote 1
        • Terry RT
          Terry R
          last edited by Oct 26, 2020, 10:50 PM

          @guy038 said in Is there a way to center text like this?:

          So here is a regex method which needs 5 consecutive search /replacement, described below. Luckily, you just can save these S/R in a macro and even give it a shortcut to trigger it after selection of some text ;-)

          I got to thinking and came up with this which appears to perform the same but in less steps and doesn’t need the use of the \K function.

          I used 80 characters as the width, but I think any number is okay (it has to be an even number). Steps are:

          1. Removal of leading and trailing spaces is recommended, I’ll borrow @guy038 regex.
            Find What:^\h+|\h+$
            replace With: leave this field empty

          2. Add half of the spaces to both start and end of line. One benefit is only having to type HALF the spaces required! ;-)
            Find What:^|$
            Replace With: type 40 spaces here, half of 80

          3. Find the middle 80 characters.
            Find What:^( +)(.{80}\1
            Replace with:\2

          4. Remove trailing spaces using the Blank Operations, Trim Trailing Spaces.

          A note, I had originally tried ^( +)(.{80}\1$, note the $, in effect forcing it to go to end of line marker, but that missed some lines, I didn’t do any further testing, just removed the $. As the + is greedy it will still attempt to capture the whole line anyways.

          Terry

          1 Reply Last reply Reply Quote 1
          • Terry RT
            Terry R
            last edited by Oct 26, 2020, 11:31 PM

            @Terry-R said in Is there a way to center text like this?:

            Removal of leading and trailing spaces is recommended

            Just figured I can remove existing leading and trailing spaces and ALSO insert the required “HALF the spaces” in the one step. So:
            Find What:^(\h+)?|(\h+)?$
            Replace With: 40 spaces here for a 80 character width

            Followed by step 3 above and then the final step 4 of removing trailing spaces.

            Terry

            1 Reply Last reply Reply Quote 1
            • G
              guy038
              last edited by guy038 Oct 27, 2020, 6:05 PM Oct 27, 2020, 2:54 PM

              Hi, @hello313241, @michael-vincent, @terry-r and All,

              Very, very clever solution Terry ! Indeed, no need for \K and just to type in half the screen size of spaces, in the Replace zone ;-))

              And I also improved the process as we do not need, any more, to trim the trailing blank chars at the end. In addition to the mandatory regex part .{80}, I placed, before, a look-ahead (?=(.+[^\x20\r\n])\x20+) which splits these 80 characters in two zones :

              • A first zone (.+[^\x20\r\n]) which must end with a non-blank and Non-EOL character, to rewrite in replacement ( \2 )

              • A second zone \x20+ of blank characters only, to ignore


              So we need two regex S/R, only ( Note that I follow my previous post with 140 characters for screen size )

              • The first S/R removes possible blank chars of a pure blank line OR replace any range, possibly null, of leading and trailing blank chars with 70 space characters ( 140 / 2 )

              SEARCH ^\h*$|(^\h*|\h*$)

              REPLACE ?1<followed with 70 spaces>

              • The second S/R replaces any full line of the selection, magically, with the range of ( 140 - L ) / 2 space chars, followed by the text to be centered, of size S :

              SEARCH (?-s)^(\x20*)(?=(.+[^\x20\r\n])\x20+).{140}\1\x20?

              REPLACE \2

              Remarks :

              • As the total of any line of the selection is 140 + S characters long, thus the length of the surrounded range \1 is S/2 characters

              • I had to add the final \x20? regex because, in case the size S is an odd number, this extra space char is also matched and deleted after replacement !

              • All lines of the selection must have a size inferior or equal to 140. Lines, with size over 140, are shifted by 140 / 2 space characters !


              So, the final macro is now simplified as :

                      <Macro name="Center Selected Lines" Ctrl="no" Alt="no" Shift="no" Key="0">
              
                          <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                          <Action type="3" message="1601" wParam="0" lParam="0" sParam="^\h*$|(^\h*|\h*$)" />
                          <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                          <Action type="3" message="1602" wParam="0" lParam="0" sParam="?1                                                                      " />
                          <Action type="3" message="1702" wParam="0" lParam="640" sParam="" />
                          <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
              
                          <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                          <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?-s)^(\x20*)(?=(.+[^\x20\r\n])\x20+).{140}\1\x20?" />
                          <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                          <Action type="3" message="1602" wParam="0" lParam="0" sParam="\2" />
                          <Action type="3" message="1702" wParam="0" lParam="640" sParam="" />
                          <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
              
                      </Macro>
              

              For instance, this initial text, where I used some sentences from the License.txt file and add some trailing blank chars :

                                                                                           This example works ONLY IF the TOTAL size of your SCREEN contains 140 characters
              				ADAPT to your CONFIGURATION, by adding HALF the APPROPRIATE number of SPACES, after "?1", in the REPLACEMENT zone of the 2ND regex S/R
                                                    
              ><
              ><
              ><
                                                                                Copyright (C)2016 Don HO <don.h@free.fr>                           
                             The licenses for most software are designed to take away your freedom to share and change it             
              1
                                                                                                 Copyright (C) 1989, 1991 Free Software Foundation, Inc.                                     
              ABCD
                                                 Everyone is permitted to copy and distribute verbatim copies            
              You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee
                                                 12345    12345    12345    12345    12345
                                   az									
              					                  1           
              To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights.
              															                   1234567890
                              				                         ><
              ><
              ><
              

              Would be modified and centered as below :

                                            This example works ONLY IF the TOTAL size of your SCREEN contains 140 characters
                 ADAPT to your CONFIGURATION, by adding HALF the APPROPRIATE number of SPACES, after "?1", in the REPLACEMENT zone of the 2ND regex S/R
              
                                                                                   ><
                                                                                   ><
                                                                                   ><
                                                                Copyright (C)2016 Don HO <don.h@free.fr>
                                      The licenses for most software are designed to take away your freedom to share and change it
                                                                                    1
                                                         Copyright (C) 1989, 1991 Free Software Foundation, Inc.
                                                                                  ABCD
                                                      Everyone is permitted to copy and distribute verbatim copies
              You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee
                                                                12345    12345    12345    12345    12345
                                                                                   az
                                                                                    1
                 To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights.
                                                                               1234567890
                                                                                   ><
                                                                                   ><
                                                                                   ><
              

              Best Regards,

              guy038

              1 Reply Last reply Reply Quote 1
              2 out of 6
              • First post
                2/6
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors