reddit / emacs
Weekly Tips, Tricks, &c. Thread
AutoModerator · 2022-02-16 08:15
This is a thread for smaller, miscellaneous items that might not warrant a full post on their own.
[See this search for previous "Weekly Tips, Tricks, &c." Threads.](https://www.reddit.com/r/emacs/search?q=Weekly+tips&restrict_sr=on&sort=new&t=all)
Don't feel constrained in regards to what you post, just keep your post vaguely, generally on the topic of emacs.
rguy84 · 2022-02-16 14:34
Q: What modes does M-a/e/k actually work in? I only can get it to work in the Tutorial.
pathemata · 2022-02-16 16:47
Regarding org-babel header arguments, let's say we have this
* Heading
:PROPERTIES:
:header-args: :dir org
:END:
** Subheading
:PROPERTIES:
:header-args+: :dir subdir
:END:
#+begin_src sh
pwd
#+end_src
#+RESULTS:
: ~/subdir
Is there any way to have `:dir` work with the hierarchy such that the block result is `~/org/subdir`?
pathemata · 2022-02-16 16:51
Also regarding org-babel, suppose we have
* Heading
:PROPERTIES:
:header-args: :var experiment="exp-1"
:END:
#+name: experiment
#+begin_src python
print(experiment)
#+end_src
#+RESULTS: experiment
: exp-1
#+begin_src python :noweb yes
<<experiment>>
#+end_src
#+RESULTS:
: exp-1
Is there a way to use the value of a variable defined with `:var` as the block name, so we can use noweb with `<<exp-1>>`?
oantolin · 2022-02-16 20:16
They work pretty much everywhere. I use them even in programming modes to deal with the sentences contained in comments and documentation strings. I use them heavily in any mode meant for prose (Org, Markdown, LaTeX, message-mode for email, etc.). There might be something you put in your configuration keeping you from using them. Try first running `emacs -q` to skip loading your configuration; if the commands work there then it's something you did to yourself. :)
rguy84 · 2022-02-16 20:46
Did `-q` as mentioned. Put 21 sentences of lorem ipsum in, `M-a/e` still acts as `C-a/e` in a buffer set as fundamental, org, and text,
oantolin · 2022-02-16 20:48
Weird! Maybe there is some odd system-wide configuration. You could try with `emacs -Q`, with a capital `Q` which skips both your personal configuration and the local system-wide configuration.
rguy84 · 2022-02-16 20:58
Found the reason! By default, it only recognizes a double space after punctuation as a sentence. To make it work with one space, you need `sentence-end-double-space nil`
oantolin · 2022-02-16 20:59
Oh, right! Sorry, I completely forgot not everyone ends sentences with two spaces. :)
rguy84 · 2022-02-16 21:01
Some of us are lazy :P
oantolin · 2022-02-16 21:03
I really shouldn't have forgotten about the two-space thing: I actually set `sentence-end-double-space` to nil in text buffers and to t in Emacs Lisp buffers. (The reason I do it in Emacs Lisp buffers is that Emacs's built-in documentation uses two spaces and I want my documentation to "blend in".)
rguy84 · 2022-02-16 21:17
You're fired! Mods!
lastnamebird · 2022-02-17 03:08
I'm not sure exactly what you want to accomplish, but for python blocks you need to either use `return` with the default header-arg of `:results value`, or change the header to `:results output` and then the print statement should work.
you can find more on this here `(info "(org)Results of Evaluation")`
additionally, maybe this will help point you in the right direction:
* Heading
#+NAME: exp-1
| result-1 |
#+NAME: exp-2
| result-2 |
#+name: exp
#+begin_src python :var experiment=exp-1
return experiment
#+end_src
#+RESULTS: exp
| result-1 |
#+begin_src python :noweb yes :results output
print ('<<exp>>')
#+end_src
#+RESULTS:
: return experiment
#+begin_src python :noweb yes
return <<exp()>>
#+end_src
#+RESULTS:
: result-1
#+begin_src python :noweb yes
return <<exp(experiment=exp-2)>>
#+end_src
#+RESULTS:
: result-2
rguy84 · 2022-02-17 16:28
Q2: is searching like in https://orgmode.org/worg/org-tutorials/advanced-searching.html broken? I added the Eversong headline minus the commas. I pulled up the agenda, did `m` then `BIB_AUTHOR="Walter Evensong"`, and it says no results found.
holgerschurig · 2022-02-17 20:20
I actually think the minority uses it. It's perhaps a custom from 20+ years ago,just like the non-existing (on most current-day keyboards) named "Meta", "Super" and "Hyper" keys ...
holgerschurig · 2022-02-17 20:22
Looked at the title of the post, expected some tips and tricks ... only saw (as of now) 4 questions :_/
rguy84 · 2022-02-17 20:23
>This is a thread for smaller, miscellaneous items that might not warrant a full post on their own.
natarey · 2022-02-17 20:27
This is probably a dumb question, but... how would I write a function to encode an image in base64 -- but with the input being the URL to that image, rather than downloading the original and operating on it as local file?
onearmedphil · 2022-02-17 23:31
Can someone help me with a simple tweak?
In org mode I want M-S-return to always move the marker to the end of the line, then basically run what M-S-return does. I keep pressing it while in the middle of my text and sending half my line down to a new todo.
Visual_Bumblebee_555 · 2022-02-18 16:56
Doesn’t `C-S-return` already do what you describe?
onearmedphil · 2022-02-18 17:45
That appends to the bottom of the list (sorry I didn't specify that's not what I wanted).
So if I have
- [ ] Johnny Appleseed
- [ ] Apple Johnnyseed
and I am in between Johnny Appleseed I want it to go to the end of Appleseed, and run M-S-Return.
crawsphi · 2022-02-18 20:05
I don't know what command that is in org mode, but basically you should be able to do the following:
- `C-h k M-S-return` and note the command name
- Create your own command to wrap that:
(defun oap/eol-and-whatever-m-s-return-does (&optional arg)
(interactive "p")
(move-end-of-line arg)
(whatever-m-s-return-does))
(You might also want to pass `arg` to the wrapped command, if it takes one.)
- Rebind M-S-return to your new command:
(define-key org-mode-map (kbd "<M-S-return>") #'oap/eol-and-whatever-m-s-return-does)
onearmedphil · 2022-02-18 21:36
Thank you. This got it. Didn't know about the arguments for move-end-of-line and org-insert-todo-header. Here's the working code for the internet wanderers.
(defun my/org-insert-todo-heading (&optional arg)
(interactive)
(move-end-of-line arg)
(org-insert-todo-heading nil)
)
(define-key org-mode-map (kbd "<M-S-return>") #'my/org-insert-todo-heading)
harizvi · 2022-02-18 23:13
Just use ./subdir in the subheading and it should work.
[deleted] · 2022-02-21 10:07
[deleted]
onearmedphil · 2022-02-22 14:38
How can I make the "you can run the command with" tips on the bottom last longer? I'm not sure how to search for this..
karthink · 2022-02-22 22:23
(with-current-buffer (url-retrieve-synchronously "https://website.com/image.png")
(base64-encode-region (point-min) (point-max))
(buffer-string))
Make it a function or whatever. Why is it a dumb question?
karthink · 2022-02-22 22:26
There's probably a variable for this, I don't know. However you can access all past echo area strings in the messages buffer, which you can pop up with `C-h e` (for echo) by default.
EDIT: Probably `minibuffer-message-timeout` or `minibuffer-message-clear-timeout`?
natarey · 2022-02-22 23:31
Hmm. So, the output of that code seems to be malformed in some way I can't discern.
Consider this example.
Original Image URL: https://elmah.io/images/logo.png
Output of `cat logo.png | base64` from a local copy of the image:
iVBORw0KGgoAAAANSUhEUgAAAFIAAAA8CAMAAADCDB2DAAAArlBMVEUAAAD///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////8tivQqAAAAOXRSTlMA0fz1BvkKiQOcEO+hX1i9lYFoJ+bgJBvcz7KljnFrOBPq49jFwbp3RC7VrYVTSjUgF8owe088K3yG+Nb0AAADGElEQVRYw63Y6XaqMBQF4CMCgggqBes8W2ett3bY7/9i17iQQEmCaL9f7Sqmp8nJPqySwvuw36tvNvNuw32hP/BR2SBhunun57R6yOg+U2q1oUHAqJj0oJcZJGYPFrpyIOV80gNcDQqa90CNGpSMQ+GjdpCj/E2FmBvkKulURAV3qFABr2XcwbhcJMurtctasDjntZWNu9i0b8ctYJ+UZ6PhLkYtdf0/SG6Jh5SPJLXIlGOPqubLsA214FWaFhrSttGjeg1qPZLYI23BG3AXVe0eA4jItnOAlLBKXAdMk+gsb9X8Pvczf8HUJPoHkQ2J/dqxcTJCB2yfPSJX0lYk9oakKUVObi0AsyAypxCrklA9neB0YR76pds1+bcmaspiVBwl+jzdwProZ2FE37Qb+7EqA9okwK/tLDqB28edrf9+y2dIvInavItIR6dRfKxGfXm0EnFqQGxAGesQ8ZJsCTdgX1VWpmq7OWecXXHy+xqaA+/22Mkz84LFtjIvP5P0wfwki2sAK7o5QsypHdNHnbkR4T51Tfl1sxwENXctiurud87AOfBNAToUO1w/qAtj0+eXTXSMfb7K5HY3uaZsgES6wslKsR673Nyr2wsg0YiKhBAfUi5gq9uIW6pGt8vLAsJktqgY1yyWZEuNrzIDWomeUipZl56EULhLdSY/zCHUPCJPkFRbv0UJn8D27iU7wn5YZSenY/HBlmNN/WyRrAtbfpOvOQdGfJrkGIgemft2eFn5NdnZZ4q0ulDrkp3TY8wXmzuxVQkqm0sFMm09zpUyDDNKOpYdwwnkJqoT5AHUveXIstxka1fnim6/dIiUFp/JIMoRnw1jj22FokrWIVLxkt/XHNGb0fGNFO9d7MEFFGPPjPoxAFr+jNcPuTfVe+rXJVPCJpuQ+pYvk+eHjXv5rxtffzYJUMCapZu8yB0KC69X2IFI/VKkg8L8KLZFvIeKnFmKWLXpI69IxWS1hOkfllHYmSdiHX+iR5zVwB+wdUoalvEkLfNGeGpoeMZ8RFmtfvvhCuufJPHC/ifWKRXRqff6XpW4/9cakQBazOzbAAAAAElFTkSuQmCC
Output of provided code:
SFRUUC8xLjEgMjAwIE9LCkNvbnRlbnQtTGVuZ3RoOiAxMTA0CkNvbnRlbnQtVHlwZTogaW1hZ2UvcG5nCkRhdGU6IFR1ZSwgMjIgRmViIDIwMjIgMjM6MjQ6MjEgR01UCkFjY2VwdC1SYW5nZXM6IGJ5dGVzCkNhY2hlLUNvbnRyb2w6IHB1YmxpYywgbWF4LWFnZT0yNTkyMDAwCkVUYWc6ICIxZDgwYjdjNmY2MjZjNTAiCkV4cGlyZXM6IFRodSwgMjQgTWFyIDIwMjIgMjM6MjQ6MjIgR01UCkxhc3QtTW9kaWZpZWQ6IE1vbiwgMTcgSmFuIDIwMjIgMDg6MzA6MDggR01UClN0cmljdC1UcmFuc3BvcnQtU2VjdXJpdHk6IG1heC1hZ2U9MjU5MjAwMApYLUNvbnRlbnQtVHlwZS1PcHRpb25zOiBub3NuaWZmCgqJUE5HDQoaCgAAAA1JSERSAAAAUgAAADwIAwAAAMIMHYMAAACuUExURQAAAP///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////y2K9CoAAAA5dFJOUwDR/PUG+QqJA5wQ76FfWL2VgWgn5uAkG9zPsqWOcWs4E+rj2MXBundELtWthVNKNSAXyjB7TzwrfIb41vQAAAMYSURBVFjDrdjpdqowFAXgIwKCCCoF6zxbZ623dtjv/2LXuJBASYJov1/tKqanyck+rJLC+7Dfq282827DfaE/8FHZIGG6e6fntHrI6D5TarWhQcComPSglxkkZg8WunIg5XzSA1wNCpr3QI0alIxD4aN2kKP8TYWYG+Qq6VREBXeoUAGvZdzBuFwky6u1y1qwOOe1lY272LRvxy1gn5Rno+EuRi11/T9IbomHlI8ktciUY4+q5suwDbXgVZoWGtK20aN6DWo9ktgjbcEbcBdV7R4DiMi2c4CUsEpcB0yT6Cxv1fw+9zN/wdQk+geRDYn92rFxMkIHbJ89IlfSViT2hqQpRU5uLQCzIDKnEKuSUD2d4HRhHvql2zX5tyZqymJUHCX6PN3A+uhnYUTftBv7sSoD2iTAr+0sOoHbx52t/37LZ0i8idq8i0hHp1F8rEZ9ebQScWpAbEAZ6xDxkmwJN2BfVVamars5Z5xdcfL7GpoD7/bYyTPzgsW2Mi8/k/TB/CSLawArujlCzKkd00eduRHhPnVN+XWzHAQ1dy2K6u53zsA58E0BOhQ7XD+oC2PT55dNdIx9vsrkdje5pmyARLrCyUqxHrvc3KvbCyDRiIqEEB9SLmCr24hbqka3y8sCwmS2qBjXLJZkS42vMgNaiZ5SKlmXnoRQuEt1Jj/MIdQ8Ik+QVFu/RQmfwPbuJTvCflhlJ6dj8cGWY039bJGsC1t+k685B0Z8muQYiB6Z+3Z4Wfk12dlnirS6UOuSndNjzBebO7FVCSqbSwUybT3OlTIMM0o6lh3DCeQmqhPkAdS95ciy3GRrV+eKbr90iJQWn8kgyhGfDWOPbYWiStYhUvGS39cc0ZvR8Y0U713swQUUY8+M+jEAWv6M1w+5N9V76tclU8Imm5D6li+T54eNe/mvG19/NglQwJqlm7zIHQoLr1fYgUj9UqSDwvwotkW8h4qcWYpYtekjr0jFZLWE6R+WUdiZJ2Idf6JHnNXAH7B1ShqW8SQt80Z4amh4xnxEWa1+++EK658k8cL+J9YpFdGp9/pelbj/1xqRAFrM7NsAAAAASUVORK5CYII=
The Emacs output is about 425 characters longer, and won't decode to the original image. I've also noticed that when I encode other test images with the Emacs code, they all begin with `SFRUUC8xLjEgMjAwIE9LC`.
Any ideas why the output would be so different? Is it getting the image container in addition to the contents of the image?
(It seemed like a dumb question, because I thought there was something obvious I was missing about getting the image from a URL rather than from a file. Which kinda turned out to be the case, because I didn't think of `url-retrieve-synchronously`, ha)
onearmedphil · 2022-02-22 23:47
Oh cool thank you
MistakeNotDotDotDot · 2022-02-23 06:59
`(url-retrieve-synchronously)` gives you a buffer with both the headers *and* the actual contents. You need to do
(with-current-buffer (url-retrieve-synchronously "https://website.com/image.png")
(base64-encode-region (1+ url-http-end-of-headers) (point-max))
(buffer-string))
(the `1+` is because otherwise it'll include the newline between the headers and the body).
To be honest, looking into this, I'd probably just call out to `curl`. Emacs's networking library is... not the best.
natarey · 2022-02-23 20:31
Was fun trying to figure out how to get it done with Emacs stuff only, but the `curl`-based solution I ended up with at your suggestion worked way better. Thanks for the help!