Giter VIP home page Giter VIP logo

yank-indent's Introduction

Logo
yank-indent

Emacs minor-mode that ensures pasted (yanked) text has the correct indentation level.

GitHub tag (latest SemVer) GitHub issues GitHub pull requests License Status

Do you often find yourself fixing the indentation of a code snippet right after pasting it somewhere? Never again! yank-indent is the answer.

Features

  • yank-indent-mode minor-mode that automatically calls indent-region on yanked/pasted text.
  • global-yank-indent-mode which is a set-it-and-forget-it style global mode that enables yank-indent-mode in relevant buffers, with a sensible default list of major-modes to exclude.
  • By default does not trigger indent-region if pasted text is longer than 5000 characters. This threshold can be can be customized with yank-indent-threshold.

Installation

use-package + straight.el

(use-package yank-indent
  :straight (:host github :repo "jimeh/yank-indent")
  :config (global-yank-indent-mode t))

Manual

Place yank-indent.el somewhere in your load-path and require it. For example ~/.emacs.d/vendor:

(add-to-list 'load-path "~/.emacs.d/vendor")
(require 'yank-indent)
(global-yank-indent-mode t)

Usage

global-yank-indent-mode

With global-yank-indent-mode enabled, you will find that yank-indent-mode is automatically enabled in relevant buffers. The defaults will specifically exclude common languages which are indentation sensitive like Python, YAML, Makefile, etc.

For fine-grained control over which major-modes it is enabled in or not, see customization options with M-x customize-group RET yank-indent.

yank-indent-mode

If you prefer not to use the global mode, you can add yank-indent-mode as a hook to relevant major-modes, or even manually toggle it on and off with M-x yank-indent-mode.

To skip the indent operation for a single yank command, use a prefix command, so C-u C-y instead of just C-y.

Keep in mind that the include/exclude major-mode customizations only affect the global mode and which buffers it enables yank-indent-mode in. If you explicitly enable yank-indent-mode in a buffer, it will operate like normal regardless of what major-mode the buffer is using.

Alternative Packages

  • snap-indent (melpa): Very similar with the addition of being able to trigger indentation on save, and extra custom formatting functions to run right after indentation.
  • auto-indent-mode (melpa): Triggers indentation in a whole suite of scenarios, more or less trying to ensure everything is always correctly indented. This also includes indenting any yanked regions. Does not seem to support any thresholds to avoid triggering indentation for large buffers/yanked text.

yank-indent's People

Contributors

github-actions[bot] avatar jimeh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

schievel1

yank-indent's Issues

This would be great if it worked for evil as well.

Should be easily possible, by using evil-paste-after instead of the yank command and maybe use evil-get-marker instead of region-beginnning/end

edit:
I am doing this here and it works quite fine with my first tries. https://github.com/Schievel1/evil-paste-indent/blob/47791aedb4053fd5ee1e15bdccd7a4a4115fa3dd/yank-indent.el#L136
But I would prefer having everything in one single package instead.

Also may instead of using post-command-hook maybe it's better to use advices for the yank/ yank-pop/ evil-paste-after/ evil-paste-before commands? Because with the post command hook we run our function that checks if things need to be indented on every single command that is run if I am not mistaken.
I also gave that a try here https://github.com/Schievel1/evil-paste-indent/blob/47791aedb4053fd5ee1e15bdccd7a4a4115fa3dd/yank-indent.el#L126

edit2: I see that you were using an advice before and switched to the post-command-hook thing because advices are not buffer local. I still rather think the advice is the better choice, because we can use evil-with-single-undo (or since emacs 29.1 with-undo-amalgamate to not depend on evil) to merge the pasting and the indentation into a single undo step.
This is more important for users than a idiomatic implementation of the modes. Most people just activate that global mode in their config and leave it on forever, never touching the minor mode command.
Now I made it so in my version, that there is a global mode, that needs to be activated. Otherwise the minor-mode will give an user-error if it is tried to be activated and is not activated. The global mode adds the advices, the minor-mode does nothing other then setting its minor mode variable. Only if this variable is true the indentation is executed.

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.