Code Monkey home page Code Monkey logo

move-text's Introduction

Move-Text CI Tests MELPA MELPA Stable

Move Text

MoveText allows you to move the current line using M-up / M-down (or any other bindings you choose) if a region is marked, it will move the region instead.

Using the prefix arg (C-u number or META number) will predetermine how many lines to move.

Install from MELPA (or MELPA stable)

M-x package-install move-text <RETURN>

If you want to use the default bindings, add the following to .emacs anywhere after (package-initialize):

(move-text-default-bindings)

This sets the keyboard shortcuts:

  • Meta-up move-text-up (line or active region)
  • Meta-down move-text-down (line or active region)

Demonstration

Indent after moving...

@jbreeden gave us this useful function advice to have Emacs re-indent the text in-and-around a text move.

(defun indent-region-advice (&rest ignored)
  (let ((deactivate deactivate-mark))
    (if (region-active-p)
        (indent-region (region-beginning) (region-end))
      (indent-region (line-beginning-position) (line-end-position)))
    (setq deactivate-mark deactivate)))

(advice-add 'move-text-up :after 'indent-region-advice)
(advice-add 'move-text-down :after 'indent-region-advice)

move-text's People

Contributors

jasonm23 avatar jcs090218 avatar paaguti avatar tarsius avatar wigust avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

move-text's Issues

M-down (move-text-down) returns error: Symbol's function definition is void: save-mark-and-excursion

M-up is working as expected.

When I have the cursor in a line (no region selected) and do M-down:

  • The line the cursor was in is NOT moved down
  • The cursor jumps to the bottom of the buffer
  • In the minibuffer, I see: "Symbol's function definition is void: save-mark-and-excursion"
  • In Messages I find: move-text--last-line-is-just-newline: Symbol's function definition is void: save-mark-and-excursion

I just upgraded the package from MELPA. Realized the above was all in Emac 24.something I was still using.

Upgraded to Emacs 25 and am seeing the same behavior, with a slightly different message:
move-text--last-line-is-just-newline: Invalid function: save-mark-and-excursion

Let me know if there's any more info I can provide.

move-text-up moves point too far

Given the following text in a buffer, where | is point (note final blank line):

first
second
|third

move-text-up produces the following:

|first
third
second

whereas the expected behaviour is:

first
|third
second

I have reproduced this on both Emacs 24.5 and Emacs trunk, so I assumed the version check (for 24.3+) in the code was simply incorrect.

Note that behaviour is slightly different if the current line is the last line in the buffer. Before:


first
second
|third

after:

|
second
first
third

Add prefix option

It would be nice to be able to C-u and then hit a number for how far down you want to move some line/region (I would be okay with implementing this myself, but it will probably take a while before I learn emacs lips)

Fail: When a region/mark has never been set in the buffer

I'm using (interactive "r/np") (get region and prefix arg) Which fails if the mark was never set in the local buffer.

I'll fix it with something like:

(defun test-it (start end n)
  (interactive
   (let ((args `(,@(if (region-active-p)
                       `(,(region-beginning) ,(region-end))
                     `(nil nil))
                 ,current-prefix-arg)))
     args))
(message "s:%s e:%s prefix:%s" start end n))

Indent text while moving

Hey there,

I've created some advice for adjusting the indentation while moving text. Thought I'd share in case you'd want to add that behavior to the package, perhaps behind an option or as another function.

My initial implementation goes like so:

(defun indent-region-advice (&rest ignored)
  (let ((deactivate deactivate-mark))
    (if (region-active-p)
        (indent-region (region-beginning) (region-end))
      (indent-region (line-beginning-position) (line-end-position)))
    (setq deactivate-mark deactivate)))

(advice-add 'move-text-up :after 'indent-region-advice)
(advice-add 'move-text-down :after 'indent-region-advice)

It's a minor convenience, to be sure, since you can just hit TAB after moving text. Still, it's pretty satisfying to watch, and it's nice to have one less thing to think about when you're refactoring.

Thanks for sharing move-text!

Possible conflict between move-text and org-mode (table)

Steps to reproduce

In a scratch buffer, run org-mode and copy-paste the following:

   | =]]= | text 1 |
   | =[[= | text 2 |

Run move-text-line-down

Expected

   | =[[= | text 2 |
   | =]]= | text 1 |

Result

   | == | text 2 |
   | == | text 1 |

There is also a large underline from the second = on the first line to the first = on the second line.

Version info

org-mode-version 8.2.10
emacs 25.2.2

Consider adding to Elpa

Hello,
I really like this package, IMO it would be great if it was part of Emacs. We may have a chance to do so through https://github.com/josteink/emacs-oob-reboot/ (project to enhance the experience by making small changes and suggesting them to the core devs).

And the first step would be to add move-text to Elpa, and thus to fill out the copyright papers (you and the contributors (hello @wigust - tarsius did it)): https://www.gnu.org/prep/maintain/html_node/Copyright-Papers.html (don't hesitate to ask questions on the other issue, they'll be able to help).

Are you willing to do so ? Regards.

M-up, M-down not working in python

M-up and M-down dont work in python files. In other files it works fine, but trying it in a python file, i see the the minibuffer:

Can't splice top level.

Why is this happening?

Study lexical binding

I have enabled native compilation on master and got a bugging message:

Warning (comp): ~/.emacs.d/elpa/move-text-20221006.1634/move-text.el:1:1: Warning: file has no ‘lexical-binding’ directive on its first line

It is nothing that would blow up things, but it would be nice to get rid of this.

The solution is easy, but it would need to be tested on early emacsen to which I have no access

Debugger entered--Lisp error: (void-variable repeat)

After a recent update, I get the following error when using move-text-down

Debugger entered--Lisp error: (void-variable repeat)
  move-text-down(nil nil 1)
  funcall-interactively(move-text-down nil nil 1)
  call-interactively(move-text-down nil nil)
  command-execute(move-text-down)

Cursor randomly moves after save-buffer

For several days, I had experienced this weird behavior that sometimes I save-buffer using the function or using key-binding (C-x C-s), the cursor moves to somewhere far away in the buffer. It happens in the ESS-R mode. I have tried to disable save-place-mode, but doesn't work until I commented the following line:

(add-hook 'before-save-hook 'delete-trailing-whitespace)

I'm not sure if it is related to the move-text package, or maybe just related to save-hook because somebody mentioned that to check save-hook. Link here:
https://www.reddit.com/r/emacs/comments/kt1oml/can_anybody_help_me_pinpoint_the_cause_for_cursor/

Thanks.

org-meta-up

Will this interfere with org-meta-up, or how do they work together?;)

Last line and penultimate line

There are issues with last line and penultimate line (if the last line is just a newline)

Extra lines are added to the buffer/narrowed if transpose lines is invoked.

Can`nt work in Elisp mode

Hi,all:
When I use " M+up/down " to move the line in emacs init.el ,it cannt work,and make a error, I think thats key conflict.

The following is the error msg:

entered--Lisp error: (error "Can’t splice top level.")
signal(error ("Can’t splice top level."))
error("Can't splice top level.")
paredit-splice-sexp((4))
paredit-splice-sexp-killing-backward(nil)
funcall-interactively(paredit-splice-sexp-killing-backward nil)
call-interactively(paredit-splice-sexp-killing-backward nil nil)
command-execute(paredit-splice-sexp-killing-backward)

So, how to solve the issue?

expand selection to whole lines

It would be nice to also have two commands move-whole-lines-up and move-whole-lines-down that do the same as move-text-up resp. move-text-down but expands the selection to whole lines before moving the text.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.