|
| 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 | +} |
0 commit comments