Giter VIP home page Giter VIP logo

emacs-noflet's Introduction

noflet - Local function decoration

noflet is dynamic, local, advice for Emacs-Lisp code.

noflet also has an Emacs indentation function for flet like macros.

It's great for test code when you need to mock another function.

This is useful for definining functions that overide a base definition in some way. You get access to the original (before you re-defined it) function through a different name.

Use it like this:

(noflet ((find-file-noselect (file-name)
           (if (string-match-p "^#.*" file-name)
               (this-fn "/tmp/mytest")
               (this-fn file-name)))
         (expand-file-name (file-name &optional thing)
           (if (string-match-p "^#.*" file-name)
               (concat "/tmp" (file-name-as-directory 
                               (substring file-name 1)))
               (funcall this-fn file-name thing))))
  (expand-file-name "#/thing"))

This specifies that two functions should be overridden:

  • find-file-noselect is changed so that if the file-name begins with # a different file-name altogether is opened
  • expand-file-name is changed so that if the file-name begins with # it's resolved via /tmp

In both cases this-fn is used to access the original function definition of these common Emacs functions.

Decorating results

noflet can also be used to decorate results, just like around-advice:

(noflet ((find-file (file-name &optional wildcards)
           (let ((result (funcall this-fn file-name wildcards)))
              (with-current-buffer result
                 (setq some-buffer-local "special"))
              result)))
    (with-current-buffer (find-file "~/some-file")
      (message "buffer local var is: %s" some-buffer-local)))

This overrides find-file to set a local variable. There are surely better ways to do it than this but it illustrates the point.

Lexical version

Because we include a good indenting function we also include a lexical flet. It's just a wrapper for cl-flet.

emacs-noflet's People

Contributors

darwinawardwinner avatar nicferrier avatar purcell avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.