File: window.c

package info (click to toggle)
zile 2.3.21-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,640 kB
  • sloc: ansic: 40,438; sh: 4,811; lisp: 575; makefile: 185; perl: 78
file content (329 lines) | stat: -rw-r--r-- 7,194 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* Window handling functions

   Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2008, 2009 Free Software Foundation, Inc.

   This file is part of GNU Zile.

   GNU Zile is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Zile is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Zile; see the file COPYING.  If not, write to the
   Free Software Foundation, Fifth Floor, 51 Franklin Street, Boston,
   MA 02111-1301, USA.  */

#include "config.h"

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "main.h"
#include "extern.h"

/*
 * Structure
 */
struct Window
{
#define FIELD(ty, name) ty name;
#include "window.h"
#undef FIELD
};

#define FIELD(ty, field)                        \
  GETTER (Window, window, ty, field)            \
  SETTER (Window, window, ty, field)

#include "window.h"
#undef FIELD

static Window *
window_new (void)
{
  return (Window *) XZALLOC (Window);
}

/*
 * Set the current window and his buffer as the current buffer.
 */
void
set_current_window (Window * wp)
{
  /* Save buffer's point in a new marker.  */
  if (cur_wp->saved_pt)
    free_marker (cur_wp->saved_pt);

  cur_wp->saved_pt = point_marker ();

  /* Change the current window.  */
  cur_wp = wp;

  /* Change the current buffer.  */
  cur_bp = wp->bp;

  /* Update the buffer point with the window's saved point
     marker.  */
  if (cur_wp->saved_pt)
    {
      set_buffer_pt (cur_bp, get_marker_pt (cur_wp->saved_pt));
      free_marker (cur_wp->saved_pt);
      cur_wp->saved_pt = NULL;
    }
}

DEFUN ("split-window", split_window)
/*+
Split current window into two windows, one above the other.
Both windows display the same buffer now current.
+*/
{
  Window *newwp;

  /* Windows smaller than 4 lines cannot be split. */
  if (cur_wp->fheight < 4)
    {
      minibuf_error ("Window height %d too small for splitting",
                     cur_wp->fheight);
      return leNIL;
    }

  newwp = window_new ();
  newwp->fwidth = cur_wp->fwidth;
  newwp->ewidth = cur_wp->ewidth;
  newwp->fheight = cur_wp->fheight / 2 + cur_wp->fheight % 2;
  newwp->eheight = newwp->fheight - 1;
  cur_wp->fheight = cur_wp->fheight / 2;
  cur_wp->eheight = cur_wp->fheight - 1;
  if (cur_wp->topdelta >= cur_wp->eheight)
    recenter (cur_wp);
  newwp->bp = cur_wp->bp;
  newwp->saved_pt = point_marker ();
  newwp->next = cur_wp->next;
  cur_wp->next = newwp;
}
END_DEFUN

void
delete_window (Window * del_wp)
{
  Window *wp;

  if (del_wp == head_wp)
    wp = head_wp = head_wp->next;
  else
    for (wp = head_wp; wp != NULL; wp = wp->next)
      if (wp->next == del_wp)
        {
          wp->next = wp->next->next;
          break;
        }

  if (wp != NULL)
    {
      wp->fheight += del_wp->fheight;
      wp->eheight += del_wp->eheight + 1;
      set_current_window (wp);
    }

  if (del_wp->saved_pt)
    free_marker (del_wp->saved_pt);

  free (del_wp);
}

DEFUN ("delete-window", delete_window)
/*+
Remove the current window from the screen.
+*/
{
  if (cur_wp == head_wp && cur_wp->next == NULL)
    {
      minibuf_error ("Attempt to delete sole ordinary window");
      return leNIL;
    }

  delete_window (cur_wp);
}
END_DEFUN

