Emacs Lisp search back

Preamble

Using the VTK library with C ++, I often write something like this:

vtkInteractorStyleRubberBandZoom *isrbz = vtkInteractorStyleRubberBandZoom::New();

In addition, 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 can I automate it, so I have to type each of the painfully long class names once instead of three?

I tried recording Little Emacs mode for him. There are probably already existing solutions (YaSnippet?), But I thought that writing myself would also be a good exercise.

the 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 finding the name include 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 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 looking back?

( ( ) , (length includename), , , , )

+3
2

, . - , (noerror) - , . , . - :

(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))
+2

, Emacs, dabbrev-expand ( M-/):

(dabbrev-expand ARG)

Expand previous word "dynamically".

Expands to the most recent, preceding word for which this is a prefix.
If no suitable preceding word is found, words following point are
considered.  If still no suitable word is found, then look in the
buffers accepted by the function pointed out by variable
`dabbrev-friend-buffer-function'.

vtkInteractorStyleRubberBandZoom , , , vtkI M-/.

+1

Source: https://habr.com/ru/post/1772588/


All Articles