reddit / emacs
Does Emacs have some way of attaching code and data to a buffer?
vfclists · 2022-06-30 18:25
Is there some kind of built-in framework of associating some data and code with a buffer which can be might to bring it up to date?
I am creating some buffers which are created from some specific code and data, and I want to update those buffers by executing some kind of "refresh" function that will update the buffers.
Eg. I have a list of documents match some criterion and I am usually adding documents and removing them programmatically. So after a few updates I want to update the view by going to the display page and refreshing it. It would be easy if the function calls and paramters which update the view could be attached to the view display and update it automatically.
**Update to question:**
I have some read-only buffers which displays programmatically generated org tables from plists which contain a table of a list of org files, some of their header data and links to open them. Files are regularly added and removed from these plists, so I just want to visit the buffers and execute some kind of `refresh view` command and have the views updated. So the buffers should have the views parameters attached to them, namely the views selection criteria the columns to be displayed.
**The difference from using file-local variables is that those parameters should not be contained in the buffers**, but should be uniquely related to the buffers. Emacs some kind of unique hex numbers associated with objects, can some data be attached to those hex numbers? It is more like having the buffers being objects of some kind of classes and attaching methods to them, one of which is refreshing the views.
I may have about five or six of these kind of buffers open, and I want to just visit them and run the `refresh view` command on them.
eli-zaretskii · 2022-06-30 18:38
File-local variables?
00-11 · 2022-06-30 18:40
Maybe use `change-major-mode-hook` to do what you need, conditional on the buffer name or whatever?
nv-elisp · 2022-06-30 18:50
Try rephrasing your question with a concrete example. It's a bit too abstract as it stands.
ieure · 2022-06-30 19:02
You probably need buffer-local variables, but your question isn't phrased well, so it's hard to tell if that's what you really need.
chrinto · 2022-06-30 20:28
buffer-local variables, you can put code in variables as well
MitchellMarquez42 · 2022-06-30 20:47
`file-changed-hook`
00-11 · 2022-06-30 23:27
Is the question only about file buffers?
rswgnu · 2022-06-30 23:28
With the Hyperbole package, you can add Lisp function calls as buttons at the top of your view buffer to run anything you like, just change the outer parens to angle brackets and then press a mouse button or M-RET within the call to execute it.
MitchellMarquez42 · 2022-06-30 23:30
I have no idea; OP was very vague. I'm just throwing stuff out here.
00-11 · 2022-07-01 01:01
I understand. And it might well be what was meant, in which case it will likely help. My question should really have been for OP.
ideasman_42 · 2022-07-01 04:49
Try: [https://codeberg.org/ideasman42/emacs-sidecar-locals](https://codeberg.org/ideasman42/emacs-sidecar-locals)
It's similar to file/directory locals but supports out of source code.
cazzipropri · 2022-07-01 05:41
It sounds like you want to write your own minor mode.
organic-bookworm · 2022-07-01 08:09
>Is there some kind of built-in framework of associating some data and code with a buffer
>I want to update those buffers by executing some kind of "refresh" function that will update the buffers
See [Buffer-Local Variables](https://www.gnu.org/software/emacs/manual/html_node/elisp/Buffer_002dLocal-Variables.html), [Reverting](https://www.gnu.org/software/emacs/manual/html_node/elisp/Reverting.html) and [`revert-buffer-function`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Reverting.html#index-revert_002dbuffer_002dfunction) variable (which is always *buffer-local*).
Also, you might want to use a [lexical closure](https://www.gnu.org/software/emacs/manual/html_node/elisp/Closures.html) as `revert-buffer-function`.
arthurno1 · 2022-07-01 09:50
It is really hard to understand what you are asking for, partly because of your English and partly because you don't seem to understand what you would like to do yourself. I am not a native English speaker myself, and I am guilty of posting some hard to understand crap myself, so my tip to you, from my own experience, is to write your post in Emacs or elsewhere, leave it up there for a while, come back after a few hours or a day and try to answer your post yourself. First, when you think it is well-formed and easily understandable, post it away. Presumably it should be written so that Winnie the Pooh can understand it.
> Is there some kind of built-in framework of associating some data and code with a buffer which can be might to bring it up to date?
Yes. It is called Emacs. It comes with a scripting language called Emacs Lisp which you can use to associate data with the code and implement some custom functionality that suits your needs. Jokes aside, there are many ways to "associate data with the code" and perform some update on the buffer, it really depends on your needs and preferences. You are asking if there is a way to script a buffer. It is too broad and general question.
> I have a list of documents match some criterion and I am usually adding documents and removing them programmatically. So after a few updates I want to update the view by going to the display page and refreshing it. It would be easy if the function calls and paramters which update the view could be attached to the view display and update it automatically.
Now the problem here to understand is what is "display page" and what is "view display". You seem to be using it interchangeably, but no idea what you mean with it. Buffer in Emacs? Some web page in your browser? What are you talking about?
If we are talking about displaying a buffer in Emacs, and if you add/remove documents programmatically as you claim you do, then you can always programmatically revert/refresh the buffer yourself at the end of your add/removal.
Another option is to use hooks. Emacs has [some hooks](https://www.gnu.org/software/emacs/manual/html_node/elisp/Standard-Hooks.html) that are called after buffer is changed, that might be useful to you. Check for example after-change-functions. You can add your own refresh function there.
> I am creating some buffers which are created from some specific code and data, and I want to update those buffers by executing some kind of "refresh" function that will update the buffers.
There is also [inotify & co](https://www.gnu.org/software/emacs/manual/html_node/elisp/File-Notifications.html) interface which you could use to plug in some functionality to.
vfclists · 2022-07-01 12:04
Update to question:
I have some read-only buffers which displays programmatically generated org tables from plists which contain a table of a list of org files, some of their header data and links to open them. Files are regularly added and removed from these plists, so I just want to visit the buffers and execute some kind of `refresh view` command and have the views updated. So the buffers should have the views parameters attached to them, namely the views selection criteria the columns to be displayed.
**The difference from using file-local variables is that those parameters should not be contained in the buffers**, but should be uniquely related to the buffers. Emacs some kind of unique hex numbers associated with objects, can some data be attached to those hex numbers? It is more like having the buffers being objects of some kind of classes and attaching methods to them, one of which is refreshing the views.
I may have about five or six of these kind of buffers open, and I want to just visit them and run the `refresh view` command on them.
vfclists · 2022-07-01 12:11
https://www.reddit.com/r/emacs/comments/vof7kh/does_emacs_have_some_way_of_attaching_code_and/iefxb3h/
vfclists · 2022-07-01 12:11
https://www.reddit.com/r/emacs/comments/vof7kh/does_emacs_have_some_way_of_attaching_code_and/iefxb3h/
vfclists · 2022-07-01 12:12
The variables are not supposed to be present in the buffer.
vfclists · 2022-07-01 12:18
The associated data and methods are not suppossed to be stored in the buffer, but attached to some data structure associated with the buffer.
https://www.reddit.com/r/emacs/comments/vof7kh/does_emacs_have_some_way_of_attaching_code_and/iefxb3h/
arthurno1 · 2022-07-01 12:57
> It is more like having the buffers being objects of some kind of classes and attaching methods to them, one of which is refreshing the views.
That is exactly what you are already suggested by others: buffer local variables. That is what they are for. I think you have to learn to differentiate between the textual content in the buffer displayed to the user, and the lisp object which can contain variables and objects not displayed in the buffer.
_viz_ · 2022-07-01 13:09
IIUC, you have a variable that you programatically update at times and
when this variable changes you want to update the buffer that shows
this variable in a nice format?
If so, I have a question: why not update the buffer when you update
the variable?
But anyway, you should be able to do this with a variable-watcher I
think.
onetom · 2022-07-01 14:26
https://youtu.be/cVy-kMSnaHc
starting from 52:05 the presenter talks about Text properties (and overlays too) and also demonstrates them.
i have the gut feeling that these facilities might be useful for what you want.
he describes a use-case where he was folding verbose, uninteresting regions of some logfile, but still wanted to show in place of the hidden content, then number of entries being hidden.
that sounds very much like the data association your were describing.
and since code is also data, that should be possible to store the same way.
chrinto · 2022-07-01 15:54
Ok you can have a global hash-map variable and store buffer -> list of variables which will be the same kind of solution what buffer-local variabels but without using buffer-local variables. You can use the buffer-file-name or something like that as key in the map