Skip to content

Instantly share code, notes, and snippets.

@vincepare
Last active November 4, 2016 07:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vincepare/8a204172d959defb2122 to your computer and use it in GitHub Desktop.
Save vincepare/8a204172d959defb2122 to your computer and use it in GitHub Desktop.
Automatic indentation detection for Notepad++ (requires Python Script extension)
# -*- coding: utf-8 -*-
# Automatic indentation detection for Notepad++
# Version 1.0.1
# Copyright (c) 2015 Vincent Paré
# Licensed under the Apache 2.0 license : http://opensource.org/licenses/Apache-2.0
# Tested on Python Script 1.0.8.0 (Python 2.7.6) @ Notepad++ 6.7.8.2
import sys
from Npp import *
sys.stderr = console
import re
import time
def indent_guess_tab(text):
for line in text.split("\n"):
pattern = re.compile("^( {4,}|\t)")
match = pattern.match(line)
if (match):
return True if ("\t" in match.group(1)) else False
def indent_auto_detect(arg):
start = time.clock()
# Get text sample
maxLen = 500000
len = editor.getTextLength()
len = len if len < maxLen else maxLen
sample = editor.getTextRange(0, len)
# Indent set
current_use_tab = editor.getUseTabs()
use_tab = indent_guess_tab(sample)
if (use_tab != None and use_tab != current_use_tab):
console.write("Indent use tab switch (%s => %s)\n" % (current_use_tab, use_tab))
editor.setUseTabs(use_tab)
end = time.clock()
console.write("Indentation detection took %s ms\n" % (round((end-start)*1000, 3)))
notepad.clearCallbacks([NOTIFICATION.BUFFERACTIVATED, NOTIFICATION.READY])
notepad.callback(indent_auto_detect, [NOTIFICATION.BUFFERACTIVATED])
notepad.callback(indent_auto_detect, [NOTIFICATION.READY])
console.write("Automatic indentation detection started\n")
indent_auto_detect(None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment