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

;;; functions.el --- A collection of my helper functions
;;
;;; Commentary:
;;
;;; Code:
(defun leo/edit-config ()
"Open ~/.doom.d/config.el."
(interactive)
(find-file "~/.doom.d/config.el"))
(defun leo/tidy ()
"Indent, untabify and unwhitespacify current buffer, or region if active."
(interactive)
(let ((beg (if (region-active-p) (region-beginning) (point-min)))
(end (if (region-active-p) (region-end) (point-max))))
(let ((inhibit-message t))
(indent-region beg end))
(whitespace-cleanup)
(untabify beg (if (< end (point-max)) end (point-max)))
(if (region-active-p) (message "Indenting Region...Done") (message "Indenting File...Done"))))
(defun leo/comment-or-uncomment-region-or-line ()
"Comment or uncomment the region or the current line if there's no active region."
(interactive)
(let (beg end)
(if (region-active-p)
(setq beg (region-beginning) end (region-end))
(setq beg (line-beginning-position) end (line-end-position)))
(comment-or-uncomment-region beg end)))
(defun leo/duplicate-thing (comment)
"Duplicates the current line, or the region if active. If an argument (COMMENT) is given, the duplicated region will be commented out."
(interactive "P")
(save-excursion
(let ((start (if (region-active-p) (region-beginning) (point-at-bol)))
(end (if (region-active-p) (region-end) (point-at-eol))))
(goto-char end)
(unless (region-active-p)
(newline))
(insert (buffer-substring start end))
(when comment (comment-region start end)))))
(defun leo/kill-this-buffer-unless-scratch ()
"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."
(interactive)
(if (or (string= (buffer-name) "*dashboard*") (string= (buffer-name) "*scratch*"))
(progn
(bury-buffer (buffer-name))
(switch-to-buffer (other-buffer)))
(kill-this-buffer)))
(defun leo/jump-to-symbol (&optional backwardp)
"Jumps to the next symbol near the point if such a symbol exists. If BACKWARDP is non-nil it jumps backward."
(let* ((point (point))
(bounds (find-tag-default-bounds))
(beg (car bounds)) (end (cdr bounds))
(str (isearch-symbol-regexp (find-tag-default)))
(search (if backwardp 'search-backward-regexp
'search-forward-regexp)))
(goto-char (if backwardp beg end))
(funcall search str nil t)
(cond ((<= beg (point) end) (goto-char point))
(backwardp (forward-char (- point beg)))
(t (backward-char (- end point))))))
(defun leo/jump-to-prev-symbol ()
"Jumps to the previous occurrence of the symbol at point."
(interactive)
(leo/jump-to-symbol t))
(defun leo/jump-to-next-symbol ()
"Jumps to the next occurrence of the symbol at point."
(interactive)
(leo/jump-to-symbol))
(defun leo/org-global-props (&optional property buffer)
"Get the plists of global org PROPERTY of current BUFFER."
(unless property (setq property "PROPERTY"))
(with-current-buffer (or buffer (current-buffer))
(org-element-map (org-element-parse-buffer) 'keyword (lambda (el) (when (string-match property (org-element-property :key el)) el)))))
(defun leo/org-global-prop-value (key)
"Get global org property KEY (case sensitive) of current buffer."
(org-element-property :value (car (leo/org-global-props key))))
(defun leo/deft-insert-boilerplate ()
"Insert boilerplate into newly create roam note."
(interactive)
(let ((title (leo/org-global-prop-value "TITLE"))
(setupfile (leo/org-global-prop-value "SETUPFILE"))
(startup (leo/org-global-prop-value "STARTUP")))
;; If we have neither SETUPFILE nor STARTUP
(when (and title
(not setupfile)
(not startup))
(kill-region (point-min) (point-max))
(goto-char (point-min))
(insert (format "#+TITLE: %s\n" title))
(insert "#+SETUPFILE: setup/setup.org\n")
(insert "\n")
(insert "* Metadata:\n")
(insert "** Tags: ")
(goto-char (point-max)))
;; If we only have STARTUP
(when (and title
startup
(not setupfile))
(goto-char (point-min))
(search-forward "STARTUP")
(beginning-of-line)
(kill-line)
(insert "#+SETUPFILE: setup/setup.org")
(message "Updated Global Properties"))
))
(defun leo/org-narrow-prev-tree ()
"When in a narrowed region, this will take you to the previous heading and narrow."
(interactive)
(goto-char (point-min))
(widen)
(org-backward-heading-same-level 1)
(org-narrow-to-subtree))
(defun leo/org-narrow-next-tree ()
"When in a narrowed region, this will take you to the next heading and narrow."
(interactive)
(goto-char (point-min))
(widen)
(org-forward-heading-same-level 1)
(org-narrow-to-subtree))
(defun leo/org-present ()
"Begin an `org-mode' presentation."
(interactive)
(defvar old-mlf nil "Temp storage of mode-line-format while in 'present mode'.")
(if (buffer-narrowed-p)
(progn
(widen)
(setq mode-line-format old-mlf)
(setq cursor-type 'box)
(read-only-mode -1)
(text-scale-adjust 0)
(visual-line-mode nil)
(toggle-word-wrap nil)
(message "No longer presenting")
)
(setq old-mlf mode-line-format)
(setq mode-line-format nil)
(org-narrow-to-subtree)
(setq cursor-type 'hbar)
(read-only-mode 1)
(text-scale-adjust 5)
(visual-line-mode 1)
(toggle-word-wrap 1)
(message "Presenting")
))
(defun leo/org-roam-reformat-tags ()
"Search for Tags: in document and replace the old single-line format with a newline separated list."
(interactive)
;; (save-excursion
;; (goto-char (point-min))
;; (if (re-search-forward "Tags:")
;; (progn
;; (replace-regexp "Tags: \\[" "Tags:\n- [" nil (point-at-bol) (point-at-eol))
;; (replace-regexp "\\] \\[" "] \n- [" nil (point-at-bol) (point-at-eol))
;; (message "Tags formatted!")
;; )
;; (message "Tags not found, format aborted")))
)
(defun leo/remove-dos-eol ()
"Do not show ^M in files containing mixed UNIX and DOS line endings."
(interactive)
(setq buffer-display-table (make-display-table))
(aset buffer-display-table ?\^M []))
(defun leo/tabnine-toggle ()
"Toggle TabNine for this buffer"
(interactive)
(if (member '(company-tabnine) company-backends)
(leo/tabnine-disable nil)
(leo/tabnine-enable nil)))
(defun leo/tabnine-only-toggle ()
"Toggle ONLY TabNine completion for this buffer"
(interactive)
(if (not (member '(company-tabnine) company-backends))
(leo/tabnine-enable t)
(leo/tabnine-disable t)))
(defun leo/tabnine-enable (only)
"turn on TabNine for this buffer using ONLY tabnine or suplimentally adding it"
(if (not only)
(setq-local company-backends (add-to-list 'company-backends '(company-tabnine)))
(progn
(setq-local backends-list 'company-backends)
(setq-local company-backends '(company-tabnine))))
(message "TabNine enabled! :)")
)
(defun leo/tabnine-disable (restore)
"turn off TabNine for this buffer, RESTORE it from saved backends list"
(if (not restore)
(setq-local company-backends (delete '(company-tabnine) company-backends))
(progn
(setq-local company-backends 'backends-list)
(setq-local backends-list nil))
)
(message "TabNine disabled :(")
)
(provide 'functions)
;;; functions.el ends here