source: global.c @ 10b866d

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 10b866d was 10b866d, checked in by Erik Nygren <nygren@mit.edu>, 22 years ago
* Fixed preservation of e->dotsend across owl_editwin_clear(). * Added history for multiline edit windows (eg, for zephyr composition). The M-n and M-p keys will cycle through the history ring. In particular, it is now possible to edit the command line of a zephyr being composed: C-c it and restart it and then M-p to get the aborted composition back.
  • Property mode set to 100644
File size: 12.2 KB
Line 
1#include <stdio.h>
2#include <unistd.h>
3#include <stdlib.h>
4#include <string.h>
5#include <netdb.h>
6#include <termios.h>
7#include <sys/ioctl.h>
8#include <time.h>
9#include "owl.h"
10
11static const char fileIdent[] = "$Id$";
12
13#ifndef MAXHOSTNAMELEN
14#define MAXHOSTNAMELEN 256
15#endif
16
17void owl_global_init(owl_global *g) {
18  struct hostent *hent;
19  char hostname[MAXHOSTNAMELEN];
20  char buff[MAXPATHLEN];
21
22  gethostname(hostname, MAXHOSTNAMELEN);
23  hent=gethostbyname(hostname);
24  if (!hent) {
25    strcpy(g->thishost, "localhost");
26  } else {
27    strcpy(g->thishost, hent->h_name);
28  }
29
30  owl_context_init(&g->ctx);
31  owl_context_set_startup(&g->ctx);
32  g->curmsg=0;
33  g->topmsg=0;
34  g->needrefresh=1;
35
36  owl_variable_dict_setup(&(g->vars));
37  owl_cmddict_setup(&(g->cmds));
38
39  g->lines=LINES;
40  g->cols=COLS;
41
42  g->rightshift=0;
43
44  owl_editwin_init(&(g->tw), NULL, owl_global_get_typwin_lines(g), g->cols, OWL_EDITWIN_STYLE_ONELINE, NULL);
45
46  owl_keyhandler_init(&g->kh);
47  owl_keys_setup_keymaps(&g->kh);
48
49  owl_list_create(&(g->filterlist));
50  owl_list_create(&(g->puntlist));
51  g->curmsg_vert_offset=0;
52  g->resizepending=0;
53  g->typwinactive=0;
54  g->direction=OWL_DIRECTION_DOWNWARDS;
55  g->zaway=0;
56  if (has_colors()) {
57    g->hascolors=1;
58  }
59  g->colorpairs=COLOR_PAIRS;
60  g->debug=OWL_DEBUG;
61  g->starttime=time(NULL); /* assumes we call init only a start time */
62  strcpy(g->buffercommand, "");
63
64  owl_global_set_config_format(g, 0);
65  owl_global_set_userclue(g, OWL_USERCLUE_NONE);
66  owl_global_set_no_have_config(g);
67  owl_history_init(&(g->msghist));
68  owl_history_init(&(g->cmdhist));
69  g->nextmsgid=0;
70
71  owl_filterelement_create_true(&(g->fe_true));
72  owl_filterelement_create_false(&(g->fe_false));
73  owl_filterelement_create_null(&(g->fe_null));
74
75  _owl_global_setup_windows(g);
76
77  /* Fill in some variables which don't have constant defaults */
78  /* TODO: come back later and check passwd file first */
79  strcpy(g->homedir, getenv("HOME"));
80  sprintf(buff, "%s/zlog/people/", owl_global_get_homedir(g));
81  owl_global_set_logpath(g, buff);
82  sprintf(buff, "%s/zlog/class/", owl_global_get_homedir(g));
83  owl_global_set_classlogpath(g, buff);
84 
85
86  owl_messagelist_create(&(g->msglist));
87  owl_mainwin_init(&(g->mw));
88  owl_popwin_init(&(g->pw));
89}
90
91void _owl_global_setup_windows(owl_global *g) {
92  int lines, cols, typwin_lines;
93
94  lines=g->lines;
95  cols=g->cols;
96  typwin_lines = owl_global_get_typwin_lines(g);
97
98  /* set the new window sizes */
99  g->recwinlines=g->lines-(typwin_lines+2);
100
101  /* create the new windows */
102  g->recwin=newwin(g->recwinlines, cols, 0, 0);
103  g->sepwin=newwin(1, cols, g->recwinlines, 0);
104  g->msgwin=newwin(1, cols, g->recwinlines+1, 0);
105  g->typwin=newwin(typwin_lines, cols, g->recwinlines+2, 0);
106
107  owl_editwin_set_curswin(&(g->tw), g->typwin, typwin_lines, g->cols);
108
109  idlok(g->typwin, FALSE);
110  idlok(g->recwin, FALSE);
111  idlok(g->sepwin, FALSE);
112  idlok(g->msgwin, FALSE);
113
114  nodelay(g->typwin, 1);
115  keypad(g->typwin, TRUE);
116  wmove(g->typwin, 0, 0);
117
118  meta(g->typwin, TRUE);
119}
120
121owl_context *owl_global_get_context(owl_global *g) {
122  return(&g->ctx);
123}
124                         
125int owl_global_get_lines(owl_global *g) {
126  return(g->lines);
127}
128
129int owl_global_get_cols(owl_global *g) {
130  return(g->cols);
131}
132
133int owl_global_get_recwin_lines(owl_global *g) {
134  return(g->recwinlines);
135}
136
137/* curmsg */
138
139int owl_global_get_curmsg(owl_global *g) {
140  return(g->curmsg);
141}
142
143void owl_global_set_curmsg(owl_global *g, int i) {
144  g->curmsg=i;
145  /* we will reset the vertical offset from here */
146  /* we might want to move this out to the functions later */
147  owl_global_set_curmsg_vert_offset(g, 0);
148}
149
150/* topmsg */
151
152int owl_global_get_topmsg(owl_global *g) {
153  return(g->topmsg);
154}
155
156void owl_global_set_topmsg(owl_global *g, int i) {
157  g->topmsg=i;
158}
159
160/* windows */
161
162owl_mainwin *owl_global_get_mainwin(owl_global *g) {
163  return(&(g->mw));
164}
165
166owl_popwin *owl_global_get_popwin(owl_global *g) {
167  return(&(g->pw));
168}
169
170/* msglist */
171
172owl_messagelist *owl_global_get_msglist(owl_global *g) {
173  return(&(g->msglist));
174}
175
176/* keyhandler */
177
178owl_keyhandler *owl_global_get_keyhandler(owl_global *g) {
179  return(&(g->kh));
180}
181
182/* curses windows */
183
184WINDOW *owl_global_get_curs_recwin(owl_global *g) {
185  return(g->recwin);
186}
187
188WINDOW *owl_global_get_curs_sepwin(owl_global *g) {
189  return(g->sepwin);
190}
191
192WINDOW *owl_global_get_curs_msgwin(owl_global *g) {
193  return(g->msgwin);
194}
195
196WINDOW *owl_global_get_curs_typwin(owl_global *g) {
197  return(g->typwin);
198}
199
200/* typwin */
201
202owl_editwin *owl_global_get_typwin(owl_global *g) {
203  return(&(g->tw));
204}
205
206/* buffercommand */
207
208void owl_global_set_buffercommand(owl_global *g, char *command) {
209  strcpy(g->buffercommand, command);
210}
211
212char *owl_global_get_buffercommand(owl_global *g) {
213  return(g->buffercommand);
214}
215
216/* refresh */
217
218int owl_global_is_needrefresh(owl_global *g) {
219  if (g->needrefresh==1) return(1);
220  return(0);
221}
222
223void owl_global_set_needrefresh(owl_global *g) {
224  g->needrefresh=1;
225}
226
227void owl_global_set_noneedrefresh(owl_global *g) {
228  g->needrefresh=0;
229}
230
231/* variable dictionary */
232
233owl_vardict *owl_global_get_vardict(owl_global *g) {
234  return &(g->vars);
235}
236
237/* command dictionary */
238
239owl_cmddict *owl_global_get_cmddict(owl_global *g) {
240  return &(g->cmds);
241}
242
243/* rightshift */
244
245void owl_global_set_rightshift(owl_global *g, int i) {
246  g->rightshift=i;
247}
248
249int owl_global_get_rightshift(owl_global *g) {
250  return(g->rightshift);
251}
252
253/* typwin */
254
255int owl_global_is_typwin_active(owl_global *g) {
256  if (g->typwinactive==1) return(1);
257  return(0);
258}
259
260void owl_global_set_typwin_active(owl_global *g) {
261  g->typwinactive=1;
262}
263
264void owl_global_set_typwin_inactive(owl_global *g) {
265  g->typwinactive=0;
266}
267
268/* resize */
269
270void owl_global_set_resize_pending(owl_global *g) {
271  g->resizepending=1;
272}
273
274char *owl_global_get_homedir(owl_global *g) {
275  return(g->homedir);
276}
277
278int owl_global_get_direction(owl_global *g) {
279  return(g->direction);
280}
281
282void owl_global_set_direction_downwards(owl_global *g) {
283  g->direction=OWL_DIRECTION_DOWNWARDS;
284}
285
286void owl_global_set_direction_upwards(owl_global *g) {
287  g->direction=OWL_DIRECTION_UPWARDS;
288}
289
290/* perl stuff */
291
292void owl_global_set_perlinterp(owl_global *g, void *p) {
293  g->perl=p;
294}
295
296void *owl_global_get_perlinterp(owl_global *g) {
297  return(g->perl);
298}
299
300int owl_global_is_config_format(owl_global *g) {
301  if (g->config_format) return(1);
302  return(0);
303}
304
305void owl_global_set_config_format(owl_global *g, int state) {
306  if (state==1) {
307    g->config_format=1;
308  } else {
309    g->config_format=0;
310  }
311}
312
313void owl_global_set_have_config(owl_global *g) {
314  g->haveconfig=1;
315}
316
317void owl_global_set_no_have_config(owl_global *g) {
318  g->haveconfig=0;
319}
320
321int owl_global_have_config(owl_global *g) {
322  if (g->haveconfig) return(1);
323  return(0);
324}
325
326void owl_global_resize(owl_global *g, int x, int y) {
327  /* resize the screen.  If x or y is 0 use the terminal size */
328  struct winsize size;
329   
330  if (!g->resizepending) return;
331
332  /* delete the current windows */
333  delwin(g->recwin);
334  delwin(g->sepwin);
335  delwin(g->msgwin);
336  delwin(g->typwin);
337  if (!isendwin()) {
338    endwin();
339  }
340
341  refresh();
342
343  /* get the new size */
344  ioctl(STDIN_FILENO, TIOCGWINSZ, &size);
345  if (x==0) {
346    g->lines=size.ws_row;
347  } else {
348    g->lines=x;
349  }
350
351  if (y==0) {
352    g->cols=size.ws_col;
353  } else {
354    g->cols=y;
355  }
356
357  /* resizeterm(size.ws_row, size.ws_col); */
358
359  /* re-initialize the windows */
360  _owl_global_setup_windows(g);
361
362  /* refresh stuff */
363  g->needrefresh=1;
364  owl_mainwin_redisplay(&(g->mw));
365  sepbar(NULL);
366
367  if (owl_global_is_typwin_active(g)) {
368    owl_editwin_redisplay(&(g->tw), 0);
369  }     
370  /* TODO: this should handle other forms of popwins */
371  if (owl_popwin_is_active(owl_global_get_popwin(g)) 
372      && owl_global_get_viewwin(g)) {
373    owl_popwin_refresh(owl_global_get_popwin(g));
374    owl_viewwin_redisplay(owl_global_get_viewwin(g), 0);
375  }
376
377  /*
378  char buff[1024];
379  sprintf(buff, "New size is %i lines, %i cols.\n", size.ws_row, size.ws_col);
380  owl_function_makemsg(buff);
381  */
382  owl_function_makemsg("");
383
384  g->resizepending=0;
385}
386
387/* tty (not fully implemented yet) */
388
389void owl_global_set_tty(owl_global *g, char *tty) {
390  if (tty) {
391    strcpy(g->thistty, tty);
392  } else if (getenv("DISPLAY")) {
393    strcpy(g->thistty, getenv("DISPLAY"));
394  } else if (ttyname(fileno(stdout))) {
395    strcpy(g->thistty, ttyname(fileno(stdout)));
396    if (!strncmp(g->thistty, "/dev/", 5)) {
397      strcpy(g->thistty, g->thistty+5);
398    }
399  } else {
400    strcpy(g->thistty, "unknown");
401  }
402   
403#ifdef HAVE_LIBZEPHYR_ZINITLOCATIONINFO
404  ZInitLocationInfo(g->thishost, g->thistty); 
405#endif
406}
407
408
409/* debug */
410
411int owl_global_is_debug_fast(owl_global *g) {
412  if (g->debug) return(1);
413  return(0);
414}
415
416/* starttime */
417
418time_t owl_global_get_starttime(owl_global *g) {
419  return(g->starttime);
420}
421
422time_t owl_global_get_runtime(owl_global *g) {
423  return(time(NULL)-g->starttime);
424}
425
426void owl_global_get_runtime_string(owl_global *g, char *buff) {
427  time_t diff;
428
429  diff=time(NULL)-owl_global_get_starttime(g);
430
431  /* print something nicer later */   
432  sprintf(buff, "%i seconds", (int) diff);
433}
434
435/* userclue */
436
437void owl_global_set_userclue(owl_global *g, int clue) {
438  g->userclue=clue;
439}
440
441void owl_global_add_userclue(owl_global *g, int clue) {
442  g->userclue|=clue;
443}
444
445int owl_global_get_userclue(owl_global *g) {
446  return(g->userclue);
447}
448
449int owl_global_is_userclue(owl_global *g, int clue) {
450  if (g->userclue & clue) return(1);
451  return(0);
452}
453
454/* viewwin */
455
456owl_viewwin *owl_global_get_viewwin(owl_global *g) {
457  return(&(g->vw));
458}
459
460
461/* vert offset */
462
463int owl_global_get_curmsg_vert_offset(owl_global *g) {
464  return(g->curmsg_vert_offset);
465}
466
467void owl_global_set_curmsg_vert_offset(owl_global *g, int i) {
468  g->curmsg_vert_offset=i;
469}
470
471/* startup args */
472
473void owl_global_set_startupargs(owl_global *g, int argc, char **argv) {
474  int i;
475
476  strcpy(g->startupargs, "");
477  for (i=0; i<argc; i++) {
478    sprintf(g->startupargs, "%s%s ", g->startupargs, argv[i]);
479  }
480  g->startupargs[strlen(g->startupargs)-1]='\0';
481}
482
483char *owl_global_get_startupargs(owl_global *g) {
484  return(g->startupargs);
485}
486
487/* history */
488
489owl_history *owl_global_get_msg_history(owl_global *g) {
490  return(&(g->msghist));
491}
492
493owl_history *owl_global_get_cmd_history(owl_global *g) {
494  return(&(g->cmdhist));
495}
496
497/* filterlist */
498
499owl_list *owl_global_get_filterlist(owl_global *g) {
500  return(&(g->filterlist));
501}
502
503owl_filter *owl_global_get_filter(owl_global *g, char *name) {
504  int i, j;
505  owl_filter *f;
506
507  j=owl_list_get_size(&(g->filterlist));
508  for (i=0; i<j; i++) {
509    f=owl_list_get_element(&(g->filterlist), i);
510    if (!strcmp(name, owl_filter_get_name(f))) {
511      return(f);
512    }
513  }
514  return(NULL);
515}
516
517void owl_global_add_filter(owl_global *g, owl_filter *f) {
518  owl_list_append_element(&(g->filterlist), f);
519}
520
521void owl_global_remove_filter(owl_global *g, char *name) {
522  int i, j;
523  owl_filter *f;
524
525  j=owl_list_get_size(&(g->filterlist));
526  for (i=0; i<j; i++) {
527    f=owl_list_get_element(&(g->filterlist), i);
528    if (!strcmp(name, owl_filter_get_name(f))) {
529      owl_filter_free(f);
530      owl_list_remove_element(&(g->filterlist), i);
531      break;
532    }
533  }
534}
535
536/* nextmsgid */
537
538int owl_global_get_nextmsgid(owl_global *g) {
539  return(g->nextmsgid++);
540}
541
542/* current view */
543
544owl_view *owl_global_get_current_view(owl_global *g) {
545  return(&(g->current_view));
546}
547
548owl_filterelement *owl_global_get_filterelement_true(owl_global *g) {
549  return(&(g->fe_true));
550}
551
552owl_filterelement *owl_global_get_filterelement_false(owl_global *g) {
553  return(&(g->fe_false));
554}
555
556owl_filterelement *owl_global_get_filterelement_null(owl_global *g) {
557  return(&(g->fe_null));
558}
559
560/* has colors */
561
562int owl_global_get_hascolors(owl_global *g) {
563  if (g->hascolors) return(1);
564  return(0);
565}
566
567/* color pairs */
568
569int owl_global_get_colorpairs(owl_global *g) {
570  return(g->colorpairs);
571}
572
573/* puntlist */
574
575owl_list *owl_global_get_puntlist(owl_global *g) {
576  return(&(g->puntlist));
577}
578
579int owl_global_message_is_puntable(owl_global *g, owl_message *m) {
580  owl_list *pl;
581  int i, j;
582
583  pl=owl_global_get_puntlist(g);
584  j=owl_list_get_size(pl);
585  for (i=0; i<j; i++) {
586    if (owl_filter_message_match(owl_list_get_element(pl, i), m)) return(1);
587  }
588  return(0);
589}
590
591int owl_global_should_followlast(owl_global *g) {
592  owl_view *v;
593 
594  if (!owl_global_is__followlast(g)) return(0);
595 
596  v=owl_global_get_current_view(g);
597 
598  if (owl_global_get_curmsg(g)==owl_view_get_size(v)-1) return(1);
599  return(0);
600}
Note: See TracBrowser for help on using the repository browser.