Coverage for C:\Repos\leo-editor\leo\commands\controlCommands.py: 46%

70 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-24 10:21 -0500

1# -*- coding: utf-8 -*- 

2#@+leo-ver=5-thin 

3#@+node:ekr.20150514040100.1: * @file ../commands/controlCommands.py 

4#@@first 

5"""Leo's control commands.""" 

6#@+<< imports >> 

7#@+node:ekr.20150514050127.1: ** << imports >> (controlCommands.py) 

8import shlex 

9import subprocess 

10from leo.core import leoGlobals as g 

11from leo.commands.baseCommands import BaseEditCommandsClass 

12#@-<< imports >> 

13 

14def cmd(name): 

15 """Command decorator for the ControlCommandsClass class.""" 

16 return g.new_cmd_decorator(name, ['c', 'controlCommands',]) 

17 

18#@+others 

19#@+node:ekr.20160514095828.1: ** class ControlCommandsClass 

20class ControlCommandsClass(BaseEditCommandsClass): 

21 

22 def __init__(self, c): 

23 """Ctor for ControlCommandsClass.""" 

24 # pylint: disable=super-init-not-called 

25 self.c = c 

26 #@+others 

27 #@+node:ekr.20150514063305.91: *3* executeSubprocess 

28 def executeSubprocess(self, event, command): 

29 """Execute a command in a separate process.""" 

30 trace = False 

31 import sys 

32 k = self.c.k 

33 try: 

34 p = subprocess.Popen( 

35 shlex.split(command), 

36 stdout=subprocess.PIPE, 

37 stderr=subprocess.DEVNULL if trace else subprocess.PIPE, 

38 shell=sys.platform.startswith('win'), 

39 ) 

40 out, err = p.communicate() 

41 for line in g.splitLines(out): # type:ignore 

42 g.es_print(g.toUnicode(line.rstrip())) 

43 except Exception: 

44 g.es_exception() 

45 k.keyboardQuit() # Inits vim mode too. 

46 g.es(f"Done: {command}") 

47 #@+node:ekr.20150514063305.92: *3* print plugins info... 

48 @cmd('show-plugin-handlers') 

49 def printPluginHandlers(self, event=None): 

50 """Print the handlers for each plugin.""" 

51 g.app.pluginsController.printHandlers(self.c) 

52 

53 def printPlugins(self, event=None): 

54 """ 

55 Print the file name responsible for loading a plugin. 

56 

57 This is the first .leo file containing an @enabled-plugins node 

58 that enables the plugin. 

59 """ 

60 g.app.pluginsController.printPlugins(self.c) 

61 

62 @cmd('show-plugins-info') 

63 def printPluginsInfo(self, event=None): 

64 """ 

65 Print the file name responsible for loading a plugin. 

66 

67 This is the first .leo file containing an @enabled-plugins node 

68 that enables the plugin. 

69 """ 

70 g.app.pluginsController.printPluginsInfo(self.c) 

71 #@+node:ekr.20150514063305.93: *3* setSilentMode 

72 @cmd('set-silent-mode') 

73 def setSilentMode(self, event=None): 

74 """ 

75 Set the mode to be run silently, without the minibuffer. 

76 The only use for this command is to put the following in an @mode node:: 

77 

78 --> set-silent-mode 

79 """ 

80 self.c.k.silentMode = True 

81 #@+node:ekr.20150514063305.94: *3* shellCommand (improved) 

82 @cmd('shell-command') 

83 def shellCommand(self, event): 

84 """Execute a shell command.""" 

85 k = self.c.k 

86 k.setLabelBlue('shell-command: ') 

87 k.get1Arg(event, self.shellCommand1) 

88 

89 def shellCommand1(self, event): 

90 k = self.c.k 

91 command = g.toUnicode(k.arg) 

92 if command: 

93 self.executeSubprocess(event, command) 

94 #@+node:ekr.20150514063305.95: *3* shellCommandOnRegion 

95 @cmd('shell-command-on-region') 

96 def shellCommandOnRegion(self, event): 

97 """Execute a command taken from the selected text in a separate process.""" 

98 k = self.c.k 

99 w = self.editWidget(event) 

100 if w: 

101 if w.hasSelection(): 

102 command = w.getSelectedText() 

103 self.executeSubprocess(event, command) 

104 else: 

105 g.es('No text selected') 

106 k.keyboardQuit() 

107 #@+node:ekr.20150514063305.96: *3* actOnNode 

108 @cmd('act-on-node') 

109 def actOnNode(self, event): 

110 """ 

111 Executes node-specific action, typically defined in a plugins as 

112 follows:: 

113 

114 import leo.core.leoPlugins 

115 

116 def act_print_upcase(c,p,event): 

117 if not p.h.startswith('@up'): 

118 raise leo.core.leoPlugins.TryNext 

119 p.h = p.h.upper() 

120 

121 g.act_on_node.add(act_print_upcase) 

122 

123 This will upcase the headline when it starts with ``@up``. 

124 """ 

125 g.act_on_node(self.c, self.c.p, event) 

126 #@+node:ekr.20150514063305.97: *3* shutdown, saveBuffersKillEmacs & setShutdownHook 

127 @cmd('save-buffers-kill-leo') 

128 def shutdown(self, event): 

129 """Quit Leo, prompting to save any unsaved files first.""" 

130 g.app.onQuit() 

131 

132 saveBuffersKillLeo = shutdown 

133 #@+node:ekr.20150514063305.98: *3* suspend & iconifyFrame 

134 @cmd('suspend') 

135 def suspend(self, event): 

136 """Minimize the present Leo window.""" 

137 w = self.editWidget(event) 

138 if not w: 

139 return 

140 self.c.frame.top.iconify() 

141 

142 @cmd('iconify-frame') 

143 def iconifyFrame(self, event): 

144 """Minimize the present Leo window.""" 

145 self.suspend(event) 

146 #@-others 

147#@-others 

148#@-leo