def unl_to_pos(self, c2, for_p, bookmark=False): """"c2 may be an outline (like c) or an UNL (string) return c, p where c is an outline and p is a node to copy data to in that outline for_p is the p to be copied - needed to check for invalid recursive copy / move """ if g.isString(c2): # c2 is an UNL indicating where to insert full_path = c2 path, unl = full_path.split('#', 1) c2 = g.openWithFileName(path, old_c=self.c) self.c.bringToFront(c2=self.c) found, maxdepth, maxp = g.recursiveUNLFind(unl.split('-->'), c2) if found: if not bookmark and (for_p == maxp or for_p.isAncestorOf(maxp)): g.es("Invalid move") return None, None nd = maxp.insertAsNthChild(0) else: g.es("Could not find '%s'"%full_path) self.c.bringToFront(c2=self.c) return None, None else: # c2 is an outline, insert at top nd = c2.rootPosition().insertAfter() nd.copy().back().moveAfter(nd) return c2, nd
def handleURL(self, url): """handleUrl - user clicked an URL / UNL link :param str url: URL for link """ g.es(url) # UNL detection copied from g.handleUrl() if ( url.lower().startswith('unl:' + '//') or url.lower().startswith('file://') and url.find('-->') > -1 or url.startswith('#') ): our_unl = 'unl://'+self.c.p.get_UNL(with_index=False) # don't use .get_UNL(with_proto=True), that # unecessarily does ' ' -> %20 conversion new_c = g.handleUnl(url, self.c) if new_c and hasattr(new_c, 'backlinkController'): unl = url.replace('%20', ' ').split('#', 1)[-1].split('-->') found, _, new_p = g.recursiveUNLFind(unl, new_c) if not found: g.es("No perfect match, not creating backlink") return new_c.backlinkController.initBacklink(new_p.v) if our_unl not in [i.rsplit('##', 1)[0] for i in new_p.v.u['_bklnk']['urls']]: new_p.v.u['_bklnk']['urls'].append("%s##%s" % (our_unl, self.c.p.h)) new_c.backlinkController.updateTabInt() new_p.setDirty() new_c.setChanged(True) g.es("NOTE: created back link automatically") else: g.handleUrl(url, c=self.c)
def unl_to_pos(self, unl, for_p): """"unl may be an outline (like c) or an UNL (string) return c, p where c is an outline and p is a node to copy data to in that outline for_p is the p to be copied - needed to check for invalid recursive copy / move """ # COPIED FROM quickMove.py # unl is an UNL indicating where to insert full_path = unl path, unl = full_path.split('#', 1) c2 = g.openWithFileName(path, old_c=self.c) self.c.bringToFront(c2=self.c) found, maxdepth, maxp = g.recursiveUNLFind(unl.split('-->'), c2) if found: if (for_p == maxp or for_p.isAncestorOf(maxp)): g.es("Invalid move") return None, None nd = maxp else: g.es("Could not find '%s'"%full_path) self.c.bringToFront(c2=self.c) return None, None return c2, nd
def ns_provide(self, id_): if id_.startswith('_leo_bookmarks_show'): c = self.c v = None if ':' in id_: gnx = id_.split(':')[1] if not gnx and '_leo_bookmarks_show' in c.db: gnx = c.db['_leo_bookmarks_show'] # first try old style local gnx lookup for i in c.all_nodes(): if str(i.gnx) == gnx: v = i break else: # use UNL lookup file_, UNL = gnx.split('#', 1) other_c = g.openWithFileName(file_, old_c=c) if other_c != c: c.bringToFront() g.es("NOTE: bookmarks for this outline\nare in a different outline:\n '%s'"%file_) ok, depth, other_p = g.recursiveUNLFind(UNL.split('-->'), other_c) if ok: v = other_p.v else: g.es("Couldn't find '%s'"%gnx) if v is None: v = c.p.v bmd = BookMarkDisplay(self.c, v=v) return bmd.w
def unl_to_pos(self, unl, for_p): """"unl may be an outline (like c) or an UNL (string) return c, p where c is an outline and p is a node to copy data to in that outline for_p is the p to be copied - needed to check for invalid recursive copy / move """ # COPIED FROM quickMove.py # unl is an UNL indicating where to insert full_path = unl path, unl = full_path.split('#') c2 = g.openWithFileName(path, old_c=self.c) self.c.bringToFront(c2=self.c) found, maxdepth, maxp = g.recursiveUNLFind(unl.split('-->'), c2) if found: if (for_p == maxp or for_p.isAncestorOf(maxp)): g.es("Invalid move") return None, None nd = maxp else: g.es("Could not find '%s'"%full_path) self.c.bringToFront(c2=self.c) return None, None return c2, nd
def ns_provide(self, id_): if id_.startswith('_leo_bookmarks_show'): c = self.c v = None if ':' in id_: gnx = id_.split(':')[1] if not gnx and '_leo_bookmarks_show' in c.db: gnx = c.db['_leo_bookmarks_show'] # first try old style local gnx lookup for i in c.all_nodes(): if str(i.gnx) == gnx: v = i break else: # use UNL lookup if '#' in gnx: file_, UNL = gnx.split('#', 1) other_c = g.openWithFileName(file_, old_c=c) else: file_, UNL = None, gnx other_c = c if other_c != c: # don't use c.bringToFront(), it breaks --minimize if hasattr(g.app.gui, 'frameFactory'): factory = g.app.gui.frameFactory if factory and hasattr(factory, 'setTabForCommander'): factory.setTabForCommander(c) g.es( "NOTE: bookmarks for this outline\nare in a different outline:\n '%s'" % file_) ok, depth, other_p = g.recursiveUNLFind( UNL.split('-->'), other_c) if ok: v = other_p.v else: g.es("Couldn't find '%s'" % gnx) if v is None: v = c.p.v bmd = BookMarkDisplay(self.c, v=v) return bmd.w return None
def ns_provide(self, id_): if id_.startswith("_leo_bookmarks_show"): c = self.c v = None if ":" in id_: gnx = id_.split(":")[1] if not gnx and "_leo_bookmarks_show" in c.db: gnx = c.db["_leo_bookmarks_show"] # first try old style local gnx lookup for i in c.all_nodes(): if str(i.gnx) == gnx: v = i break else: # use UNL lookup if "#" in gnx: file_, UNL = gnx.split("#", 1) other_c = g.openWithFileName(file_, old_c=c) else: file_, UNL = None, gnx other_c = c if other_c != c: # don't use c.bringToFront(), it breaks --minimize if hasattr(g.app.gui, "frameFactory"): factory = g.app.gui.frameFactory if factory and hasattr(factory, "setTabForCommander"): factory.setTabForCommander(c) g.es("NOTE: bookmarks for this outline\nare in a different outline:\n '%s'" % file_) ok, depth, other_p = g.recursiveUNLFind(UNL.split("-->"), other_c) if ok: v = other_p.v else: g.es("Couldn't find '%s'" % gnx) if v is None: v = c.p.v bmd = BookMarkDisplay(self.c, v=v) return bmd.w
def copy_to_my_settings(self, unl, which): """copy_to_my_settings - copy setting from leoSettings.leo :param str unl: Leo UNL to copy from :param int which: 1-3, leaf, leaf's parent, leaf's grandparent :return: unl of leaf copy in myLeoSettings.leo :rtype: str """ trace = False and not g.unitTesting if trace: g.es(unl) path, unl = unl.split('#', 1) # Undo the replacements made in p.getUNL. path = path.replace("file://", "") path = path.replace("unl://", "") # Fix #434: Potential bug in settings unl = unl.replace('%20', ' ').split("-->") tail = [] if which > 1: # copying parent or grandparent but select leaf later tail = unl[-(which - 1):] unl = unl[:len(unl) + 1 - which] my_settings_c = self.c.openMyLeoSettings() my_settings_c.save() # if it didn't exist before, save required settings = g.findNodeAnywhere(my_settings_c, '@settings') c2 = g.app.loadManager.openSettingsFile(path) if not c2: return '' # Fix 434. found, maxdepth, maxp = g.recursiveUNLFind(unl, c2) if trace: g.trace('COPYING', unl) nd = settings.insertAsLastChild() dest = nd.get_UNL() self.copy_recursively(maxp, nd) my_settings_c.setChanged() my_settings_c.redraw() shortcutsDict, settingsDict = g.app.loadManager.createSettingsDicts( my_settings_c, False) self.c.config.settingsDict.update(settingsDict) my_settings_c.config.settingsDict.update(settingsDict) if trace: g.trace('-->'.join([dest] + tail)) return '-->'.join([dest] + tail)
def copy_to_my_settings(self, unl, which): """copy_to_my_settings - copy setting from leoSettings.leo :param str unl: Leo UNL to copy from :param int which: 1-3, leaf, leaf's parent, leaf's grandparent :return: unl of leaf copy in myLeoSettings.leo :rtype: str """ trace = False and not g.unitTesting if trace: g.es(unl) path, unl = unl.split('#', 1) # Undo the replacements made in p.getUNL. path = path.replace("file://", "") path = path.replace("unl://", "") # Fix #434: Potential bug in settings unl = unl.replace('%20', ' ').split("-->") tail = [] if which > 1: # copying parent or grandparent but select leaf later tail = unl[-(which - 1):] unl = unl[: len(unl) + 1 - which] my_settings_c = self.c.openMyLeoSettings() my_settings_c.save() # if it didn't exist before, save required settings = g.findNodeAnywhere(my_settings_c, '@settings') c2 = g.app.loadManager.openSettingsFile(path) if not c2: return '' # Fix 434. found, maxdepth, maxp = g.recursiveUNLFind(unl, c2) if trace: g.trace('COPYING', unl) nd = settings.insertAsLastChild() dest = nd.get_UNL() self.copy_recursively(maxp, nd) my_settings_c.setChanged() my_settings_c.redraw() shortcutsDict, settingsDict = g.app.loadManager.createSettingsDicts(my_settings_c, False) self.c.config.settingsDict.update(settingsDict) my_settings_c.config.settingsDict.update(settingsDict) if trace: g.trace('-->'.join([dest] + tail)) return '-->'.join([dest] + tail)
def ns_provide(self, id_): if id_.startswith('_leo_bookmarks_show'): c = self.c v = None if ':' in id_: gnx = id_.split(':')[1] if not gnx and '_leo_bookmarks_show' in c.db: gnx = c.db['_leo_bookmarks_show'] # first try old style local gnx lookup for i in c.all_nodes(): if str(i.gnx) == gnx: v = i break else: # use UNL lookup file_, UNL = gnx.split('#', 1) other_c = g.openWithFileName(file_, old_c=c) if other_c != c: c.bringToFront() g.es( "NOTE: bookmarks for this outline\nare in a different outline:\n '%s'" % file_) ok, depth, other_p = g.recursiveUNLFind( UNL.split('-->'), other_c) if ok: v = other_p.v else: g.es("Couldn't find '%s'" % gnx) if v is None: v = c.p.v bmd = BookMarkDisplay(self.c, v=v) return bmd.w
def find_setting(self, setting): # g.es("Settings finder: find %s" % setting) key = g.app.config.canonicalizeSettingName(setting) value = self.c.config.settingsDict.get(key) which = None while value and isinstance(value.val, str) and value.val.startswith('@'): msg = ( "The relevant setting, '@{specific}', is using the value of " "a more general setting, '{general}'. Would you like to edit the " "more specific setting, '@{specific}', or the more general setting, " "'{general}'? The more general setting may alter appearance / " "behavior in more places, which may or may not be what you prefer." ).format(specific=setting, general=value.val) which = g.app.gui.runAskYesNoCancelDialog( self.c, "Which setting?", message=msg, yesMessage='Edit Specific', noMessage='Edit General') if which != 'no': break setting = value.val key = g.app.config.canonicalizeSettingName(setting[1:]) value = self.c.config.settingsDict.get(key) if which == 'cancel' or not value: return unl = value and value.unl if (g.os_path_realpath(value.path) == g.os_path_realpath( g.os_path_join(g.app.loadManager.computeGlobalConfigDir(), 'leoSettings.leo'))): msg = ( "The setting '@{specific}' is in the Leo global configuration " "file 'leoSettings.leo'\nand should be copied to " "'myLeoSettings.leo' before editing.\n" "It may make more sense to copy a group or category of settings.\nIf " "'myLeoSettings.leo' contains duplicate settings, the last definition " "is used." "\n\nChoice:\n" "1. just select the node in 'leoSettings.leo', I will decide how much\n" " to copy into 'myLeoSettings.leo' (Recommended).\n" "2. copy the one setting, '@{specific}'\n") # get the settings already defined in myLeoSettings my_settings_c = self.c.openMyLeoSettings() _, settingsDict = g.app.loadManager.createSettingsDicts( my_settings_c, False) # find this setting's node path, src_unl = unl.split('#', 1) path = path.replace("file://", "").replace("unl://", "") src_unl = src_unl.replace('%20', ' ').split("-->") c2 = g.app.loadManager.openSettingsFile(path) found, maxdepth, maxp = g.recursiveUNLFind(src_unl, c2) # scan this setting's group and category for conflicts up = maxp.parent() if up and self.no_conflict(up, settingsDict): msg += "3. copy the setting group, '{group}'\n" up = up.parent() if up and self.no_conflict(up, settingsDict): msg += "4. copy the whole setting category, '{category}'\n" msg = msg.format( specific=setting.lstrip('@'), group=unl.split('-->')[-2].split(':', 1)[0].replace('%20', ' '), category=unl.split('-->')[-3].split(':', 1)[0].replace('%20', ' ')) which = g.app.gui.runAskOkCancelNumberDialog(self.c, "Copy setting?", message=msg) if which is None: return which = int(which) if which > 1: unl = self.copy_to_my_settings(unl, which - 1) if unl: g.handleUnl(unl, c=self.c)
def find_setting(self, setting): # g.es("Settings finder: find %s" % setting) key = g.app.config.canonicalizeSettingName(setting) value = self.c.config.settingsDict.get(key) which = None while value and g.isString(value.val) and value.val.startswith('@'): msg = ("The relevant setting, '@{specific}', is using the value of " "a more general setting, '{general}'. Would you like to edit the " "more specific setting, '@{specific}', or the more general setting, " "'{general}'? The more general setting may alter appearance / " "behavior in more places, which may or may not be what you prefer." ).format(specific=setting, general=value.val) which = g.app.gui.runAskYesNoCancelDialog(self.c, "Which setting?", message=msg, yesMessage='Edit Specific', noMessage='Edit General') if which != 'no': break setting = value.val key = g.app.config.canonicalizeSettingName(setting[1:]) value = self.c.config.settingsDict.get(key) if which == 'cancel' or not value: return unl = value and value.unl if ( g.os_path_realpath(value.path) == g.os_path_realpath(g.os_path_join( g.app.loadManager.computeGlobalConfigDir(), 'leoSettings.leo') )): msg = ("The setting '@{specific}' is in the Leo global configuration " "file 'leoSettings.leo'\nand should be copied to " "'myLeoSettings.leo' before editing.\n" "It may make more sense to copy a group or category of settings.\nIf " "'myLeoSettings.leo' contains duplicate settings, the last definition " "is used." "\n\nChoice:\n" "1. just select the node in 'leoSettings.leo', I will decide how much\n" " to copy into 'myLeoSettings.leo' (Recommended).\n" "2. copy the one setting, '@{specific}'\n") # get the settings already defined in myLeoSettings my_settings_c = self.c.openMyLeoSettings() _, settingsDict = g.app.loadManager.createSettingsDicts(my_settings_c, False) # find this setting's node path, src_unl = unl.split('#', 1) path = path.replace("file://", "").replace("unl://", "") src_unl = src_unl.replace('%20', ' ').split("-->") c2 = g.app.loadManager.openSettingsFile(path) found, maxdepth, maxp = g.recursiveUNLFind(src_unl, c2) # scan this setting's group and category for conflicts up = maxp.parent() if up and self.no_conflict(up, settingsDict): msg += "3. copy the setting group, '{group}'\n" up = up.parent() if up and self.no_conflict(up, settingsDict): msg += "4. copy the whole setting category, '{category}'\n" msg = msg.format(specific=setting.lstrip('@'), group=unl.split('-->')[-2].split(':', 1)[0].replace('%20', ' '), category=unl.split('-->')[-3].split(':', 1)[0].replace('%20', ' ')) which = g.app.gui.runAskOkCancelNumberDialog( self.c, "Copy setting?", message=msg) if which is None: return which = int(which) if which > 1: unl = self.copy_to_my_settings(unl, which-1) if unl: g.handleUnl(unl, c=self.c)