utils.el 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ;;; utils.el --- General utility functions -*- lexical-binding: t; -*-
  2. (defun dired-follow-symlink ()
  3. "In dired, visit the file or directory at point by its true physical path."
  4. (interactive)
  5. (let ((filename (dired-get-file-for-visit)))
  6. (if filename
  7. (find-file (file-truename filename))
  8. (message "No file at point"))))
  9. (defun my/delete-word-backward ()
  10. "Delete the previous 'block' of characters without killing them.
  11. Blocks are defined by: whitespace, alphanumerics, or groups of symbols."
  12. (interactive)
  13. (if (use-region-p)
  14. (delete-region (region-beginning) (region-end))
  15. (let ((end (point)))
  16. (save-excursion
  17. (cond
  18. ;; Case 1: Multiple spaces or tabs (but not a newline)
  19. ((looking-back "[ \t]+" 1)
  20. (re-search-backward "[^ \t]" nil t)
  21. (forward-char 1))
  22. ;; Case 2: Newlines
  23. ((looking-back "\n+" 1)
  24. (re-search-backward "[^\n]" nil t)
  25. (forward-char 1))
  26. ;; Case 3: Alphanumerics (Words)
  27. ((looking-back "[[:alnum:]]+" 1)
  28. (re-search-backward "[^[:alnum:]]" nil t)
  29. (forward-char 1))
  30. ;; Case 4: Special Characters (Symbols)
  31. (t
  32. (re-search-backward "[[:alnum:][:space:]]" nil t)
  33. (forward-char 1)))
  34. (delete-region (point) end)))))
  35. (defun my-set-buffer-font ()
  36. "Set a specific font for the current buffer only."
  37. (buffer-face-set '(:family "Noto Sans" :height 140)))
  38. (defun my/toggle-quote-wrap-all-in-region (beg end)
  39. "Toggle wrapping all items in region with double quotes."
  40. ;;https://xenodium.com/emacs-quote-wrap-all-in-region
  41. (interactive (list (mark) (point)))
  42. (unless (region-active-p)
  43. (user-error "no region to wrap"))
  44. (let ((deactivate-mark nil)
  45. (replacement (string-join
  46. (mapcar (lambda (item)
  47. (if (string-match-p "^\".*\"$" item)
  48. (string-trim item "\"" "\"")
  49. (format "\"%s\"" item)))
  50. (split-string (buffer-substring beg end)))
  51. " ")))
  52. (delete-region beg end)
  53. (insert replacement)))
  54. (defun my/meow-wrap-arbitrary (s e)
  55. "Wrap the current meow selection with arbitrary strings."
  56. (interactive "r")
  57. (let ((start-str (read-string "Start: "))
  58. (end-str (read-string "End: ")))
  59. (save-excursion
  60. (goto-char e)
  61. (insert end-str)
  62. (goto-char s)
  63. (insert start-str))))
  64. (defun my/column-align-regexp ()
  65. "Aligns the matched regexp to `comment-column`.
  66. If the text preceding the regexp is already past `comment-column`,
  67. it ensures exactly one space of separation."
  68. (interactive)
  69. (let* ((regexp (read-string "Align on regexp: "))
  70. ;; Use comment-column if set, otherwise default to 40
  71. (target-col (if (and (boundp 'comment-column)
  72. (numberp comment-column))
  73. comment-column
  74. 40))
  75. (start (region-beginning))
  76. (end (region-end)))
  77. (save-excursion
  78. (save-restriction
  79. (narrow-to-region start end)
  80. (goto-char (point-min))
  81. (while (re-search-forward regexp nil t)
  82. (let* ((match-start (match-beginning 0))
  83. (match-end (match-end 0)))
  84. ;; 1. Find the end of the actual content before the match
  85. ;; We move back from the match start and skip all whitespace
  86. (save-excursion
  87. (goto-char match-start)
  88. (skip-chars-backward " \t")
  89. (let ((content-end-pos (point))
  90. (content-end-col (current-column)))
  91. ;; Delete the whitespace between the content and the match
  92. (delete-region content-end-pos match-start)
  93. ;; Calculate how much space we need
  94. (if (< content-end-col target-col)
  95. (let ((num-spaces (- target-col content-end-col)))
  96. (insert (make-string num-spaces ?\s)))
  97. ;; If content is already past the target, just insert one space
  98. (insert " "))))
  99. ;; Move point to the end of the match to continue searching
  100. (goto-char match-end)))))))
  101. ;; Optional: Bind it to a key for easier access
  102. ;; (global-set-key (kbd "C-c a l") 'my/column-align-regexp)
  103. (defun diff-buffers (buffer-a buffer-b)
  104. "Generate a diff between BUFFER-A and BUFFER-B using standard diff.
  105. Prompts for the two buffers, defaulting to the current buffer and the
  106. most recently active one."
  107. (interactive
  108. (let* ((current (current-buffer))
  109. (other (other-buffer current t))
  110. (buf-a (read-buffer "New buffer (to diff): " current t))
  111. (buf-b (read-buffer "Old buffer (against): " other t)))
  112. (list (get-buffer buf-a) (get-buffer buf-b))))
  113. (let ((name-a (buffer-name buffer-a))
  114. (name-b (buffer-name buffer-b)))
  115. (diff buffer-b buffer-a nil t)
  116. ;; Rename the diff output buffer to make it clear what was diffed
  117. (when (get-buffer "*Diff*")
  118. (with-current-buffer "*Diff*"
  119. (rename-buffer (format "*Diff: %s vs %s*" name-b name-a) t)))))
  120. (defun my/list-no-prefix-bindings (keymap &optional map-name)
  121. "Extract and display all single-key (no-prefix) bindings from KEYMAP.
  122. Outputs a clean list into a dedicated buffer."
  123. (interactive (list global-map "global-map"))
  124. (let ((buf (get-buffer-create "*No-Prefix Bindings*"))
  125. (bindings '()))
  126. (cl-labels ((map-extract (map)
  127. (map-keymap
  128. (lambda (event binding)
  129. (cond
  130. ;; Skip nested/prefix keymaps entirely
  131. ((keymapp binding) nil)
  132. ;; Handle standard events (symbols or characters)
  133. ((or (symbolp event) (integerp event))
  134. (let ((key-str (single-key-description event)))
  135. ;; Filter for simple keys (no spaces, meaning no multi-key sequences)
  136. (unless (string-match-p " " key-str)
  137. (push (cons key-str binding) bindings))))))
  138. map)))
  139. (map-extract keymap)
  140. ;; Sort logically by key description
  141. ;; Print the results cleanly
  142. (with-current-buffer buf
  143. (text-mode)
  144. (setq buffer-read-only nil)
  145. (erase-buffer)
  146. (insert (format "No-Prefix Bindings for: %s\n" (or map-name "Selected Map")))
  147. (insert "==================================================\n\n")
  148. (dolist (b bindings)
  149. (insert (format "%-15s -> %s\n" (car b) (cdr b))))
  150. (setq buffer-read-only t))
  151. (pop-to-buffer buf))))
  152. (provide 'utils)