all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* RFE: Editing string literals like org-mode?
@ 2021-03-27 14:05 Tim Landscheidt
  2021-03-27 14:25 ` Jean Louis
  0 siblings, 1 reply; 7+ messages in thread
From: Tim Landscheidt @ 2021-03-27 14:05 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

motivation: I was editing some Ansible files that are in
YAML syntax with yaml-mode
(https://github.com/yoshiki/yaml-mode).  I found the YAML
type of quoting rather confusing so I mused that there
should be a yaml-yank-quoted function that, with no prefix
argument, inserts the last kill in double quotes, with a
prefix argument, inserts the last kill as a multiline block,
etc. so that I could edit the "value" in another buffer,
kill it there and yank it properly quoted in my YAML buffer.

Ideally, there would be a corresponding
kill-yaml-value-at-point function (or an extension to
thing-at-point) so that I could copy the "value" dequoted to
that other buffer.  And org-mode has that nice feature
org-edit-special (C-c ') for inter alia #+BEGIN_SRC blocks
where you can edit the block's contents in another buff-
er--so why couldn't there be an equivalent for yaml-mode?

Thinking of other modes, I recalled that I have often yearn-
ed for a way to insert/edit a properly quoted string liter-
al, whether in SQL or shell scripts that call sed or perl,
just like I can in emacs-lisp-mode with prin1 et al.  And
Emacs has already a notion for what a string literal looks
like in different modes by marking them up with
font-lock-string-face.

And, to complete the circle, #+BEGIN_SRC blocks in org-mode
could be interpreted as just another form of string literal.

So Emacs seems to be missing (to be bikeshedded):

- an (optional) function foo-quote for each major mode so
  that (insert (foo-quote (read-string "String: ")))
  inserts the input properly quoted in the current buffer,

- an (optional) function foo-read-string-literal-at-point
  that returns the dequoted string literal at point, and

- a function bound to C-c ' that edits the string literal at
  point just like org-mode does (if the major mode provides
  the necessary foo-quote and
  foo-read-string-literal-at-point functions).

Now this suggestion appears to be so obvious that it is
either already implemented or could not work :-).  Comments?

Tim




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: RFE: Editing string literals like org-mode?
  2021-03-27 14:05 RFE: Editing string literals like org-mode? Tim Landscheidt
@ 2021-03-27 14:25 ` Jean Louis
  2021-03-27 16:32   ` Tim Landscheidt
  0 siblings, 1 reply; 7+ messages in thread
From: Jean Louis @ 2021-03-27 14:25 UTC (permalink / raw)
  To: Tim Landscheidt; +Cc: help-gnu-emacs

* Tim Landscheidt <tim@tim-landscheidt.de> [2021-03-27 17:06]:
> - an (optional) function foo-quote for each major mode so
>   that (insert (foo-quote (read-string "String: ")))
>   inserts the input properly quoted in the current buffer,

(insert (prin1-to-string (read-string "String: "))) → "Some \"New\" string"

Is that?

> - an (optional) function foo-read-string-literal-at-point
>   that returns the dequoted string literal at point, and

Do you mean that it works on quote strings like: "Some \"New\" string"?

> - a function bound to C-c ' that edits the string literal at
>   point just like org-mode does (if the major mode provides
>   the necessary foo-quote and
>   foo-read-string-literal-at-point functions).

I have tried searching in the manual, so can you help me, is it
in source block? Or in table?

If I got it right, it would be useful to be able to edit strings
without observing quotations.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: RFE: Editing string literals like org-mode?
  2021-03-27 14:25 ` Jean Louis
@ 2021-03-27 16:32   ` Tim Landscheidt
  2021-03-27 17:09     ` Stefan Monnier
  2021-03-27 18:08     ` Jean Louis
  0 siblings, 2 replies; 7+ messages in thread
From: Tim Landscheidt @ 2021-03-27 16:32 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis <bugs@gnu.support> wrote:

>> - an (optional) function foo-quote for each major mode so
>>   that (insert (foo-quote (read-string "String: ")))
>>   inserts the input properly quoted in the current buffer,

> (insert (prin1-to-string (read-string "String: "))) → "Some \"New\" string"

> Is that?

That is the existing solution for Emacs Lisp.  I'm looking
for a universal one.

>> - an (optional) function foo-read-string-literal-at-point
>>   that returns the dequoted string literal at point, and

> Do you mean that it works on quote strings like: "Some \"New\" string"?

For example.

>> - a function bound to C-c ' that edits the string literal at
>>   point just like org-mode does (if the major mode provides
>>   the necessary foo-quote and
>>   foo-read-string-literal-at-point functions).

> I have tried searching in the manual, so can you help me, is it
> in source block? Or in table?

For example in source blocks (#+BEGIN_SRC).

> If I got it right, it would be useful to be able to edit strings
> without observing quotations.

That would be the point; assuming a shell-script-mode buffer
with the text:

| ls 'string"literal""'

or a python-mode buffer with the text:

| print("string\"literal\"\"")

or a sql-mode buffer with the text:

| SELECT 'string"literal""';

or a perl-mode buffer with the text:

| print "string\"literal\"\"";

hitting C-c ' on any of those string literals would open a
new buffer with the text:

| string"literal""

where one could edit that string literal and hit C-c ' again
to replace the string literal in the original buffer.

(There are lots of edge cases where it is harder to DWIM,
but I'm fine with something that approximates the success
rate of Emacs's fontification.)

Tim




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: RFE: Editing string literals like org-mode?
  2021-03-27 16:32   ` Tim Landscheidt
@ 2021-03-27 17:09     ` Stefan Monnier
  2021-03-28 11:42       ` Tim Landscheidt
  2021-03-27 18:08     ` Jean Louis
  1 sibling, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2021-03-27 17:09 UTC (permalink / raw)
  To: help-gnu-emacs

> That is the existing solution for Emacs Lisp.  I'm looking
> for a universal one.

Sounds good.  You basically need to add some standard way for major
modes to tell how to quote/unquote strings and then to go around add
change the major modes to set that properly.

E.g. major modes would have to do something like:

    (setq-local string-quote-function #'foo-mode-string-quote)

And then add some globally-defined commands that make use of this new
information, such as `string-quote-edit` which gets the "string at
point" and lets you edit it without the quoting, and commands
`string-quote-yank` and `string-quote-kill`.

To get started, you'll probably want to provide a useful default value
for `string-quote-function` (presumably relying on the syntax tables,
maybe with some heuristics to handle some \t and friends) so that the
feature works out of the box (to some extent) in many major modes.


        Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: RFE: Editing string literals like org-mode?
  2021-03-27 16:32   ` Tim Landscheidt
  2021-03-27 17:09     ` Stefan Monnier
@ 2021-03-27 18:08     ` Jean Louis
  2021-03-28 12:31       ` Tim Landscheidt
  1 sibling, 1 reply; 7+ messages in thread
From: Jean Louis @ 2021-03-27 18:08 UTC (permalink / raw)
  To: Tim Landscheidt; +Cc: help-gnu-emacs

* Tim Landscheidt <tim@tim-landscheidt.de> [2021-03-27 19:33]:
> Jean Louis <bugs@gnu.support> wrote:
> 
> >> - an (optional) function foo-quote for each major mode so
> >>   that (insert (foo-quote (read-string "String: ")))
> >>   inserts the input properly quoted in the current buffer,
> 
> > (insert (prin1-to-string (read-string "String: "))) → "Some \"New\" string"
> 
> > Is that?

> That is the existing solution for Emacs Lisp.  I'm looking
> for a universal one.

Do you mean like a function maybe, that works everywhere?

(defun my-insert-string-literal (&optional string)
  "Asks for STRING if not supplied and inserts it as literal
  string with quotes."
  (interactive)
  (let ((string (or string (read-string "String: "))))
    (insert (prin1-to-string string))))

> >> - an (optional) function foo-read-string-literal-at-point
> >>   that returns the dequoted string literal at point, and
> 
> > Do you mean that it works on quote strings like: "Some \"New\" string"?
> 
> For example.

thing-at-point would need to be defined, I would not know now how
to define it universally to find out string with quote on both
sides and that it works over new lines.

"Some string✗ here \"Mo✗re\" and more."
"Then again✗ maybe \"one\" here".

What would be then string with quotes if cursor is at second ✗ on
first line or first, or in the second line?

Should program count all quotes, and what about others? It is
hard to find general boundaries.

It would be easier to find boundaries in a sexp like
'(Something "String \"more\" here"). 

> >> - a function bound to C-c ' that edits the string literal at
> >>   point just like org-mode does (if the major mode provides
> >>   the necessary foo-quote and
> >>   foo-read-string-literal-at-point functions).
> 
> > I have tried searching in the manual, so can you help me, is it
> > in source block? Or in table?
> 
> For example in source blocks (#+BEGIN_SRC).
> 
> > If I got it right, it would be useful to be able to edit strings
> > without observing quotations.
> 
> That would be the point; assuming a shell-script-mode buffer
> with the text:

For that I have tried with:

#+BEGIN_SRC shell-script
  Something
#+END_SRC

And I do not see how it helps with quoting, maybe I
misunderstood. What I could see is that it is making new buffer
and edits it maybe with a different mode.

Do you mean to temporarily, during editing, change the mode and
then come back to original mode?




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: RFE: Editing string literals like org-mode?
  2021-03-27 17:09     ` Stefan Monnier
