Skip to content

Commit 3e38966

Browse files
authored
Merge pull request #1350 from scresto09/vimode-fold-support-fix
Vimode: implement fold in vimode plugin
2 parents 7da7553 + 5c74ce1 commit 3e38966

File tree

5 files changed

+187
-2
lines changed

5 files changed

+187
-2
lines changed

vimode/README

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ This is an incomplete list of known limitations of the plugin:
9696
* named registers and related commands are not implemented
9797
* Ctrl+X mode is not implemented
9898
* marks are not implemented
99-
* fold commands are not implemented
99+
* many fold commands are not implemented
100100
* most commands starting with "'", "z", and "g" are not implemented
101101
* most ex mode commands are not implemented (excluding basic stuff like search,
102102
replace, saving, etc.)
@@ -501,7 +501,16 @@ a new command, please do not forget to update the table below.::
501501
cursor on first non-blank
502502
z. z. redraw, cursor line to center of window,
503503
cursor on first non-blank
504+
zA zA open a closed fold or close an open fold
505+
recursively
506+
zC zC close folds recursively
507+
zM zM set 'foldlevel' to zero
508+
zO zO open folds recursively
509+
zR zR set 'foldlevel' to the deepest fold
510+
za za open a closed fold, close an open fold
504511
zb zb redraw, cursor line at bottom of window
512+
zc zc close a fold
513+
zo zo open fold
505514
zt zt redraw, cursor line at top of window
506515
zz zz redraw, cursor line at center of window
507516

vimode/src/Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ vi_srcs = \
3737
cmds/excmds.h \
3838
cmds/excmds.c \
3939
cmds/undo.h \
40-
cmds/undo.c
40+
cmds/undo.c \
41+
cmds/fold.h \
42+
cmds/fold.c
4143

4244
vimode_la_SOURCES = \
4345
backends/backend-geany.c \

vimode/src/cmd-runner.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "cmds/changemode.h"
2626
#include "cmds/edit.h"
2727
#include "cmds/special.h"
28+
#include "cmds/fold.h"
2829

2930
#include <gdk/gdkkeysyms.h>
3031

