emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/objed fbabb7a 178/216: Add command to insert new object


From: Stefan Monnier
Subject: [elpa] externals/objed fbabb7a 178/216: Add command to insert new object of current type
Date: Tue, 8 Jan 2019 12:29:35 -0500 (EST)

branch: externals/objed
commit fbabb7aa8010308180ab8b2c55164333275ec845
Author: Clemera <address@hidden>
Commit: Clemera <address@hidden>

    Add command to insert new object of current type
---
 objed-objects.el | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 objed.el         | 51 ++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 97 insertions(+), 8 deletions(-)

diff --git a/objed-objects.el b/objed-objects.el
index def9a75..84d9b48 100644
--- a/objed-objects.el
+++ b/objed-objects.el
@@ -361,7 +361,8 @@ Either the symbol `whole' or `inner'.")
 (defun objed--beg (&optional obj)
   "Get beginning position of object.
 
-OBJ is the object to use and defaults to `objed--current-obj'."
+Ignores current object state. OBJ is the object to use and
+defaults to `objed--current-obj'."
   (let ((obj (or obj objed--current-obj)))
     (caar obj)))
 
@@ -369,14 +370,16 @@ OBJ is the object to use and defaults to 
`objed--current-obj'."
 (defun objed--end (&optional obj)
   "Get end position of object.
 
-OBJ is the object to use and defaults to `objed--current-obj'."
+Ignores current object state. OBJ is the object to use and
+defaults to `objed--current-obj'."
   (let ((obj (or obj objed--current-obj)))
     (cadr (car obj))))
 
 (defun objed--other (&optional obj)
   "Return object position opposite to point.
 
-OBJ is the object to use and defaults to `objed--current-obj'."
+Ignores current object state. OBJ is the object to use and
+defaults to `objed--current-obj'."
   (let ((obj (or obj objed--current-obj)))
     (if (/= (point) (objed--end))
         (objed--end obj)
@@ -445,6 +448,51 @@ OBJ is the object to use and defaults to 
`objed--current-obj'."
          (posn (objed--alt obj)))
     (cadr posn)))
 
+(defun objed--obeg (&optional obj)
+  "Get beginning position of object.
+
+OBJ is the object to use and defaults to `objed--current-obj'."
+  (let ((obj (or obj objed--current-obj)))
+    (if (objed--inner-p)
+        (objed--alt-beg obj)
+      (objed--beg obj))))
+
+(defun objed--oend (&optional obj)
+  "Get end position of object.
+
+OBJ is the object to use and defaults to `objed--current-obj'"
+  (let ((obj (or obj objed--current-obj)))
+    (if (objed--inner-p)
+        (objed--alt-end obj)
+      (objed--end obj))))
+
+(defun objed--ibeg (&optional obj)
+  "Get inner beginning position of object.
+
+OBJ is the object to use and defaults to `objed--current-obj'."
+  (let ((obj (or obj objed--current-obj)))
+    (if (objed--inner-p)
+        (objed--beg obj)
+      (objed--alt-beg obj))))
+
+(defun objed--iend (&optional obj)
+    "Get inner end position of object.
+
+OBJ is the object to use and defaults to `objed--current-obj'."
+  (let ((obj (or obj objed--current-obj)))
+    (if (objed--inner-p)
+        (objed--end obj)
+      (objed--alt-end obj))))
+
+(defun objed--get-left-boundary ()
+  "Get left boundary of current object."
+  (buffer-substring (objed--obeg) (objed--ibeg)))
+
+(defun objed--get-right-boundary ()
+  "Get right boundary of current object."
+  (buffer-substring (objed--iend) (objed--oend)))
+
+
 (defun objed--goto-char (pos)
   "Move to position POS possibly skipping leading whitespace."
   (goto-char
diff --git a/objed.el b/objed.el
index 07171f4..b618a02 100644
--- a/objed.el
+++ b/objed.el
@@ -712,6 +712,8 @@ BEFORE and AFTER are forms to execute before/after calling 
the command."
     (define-key map "|"
       (objed-define-op nil objed-ipipe))
 
+    ;; (define-key map (kbd "<C-return>") 'objed-quickrun)
+    (define-key map (kbd "<M-return>") 'objed-insert-new-object)
 
     (define-key map "!"
       (objed-define-op nil objed-replace-op))
@@ -2653,14 +2655,12 @@ Returns cons cell with cmd as car and possible 
arguemtns as cdr."
             (buffer-substring (point) (line-end-position))))))
 
 
-(defun objed--ipipe-to-string (cmd-args str)
+(defun objed--ipipe-to-string (cmdline str)
   "Pipe string STR through CMD-ARGS.
 
 Return restult if any or nil."
-  (let* ((cmdline (minibuffer-contents))
-         (parsed (objed--ipipe-parse cmdline))
-         (cmd (car-safe parsed))
-         (args (cdr-safe parsed)))
+  (let* ((parsed (objed--ipipe-parse cmdline))
+         (cmd (car-safe parsed)))
     (cond ((and cmd (executable-find cmd))
            (with-temp-buffer
              (let ((exit (call-process-region
@@ -2760,6 +2760,47 @@ Commands can be shell commands or region commands."
         (remove-hook 'after-change-functions 'objed--ipipe-schedule-time t)))
     (delete-overlay objed--ipipe-overlay)))
 
+(defun objed-insert-new-object ()
+  "Insert boundaries of object after current one."
+  (interactive)
+  (let* ((opos (objed--oend))
+         (left (objed--get-left-boundary))
+         (right (objed--get-right-boundary))
+         (oc (buffer-substring (objed--obeg) (objed--oend)))
+         (ic (buffer-substring (objed--ibeg) (objed--iend)))
+         (linep (objed--line-p oc))
+         (lispy (derived-mode-p 'lisp-mode
+                                  'emacs-lisp-mode
+                                  'clojure-mode
+                                  'racket-mode
+                                  'lisp-interaction-mode))
+         (nb nil))
+    (goto-char opos)
+    (if (string= ic oc)
+        (insert " " oc)
+      (insert (format "%s%s"
+                      (if (and linep
+                               (not (string= "" ic))
+                               (not (string-match "\\`\n\\|\n\\'" right))
+                               (not (string-match "\\`\n\\|\n\\'" left)))
+                         "\n"
+                        (if (string-match "\n\\'" oc) ""
+                          (if (looking-at "\n") "\n" " ")))
+                      left))
+      ;; stay in the middle
+      (save-excursion
+        (insert (format "%s" right))))
+    ;; dumb search for probably best pos
+    (when (re-search-backward "\\_>" opos t)
+      (re-search-backward "\\_<" opos t))
+    (if (and (not lispy)
+             (setq nb (bounds-of-thing-at-point 'list)))
+        (goto-char (car nb))
+      (when lispy
+        (indent-according-to-mode)))
+    ;; goto ref if there is one...
+    (objed--exit-objed)))
+
 ;; * Exit active state
 
 (defun objed--line-p (text)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]