]> git.neil.brown.name Git - edlib.git/blob - edlib.c
display-x11-xcb - initial commit
[edlib.git] / edlib.c
1 /*
2  * Copyright Neil Brown ©2015-2021 <neil@brown.name>
3  * May be distributed under terms of GPLv2 - see file:COPYING
4  *
5  * main loop for edlib.
6  */
7
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <locale.h>
11 #include <fcntl.h>
12 #include <wchar.h>
13 #include <dirent.h>
14 #include <string.h>
15 #include <stdio.h>
16
17 #include "core.h"
18 #include "misc.h"
19
20 static char WelcomeText[] =
21         "\n"
22         "Welcome to 'edlib' - a document editor that comes in well defined pieces.\n"
23         "\n"
24         "Current functionality includes:\n"
25         "  splitting and closing windows (C-x 0,1,2,3)\n"
26         "  Resize current window (C-x },{,^)\n"
27         "  Move among windows (C-x o,O  or mouse click)\n"
28         "  Opening a file or directory (C-x C-f)\n"
29         "    TAB performs file-name completion in a menu\n"
30         "  Save files - current one (C-x C-s) or all (C-x s)\n"
31         "  Open a document by name (C-x b) - with TAB completion\n"
32         "  Open a file or document in another pane (C-x 4 C-f,b)\n"
33         "  Kill the current document (C-x k)\n"
34         "  Movement by char, word, line, page, start/end file (standard emacs keys)\n"
35         "  Insert/delete text\n"
36         "  C-_ to undo and redo changes\n"
37         "  C-s to search (incrementally) in text document\n"
38         "  Visit list of documents (C-x C-b)\n"
39         "  Open file from directory list, or document from document list ('f').\n"
40         "  Open file in 'hex' view from directory listing ('h').\n"
41         "  Numeric prefixes with M-0 to M-9.\n"
42         "  Run make (C-c C-m) or grep (M-x grep Return)\n"
43         "\n"
44         "And C-x C-c to close - type 's' to save or '%' to not save in the pop-up\n"
45         "Mouse clicks move the cursor, and clicking on the scroll bar scrolls\n"
46         ;
47
48 static char shortopt[] = "gtx";
49
50 int main(int argc, char *argv[])
51 {
52         struct pane *ed = editor_new();
53         struct pane *first_window = NULL;
54         struct pane *p, *doc = NULL;
55         bool gtk = False, term = False, x11 = False;
56         int opt;
57
58         if (!ed)
59                 exit(1);
60
61         while ((opt = getopt(argc, argv, shortopt)) != EOF) {
62                 switch (opt) {
63                 case 'g': gtk = True;
64                         break;
65                 case 't': term = True;
66                         break;
67                 case 'x': x11 = True;
68                         break;
69                 default:
70                         fprintf(stderr, "Usage: edlib [-g] [-t] [file ...]\n");
71                         exit(2);
72                 }
73         }
74         if (!gtk && !term && !x11)
75                 term = 1;
76
77         setlocale(LC_ALL, "");
78         setlocale(LC_CTYPE, "enUS.UTF-8");
79
80         call("attach-doc-docs", ed);
81         call("global-load-module", ed, 0, NULL, "lib-linecount");
82         call("global-load-module", ed, 0, NULL, "lib-search");
83         call("global-load-module", ed, 0, NULL, "lib-popup");
84         call("global-load-module", ed, 0, NULL, "lang-python");
85         call("global-load-module", ed, 0, NULL, "doc-text");
86         call("global-load-module", ed, 0, NULL, "doc-dir");
87         call("global-load-module", ed, 0, NULL, "render-hex");
88         call("global-load-module", ed, 0, NULL, "render-present");
89         call("global-load-module", ed, 0, NULL, "render-lines");
90         call("global-load-module", ed, 0, NULL, "module-notmuch");
91         call("global-load-module", ed, 0, NULL, "doc-email");
92         call("global-load-module", ed, 0, NULL, "lib-viewer");
93         call("global-load-module", ed, 0, NULL, "lib-qprint");
94         call("global-load-module", ed, 0, NULL, "lib-copybuf");
95         call("global-load-module", ed, 0, NULL, "lib-colourmap");
96         call("global-load-module", ed, 0, NULL, "lib-textfill");
97         call("global-load-module", ed, 0, NULL, "lib-autosave");
98         call("global-load-module", ed, 0, NULL, "render-format");
99
100         call("global-load-module", ed, 0, NULL, "render-c-mode");
101         call("global-load-module", ed, 0, NULL, "lib-make");
102         call("global-load-module", ed, 0, NULL, "lib-server");
103         call("global-load-module", ed, 0, NULL, "lib-utf8");
104         call("global-load-module", ed, 0, NULL, "lib-charset");
105
106         call("global-load-module", ed, 0, NULL, "config");
107
108         while (optind < argc) {
109                 char *file = argv[optind++];
110                 int fd = open(file, O_RDONLY);
111                 if (fd < 0) {
112                         /* '4' says 'allow create' */
113                         doc = call_ret(pane, "doc:open", ed, -1, NULL, file, 4);
114                 } else {
115                         doc = call_ret(pane, "doc:open", ed, fd, NULL, file);
116                         close(fd);
117                 }
118         }
119
120         if (!doc)
121                 doc = call_ret(pane, "doc:from-text", ed, 0, NULL,
122                                "*Welcome*", 0, NULL, WelcomeText);
123         if (!doc) {
124                 fprintf(stderr, "edlib: cannot create default document.\n");
125                 exit(1);
126         }
127
128         if (term) {
129                 char *TERM = getenv("TERM");
130
131                 p = call_ret(pane, "attach-display-ncurses", ed,
132                              0, NULL, "-", 0, NULL, TERM);
133                 if (p) {
134                         char *e;
135                         e = getenv("SSH_CONNECTION");
136                         if (e && *e)
137                                 attr_set_str(&p->attrs, "REMOTE_SESSION", "yes");
138
139                         attr_set_str(&p->attrs, "DISPLAY", getenv("DISPLAY"));
140                         p = call_ret(pane, "editor:activate-display", p);
141                 }
142                 if (p)
143                         p = home_call_ret(pane, doc, "doc:attach-view",
144                                           p, 1);
145                 if (!first_window)
146                         first_window = p;
147                 if (p)
148                         call("Display:set-noclose", p, 1, NULL,
149                              "Cannot close primary display");
150         }
151
152         if (gtk) {
153                 p = call_ret(pane, "attach-display-gtk",
154                              ed, 0, NULL, getenv("DISPLAY"));
155                 if (p)
156                         p = call_ret(pane, "editor:activate-display", p);
157                 if (p)
158                         p = home_call_ret(pane, doc, "doc:attach-view",
159                                           p, 1);
160                 if (!first_window)
161                         first_window = p;
162         }
163
164         if (x11) {
165                 p = call_ret(pane, "attach-display-x11",
166                              ed, 0, NULL, getenv("DISPLAY"));
167                 if (p)
168                         p = call_ret(pane, "editor:activate-display", p);
169                 if (p)
170                         p = home_call_ret(pane, doc, "doc:attach-view",
171                                           p, 1);
172                 if (!first_window)
173                         first_window = p;
174         }
175
176         if (first_window) {
177                 call("global-multicall-startup-", first_window);
178                 time_start(TIME_REFRESH);
179                 pane_refresh(ed);
180                 time_stop(TIME_REFRESH);
181                 while (call("event:run", ed) == 1) {
182                         time_start(TIME_IDLE);
183                         call("global-multicall-on_idle-", ed);
184                         time_stop(TIME_IDLE);
185                         time_start(TIME_REFRESH);
186                         pane_refresh(ed);
187                         time_stop(TIME_REFRESH);
188                 }
189         } else
190                 fprintf(stderr, "edlib: cannot create a display\n");
191         pane_close(ed);
192         exit(0);
193 }