Duplicating a string in emacs with Ace-Jump

I'm new to elisp, but one thing I really want to understand is either to wait for the ace jump to finish before following the instructions, or how to get the position from the ace jump instead of moving the cursor. My goal is to be able to select a line with ace-jump, copy it, and then paste it right above my current line. I started by first trying to go to the line with an ace jump and then duplicating it in place, but that didn't work. Here is what I have for this:

(defun ace-jump-yank-line-above ()
  (interactive)
  (ace-jump-line-mode)
  (kill-ring-save (line-beginning-position) (line-beginning-position 2) )
  (yank)
)

But it gives me weird behavior

+4
source share
4 answers

, , script, . , :

;; The base function for the line-based ones
(defun ace-jump-end-do (dfunc afunc)

  ;; Save where to return to as a marker
  (setq ace-jump-do-retpos (make-marker))
  (set-marker ace-jump-do-retpos (point))
  ;; Add our during function to the hook
  (setq ace-jump-mode-end-hook
        (list `(lambda()
                 (progn
                 (setq ace-jump-mode-end-hook)
                 (,dfunc)
                 (goto-char ace-jump-do-retpos)
                 (set-marker ace-jump-do-retpos nil)
                 (,afunc)
                 ))))

  (ace-jump-line-mode)
  )

;; Copy the line above the current line
(defun ace-jump-yank-line-above ()
  (interactive)
  (ace-jump-end-do
   ;; At the line
   (lambda ()
     ;; Store the line in a variable
     (setq line (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
     )

   ;; Upon returning
   (lambda ()
     (save-excursion
       (goto-char (point-at-bol))
       (insert (concat line "\n"))
       )
     (when (bolp) (goto-char (point-at-bol 2)))
  )))

, - . , , . , - .

0

lispy.el. , ace-jump-mode - . , lispy-ace-symbol ace . - ace-jump-mode-hook:

(defun lispy--ace-do (x bnd &optional filter func no-narrow)
  "Use `ace-jump-do' to X within BND when FILTER return t.
When FUNC is not nil, call it after a successful move.
When NO-NARROW is not nil, don't narrow to BND."
  (require 'ace-jump-mode)
  (lispy--recenter-bounds bnd)
  (unless no-narrow
    (narrow-to-region (car bnd) (cdr bnd)))
  (when func
    (setq ace-jump-mode-end-hook
          (list `(lambda()
                   (setq ace-jump-mode-end-hook)
                   (,func)))))
  (let ((ace-jump-mode-scope 'window)
        (ace-jump-search-filter filter))
    (ace-jump-do x))
  (widen))
+4

-, - , - ( ace-jump-line-mode):

(defun ace-jump-yank-line-above ()
  (interactive)
  (let ((loc (point-at-bol))
        (line nil))
    (save-excursion
      (ace-jump-line-mode)
      (setq line (buffer-substring-no-properties
                  (point-at-bol) (point-at-eol)))
      (goto-char (1- loc))
      (if (bobp)
          (insert (concat line "\n"))
        (insert (concat "\n" line))))))
+2

ace-jump-mode ... - , , : .

, ace-jump . save-excursion, , .

I wrote a new package to solve all these problems, you can find it at https://github.com/Fuco1/better-jump We hope that people will pick it up, but it will suit me, at least. It took me about 2 hours to write a basic working prototype, and it already covers all the functionality of packages like ace-link, ace-window and ace-whatever-else-you-can-find (also an ace jump, obviously :) )

0
source

Source: https://habr.com/ru/post/1528008/


All Articles