Library Information
auto-indent-mode.el — Auto indent Minor mode
- Filename – auto-indent-mode.el
- Description – Auto Indent text on Yank/Paste
- Author – Matthew L. Fidler, Le Wang & Others
- Maintainer – Matthew L. Fidler
- Created – Sat Nov 6 11:02:07 2010 (-0500)
- Version – 0.99
- Last-Updated – Tue Aug 21 13:08:42 2012 (-0500)
- By – Matthew L. Fidler
- Update # – 1467
- URL – https://github.com/mlf176f2/auto-indent-mode.el/
- Keywords – Auto Indentation
- Compatibility – Tested with Emacs 23.x
Possible Dependencies
None
About auto-indent-mode
Provides auto-indentation minor mode for Emacs. This allows the following:
- Return automatically indents the code appropriately (if enabled)
- Pasting/Yanking indents the appropriately
- Killing line will take off unneeded spaces (if enabled)
- On visit file, indent appropriately, but DONT SAVE. (Pretend like nothing happened, if enabled)
- On save, optionally unttabify, remove trailing white-spaces, and definitely indent the file (if enabled).
- TextMate behavior of keys if desired (see below)
- Deleting the end of a line will shrink the whitespace to just one (if desired and enabled)
- Automatically indent balanced parenthetical expression, or sexp, if desired auto-indent-current-pairs or auto-indent-next-pair is set to be true (disabled by default). This is not immediate but occurs after a bit to allow better responsiveness in emacs.
- Attempts to set the indentation level (number of spaces for an indent) for a major-mode.
All of these options can be customized. (customize auto-indent)
Installing auto-indent-mode
To use put this in your load path and then put the following in your emacs file:
(setq auto-indent-on-visit-file t)
(require 'auto-indent-mode)
If you (almost) always want this on, add the following to ~/.emacs:
(auto-indent-global-mode)
Excluded modes are defined in auto-indent-disabled-modes-list
If you only want this on for a single mode, you would add the following to ~/.emacs
(add-hook 'emacs-lisp-mode-hook 'auto-indent-minor-mode)
You could always turn on the minor mode with the command auto-indent-minor-mode
Auto-indent and repositories
auto-indent-mode will now be more conservative when it determines that you are in a repository. It will only indent the local area you are editing. This can be changed to be conservative everywhere by:
(setq auto-indent-indent-style 'conservative)
You can revert to the old behavior of aggressive by:
(setq auto-indent-indent-style 'aggressive)
Setting the number of spaces for indenting major modes
While this is controlled by the major mode, as a convenience, auto-indent-mode attempts to set the default number of spaces for an indentation for specific major mode.
This is done by:
- Making local variables of all the variables specified in auto-indent-known-indent-level-variables and setting them to auto-indent’s auto-indent-assign-indent-level
- Looking to see if major mode variables major-mode-indent-level and major-mode-basic-offset variables are present. If either of these variables are present, auto-indent-mode sets these variables to the default auto-indent-assign-indent-level.
TextMate Meta-Return behavior
If you would like TextMate behavior of Meta-RETURN going to the end of the line and then inserting a newline, as well as Meta-shift return going to the end of the line, inserting a semi-colon then inserting a newline, use the following:
(setq auto-indent-key-for-end-of-line-then-newline "<M-return>")
(setq auto-indent-key-for-end-of-line-insert-char-then-newline "<M-S-return>")
(require 'auto-indent-mode)
(auto-indent-global-mode)
This may or may not work on your system. Many times emacs cannot distinguish between M-RET and M-S-RET, so if you don’t mind a slight redefinition use:
(setq auto-indent-key-for-end-of-line-then-newline "<M-return>")
(setq auto-indent-key-for-end-of-line-insert-char-then-newline "<C-M-return>")
(require 'auto-indent-mode)
(auto-indent-global-mode)
If you want to insert something other than a semi-colon (like a colon) in a specific mode, say colon-mode, do the following:
(add-hook 'colon-mode-hook (lambda () (setq auto-indent-eol-char ":")))
Notes about autopair-mode and yasnippet compatibility
If you wish to use this with autopairs and yasnippet, please load this library first.
Using specific functions from auto-indent-mode
Also if you wish to just use specific functions from this library that is possible as well.
To have the auto-indentation-paste use:
(autoload 'auto-indent-yank "auto-indent-mode" "" t)
(autoload 'auto-indent-yank-pop "auto-indent-mode" "" t)
(define-key global-map [remap yank] 'auto-indent-yank)
(define-key global-map [remap yank-pop] 'auto-indent-yank-pop)
(autoload 'auto-indent-delete-char "auto-indent-mode" "" t)
(define-key global-map [remap delete-char] 'auto-indent-delete-char)
(autoload 'auto-indent-kill-line "auto-indent-mode" "" t)
(define-key global-map [remap kill-line] 'auto-indent-kill-line)
However, this does not honor the excluded modes in auto-indent-disabled-modes-list
Making certain modes perform tasks on paste/yank.
Sometimes, like in R, it is convenient to paste c:\ and change it to c:/. This can be accomplished by modifying the auto-indent-after-yank-hook.
The code for changing the paths is as follows:
(defun kicker-ess-fix-path (beg end)
"Fixes ess path"
(save-restriction
(save-excursion
(narrow-to-region beg end)
(goto-char (point-min))
(when (looking-at "[A-Z]:\\\\")
(while (search-forward "\\" nil t)
(replace-match "/"))))))
(defun kicker-ess-turn-on-fix-path ()
(interactive)
(when (string= "S" ess-language)
(add-hook 'auto-indent-after-yank-hook 'kicker-ess-fix-path t t)))
(add-hook 'ess-mode-hook 'kicker-ess-turn-on-fix-path)
Another R-hack is to take of the “>” and “+” of a command line copy. For example copying:
> ## set up
> availDists <- c(Normal<tt>"rnorm", Exponential</tt>"rexp")
> availKernels <- c("gaussian", "epanechnikov", "rectangular",
+ "triangular", "biweight", "cosine", "optcosine")
Should give the following code on paste:
## set up
availDists <- c(Normal<tt>"rnorm", Exponential</tt>"rexp")
availKernels <- c("gaussian", "epanechnikov", "rectangular",
"triangular", "biweight", "cosine", "optcosine")
This is setup by the following code snippet:
(defun kicker-ess-fix-code (beg end)
"Fixes ess path"
(save-restriction
(save-excursion
(save-match-data
(narrow-to-region beg end)
(goto-char (point-min))
(while (re-search-forward "^[ \t]*[>][ \t]+" nil t)
(replace-match "")
(goto-char (point-at-eol))
(while (looking-at "[ \t\n]*[+][ \t]+")
(replace-match "\n")
(goto-char (point-at-eol))))))))
(defun kicker-ess-turn-on-fix-code ()
(interactive)
(when (string= "S" ess-language)
(add-hook 'auto-indent-after-yank-hook 'kicker-ess-fix-code t t)))
(add-hook 'ess-mode-hook 'kicker-ess-turn-on-fix-code)
Auto-indent and org-mode
Auto-indent does not technically turn on for org-mode. Instead the following can be added/changed:
- org-indent-mode is turned on when auto-indent-start-org-indent is true.
- The return behavior is changed to newline and indent in code blocks when auto-indent-fix-org-return is true.
- The backspace behavior is changed to auto-indent’s backspace when auto-indent-delete-backward-char is true. This only works in code blocks.
- The home beginning of line behavior is changed to auto-indent’s when auto-indent-fix-org-move-beginning-of-line is true.
- The yank/paste behavior is changed to auto-indent in a code block when auto-indent-fix-org-yank is true.
- The auto-filling activity in source-code blocks can break your code depending on the language. When auto-indent-fix-org-auto-fill is true, auto-filling is turned off inorg-mode source blocks.
FAQ
Why isn't my mode indenting?
Some modes are excluded for compatability reasons, such as text-modes. This is controlled by the variable auto-indent-disabled-modes-list
Why isn't my specific mode have the right number of spaces?
Actually, the number of spaces for indentation is controlled by the major mode. If there is a major-mode specific variable that controls this offset, you can add this variable to auto-indent-known-indent-level-variables to change the indentation for this mode when auto-indent-mode starts.
See:
Functions
Interactive Functions
Internal Functions
Variables
Customizable Variables
Internal Variables
History
- 15-May-2013 – Made the indentation selective. Should fix Issue #15. 9-May-2013 Matthew L. Fidler Last-Updated: Tue Aug 21 13:08:42 2012 (-0500) #1467 (Matthew L. Fidler) Changed the AI indicator to be used at all times. That way it doesn’t interfere with the diminish package. I believe that is the standard way to take off mode lines. (Matthew L. Fidler)
- 18-Mar-2013 – Should fix issue #14 (Matthew L. Fidler)
- 18-Mar-2013 – Add bug fix for Issue #13 (Matthew L. Fidler)
- 13-Mar-2013 – Push again after merging minibuffer fix (Matthew L. Fidler)
- 13-Mar-2013 – Remove all starred comments. (Matthew L. Fidler)
- 13-Mar-2013 – Fixed Github Issue #11. Org-readme doesn’t like the starred variable names (Matthew L. Fidler)
- 13-Mar-2013 – Figured out the duplicated information was caused by a starred variable. This convention is now depreciated, and caused an error with org-readme. Therefore, it has been changed. (Matthew L. Fidler)
- 13-Mar-2013 – Push new version. I believe that the strange duplication issue is fixed. (Matthew L. Fidler)
- 13-Mar-2013 – Attempt to fix issue #11 (Matthew L. Fidler)
- 05-Dec-2012 – Added support for new ergoemacs-mode. Also provided updated bug-fix for indent-region (Matthew L. Fidler)
- 19-Nov-2012 – Bug fix for aligning parenthetical region when a yasnippet is active (It messes up yasnippet expansions.) (Matthew L. Fidler)
- 12-Nov-2012 – Bug fix for overflows and NaNs (Matthew L. Fidler)
- 17-Oct-2012 – Bug fix for yanking in org-mode. (Matthew L. Fidler)
- 17-Oct-2012 – Now auto-indent-mode can suppress auto-fill in source code blocks. Small bug fix for yanking. (Matthew L. Fidler)
- 12-Oct-2012 – Add auto-indent on yank support for org-mode code buffers (Matthew L. Fidler)
- 12-Oct-2012 – Removed History section from texinfo file. (Matthew L. Fidler)
- 12-Oct-2012 – Fix header readme by using the latest version of org-readme. (Matthew L. Fidler)
- 12-Oct-2012 – Took out documentation that started with a star since it messes up org-readme. (Matthew L. Fidler)
- 12-Oct-2012 – Trying to fix header (Matthew L. Fidler)
- 12-Oct-2012 – Added better org-mode support for code-blocks. (Matthew L. Fidler)
- 12-Sep-2012 – Fixed commentary section. (Matthew L. Fidler)
- 12-Sep-2012 – Changed yasnippet checking to be compatible with yasnippet 0.8’s function renaming. (Matthew L. Fidler)
- 21-Aug-2012 – Attempt to fix documentation with updated org-readme. (Matthew L. Fidler)
- 21-Aug-2012 – Added auto-indent-next-pair-timer-interval-max and a bug fix to the interval-growth algorithm. (Matthew L. Fidler)
- 21-Aug-2012 – Attempt to change documentation. (Matthew L. Fidler)
- 21-Aug-2012 – Changed the default auto-indent-next-pairt-timer-interval-do-not-grow to nil. (Matthew L. Fidler)
- 20-Aug-2012 – Drop Readme.md (Matthew L. Fidler)
- 20-Aug-2012 – Another documentation revision. (Matthew L. Fidler)
- 20-Aug-2012 – Documentation update. (Matthew L. Fidler)
- 20-Aug-2012 – Added a generic function to change the number of spaces for an indentation. Should fix issue #4. (Matthew L. Fidler)
- 20-Aug-2012 – Clarified documentation (Matthew L. Fidler)
- 20-Aug-2012 – Added some documentation about major mode indentation issues. 7-Aug-2012 Matthew L. Fidler Last-Updated: Sun Aug 5 12:36:11 2012 (-0500) #1411 (Matthew L. Fidler) Changed a mistake in the documentation; Autoindenting of balanced sexps are not supported by default but need to be enabled. (Matthew L. Fidler)
- 04-Aug-2012 – Added ability to turn off dynamic growth of timers per mode. The algorithm to change has not been perfected yet. (Matthew L. Fidler)
- 04-Aug-2012 – Fixed a bug introduced by cleaning typos. Changing again. (Matthew L. Idler)
- 03-Aug-2012 – Save indentation settings on exit emacs. (Matthew L. Fidler)
- 03-Aug-2012 – Fixed Documentation, and a few minor bugs caught by linting. (Matthew L. Fidler)
- 30-Jul-2012 – Made the Fix for issue #3 more specific to org tables. (Matthew L. Fidler)
- 30-Jul-2012 – Actual Fix for Issue #3. Now the delete character may not work in org-mode. (Matthew L. Fidler)
- 23-Jul-2012 – Fix Issue #3. Thanks harrylove for pointing it out. (Matthew L. Fidler)
- 02-Jul-2012 – Have an mode-based timer normalized to the number of lines used for next parenthetical indentation. (Matthew L. Fidler)
- 26-Jun-2012 – Bug fix for point-shift involved in auto-indent-after-yank-hook (Matthew L. Fidler)
- 13-Jun-2012 – Added auto-indent-after-yank-hook (Matthew L. Fidler)
- 18-May-2012 – Changed auto-indent-next-pair to be off by default. (Matthew L. Fidler)
- 13-Mar-2012 – Made timer for parenthetical statements customizable. (Matthew L. Fidler)
- 06-Mar-2012 – Speed enhancements for parenthetical statements. (Matthew L. Fidler)
- 05-Mar-2012 – Bug fix for autopair-backspace. (Matthew L. Fidler)
- 05-Mar-2012 – Have backspace cancel parenthetical alignment timer canceling (Matthew L. Fidler)
- 29-Feb-2012 – Bug fix for paren handling. (Matthew L. Fidler)
- 29-Feb-2012 – Made the handling of pairs a timer-based function so it doesn’t interfere with work flow. (Matthew L. Fidler)
- 29-Feb-2012 – Better handling of pairs. (Matthew L. Fidler)
- 28-Feb-2012 – Added subsequent-whole-line from Le Wang’s fork. (Matthew L. Fidler)
- 14-Feb-2012 – Fixing issue #2 (Matthew L. Fidler)
- 01-Feb-2012 – Added makefile-gmake-mode to the excluded auto-indent modes. (Matthew L. Fidler)
- 22-Dec-2011 – Added bug fix for home-key (Matthew L. Fidler)
- 21-Dec-2011 – Added another smart delete case. (Matthew L. Fidler)
- 14-Dec-2011 – Went back to last known working auto-indent-def-del-forward-char and deleted message. (Matthew L. Fidler)
- 14-Dec-2011 – Another Paren (Matthew L. Fidler)
- 14-Dec-2011 – Paren Bug Fix. (Matthew L. Fidler)
- 14-Dec-2011 – Changed the auto-indent-kill-remove-extra-spaces default to nil so that you copy-paste what you expect. (us041375)
- 10-Dec-2011 – Bug fix for annoying old debugging macros. (Matthew L. Fidler)
- 08-Dec-2011 – Added autoload cookie. (Matthew L. Fidler)
- 08-Dec-2011 – Bug fix for duplicate macros (Matthew L. Fidler)
- 08-Dec-2011 – Added (( and )) to the automatically delete extra whitespace at the end of a function list. (Matthew L. Fidler)
- 08-Dec-2011 – Added auto-indent-alternate-return-function-for-end-of-line-then-newline option (Matthew L. Fidler)
- 08-Dec-2011 – Added a possibility of adding a space if necessary. (Matthew L. Fidler)
- 08-Dec-2011 – Smarter delete end of line character enhancements. (Matthew L. Fidler)
- 08-Dec-2011 – Changed default options. (Matthew L. Fidler)
- 29-Nov-2011 – Bug Fix in auto-indent-mode-pre-command-hook (Matthew L. Fidler)
- 28-Nov-2011 – Bugfix for auto-indent-mode (Matthew L. Fidler)
- 21-Nov-2011 – Changed auto-indent-after-begin-or-finish-sexp to be called after every other hook has been run. That way autopair-mode should be indented correctly. (Matthew L. Fidler)
- 18-Nov-2011 – Added auto-indent-after-begin-or-finish-sexp (Matthew L. Fidler)
- 08-Apr-2011 – Bug fix for when Yasnippet is disabled. Now will work with it disabled or enabled. (MatthewL. Fidler)
- 08-Mar-2011 – Changed auto-indent-delete-line-char-remove-extra-spaces to nil by default. (Matthew L. Fidler)
- 16-Feb-2011 – Added a just one space function for pasting (Matthew L. Fidler)
- 15-Feb-2011 – Removed the deactivation of advices when this mode is turned off. I think it was causing some issues. (Matthew L. Fidler)
- 10-Feb-2011 – Added check to make sure not trying to paste on indent for auto-indent-disabled-modes-list (Matthew L. Fidler)
- 03-Feb-2011 – Swap backward-delete-char with backward-delete-char-untabify. Also use auto-indent-backward-delete-char-behavior when auto-indent-mode is active. (Matthew L. Fidler)
- 03-Feb-2011 – Added definition of cua-copy-region to advised functions (I thought it would have been taken care of with kill-ring-save) (Matthew L. Fidler)
- 03-Feb-2011 – Added option to delete indentation when copying or cutting regions using kill-region and kill-ring-save. Also changed auto-indent-kill-line-remove-extra-spaces to auto-indent-kill-remove-extra-spaces (Matthew L. Fidler)
- 03-Feb-2011 – Made sure that auto-indent-kill-line doesn’t use the kill-line advice. (Matthew L. Fidler)
- 03-Feb-2011 – (Matthew L. Fidler)
- 03-Feb-2011 – Another kill-line bug-fix. (Matthew L. Fidler)
- 03-Feb-2011 – Fixed the kill-line bug (Matthew L. Fidler)
- 03-Feb-2011 – yank engine bug fix. (Matthew L. Fidler)
- 03-Feb-2011 – Bug fix for determining if the function is a yank (Matthew L. Fidler)
- 02-Feb-2011 – Added kill-line bug-fix from Le Wang. Also there is a the bug of when called as a function, you need to check for disabled modes every time. (Matthew L. Fidler)
- 02-Feb-2011 – Added interactive requriment again. This time tried to back-guess if the key has been hijacked. If so assume it was called interactively. (Matthew L. Fidler)
- 01-Feb-2011 – Took out the interactive requirement again. Causes bugs like org-delete-char below. (Matthew L. Fidler)
- 01-Feb-2011 – Bug fix for org-delete-char (and possibly others). Allow delete-char to have auto-indent changed behavior when the command lookup is the same as the delete command (as well as if it is called interactively) (Matthew L. Fidler)
- 01-Feb-2011 – Added bugfix to kill-line advice and function (from Le Wang) (Matthew L. Fidler)
- 01-Feb-2011 – Added cua-paste and cua-paste-pop (Matthew L. Fidler)
- 01-Feb-2011 – Added auto-indent on move up and down with the arrow keys. (Matthew L. Fidler)
- 01-Feb-2011 – Added a keyboard engine that indents instead of using hooks and advices. (Matthew L. Fidler)
- 01-Feb-2011 – Removed the interactivity in the hooks. They are definitely not interactive. (Matthew L. Fidler)
- 01-Feb-2011 – Added Le Wang’s fixes:
- Many functions are checked for interactivity
- Kill-line prefix argument is fixed
- Kill region when region is active is controled by auto-indent-kill-line-kill-region-when-active
- Kill-line when at eol has more options
- Change auto-indent-indentation-function to auto-indent-newline-function (Matthew L. Fidler)
- 31-Jan-2011 – Removed indirect reference to shrink-whitespaces. Thanks Le Wang (Matthew L. Fidler)
- 31-Jan-2011 – Added explicit requirement for functions (Matthew L. Fidler)
- 18-Jan-2011 – Added support to turn on org-indent-mode when inside an org-file. (Matthew L. Fidler)
- 12-Jan-2011 – Added fix for ortbl-minor-mode. Now it will work when orgtbl-minor mode is enabled. (Matthew L. Fidler)
- 09-Dec-2010 – Bugfix. Now instead of indenting the region pasted, indent the region-pasted + beginning of line at region begin and end of line at region end. (Matthew L. Fidler) 02-Dec-2010 Matthew L. Fidler
Last-Updated: Thu Dec 2 13:02:02 2010 (-0600) #411 (Matthew L. Fidler) Made ignoring of modes with indent-relative and indent-relative-maybe apply to indenting returns as well.
- 02-Dec-2010 – Removed auto-indent on paste/yank for modes with indent-relative and indent-relative-maybe. This has annoyed me forever. (Matthew L. Fidler)
- 02-Dec-2010 – Added an advice to delete-char. When deleting a new-line character, shrink white-spaces afterward. (Matthew L. Fidler)
- 02-Dec-2010 – Speed enhancement by checking for yasnippets only on indentation. (Matthew L. Fidler)
- 29-Nov-2010 – Bug fix to allow authotkey files to save. (Matthew L. Fidler)
- 29-Nov-2010 – Change auto-indent-on-save to be disabled by default. (Matthew L. Fidler)
- 22-Nov-2010 – Yasnippet bug-fix. (Matthew L. Fidler)
- 22-Nov-2010 – auto-indent bug fix for save on save buffer hooks. (Matthew L. Fidler)
- 16-Nov-2010 – Added conf-windows-mode to ignored modes. (Matthew L. Fidler)
- 15-Nov-2010 – Bugfix for deletion of whitespace (Matthew L. Fidler)
- 15-Nov-2010 – Bugfix for post-command-hook. (Matthew L. Fidler)
- 15-Nov-2010 – Added diff-mode to excluded modes for auto-indentaion. (Matthew L. Fidler)
- 15-Nov-2010 – Added fundamental mode to excluded modes for auto-indentation. (Matthew L. Fidler)
- 13-Nov-2010 – Bug fix try #3 (Matthew L. Fidler)
- 13-Nov-2010 – Anothe bug-fix for yasnippet. (Matthew L. Fidler)
- 13-Nov-2010 – Bug fix for auto-indent-mode. Now it checks to make sure that last-command-event is non-nil. (Matthew L. Fidler)
- 11-Nov-2010 – Put back processes in. Made the return key handled by pre and post-command-hooks. (Matthew L. Fidler)
- 11-Nov-2010 – Took out processes such as R or eshell (Matthew L. Fidler)
- 09-Nov-2010 – Bug fix when interacting with the SVN version of yasnippet. It will not perform the line indentation when Yasnippet is running. (Matthew L. Fidler)
- 09-Nov-2010 – Made sure that the auto-paste indentation doesn’t work in minibuffer. (Matthew L. Fidler)
- 09-Nov-2010 – When auto-indent-pre-command-hook is inactivated by some means, add it back. (Matthew L. Fidler)
- 09-Nov-2010 – Added snippet-mode to excluded modes. Also turned off the kill-line by default. (Matthew L. Fidler)
- 07-Nov-2010 – Added the possibility of TextMate type returns. (Matthew L. Fidler)
- 07-Nov-2010 – Bug fix where backspace on indented region stopped working.Added TextMate (Matthew L. Fidler)
- 07-Nov-2010 – Another small bug fix. (Matthew L. Fidler)
- 07-Nov-2010 – Added bugfix and also allow movement on blank lines to be automatically indented to the correct position. (Matthew L. Fidler)
- 06-Nov-2010 – Initial release. (Matthew L. Fidler)
This was generated with OrgReadme. On updating the library, this page is likely to be replaced with updated content.