Community
    • Login

    Force Tab, One space Indents/UnIndents

    Scheduled Pinned Locked Moved General Discussion
    4 Posts 2 Posters 434 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.
    • W
      westyles
      last edited by PeterJones

      For PythonScript plugins.
      I moved it from issue because it won’t be added to npp functionality, but it may be useful:
      https://github.com/notepad-plus-plus/notepad-plus-plus/issues/15687
      and
      https://github.com/notepad-plus-plus/notepad-plus-plus/issues/15676

      # -*- coding: utf-8 -*-
      """
         Force Input Tab characters: "\t"
      
         Author:   westlife | ru-board.com
         Date:     2024-10-06
      
         Script for PythonScript plugin
      
         It is required to bind "Force_Input_Tab.py" to "Tab" key (Change default SCI_TAB's hotkeys to "Ctrl + Alt + Shift + Tab")
         To be able to input Tab characters in specified files, except inside the Indents,
         if the file is set to Indent using: Space character(s) (which converts Tab characters to Spaces everywhere)
         Works with multi selection and selection rectangular
      
      """
      
      ##### all imports added to startup.py, and: from Force_Input_Tab import *
      #import re  # regex
      
      # import all from Npp (editor, notepad, ...)
      #from Npp import *
      
      
      def Force_Input_Tab():
          # If Tabs are used, Input the Tab character (as default according to the indentation settings).
          if editor.getUseTabs(): editor.tab(); return
      
          # If there is a selection and not Rectangle
          # Add Indent the lines (as the default when selecting lines according to the indentation settings)
          if not editor.getSelectionEmpty() and not editor.selectionIsRectangle(): editor.tab(); return
      
          # Next, change the input to Tab characters, except inside the Indents.
          # Only for this extensions:
          if not re.fullmatch(r".*\.(txt|text)", notepad.getCurrentFilename(), re.I): editor.tab(); return
      
          _pos = editor.getSelectionStart()
      
          _colCurr = editor.getColumn(_pos)                                   # number of columns in the line up to the current caret position
          _colInd  = editor.getLineIndentation(editor.lineFromPosition(_pos)) # number of columns that a line is indented (line indentation size)
      
          # If not inside the Indents, input Tab character "\t":
          if _colCurr > _colInd:
      
              editor.setUseTabs(True)
      
              if 0 != editor.getSelectionMode(): editor.setSelectionMode(0) # <-- A must when selection rectangular! And has no effect with other selections.
              editor.tab()
      
              editor.setUseTabs(False)
          else:
              editor.tab()
          return
      
      if __name__ == '__main__':
          Force_Input_Tab()
      
      # -*- coding: utf-8 -*-
      """
         One Space character indents for multiple lines.
      
         Author:   westlife | ru-board.com
         Date:     2024-10-08
      
         Script for PythonScript plugin.
      
         It is required to bind "One_Space_Indents.py" to "Alt + SpaceBar"
         Work with multi-select with 1 selecting with multiple lines, except rectangular selections.
      
         If you hold down the hotkey a warning pop-up window appears from plugin.
         When for file is configured: "Indent using: Tabs characters"!
         Because of editor.setUseTabs(True) it already doesn't have time to execute.
         before the second command from npp is sent to execute this script. If you hold down the hotkey.
         There are no problems with repeated manual presses.
      
      """
      
      ##### all imports added to startup.py
      # import all from Npp (editor, notepad, ...)
      #from Npp import *
      
      def One_Space_Indents():
          if editor.getSelectionEmpty(): return     # if nothing is selected
          if editor.selectionIsRectangle(): return  # if rectangular selection
      
          start_line, end_line = editor.getUserLineSelection()
          if start_line == end_line: return         # if selection is in a single line
      
          _useTabs  = editor.getUseTabs()           # Indent using: Tabs characters for this file?
      
          if _useTabs: editor.setUseTabs(False)     # Set Indent using: Space characters
      
          editor.setIndent(1) # Indent Size: 1 (character); Only for next inputs
          editor.tab()        # adding indent with 1 Space character
          editor.setIndent(0) # Indent Size Auto (default)
      
          if _useTabs: editor.setUseTabs(True)
          return
      
      if __name__ == '__main__':
          One_Space_Indents()
      
      # -*- coding: utf-8 -*-
      """
         One Space character UnIndents for multiple lines.
      
         Author:   westlife | ru-board.com
         Date:     2024-10-12
      
         Script for PythonScript plugin.
      
         It is required to bind "One_Space_UnIndents.py" to "Shift + SpaceBar".
         Work with multi-select with 1 selecting with multiple lines, except rectangular selections.
      
         If you hold down the hotkey a warning pop-up window appears from plugin.
         When for file is configured: "Indent using: Tabs characters"!
         Because of editor.setUseTabs(True) it already doesn't have time to execute.
         before the second command from npp is sent to execute this script. If you hold down the hotkey.
         There are no problems with repeated manual presses.
      
         To assign to "Shift + SpaceBar", you must first assign to "Shift + Alt + SpaceBar".
         Then in the shortcuts.xml file fix Alt="yes" to Alt="no" (SpaceBar -> Key="32"):
         <PluginCommand moduleName="PythonScript.dll" internalID="Your ID" Ctrl="no" Alt="no" Shift="yes" Key="32" />
      
      """
      
      ##### all imports added to startup.py
      # import all from Npp (editor, notepad, ...)
      #from Npp import *
      
      def One_Space_UnIndents():
          if editor.getSelectionEmpty(): return     # if nothing is selected
          if editor.selectionIsRectangle(): return  # if rectangular selection
      
          start_line, end_line = editor.getUserLineSelection()
          if start_line == end_line: return         # if selection is in a single line
      
          _useTabs  = editor.getUseTabs()           # Indent using: Tabs characters for this file?
      
          if _useTabs: editor.setUseTabs(False)     # Set Indent using: Space characters
      
          editor.setIndent(1) # Indent Size: 1 (character); Only for next inputs
          editor.backTab()    # removing indent by 1 Space character
          editor.setIndent(0) # Indent Size Auto (default)
      
          if _useTabs: editor.setUseTabs(True)
          return
      
      if __name__ == '__main__':
          One_Space_UnIndents()
      

      Example of use:
      notepad++_Example.gif

      W 2 Replies Last reply Reply Quote 1
      • W
        westyles @westyles
        last edited by westyles

        Moved to another post

        Alan KilbornA 1 Reply Last reply Reply Quote 0
        • Alan KilbornA
          Alan Kilborn @westyles
          last edited by

          @westyles said in Force Tab, One space Indents/UnIndents:

          Bonus:

          It’s probably more reasonable to post unrelated things in different topics.
          It makes continuing discussion easier.
          (Sure, these things are related because they are scripts, but that’s their only relation.)

          1 Reply Last reply Reply Quote 0
          • Alan KilbornA Alan Kilborn referenced this topic on
          • W
            westyles @westyles
            last edited by

            Replace with this in the code in both One_Space* so that it doesn’t block entering spaces without multiple lines selected, while holding SHIFT or Alt.

                if editor.getSelectionEmpty():  # If nothing is selected, add a space
                    editor.addText(" ")
                    return
                if editor.selectionIsRectangle(): return  # if rectangular selection
            
                start_line, end_line = editor.getUserLineSelection()
                if start_line == end_line:
                    editor.replaceSel(" ") # If the selection is on a single line, replace it with a space.
                    return
            
            1 Reply Last reply Reply Quote 1
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors