skeeto / skewer-mode

Live web development in Emacs
The Unlicense
1.09k stars 57 forks source link

evaluate buffer or region? #43

Open sunilw opened 10 years ago

sunilw commented 10 years ago

Just wondering if at some point you will add the ability to evaluate a region or the buffer.

skeeto commented 10 years ago

Just wondering if at some point you will add the ability to evaluate a region or the buffer.

You can evaluate the entire buffer with skewer-load-buffer. It's a little bit smarter than just sending the whole buffer the same way it sends expressions. The buffer is loaded as a script so you get correct line number / file information in your error messages.

I don't see much use in a skewer-eval-region. For most users it's probably a sign that they're doing something wrong, so I don't really want to add one. Fortunately you can provide your own just by dropping this in your Emacs config:

(defun skewer-eval-region (beg end)
  "Execute the region as JavaScript code in the attached browsers."
  (interactive "r")
  (skewer-eval (buffer-substring beg end) #'skewer-post-minibuffer))
romario89 commented 7 years ago

You can evaluate the entire buffer with skewer-load-buffer That (M-x skewer-load-buffer) seems to be necessary for evaluating html tags with C-M-x. I have tried to make C-M-x work for an hour (js2 & C-x C-e was working fine) and it somehow just didn't work. After I've done M-x skewer-load-buffer, it started to work fine. I 've looked back on the READ.md if M-x skewer-load-buffer was talked about no it wasn't. I recommend it be included in READ.md.

skeeto commented 7 years ago

M-x skewer-load-buffer only works with JavaScript, and should only be used with JavaScript. Skewer's JavaScript evaluations normally go through an indirect eval() in the page. For skewer-load-buffer, a snapshot of the buffer is hosted by Skewer and added to the page via a script tag. It's semantically slightly different, but occasionally is convenient or necessary.

It has no meaning for skewer-html-mode. But I'm curious why it seems to fix your problem. Maybe it's some kind of autoload issue?

romario89 commented 7 years ago

What you said eventually turned out to be right: After one or two successfully working C-M-x key presses, it stopped evaluating the html tags.

Now I've restarted Emacs and run-skewer from scratch and did M-x skewer-html-mode and it started to evaluate the html tags (finally :) For instance it evaluated the < b u tton class="myButton" on c lick=" calculate()" type="button" > CALCULATE </ button > successfully.

That's fine. But out of curiosity I reproduced the steps which caused me trouble at first hand. I noticed that if I try to M-C-x on a tag such as on: < s p an class="caption_span">< i>< u >Old Tax Rate | < / u>< / i>< / s pan>< b r > ( where | is the position of cursor) it then stops evaluating all html tags! But if I place the cursor on < / span > tag, then it works! I guess that's where I went into trouble at first hand. Now it's all fine but I think that this small detail be included somewhere in the documentation. My apologies if it was already included but I didn't see it :=)

skeeto commented 7 years ago

Yup, this is a bug. The problem is all those tags being on the same line, which the code was not prepared to handle. It causes the HTML selector to be miscomputed, which then crashes the script, which breaks the long poll loop and prevents further tag evaluation.

I gave the HTML selector stuff an overhaul, this time not being sensitive to newlines. This negative property was inherited from sgml-get-context, itself sensitive to newlines.

kisp commented 1 year ago

Just wanted to note that I did add a skewer-eval-region as defined above together with a (define-key skewer-mode-map (kbd "C-c C-r") 'skewer-eval-region).

I find it useful sometimes to control exactly (and to know beforehand) what will get evaluated.

Of course, I'll run skewer-eval-defun and skewer-eval-last-expression whenever it makes sense. :)