summaryrefslogtreecommitdiff
path: root/emacs/shift-text.el
blob: df989a17cc5bbee42df4176d2186e0a717c021f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
;;; shift-text.el --- Move the region in 4 directions, in a way similar to Eclipse's
;;; Version: 0.3
;;; Author: sabof
;;; URL: https://github.com/sabof/shift-text
;;; Package-Requires: ((cl-lib "1.0") (es-lib "0.3"))

;;; Commentary:

;; The project is hosted at https://github.com/sabof/shift-text
;; The latest version, and all the relevant information can be found there.

;;; License:

;; This file is NOT part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program ; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Code:

(require 'cl-lib)
(require 'es-lib)

(defvar st-indent-step
  (lambda ()
    (cond
      ( (eq major-mode 'js-mode)
        js-indent-level)
      ( (eq major-mode 'css-mode)
        css-indent-offset)
      ( (memq major-mode
              '(emacs-lisp-mode
                lisp-mode
                lisp-interaction-mode
                scheme-mode))
        2)
      ( t tab-width)
      ))
  "How much to indent when shifting horizontally.

You can set it for specific modes in their mode-hooks with `setq-local'.
For example \(setq-local st-indent-step 2\).

Can also be a function called without arguments and evaluting to a number.")

(defun st--section-marking-end-of-line (&optional pos using-region)
  (save-excursion
    (when pos
      (goto-char pos))
    (if (and using-region (equal (current-column) 0))
        (point)
      (min (point-max) (1+ (es-total-line-end-position))))))

(defun st--normalize-pos (pos)
  (min (point-max) (max (point-min) pos)))

(defun st--shift-text-internal (arg)
  (let* (( was-active (use-region-p))
         ( first-line-was-folded
           (save-excursion
             (when was-active
               (goto-char (region-beginning)))
             (es-line-folded-p)))
         ( initial-column (current-column))
         ( start (es-total-line-beginning-position
                  (if was-active
                      (region-beginning)
                      (point))))
         ( end (st--section-marking-end-of-line
                (if was-active
                    (region-end)
                    (point))
                was-active))
         ( virtual-overlays
           (mapcar 'es-preserve-overlay (overlays-in start end)))
         ( text (delete-and-extract-region start end))
         new-start
         difference)
    (es-total-forward-line arg)
    (unless (zerop (current-column))
      (insert ?\n ))
    (setq new-start (point)
          difference (- new-start start))
    (insert text)
    (unless (equal (aref text (1- (length text)))
                   ?\n )
      (insert ?\n ))
    (cl-dolist (ov virtual-overlays)
      (setf (nth 1 ov) (st--normalize-pos (+ (nth 1 ov) difference)))
      (setf (nth 2 ov) (st--normalize-pos (+ (nth 2 ov) difference))))
    (mapc 'es-restore-overlay virtual-overlays)
    (set-mark new-start)
    (exchange-point-and-mark)
    (if (or was-active first-line-was-folded)
        (setq deactivate-mark nil
              cua--explicit-region-start nil)
      (progn (move-to-column initial-column t)
             (deactivate-mark)))))

(defun st--indent-rigidly-internal (arg)
  (let* (( indent-step (if (functionp st-indent-step)
                           (funcall st-indent-step)
                         st-indent-step))
         ( indentation-ammout
           (* arg indent-step))
         ( old-indentation
           (save-excursion
             (when (use-region-p)
               (goto-char (region-beginning)))
             (current-indentation)))
         ( old-indentation-adujusted
           (* indent-step
              (/ old-indentation
                 indent-step)))
         ( desired-indentation
           (+ old-indentation-adujusted
              indentation-ammout))
         ( new-ammount
           (- desired-indentation old-indentation)))
    (cond ( (use-region-p)
            (let (( start
                    (es-total-line-beginning-position
                     (region-beginning)))
                  ( end
                    (st--section-marking-end-of-line
                     (region-end)
                     t)))
              (set-mark end)
              (goto-char start)
              (indent-rigidly start end new-ammount)
              (setq deactivate-mark nil)))
          ( (es-line-empty-p)
            (if (> desired-indentation old-indentation)
                (indent-to desired-indentation)
              (goto-char (max (line-beginning-position)
                              (+ desired-indentation
                                 (line-beginning-position))))
              (delete-region (point) (line-end-position))
              ))
          ( t (indent-rigidly
               (es-total-line-beginning-position (point))
               (st--section-marking-end-of-line (point))
               new-ammount)))))

;;;###autoload
(defun shift-text-down ()
  "Move region or the current line down."
  (interactive)
  (st--shift-text-internal 1))

;;;###autoload
(defun shift-text-up ()
  "Move region or the current line up."
  (interactive)
  (st--shift-text-internal -1))

;;;###autoload
(defun shift-text-left ()
  "Move region or the current line left."
  (interactive)
  (st--indent-rigidly-internal -1))

;;;###autoload
(defun shift-text-right ()
  "Move region or the current line right."
  (interactive)
  (st--indent-rigidly-internal 1))

(provide 'shift-text)
;;; shift-text.el ends here