reddit / emacs

Weekly Tips, Tricks, &c. Thread

AutoModerator · 2022-03-09 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.
spekvojbek · 2022-03-09 09:24
Can I declare a local variable in a yasnippet? For example, I have a snippet where the filename gets inserted several times with several call to ```${`(file-name-nondirectory (file-name-sans-extension (buffer-file-name)))`_H}``` I would like to do is turning that filename in a variable ala ```${var_fn:`(file-name-nondirectory (file-name-sans-extension (buffer-file-name)))`_H}``` with further call to just `${var_fn}`. I do not want to TAB-stop to this
condor2000 · 2022-03-09 14:07
Is it possible to show the buffer file encoding more explicitly?. I would like UTF-8, UTF-16 BE, latin1, etc, in the modeline
organic-bookworm · 2022-03-09 16:58
See [\# expand-env](https://joaotavora.github.io/yasnippet/snippet-development.html#orga37203f) directive. >This is another piece of Emacs-lisp code in the form of a let varlist form, i.e. a list of lists assigning values to variables. It can be used to override variable values while the snippet is being expanded. Here's an example # key: myt # expand-env: ((var "life")) # -- `(string-replace "f" "v" var)` is `var`
mmaug · 2022-03-10 04:22
You want to look at `mode-line-format` and use `buffer-file-coding-system` to determine what you want. It's probably more complex than just those two symbols, but that should at least put you in the neighborhood
spekvojbek · 2022-03-10 09:42
Cheers, works like a charm, with added benefit it also doesn't act as a TAB-stop.
Acceptable-5521 · 2022-03-10 21:16
Is there anything special about using warnings (e.g. `warn`, `display-warning`) during init time (during evaluation of `init.el`) and Yes-or-No queries (`y-or-n-p` and `yes-or-no-p`)? While debugging an issue, I had essentially this code in `init.el`: ;; some code that reports a warning - here just simplified to `warn` (warn "foo") ;; ...somewhere later in init.el (y-or-n-p "prompt ") When opening Emacs (and consequentially, evaluating `init.el`), only the above `y-or-n-p` is displayed and asks me the question, the `*Warnings*` buffer is not opened and the warning is not displayed. When removing the `(y-or-n-p "prompt ")` from the code above, the `*Warnings*` buffer is opened and the warning is displayed as expected. After some testing, I tried using this exact `init.el`: ​ (warn "foo") (add-hook 'after-init-hook (lambda () (y-or-n-p "prompt "))) In other words, I was deferring the `y-or-n-p` execution until after initializing the Emacs session (and in particular, `init.el` evaluation). Now, everything works as expected - the `*Warnings*` buffer is opened and a warning with a message `"foo"` is displayed. Also, the `y-or-n-p` is displayed and behaves as expected. Why is this behavior happening? Should I avoid using (evaluating) `y-or-n-p` in my `init.el`?
crawsphi · 2022-03-10 23:36
Doom modeline does this; you can have a look at its code to see how: https://github.com/seagle0128/doom-modeline/blob/eb61e3ec360474adc0b6de5a929d742c4876807e/doom-modeline-segments.el#L498-L504
_viz_ · 2022-03-11 05:59
If you are annoyed by find -ls escaping the non-ASCII characters when using find-dired and friends, then the following function should help you. See also bug#36085. Unfortunately, the xargs option is not really feasible since it noticeably slows down the entire process. The function goes a bit beyond just unescaping the octal strings but all you need to remove the extra bits is to just delete the (and (re-search-forward ...) ...) bits in the beginning of the loop. (defun vz/find-dired-refine () "Refine `find-dired' output. This unescapes the C-style octal escape strings, prints the inode size in human-readable format, deletes the first two superfluous columns." (while (not (eobp)) (and (re-search-forward dired-re-perms (line-end-position)) (delete-region (+ 2 (line-beginning-position)) (match-beginning 0))) (and (re-search-forward " +\\([0-9]+\\)" (line-end-position) 'noerror 2) (replace-match (propertize (format "%7s" (file-size-human-readable (string-to-number (match-string 1)))) 'invisible 'dired-hide-details-detail 'fontified t))) (when-let ((beg (next-single-property-change (point) 'dired-filename)) (props (text-properties-at beg))) (goto-char beg) (while (and (re-search-forward (rx "\\" (group (any "0-7") (? (any "0-7") (? (any "0-7"))))) (line-end-position) 'noerror) (not (eq (char-before (match-beginning 0)) ?\\))) (let ((num (string-to-number (match-string 1) 8))) (replace-match (unibyte-string num) t nil nil 0))) (decode-coding-region beg (line-end-position) buffer-file-coding-system) (set-text-properties beg (line-end-position) props)) (forward-line))) And I have the following relevant options set in my init file, (custom-set-variables '(find-ls-option (cons "-ls" "-dlh")) '(find-dired-refine-function #'vz/find-dired-refine)))
pathemata · 2022-03-11 10:44
How do you guys deal with window popups, like help buffers and errors? Sometimes I find it annoying having to go to an error window to close it. I tried to mess around with `display-buffer-base-action`.
meme_streak · 2022-03-11 14:07
Can yasnippets perform completion based on a partially entered key? I'd like to be able to enter the first few letters of a snippet key and, when pressing TAB, be given a menu of snippets whose keys start with what I've already entered. If this is possible, how?
WorldsEndless · 2022-03-11 14:20
I want to be able to toggle case-folding in my regexp query replace process. I found the variable to do this during regular isearch – just `C-c` – but I can't find the map to bind such a thing for `isearch-query-replace-regexp`. Any tips?
karthink · 2022-03-11 17:51
I use [Popper](https://github com/karthink/popper). A built in way would be to add a `display-buffer-alist` entry for these buffers with the `body-function` action parameter set to `select-window`.
karthink · 2022-03-11 17:52
See company-yasnippet.
meme_streak · 2022-03-12 17:07
Much appreciation, karthink! Following up, this is what worked for me (to just prompt snippet completion): (require 'company) (require 'company-yasnippet) (setq company-backends '(company-yasnippet)) (global-company-mode 1)
ZZhanChi · 2022-03-13 00:26
Org Agenda Posframe: ``` (defun org-agenda-posframe () "`org-agenda-list' in a posframe. Quit with 'q' as usual." (interactive) ;; Open org agenda without showing it in the current frame (save-window-excursion (org-agenda-list)) ;; Create posframe with the org agenda buffer (let ((frame (posframe-show org-agenda-buffer :poshandler 'posframe-poshandler-frame-center :border-width 2 :border-color "gray"))) ;; Focus org agenda frame to be able to use it's shorcuts (x-focus-frame frame) ;; Bring back the disappeared cursor (with-current-buffer org-agenda-buffer (setq-local cursor-type 'box)))) ```
[deleted] · 2022-03-13 04:21
[deleted]
spyder_alt · 2022-03-13 04:32
I'm not sure about the right terminology on this so apologies in advance but I'm trying to solve a problem using evil mode. Basically I would like to have a quick release-type key. Where I can go into normal mode for a couple of commands then go back into insert mode. Say while holding down right alt. I know that vim has `c-o` for one-off commands but I would like to be able to use more than one command. Ideally, I write for example `the quick brown fox jumped` can delete fox in normal mode with `alt+bbdw` and then quickly switch back to the end of a line with `alt+A`. This is just one example but I'm trying to not have to use escape so much to switch back and forth between modes. Not a big deal tbh as I'm very used to Vim's way of doing things but just wondering!
pathemata · 2022-03-15 08:56
Hi Karthink, just tried `popper` here. It is great and solves my problem, thanks for making it.
trs_80 · 2022-03-15 21:05
This is totally off the cuff (I did not look anything up) but I would first look if there are settings or keybindings which do what you want. It sounds like you have though, but I just wanted to mention that should always be the first step. Maybe studying source for `isearch-query-replace-regexp` might yield something helpful? Anyway, barring that, you could always create your own custom Elisp wrapper to `let` bind the variable `case-fold-search` and then call the function in question. You may then want to bind your custom function to the default keys.
WorldsEndless · 2022-03-15 21:19
the thing is, I don't think case-fold-search is actually having an effect on my `isearch-query-replace-regexp`. I tried temporarily setting it globally and it works with the regular isearch (and breaks stuff), but doesn't seem to apply to the regexp replace. So even what variable to toggle is unclear.
Phil-Hudson · 2022-03-15 21:51
What I do is this: I've put everything in my startup routine that does or may require interaction into a command that I invoke manually after all the noninteractive init/config code has completed. I don't know if something like that might be necessary or sufficient for your issue, but it might be worth a try.
trs_80 · 2022-03-15 23:15
> I don't think case-fold-search is actually having an effect I experienced the same! However in my case (lol) I was trying to sort a list of strings, so I ended up writing [my-sort-ignore-case](https://old.reddit.com/r/emacs/comments/telb9f/sorting_text/i0sizbf/). However I have no idea what you might have to do to get that to work with `isearch` though. FWIW, I also use Swiper, which is by default all lowercase, until you enter something in uppercase and then the search automagically becomes exact / case-sensitive.
Qwarctick · 2022-03-16 12:32
You can use `C-g` instead of `Esc` in Emacs.
WorldsEndless · 2022-03-16 14:09
yes, I believe that "until uppercase" is the normal functionality for all search-replaces -- which is sometimes a lifesaver, but sometimes a pain. It doesn't help me with trying to replace ONLY the lower-cased or ONLY the capitalized strings, though.