Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3324546
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:26:03+00:00 2026-05-17T23:26:03+00:00

Preamble Using VTK library with C++, quite often I have to write something like

  • 0

Preamble

Using VTK library with C++, quite often I have to write something like this:

vtkInteractorStyleRubberBandZoom *isrbz = vtkInteractorStyleRubberBandZoom::New();

Furthermore, every time I need to use a new VTK class in my program, I have to go somewhere up the source file and add #include “vtkInteractorStyleRubberBandZoom.h”

How do I automate it, so I have to type each of the excruciatingly long class names one time instead of three?

I tried writing an Emacs minor mode for it. There are probably existing solutions out there already (YaSnippet?), but I thought that writing it myself would be a good excercise, too.

Code

;vtk-mode.el
;add to .emacs:
;(load  "vtk-mode")
;(global-set-key [(control =)] 'expand-vtk)

(defun expand-vtk ()
  (interactive)
  (setq now (point))
  (setq vtkstart (search-backward "vtk"))
  (setq vtkend (- (search-forward " ") 1))
  (setq vtkname (buffer-substring vtkstart vtkend))

  ;check for #include "vtkBlah.h"
  (setq includename (format "#include \"%s.h\"\n" vtkname))
  (search-backward includename nil (append-include-vtk includename))
  (goto-char (+ now (length includename)))

  (insert (format "= %s::New();" vtkname)))

(defun append-include-vtk (incname)
  (goto-char 0)
  (insert incname))

Problem

Basically, it works, except that searching for an include name always fails, e. g.:

vtkSomething *smth /*press C-= here, it looks backward for 
#include "vtkSomething.h", can't find it and 
calls append-include-vtk, adding it to the beginning 
of the file, then comes back here and expands this line into: */

vtkSomething *smth = vtkSomething::New();

//and let's add another instance of vtkSomething...
vtkSomething *smth2 /*press C-= again, it looks backward for 
#include "vtkSomething", and fails, despite the fact 
that it was added by the previous command. So it adds it again."*/

What am I doing wrong here with search-backward?

(there’s another (at least one) bug in the code, I shouldn’t add the (length includename) if the search-backward for it was successful, but for now I am more interested in how to make it successful, in the first place)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:26:04+00:00Added an answer on May 17, 2026 at 11:26 pm

    OK, I got it. Somehow I got an idea that the third argument of search-backward (noerror) is a callback, which it isn’t. Therefore it is evaluated every time, not just when the search fails. It should be something like this instead:

    (defun expand-vtk ()
      (interactive)
      (setq now (point))
      (setq vtkstart (search-backward "vtk"))
      (setq vtkend (- (search-forward " ") 1))
      (setq vtkname (buffer-substring vtkstart vtkend))
    
      ;check for #include "vtkBlah.h"
      (setq includename (format "#include \"%s.h\"\n" vtkname))
      (if (search-backward includename nil t) 
          (goto-char now) 
          (progn (append-include-vtk includename) 
                 (goto-char (+ now (length includename)))))  
    
      (insert (format "= %s::New();" vtkname)))
    
    (defun append-include-vtk (incname)
      (goto-char 0)
      (insert incname))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my document I have to specify the references I'm using. I used this
Preamble: This is very close to this question , and I have read this
Preamble To build dynamic web-sites, we have to master at least four languages: HTML
Small preamble. I was good java developer on 1.4 jdk. After it I have
I am using ntheorem to typeset a set of conditions. In my preamble I
Preamble: I know, disabling warnings is not a good idea. Anyway, I have a
I am using nokogiri to generate svg pictures. I would like to add the
The preamble: I have designed a strongly interfaced and fully mockable data layer class
Without any preamble I want to show you problem I have in my program,
Preamble: I am aware of using a list or other collections to return a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.