unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Skeleton angle brackets '<' and '>'
@ 2016-01-05 21:12 B.V. Raghav
  0 siblings, 0 replies; 5+ messages in thread
From: B.V. Raghav @ 2016-01-05 21:12 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

There are smart tools for the purpose, but I prefer to stick to the old
school for auto pairing.

Electric Pair mode and Skeleton pair, like this:
(electric-pair-mode t)
(setq skeleton-pair t)

While in the c++ mode, (hopefully true for all c-modes)
I am able to successfully pair (), {}, and [], with a single key press,
i.e. of the preceding character of the pair.

I also want to pair the angle brackets, i.e. `<' and `>' characters.

The documentation for the variable sekeleton-pair-alist says,

[...]
Each alist element, which looks like (ELEMENT ...), is passed to
`skeleton-insert' with no interactor. Variable `str' does nothing.

Elements might be (?` ?` _ "''"), (?\( ? _ ")") or(?{ \n > _ \n ?} >).
[...]

I am sorry to say, I cant make a head or tail of this cryptic value
suggested for the variable.

Help solicited.

Thanks,
r
-- 
(B.V. Raghav)




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

* Re: Skeleton angle brackets '<' and '>'
       [not found] <mailman.1800.1452028390.843.help-gnu-emacs@gnu.org>
@ 2016-01-06 17:59 ` Alan Mackenzie
  2016-01-07  4:03   ` B.V. Raghav
                     ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Alan Mackenzie @ 2016-01-06 17:59 UTC (permalink / raw)
  To: help-gnu-emacs

B.V. Raghav <bvraghav@iitk.ac.in> wrote:
> Hi,

> There are smart tools for the purpose, but I prefer to stick to the old
> school for auto pairing.

> Electric Pair mode and Skeleton pair, like this:
> (electric-pair-mode t)
> (setq skeleton-pair t)

> While in the c++ mode, (hopefully true for all c-modes)
> I am able to successfully pair (), {}, and [], with a single key press,
> i.e. of the preceding character of the pair.

> I also want to pair the angle brackets, i.e. `<' and `>' characters.

Be careful what you want!  Do you also want automatically to pair up
"less than" with "greater than"?  It could get pretty tedious if, every
time you wanted to write "i < 10", you got "i <>", forcing you to delete
the ">" before writing in the "10".

This overloading of < and > with two meanings (actually, three when you
count "#include <stdio.h>", but that causes few problems) causes great
difficulties for C++ Mode.  Only after extensive analysis can the mode
determine that a < and a > are a pair of template delimiters, and even
then, can't do it with 100% certainty.

For example, how many parameters are passed to foo in the following:

    foo (a < b, c > d);

?  It might be a function call with two relational expressions, it might
be a declaration with the single parameter d of templated type a <b , c>.
Such was the wisdom of C++'s designers.

When it is determined there is a pair of delimiters, they are marked as
such with syntax-table text properties, but this marking is always done
on both delimiters at once.

In short, there is little chance of ever being able to insert angle
brackets as a pair in C++ Mode.  Sorry!

> The documentation for the variable sekeleton-pair-alist says,

> [...]
> Each alist element, which looks like (ELEMENT ...), is passed to
> `skeleton-insert' with no interactor. Variable `str' does nothing.

> Elements might be (?` ?` _ "''"), (?\( ? _ ")") or(?{ \n > _ \n ?} >).
> [...]

> I am sorry to say, I cant make a head or tail of this cryptic value
> suggested for the variable.

Sorry to say this, but neither can I.  I don't know what an "interactor"
is, variable `str' is obscure, and it is totally unclear what the various
example elements might mean.

> Help solicited.

Can I suggest you submit a bug report for this dreadful doc string?

> Thanks,
> r
> -- 
> (B.V. Raghav)

-- 
Alan Mackenzie (Nuremberg, Germany).



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

* Re: Skeleton angle brackets '<' and '>'
  2016-01-06 17:59 ` Skeleton angle brackets '<' and '>' Alan Mackenzie
@ 2016-01-07  4:03   ` B.V. Raghav
  2016-01-07  9:07   ` Yuri Khan
  2016-01-08 18:33   ` B.V. Raghav
  2 siblings, 0 replies; 5+ messages in thread
From: B.V. Raghav @ 2016-01-07  4:03 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: help-gnu-emacs

Hi Alan,

Alan Mackenzie <acm@muc.de> writes:

>
> Be careful what you want!  Do you also want automatically to pair up
> "less than" with "greater than"?  It could get pretty tedious if, every
> time you wanted to write "i < 10", you got "i <>", forcing you to delete
> the ">" before writing in the "10".
>
> This overloading of < and > with two meanings (actually, three when you
> count "#include <stdio.h>", but that causes few problems) causes great
> difficulties for C++ Mode.  Only after extensive analysis can the mode
> determine that a < and a > are a pair of template delimiters, and even
> then, can't do it with 100% certainty.
>
> For example, how many parameters are passed to foo in the following:
>
>     foo (a < b, c > d);
>
> ?  It might be a function call with two relational expressions, it might
> be a declaration with the single parameter d of templated type a <b , c>.
> Such was the wisdom of C++'s designers.
>
> When it is determined there is a pair of delimiters, they are marked as
> such with syntax-table text properties, but this marking is always done
> on both delimiters at once.
>
> In short, there is little chance of ever being able to insert angle
> brackets as a pair in C++ Mode.  Sorry!

Oh my, my! I totally forgot about the relational operator part! And the
elaborate explanation was highly insightful---a brilliant recap of the
C++ basics.

Thanks,
r
-- 
(B.V. Raghav)



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

* Re: Skeleton angle brackets '<' and '>'
  2016-01-06 17:59 ` Skeleton angle brackets '<' and '>' Alan Mackenzie
  2016-01-07  4:03   ` B.V. Raghav
@ 2016-01-07  9:07   ` Yuri Khan
  2016-01-08 18:33   ` B.V. Raghav
  2 siblings, 0 replies; 5+ messages in thread
From: Yuri Khan @ 2016-01-07  9:07 UTC (permalink / raw)
  Cc: help-gnu-emacs@gnu.org

On Wed, Jan 6, 2016 at 11:59 PM, Alan Mackenzie <acm@muc.de> wrote:

>> I also want to pair the angle brackets, i.e. `<' and `>' characters.
>
> Be careful what you want!  Do you also want automatically to pair up
> "less than" with "greater than"?  It could get pretty tedious if, every
> time you wanted to write "i < 10", you got "i <>", forcing you to delete
> the ">" before writing in the "10".

With some careful coding convention planning, the impossible becomes easy.

> For example, how many parameters are passed to foo in the following:
>
>     foo (a < b, c > d);
>
> ?

As written, it is clearly a function call, because there are spaces on
both sides of the operators.

> It might be a function call with two relational expressions, it might
> be a declaration with the single parameter d of templated type a <b , c>.

In this case, there will be no spaces on the inner side of the
brackets, and in some coding conventions, neither before the opening
bracket: f(x, y); a<b, c>. So

    Foo foo(a<b, c> d);

declares a function foo taking a single parameter named d of type a<b,
c> and returning Foo; while

    Foo foo(a < b, c > d);

declares a variable foo of type Foo passing a < b as the first
constructor argument and c > d as the second.

(The original example is invalid because C++ does not have implicit int.)

(There is one technical exception to the “no space on the inner side
of a bracket” rule: we have to put an intervening space when closing
two template argument lists in code that has to preserve C++03
compatibility.)


So, returning to the original question, I do it with smartparens this way:

    (sp-local-pair 'c++-mode "<" ">" :when '(sp-point-after-word-p))

This way, < is treated as a bracket only when used immediately after
an identifier with no intervening space. This also solves the case of
the << operator — the first angle is not paired because it is preceded
by a space, and the second angle because the immediately preceding
token (the first angle) is not a word.

(I do, however, get pairing when writing operator< or operator<<, and
no pairing when writing #include <>. Should probably code a couple of
exceptions for those.)



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

* Re: Skeleton angle brackets '<' and '>'
  2016-01-06 17:59 ` Skeleton angle brackets '<' and '>' Alan Mackenzie
  2016-01-07  4:03   ` B.V. Raghav
  2016-01-07  9:07   ` Yuri Khan
@ 2016-01-08 18:33   ` B.V. Raghav
  2 siblings, 0 replies; 5+ messages in thread
From: B.V. Raghav @ 2016-01-08 18:33 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: help-gnu-emacs

Alan Mackenzie <acm@muc.de> writes:

> B.V. Raghav <bvraghav@iitk.ac.in> wrote:
>> Hi,
>
>> There are smart tools for the purpose, but I prefer to stick to the old
>> school for auto pairing.
>
>> Electric Pair mode and Skeleton pair, like this:
>> (electric-pair-mode t)
>> (setq skeleton-pair t)
>
>> While in the c++ mode, (hopefully true for all c-modes)
>> I am able to successfully pair (), {}, and [], with a single key press,
>> i.e. of the preceding character of the pair.
>
>> I also want to pair the angle brackets, i.e. `<' and `>' characters.
>
> Be careful what you want!  Do you also want automatically to pair up
> "less than" with "greater than"?  It could get pretty tedious if, every
> time you wanted to write "i < 10", you got "i <>", forcing you to delete
> the ">" before writing in the "10".
>
> This overloading of < and > with two meanings (actually, three when you
> count "#include <stdio.h>", but that causes few problems) causes great
> difficulties for C++ Mode.  Only after extensive analysis can the mode
> determine that a < and a > are a pair of template delimiters, and even
> then, can't do it with 100% certainty.
>
> For example, how many parameters are passed to foo in the following:
>
>     foo (a < b, c > d);

Now I remember the problem. It was there when I simillarly, had to pass
a templated type as argumentn to preprocessors using typedefs. This
thread helped me recall.
http://stackoverflow.com/a/7304772/4366367

One of the examples, that extensively uses typedefs to avoid this
problem is Boost.Exceptions uses #define , template<> and overloading
operator << --- extensively
http://www.boost.org/doc/libs/1_60_0/libs/exception/doc/boost-exception.html

>
> ?  It might be a function call with two relational expressions, it might
> be a declaration with the single parameter d of templated type a <b , c>.
> Such was the wisdom of C++'s designers.
>
> When it is determined there is a pair of delimiters, they are marked as
> such with syntax-table text properties, but this marking is always done
> on both delimiters at once.
>
> In short, there is little chance of ever being able to insert angle
> brackets as a pair in C++ Mode.  Sorry!
>
>> The documentation for the variable sekeleton-pair-alist says,
>
>> [...]
>> Each alist element, which looks like (ELEMENT ...), is passed to
>> `skeleton-insert' with no interactor. Variable `str' does nothing.
>
>> Elements might be (?` ?` _ "''"), (?\( ? _ ")") or(?{ \n > _ \n ?} >).
>> [...]
>
>> I am sorry to say, I cant make a head or tail of this cryptic value
>> suggested for the variable.
>
> Sorry to say this, but neither can I.  I don't know what an "interactor"
> is, variable `str' is obscure, and it is totally unclear what the various
> example elements might mean.
>
>> Help solicited.
>
> Can I suggest you submit a bug report for this dreadful doc string?
>
>> Thanks,
>> r
>> -- 
>> (B.V. Raghav)

-- 
(B.V. Raghav)



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

end of thread, other threads:[~2016-01-08 18:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1800.1452028390.843.help-gnu-emacs@gnu.org>
2016-01-06 17:59 ` Skeleton angle brackets '<' and '>' Alan Mackenzie
2016-01-07  4:03   ` B.V. Raghav
2016-01-07  9:07   ` Yuri Khan
2016-01-08 18:33   ` B.V. Raghav
2016-01-05 21:12 B.V. Raghav

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).