@ 2021-03-28 11:42       ` Tim Landscheidt
  0 siblings, 0 replies; 7+ messages in thread
From: Tim Landscheidt @ 2021-03-28 11:42 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> wrote:

> […]

> To get started, you'll probably want to provide a useful default value
> for `string-quote-function` (presumably relying on the syntax tables,
> maybe with some heuristics to handle some \t and friends) so that the
> feature works out of the box (to some extent) in many major modes.

I was more hoping for someone's "I'm on it!" :-).  The last
time I looked at fixing a minor nuisance in a mode's fonti-
fication, I was overwhelmed by Emacs's code in that corner
that, eh, seemed appropriately sized for a very complex pro-
blem.

Tim




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: RFE: Editing string literals like org-mode?
  2021-03-27 18:08     ` Jean Louis
@ 2021-03-28 12:31       ` Tim Landscheidt
  0 siblings, 0 replies; 7+ messages in thread
From: Tim Landscheidt @ 2021-03-28 12:31 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis <bugs@gnu.support> wrote:

>> >> - an (optional) function foo-quote for each major mode so
>> >>   that (insert (foo-quote (read-string "String: ")))
>> >>   inserts the input properly quoted in the current buffer,

>> > (insert (prin1-to-string (read-string "String: "))) → "Some \"New\" string"

>> > Is that?

>> That is the existing solution for Emacs Lisp.  I'm looking
>> for a universal one.

> Do you mean like a function maybe, that works everywhere?

> (defun my-insert-string-literal (&optional string)
>   "Asks for STRING if not supplied and inserts it as literal
>   string with quotes."
>   (interactive)
>   (let ((string (or string (read-string "String: "))))
>     (insert (prin1-to-string string))))

Exactly, but your function will for example not work in SQL.

>> >> - an (optional) function foo-read-string-literal-at-point
>> >>   that returns the dequoted string literal at point, and

>> > Do you mean that it works on quote strings like: "Some \"New\" string"?

>> For example.

> thing-at-point would need to be defined, I would not know now how
> to define it universally to find out string with quote on both
> sides and that it works over new lines.

> "Some string✗ here \"Mo✗re\" and more."
> "Then again✗ maybe \"one\" here".

> What would be then string with quotes if cursor is at second ✗ on
> first line or first, or in the second line?

The string-edit buffer's contents would be:

| Some string here "More" and more.

or

| Then again maybe "one" here

> Should program count all quotes, and what about others? It is
> hard to find general boundaries.

> It would be easier to find boundaries in a sexp like
> '(Something "String \"more\" here").

Not really; if I enter:

| print "Some string here \"More\" and more.";

into a perl-mode buffer, Emacs "knows" that there is a
string literal starting at the first and ending at the last
quote in that line.

>> >> - a function bound to C-c ' that edits the string literal at
>> >>   point just like org-mode does (if the major mode provides
>> >>   the necessary foo-quote and
>> >>   foo-read-string-literal-at-point functions).

>> > I have tried searching in the manual, so can you help me, is it
>> > in source block? Or in table?

>> For example in source blocks (#+BEGIN_SRC).

>> > If I got it right, it would be useful to be able to edit strings
>> > without observing quotations.

>> That would be the point; assuming a shell-script-mode buffer
>> with the text:

> For that I have tried with:

> #+BEGIN_SRC shell-script
>   Something
> #+END_SRC

> And I do not see how it helps with quoting, maybe I
> misunderstood. What I could see is that it is making new buffer
> and edits it maybe with a different mode.

I did not say that org-mode's edit-in-another-buffer feature
is primarily useful for quoting; it is just a similar con-
cept.

But if, for example, you enter "#+END_SRC" in an org-mode
edit buffer, you will see that it not only gets indented,
but also quoted as:

| #+BEGIN_SRC shell-script
|   ,#+END_SRC
| #+END_SRC

> Do you mean to temporarily, during editing, change the mode and
> then come back to original mode?

I believe there are a number of "multiple major modes in one
buffer" solutions, but (AFAIK) they are aimed at situations
like editing HTML with embedded (verbatim) PHP or Java-
Script, not editing an SQL statement in a Perl script in a
shell script in an Emacs Lisp function.

Tim




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-03-28 12:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-03-27 14:05 RFE: Editing string literals like org-mode? Tim Landscheidt
2021-03-27 14:25 ` Jean Louis
2021-03-27 16:32   ` Tim Landscheidt
2021-03-27 17:09     ` Stefan Monnier
2021-03-28 11:42       ` Tim Landscheidt
2021-03-27 18:08     ` Jean Louis
2021-03-28 12:31       ` Tim Landscheidt

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.