reddit / emacs
Weekly Tips, Tricks, &c. Thread
AutoModerator · 2022-06-22 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.
ScottWC2 · 2022-06-22 14:26
Anyone know why r/planetemacs went private? Private subs don't work with rss and I just noticed it changed around April 15th.
twillisagogo · 2022-06-22 16:31
Probably to keep the vin trolls out.
thetemp_ · 2022-06-22 22:49
Is anyone able to access it at all?
It just comes up as private for me. And I'm definitely not a Vim troll. I don't even use evil-mode now. :-P
ScottWC2 · 2022-06-22 23:23
Understandable if true. Half the posts and comments on this sub are in someway vi related.
cabbagegandalf · 2022-06-23 14:56
Hey folks, I'm trying to find a way to improve the compilation buffer name for projectile, as in:
- if I'm running the test command (`projectile-test-project`) I'd like for the compile buffer name to be `*compilation <project-name>:test`, the same thing for the other commands like run, build, etc. so I can run the tests while running the project.
Is that possible?
natarey · 2022-06-23 16:07
Does `embark` not understand org links?
When I'm in an org buffer, and try to use `embark-act` or `embark-dwim` on an org link, I either get a definition of one of the words in the link, or I get a message about the symbol being undefined.
[Yes, I understand you can just hit return on Org links when in an org buffer -- this just seems like a weird oversight.]
_viz_ · 2022-06-24 13:57
You might be interested in the "secret" embark-org.el file in the embark repo.
natarey · 2022-06-24 14:55
Aha! Works perfectly -- thanks for that!
tryptych · 2022-06-25 06:58
Before I submit a bug or dig further -- should [consult](https://github.com/minad/consult) work with [session](http://emacs-session.sourceforge.net/)? It seems to be adding characters from a different coding system.
I have isolated it down to (using Emacs from master):
* emacs -Q
* `M-x package-initialize` (it's in my elpa cache already)
* `M-: (require 'consult)`
* `M-x consult-buffer` (switch to \*Messages\*)
And now `consult--buffer-history` is `(#("*Messages*�����" 10 11 (invisible t consult-strip t)))` (where the unprintable bit displays as a box with 200002; C-u C-x = says it's codepoint 2097154 and charset gb18030). That character of course can't be encoded in utf-8 so session-save throws an error, effectively meaning I can't quit Emacs!
LemonBreezes · 2022-06-25 14:28
I remember applying to join it but never being able to join it afterwards.
crawsphi · 2022-06-25 17:42
I don't know whether Projectile has specific configuration for this, but you can customize `compilation-buffer-name-function` to render whatever information you like.
cabbagegandalf · 2022-06-25 21:16
That was my first try but projectile overrides I’d, I can override the projectile method but I haven’t found a way to check which command is being run to set the proper name
vifon · 2022-06-25 21:39
These codepoints are called "tofu" by `consult`. Frankly, I don't feel equipped to explain them correctly, but grepping their source for "tofu" should give you a lot of docs. It seems to me `session` interacts badly with them, so the bug might be theirs.
tryptych · 2022-06-25 22:54
Thank you! I do remember seeing "tofu" references, I'll have a look with that in mind.
What's the modern equivalent for `session`, if it's easiest just to swap out?
vifon · 2022-06-25 23:07
I'm not familiar with `session`. Looks like a combination of `savehist-mode`, `desktop-save-mode` and maybe `recentf-mode`, but could you describe what you want to achieve?
tryptych · 2022-06-25 23:46
I've just been having a bit of a play; you can exclude variables to save from `session`, so I might just build up a list of those. For anyone else following, starting with:
`(setq session-globals-exclude '(consult--buffer-history))`
(I'm also experimenting with prescient, so there's probably a bit of overlap going on)
ppvvaa · 2022-06-26 14:04
After some good answers in my post [Problems with pdf-tools + AUCTeX on Mac M1: blurry pdf, pages don't load properly](https://www.reddit.com/r/emacs/comments/vk2w18/problems_with_pdftools_auctex_on_mac_m1_blurry/), I would like to know: *is there currently anyone* who checks all the following boxes:
* is on an M1 mac;
* is using latex with AUCTeX;
* has source - pdf sync working both ways, either with Skim , or pdf-tools, or whatever.
This will help me decide whether to install Skim.
crawsphi · 2022-06-26 20:07
I'd think `current-command` should be set to the projectile function, so you can check that. But if not there's also `compile-command`, which is the actual shell command that's being run. Depending on how your project works, maybe you can figure it out from that value. Even something as simple as a regex looking for the word "test" could work.
cabbagegandalf · 2022-06-26 22:55
Thanks! You are awesome, I've managed to make it work with this:
```elisp
(defvar me/projectie-task-name-map
'((projectile-test-project . "test")
(projectile-compile-project . "compile")
(projectile-run-project . "run")))
(defun me/compilation-buffer-name (compilation-mode &optional task-name)
(if (eq task-name nil)
(project-prefixed-buffer-name compilation-mode)
(project-prefixed-buffer-name (format "%s[%s]" compilation-mode task-name))))
(defun me/projectile-compilation-buffer-name (compilation-mode)
(let* ((task-name (alist-get real-last-command me/projectie-task-name-map nil nil #'equal)))
(me/compilation-buffer-name compilation-mode task-name)))
(advice-add 'projectile-compilation-buffer-name :override #'me/projectile-compilation-buffer-name)
```
I'm not very versed on Elisp but it seems to work fine
FluentFelicity · 2022-06-27 19:55
If you use `tree-sitter-hl-mode` and `flyspell-prog-mode`, then you'll notice that `flyspell` no longer spell checks.
You can fix this by including the string, comment, and doc faces of `tree-sitter-hl` to `flyspell-prog-text-faces` like so:
```
'(font-lock-string-face
font-lock-comment-face
font-lock-doc-face
tree-sitter-hl-face:string
;; tree-sitter-hl-face:string.special ; For things like regexps
tree-sitter-hl-face:comment
tree-sitter-hl-face:doc)
```
Additionally, if you use `wucuo`, you should add those `tree-sitter-hl` faces to `wucuo-personal-font-faces-to-check` or `wucuo-font-faces-to-check`.
yigitemres · 2022-06-28 17:20
Is there any function to read-number and check if the number is between values? I searched it but can't find anything. Because of this I write my own function. If there is a function is already implemented in Emacs please let me know.
;;;###autoload
(defun my/read-number (prompt &optional default low high)
"Modifies `read-number' to check if the number is between intervals."
(unless (and (numberp low) (numberp high))
(unless (numberp low)
(error "Please enter number instead of: %s(%s)" low (type-of low)))
(error "Please enter number instead of: %s(%s)" high (type-of high)))
(unless (< low high)
(error "Please enter values correct order: %s ≰ %s" low high))
(let ((check (read-number prompt default)))
(if (and (>= check low) (<= check high))
check
(message "Please enter valid value between %d and %d." low high)
(sit-for 1)
(my/read-number prompt default low high))))
Phil-Hudson · 2022-06-28 20:28
This is a pretty good solution IMO. A few tips/pointers:
1. I think all of the `error` forms would be better implemented as `user-error`.
2. It's worth adding an `(interactive)` form at the start. This tells Emacs that it's a command which can be bound to a key combination and called using `M-x`.
3. Elisp's inequality functions can elegantly chain: `(<= low check high)`.
4. The last form makes the function recursive. Be very careful about recursion. You might be alright in this case, but it would be more consistent with Emacs conventions to error again rather than `(message ...) (sit-for 1) (<recurse>)`.
5. The `low` and `high` parameters are not optional, so don't declare them as such: `(prompt low high &optional default)`.
yigitemres · 2022-06-28 23:53
> 1\. I think all of the error forms would be better implemented as user-error.
Thanks for the tip. I see the user-error but not try it. This is actually what I want.
> 2\. It's worth adding an (interactive) form at the start. This tells Emacs that it's a command which can be bound to a key combination and called using M-x.
I'm not using as a callable function. I just made this for a emacs yee light package for my self use (It's actually not even a package just a lib-yee.el file which contains functions that sends shell commands). I use this package for a long time and when I learn something new from elisp I look the ways implemented inside this lib. When I found read-x functions I immediately want to look my lib-yee and try to find is there any use case for it. There is a specific command which takes temperature and values are has to be between 1700 and 6500. So this defun is born. =)
> 3\. Elisp's inequality functions can elegantly chain: (<= low check high).
This is huge. Thanks for the another tip.
> 4\. The last form makes the function recursive. Be very careful about recursion. You might be alright in this case, but it would be more consistent with Emacs conventions to error again rather than (message ...) (sit-for 1) (<recurse>).
Just wanted to simulate how (read-number "a") behaves. If you can show me a better way I will be happy.
> 5\. The low and high parameters are not optional, so don't declare them as such: (prompt low high &optional default).
Overlooked. Thanks for the correction.
Edit: I wonder that is there a way to something like:
(&musthave prompt &optional default &musthave low high)
Phil-Hudson · 2022-06-29 07:03
On that last point, the best way around this strict-ordering problem is to use `cl-defun`'s keyword arguments:
(require 'cl-macs) ; not really necessary, always available
(cl-defun my/read-number (prompt &key default low high)
which you can then pass keyword arguments to in any order:
(my/read-number "Constrained number: " :high 15 :low 0)
crawsphi · 2022-07-01 22:09
Very nice!
ifixcomputers4money · 2022-07-02 15:45
I often(?) find myself inserting dates into a file. While I could just bind `(insert (format-time-string "%F"))` to a key, I figure that a nice keybinding would be better, this this monstrosity was born:
```elisp
(defun my/insert-date ()
"Inserts an ISO date by default, prefixing with C-u asks for a
format."
(interactive)
(if (null current-prefix-arg)
(insert (format-time-string "%F")))
(if (eq (prefix-numeric-value current-prefix-arg) 4)
(insert (format-time-string "%FT%T%z")))
(if (> (prefix-numeric-value current-prefix-arg) 4)
(insert (format-time-string (read-string "Format: ")))))
```