unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#25903: Question re syntax tables and unexpected behaviour in C/C++ major mode
@ 2017-02-28 22:04 Peter Milliken
  2017-03-02  4:32 ` Glenn Morris
  2017-03-02  4:49 ` npostavs
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Milliken @ 2017-02-28 22:04 UTC (permalink / raw)
  To: 25903

[-- Attachment #1: Type: text/plain, Size: 1969 bytes --]

I tried asking this question in gnu.emacs.help but obviously didn't attract
the attention of the correct person/people. Hopefully somebody on this list
can help.

My code is intended to work as a minor mode with any (programming) major
mode (I am actually re-writing a minor mode that I wrote back when Emacs
was still at version 19 - a lot has changed since then :-)).

I want to be able to search through a buffer to detect text within {}/[]
pairings - and nothing else. I could use regexp searches of course, but I
thought it would be a good learning opportunity to use syntax tables (and
the forward/backward-sexp functions).

My code works fine in buffers that have Python, Ada and Lisp major modes
but it experiences difficulties in buffers with C/C++ major mode.
Basically, when in a C/C++ buffer, the code locates text within "<>" pairs
as well as "{}"/"[]" pairs.  I really don't want my code to have an (ugly)
exception case where it tests "if in C/C++ mode then check if <> has been
detected and skip over them and continue looking for {}/[] pairs"

My code copies the default syntax table, changing every entry to be a
word-constituent and then adding the appropriate open parenthesis
characters i.e.

(defvar my-syntax-table (copy-syntax-table (standard-syntax-table)))

(modify-syntax-entry (cons 0 (max-char)) "w" my-syntax-table)

(modify-syntax-entry ?\{ "(}" my-syntax-table)
(modify-syntax-entry ?\} "){" my-syntax-table)
(modify-syntax-entry ?[ "(]" my-syntax-table)
(modify-syntax-entry ?] ")[" my-syntax-table)

my code then starts with:

(with-syntax-table my-syntax-table
  .
  .
  .



I have checked the "<", ">" entries in the syntax table when running in a
buffer with C/C++ and the aref returns (2)  which I believe is the code for
a word-constituent, so something in the C/C++ mode must be affecting the
sexp functions, but I have no idea what it could be or how to isolate it.

Any help/clarification would be appreciated - thanks

Peter

[-- Attachment #2: Type: text/html, Size: 5270 bytes --]

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

* bug#25903: Question re syntax tables and unexpected behaviour in C/C++ major mode
  2017-02-28 22:04 bug#25903: Question re syntax tables and unexpected behaviour in C/C++ major mode Peter Milliken
@ 2017-03-02  4:32 ` Glenn Morris
  2017-03-02  4:49 ` npostavs
  1 sibling, 0 replies; 4+ messages in thread
From: Glenn Morris @ 2017-03-02  4:32 UTC (permalink / raw)
  To: Peter Milliken; +Cc: 25903


Since this isn't an Emacs bug, I'm going to close this in the tracker.
We've got enough problems of our own to worry about. :)
This doesn't stop anyone from responding though.





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

* bug#25903: Question re syntax tables and unexpected behaviour in C/C++ major mode
  2017-02-28 22:04 bug#25903: Question re syntax tables and unexpected behaviour in C/C++ major mode Peter Milliken
  2017-03-02  4:32 ` Glenn Morris
@ 2017-03-02  4:49 ` npostavs
  2017-03-02 20:10   ` Alan Mackenzie
  1 sibling, 1 reply; 4+ messages in thread
From: npostavs @ 2017-03-02  4:49 UTC (permalink / raw)
  To: Peter Milliken; +Cc: 25903

Peter Milliken <peter.milliken@gmail.com> writes:

> My code works fine in buffers that have Python, Ada and Lisp major
> modes but it experiences difficulties in buffers with C/C++ major
> mode. Basically, when in a C/C++ buffer, the code locates text within
> "<>" pairs as well as "{}"/"[]" pairs. I really don't want my code to
> have an (ugly) exception case where it tests "if in C/C++ mode then
> check if <> has been detected and skip over them and continue looking
> for {}/[] pairs"

You'll probably have to though.  In C++, "<" matches ">" when they
denote template arguments.  cc-mode implements this by adding text
properties to those characters so that they have pair syntax.  Changing
the syntax table doesn't change the text properties, so for those
characters, the syntax stays the same.

Use C-u C-x = to check the syntax and properties of a particular
character.





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

* bug#25903: Question re syntax tables and unexpected behaviour in C/C++ major mode
  2017-03-02  4:49 ` npostavs
@ 2017-03-02 20:10   ` Alan Mackenzie
  0 siblings, 0 replies; 4+ messages in thread
From: Alan Mackenzie @ 2017-03-02 20:10 UTC (permalink / raw)
  To: npostavs; +Cc: Peter Milliken, 25903

Hello, Peter and Noam

On Wed, Mar 01, 2017 at 23:49:17 -0500, npostavs@users.sourceforge.net wrote:
> Peter Milliken <peter.milliken@gmail.com> writes:

> > My code works fine in buffers that have Python, Ada and Lisp major
> > modes but it experiences difficulties in buffers with C/C++ major
> > mode. Basically, when in a C/C++ buffer, the code locates text within
> > "<>" pairs as well as "{}"/"[]" pairs. I really don't want my code to
> > have an (ugly) exception case where it tests "if in C/C++ mode then
> > check if <> has been detected and skip over them and continue looking
> > for {}/[] pairs"

Depending on your exact usage, it might be well worth your while leaving
the stuff for strings and comments in your syntax table.

> You'll probably have to though.  In C++, "<" matches ">" when they
> denote template arguments.  cc-mode implements this by adding text
> properties to those characters so that they have pair syntax.  Changing
> the syntax table doesn't change the text properties, so for those
> characters, the syntax stays the same.

What can be done, though, is to disable the effect of the syntax-table
text property by binding parse-sexp-lookup-properties to nil.

> Use C-u C-x = to check the syntax and properties of a particular
> character.

-- 
Alan Mackenzie (Nuremberg, Germany).





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

end of thread, other threads:[~2017-03-02 20:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-02-28 22:04 bug#25903: Question re syntax tables and unexpected behaviour in C/C++ major mode Peter Milliken
2017-03-02  4:32 ` Glenn Morris
2017-03-02  4:49 ` npostavs
2017-03-02 20:10   ` Alan Mackenzie

Code repositories for project(s) associated with this public inbox

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

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