Re: custom braced names for filename and syntax
Luca Coraggio <[email protected]> Wed, 19 Nov 2025 10:56:15 +0100
| Newsgroups | gmane.editors.nano.devel |
|---|---|
| Message-ID | <[email protected]> |
Wow, that is great! Thank you!
On 18/11/2025 16:25, Benno Schulenberg wrote:
> Then, instead of your patch, I would suggest the attached one. It is
> just
> twenty lines, and easier to understand, I think.
Definitely. I like your approach way better. I think it is more natural
and fits nano better.
I played with it a bit, and I note some differences compared to the
"placeholder substitution" approach:
1) The "placeholder substitution" saves in the history the string with
the placeholder, making the command reusable across different files
(using Arrow Up in the execute prompt).
2) The "placeholder substitution" works even with new prompts. For
example executing "echo \FTYPE" will work, while executing "echo
{filename}" won't.
That being said, I think your approach is still to be preferred for
several reasons: I think it is more natural to nano users, does not
introduce a "new thing," and is easier to maintain and less verbose.
For point 1: I think it rarely happens that one wants to use the same
command on another file, so that this "inconvenience" is negligible.
For point 2: This can be easily solved by mapping for example:
bind Sh-M-M "{filename}" execute
I attach a modified version of your patch to make {filename} return the
full file path (adding a single line to the paste_filename function in
prompt.c). I think it is better to always return the full file path for
consistency and let
the external script handle dirname or basename.
Differently, using $(pwd)/{filename} would not work in all cases: when
nano opens files that are not in the current
working directory, the {filename} is the full path; otherwise, it equals
the file base name (you know better than me).
This would result in cumbersome checks for the user in external scripts
(e.g. let the script check if the {filename}
contains a full path or otherwise print the working directory and attach
it to the filename).
Moreover, the binding above with $pwd won't work:
bind Sh-M-M "$(pwd)/{filename}" execute
In my opinion, returning the full file path is more consistent and
carries the most information.
What do you think about it?
Luca
0002-custom-braced-names-for-injecting-filename-or-syntax-into-answer.patch
(text/x-patch, 3 KB)
From e307a577cc04630ca103741526cbacd7a42be240 Mon Sep 17 00:00:00 2001 From: Luca Coraggio <[email protected]> Date: Wed, 19 Nov 2025 10:26:21 +0100 Subject: [PATCH] {filename} from Benno's patch now gives full file path Signed-off-by: Luca Coraggio <[email protected]> --- src/prompt.c | 14 ++++++++++++++ src/prototypes.h | 2 ++ src/rcfile.c | 4 ++++ syntax/nanorc.nanorc | 2 +- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/prompt.c b/src/prompt.c index 0a9c6b48..e9f140d0 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -230,6 +230,20 @@ void inject_into_answer(char *burst, size_t count) typing_x += count; } +/* Insert the filename of the current buffer into the answer. */ +void paste_filename(void) +{ + char *p = get_full_path(openfile->filename); + inject_into_answer(p, strlen(p)); +} + +/* Insert the name of the current syntax into the answer. */ +void paste_syntax(void) +{ + if (openfile->syntax) + inject_into_answer(openfile->syntax->name, strlen(openfile->syntax->name)); +} + /* Get a verbatim keystroke and insert it into the answer. */ void do_statusbar_verbatim_input(void) { diff --git a/src/prototypes.h b/src/prototypes.h index e1cd8f61..a68669b0 100644 --- a/src/prototypes.h +++ b/src/prototypes.h @@ -444,6 +444,8 @@ void suck_up_input_and_paste_it(void); void inject(char *burst, size_t count); /* Most functions in prompt.c. */ +void paste_filename(void); +void paste_syntax(void); size_t get_statusbar_page_start(size_t base, size_t column); void put_cursor_at_end_of_answer(void); void add_or_remove_pipe_symbol_from_answer(void); diff --git a/src/rcfile.c b/src/rcfile.c index 6143f5fc..bf60282d 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -404,6 +404,10 @@ keystruct *strtosc(const char *input) s->func = flip_replace; else if (!strcmp(input, "flipgoto")) s->func = flip_goto; + else if (!strcmp(input, "filename")) + s->func = paste_filename; + else if (!strcmp(input, "syntax")) + s->func = paste_syntax; #ifdef ENABLE_HISTORIES else if (!strcmp(input, "older")) s->func = get_older_item; diff --git a/syntax/nanorc.nanorc b/syntax/nanorc.nanorc index 0c16a6be..ad3b9637 100644 --- a/syntax/nanorc.nanorc +++ b/syntax/nanorc.nanorc @@ -32,7 +32,7 @@ color crimson "\{(location|gotoline|(begin|end)para|comment|complete|(un)?indent color crimson "\{(left|right|up|down|home|end|(scroll|page)(up|down)|(top|bottom)row|center|cycle|(prev|next)(word|block|anchor|buf))\}" color crimson "\{(tab|enter|delete|backspace|verbatim|refresh|suspend|casesens|regexp|backwards|older|newer|(dos|mac)format)\}" color crimson "\{(append|prepend|backup|flip(goto|replace|execute|pipe|convert|newbuffer)|browser|gotodir|(first|last)(file|line))\}" -color crimson "\{(nohelp|constantshow|softwrap|linenumbers|whitespacedisplay|nosyntax|zero)\}" +color crimson "\{(nohelp|constantshow|softwrap|linenumbers|whitespacedisplay|nosyntax|zero|filename|syntax)\}" color crimson "\{(smarthome|autoindent|cutfromcursor|breaklonglines|tabstospaces|mouse|\{|\})\}" # Commands -- 2.51.1