|
2 | 2 | UI boxes for entering text: WriteBox, MessageSearchBox, PanelSearchBox
|
3 | 3 | """
|
4 | 4 |
|
| 5 | +import os |
5 | 6 | import re
|
| 7 | +import shutil |
| 8 | +import subprocess |
6 | 9 | import unicodedata
|
7 | 10 | from collections import Counter
|
8 | 11 | from datetime import datetime, timedelta
|
| 12 | +from tempfile import NamedTemporaryFile |
9 | 13 | from time import sleep
|
10 | 14 | from typing import Any, Callable, Dict, List, NamedTuple, Optional, Tuple
|
11 | 15 |
|
@@ -810,6 +814,40 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
|
810 | 814 | elif is_command_key("MARKDOWN_HELP", key):
|
811 | 815 | self.view.controller.show_markdown_help()
|
812 | 816 | return key
|
| 817 | + elif is_command_key("OPEN_EXTERNAL_EDITOR", key): |
| 818 | + editor = os.environ.get("ZULIP_EDITOR_COMMAND", os.environ.get("EDITOR")) |
| 819 | + if editor is None: |
| 820 | + self.view.controller.report_error( |
| 821 | + "Configure $EDITOR or $ZULIP_EDITOR_COMMAND shell environment." |
| 822 | + ) |
| 823 | + return key |
| 824 | + editor_splits = editor.split(" ") |
| 825 | + if len(editor_splits) >= 1: |
| 826 | + fullpath_program = shutil.which(editor_splits[0]) |
| 827 | + if fullpath_program is None: |
| 828 | + self.view.controller.report_error( |
| 829 | + "Editor program not found, check $EDITOR " |
| 830 | + "or $ZULIP_EDITOR_COMMAND." |
| 831 | + ) |
| 832 | + return key |
| 833 | + editor_splits[0] = fullpath_program |
| 834 | + edit_tempfile = NamedTemporaryFile(suffix=".md") |
| 835 | + with open(edit_tempfile.name, mode="w") as edit_writer: |
| 836 | + edit_writer.write(self.msg_write_box.edit_text) |
| 837 | + self.view.controller.loop.screen.stop() |
| 838 | + if "TEMPFILE" not in editor: |
| 839 | + editor_splits.append(edit_tempfile.name) |
| 840 | + else: |
| 841 | + editor_splits = [ |
| 842 | + part if part != "TEMPFILE" else edit_tempfile.name |
| 843 | + for part in editor_splits |
| 844 | + ] |
| 845 | + subprocess.call(editor_splits) |
| 846 | + with open(edit_tempfile.name, mode="r") as edit_reader: |
| 847 | + self.msg_write_box.edit_text = edit_reader.read() |
| 848 | + edit_tempfile.close() |
| 849 | + self.view.controller.loop.screen.start() |
| 850 | + return key |
813 | 851 | elif is_command_key("SAVE_AS_DRAFT", key):
|
814 | 852 | if self.msg_edit_state is None:
|
815 | 853 | if self.compose_box_status == "open_with_private":
|
|
0 commit comments