TiddlyWiki5/plugins/tiddlywiki/codemirror-keymap-emacs/files/keymap/emacs.js

4 wiersze
7.3 KiB
JavaScript
Executable File

// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: https://codemirror.net/LICENSE
!function(t){"object"==typeof exports&&"object"==typeof module?t(require("../lib/codemirror")):"function"==typeof define&&define.amd?define(["../lib/codemirror"],t):t(CodeMirror)}(function(t){"use strict";var c=t.Pos;function u(t,e){return t.line==e.line&&t.ch==e.ch}var i=[];function l(t){i.push(t),50<i.length&&i.shift()}function n(t){return i[i.length-(t?Math.min(t,1):1)]||""}var a=null;function f(t,e,n,r,o){null==o&&(o=t.getRange(e,n)),"grow"==r&&a&&a.cm==t&&u(e,a.pos)&&t.isClean(a.gen)?function(t){if(!i.length)return l(t);i[i.length-1]+=t}(o):!1!==r&&l(o),t.replaceRange("",e,n,"+delete"),a="grow"==r?{cm:t,pos:e,gen:t.changeGeneration()}:null}function e(t,e,n){return t.findPosH(e,n,"char",!0)}function r(t,e,n){return t.findPosH(e,n,"word",!0)}function o(t,e,n){return t.findPosV(e,n,"line",t.doc.sel.goalColumn)}function s(t,e,n){return t.findPosV(e,n,"page",t.doc.sel.goalColumn)}function g(t,e,n){for(var r=e.line,o=t.getLine(r),i=/\S/.test(n<0?o.slice(0,e.ch):o.slice(e.ch)),l=t.firstLine(),a=t.lastLine();;){if((r+=n)<l||a<r)return t.clipPos(c(r-n,n<0?0:null));o=t.getLine(r);if(/\S/.test(o))i=!0;else if(i)return c(r,0)}}function C(t,e,n){for(var r=e.line,o=e.ch,i=t.getLine(e.line),l=!1;;){var a=i.charAt(o+(n<0?-1:0));if(a){if(l&&/[!?.]/.test(a))return c(r,o+(0<n?1:0));l=l||/\w/.test(a),o+=n}else{if(r==(n<0?t.firstLine():t.lastLine()))return c(r,o);if(i=t.getLine(r+n),!/\S/.test(i))return c(r,o);r+=n,o=n<0?i.length:0}}}function d(t,e,n){var r;if(t.findMatchingBracket&&(r=t.findMatchingBracket(e,{strict:!0}))&&r.match&&(r.forward?1:-1)==n)return 0<n?c(r.to.line,r.to.ch+1):r.to;for(var o=!0;;o=!1){var i=t.getTokenAt(e),l=c(e.line,n<0?i.start:i.end);if(!(o&&0<n&&i.end==e.ch)&&/\w/.test(i.string))return l;var a=t.findPosH(l,n,"char");if(u(l,a))return e;e=a}}function p(t,e){var n=t.state.emacsPrefix;return n?(L(t),"-"==n?-1:Number(n)):e?null:1}function h(e){var r="string"==typeof e?function(t){t.execCommand(e)}:e;return function(t){var e=p(t);r(t);for(var n=1;n<e;++n)r(t)}}function A(t,e,n,r){var o=p(t);o<0&&(r=-r,o=-o);for(var i=0;i<o;++i){var l=n(t,e,r);if(u(l,e))break;e=l}return e}function m(e,n){function t(t){t.extendSelection(A(t,t.getCursor(),e,n))}return t.motion=!0,t}function v(t,e,n,r){for(var o,i=t.listSelections(),l=i.length;l--;)f(t,o=i[l].head,A(t,o,e,n),r)}function S(t,e){if(t.somethingSelected()){for(var n,r=t.listSelections(),o=r.length;o--;)f(t,(n=r[o]).anchor,n.head,e);return!0}}function P(t,e){t.state.emacsPrefix?"-"!=e&&(t.state.emacsPrefix+=e):(t.state.emacsPrefix=e,t.on("keyHandled",x),t.on("inputRead",R))}var w={"Alt-G":!0,"Ctrl-X":!0,"Ctrl-Q":!0,"Ctrl-U":!0};function x(t,e){t.state.emacsPrefixMap||w.hasOwnProperty(e)||L(t)}function L(t){t.state.emacsPrefix=null,t.off("keyHandled",x),t.off("inputRead",R)}function R(t,e){var n=p(t);if(1<n&&"+input"==e.origin){for(var r=e.text.join("\n"),o="",i=1;i<n;++i)o+=r;t.replaceSelection(o)}}function y(t,e){"string"==typeof e&&(/^\d$/.test(e)||"Ctrl-U"==e)||(t.removeKeyMap(D),t.state.emacsPrefixMap=!1,t.off("keyHandled",y),t.off("inputRead",y))}function k(t){t.setCursor(t.getCursor()),t.setExtending(!t.getExtending()),t.on("change",function(){t.setExtending(!1)})}function b(t){t.setExtending(!1),t.setCursor(t.getCursor())}function U(t,e){var n=t.getCursor(),r=t.findPosH(n,1,"word");t.replaceRange(e(t.getRange(n,r)),n,r),t.setCursor(r)}t.emacs={kill:f,killRegion:S,repeated:h};var X=t.keyMap.emacs=t.normalizeKeyMap({"Ctrl-W":function(t){f(t,t.getCursor("start"),t.getCursor("end"),!0)},"Ctrl-K":h(function(t){var e=t.getCursor(),n=t.clipPos(c(e.line)),r=t.getRange(e,n);/\S/.test(r)||(r+="\n",n=c(e.line+1,0)),f(t,e,n,"grow",r)}),"Alt-W":function(t){l(t.getSelection()),b(t)},"Ctrl-Y":function(t){var e=t.getCursor();t.replaceRange(n(p(t)),e,e,"paste"),t.setSelection(e,t.getCursor())},"Alt-Y":function(t){t.replaceSelection((1<i.length&&i.pop(),n()),"around","paste")},"Ctrl-Space":k,"Ctrl-Shift-2":k,"Ctrl-F":m(e,1),"Ctrl-B":m(e,-1),Right:m(e,1),Left:m(e,-1),"Ctrl-D":function(t){v(t,e,1,!1)},Delete:function(t){S(t,!1)||v(t,e,1,!1)},"Ctrl-H":function(t){v(t,e,-1,!1)},Backspace:function(t){S(t,!1)||v(t,e,-1,!1)},"Alt-F":m(r,1),"Alt-B":m(r,-1),"Alt-Right":m(r,1),"Alt-Left":m(r,-1),"Alt-D":function(t){v(t,r,1,"grow")},"Alt-Backspace":function(t){v(t,r,-1,"grow")},"Ctrl-N":m(o,1),"Ctrl-P":m(o,-1),Down:m(o,1),Up:m(o,-1),"Ctrl-A":"goLineStart","Ctrl-E":"goLineEnd",End:"goLineEnd",Home:"goLineStart","Alt-V":m(s,-1),"Ctrl-V":m(s,1),PageUp:m(s,-1),PageDown:m(s,1),"Ctrl-Up":m(g,-1),"Ctrl-Down":m(g,1),"Alt-A":m(C,-1),"Alt-E":m(C,1),"Alt-K":function(t){v(t,C,1,"grow")},"Ctrl-Alt-K":function(t){v(t,d,1,"grow")},"Ctrl-Alt-Backspace":function(t){v(t,d,-1,"grow")},"Ctrl-Alt-F":m(d,1),"Ctrl-Alt-B":m(d,-1),"Shift-Ctrl-Alt-2":function(t){var e=t.getCursor();t.setSelection(A(t,e,d,1),e)},"Ctrl-Alt-T":function(t){var e=d(t,t.getCursor(),-1),n=d(t,e,1),r=d(t,n,1),o=d(t,r,-1);t.replaceRange(t.getRange(o,r)+t.getRange(n,o)+t.getRange(e,n),e,r)},"Ctrl-Alt-U":h(function(t){for(var e=t.getCursor(),n=e.line,r=e.ch,o=[];n>=t.firstLine();){for(var i=t.getLine(n),l=null==r?i.length:r;0<l;){if(")"==(r=i.charAt(--l)))o.push("(");else if("]"==r)o.push("[");else if("}"==r)o.push("{");else if(/[\(\{\[]/.test(r)&&(!o.length||o.pop()!=r))return t.extendSelection(c(n,l))}--n,r=null}}),"Alt-Space":function(t){for(var e=t.getCursor(),n=e.ch,r=e.ch,o=t.getLine(e.line);n&&/\s/.test(o.charAt(n-1));)--n;for(;r<o.length&&/\s/.test(o.charAt(r));)++r;t.replaceRange(" ",c(e.line,n),c(e.line,r))},"Ctrl-O":h(function(t){t.replaceSelection("\n","start")}),"Ctrl-T":h(function(t){t.execCommand("transposeChars")}),"Alt-C":h(function(t){U(t,function(t){var e=t.search(/\w/);return-1==e?t:t.slice(0,e)+t.charAt(e).toUpperCase()+t.slice(e+1).toLowerCase()})}),"Alt-U":h(function(t){U(t,function(t){return t.toUpperCase()})}),"Alt-L":h(function(t){U(t,function(t){return t.toLowerCase()})}),"Alt-;":"toggleComment","Ctrl-/":h("undo"),"Shift-Ctrl--":h("undo"),"Ctrl-Z":h("undo"),"Cmd-Z":h("undo"),"Shift-Ctrl-Z":"redo","Shift-Alt-,":"goDocStart","Shift-Alt-.":"goDocEnd","Ctrl-S":"findPersistentNext","Ctrl-R":"findPersistentPrev","Ctrl-G":function(t){t.execCommand("clearSearch"),b(t)},"Shift-Alt-5":"replace","Alt-/":"autocomplete",Enter:"newlineAndIndent","Ctrl-J":h(function(t){t.replaceSelection("\n","end")}),Tab:"indentAuto","Alt-G G":function(n){var t,e,r,o=p(n,!0);if(null!=o&&0<o)return n.setCursor(o-1);e="Goto line",r=function(t){var e;t&&!isNaN(e=Number(t))&&e==(0|e)&&0<e&&n.setCursor(e-1)},(t=n).openDialog?t.openDialog(e+': <input type="text" style="width: 10em"/>',r,{bottom:!0}):r(prompt(e,""))},"Ctrl-X Tab":function(t){t.indentSelection(p(t,!0)||t.getOption("indentUnit"))},"Ctrl-X Ctrl-X":function(t){t.setSelection(t.getCursor("head"),t.getCursor("anchor"))},"Ctrl-X Ctrl-S":"save","Ctrl-X Ctrl-W":"save","Ctrl-X S":"saveAll","Ctrl-X F":"open","Ctrl-X U":h("undo"),"Ctrl-X K":"close","Ctrl-X Delete":function(t){f(t,t.getCursor(),C(t,t.getCursor(),1),"grow")},"Ctrl-X H":"selectAll","Ctrl-Q Tab":h("insertTab"),"Ctrl-U":function(t){t.state.emacsPrefixMap=!0,t.addKeyMap(D),t.on("keyHandled",y),t.on("inputRead",y)},fallthrough:"default"}),D={"Ctrl-G":L};function E(e){D[e]=function(t){P(t,e)},X["Ctrl-"+e]=function(t){P(t,e)},w["Ctrl-"+e]=!0}for(var H=0;H<10;++H)E(String(H));E("-")});