Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation in C++ file #209

Closed
ghost opened this issue Nov 2, 2013 · 9 comments
Closed

Navigation in C++ file #209

ghost opened this issue Nov 2, 2013 · 9 comments

Comments

@ghost
Copy link

ghost commented Nov 2, 2013

Is it possible to navigate conveniently forward (and backward) between successive curly braces { . The functions sp- forward-sexp and sp-next-sexp don't seem suitable. For example, if the cursor is before the first opening brace it jumps to after the last closing brace.

int 
main(int argc, char *argv[])
{
  Kinetic::start(argc,argv); 

  if( argc > 1 )
  { 
    for( int i=1; i<argc; i++ )
    {
      line=argv[i];
      if( line=="noplot" || line=="nopause" || line=="abortOnEnd" ) continue;
      else
      {
     const char * dot = strrchr((const char*) line, '.');
      }
    }
  }
}
@Fuco1
Copy link
Owner

Fuco1 commented Nov 2, 2013

Good question, and the answer is no. I have this noted down in my todo-file, that is: "provide a way to navigate with just specific pair". Something akin to the symbol/pair/tag prefix arguments, but it would probably be some macro to generate functions users would bind to a key.

The best aproximation now is sp-down-sexp but that would also consider the () pairs, which might be rather annoying.

If you aren't afraid of a bit of a programing, you can define your own functions by using dynamic scoping in elisp:

(defun my-curly-sp-down-sexp (&optional arg)
  (interactive "P")
  (let ((sp-pair-list '(("{" . "}"))))
    (sp-down-sexp arg)))

This pattern can be used to "augument" all the other similar functions to work just on curly brackets.

You can also check sp-html-next-tag funciton in smartparens-html.el and change the let binding to the one above. That will take you from {foo |bar} {baz qux} -> {foo bar} {|baz qux} that is, inside the next bracket

@ghost
Copy link
Author

ghost commented Nov 2, 2013

The original sp-down-exp moves into the () expression and then further forward navigation is cumbersome.

The function you suggested my-curly-sp-down-sexp works fine and I will modify the other functions to navigate the file.

I am not sure about how to implement your other suggestion on changing the let binding on sp-html-next-tag but I will look into the documentation some more. Thanks for your help.

Fuco1 added a commit that referenced this issue Nov 4, 2013
…ing of navigation functions to specific pairs/objects
@Fuco1
Copy link
Owner

Fuco1 commented Nov 4, 2013

@Rchella I've added some functions to make these restrictions as easy as possible

sp-restrict-to-pairs
sp-restrict-to-object
sp-restrict-to-pairs-interactive
sp-restrict-to-object-interactive

Check the documentation strings, if something isn't clear, please let me know so I'll rewrite the docs better.

Fuco1 added a commit that referenced this issue Nov 4, 2013
@ghost
Copy link
Author

ghost commented Nov 6, 2013

I tried to implement some navigation functions using the newly defined functions.

(global-set-key (kbd "M-{") (sp-restrict-to-pairs-interactive "{" 'sp-down-sexp))
(global-set-key (kbd "M-}") (sp-restrict-to-pairs-interactive "{" 'sp-up-sexp))

seem to work as expected. But

(global-set-key (kbd "M->") (sp-restrict-to-pairs-interactive "{" 'sp-forward-sexp))
(global-set-key (kbd "M-<") (sp-restrict-to-pairs-interactive "{" 'sp-backward-sexp))

seem to work OK when adjacent to one of the {, but from inside a sexp the restriction to { does not seem to be observed. Perhaps I did not set them up correctly.

@Fuco1
Copy link
Owner

Fuco1 commented Nov 6, 2013

What you mean "from inside a sexp"? Can you give an example?

@ghost
Copy link
Author

ghost commented Nov 6, 2013

Referring back to this line in the example file I first posted

  Kinetic::start(argc,argv);

if the cursor is before K, then the first M-> (defined as indicated above) moves the cursor to the end of the word Kinetic and the next M-> places the cursor before the (. I would expect M-> to go to the following {.

@Fuco1
Copy link
Owner

Fuco1 commented Nov 6, 2013

I see. You will need to compose it with "object restrictor", because foo is technically a "sexp" but without any delimiter (I use terminology symbol)

(define-key c++-mode-map (kbd "M->") (sp-restrict-to-object-interactive
                                      'sp-prefix-pair-object
                                      (sp-restrict-to-pairs-interactive "{" 'sp-forward-sexp)))

I treat them in a special way because it allows greater flexibility.

@Fuco1
Copy link
Owner

Fuco1 commented Nov 6, 2013

@ghost
Copy link
Author

ghost commented Nov 6, 2013

OK, this works fine now. Thanks for the quick response and the detailed documentation of how to use the new functions.

@ghost ghost closed this as completed Nov 6, 2013
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant