From 69f325133c43804fff50bed1d5f381a08f8942bd Mon Sep 17 00:00:00 2001 From: btstream Date: Fri, 19 Sep 2014 12:53:58 +0800 Subject: [PATCH 1/2] Add Better Fold Sections Better Fold Sections. --- Default (Linux).sublime-keymap | 68 +++++- Default (OSX).sublime-keymap | 80 ++++++- Default (Windows).sublime-keymap | 68 +++++- latexFoldSection.py | 354 +++++++++++++++++++++++++++++++ 4 files changed, 567 insertions(+), 3 deletions(-) mode change 100644 => 100755 Default (Linux).sublime-keymap mode change 100644 => 100755 Default (OSX).sublime-keymap mode change 100644 => 100755 Default (Windows).sublime-keymap create mode 100644 latexFoldSection.py diff --git a/Default (Linux).sublime-keymap b/Default (Linux).sublime-keymap old mode 100644 new mode 100755 index 3d4cab9aa..b73123bca --- a/Default (Linux).sublime-keymap +++ b/Default (Linux).sublime-keymap @@ -287,5 +287,71 @@ LaTeX Package keymap for Linux { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }, { "key": "following_text", "operator": "regex_match", "operand": "^$", "match_all": true } ] - } + }, + + // Fill all command + // Similar to latexing's fill all command + { "keys": ["ctrl+l", "ctrl+f"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fill_all"}, + + //Redefine Fold Command shortcuts + { "keys": ["ctrl+k", "ctrl+1"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_level", "args": {"FoldLevel": 1}}, + { "keys": ["ctrl+k", "ctrl+2"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_level", "args": {"FoldLevel": 2}}, + { "keys": ["ctrl+k", "ctrl+3"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_level", "args": {"FoldLevel": 3}}, + { "keys": ["ctrl+k", "ctrl+4"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_level", "args": {"FoldLevel": 4}}, + { "keys": ["ctrl+k", "ctrl+5"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_level", "args": {"FoldLevel": 5}}, + { "keys": ["ctrl+k", "ctrl+6"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_level", "args": {"FoldLevel": 6}}, + { "keys": ["ctrl+k", "ctrl+7"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_level", "args": {"FoldLevel": 7}}, + + // Fold to toc + { "keys": ["ctrl+k", "ctrl+9"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_toc", "args": {"FocusCurrent": false}}, + + //Fold to toc except current focus + { "keys": ["ctrl+k", "ctrl+0"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_toc", "args": {"FocusCurrent": true}}, + + //Fold current + { "keys": ["ctrl+shift+["], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_current"}, ] diff --git a/Default (OSX).sublime-keymap b/Default (OSX).sublime-keymap old mode 100644 new mode 100755 index 88af0b3cc..28fb25a76 --- a/Default (OSX).sublime-keymap +++ b/Default (OSX).sublime-keymap @@ -288,5 +288,83 @@ LaTeX Package keymap for OS X { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }, { "key": "following_text", "operator": "regex_match", "operand": "^$", "match_all": true } ] - } + }, + + // Fill all command + // Similar to latexing's fill all command + { "keys": ["super+l","super+f"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} + ], + "command": "latex_fill_all"}, + + //Redefine Fold Command shortcuts + { "keys": ["super+k", "super+1"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} + ], + "command": "latex_fold_level", "args": {"FoldLevel": 1}}, + { "keys": ["super+k", "super+2"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} + ], + "command": "latex_fold_level", "args": {"FoldLevel": 2}}, + { "keys": ["super+k", "super+3"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} + ], + "command": "latex_fold_level", "args": {"FoldLevel": 3}}, + { "keys": ["super+k", "super+4"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} + ], + "command": "latex_fold_level", "args": {"FoldLevel": 4}}, + { "keys": ["super+k", "super+5"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} + ], + "command": "latex_fold_level", "args": {"FoldLevel": 5}}, + { "keys": ["super+k", "super+6"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} + ], + "command": "latex_fold_level", "args": {"FoldLevel": 6}}, + { "keys": ["super+k", "super+7"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} + ], + "command": "latex_fold_level", "args": {"FoldLevel": 7}}, + + // Fold to toc + { "keys": ["super+k", "super+9"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} + ], + "command": "latex_fold_toc", "args": {"FocusCurrent": false}}, + + //Fold to toc except current focus + { "keys": ["super+k", "super+0"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} + ], + "command": "latex_fold_toc", "args": {"FocusCurrent": true}}, + + //Fold current + { "keys": ["super+alt+["], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} + ], + "command": "latex_fold_current"}, + + { "keys": ["super+alt+]"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} + ], + "command": "latex_unfold_current"}, + + { "keys": ["super+k", "super+o"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"} + ], + "command": "latex_unfold_current"} ] diff --git a/Default (Windows).sublime-keymap b/Default (Windows).sublime-keymap old mode 100644 new mode 100755 index 25536e424..2700927cd --- a/Default (Windows).sublime-keymap +++ b/Default (Windows).sublime-keymap @@ -287,5 +287,71 @@ LaTeX Package keymap for Windows { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }, { "key": "following_text", "operator": "regex_match", "operand": "^$", "match_all": true } ] - } + }, + + // Fill all command + // Similar to latexing's fill all command + { "keys": ["ctrl+l", "ctrl+f"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fill_all"}, + + //Redefine Fold Command shortcuts + { "keys": ["ctrl+k", "ctrl+1"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_level", "args": {"FoldLevel": 1}}, + { "keys": ["ctrl+k", "ctrl+2"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_level", "args": {"FoldLevel": 2}}, + { "keys": ["ctrl+k", "ctrl+3"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_level", "args": {"FoldLevel": 3}}, + { "keys": ["ctrl+k", "ctrl+4"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_level", "args": {"FoldLevel": 4}}, + { "keys": ["ctrl+k", "ctrl+5"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_level", "args": {"FoldLevel": 5}}, + { "keys": ["ctrl+k", "ctrl+6"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_level", "args": {"FoldLevel": 6}}, + { "keys": ["ctrl+k", "ctrl+7"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_level", "args": {"FoldLevel": 7}}, + + // Fold to toc + { "keys": ["ctrl+k", "ctrl+9"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_toc", "args": {"FocusCurrent": false}}, + + //Fold to toc except current focus + { "keys": ["ctrl+k", "ctrl+0"], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_toc", "args": {"FocusCurrent": true}}, + + //Fold current + { "keys": ["ctrl+shift+["], + "context": [ + {"key": "selector", "operator": "equal", "operand": "text.tex.latex"}, + ], + "command": "latex_fold_current"}, ] diff --git a/latexFoldSection.py b/latexFoldSection.py new file mode 100644 index 000000000..ad5227eef --- /dev/null +++ b/latexFoldSection.py @@ -0,0 +1,354 @@ +from __future__ import print_function +import sublime +import sublime_plugin +import re + +if sublime.version() < '3000': + _ST3 = False +else: + _ST3 = True + +SECTION_TYPES = ['part', 'chapter', 'section', 'subsection', + 'subsubsection', 'paragraph', 'subparagraph'] + +def get_begin_mark_patter(sec_type): + return r'\\' + sec_type + r'\*?(?:\[.*\])?\{.*\}(?:\s*%\s\(fold\))?' + +def get_end_mark_patter(sec_type): + return r'%\s*' + sec_type + r'.*\s*\(end\)' + +# Find top level's mark name in this buffer +# return None if find failed +def find_top_level(view): + top_level = None + for i in SECTION_TYPES: + regions = view.find_all(get_begin_mark_patter(i)) + if len(regions) == 0: + continue + else: + top_level = i + break + return top_level + +def get_Region(a, b): + + if _ST3: + return sublime.Region(a, b) + else: + return sublime.Region(long(a), long(b)) + + +# Retrive all segments by section types +# returned result is a list, each element in it +# is a tuple in the form of +# [Region of this sec_type, Fold start mark's Region, Fold End mark's region] +# the first one is the area really needed to fold +# latter ones are used to judge whether the current cursor's position +# is in fold start mark or end mark +def find_sec_regions(view, sec_type): + + extrame_fold = sublime.load_settings("LaTeXTools.sublime-settings").get('extreme_fold') + + if extrame_fold != "ALL": + extrame_fold = True + else: + extrame_fold = False + + begin_mark = get_begin_mark_patter(sec_type) + end_mark = get_end_mark_patter(sec_type) + + # Find sec_type's parent mark in region range a to b + # if find, return first match's index, else return None + def find_parent_tags(sec_type, a, b): + + result = [] + s = view.substr(get_Region(a,b)) + for i in SECTION_TYPES[:SECTION_TYPES.index(sec_type)]: + rs = re.findall(get_begin_mark_patter(i), s) + if len(rs) != 0: + for r in rs: + m_pos = s.find(r) + m_size = len(r) + result.append(get_Region(a + m_pos, a + m_pos + m_size)) + + # sorted by apperance + return sorted(result, key = lambda x: x.begin()) + + # Find sec_type's parent mark in region range a to b + # if find, return first match's index, else return None + def find_child_tags(sec_type, a, b): + result = [] + s = view.substr(get_Region(a,b)) + for i in SECTION_TYPES[SECTION_TYPES.index(sec_type) + 1:]: + rs = re.findall(get_begin_mark_patter(i), s) + if len(rs) != 0: + for r in rs: + m_pos = s.find(r) + m_size = len(r) + result.append(get_Region(a + m_pos, a + m_pos + m_size)) + + return sorted(result, key = lambda x: x.begin()) + + start_regions = view.find_all(begin_mark) + code_segs = [] + + for i in range(len(start_regions)): + current_mark = start_regions[i] + if i + 1 < len(start_regions): + next_mark = start_regions[i + 1] + else: + next_mark = view.line(view.size()) # last line + + parent_tags = find_parent_tags(sec_type, current_mark.end(), next_mark.begin()) + + # if there is no parent tags + if len(parent_tags) != 0: + next_mark = parent_tags[0] + + child_tags = find_child_tags(sec_type, current_mark.end(), next_mark.begin()) + + if extrame_fold: # if extreme_fold set to all, ignore the end mark in the form of '% ... (end)' + if len(child_tags) != 0: + first_end_mark = view.find(end_mark, child_tags[-1].end()) + else: + first_end_mark = view.find(end_mark, current_mark.end()) + + if not _ST3 and first_end_mark == None: + first_end_mark = sublime.Region(-1, -1) + + # if there is no end mark or next end mark is bellow next same level's mark + # or a parents' mark + if first_end_mark.begin() == -1 or first_end_mark.begin() > next_mark.begin(): + current_end_mark = next_mark + else: + current_end_mark = first_end_mark + else: + current_end_mark = next_mark + + # if just one line at the last + if current_mark.end() == current_end_mark.end(): + content = get_Region(current_mark.end(), current_mark.end()) + else: + content = get_Region(current_mark.end(), + view.line(current_end_mark.begin()).begin() - 1) + code_segs.append([content, current_mark, current_end_mark]) + + return code_segs + + +# Functions to find all foldable code segments +# This function is only used for fold to toc when +# extreme_fold set to None +def find_all_fold(view, depth = None): + + # Find fold depth + if depth != None: + top_level = find_top_level(view) + if top_level != None: + top_level = SECTION_TYPES.index(top_level) + if top_level + depth - 1 > len(SECTION_TYPES): + search_secs = SECTION_TYPES[top_level:] + else: + search_secs = SECTION_TYPES[top_level:top_level + depth] + else: + search_secs = SECTION_TYPES + + # Finding all regions need to be folded + result = [] + for sec in search_secs: + r = find_sec_regions(view, sec) + if len(r) == 0: + continue + result += r + + return result + +# Reform the whole buffer to foldable regions following +# the struct of TOC. Return result format is identical to +# find_sec_regions function, this function is only used for +# none extreme_fold satuations +def get_toc(view): + s = sublime.load_settings("LaTeXTools.sublime-settings") + fold_toc_depth = s.get('fold_toc_depth') + + if fold_toc_depth == None: + fold_toc_depth = 4 + + # Get all codes + code_segs = find_all_fold(view, depth = fold_toc_depth) + + # Sorted by start mark position + code_segs = sorted(code_segs, key = lambda x: x[0].begin()) + + # Generating toc fold region + toc_strc = [] + for i in code_segs: + if code_segs.index(i) < len(code_segs) - 1: + j = code_segs[code_segs.index(i) + 1] + if i[0].end() > j[0].begin(): + toc_strc.append([ + get_Region(i[0].begin(), view.line(j[0].begin()).begin() - 1), + i[1], # i's start mark as new start mark + j[1] # j's start mark as new end mark + ]) + else: + toc_strc.append(i) + else: + toc_strc.append(i) + + return toc_strc + +def extreme_fold(view, FocusCurrent = False): + depth = sublime.load_settings("LaTeXTools.sublime-settings").get('fold_toc_depth') + + if depth == None: + depth = 4 + + top_level = find_top_level(view) + + if top_level != None: + top_level = SECTION_TYPES.index(top_level) + if top_level + depth - 1 > len(SECTION_TYPES): + search_secs = SECTION_TYPES[top_level:] + else: + search_secs = SECTION_TYPES[top_level:top_level + depth] + + s = r'|'.join([get_begin_mark_patter(x) for x in search_secs]) + # print(get_begin_mark_patter(s)) + regions = view.find_all(s) + + for r in regions: + if r != regions[-1]: + j = regions[regions.index(r) + 1] + nr = sublime.Region(view.line(r.end()).end(), view.line(j.begin()).begin() - 1) + else: + if view.line(r.end()).end() != view.size(): + nr = sublime.Region(view.line(r.end()).end(), view.size()) + else: + nr = None + + if FocusCurrent and (nr == None or nr.contains(view.sel()[0].b)): + continue + else: + view.fold(nr) + +# Fold whole buffer to "Table of Contents", +# TOC depth is set by "fold_toc_depth" in settings files +class LatexFoldTocCommand(sublime_plugin.TextCommand): + + def run(self, edit, FocusCurrent = False): + + view = self.view + + extrame_fold = sublime.load_settings("LaTeXTools.sublime-settings").get('extreme_fold') + + if extrame_fold == None or extrame_fold == 'TOC': + extreme_fold(view, FocusCurrent) + return + + # Getting fold_toc_depth from settings + toc_strc = get_toc(view) + + point = view.sel()[0].end() + for i in toc_strc: + + region_span = i[0].cover(i[1]) + + # whether end mark is in the form of '% ... (end) form' + # if it is, end mark should be in the region + if re.search(r'%\s*.*\(end\)', view.substr(i[2])) != None: + region_span = region_span.cover(i[2]) + + if FocusCurrent and region_span.contains(point): + continue + else: + view.fold(i[0]) + + + +# Command to fold expect levels. +class LatexFoldLevelCommand(sublime_plugin.TextCommand): + + def run(self, edit, FoldLevel = 1): + + view = self.view + + # Get top level mark + top_level = find_top_level(view) + + # Only work when there exist top level marks + if top_level != None: + + top_level = SECTION_TYPES.index(top_level) + fold_level = top_level + (FoldLevel - 1) + + # make sure FoldLevel do not exceed the scope of secion_types + if fold_level >= len(SECTION_TYPES): + fold_level = len(SECTION_TYPES) - 1 + + sec_type = SECTION_TYPES[fold_level] + + fold_regions = find_sec_regions(view, sec_type) + for i in fold_regions: + view.fold(i[0]) + + +# Fold current section +class LatexFoldCurrentCommand(sublime_plugin.TextCommand): + + def run(self, edit): + extrame_fold = sublime.load_settings("LaTeXTools.sublime-settings").get('extreme_fold') + + view = self.view + point = view.sel()[0].end() + + for sec in SECTION_TYPES[::-1]: # Searching from the direct parent. + regions = find_sec_regions(view, sec) + for i in regions: + + region_span = i[0].cover(i[1]) + + # if end mark is a norm end mark + if extrame_fold != 'ALL': + if re.search(r'%\s*.*\(end\)', view.substr(i[2])) != None: + region_span = region_span.cover(i[2]) + + if region_span.contains(point): + + # if current foldable segment contains current point + # and have not been folded then fold it, or go to parent + # segment + if view.fold(i[0]) == True: + return + else: + break + +# Unfold current section. +class LatexUnfoldCurrentCommand(sublime_plugin.TextCommand): + + def run(self, edit): + + extrame_fold = sublime.load_settings("LaTeXTools.sublime-settings").get('extreme_fold') + + view = self.view + point = view.sel()[0].end() + + for sec in SECTION_TYPES[::-1]: # Searching from the direct parent. + regions = find_sec_regions(view, sec) + for i in regions: + + region_span = i[0].cover(i[1]) + + # print(i[2]) + + # if end mark is a norm end mark + if extrame_fold != 'ALL': + if re.search(r'%\s*.*\(end\)', view.substr(i[2])) != None: + region_span = region_span.cover(i[2]) + + if region_span.contains(point): + regions = view.unfold(i[0]) + if len(regions) > 0: + view.sel().clear() + view.sel().add(i[0]) + return From acec4b4c4ceff6f3b287e8e94db3356b347cfdf4 Mon Sep 17 00:00:00 2001 From: btstream Date: Thu, 25 Sep 2014 21:42:42 +0800 Subject: [PATCH 2/2] Add Preamble fold support Add Preamble fold support --- latexFoldSection.py | 195 +++++++++++++++++++++++++++++++------------- 1 file changed, 138 insertions(+), 57 deletions(-) diff --git a/latexFoldSection.py b/latexFoldSection.py index ad5227eef..0fb8a7bcc 100644 --- a/latexFoldSection.py +++ b/latexFoldSection.py @@ -45,14 +45,16 @@ def get_Region(a, b): # the first one is the area really needed to fold # latter ones are used to judge whether the current cursor's position # is in fold start mark or end mark -def find_sec_regions(view, sec_type): +def find_sec_regions(view, sec_type, EXECUTE_TOC = False): - extrame_fold = sublime.load_settings("LaTeXTools.sublime-settings").get('extreme_fold') + EF = sublime.load_settings("LaTeXTools.sublime-settings").get('extreme_fold') - if extrame_fold != "ALL": - extrame_fold = True + # if EF == All and excuted from fold_to_toc command and EF is set + # to "TOC" or not set + if EF == "ALL" or (EXECUTE_TOC and ( EF == "TOC" or EF == None)): + EF = True else: - extrame_fold = False + EF = False begin_mark = get_begin_mark_patter(sec_type) end_mark = get_end_mark_patter(sec_type) @@ -96,8 +98,17 @@ def find_child_tags(sec_type, a, b): current_mark = start_regions[i] if i + 1 < len(start_regions): next_mark = start_regions[i + 1] - else: - next_mark = view.line(view.size()) # last line + else: # if there is no next marks + end_doc = view.find(r'\\end{document}', 0) # find \end{document} + + # ST2 compatible + if not _ST3 and end_doc == None: + end_doc = sublime.Region(-1, -1) + + if end_doc.begin() != -1: + next_mark = end_doc + else: + next_mark = view.line(view.size()) # last line parent_tags = find_parent_tags(sec_type, current_mark.end(), next_mark.begin()) @@ -107,7 +118,7 @@ def find_child_tags(sec_type, a, b): child_tags = find_child_tags(sec_type, current_mark.end(), next_mark.begin()) - if extrame_fold: # if extreme_fold set to all, ignore the end mark in the form of '% ... (end)' + if not EF: # if extreme_fold set to all, search the end mark in the form of '% ... (end)' if len(child_tags) != 0: first_end_mark = view.find(end_mark, child_tags[-1].end()) else: @@ -131,7 +142,7 @@ def find_child_tags(sec_type, a, b): else: content = get_Region(current_mark.end(), view.line(current_end_mark.begin()).begin() - 1) - code_segs.append([content, current_mark, current_end_mark]) + code_segs.append([content, current_mark, current_end_mark]) return code_segs @@ -139,7 +150,7 @@ def find_child_tags(sec_type, a, b): # Functions to find all foldable code segments # This function is only used for fold to toc when # extreme_fold set to None -def find_all_fold(view, depth = None): +def find_all_fold(view, depth = None, EXECUTE_TOC = False): # Find fold depth if depth != None: @@ -156,13 +167,14 @@ def find_all_fold(view, depth = None): # Finding all regions need to be folded result = [] for sec in search_secs: - r = find_sec_regions(view, sec) + r = find_sec_regions(view, sec, EXECUTE_TOC = EXECUTE_TOC) if len(r) == 0: continue result += r return result + # Reform the whole buffer to foldable regions following # the struct of TOC. Return result format is identical to # find_sec_regions function, this function is only used for @@ -175,7 +187,50 @@ def get_toc(view): fold_toc_depth = 4 # Get all codes - code_segs = find_all_fold(view, depth = fold_toc_depth) + code_segs = find_all_fold(view, depth = fold_toc_depth, EXECUTE_TOC = True) + + # Process \documentclass, \begin{document} and \end{document} tags + documentclass = view.find(r'\\documentclass(?:\[.*\])?\{.*\}', 0) + if not _ST3 and documentclass == None: + documentclass = sublime.Region(-1, -1) + + # if \documentclass tag exist! + if documentclass.begin() > -1: + doc_begin = view.find(r'\\begin\{document\}', 0) + end_begin = view.find(r'\\end\{document\}', 0) + + # region contain preamble + preamble_region = [ + sublime.Region( + documentclass.end(), + doc_begin.begin() - 1 + ), # real preamble_region + documentclass, + view.line(doc_begin.begin() - 1) + ] + + first_sec_mark = min(code_segs, key = lambda x: x[0].begin()) + doc_begin_region = [ + sublime.Region( + doc_begin.end(), + first_sec_mark[1].begin() - 1 + ), + doc_begin, + view.line(first_sec_mark[1].begin() - 1) + ] + + doc_end_region = [ + sublime.Region( + end_begin.end(), + view.size() + ), + end_begin, + view.line(view.size()) + ] + + code_segs.append(preamble_region) + code_segs.append(doc_begin_region) + code_segs.append(doc_end_region) # Sorted by start mark position code_segs = sorted(code_segs, key = lambda x: x[0].begin()) @@ -198,40 +253,6 @@ def get_toc(view): return toc_strc -def extreme_fold(view, FocusCurrent = False): - depth = sublime.load_settings("LaTeXTools.sublime-settings").get('fold_toc_depth') - - if depth == None: - depth = 4 - - top_level = find_top_level(view) - - if top_level != None: - top_level = SECTION_TYPES.index(top_level) - if top_level + depth - 1 > len(SECTION_TYPES): - search_secs = SECTION_TYPES[top_level:] - else: - search_secs = SECTION_TYPES[top_level:top_level + depth] - - s = r'|'.join([get_begin_mark_patter(x) for x in search_secs]) - # print(get_begin_mark_patter(s)) - regions = view.find_all(s) - - for r in regions: - if r != regions[-1]: - j = regions[regions.index(r) + 1] - nr = sublime.Region(view.line(r.end()).end(), view.line(j.begin()).begin() - 1) - else: - if view.line(r.end()).end() != view.size(): - nr = sublime.Region(view.line(r.end()).end(), view.size()) - else: - nr = None - - if FocusCurrent and (nr == None or nr.contains(view.sel()[0].b)): - continue - else: - view.fold(nr) - # Fold whole buffer to "Table of Contents", # TOC depth is set by "fold_toc_depth" in settings files class LatexFoldTocCommand(sublime_plugin.TextCommand): @@ -240,13 +261,6 @@ def run(self, edit, FocusCurrent = False): view = self.view - extrame_fold = sublime.load_settings("LaTeXTools.sublime-settings").get('extreme_fold') - - if extrame_fold == None or extrame_fold == 'TOC': - extreme_fold(view, FocusCurrent) - return - - # Getting fold_toc_depth from settings toc_strc = get_toc(view) point = view.sel()[0].end() @@ -297,11 +311,12 @@ def run(self, edit, FoldLevel = 1): class LatexFoldCurrentCommand(sublime_plugin.TextCommand): def run(self, edit): - extrame_fold = sublime.load_settings("LaTeXTools.sublime-settings").get('extreme_fold') + EF = sublime.load_settings("LaTeXTools.sublime-settings").get('extreme_fold') view = self.view point = view.sel()[0].end() + FOLD_DONE = False for sec in SECTION_TYPES[::-1]: # Searching from the direct parent. regions = find_sec_regions(view, sec) for i in regions: @@ -309,7 +324,7 @@ def run(self, edit): region_span = i[0].cover(i[1]) # if end mark is a norm end mark - if extrame_fold != 'ALL': + if EF != 'ALL': if re.search(r'%\s*.*\(end\)', view.substr(i[2])) != None: region_span = region_span.cover(i[2]) @@ -319,20 +334,86 @@ def run(self, edit): # and have not been folded then fold it, or go to parent # segment if view.fold(i[0]) == True: + FOLD_DONE = True return else: break + if not FOLD_DONE: + documentclass = view.find(r'\\documentclass(?:\[.*\])?\{.*\}', 0) + if not _ST3 and documentclass == None: + documentclass = sublime.Region(-1, -1) + + # if \documentclass tag exist! + if documentclass.begin() > -1: + doc_begin = view.find(r'\\begin\{document\}', 0) + end_begin = view.find(r'\\end\{document\}', 0) + + # region contain preamble + preamble_region = sublime.Region( + documentclass.end(), + doc_begin.begin() - 1 + ) + + first_sec_mark = min(find_all_fold(view, depth = 1), key = lambda x: x[0].begin()) + + doc_begin_region = sublime.Region(doc_begin.end(), first_sec_mark[1].begin() - 1) + + doc_region = sublime.Region(doc_begin.end(), end_begin.begin() - 1) + + if preamble_region.contains(point): + view.fold(preamble_region) + elif doc_begin_region.contains(point): + if view.fold(doc_begin_region) == False: + view.fold(doc_region) + else: + view.fold(doc_region) + # Unfold current section. class LatexUnfoldCurrentCommand(sublime_plugin.TextCommand): def run(self, edit): - extrame_fold = sublime.load_settings("LaTeXTools.sublime-settings").get('extreme_fold') + EF = sublime.load_settings("LaTeXTools.sublime-settings").get('extreme_fold') view = self.view point = view.sel()[0].end() + documentclass = view.find(r'\\documentclass(?:\[.*\])?\{.*\}', 0) + if not _ST3 and documentclass == None: + documentclass = sublime.Region(-1, -1) + + # if \documentclass tag exist! + if documentclass.begin() > -1: + doc_begin = view.find(r'\\begin\{document\}', 0) + end_begin = view.find(r'\\end\{document\}', 0) + + # region contain preamble + preamble_region = sublime.Region( + documentclass.end(), + doc_begin.begin() - 1 + ) + + first_sec_mark = min(find_all_fold(view, depth = 1), key = lambda x: x[0].begin()) + + doc_begin_region = sublime.Region(doc_begin.end(), first_sec_mark[1].begin() - 1) + + # doc_region = sublime.Region(doc_begin.end(), end_begin.begin() - 1) + + if preamble_region.cover(documentclass).contains(point): + unfold = view.unfold(preamble_region) + if len(unfold) > 0: + view.sel().clear() + view.sel().add(preamble_region) + return + + if doc_begin_region.cover(doc_begin).contains(point): + unfold = view.unfold(doc_begin_region) + if len(unfold) > 0: + view.sel().clear() + view.sel().add(doc_begin_region) + return + for sec in SECTION_TYPES[::-1]: # Searching from the direct parent. regions = find_sec_regions(view, sec) for i in regions: @@ -342,7 +423,7 @@ def run(self, edit): # print(i[2]) # if end mark is a norm end mark - if extrame_fold != 'ALL': + if EF != 'ALL': if re.search(r'%\s*.*\(end\)', view.substr(i[2])) != None: region_span = region_span.cover(i[2])