Sublime Forum

LaTeX Outlines package

#1

I just found out about Sublime and it looks amazing. The fact that is works on both windows and OSX is a huge plus for me. So much, I will give up TexShop on the Mac if I can get the functionality below. I spend hundreds of hours each year making documents in the following format.

I use the LaTeX outlines package extensively. It basically allows one to use \1 \2 \3 \4 at the beginning of a line to indicate the levels of outline.

When I type up my content I never use soft returns so each line is continuous beginning with \1 … \4.

\1 Some content.
\1 More content.
\2 Indented content but not really indented
\3 blah blah

I would like to develop a way to “promote” or “demote” either the current line or a selection of lines. ie for promoting I would add 1 to \1 to make it \2, etc…

So demoting the example above:

\2 Some content.
\2 More content.
\3 Indented content but not really indented
\4 blah blah

Does anyone have any hints at where to begin to code this functionality? I started to look at the default package, but I am not having much luck. I am brand new to python.

Ideally I would like to invoke command when I am anywhere on the line for a single line. For a group of lines, I would like to be able to begin highlighting anywhere on a line and end anywhere as well and do this to the line.

Any helps or hints would be appreciated.

StephenL

0 Likes

#2

Answering my own question after many hours of becoming familiar with python and the API.

If there are any suggestions or different approaches I’d be glad to hear them.

StephenL

class DemoteCommand(sublime_plugin.TextCommand):
	def run(self, edit):
			# Get a reference to the selections
			sel = self.view.sel()
			# Loop over the various selections
			for region in self.view.sel():
				# grab full lines regardless of where they start in the line
				line2 = self.view.line(region)
				# split them into individual lines
				lines = self.view.split_by_newlines(line2)
				for l in lines:
					# return the contents as a string
					s = self.view.substr(l)
					# check if it is an empty line
					if len(s) > 0:
						# check if the first character is a slash
						if s[0] == '\\':
							# print(s[0])
							if int(s[1]) < 4:
								new_num = str(int(s[1])+1)
								#print(new_num)
								self.view.replace(edit, sublime.Region(l.begin()+1, l.begin()+2), new_num)
0 Likes