DEFUN ("enlarge-window", enlarge_window)
/*+
Make current window one line bigger.
+*/
{
  Window *wp;

  if (cur_wp == head_wp && cur_wp->next == NULL)
    return leNIL;

  wp = cur_wp->next;
  if (wp == NULL || wp->fheight < 3)
    for (wp = head_wp; wp != NULL; wp = wp->next)
      if (wp->next == cur_wp)
        {
          if (wp->fheight < 3)
            return leNIL;
          break;
        }

  if (cur_wp == head_wp && cur_wp->next->fheight < 3)
    return leNIL;

  --wp->fheight;
  --wp->eheight;
  if (wp->topdelta >= wp->eheight)
    recenter (wp);
  ++cur_wp->fheight;
  ++cur_wp->eheight;
}
END_DEFUN

DEFUN ("shrink-window", shrink_window)
/*+
Make current window one line smaller.
+*/
{
  Window *wp;

  if ((cur_wp == head_wp && cur_wp->next == NULL) || cur_wp->fheight < 3)
    return leNIL;

  wp = cur_wp->next;
  if (wp == NULL)
    {
      for (wp = head_wp; wp != NULL; wp = wp->next)
        if (wp->next == cur_wp)
          break;
    }

  ++wp->fheight;
  ++wp->eheight;
  --cur_wp->fheight;
  --cur_wp->eheight;
  if (cur_wp->topdelta >= cur_wp->eheight)
    recenter (cur_wp);
}
END_DEFUN

Window *
popup_window (void)
{
  if (head_wp->next == NULL)
    {
      /* There is only one window on the screen, so split it. */
      FUNCALL (split_window);
      return cur_wp->next;
    }

  /* Use the window after the current one. */
  if (cur_wp->next != NULL)
    return cur_wp->next;

  /* Use the first window. */
  return head_wp;
}

DEFUN ("delete-other-windows", delete_other_windows)
/*+
Make the selected window fill the screen.
+*/
{
  Window *wp, *nextwp;

  for (wp = head_wp; wp != NULL; wp = nextwp)
    {
      nextwp = wp->next;
      if (wp != cur_wp)
        delete_window (wp);
    }
}
END_DEFUN

DEFUN ("other-window", other_window)
/*+
Select the first different window on the screen.
All windows are arranged in a cyclic order.
This command selects the window one step away in that order.
+*/
{
  set_current_window ((cur_wp->next != NULL) ? cur_wp->next : head_wp);
}
END_DEFUN

/*
 * This function creates the scratch buffer and window when there are
 * no other windows (and possibly no other buffers).
 */
void
create_scratch_window (void)
{
  Window *wp;
  Buffer *bp = create_scratch_buffer ();

  wp = window_new ();
  cur_wp = head_wp = wp;
  wp->fwidth = wp->ewidth = term_width ();
  /* Save space for minibuffer. */
  wp->fheight = term_height () - 1;
  /* Save space for status line. */
  wp->eheight = wp->fheight - 1;
  wp->bp = cur_bp = bp;
}

Window *
find_window (const char *name)
{
  Window *wp;

  for (wp = head_wp; wp != NULL; wp = wp->next)
    if (!strcmp (get_buffer_name (wp->bp), name))
      return wp;

  return NULL;
}

Point
window_pt (Window * wp)
{
  /* The current window uses the current buffer point; all other
     windows have a saved point, except that if a window has just been
     killed, it needs to use its new buffer's current point. */
  assert (wp != NULL);
  if (wp == cur_wp)
    {
      assert (wp->bp == cur_bp);
      assert (wp->saved_pt == NULL);
      assert (cur_bp);
      return get_buffer_pt (cur_bp);
    }
  else
    {
      if (wp->saved_pt != NULL)
        return get_marker_pt (wp->saved_pt);
      else
        return get_buffer_pt (wp->bp);
    }
}

bool
window_top_visible (Window * wp)
{
  return window_pt (wp).n == get_window_topdelta (wp);
}

bool
window_bottom_visible (Window * wp)
{
  return window_pt (wp).n + (get_window_eheight (wp) - get_window_topdelta (wp)) >
    get_buffer_last_line (get_window_bp (wp));
}