Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 162 lines (140 sloc) 5.45 kB
b91f455 Add the current version of the Moss/Thorn/Marlow Emacs mode, along wi…
rrt authored
1 ;;; haskell-simple-indent.el --- Simple indentation module for Haskell Mode
2
3 ;; Copyright 1998 Heribert Schuetz, Graeme E Moss
4
5 ;; Authors:
6 ;; 1998 Heribert Schuetz <Heribert.Schuetz@informatik.uni-muenchen.de> and
7 ;; Graeme E Moss <gem@cs.york.ac.uk>
8 ;; Keywords: indentation files Haskell
9 ;; Version: 1.0
10 ;; URL: http://www.cs.york.ac.uk/~gem/haskell-mode/simple-indent.html
11
12 ;;; This file is not part of GNU Emacs.
13
14 ;; This file is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; This file is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29
30 ;;; Commentary:
31
32 ;; Purpose:
33 ;;
34 ;; To support simple indentation of Haskell scripts.
35 ;;
36 ;;
37 ;; Installation:
38 ;;
39 ;; To bind TAB to the indentation command for all Haskell buffers, add
40 ;; this to .emacs:
41 ;;
42 ;; (add-hook 'haskell-mode-hook 'turn-on-haskell-simple-indent)
43 ;;
44 ;; Otherwise, call `turn-on-haskell-simple-indent'.
45 ;;
46 ;;
47 ;; Customisation:
48 ;;
49 ;; None supported.
50 ;;
51 ;;
52 ;; History:
53 ;;
54 ;; If you have any problems or suggestions, after consulting the list
55 ;; below, email gem@cs.york.ac.uk quoting the version of you are
56 ;; using, the version of emacs you are using, and a small example of
57 ;; the problem or suggestion.
58 ;;
59 ;; Version 1.0:
60 ;; Brought over from Haskell mode v1.1.
61 ;;
62 ;; Present Limitations/Future Work (contributions are most welcome!):
63 ;;
64 ;; (None so far.)
65
66 ;;; All functions/variables start with
67 ;;; `(turn-(on/off)-)haskell-simple-indent'.
68
69 ;; Version.
70 (defconst haskell-simple-indent-version "1.1"
eb13e33 Tweaks to the doc strings and support for customization, from
simonmar authored
71 "`haskell-simple-indent' version number.")
b91f455 Add the current version of the Moss/Thorn/Marlow Emacs mode, along wi…
rrt authored
72 (defun haskell-simple-indent-version ()
eb13e33 Tweaks to the doc strings and support for customization, from
simonmar authored
73 "Echo the current version of `haskell-simple-indent' in the minibuffer."
b91f455 Add the current version of the Moss/Thorn/Marlow Emacs mode, along wi…
rrt authored
74 (interactive)
75 (message "Using haskell-simple-indent version %s"
76 haskell-simple-indent-version))
77
78 ;; Partly stolen from `indent-relative' in indent.el:
79 (defun haskell-simple-indent ()
80 "Space out to under next visible indent point.
81 Indent points are positions of non-whitespace following whitespace in
82 lines preceeding point. A position is visible if it is to the left of
83 the first non-whitespace of every nonblank line between the position and
84 the current line. If there is no visible indent point beyond the current
85 column, `tab-to-tab-stop' is done instead."
86 (interactive)
87 (let ((start-column (current-column))
88 (invisible-from nil) ; `nil' means infinity here
89 indent )
90 (catch 'haskell-simple-indent-break
91 (save-excursion
92 (while (progn (beginning-of-line)
93 (> (point) (point-min)))
94 (forward-line -1)
95 (if (not (looking-at "[ \t]*\n"))
96 (let ((this-indentation (current-indentation)))
97 (if (or (not invisible-from)
98 (< this-indentation invisible-from))
99 (if (> this-indentation start-column)
100 (setq invisible-from this-indentation)
101 (let ((end (save-excursion (forward-line 1) (point))))
102 (move-to-column start-column)
103 ;; Is start-column inside a tab on this line?
104 (if (> (current-column) start-column)
105 (backward-char 1))
106 (or (looking-at "[ \t]")
107 (skip-chars-forward "^ \t" end))
108 (skip-chars-forward " \t" end)
109 (let ((col (current-column)))
110 (setq indent
111 (if (or (= (point) end)
112 (and invisible-from
113 (> col invisible-from)))
114 invisible-from
115 col ) ) )
116 (throw 'haskell-simple-indent-break nil)))))))))
117 (if indent
118 (let ((opoint (point-marker)))
119 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
120 (indent-to indent 0)
121 (if (> opoint (point))
122 (goto-char opoint))
123 (move-marker opoint nil))
124 (tab-to-tab-stop)) ) )
125
126 ;; The main functions.
127 (defun turn-on-haskell-simple-indent ()
128 "Sets `indent-line-function' to `haskell-simple-indent'---a simple
129 indentation function. Ensures TAB is bound to
130 `indent-for-tab-command', and LFD to `newline-and-indent'. TAB will
131 now move the cursor to the next indent point in the previous nonblank
132 line. An indent point is a non-whitespace character following
133 whitespace.
134
135 Invokes `haskell-simple-indent-hook' if not nil.
136
137 Use `haskell-simple-indent-version' to find out what version this is."
138
139 (interactive)
140 (make-local-variable 'haskell-simple-indent-old)
141 (setq haskell-simple-indent-old indent-line-function)
142 (make-local-variable 'indent-line-function)
143 (setq indent-line-function 'haskell-simple-indent)
144 (local-set-key "\t" 'indent-for-tab-command)
145 (local-set-key "\n" 'newline-and-indent)
146 (run-hooks 'haskell-simple-indent-hook))
147
148 (defun turn-off-haskell-simple-indent ()
149 "Returns `indent-line-function' to original value, before
150 `turn-on-haskell-simple-indent' was called. Leaves TAB and LFD bound
151 to `indent-for-tab-command' and `newline-and-indent' respectively."
152
153 (interactive)
154 (if (boundp 'haskell-simple-indent-old)
155 (setq indent-line-function haskell-simple-indent-old)))
156
157 ;;; Provide ourselves:
158
159 (provide 'haskell-simple-indent)
160
161 ;;; haskell-simple-indent ends here.
Something went wrong with that request. Please try again.