Supported commands are: za / zo / zc toggle / open / close fold on one level of folding zA / zO / zC toggle / open / close fold on all folding levels zR / zM open / close all folds You can view, comment on, or merge this pull request online at:
https://github.com/geany/geany-plugins/pull/1327
-- Commit Summary --
* Implement fold in vimode plugin
-- File Changes --
M vimode/src/cmd-runner.c (9) M vimode/src/cmds/edit.c (45) M vimode/src/cmds/edit.h (12)
-- Patch Links --
https://github.com/geany/geany-plugins/pull/1327.patch https://github.com/geany/geany-plugins/pull/1327.diff
Looks good in general, but I think folding should work when the cursor is anywhere in the block, not just on the fold point (vim seems to work this way). I played with it a little and came up with something like this: ```C static gint prepare_fold(CmdParams *p) { /* foldparent of the next line */ gint line = SSM(p->sci, SCI_GETFOLDPARENT, p->line + 1, 0);
if (p->line == line) ; /* we are already on the fold point line */ else { /* foldparent of the current line */ line = SSM(p->sci, SCI_GETFOLDPARENT, p->line, 0); }
if (line != -1) { /* move the cursor on the visible line before the fold */ gint pos = SSM(p->sci, SCI_POSITIONFROMLINE, line, 0); SET_POS_NOX(p->sci, pos, TRUE); }
return line; }
void cmd_toggle_fold(CmdContext *c, CmdParams *p) { gint line = prepare_fold(p); if (line != -1) SSM(p->sci, SCI_FOLDLINE, (uptr_t) line, SC_FOLDACTION_TOGGLE); } ```
If the code looks OK to you and works as expected, could you update all the folding functions similarly to `cmd_toggle_fold()` above?
In addition, these aren't exactly "edit" commands and should be placed in a separate file. Could you create `fold.c/h`, add them to `Makefile.am` and move all these commands there?
If you are planning to continue working on this PR, there's one catch. Because of
https://github.com/geany/geany-plugins/commit/33984abc9bd24bbf03db3ac517eb45...
you have to first move the cursor on the visible line before performing a fold - otherwise folding won't work because the above code will expand the fold (I experienced this problem myself when testing the code above and it took me a while to realize what was going on).
@scresto09 pushed 1 commit.
904ee74be16f74c9cd0173353c224497c87b861e Merge branch 'geany:master' into vimode-fold-support
@scresto09 pushed 3 commits.
c5e4c30b9a8e2eef9bbc6a7ba9c3516e1b6b33a3 Move fold functions to fold.c/fold.h files c9e04135595b28fc2390d156b9686621b2d6dd24 Merge branch 'vimode-fold-support' of https://github.com/scresto09/geany-plugins into vimode-fold-support c902da4df5df85fcbcb2bd6a2d8048480635ebc7 vimode: try to improve fold support
OK, I tried to make some changes to improve fold support and react more like vim. With za/zo/zc we fold the current block as before. With zA we change the fold depending on the state of the parent, if there is one that is contracted we expand it otherwise we contract it. I was unable to reproduce the issue you are talking about with commit 33984ab, let me know if the issue persists. And another thing, sorry but I'm not sure I did the right things with git/merge, let me know if I need to fix anything on this. Thanks.
@techee requested changes on this pull request.
I think it looks good functionality-wise, I have just suggested to rename some things to make them clearer (at least to me).
I was unable to reproduce the issue you are talking about with commit https://github.com/geany/geany-plugins/commit/33984abc9bd24bbf03db3ac517eb45..., let me know if the issue persists.
When you call `prepare_fold()` the cursor is moved above it so you don't see it. But you can reproduce it with `cmd_close_fold_all()` which currently doesn't call `prepare_fold()` (but it should) and it doesn't collapse the fold where the cursor is placed - see the comment below.
It would be best if you rebased this PR on top of current geany-plugins master as there's a merge conflict in the Makefile.
- GNU General Public License for more details.
+ * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "cmds/fold.h" +#include "utils.h" + +/* fold command does not depend on state of parents */ +#define FOLD_NONE 0 +/* fold command depend on state of parents */ +#define FOLD_PARENT 1 +/* fold command depend on state of contracted parent if it exists */ +#define FOLD_CONTRACTED 2
I was super-confused by the naming. I's suggest the following: * `FOLD_NONE` -> `GOTO_NEAREST_PARENT` * `FOLD_PARENT` -> `GOTO_TOPMOST_PARENT` * `FOLD_CONTRACTED` -> `GOTO_CONTRACTED_PARENT`
plus the corresponding comment changes.
- You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "cmds/fold.h" +#include "utils.h" + +/* fold command does not depend on state of parents */ +#define FOLD_NONE 0 +/* fold command depend on state of parents */ +#define FOLD_PARENT 1 +/* fold command depend on state of contracted parent if it exists */ +#define FOLD_CONTRACTED 2 + +static gint prepare_fold(CmdParams *p, gint filter)
Rename to `goto_above_fold()` and the `filter` parameter to `type`.
+void cmd_close_fold_child(CmdContext *c, CmdParams *p)
+{ + gint line = prepare_fold(p, FOLD_PARENT); + if (line != -1) + SSM(p->sci, SCI_FOLDCHILDREN, (uptr_t) line, SC_FOLDACTION_CONTRACT); +} + + +void cmd_open_fold_all(CmdContext *c, CmdParams *p) +{ + SSM(p->sci, SCI_FOLDALL, SC_FOLDACTION_EXPAND | SC_FOLDACTION_CONTRACT_EVERY_LEVEL, 0); +} + + +void cmd_close_fold_all(CmdContext *c, CmdParams *p) +{
This will need `goto_above_fold(p, GOTO_TOPMOST_PARENT);` otherwise the fold where the cursor is won't get contracted because of what I mentioned before.
; /* this fold point is contracted and filter == FOLD_CONTRACTED */
+ else if (filter != FOLD_NONE) + { + gint prev_line = line; + while (prev_line != -1) + { + prev_line = SSM(p->sci, SCI_GETFOLDPARENT, prev_line, 0); + if (prev_line != -1) + { + line = prev_line; + if (filter == FOLD_CONTRACTED && ! SSM(p->sci, SCI_GETFOLDEXPANDED, line, 0)) + break; + } + } + } +
Remove extra empty line.
One more thing, the README file contains all the supported vi commands - would you copy the added commands from `index.txt` in the vimode directory into README?
@scresto09 pushed 1 commit.
e0252c96bc17c21d1f9723cfa8e89fbb021644ad Vimode: constants and functions renamed and README completed
@scresto09 pushed 0 commits.
Closed #1327.
@scresto09 You seem to have pushed an unmodified branch and the pull request closed as a result. There are no commits in this pull request now.
Sorry, I made a mistake and this closed this pull request...
I just created a new pull request [#1350](https://github.com/geany/geany-plugins/pull/1350) with all these changes.. sorry!
github-comments@lists.geany.org