unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Yuri Khan <yuri.v.khan@gmail.com>
Cc: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Subject: Re: Skeleton angle brackets '<' and '>'
Date: Thu, 7 Jan 2016 15:07:15 +0600	[thread overview]
Message-ID: <CAP_d_8XY7H1kc49cCrn2zMeANgzpBaDpnOZAojN84OubafuNOw@mail.gmail.com> (raw)
In-Reply-To: <n6jkmb$hmt$1@colin.muc.de>

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.)



  parent reply	other threads:[~2016-01-07  9:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 [this message]
2016-01-08 18:33   ` B.V. Raghav
2016-01-05 21:12 B.V. Raghav

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAP_d_8XY7H1kc49cCrn2zMeANgzpBaDpnOZAojN84OubafuNOw@mail.gmail.com \
    --to=yuri.v.khan@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).