Skip to content

Instantly share code, notes, and snippets.

@ruliana
Created September 29, 2020 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruliana/14435df3a33ca63a857d6e97294884ba to your computer and use it in GitHub Desktop.
Save ruliana/14435df3a33ca63a857d6e97294884ba to your computer and use it in GitHub Desktop.
Spacemacs utilities - minor stuff I use here and there
;; Reference: https://stackoverflow.com/questions/18102004/emacs-evil-mode-how-to-create-a-new-text-object-to-select-words-with-any-non-sp
(defmacro define-and-bind-text-object (key start-regex end-regex)
"Create and bind a text object to a key using regexp as delimiters"
(let ((inner-name (make-symbol "inner-name"))
(outer-name (make-symbol "outer-name")))
`(progn
(evil-define-text-object ,inner-name (count &optional beg end type)
(evil-select-paren ,start-regex ,end-regex beg end type count nil))
(evil-define-text-object ,outer-name (count &optional beg end type)
(evil-select-paren ,start-regex ,end-regex beg end type count t))
(define-key evil-inner-text-objects-map ,key (quote ,inner-name))
(define-key evil-outer-text-objects-map ,key (quote ,outer-name)))))
(defun evil-copy-accumulate-paragraph-header ()
"Some fun here"
(interactive)
(save-excursion
(backward-paragraph)
(skip-chars-forward "[\t\n ]*")
(let* ((reg-char ?a)
(reg-text (get-register reg-char))
(line-text (thing-at-point 'line))
(full-text (if reg-text (concat reg-text line-text) line-text))
(line-text (progn
(remove-list-of-text-properties 0 (length full-text) '(yank-handler) full-text)
(propertize full-text 'yank-handler '(evil-yank-line-handler)))))
(set-register reg-char line-text))))
;; Reference: https://emacs.stackexchange.com/questions/45202/how-to-define-an-evil-operator-to-move-forward-backward-a-text-object
;; Move using text objects
(evil-define-motion evil-forward-text-object (count)
"Move to the end of following input text-object define in evil-inner-text-objects-map."
(let* ((input-char (read-char-exclusive "text-object?"))
(key (char-to-string input-char))
(text-object (lookup-key evil-inner-text-objects-map key))
(region (funcall text-object count))
(end (nth 1 region)))
(goto-char end)))
(define-key evil-motion-state-map (kbd "S-<right>") #'evil-forward-text-object)
(evil-define-motion evil-backward-text-object (count)
"Move to the beginning of following input text-object define in evil-inner-text-objects-map."
(let* ((input-char (read-char-exclusive "text-object?"))
(key (char-to-string input-char))
(text-object (lookup-key evil-inner-text-objects-map key))
(region (funcall text-object count))
(start (nth 0 region)))
(goto-char start)))
(define-key evil-motion-state-map (kbd "S-<left>") #'evil-backward-text-object)
(spacemacs/declare-prefix "o" "custom")
(spacemacs/set-leader-keys "oc" 'my-custom-command)
;; =================================================
;; Create a reconciliation SQL from a list of words
;; =================================================
(setq starscream/replace-text-diff-sql-template (concat ", 1.0 * count_if(new.\\& is not distinct from old.\\&) / count(1) as match_\\&\n"
", 1.0 * count_if(new.\\& <> old.\\&) / count(1) as non_match_\\&\n"
", 1.0 * count_if(new.\\& is not null and old.\\& is null) / count(1) as added_\\&\n"
", 1.0 * count_if(new.\\& is null and old.\\& is not null) / count(1) as removed_\\&\n"))
(defun starscream/replace-text-diff-sql ()
"Replace the word in a line by 4 lines used to sql diff"
(interactive)
(beginning-of-line)
(while (and
(not (looking-at-p "[[:space:]]*$")) ;; not empty line
(re-search-forward "^[^ \n]+"))
(replace-match starscream/replace-text-diff-sql-template)
(forward-char)))
(spacemacs/declare-prefix "o" "custom")
(spacemacs/set-leader-keys "or" 'starscream/replace-text-diff-sql)
;; ==============================================
;; Experimenting with refined search
;; ===============================================
(defun user/search-class-at-point ()
"Search class definition"
(interactive)
(helm-ag nil (format "--ruby --python -Gmd$ \\bclass\\b.*\\b%s\\b" (symbol-at-point))))
(defun user/search-function-at-point ()
"Search class definition"
(interactive)
(helm-ag nil (format "--ruby --python -Gmd$ \\bdef\\b.*\\b%s\\b" (symbol-at-point))))
(spacemacs/declare-prefix "o" "custom")
(spacemacs/set-leader-keys "oc" 'user/search-class-at-point)
;; Replace function name on description for the text
(which-key-add-key-based-replacements "SPC o c" "search class definition")
(spacemacs/set-leader-keys "of" 'user/search-function-at-point)
;; Replace function name on description for the text
(which-key-add-key-based-replacements "SPC o f" "search function definition")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment