Created
August 4, 2017 14:36
-
-
Save r2evans/d723956ab62823b2e3bed75dd7ef46d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; my-align.el | |
;; Copyright (c) 2017 Bill Evans (r2evans) | |
;; | |
;; Author: Bill Evans | |
;; Created: 2017 Aug 03 | |
;; Keywords: code align programming | |
;;; Commentary: | |
;; Simple wrapper wround 'align-regexp' with sane defaults. | |
;; Based on code first seen here | |
;; https://stackoverflow.com/questions/3633120/emacs-hotkey-to-align-equal-signs | |
;; then extended to allow secondary alignment strings. | |
;; My preference is to assign it to `C-c =`, with | |
;; (global-set-key (kbd "C-c =") 'align-to-equals) | |
;;; Usage: | |
;; - (default, no prefix) aligns on '=' | |
;; e.g., from R, starting with | |
;; | |
;; list( | |
;; short = 1000 - 1, | |
;; longvariablename = 3 + 1415, | |
;; another = 1 - 0 | |
;; ) | |
;; | |
;; `C-c =`: | |
;; | |
;; list( | |
;; short = 1000 - 1, | |
;; longvariablename = 3 + 1415, | |
;; another = 1 - 0 | |
;; ) | |
;; | |
;; - single prefix (C-u), user-defined alignment string | |
;; | |
;; `C-u C-c =` then `-`: | |
;; | |
;; list( | |
;; short = 1000 - 1, | |
;; longvariablename = 3 + 1415, | |
;; another = 1 - 0 | |
;; ) | |
;; | |
;; Note: user strings can be valid regexps, so a single period | |
;; (e.g.) will need to be escaped as '\.' Instead of a simple '-', | |
;; one could align on different characters with: | |
;; | |
;; `C-u C-c =` then `[-+]` or | |
;; `C-u C-c =` then `\(-\|+\|_\|:::\)`: | |
;; | |
;; list( | |
;; short = 1000 - 1, | |
;; longvariablename = 3 + 1415, | |
;; another = 1 - 0 | |
;; ) | |
;;; Code: | |
(defun align-to-equals (prefix) | |
"Align region to equal signs" | |
(interactive "p") | |
(let ((ch (if (= prefix 1) | |
"=" | |
(read-from-minibuffer "Indent on string: "))) | |
(begin (region-beginning)) | |
(end (region-end)) | |
) | |
(message (concat "Indenting on: '" ch "'")) | |
(align-regexp begin end (concat "\\(\\s-*\\)" ch) 1 1 )) | |
) | |
;; works in R and python, likely others, so keep it global | |
(global-set-key (kbd "C-c =") 'align-to-equals) | |
;;; my-align.el ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment