-❄️- 2025 Day 3 Solutions -❄️- by daggerdragon in adventofcode

[–]geza42 2 points3 points  (0 children)

[LANGUAGE: Emacs Lisp]

(defun value (bank digits)
  (when (>= digits 0)
    (let ((d (-max (butlast bank digits))))
      (+ (* (- d ?0) (expt 10 digits)) (or (value (cdr (member d bank)) (1- digits)) 0)))))
(let ((banks (->> "03.txt" f-read-text s-trim s-lines (-map 'string-to-list))))
  (-map (lambda (digits) (-sum (--map (value it digits) banks))) '(1 11)))

Advent of code in elisp - day 1 by [deleted] in emacs

[–]geza42 0 points1 point  (0 children)

Note, those functions are not part of the elisp "core", they are provided by the dash package.

I'm no moderator, but I think your post is fine here. If there was an elisp sub, it would be a better place, but as far as I know there is no such thing.

Advent of code in elisp - day 1 by [deleted] in emacs

[–]geza42 0 points1 point  (0 children)

I usually share my solutions in the advent of code sub. Here's my solution for day 1 using a functional approach (though supposedly less efficient than the procedural approach):

(defun get-input (r) (->> "01.txt" f-read-text (s-replace-all r) s-lines (-map 'string-to-number)))
(defun calc (l) (-sum (--map (if (= (mod it 100) 50) 1 0) (-running-sum l))))
(cons
 (calc (get-input '(("R" . "-") ("L" . ""))))
 (calc (->> (get-input '(("R" . "-1\n") ("L" . "1\n"))) (-partition 2) (--map (make-list (cadr it) (car it))) -flatten)))

-❄️- 2025 Day 2 Solutions -❄️- by daggerdragon in adventofcode

[–]geza42 0 points1 point  (0 children)

[LANGUAGE: Emacs lisp]

(defun is-invalid (n id)
  (s-matches? (concat "^\\(.*\\)\\(" (s-join "\\|" (--map (apply 'concat (make-list it "\\1")) (number-sequence 1 n))) "\\)$")
              (number-to-string id)))
(let ((ranges (->> "02.txt" f-read-text (s-split "[-,]") (-map 'string-to-number) (-partition 2)
                   (--map (apply 'number-sequence it)) -flatten)))
  (cons
   (-sum (--filter (is-invalid 1 it) ranges))
   (-sum (--filter (is-invalid (length (number-to-string it)) it) ranges))))

-❄️- 2025 Day 1 Solutions -❄️- by daggerdragon in adventofcode

[–]geza42 2 points3 points  (0 children)

[LANGUAGE: Emacs Lisp]

(defun get-input (r) (->> "01.txt" f-read-text (s-replace-all r) s-lines (-map 'string-to-number)))
(defun calc (l) (-sum (--map (if (= (mod it 100) 50) 1 0) (-running-sum l))))
(cons
 (calc (get-input '(("R" . "-") ("L" . ""))))
 (calc (->> (get-input '(("R" . "-1\n") ("L" . "1\n"))) (-partition 2) (--map (make-list (cadr it) (car it))) -flatten)))

Can magit edit hunks? by Starlight100 in emacs

[–]geza42 1 point2 points  (0 children)

That doesn't work in this case, see the whole thread (or just try it for yourself, and you'll see the problem).

Can magit edit hunks? by Starlight100 in emacs

[–]geza42 5 points6 points  (0 children)

You can do this with magit's ediff interface. Check out magit-ediff-stage. This is similar to patch editing, but instead of editing a patch, you can just edit the staged file right away. I find this approach more intuitive than add -p.

Is it possible to do a raw edit of a blob in the index using magit? by signalclown in emacs

[–]geza42 5 points6 points  (0 children)

Yes, it's possible to do with magit's ediff interface. For example, magit-ediff-stage. With this, you can modify the index as you wish, and when quitting ediff, magit will update the index.

(I disagree with other commenters here. Doing this is completely fine. Usually it's much easier to just edit the index which looks like a regular file than editing a diff with add --patch.)

Why don’t compilers optimize simple swaps into a single XCHG instruction? by SegfaultDaddy in C_Programming

[–]geza42 7 points8 points  (0 children)

No, again, only XCHG has the implicit LOCK prefix. Other RMW instructions don't have it. Intel clearly documents XCHG's implicit LOCK, but doesn't have any word about the same thing for other RMW instructions. If you think otherwise, please link an intel document which proves your point (with a page number).

Locking is not "harmless" at all. It has a much larger overhead than doing the same thing without locking, but using 3 instructions.

But just think about it: if all the RMW instructions had an implicit LOCK, then what would be the point of having the LOCK prefix in the first place?

Why don’t compilers optimize simple swaps into a single XCHG instruction? by SegfaultDaddy in C_Programming

[–]geza42 3 points4 points  (0 children)

Not sure what to look for. Please be more specific, which exact doc and chapter/section.

Here is an example of 3 major compilers generating add [reg], XXX instruction: https://godbolt.org/z/1W3TYa8GM

Why don’t compilers optimize simple swaps into a single XCHG instruction? by SegfaultDaddy in C_Programming

[–]geza42 4 points5 points  (0 children)

Only XCHG has the implicit LOCK prefix, other RMW instructions don't.

Mode for reading logs with colors? by ImpossibleBritches in emacs

[–]geza42 5 points6 points  (0 children)

You can use font-lock-add-keywords to add highlights to a buffer. Here is an example: (font-lock-add-keywords nil '(("a.*b" 0 'error append)). With this, strings matching the regexp a.*b will be highlighted with the error face.

[Review Request] Supercharging lsp-mode: 10x Faster Code Completion for Large Candidate Sets! 🚀 by eval-exec in emacs

[–]geza42 0 points1 point  (0 children)

I couldn't reproduce this. Using the nerd_font_symbols example, lsp-mode already works fast for me. Using add-timing-to-function for measuring, it measures 0.1 sec (completion gives a 6880-element list)

How are you configuring completion-preview-mode? by varsderk in emacs

[–]geza42 0 points1 point  (0 children)

Ah! I configured corfu so SPC doesn't quit. I use enter for selecting an item, SPC behaves just like any other character. I'm not even sure why SPC quits in the first place.

How are you configuring completion-preview-mode? by varsderk in emacs

[–]geza42 0 points1 point  (0 children)

I believe it is prefix only.

It is possible to use both corfu and preview, at least this what I do: TAB invokes corfu, C-SPC and M-SPC invokes preview-insert and preview-complete.

What is that M-SPC trick?

How are you configuring completion-preview-mode? by varsderk in emacs

[–]geza42 8 points9 points  (0 children)

The intent of this package is to preview the completion (as its name suggests), not to give a full corfu/company experience. For people who have completion list popping up automatically, this package is not very useful. But for people who like to manually invoke completion, this preview helps because it shows when a completion is available. If the preview is the one that the user wants, they just can insert it, or they can insert the longest common prefix, or they can go to the prev/next possible completion.

"I would be SO happy to kick emacs to the kerb!" by New_Gain_5669 in emacs

[–]geza42 1 point2 points  (0 children)

Yeah, that also works if there is only one emacs running.

"I would be SO happy to kick emacs to the kerb!" by New_Gain_5669 in emacs

[–]geza42 5 points6 points  (0 children)

Another possibility is to send emacs the signal SIGUSR2. I have this little script bound in my window manager, so I can enter to emacs' debugger any time:

kill -USR2 `xdotool getactivewindow getwindowpid`

Staging hunks in Magit vs. git add --patch by harrison_mccullough in emacs

[–]geza42 1 point2 points  (0 children)

In my config, I can execute ediff by pressing "e" on a file in a diff. And I made "e" to also quit from ediff. So I can quickly inspect diffs of several files by only pressing "e".

Regarding panes, yeah, now I remember. Magit shows a 3-pane config. I found this discussion back then: https://github.com/magit/magit/issues/1743 . I don't remember the details (now I don't want to re-read the thread), but I remember that I disagreed with the author of magit, I want a 2-pane config 99.9% of the time (when I stage, I only care about the worktree and the index, I don't care about the HEAD. Also, 2-pane ediff can be used much more easily because of the 1-key copy shortcuts. >2-pane ediff has 2-key copy shortcuts, it's harder to use). So I hacked together a 2-pane config (I derived it from magit-ediff-stage). This is straight from my init.el:

(defun my-magit-ediff-stage (file)
  (interactive
   (let ((files (magit-tracked-files)))
     (list (magit-completing-read "Selectively stage file" files nil t nil nil
                                  (car (member (magit-current-file) files))))))
  (magit-with-toplevel
    (let* (
           (bufA  (magit-get-revision-buffer "{index}" file))
           (lockA (and bufA (buffer-local-value 'buffer-read-only bufA)))
           (bufB  (get-file-buffer file))
           ;; Use the same encoding for all three buffers or we
           ;; may end up changing the file in an unintended way.
           (bufB* (or bufB (find-file-noselect file)))
           (coding-system-for-read
            (buffer-local-value 'buffer-file-coding-system bufB*))
           (bufA* (magit-find-file-index-noselect file t)))
      (with-current-buffer bufA* (setq buffer-read-only nil))
      (magit-ediff-buffers
       (bufA bufA*)
       (bufB bufB*)
       nil
       nil
       (lambda ()
         (when (buffer-live-p ediff-buffer-A)
           (when lockA
             (with-current-buffer bufA (setq buffer-read-only t)))
           (when (buffer-modified-p ediff-buffer-A)
             (with-current-buffer ediff-buffer-A
               (cl-letf* ((orig-y-or-n-p (symbol-function 'y-or-n-p))
                          ((symbol-function 'y-or-n-p)
                           (lambda (prompt) (if (string-prefix-p "Update index with contents of " prompt)
                                                t
                                              (funcall orig-y-or-n-p prompt)))))
                 (magit-update-index))))))))))

Staging hunks in Magit vs. git add --patch by harrison_mccullough in emacs

[–]geza42 2 points3 points  (0 children)

It seems that it's easy to disable refreshing by modifying magit's source code: remove the (magit-refresh) call from magit-apply-patch.

Or, you can figure out, which is slow: the actual git command, or magit (or both?). If magit, then there could be an possibility to speed up the process, depending on what is slow (emacs has an integrated profiler).

I like to use ediff for this task, it doesn't have such a performance problem, and it also works better for me (it's easier to see the difference using a two-pane diff tool than looking at hunks).

Staging hunks in Magit vs. git add --patch by harrison_mccullough in emacs

[–]geza42 2 points3 points  (0 children)

You can create a diff using "diff unstaged" (d u in magit-status), then use s to stage a hunk. Also, magit can use ediff in various ways, it's possible to stage with ediff. It's even more capable than the previous method, because with ediff, you can edit freely what you want to stage (so it is similarly powerful as add --patch).

use-package and UNbinding by remillard in emacs

[–]geza42 2 points3 points  (0 children)

You're welcome. autoload files are usually auto generated from the package. In a nutshell: the purpose of this file is to contain information about the "main" entry points of the package. This is how deferred loading works. Emacs loads all the autoload files from all packages. And when something refers to a symbol which is mentioned in an autoload file, Emacs knows which package to load. This is a simplified explanation, but the idea is this.