Convert custom menu to snippets.py

From Bluefish Wiki
Jump to: navigation, search

In Bluefish 2 Custom menu is replaced by Snippets in left panel.
Bluefish_2 become without any snippets, you have to import or create them.

Man2 note.png To import your Bluefish_1 Custom menu in snippets you can use This Olivier's python script convert_custom_menu_to_snippets.py.
  • put convert_custom_menu_to_snippets.py in ~/.bluefish/ directory
  • make the script executable: chmod a+x convert_custom_menu_to_snippets.py and run the script.

#!/usr/bin/python

import sys
import os
from xml.sax.saxutils import escape, quoteattr

def leaf_insert(title, before, after, params):
	print '<leaf title='+quoteattr(title)+' type="insert">'
	#  tooltip="" hotkey=""
	print '<before>'+escape(before)+'</before>'
	print '<after>'+escape(after)+'</after>'
	i = 0
	for param in params:
		print '<param num="'+str(i)+'" name='+quoteattr(param)+'/>'
		i = i + 1
	print '</leaf>'

def leaf_snr(title, casesens, matchtype, region, search, replace, params):
	print '<leaf title='+quoteattr(title)+' type="snr" casesens="'+casesens+'" matchtype="'+matchtype+'" region="'+region+'">'
	# tooltip="" hotkey=""
	print '<searchpat>'+escape(search)+'</searchpat>'
	print '<replacepat>'+escape(replace)+'</replacepat>'
	i = 0
	for param in params:
		print '<param num="'+str(i)+'" name='+quoteattr(param)+'/>'
		i = i + 1
	print '</leaf>'

def specialsplit(input):
	retval = []
	i=0
	inlen = len(input)
	tmp = ''
	while (i < inlen):
		if (input[i] == '\\'):
			if (input[i+1] == 't'):
				tmp += '\t'
			elif (input[i+1] == 'n'):
				tmp += '\n'
			elif (input[i+1] == 'r'):
				tmp += '\r'
			elif (input[i+1] == '\\'):
				tmp += '\\'
			elif (input[i+1] == ':'):
				tmp += ':'
			i += 2
		elif (input[i] == ':'):
			retval.append(tmp)
			tmp = ''
			i += 1
		else:
			tmp += input[i]
			i += 1
	if (len(tmp)):
		retval.append(tmp)
	return retval

def splittitle(input):
	retval = []
	i=0
	inlen = len(input)
	tmp = ''
	while (i < inlen):
		if (input[i] == '\\'):
			if (input[i+1] == '/'):
				tmp += '/'
				i += 2
			else: #weird
				tmp += '\\'
				i += 1  
		elif (input[i] == '/'):
			if (len(tmp)):
				retval.append(tmp)
				tmp = ''
			i += 1
		else:
			tmp += input[i]
			i += 1
	if (len(tmp)):
		retval.append(tmp)
	return retval

def numtoregion(num):
	if (num == '0'):
		return 'beginning'
	elif (num == '1'):
		return 'cursor'	
	elif (num == '2'):
		return 'selection'	
	elif (num == '3'):
		return 'allopenfiles'
	else:
		print 'ERROR'
		sys.exit(1)	

def numtomatchtype(num):
	# type 'word' did not yet exist in the stable version
	if (num == '0'):
		return 'normal'
	elif (num == '1'):
		return 'posix'	
	elif (num == '2'):
		return 'perl'	
	else:
		print 'ERROR'
		sys.exit(1)	

list = {}
fd = open(os.environ.get("HOME")+'/.bluefish/custom_menu')
line = fd.readline()
while (len(line)>0):
	tmp = specialsplit(line[:-1])
	tmp[1] = tmp[1][1:]
	list[tmp[1]] = tmp
	line = fd.readline()

keys = list.keys()
keys.sort()

print '<?xml version="1.0"?>'
print '<snippets>'

stack = []

for key in keys:
	tmp = list[key]
	
	branches = splittitle(tmp[1])
	blen = len(branches)-1
	i=0
	while (i < blen):
		if (i >= len(stack)):
			# start creating branches
			print '<branch title="'+escape(branches[i])+'">'
			stack.append(branches[i])
		if (stack[i] != branches[i]):
			# end the branches on the stack, startnewbranches
			j = i
			slen = len(stack)
			while (j < slen):
				print '</branch>'
				j += 1
				stack.pop()
			print '<branch title="'+escape(branches[i])+'">'
			stack.append(branches[i])
		else:
			pass
		i += 1
	if (tmp[0] == 'cmenu_insert'):
		leaf_insert(branches[-1], tmp[2], tmp[3], tmp[5:])
	else:
		leaf_snr(branches[-1], tmp[6], numtomatchtype(tmp[5]), numtoregion(tmp[4]), tmp[2], tmp[3], tmp[8:])
j = 0
slen = len(stack)
while (j < slen):
	print '</branch>'
	j += 1
	stack.pop()
print '</snippets>'