Skip to content

Instantly share code, notes, and snippets.

@CuboidRaptor
Last active July 13, 2023 02:55
Show Gist options
  • Select an option

  • Save CuboidRaptor/75d738dcd8140b7ec294aa860fc4f225 to your computer and use it in GitHub Desktop.

Select an option

Save CuboidRaptor/75d738dcd8140b7ec294aa860fc4f225 to your computer and use it in GitHub Desktop.
Auto wrap.
from Npp import notepad, editor # imports...?
from Npp import NOTIFICATION, MENUCOMMAND
from Npp import SCINTILLANOTIFICATION as SCN
# This plugin auto-adjusts Notepad++ word wrap for txt/new files.
import os
import re
# constants
wrapfiles = [
"txt"
]
customwrap = re.compile(r"((//)|(#))? ?&(\{.*\})*\{wrap (?P<wrap>\d)\}(\{.*\})*")
newf = re.compile(r"new \d+")
cwrapped = None
newfwrapped = True
def check_first_line(*args, **kwargs):
global customwrap, cwrapped
lineone = editor.getLine(0) # custom wrap
matched = customwrap.match(lineone)
if matched:
matched = int(matched.group("wrap"))
# set wrap
if matched != None:
editor.setWrapMode(matched)
cwrapped = 1
return True
else:
if cwrapped == 1:
cwrapped = 0
buffer_active()
return False
def buffer_active(*args, **kwargs):
global wrapfiles, customwrap, newfwrapped, newf
cwrapped = None # idk custom wrapped
# get file extension
fileExt = os.path.basename(cfname := notepad.getCurrentFilename()).split(".")
if len(fileExt) > 1:
fileExt = fileExt[~0]
else:
fileExt = ""
# set wrap
if (newfwrapped) and (newf.match(cfname)):
editor.setWrapMode(1) # new files are wrapped
elif fileExt in wrapfiles:
editor.setWrapMode(1)
else:
editor.setWrapMode(0)
if check_first_line():
pass
notepad.clearCallbacks(buffer_active)
notepad.clearCallbacks(check_first_line)
editor.callback(check_first_line, [SCN.UPDATEUI, SCN.MODIFIED])
notepad.callback(buffer_active, [NOTIFICATION.BUFFERACTIVATED, NOTIFICATION.FILEOPENED]) # callbackkkkkk kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
buffer_active() # check first text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment