comparison .emacs.d/haskell-mode/haskell-move-nested.el @ 0:2764b4f45f9f

1st commit
author Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
date Mon, 21 Apr 2014 04:30:59 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:2764b4f45f9f
1 ;;; haskell-move-nested.el --- Change the column of text nested below a line
2
3 ;; Copyright (C) 2010 Chris Done
4
5 ;; Author: Chris Done <chrisdone@gmail.com>
6
7 ;; This file is not part of GNU Emacs.
8
9 ;; This program is free software: you can redistribute it and/or
10 ;; modify it under the terms of the GNU General Public License as
11 ;; published by the Free Software Foundation, either version 3 of
12 ;; the License, or (at your option) any later version.
13
14 ;; This program is distributed in the hope that it will be
15 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
16 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17 ;; PURPOSE. See the GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public
20 ;; License along with this program. If not, see
21 ;; <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; This module is intended for Haskell mode users, but is
26 ;; independent of Haskell mode.
27
28 ;; Example usage:
29
30 ;; (define-key haskell-mode-map (kbd "C-,") 'haskell-move-nested-left)
31 ;; (define-key haskell-mode-map (kbd "C-.") 'haskell-move-nested-right)
32
33 ;;; Code:
34
35 ;;;###autoload
36 (defun haskell-move-nested (cols)
37 "Shift the nested off-side-rule block adjacent to point by COLS columns to the right.
38
39 In Transient Mark mode, if the mark is active, operate on the contents
40 of the region instead.
41 "
42 (save-excursion
43 (if (and transient-mark-mode mark-active)
44 (progn
45 (indent-rigidly (region-beginning) (region-end) cols)
46 (setq deactivate-mark nil))
47 (let ((region (haskell-move-nested-region)))
48 (when region
49 (indent-rigidly (car region) (cdr region) cols))))))
50
51 ;;;###autoload
52 (defun haskell-move-nested-right (cols)
53 "Increase indentation of the following off-side-rule block adjacent to point.
54
55 Use a numeric prefix argument to indicate amount of indentation to apply.
56
57 In Transient Mark mode, if the mark is active, operate on the contents
58 of the region instead."
59 (interactive "p")
60 (haskell-move-nested cols)
61 )
62
63 ;;;###autoload
64 (defun haskell-move-nested-left (cols)
65 "Decrease indentation of the following off-side-rule block adjacent to point.
66
67 Use a numeric prefix argument to indicate amount of indentation to apply.
68
69 In Transient Mark mode, if the mark is active, operate on the contents
70 of the region instead."
71 (interactive "p")
72 (haskell-move-nested (- cols))
73 )
74
75 (defun haskell-move-nested-region ()
76 "Infer region off-side-rule block adjacent to point.
77 Used by `haskell-move-nested'.
78 "
79 (save-excursion
80 (let ((starting-level (current-column)))
81 (forward-line)
82 (let ((current-level (haskell-move-nested-indent-level)))
83 (let ((start-point (line-beginning-position))
84 (start-end-point (line-end-position))
85 (end-point nil)
86 (last-line 0))
87 (forward-line)
88 (while (and (not (= (line-beginning-position) last-line))
89 (or (> (haskell-move-nested-indent-level) starting-level)
90 (and (> current-level starting-level)
91 (>= (haskell-move-nested-indent-level) current-level))))
92 (setq last-line (line-beginning-position))
93 (setq end-point (line-end-position))
94 (forward-line))
95 (cons start-point (or end-point
96 start-end-point)))))))
97
98 (defun haskell-move-nested-indent-level ()
99 (max
100 0
101 (1- (length
102 (buffer-substring-no-properties
103 (line-beginning-position)
104 (or (save-excursion (goto-char (line-beginning-position))
105 (search-forward-regexp "[^ ]" (line-end-position) t 1))
106 (line-beginning-position)))))))
107
108 (defun haskell-kill-nested ()
109 "Kill the nested region after point."
110 (interactive)
111 (let ((start (point))
112 (reg (save-excursion
113 (search-backward-regexp "^[ ]+" (line-beginning-position) t 1)
114 (search-forward-regexp "[^ ]" (line-end-position) t 1)
115 (haskell-move-nested-region))))
116 (kill-region start (cdr reg))))
117
118 (defun haskell-delete-nested ()
119 "Kill the nested region after point."
120 (interactive)
121 (let ((start (point))
122 (reg (save-excursion
123 (search-backward-regexp "^[ ]+" (line-beginning-position) t 1)
124 (search-forward-regexp "[^ ]" (line-end-position) t 1)
125 (haskell-move-nested-region))))
126 (delete-region start (cdr reg))))
127
128 (provide 'haskell-move-nested)
129
130 ;;; haskell-move-nested.el ends here