Hide keyboard shortcuts

Hot-keys on this page

r m x p   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.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() 

46 # Inits vim mode too. 

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

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

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

50 def printPluginHandlers(self, event=None): 

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

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

53 

54 def printPlugins(self, event=None): 

55 """ 

56 Print the file name responsible for loading a plugin. 

57 

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

59 that enables the plugin. 

60 """ 

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

62 

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

64 def printPluginsInfo(self, event=None): 

65 """ 

66 Print the file name responsible for loading a plugin. 

67 

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

69 that enables the plugin. 

70 """ 

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

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

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

74 def setSilentMode(self, event=None): 

75 """ 

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

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

78 

79 --> set-silent-mode 

80 """ 

81 self.c.k.silentMode = True 

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

83 @cmd('shell-command') 

84 def shellCommand(self, event): 

85 """Execute a shell command.""" 

86 k = self.c.k 

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

88 k.get1Arg(event, self.shellCommand1) 

89 

90 def shellCommand1(self, event): 

91 k = self.c.k 

92 command = g.toUnicode(k.arg) 

93 if command: 

94 self.executeSubprocess(event, command) 

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

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

97 def shellCommandOnRegion(self, event): 

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

99 k = self.c.k 

100 w = self.editWidget(event) 

101 if w: 

102 if w.hasSelection(): 

103 command = w.getSelectedText() 

104 self.executeSubprocess(event, command) 

105 else: 

106 g.es('No text selected') 

107 k.keyboardQuit() 

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

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

110 def actOnNode(self, event): 

111 """ 

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

113 follows:: 

114 

115 import leo.core.leoPlugins 

116 

117 def act_print_upcase(c,p,event): 

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

119 raise leo.core.leoPlugins.TryNext 

120 p.h = p.h.upper() 

121 

122 g.act_on_node.add(act_print_upcase) 

123 

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

125 """ 

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

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

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

129 def shutdown(self, event): 

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

131 g.app.onQuit() 

132 

133 saveBuffersKillLeo = shutdown 

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

135 @cmd('suspend') 

136 def suspend(self, event): 

137 """Minimize the present Leo window.""" 

138 w = self.editWidget(event) 

139 if not w: 

140 return 

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

142 

143 @cmd('iconify-frame') 

144 def iconifyFrame(self, event): 

145 """Minimize the present Leo window.""" 

146 self.suspend(event) 

147 #@-others 

148#@-others 

149#@-leo