Coverage for C:\leo.repo\leo-editor\leo\core\leoHistory.py: 50%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2#@+leo-ver=5-thin
3#@+node:ekr.20150514154159.1: * @file leoHistory.py
4#@@first
5from leo.core import leoGlobals as g
6assert g
7#@+others
8#@+node:ekr.20160514120255.1: ** class NodeHistory
9class NodeHistory:
10 """A class encapsulating knowledge of visited nodes."""
11 #@+others
12 #@+node:ekr.20070615131604.1: *3* NodeHistory.ctor
13 def __init__(self, c):
14 """Ctor for NodeHistory class."""
15 self.c = c
16 self.beadList = []
17 # a list of (position,chapter) tuples.
18 self.beadPointer = -1
19 self.skipBeadUpdate = False
20 #@+node:ekr.20160426061203.1: *3* NodeHistory.dump
21 def dump(self):
22 """Dump the beadList"""
23 for i, data in enumerate(self.beadList):
24 p, chapter = data
25 p = p.h if p else 'no p'
26 chapter = chapter.name if chapter else 'main'
27 mark = '**' if i == self.beadPointer else ' '
28 print(f"{mark} {i} {chapter} {p}")
29 #@+node:ekr.20070615134813: *3* NodeHistory.goNext
30 def goNext(self):
31 """Select the next node, if possible."""
32 if self.beadPointer + 1 < len(self.beadList):
33 self.beadPointer += 1
34 p, chapter = self.beadList[self.beadPointer]
35 self.select(p, chapter)
36 #@+node:ekr.20130915111638.11288: *3* NodeHistory.goPrev
37 def goPrev(self):
38 """Select the previously visited node, if possible."""
39 if self.beadPointer > 0:
40 self.beadPointer -= 1
41 p, chapter = self.beadList[self.beadPointer]
42 self.select(p, chapter)
43 #@+node:ekr.20130915111638.11294: *3* NodeHistory.select
44 def select(self, p, chapter):
45 """
46 Update the history list when selecting p.
47 Called only from self.goToNext/PrevHistory
48 """
49 c, cc = self.c, self.c.chapterController
50 if c.positionExists(p):
51 self.skipBeadUpdate = True
52 try:
53 oldChapter = cc.getSelectedChapter()
54 if oldChapter != chapter:
55 cc.selectChapterForPosition(p, chapter=chapter)
56 c.selectPosition(p) # Calls cc.selectChapterForPosition
57 finally:
58 self.skipBeadUpdate = False
59 # Fix bug #180: Always call self.update here.
60 self.update(p, change=False)
61 #@+node:ville.20090724234020.14676: *3* NodeHistory.update
62 def update(self, p, change=True):
63 """
64 Update the beadList while p is being selected.
65 Called *only* from c.frame.tree.selectHelper.
66 """
67 c, cc = self.c, self.c.chapterController
68 if not p or not c.positionExists(p) or self.skipBeadUpdate:
69 return
70 # A hack: don't add @chapter nodes.
71 # These are selected during the transitions to a new chapter.
72 if p.h.startswith('@chapter '):
73 return
74 # Fix bug #180: handle the change flag.
75 aList, found = [], -1
76 for i, data in enumerate(self.beadList):
77 p2, junk_chapter = data
78 if c.positionExists(p2):
79 if p == p2:
80 if change:
81 pass # We'll append later.
82 elif found == -1:
83 found = i
84 aList.append(data)
85 else:
86 pass # Remove any duplicate.
87 else:
88 aList.append(data)
89 if change or found == -1:
90 data = p.copy(), cc.getSelectedChapter()
91 aList.append(data)
92 self.beadPointer = len(aList) - 1
93 else:
94 self.beadPointer = found
95 self.beadList = aList
96 #@-others
97#@-others
98#@@language python
99#@@tabwidth -4
100#@@pagewidth 70
101#@-leo