My attempt to optimize my emacs load time <1 second
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.

104 lines
3.6 KiB

5 years ago
  1. ;;; 05-custom-functions --- custom functions prefixed with "leo/"
  2. ;;; Commentary:
  3. ;;; Code:
  4. (defun leo/compile-init ()
  5. "If the current buffer is 'init.el', compile it."
  6. (interactive)
  7. (when (equal (buffer-file-name)
  8. (expand-file-name (concat user-emacs-directory "init.el")))
  9. ;; Avoid running hooks when tangling.
  10. (let ((prog-mode-hook nil))
  11. (byte-compile-file (concat user-emacs-directory "init.el")))))
  12. (defun leo/find-user-init-file ()
  13. "Edit the `~/.emacs.d/init.el' file."
  14. (interactive)
  15. (find-file "~/.emacs.d/init.el"))
  16. (defun leo/tidy ()
  17. "Indent, untabify and unwhitespacify current buffer, or region if active."
  18. (interactive)
  19. (let ((beg (if (region-active-p) (region-beginning) (point-min)))
  20. (end (if (region-active-p) (region-end) (point-max))))
  21. (let ((inhibit-message t))
  22. (indent-region beg end))
  23. (whitespace-cleanup)
  24. (untabify beg (if (< end (point-max)) end (point-max)))
  25. (if (region-active-p) (message "Indenting Region...Done") (message "Indenting File...Done"))))
  26. (defun leo/comment-or-uncomment-region-or-line ()
  27. "Comment or uncomment the region or the current line if there's no active region."
  28. (interactive)
  29. (let (beg end)
  30. (if (region-active-p)
  31. (setq beg (region-beginning) end (region-end))
  32. (setq beg (line-beginning-position) end (line-end-position)))
  33. (comment-or-uncomment-region beg end)))
  34. (defun leo/duplicate-thing (comment)
  35. "Duplicates the current line, or the region if active. If an argument (COMMENT) is given, the duplicated region will be commented out."
  36. (interactive "P")
  37. (save-excursion
  38. (let ((start (if (region-active-p) (region-beginning) (point-at-bol)))
  39. (end (if (region-active-p) (region-end) (point-at-eol))))
  40. (goto-char end)
  41. (unless (region-active-p)
  42. (newline))
  43. (insert (buffer-substring start end))
  44. (when comment (comment-region start end)))))
  45. (defun leo/kill-this-buffer-unless-scratch ()
  46. "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."
  47. (interactive)
  48. (if (or (string= (buffer-name) "*dashboard*") (string= (buffer-name) "*scratch*"))
  49. (progn
  50. (bury-buffer (buffer-name))
  51. (switch-to-buffer (other-buffer)))
  52. (kill-this-buffer)))
  53. (defun leo/jump-to-symbol (&optional backwardp)
  54. "Jumps to the next symbol near the point if such a symbol exists. If BACKWARDP is non-nil it jumps backward."
  55. (let* ((point (point))
  56. (bounds (find-tag-default-bounds))
  57. (beg (car bounds)) (end (cdr bounds))
  58. (str (isearch-symbol-regexp (find-tag-default)))
  59. (search (if backwardp 'search-backward-regexp
  60. 'search-forward-regexp)))
  61. (goto-char (if backwardp beg end))
  62. (funcall search str nil t)
  63. (cond ((<= beg (point) end) (goto-char point))
  64. (backwardp (forward-char (- point beg)))
  65. (t (backward-char (- end point))))))
  66. (defun leo/jump-to-prev-symbol ()
  67. "Jumps to the previous occurrence of the symbol at point."
  68. (interactive)
  69. (leo/jump-to-symbol t))
  70. (defun leo/jump-to-next-symbol ()
  71. "Jumps to the next occurrence of the symbol at point."
  72. (interactive)
  73. (leo/jump-to-symbol))
  74. (defun leo/last-car (list)
  75. "Return only the item in the last position of a LIST."
  76. (car (last list))
  77. )
  78. (defun leo/get-package-from-url (url)
  79. "Accept a URL and return a stripped package name."
  80. (let ((mode
  81. (car
  82. (split-string
  83. (leo/last-car (split-string url "/" t))
  84. "\\." nil)
  85. )
  86. ))
  87. (message mode))
  88. )
  89. (provide '05-custom-functions)
  90. ;;; 05-custom-functions.el ends here