-
Notifications
You must be signed in to change notification settings - Fork 199
/
Emacs.hs
282 lines (251 loc) · 12.7 KB
/
Emacs.hs
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UnicodeSyntax #-}
{-# OPTIONS_HADDOCK show-extensions #-}
-- |
-- Module : Yi.Keymap.Emacs
-- License : GPL-2
-- Maintainer : yi-devel@googlegroups.com
-- Stability : experimental
-- Portability : portable
--
-- This module aims at a mode that should be (mostly) intuitive to
-- emacs users, but mapping things into the Yi world when convenient.
-- Hence, do not go into the trouble of trying 100% emulation. For
-- example, @M-x@ gives access to Yi (Haskell) functions, with their
-- native names.
module Yi.Keymap.Emacs ( keymap
, mkKeymap
, defKeymap
, ModeMap(..)
, eKeymap
, completionCaseSensitive
) where
import Control.Applicative
import Control.Lens
import Control.Monad
import Data.Char
import Data.Maybe
import Data.Prototype
import Data.Text ()
import Yi.Buffer
import Yi.Command (shellCommandE)
import Yi.Core (closeWindow, runAction, suspendEditor, userForceRefresh, withSyntax)
import Yi.Dired
import Yi.Editor
import Yi.File
import Yi.Keymap
import Yi.Keymap.Emacs.KillRing
import Yi.Keymap.Emacs.Utils
(askQuitEditor, evalRegionE, executeExtendedCommandE, findFile,
findFileNewTab, promptFile, insertNextC, isearchKeymap, killBufferE,
queryReplaceE, readUniversalArg, scrollDownE, scrollUpE, switchBufferE,
askSaveEditor, argToInt, promptTag, justOneSep, joinLinesE, countWordsRegion,
findFileReadOnly)
import Yi.Keymap.Keys
import Yi.MiniBuffer
import Yi.Misc (adjBlock, adjIndent, selectAll, placeMark)
import Yi.Mode.Buffers ( listBuffers )
import Yi.Rectangle
import Yi.Search (isearchFinishWithE, resetRegexE)
import Yi.TextCompletion
data ModeMap = ModeMap { _eKeymap :: Keymap
, _completionCaseSensitive :: Bool
}
$(makeLenses ''ModeMap)
keymap :: KeymapSet
keymap = mkKeymap defKeymap
mkKeymap :: Proto ModeMap -> KeymapSet
mkKeymap = modelessKeymapSet . _eKeymap . extractValue
defKeymap :: Proto ModeMap
defKeymap = Proto template
where
template self = ModeMap { _eKeymap = emacsKeymap
, _completionCaseSensitive = False }
where
emacsKeymap :: Keymap
emacsKeymap = selfInsertKeymap Nothing isDigit <|> completionKm (_completionCaseSensitive self) <|>
do univArg <- readUniversalArg
selfInsertKeymap univArg (not . isDigit) <|> emacsKeys univArg
selfInsertKeymap :: Maybe Int -> (Char -> Bool) -> Keymap
selfInsertKeymap univArg condition = do
c <- printableChar
unless (condition c) empty
let n = argToInt univArg
write (adjBlock n >> replicateM_ n (insertB c))
completionKm :: Bool -> Keymap
completionKm caseSensitive = do void $ some (meta (char '/') ?>>! wordComplete' caseSensitive)
deprioritize
write resetComplete
-- 'adjustPriority' is there to lift the ambiguity between "continuing" completion
-- and resetting it (restarting at the 1st completion).
deleteB' :: BufferM ()
deleteB' = adjBlock (-1) >> deleteN 1
-- | Wrapper around 'moveE' which also cancels incremental search. See
-- issue #499 for details.
moveE ∷ TextUnit → Direction → EditorM ()
moveE u d = isearchFinishWithE resetRegexE >> withCurrentBuffer (moveB u d)
emacsKeys :: Maybe Int -> Keymap
emacsKeys univArg =
choice [ -- First all the special key bindings
spec KTab ?>>! adjIndent IncreaseCycle
, shift (spec KTab) ?>>! adjIndent DecreaseCycle
, spec KEnter ?>>! repeatingArg newlineB
, spec KDel ?>>! repeatingArg (blockKillring >> deleteB')
, spec KBS ?>>! repeatingArg (blockKillring >>
adjBlock (-1) >>
bdeleteB)
, spec KHome ?>>! repeatingArg moveToSol
, spec KEnd ?>>! repeatingArg moveToEol
, spec KLeft ?>>! repeatingArg leftB
, spec KRight ?>>! repeatingArg rightB
, spec KUp ?>>! repeatingArg $ moveE VLine Backward
, spec KDown ?>>! repeatingArg $ moveE VLine Forward
, spec KPageDown ?>>! repeatingArg downScreenB
, spec KPageUp ?>>! repeatingArg upScreenB
, shift (spec KUp) ?>>! repeatingArg (scrollB (-1))
, shift (spec KDown) ?>>! repeatingArg (scrollB 1)
-- All the keybindings of the form 'Ctrl + special key'
, ctrl (spec KLeft) ?>>! repeatingArg prevWordB
, ctrl (spec KRight) ?>>! repeatingArg nextWordB
, ctrl (spec KHome) ?>>! repeatingArg topB
, ctrl (spec KEnd) ?>>! repeatingArg botB
, ctrl (spec KUp) ?>>! repeatingArg (prevNParagraphs 1)
, ctrl (spec KDown) ?>>! repeatingArg (nextNParagraphs 1)
-- All the keybindings of the form "C-c" where 'c' is some character
, ctrlCh '@' ?>>! placeMark
, ctrlCh ' ' ?>>! placeMark
, ctrlCh '/' ?>>! repeatingArg undoB
, ctrlCh '_' ?>>! repeatingArg undoB
, ctrlCh 'a' ?>>! repeatingArg (maybeMoveB Line Backward)
, ctrlCh 'b' ?>>! repeatingArg leftB
, ctrlCh 'd' ?>>! repeatingArg (blockKillring >> deleteB')
, ctrlCh 'e' ?>>! repeatingArg (maybeMoveB Line Forward)
, ctrlCh 'f' ?>>! repeatingArg rightB
, ctrlCh 'g' ?>>! setVisibleSelection False
, ctrlCh 'h' ?>> char 'b' ?>>! acceptedInputsOtherWindow
, ctrlCh 'i' ?>>! adjIndent IncreaseOnly
, ctrlCh 'j' ?>>! newlineAndIndentB
, ctrlCh 'k' ?>>! killLineE univArg
, ctrlCh 'l' ?>>! (withCurrentBuffer scrollToCursorB >> userForceRefresh)
, ctrlCh 'm' ?>>! repeatingArg (insertB '\n')
, ctrlCh 'n' ?>>! repeatingArg (moveE VLine Forward)
, ctrlCh 'o' ?>>! repeatingArg (insertB '\n' >> leftB)
, ctrlCh 'p' ?>>! repeatingArg (moveE VLine Backward)
, ctrlCh 'q' ?>> insertNextC univArg
, ctrlCh 'r' ?>> isearchKeymap Backward
, ctrlCh 's' ?>> isearchKeymap Forward
, ctrlCh 't' ?>>! repeatingArg swapB
, ctrlCh 'v' ?>>! scrollDownE univArg
, ctrlCh 'w' ?>>! killRegion
, ctrlCh 'y' ?>>! yankE
, ctrlCh 'z' ?>>! suspendEditor
, ctrlCh '+' ?>>! repeatingArg (increaseFontSize 1)
, ctrlCh '-' ?>>! repeatingArg (decreaseFontSize 1)
-- All the keybindings of the form "C-M-c" where 'c' is some character
, ctrl (metaCh 'w') ?>>! appendNextKillE
, ctrl (metaCh ' ') ?>>! layoutManagersNextE
, ctrl (metaCh ',') ?>>! layoutManagerNextVariantE
, ctrl (metaCh '.') ?>>! layoutManagerPreviousVariantE
, ctrl (metaCh 'j') ?>>! nextWinE
, ctrl (metaCh 'k') ?>>! prevWinE
, ctrl (meta $ spec KEnter) ?>>! swapWinWithFirstE
-- All the keybindings of the form "S-C-M-c" where 'c' is some key
, shift (ctrl $ metaCh 'j') ?>>! moveWinNextE
, shift (ctrl $ metaCh 'k') ?>>! moveWinPrevE
, shift (ctrl $ meta $ spec KEnter) ?>>! pushWinToFirstE
, Event (KASCII ' ') [MShift,MCtrl,MMeta] ?>>! layoutManagersPreviousE
-- All the key-bindings which are preceded by a 'C-x'
, ctrlCh 'x' ?>> ctrlX
, ctrlCh 'c' ?>> ctrlC
-- All The key-bindings of the form M-c where 'c' is some character.
, metaCh ' ' ?>>! justOneSep univArg
, metaCh 'v' ?>>! scrollUpE univArg
, metaCh '!' ?>>! shellCommandE
, metaCh '<' ?>>! repeatingArg topB
, metaCh '>' ?>>! repeatingArg botB
, metaCh '%' ?>>! queryReplaceE
, metaCh '^' ?>>! joinLinesE univArg
, metaCh ';' ?>>! commentRegion
, metaCh 'a' ?>>! repeatingArg (moveE unitSentence Backward)
, metaCh 'b' ?>>! repeatingArg prevWordB
, metaCh 'c' ?>>! repeatingArg capitaliseWordB
, metaCh 'd' ?>>! repeatingArg killWordB
, metaCh 'e' ?>>! repeatingArg (moveE unitSentence Forward)
, metaCh 'f' ?>>! repeatingArg nextWordB
, metaCh 'h' ?>>! (setSelectRegionB =<< regionOfB unitParagraph)
, metaCh 'k' ?>>! repeatingArg (deleteB unitSentence Forward)
, metaCh 'l' ?>>! repeatingArg lowercaseWordB
, metaCh 'm' ?>>! firstNonSpaceB
, metaCh 'q' ?>>! withSyntax modePrettify
, metaCh 'u' ?>>! repeatingArg uppercaseWordB
, metaCh 't' ?>>! repeatingArg (transposeB unitWord Forward)
, metaCh 'w' ?>>! killRingSaveE
, metaCh 'x' ?>>! executeExtendedCommandE
, metaCh 'y' ?>>! yankPopE
, metaCh '.' ?>>! promptTag
, metaCh '{' ?>>! repeatingArg (prevNParagraphs 1)
, metaCh '}' ?>>! repeatingArg (nextNParagraphs 1)
, metaCh '=' ?>>! countWordsRegion
, metaCh '\\' ?>>! deleteHorizontalSpaceB univArg
-- Other meta key-bindings
, meta (spec KBS) ?>>! repeatingArg bkillWordB
, metaCh 'g' ?>>
optMod meta (char 'g') >>! (gotoLn . fromDoc :: Int ::: LineNumber -> BufferM Int)
]
where
-- inserting the empty string prevents the deletion from appearing in the killring
-- which is a good thing when we are deleting individuals characters. See
-- http://code.google.com/p/yi-editor/issues/detail?id=212
blockKillring = insertN ""
withUnivArg :: YiAction (m ()) () => (Maybe Int -> m ()) -> YiM ()
withUnivArg cmd = runAction $ makeAction (cmd univArg)
repeatingArg :: (Monad m, YiAction (m ()) ()) => m () -> YiM ()
repeatingArg f = withIntArg $ \n -> replicateM_ n f
withIntArg :: YiAction (m ()) () => (Int -> m ()) -> YiM ()
withIntArg cmd = withUnivArg $ \arg -> cmd (fromMaybe 1 arg)
ctrlC = choice [ ctrlCh 'c' ?>>! commentRegion ]
rectangleFunctions = choice [ char 'a' ?>>! alignRegionOn
, char 'o' ?>>! openRectangle
, char 't' ?>>! stringRectangle
, char 'k' ?>>! killRectangle
, char 'y' ?>>! yankRectangle
]
tabFunctions :: Keymap
tabFunctions = choice [ optMod ctrl (char 'n') >>! nextTabE
, optMod ctrl (char 'p') >>! previousTabE
, optMod ctrl (char 't') >>! newTabE
, optMod ctrl (char 'e') >>! findFileNewTab
, optMod ctrl (char 'd') >>! deleteTabE
, charOf id '0' '9' >>=! moveTabE . Just . digitToInt
]
-- These keybindings are all preceded by a 'C-x' so for example to
-- quit the editor we do a 'C-x C-c'
ctrlX = choice [ ctrlCh 'o' ?>>! deleteBlankLinesB
, char '0' ?>>! closeWindow
, char '1' ?>>! closeOtherE
, char '2' ?>>! splitE
, char 'h' ?>>! selectAll
, char 's' ?>>! askSaveEditor
, ctrlCh 'b' ?>>! listBuffers
, ctrlCh 'c' ?>>! askQuitEditor
, ctrlCh 'f' ?>>! findFile
, ctrlCh 'r' ?>>! findFileReadOnly
, ctrlCh 'q' ?>>!
((withCurrentBuffer (readOnlyA %= not)) :: EditorM ())
, ctrlCh 's' ?>>! fwriteE
, ctrlCh 'w' ?>>! promptFile "Write file:" fwriteToE
, ctrlCh 'x' ?>>! (exchangePointAndMarkB >>
assign highlightSelectionA True)
, char 'b' ?>>! switchBufferE
, char 'd' ?>>! dired
, char 'e' ?>>
char 'e' ?>>! evalRegionE
, char 'o' ?>>! nextWinE
, char 'k' ?>>! killBufferE
, char 'r' ?>> rectangleFunctions
, char 'u' ?>>! repeatingArg undoB
, optMod ctrl (char 't') >> tabFunctions
]