reddit / emacs
Weekly Tips, Tricks, &c. Thread
AutoModerator · 2022-05-18 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.
diamondnbond · 2022-05-18 11:38
[I Recently discovered engine-mode.](https://github.com/DiamondBond/emacs/blob/master/config.org#initialize-engine-mode)
bgcartman · 2022-05-18 13:45
I little advice I use in org-mode to auto load org-babel modules only when needed.
```
(defadvice org-babel-execute-src-block (around load-language nil activate)
"Load ob-{language} only when needed."
(let ((lang (org-element-property :language (org-element-at-point))))
(when (or (string= lang "bash") (string= lang "sh")) (setq lang "shell"))
(unless (cdr (assoc (intern lang) org-babel-load-languages))
(add-to-list 'org-babel-load-languages (cons (intern lang) t))
(org-babel-do-load-languages 'org-babel-load-languages org-babel-load-languages))
ad-do-it))
```
Saves me some time and speeds up loading org-mode as a nice bonus.
P.S. Speaking of speeding up the loading of org-mode I found out that setting this before loading it helps a lot. Obviously one should modify it to include used modules/back-ends:
```
(setq org-modules nil
org-export-backends '(html)))
```
keepitsalty · 2022-05-18 17:10
Is there a way to keep a queue of window configurations?
Suppose I have two files open side by side and then want to switch to just one window for a different file. I would like to be able to swap back and forth between the two window configurations their respective files open.
thetemp_ · 2022-05-18 20:26
Tab-bar-mode does this. You can turn off tab visibility and just move between different window configurations.
mkthree · 2022-05-18 21:11
I came up with this "delete-windows-dwim" kind of command and use it all the time. Maybe someone else will find it useful. I usually use Emacs with two windows side-by-side, but occasionally other I'll open a third window, or a side window will pop up. When I want to get rid of these extra windows, I'll use this:
;; These first two are helper functions
(defun my-delete-side-windows ()
"Delete all side windows."
(dolist (win (window-list))
(when (window-parameter win 'window-side)
(delete-window win))))
;; Uses -any from dash.el
(defun my--has-side-window-p ()
"Return non-nil if there is currently at least one side window being displayed."
(-any? (lambda (win) (window-parameter win 'window-side)) (window-list)))
;; This is the main command
(defun my-delete-other-windows ()
"If there are any side windows, delete all of them. Otherwise, if
there are only two windows, delete the non-selected one.
Otherwise, delete all but two windows."
(interactive)
(if (my--has-side-window-p)
(my-delete-side-windows)
(let ((windows (window-list)))
(if (<= (length windows) 2)
(delete-other-windows)
(dolist (win (cddr windows))
(delete-window win))))))
I bind `my-delete-other-windows` to `C-\`. Then I just press it until I've deleted the windows that I want to (first side windows, then all but two windows, then all but the current window).
organic-bookworm · 2022-05-19 08:33
I use `org-store-link` and `org-insert-link-global` globally to have `org-mode` links in arbitrary buffers and [orglink](https://github.com/tarsius/orglink) to activate them, i. e. add the same text properties, as `org-mode` does.
It's pretty obvious, but I've just recently realized I can now also use `org-toggle-link-display` globally to hide or show parts of link text.
Phil-Hudson · 2022-05-19 11:11
I have an Org file I use to tangle out consistent engine/webjump definitions to Emacs, browsers and browser extensions, and my WM (StumpWM) -- anywhere it makes sense to. By "consistent" I mean not just the same URL template but also the same keybinding (if anything comparable is available; engine offers the `:key` option) and the same results browser override (ditto, `:browser`).
xamac · 2022-05-19 16:57
And have a look at desktop-save-mode...
pathemata · 2022-05-20 07:01
About `outline-minor-mode` in `python-mode`.
Does anyone have a good logic for `outline-regex` to avoid it getting keywords like "if" or "for" inside the docstring?
Reddit-Mods-Eat-Shit · 2022-05-20 09:36
How about registers? You can bind keys 1 through 9
Although there's no next and previous register command.
I wrote some code to do this a while back. Not sure if I still have it...
keepitsalty · 2022-05-20 12:43
Ah yes I’ve played with registers before. I would love to see the code if you find it. No worries if you can’t find it!
natarey · 2022-05-20 16:02
I took a stab at re-formatting that for you:
(defadvice org-babel-execute-src-block (around load-language nil activate)
"Load ob-{language} only when needed."
(let ((lang (org-element-property :language (org-element-at-point))))
(when (or (string= lang "bash") (string= lang "sh"))
(setq lang "shell"))
(unless (cdr (assoc (intern lang) org-babel-load-languages))
(add-to-list 'org-babel-load-languages (cons (intern lang) t))
(org-babel-do-load-languages 'org-babel-load-languages org-babel-load-languages))
ad-do-it))
CulturMultur · 2022-05-20 19:38
I use these two helper functions to calculate something quickly using [Calculator](https://www.gnu.org/software/emacs/manual/html_node/calc/index.html). Sometimes I use crux-eval-and-replace or embark-eval-replace to evaluate S-expressions.
```
(defun calc-eval-selection (beg end)
"Evaluate region from BEG to END using 'calc-eval' appending results."
(interactive "r")
(let ((results (calc-eval (buffer-substring-no-properties beg end) " = ")))
(goto-char end)
(insert (format " = %s" results))))
(defun calc-eval-and-replace (beg end)
"Evaluate region from BEG to END using 'calc-eval' replacing region with results."
(interactive "r")
(let ((results (calc-eval (buffer-substring-no-properties beg end) " = ")))
(delete-region beg end)
(goto-char beg)
(insert results)))
```
cabbagegandalf · 2022-05-20 23:25
Hey all, I'm trying to setup a keybinding on the minibuffer to open the selected entry in another window.
For example: if I call \`find-file\` and select a file in the listing then press <C-v> I'd like to open as if it wash \`find-file-other-window\`, is that possible?
I'm currently using vertico+embark and I know I can call the embark map but that would require more keystrokes.
oantolin · 2022-05-21 04:23
> I'm currently using vertico+embark and I know I can call the embark map but that would require more keystrokes.
That would require _one_ more keystroke. :)
But if you want to avoid even that keystroke:
Option 1: Use a keyboard macro! This is by far the easiest way. If your binding for `embark-act` is, say, `C-.`, then you can use:
(define-key minibuffer-local-map (kbd "C-v") (kbd "C-. o"))
Option 2: Define a command reusing some Embark facilities:
(defun find-current-target-other-window ()
(interactive)
(embark--act #'find-file-other-window (car (embark--targets))))
(define-key minibuffer-local-map (kbd "C-v")
#'find-current-target-other-window)
I wrote about this here on r/emacs [last year](https://www.reddit.com/r/emacs/comments/pac8kp/comment/ha3xx9j/?utm_source=share&utm_medium=web2x&context=3), with a bit of discussion of other options and what Embark buys you here.
MhvxvLvJrg · 2022-05-21 13:32
winner-mode could help you
jumpUpHigh · 2022-05-22 19:24
Is the `geo:` URI defined in orgmode links?
I found [this](https://list.orgmode.org/87k4m57z7x.fsf@kotik.lan/#t) in the mailing list, but I couldn't find it any official repos
peterhoeg · 2022-05-23 02:07
Here's my take on a similar thing. Obviously quite opinionated:
```elisp
(defun +close-dwim ()
"Be clever and close the right thing."
(interactive)
(cond
;; these kill *another* thing, not the current
;; minibuffer
((minibufferp)
(minibuffer-keyboard-quit))
;; flycheck
((bound-and-true-p +flycheck-error-list-visible-p)
(+flycheck-toggle-error-list))
;; org-roam backlinks (we could just use bound-and-true-p)
((and (fboundp '+org-roam-visible-p)
(fboundp 'org-roam-buffer-deactivate)
(+org-roam-visible-p))
(org-roam-buffer-deactivate))
;; these kill *this* thing
;; split
((> (count-windows) 1)
(if (bound-and-true-p evil-mode)
(evil-window-delete)
(delete-window)))
;; tab-bar
((and (bound-and-true-p tab-bar-mode)
(> (length (tab-bar-tabs)) 1))
;; we don't use +tab-bar-close-tab as it doesn't actually remove the tab in
;; question, only the buffer
(tab-bar-close-tab))
;; centaur tabs
((and (bound-and-true-p centaur-tabs-mode)
(> (length (centaur-tabs-tabs (centaur-tabs-current-tabset))) 1))
(kill-buffer))
;; atomic-chrome
((bound-and-true-p atomic-chrome-edit-mode)
(atomic-chrome-close-current-buffer))
;; workspaces
((and (fboundp '+workspace-list-names)
(> (length (+workspace-list-names)) 1))
(+workspace/close-window-or-workspace))
;; frame
(t (delete-frame))))
```
columbcille · 2022-05-24 11:53
Hello! Newbie here. Can anyone point me toward a package that would put a buffer in a persistent sidebar? I’m thinking a sidebar like treemacs, but I can specify the content to be displayed (say, a file that acts like a table of contents).
pinlord · 2022-05-24 13:04
sounds like the included 'speedbar':
Mx-speedbar
(or from the menu: Options>show/hide>speedbar)
karthink · 2022-05-24 18:03
`my-delete-side-windows` can be replaced with the built-in `window-toggle-side-windows`.
vjgoh · 2022-05-24 22:35
I've started working with Unreal, and it has stuff like
UCLASS(<some stuff>)
[Normal line of C++];
How do I get emacs to treat those special declarations like they're terminated with a semicolon? (The compiler will complain if I put a semicolon there myself, and it would be at odds with how everyone else is coding anyway.)