reddit / emacs
Weekly Tips, Tricks, &c. Thread
AutoModerator · 2022-07-20 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.
[deleted] · 2022-07-20 20:51
[deleted]
sauntcartas · 2022-07-20 21:03
I sometimes want to pick a random choice from a long org-mode checkbox list from among those items not already checked. I recently whipped up a little helper function for that:
(defun random-choice ()
(interactive)
(let ((n 0)
(selected nil))
(while (search-forward-regexp (rx point "- [" (group nonl) "]" (+ nonl) "\n") nil t)
(when (and (string= " " (match-string 1)) (zerop (random (setq n (1+ n)))))
(setq selected (match-beginning 0))))
(if selected
(goto-char selected)
(error "No unfinished choice found"))))
ispinfx · 2022-07-21 00:51
Bar cursor gets lost quite often when a file is auto reverted due to external modification. How can I fix this? The issue does not happen for box cursor or other types.
[deleted] · 2022-07-21 17:43
Today I discovered by accident (and not in the info manual) that we can fill a whole Org tree (incl. sub-sections/-headings) with command \``fill-region`' and point on the headline. No active region is required.
agumonkey · 2022-07-21 21:17
you can have an org-mode file in source block in an org file
trae · 2022-07-21 22:23
I don't get it. I just spent a few minutes reading the docs, and trying to understand what it does...reformats paragraphs somehow?
trae · 2022-07-21 23:16
I'm playing around with swiper, and helm-swoop. There's a noticeable delay when I invoke either. I'm not using a huge file either, around 300 lines. Is this normal?
TeMPOraL_PL · 2022-07-21 23:29
Yes. "Filling" is Emacs term for automatically rearranging newlines in a block of text (typically a paragraph) as to keep each line under `fill-column` characters length (80 by default, IIRC).
azhake · 2022-07-22 08:26
If you are running Emacs in the Terminal and using dark and light modes the following function will switch the cursor color for you. This example uses modus-themes, but you can obviously replace `modus-operandi` and `modus-vivendi` for any other pair of themes.
(defun aj8/modus-themes-custom-settings ()
"If in terminal, change cursor color on theme switch."
(when (not (display-graphic-p)) ; if using terminal
(cond
((string-match-p "modus-operandi" (symbol-name (modus-themes--current-theme)))
(send-string-to-terminal "\033]12;black\007"))
((string-match-p "modus-vivendi" (symbol-name (modus-themes--current-theme)))
(send-string-to-terminal "\033]12;white\007"))
(t
(error "Unknown modus-theme name")))))
(add-hook 'modus-themes-after-load-theme-hook #'aj8/modus-themes-custom-settings)
tryptych · 2022-07-22 12:25
Emacs has better long-lines support now??
I just noticed while looking at the latest additions in the NEWS:
>\*\* Emacs is now capable of editing files with arbitrarily long lines. The display of long lines has been optimized, and Emacs no longer chokes when a buffer on display contains long lines. If you still experience slowdowns while editing files with long lines, this is either due to font locking, which you can turn off with M-x font-lock-mode or C-u C-x x f, or to the current major mode or one of the enabled minor modes, in which case you should open the the file with M-x find-file-literally instead of C-x C-f. The variable 'long-line-threshold' controls whether and when these display optimizations are used.
That sounds like great news! Does anyone know what went into it?
(edit to add: this was added some time this week. I rebuild from master weekly, and check out the NEWS diff each time)
paretoOptimalDev · 2022-07-22 21:42
Oh, people do this manually instead of using word wrap or truncate lines?
Do you know why?
snippins1987 · 2022-07-23 09:05
The [commit](https://github.com/emacs-mirror/emacs/commit/616da8fa8efa9023f56fa731072d877e2150afbc) said something about pretends the buffer is narrowed where there are long lines to improve performance.
I don't really understand how it works though.
tryptych · 2022-07-23 11:18
Thanks -- yeah I'm not going to pretend I understand any of the display code though.
_viz_ · 2022-07-23 15:33
I prefer not to have neck pain. Jokes aside, I find it genuinely hard to read when the text spans too long across the window.
_viz_ · 2022-07-23 15:39
Profiler is your friend here. Start the profiler, do the usual stuff, poke around in the report. If the function that takes the most amount of time is labelled as #<compiled function some-junk> then evaluate the corresponding function using C-M-x and repeat the experiment. You can usually find the identity of that function by looking higher up in the hierarchy.
kundry_wag · 2022-07-25 01:50
I'm wondering if this affects only *files*, as the NEWS entry says, or if it impacts any buffer, even if it does not visit a file.
Kyo91 · 2022-07-25 12:58
Great news, but I thought font-lock-mode was already the #1 culprit of choking on long lines (which this doesn't resolve)?
mmarshall540 · 2022-07-25 17:31
I'm learning about GNU Hyperbole and found this tip about using it with Avy:
You can combine Hyperbole with Avy by creating an avy-dispatch function to press the Hyperbole action-key at target.
(add-to-list 'avy-dispatch-alist '(?: . (lambda (pt)
(goto-char pt)
(hkey-either))))
This comes from [a recent message](https://lists.gnu.org/archive/html/emacs-orgmode/2022-06/msg00686.html) on the org-mode mailing list posted by Juan Manuel Macías.
tryptych · 2022-07-25 21:40
I had that thought in the back of my mind too, actually.
nnreddit-user · 2022-07-25 22:23
Auto-translate hook for reading emacs-china.org:
(defun translate-emacs-china ()
(when (equal (gnus-method-to-server-name
(gnus-find-method-for-group gnus-newsgroup-name))
(gnus-method-to-server-name '(nndiscourse "emacs-china.org")))
(require 'google-translate)
(with-current-buffer gnus-article-buffer
(save-mark-and-excursion
(article-goto-body)
(cl-destructuring-bind (beg . end)
(last (mapcar #'car (save-excursion (gnus-dissect-cited-text))) 2)
(let ((inhibit-read-only t)
kill-ring)
(delete-region (point) beg)
(save-excursion
(set-mark (point))
(goto-char (or (car end) (point-max)))
(setq deactivate-mark nil)
(let (google-translate-pop-up-buffer-set-focus
google-translate-show-phonetic
google-translate-display-translation-phonetic
(google-translate-output-destination 'paragraph-insert))
(save-excursion
(with-silent-modifications
(google-translate-at-point))))
(kill-region (point) (point-max)))
(forward-line -1)
(yank)))))))
(add-hook 'gnus-article-prepare-hook #'translate-emacs-china)
Tohiko · 2022-07-26 08:05
Is it correct practice to use `require` inside functions? I have a custom function that uses two package (org and consult) which I lazy-load.
If the function is used, these packages should be loaded. I currently `require` them inside the function so that the packages are not loaded otherwise. Is this correct practice? Is there a better way to define function dependencies?
northgaard1 · 2022-07-26 10:57
If whatever you are invoking from org or consult in your function is autoloaded, then the `require` is redundant. Otherwise, yes, using `require` within a function is fairly standard practice, since it will simply be a no-op if the package(s) are already loaded.
Tohiko · 2022-07-26 15:55
How do I correctly compile this piece of code? (taken from [here](https://github.com/minad/vertico/wiki)):
```
(advice-add #'vertico--arrange-candidates :around
(defun vertico-format-candidates+ (func)
(let ((hl-func (or (alist-get (vertico--metadata-get 'category)
+completion-category-hl-func-overrides)
#'identity)))
(cl-letf* (((symbol-function 'actual-vertico-format-candidate)
(symbol-function #'vertico--format-candidate))
((symbol-function #'vertico--format-candidate)
(lambda (cand &rest args)
(apply #'actual-vertico-format-candidate
(funcall hl-func cand) args))))
(funcall func)))))
```
When I try to compile it, I get the warning `‘actual-vertico-format-candidate’ is not known to be defined.`
EMP_Account_Siphon · 2022-07-26 23:48
I've been noticing general slowness activating any minibuffer command. For ex, I turned off vertico to use plain M-x, find-file, etc and they all take maybe 0.25 - 0.5 seconds for the prompt to appear.
Running the CPU profiler claims over 70% of time is spent in `minibuffer-inactive-mode`. This seems to be running a bunch of functions to turn on all my global modes, where each of them is like <5% of the time, but then I guess they add up.
Anybody got any suggestions?
Phil-Hudson · 2022-07-27 15:56
I don't use/know vertico, so I'm guessing blind, but my first thought would be that you need to add somewhere earlier in your file either a `require` form for the package that defines `actual-vertico-format-candidate` or an `autoload` form for the function itself. In either case, I would wrap it in an `eval-and-compile` form.