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.

221 lines
7.4 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. (defun leo/org-global-props (&optional property buffer)
  69. "Get the plists of global org PROPERTY of current BUFFER."
  70. (unless property (setq property "PROPERTY"))
  71. (with-current-buffer (or buffer (current-buffer))
  72. (org-element-map (org-element-parse-buffer) 'keyword (lambda (el) (when (string-match property (org-element-property :key el)) el)))))
  73. (defun leo/org-global-prop-value (key)
  74. "Get global org property KEY (case sensitive) of current buffer."
  75. (org-element-property :value (car (leo/org-global-props key))))
  76. (defun leo/deft-insert-boilerplate ()
  77. "Insert boilerplate into newly create roam note."
  78. (interactive)
  79. (let ((title (leo/org-global-prop-value "TITLE"))
  80. (setupfile (leo/org-global-prop-value "SETUPFILE"))
  81. (startup (leo/org-global-prop-value "STARTUP")))
  82. ;; If we have neither SETUPFILE nor STARTUP
  83. (when (and title
  84. (not setupfile)
  85. (not startup))
  86. (kill-region (point-min) (point-max))
  87. (goto-char (point-min))
  88. (insert (format "#+TITLE: %s\n" title))
  89. (insert "#+SETUPFILE: setup/setup.org\n")
  90. (insert "\n")
  91. (insert "* Metadata:\n")
  92. (insert "** Tags: ")
  93. (goto-char (point-max)))
  94. ;; If we only have STARTUP
  95. (when (and title
  96. startup
  97. (not setupfile))
  98. (goto-char (point-min))
  99. (search-forward "STARTUP")
  100. (beginning-of-line)
  101. (kill-line)
  102. (insert "#+SETUPFILE: setup/setup.org")
  103. (message "Updated Global Properties"))
  104. ))
  105. (defun leo/org-narrow-prev-tree ()
  106. "When in a narrowed region, this will take you to the previous heading and narrow."
  107. (interactive)
  108. (goto-char (point-min))
  109. (widen)
  110. (org-backward-heading-same-level 1)
  111. (org-narrow-to-subtree))
  112. (defun leo/org-narrow-next-tree ()
  113. "When in a narrowed region, this will take you to the next heading and narrow."
  114. (interactive)
  115. (goto-char (point-min))
  116. (widen)
  117. (org-forward-heading-same-level 1)
  118. (org-narrow-to-subtree))
  119. (defun leo/org-present ()
  120. "Begin an `org-mode' presentation."
  121. (interactive)
  122. (defvar old-mlf nil "Temp storage of mode-line-format while in 'present mode'.")
  123. (if (buffer-narrowed-p)
  124. (progn
  125. (widen)
  126. (setq mode-line-format old-mlf)
  127. (setq cursor-type 'box)
  128. (read-only-mode -1)
  129. (text-scale-adjust 0)
  130. (visual-line-mode nil)
  131. (toggle-word-wrap nil)
  132. (message "No longer presenting")
  133. )
  134. (setq old-mlf mode-line-format)
  135. (setq mode-line-format nil)
  136. (org-narrow-to-subtree)
  137. (setq cursor-type 'hbar)
  138. (read-only-mode 1)
  139. (text-scale-adjust 5)
  140. (visual-line-mode 1)
  141. (toggle-word-wrap 1)
  142. (message "Presenting")
  143. ))
  144. (defun leo/org-roam-reformat-tags ()
  145. "Search for Tags: in document and replace the old single-line format with a newline separated list."
  146. (interactive)
  147. ;; (save-excursion
  148. ;; (goto-char (point-min))
  149. ;; (if (re-search-forward "Tags:")
  150. ;; (progn
  151. ;; (replace-regexp "Tags: \\[" "Tags:\n- [" nil (point-at-bol) (point-at-eol))
  152. ;; (replace-regexp "\\] \\[" "] \n- [" nil (point-at-bol) (point-at-eol))
  153. ;; (message "Tags formatted!")
  154. ;; )
  155. ;; (message "Tags not found, format aborted")))
  156. )
  157. (defun leo/remove-dos-eol ()
  158. "Do not show ^M in files containing mixed UNIX and DOS line endings."
  159. (interactive)
  160. (setq buffer-display-table (make-display-table))
  161. (aset buffer-display-table ?\^M []))
  162. (defun leo/tabnine-toggle ()
  163. "Toggle TabNine for this buffer"
  164. (interactive)
  165. (if (member '(company-tabnine) company-backends)
  166. (leo/tabnine-disable nil)
  167. (leo/tabnine-enable nil)))
  168. (defun leo/tabnine-only-toggle ()
  169. "Toggle ONLY TabNine completion for this buffer"
  170. (interactive)
  171. (if (not (member '(company-tabnine) company-backends))
  172. (leo/tabnine-enable t)
  173. (leo/tabnine-disable t)))
  174. (defun leo/tabnine-enable (only)
  175. "turn on TabNine for this buffer using ONLY tabnine or suplimentally adding it"
  176. (if (not only)
  177. (setq-local company-backends (add-to-list 'company-backends '(company-tabnine)))
  178. (progn
  179. (setq-local backends-list 'company-backends)
  180. (setq-local company-backends '(company-tabnine))))
  181. (message "TabNine enabled! :)")
  182. )
  183. (defun leo/tabnine-disable (restore)
  184. "turn off TabNine for this buffer, RESTORE it from saved backends list"
  185. (if (not restore)
  186. (setq-local company-backends (delete '(company-tabnine) company-backends))
  187. (progn
  188. (setq-local company-backends 'backends-list)
  189. (setq-local backends-list nil))
  190. )
  191. (message "TabNine disabled :(")
  192. )
  193. (provide 'functions)
  194. ;;; functions.el ends here