My personal configuration files for Doom emacs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.9 KiB

  1. ;;; functions.el --- A collection of my helper functions
  2. ;;
  3. ;;; Commentary:
  4. ;;
  5. ;;; Code:
  6. (defun leo/edit-config ()
  7. "Open ~/.doom.d/config.el."
  8. (interactive)
  9. (find-file "~/.doom.d/config.el"))
  10. (defun leo/tidy ()
  11. "Indent, untabify and unwhitespacify current buffer, or region if active."
  12. (interactive)
  13. (let ((beg (if (region-active-p) (region-beginning) (point-min)))
  14. (end (if (region-active-p) (region-end) (point-max))))
  15. (let ((inhibit-message t))
  16. (indent-region beg end))
  17. (whitespace-cleanup)
  18. (untabify beg (if (< end (point-max)) end (point-max)))
  19. (if (region-active-p) (message "Indenting Region...Done") (message "Indenting File...Done"))))
  20. (defun leo/comment-or-uncomment-region-or-line ()
  21. "Comment or uncomment the region or the current line if there's no active region."
  22. (interactive)
  23. (let (beg end)
  24. (if (region-active-p)
  25. (setq beg (region-beginning) end (region-end))
  26. (setq beg (line-beginning-position) end (line-end-position)))
  27. (comment-or-uncomment-region beg end)))
  28. (defun leo/duplicate-thing (comment)
  29. "Duplicates the current line, or the region if active. If an argument (COMMENT) is given, the duplicated region will be commented out."
  30. (interactive "P")
  31. (save-excursion
  32. (let ((start (if (region-active-p) (region-beginning) (point-at-bol)))
  33. (end (if (region-active-p) (region-end) (point-at-eol))))
  34. (goto-char end)
  35. (unless (region-active-p)
  36. (newline))
  37. (insert (buffer-substring start end))
  38. (when comment (comment-region start end)))))
  39. (defun leo/kill-this-buffer-unless-scratch ()
  40. "Works like `kill-this-buffer' unless the current buffer is the *scratch* buffer. In which case the buffer content is deleted and the buffer is buried."
  41. (interactive)
  42. (if (or (string= (buffer-name) "*dashboard*") (string= (buffer-name) "*scratch*"))
  43. (progn
  44. (bury-buffer (buffer-name))
  45. (switch-to-buffer (other-buffer)))
  46. (kill-this-buffer)))
  47. (defun leo/jump-to-symbol (&optional backwardp)
  48. "Jumps to the next symbol near the point if such a symbol exists. If BACKWARDP is non-nil it jumps backward."
  49. (let* ((point (point))
  50. (bounds (find-tag-default-bounds))
  51. (beg (car bounds)) (end (cdr bounds))
  52. (str (isearch-symbol-regexp (find-tag-default)))
  53. (search (if backwardp 'search-backward-regexp
  54. 'search-forward-regexp)))
  55. (goto-char (if backwardp beg end))
  56. (funcall search str nil t)
  57. (cond ((<= beg (point) end) (goto-char point))
  58. (backwardp (forward-char (- point beg)))
  59. (t (backward-char (- end point))))))
  60. (defun leo/jump-to-prev-symbol ()
  61. "Jumps to the previous occurrence of the symbol at point."
  62. (interactive)
  63. (leo/jump-to-symbol t))
  64. (defun leo/jump-to-next-symbol ()
  65. "Jumps to the next occurrence of the symbol at point."
  66. (interactive)
  67. (leo/jump-to-symbol))
  68. (provide 'functions)
  69. ;;; functions.el ends here