utils.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ;;; utils.el --- General utility functions -*- lexical-binding: t; -*-
  2. (defun my/delete-word-backward ()
  3. "Delete the previous 'block' of characters without killing them.
  4. Blocks are defined by: whitespace, alphanumerics, or groups of symbols."
  5. (interactive)
  6. (if (use-region-p)
  7. (delete-region (region-beginning) (region-end))
  8. (let ((end (point)))
  9. (save-excursion
  10. (cond
  11. ;; Case 1: Multiple spaces or tabs (but not a newline)
  12. ((looking-back "[ \t]+" 1)
  13. (re-search-backward "[^ \t]" nil t)
  14. (forward-char 1))
  15. ;; Case 2: Newlines
  16. ((looking-back "\n+" 1)
  17. (re-search-backward "[^\n]" nil t)
  18. (forward-char 1))
  19. ;; Case 3: Alphanumerics (Words)
  20. ((looking-back "[[:alnum:]]+" 1)
  21. (re-search-backward "[^[:alnum:]]" nil t)
  22. (forward-char 1))
  23. ;; Case 4: Special Characters (Symbols)
  24. (t
  25. (re-search-backward "[[:alnum:][:space:]]" nil t)
  26. (forward-char 1)))
  27. (delete-region (point) end)))))
  28. (defun dired-follow-symlink ()
  29. "In dired, visit the file or directory at point by its true physical path."
  30. (interactive)
  31. (let ((filename (dired-get-file-for-visit)))
  32. (if filename
  33. (find-file (file-truename filename))
  34. (message "No file at point"))))
  35. (defun my/column-align-regexp ()
  36. "Aligns the matched regexp to `comment-column`.
  37. If the text preceding the regexp is already past `comment-column`,
  38. it ensures exactly one space of separation."
  39. (interactive)
  40. (let* ((regexp (read-string "Align on regexp: "))
  41. ;; Use comment-column if set, otherwise default to 40
  42. (target-col (if (and (boundp 'comment-column)
  43. (numberp comment-column))
  44. comment-column
  45. 80))
  46. (start (region-beginning))
  47. (end (region-end)))
  48. (save-excursion
  49. (save-restriction
  50. (narrow-to-region start end)
  51. (goto-char (point-min))
  52. (while (re-search-forward regexp nil t)
  53. (let* ((match-start (match-beginning 0))
  54. (match-end (match-end 0)))
  55. ;; 1. Find the end of the actual content before the match
  56. ;; We move back from the match start and skip all whitespace
  57. (save-excursion
  58. (goto-char match-start)
  59. (skip-chars-backward " \t")
  60. (let ((content-end-pos (point))
  61. (content-end-col (current-column)))
  62. ;; Delete the whitespace between the content and the match
  63. (delete-region content-end-pos match-start)
  64. ;; Calculate how much space we need
  65. (if (< content-end-col target-col)
  66. (let ((num-spaces (- target-col content-end-col)))
  67. (insert (make-string num-spaces ?\s)))
  68. ;; If content is already past the target, just insert one space
  69. (insert " "))))
  70. ;; Move point to the end of the match to continue searching
  71. (goto-char match-end)))))))
  72. ;; Optional: Bind it to a key for easier access
  73. ;; (global-set-key (kbd "C-c a l") 'my/column-align-regexp)
  74. (defun my/list-no-prefix-bindings (keymap &optional map-name)
  75. "Extract and display all single-key (no-prefix) bindings from KEYMAP.
  76. Outputs a clean list into a dedicated buffer."
  77. (interactive (list global-map "global-map"))
  78. (let ((buf (get-buffer-create "*No-Prefix Bindings*"))
  79. (bindings '()))
  80. (cl-labels ((map-extract (map)
  81. (map-keymap
  82. (lambda (event binding)
  83. (cond
  84. ;; Skip nested/prefix keymaps entirely
  85. ((keymapp binding) nil)
  86. ;; Handle standard events (symbols or characters)
  87. ((or (symbolp event) (integerp event))
  88. (let ((key-str (single-key-description event)))
  89. ;; Filter for simple keys (no spaces, meaning no multi-key sequences)
  90. (unless (string-match-p " " key-str)
  91. (push (cons key-str binding) bindings))))))
  92. map)))
  93. (map-extract keymap)
  94. ;; Sort logically by key description
  95. ;; Print the results cleanly
  96. (with-current-buffer buf
  97. (text-mode)
  98. (setq buffer-read-only nil)
  99. (erase-buffer)
  100. (insert (format "No-Prefix Bindings for: %s\n" (or map-name "Selected Map")))
  101. (insert "==================================================\n\n")
  102. (dolist (b bindings)
  103. (insert (format "%-15s -> %s\n" (car b) (cdr b))))
  104. (setq buffer-read-only t))
  105. (pop-to-buffer buf))))
  106. (defun my/insert-file-path (file)
  107. "Read a file name from the minibuffer and insert its absolute path at point."
  108. (interactive "fInsert path to file: ")
  109. (insert (expand-file-name file)))
  110. (provide 'utils)