notepad - ghdrako/doc_snipets GitHub Wiki

Notepad++

  • NPPN_FILEOPENED notification allows detection immediately after a file is loaded into the editor
  • NPPN_SHUTDOWN signals impending exit, usable for finalization tasks
  • FILEBEFORESAVE event can perform cleanup transforms, while
  • FILECLOSETARGET hooks enable resource deallocation or session state serialization

PythonScript example for trimming trailing whitespace before every save operation leverages the FILEBEFORESAVE hook to iterate over each line buffer, removing extraneous spaces:

def trim_trailing_whitespace():
  editor.beginUndoAction()
  line_count = editor.getLineCount()
  for line in range(line_count):
    line_text = editor.getLine(line)
    trimmed = line_text.rstrip(’ \t\r\n’)
    if trimmed != line_text:
      editor.setTargetStart(editor.positionFromLine(line))
      editor.setTargetEnd(editor.getLineEndPosition(line))
      editor.replaceTarget(trimmed)
      editor.endUndoAction()
# Hook to file save event
notepad.callback(trim_trailing_whitespace, [NOTIFICATION.FILEBEFORESAVE])