@@ -262,6 +263,15 @@ CmdDef text_object_cmds[] = {
262263
{cmd_copy_line, GDK_KEY_Y, 0, 0, 0, FALSE, FALSE}, \
263264
{cmd_paste_after, GDK_KEY_p, 0, 0, 0, FALSE, FALSE}, \
264265
{cmd_paste_before, GDK_KEY_P, 0, 0, 0, FALSE, FALSE}, \
266+
/* fold */ \
267+
{cmd_toggle_fold, GDK_KEY_z, GDK_KEY_a, 0, 0, FALSE, FALSE}, \
268+
{cmd_open_fold, GDK_KEY_z, GDK_KEY_o, 0, 0, FALSE, FALSE}, \
269+
{cmd_close_fold, GDK_KEY_z, GDK_KEY_c, 0, 0, FALSE, FALSE}, \
270+
{cmd_toggle_fold_child, GDK_KEY_z, GDK_KEY_A, 0, 0, FALSE, FALSE}, \
271+
{cmd_open_fold_child, GDK_KEY_z, GDK_KEY_O, 0, 0, FALSE, FALSE}, \
272+
{cmd_close_fold_child, GDK_KEY_z, GDK_KEY_C, 0, 0, FALSE, FALSE}, \
273+
{cmd_open_fold_all, GDK_KEY_z, GDK_KEY_R, 0, 0, FALSE, FALSE}, \
274+
{cmd_close_fold_all, GDK_KEY_z, GDK_KEY_M, 0, 0, FALSE, FALSE}, \
265275
/* changing text */ \
266276
{cmd_enter_insert_cut_line, GDK_KEY_c, GDK_KEY_c, 0, 0, FALSE, FALSE}, \
267277
{cmd_enter_insert_cut_line, GDK_KEY_S, 0, 0, 0, FALSE, FALSE}, \

vimode/src/cmds/fold.c

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright 2024 Sylvain Cresto <scresto@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
19+
#include "cmds/fold.h"
20+
#include "utils.h"
21+
22+
#define GOTO_NEAREST_PARENT 0
23+
#define GOTO_TOPMOST_PARENT 1
24+
#define GOTO_CONTRACTED_PARENT 2
25+
26+
static gint goto_above_fold(CmdParams *p, gint type)
27+
{
28+
/* foldparent of the next line */
29+
gint line = SSM(p->sci, SCI_GETFOLDPARENT, p->line + 1, 0);
30+
31+
if (p->line == line)
32+
; /* we are already on the fold point line */
33+
else
34+
{
35+
/* foldparent of the current line */
36+
line = SSM(p->sci, SCI_GETFOLDPARENT, p->line, 0);
37+
}
38+
39+
/* retreive first parent when type != GOTO_NEAREST_PARENT
40+
when type == GOTO_CONTRACTED_PARENT we stop on first contracted parent if exist
41+
*/
42+
if (type == GOTO_CONTRACTED_PARENT && line != -1 && ! SSM(p->sci, SCI_GETFOLDEXPANDED, line, 0))
43+
; /* this fold point is contracted and type == GOTO_CONTRACTED_PARENT */
44+
else if (type != GOTO_NEAREST_PARENT)
45+
{
46+
gint prev_line = line;
47+
while (prev_line != -1)
48+
{
49+
prev_line = SSM(p->sci, SCI_GETFOLDPARENT, prev_line, 0);
50+
if (prev_line != -1)
51+
{
52+
line = prev_line;
53+
if (type == GOTO_CONTRACTED_PARENT && ! SSM(p->sci, SCI_GETFOLDEXPANDED, line, 0))
54+
break;
55+
}
56+
}
57+
}
58+
59+
if (line != -1)
60+
{
61+
/* move the cursor on the visible line before the fold */
62+
gint pos = SSM(p->sci, SCI_POSITIONFROMLINE, line, 0);
63+
SET_POS_NOX(p->sci, pos, TRUE);
64+
}
65+
66+
return line;
67+
}
68+
69+
70+
void cmd_toggle_fold(CmdContext *c, CmdParams *p)
71+
{
72+
gint line = goto_above_fold(p, GOTO_NEAREST_PARENT);
73+
if (line != -1)
74+
SSM(p->sci, SCI_FOLDLINE, (uptr_t) line, SC_FOLDACTION_TOGGLE);
75+
}
76+
77+
78+
void cmd_open_fold(CmdContext *c, CmdParams *p)
79+
{
80+
gint line = goto_above_fold(p, GOTO_NEAREST_PARENT);
81+
if (line != -1)
82+
SSM(p->sci, SCI_FOLDLINE, (uptr_t) line, SC_FOLDACTION_EXPAND);
83+
}
84+
85+
86+
void cmd_close_fold(CmdContext *c, CmdParams *p)
87+
{
88+
gint line = goto_above_fold(p, GOTO_NEAREST_PARENT);
89+
if (line != -1)
90+
SSM(p->sci, SCI_FOLDLINE, (uptr_t) line, SC_FOLDACTION_CONTRACT);
91+
}
92+
93+
94+
void cmd_toggle_fold_child(CmdContext *c, CmdParams *p)
95+
{
96+
gint line = goto_above_fold(p, GOTO_CONTRACTED_PARENT);
97+
if (line != -1)
98+
SSM(p->sci, SCI_FOLDCHILDREN, (uptr_t) line, SC_FOLDACTION_TOGGLE);
99+
}
100+
101+
102+
void cmd_open_fold_child(CmdContext *c, CmdParams *p)
103+
{
104+
gint line = goto_above_fold(p, GOTO_NEAREST_PARENT);
105+
SSM(p->sci, SCI_FOLDCHILDREN, (uptr_t) line, SC_FOLDACTION_EXPAND);
106+
}
107+
108+
109+
void cmd_close_fold_child(CmdContext *c, CmdParams *p)
110+
{
111+
gint line = goto_above_fold(p, GOTO_TOPMOST_PARENT);
112+
if (line != -1)
113+
SSM(p->sci, SCI_FOLDCHILDREN, (uptr_t) line, SC_FOLDACTION_CONTRACT);
114+
}
115+
116+
117+
void cmd_open_fold_all(CmdContext *c, CmdParams *p)
118+
{
119+
SSM(p->sci, SCI_FOLDALL, SC_FOLDACTION_EXPAND | SC_FOLDACTION_CONTRACT_EVERY_LEVEL, 0);
120+
}
121+
122+
123+
void cmd_close_fold_all(CmdContext *c, CmdParams *p)
124+
{
125+
goto_above_fold(p, GOTO_TOPMOST_PARENT);
126+
SSM(p->sci, SCI_FOLDALL, SC_FOLDACTION_CONTRACT | SC_FOLDACTION_CONTRACT_EVERY_LEVEL, 0);
127+
}

vimode/src/cmds/fold.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2024 Sylvain Cresto <scresto@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
19+
#ifndef __VIMODE_CMDS_FOLD_H__
20+
#define __VIMODE_CMDS_FOLD_H__
21+
22+
#include "context.h"
23+
#include "cmd-params.h"
24+
25+
void cmd_toggle_fold(CmdContext *c, CmdParams *p);
26+
void cmd_open_fold(CmdContext *c, CmdParams *p);
27+
void cmd_close_fold(CmdContext *c, CmdParams *p);
28+
29+
void cmd_toggle_fold_child(CmdContext *c, CmdParams *p);
30+
void cmd_open_fold_child(CmdContext *c, CmdParams *p);
31+
void cmd_close_fold_child(CmdContext *c, CmdParams *p);
32+
33+
void cmd_toggle_fold_all(CmdContext *c, CmdParams *p);
34+
void cmd_open_fold_all(CmdContext *c, CmdParams *p);
35+
void cmd_close_fold_all(CmdContext *c, CmdParams *p);
36+
37+
#endif

0 commit comments

Comments
 (0)