summaryrefslogtreecommitdiff
path: root/README.creole
blob: 14553c2d5ea3f2df880e8704abd295cf7218f1ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
= noflet - Local function decoration =

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.

It's useful for things 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))))
 (progn 
   (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 of course:

{{{
(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.