Commits

Anonymous committed 5d0df4d

[Issue #161] Flip the defaults on `sp-autoinsert-if-followed-by-word`

Comments (0)

Files changed (1)

files/emacs-lisp-mode.el

   (eldoc-mode 1)
   (letcheck-mode t))
 
+(defun my-wrap-region (beg end)
+  (goto-char end)
+  (insert ")")
+  (goto-char beg)
+  (insert "("))
+
+(defun my-goto-dominating-let ()
+  "Find dominating let form"
+  (while (and (> (car (syntax-ppss)) 0)
+              (not (ignore-errors
+                     (backward-up-list)
+                     (save-excursion
+                       (down-list)
+                       (memq (symbol-at-point) '(let let*))))))))
+
+(defun my-extract-to-let (name &optional arg)
+  "Extract the form at point into a variable called NAME placed
+in a let form ARG levels up.
+
+If ARG is \\[universal-argument] place the variable into the most
+inner let form point is inside of."
+  (interactive "sName of variable: \nP")
+  (let ((new-vform (sp-get (sp--next-thing-selection) (delete-and-extract-region :beg-prf :end)))
+        (raw (sp--raw-argument-p arg))
+        (arg (prefix-numeric-value arg)))
+    (save-excursion
+      (cond
+       (raw
+        (my-goto-dominating-let)
+        (progn
+          (down-list)
+          (forward-sexp 2)
+          (backward-down-list)
+          (insert "\n(" name " " new-vform ")")
+          (indent-according-to-mode)))
+       (t
+        (backward-up-list arg)
+        (if (> arg 0)
+            (sp-get (sp--next-thing-selection) (my-wrap-region :beg-prf :end))
+          (insert "()")
+          (backward-char))
+        (insert "let ((" name " " new-vform "))\n")
+        (backward-up-list)
+        (indent-sexp))))
+    (if (> arg 0)
+        (insert name)
+      (forward-sexp)
+      (backward-down-list)
+      (save-excursion
+        (insert "\n")
+        (backward-up-list)
+        (indent-sexp)))))
+
+(defun my-merge-let-forms ()
+  "Merge the most inner let form into the next outer one."
+  (interactive)
+  (save-excursion
+    (my-goto-dominating-let)
+    (down-list)
+    (forward-sexp 2)
+    (backward-sexp)
+    (let ((var-list (sp-get (sp--next-thing-selection) (delete-and-extract-region :beg-prf :end))))
+      (sp-splice-sexp-killing-backward 1)
+      (my-goto-dominating-let)
+      (down-list)
+      (forward-sexp 2)
+      (backward-down-list)
+      (insert "\n" var-list)
+      (backward-down-list)
+      (sp-unwrap-sexp)
+      (backward-up-list 2)
+      (indent-sexp))))
+
 (add-hook 'emacs-lisp-mode-hook 'my-emacs-lisp-init)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.