1

I was wondering if are existing tools or package to write C++ (or other langage) code from lisp.

For example, this afternoon I needed to create 4 basic classes:

class D{
public:
    D(){
    }
};

class C{
public:
    C(){
    }
};

class B{
public:
    B(){
    }
};

class A{
public:
    A(){
    }
};

Using lisp it would be fairly easy to create a function that takes as an argument the class name and generate the template code from one instance:

class $TAG{
public:
    $TAG(){
    }
};

where $TAG is going to be replaced by the class name.

I am not really looking at the exact lisp code to produce this because I can code it myself but rather at a package about this philosophy and with extra stuff I haven't thought of.

Please share your thoughts

9

It sounds like a job for YASnippet

The philosophy behind it is to shorten the amount of keystrokes to enter frequently used structures and to not repeat yourself while writing the boilerplate code.

YASnippet comes with a lot of useful snippets preloaded for many major modes. In case of C++ it means that you can type for example cls and Tab and it will expand to

class Name
{
public:
  Name();
  virtual ~Name();
};

The cursor will be positioned on Name and as you type your name it will replace all occurrences of Name to create a valid C++ class.

To initialize YASnippet I use the following code:

(use-package yasnippet
  :ensure    yasnippet
  :init      (setq yas-verbosity 1)
  :config    (progn
               (yas-global-mode 1)
               (let* ((yas-elpa-snippets-folder (car (file-expand-wildcards
                                                      (concat user-emacs-directory "elpa/yasnippet-*/snippets"))))
                      (yas-folder-candidates  `(,yas-elpa-snippets-folder
                                                ,(concat user-emacs-directory "snippets"))))

                 (cl-loop for p in yas-folder-candidates
                          unless (file-exists-p p)
                          do (cl-delete p yas-folder-candidates))

                 (setq yas-snippet-dirs yas-folder-candidates))
               (yas-reload-all)
               (define-key yas-minor-mode-map (kbd "C-c <tab>") 'yas/insert-snippet))
  :diminish  yas-minor-mode)

I've adapted it from what I found on the internet, probably from emacs-rimero-setup. It uses the wonderful use-package to install and initialize YASnippet.

  • Great that's exactly what I was looking for! – wizmer Apr 6 '16 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.