Commit a1f58a26 authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

import emmet aka zen-coding

adjusted it to use new scripting require
splitted into commands folder and libraries
you may split the lib.js even more, if you want, like you mentioned
REVIEW: 107101
parent 4472f384
# install default libraries files
file (GLOB KATE_JS_LIBRARIES_FILES "${CMAKE_CURRENT_SOURCE_DIR}/libraries/*.js")
install (FILES ${KATE_JS_LIBRARIES_FILES} DESTINATION ${DATA_INSTALL_DIR}/katepart/script/libraries)
install (DIRECTORY libraries/ DESTINATION ${DATA_INSTALL_DIR}/katepart/script/libraries
FILES_MATCHING PATTERN "*.js")
# install default commands files
file (GLOB KATE_JS_COMMANDS_FILES "${CMAKE_CURRENT_SOURCE_DIR}/commands/*.js")
install (FILES ${KATE_JS_COMMANDS_FILES} DESTINATION ${DATA_INSTALL_DIR}/katepart/script/commands)
install (DIRECTORY commands/ DESTINATION ${DATA_INSTALL_DIR}/katepart/script/commands
FILES_MATCHING PATTERN "*.js")
# install default indentation files
file (GLOB KATE_JS_INDENTATION_FILES "${CMAKE_CURRENT_SOURCE_DIR}/indentation/*.js")
install (FILES ${KATE_JS_INDENTATION_FILES} DESTINATION ${DATA_INSTALL_DIR}/katepart/script/indentation)
install (DIRECTORY indentation/ DESTINATION ${DATA_INSTALL_DIR}/katepart/script/indentation
FILES_MATCHING PATTERN "*.js")
/* kate-script
* author: Gregor Petrin
* license: BSD
* revision: 1
* kate-version: 3.4
* functions: emmetExpand, emmetWrap, emmetSelectTagPairInwards, emmetSelectTagPairOutwards, emmetMatchingPair, emmetToggleComment ,emmetNext, emmetPrev, emmetSelectNext, emmetSelectPrev, emmetDelete, emmetSplitJoinTab, emmetEvaluateMathExpression, emmetDecrementNumberBy1, emmetDecrementNumberBy10, emmetDecrementNumberBy01, emmetIncrementNumberBy1, emmetIncrementNumberBy10, emmetIncrementNumberBy01
*
*/
// required katepart js libraries
require ("range.js");
require ("emmet/lib.js");
require ("emmet/editor_interface.js");
function help(cmd)
{
cmds = {
"emmetExpand" : "Expands the abbreviation using Emmet expressions; see http://code.google.com/p/zen-coding/wiki/ZenHTMLSelectorsEn",
"emmetWrap" : "Wraps the selected text in XML tags constructed from the provided Emmet expression (defaults to div).",
"emmetMatchingPair" : "Moves the caret to the current tag's pair",
"emmetSelectTagPairInwards" : "Select contents of HTML/XML tag, moving inward on continious invocations",
"emmetSelectTagPairOutwards" : "Select contents of HTML/XML tag, moving outwards on continious invocations",
"emmetNext" : "Move to the next edit point (tag or empty attribute).",
"emmetPrev" : "Move to the previous edit point (tag or empty attribute).",
"emmetSelectNext" : "Select next edit point (tag or empty attribute).",
"emmetSelectPrev" : "Select previous edit point (tag or empty attribute).",
"emmetToggleComment" : "Toggle comment of current tag or CSS selector",
"emmetDelete" : "Deletes tag under cursor",
"emmetSplitJoinTab" : "Splits or joins a tag",
"emmetEvaluateMathExpression" : "Evaluates a simple math expression",
"emmetDecrementNumberBy1" : "Decrement number under cursor by 1",
"emmetDecrementNumberBy10" : "Decrement number under cursor by 10",
"emmetDecrementNumberBy01" : "Decrement number under cursor by 0.1",
"emmetIncrementNumberBy1" : "Increment number under cursor by 1",
"emmetIncrementNumberBy10" : "Increment number under cursor by 10",
"emmetIncrementNumberBy01" : "Increment number under cursor by 0.1"
}
return i18n(cmds[cmd]);
}
function action(cmd)
{
var a = new Object();
a.icon = "";
a.category = "";
a.shortcut = "";
a.interactive = false;
if (cmd == "emmetExpand") {
a.text = i18n("Emmet: expand abbreviation");
} else if (cmd == "emmetWrap") {
a.text = i18n("Emmet: wrap with tag");
a.interactive = true;
} else if (cmd == "emmetMatchingPair") {
a.text = i18n("Emmet: move cursor to matching tag");
} else if (cmd == "emmetSelectTagPairInwards") {
a.text = i18n("Emmet: select HTML/XML tag contents inwards");
} else if (cmd == "emmetSelectTagPairOutwards") {
a.text = i18n("Emmet: select HTML/XML tag contents outwards");
} else if (cmd == "emmetToggleComment") {
a.text = i18n("Emmet: toggle comment");
} else if (cmd == "emmetNext") {
a.text = i18n("Emmet: go to next edit point");
} else if (cmd == "emmetPrev") {
a.text = i18n("Emmet: go to previous edit point");
} else if (cmd == "emmetSelectNext") {
a.text = i18n("Emmet: select next edit point");
} else if (cmd == "emmetSelectPrev") {
a.text = i18n("Emmet: select previous edit point");
} else if (cmd == "emmetDelete") {
a.text = i18n("Emmet: delete tag under cursor");
} else if (cmd == "emmetSplitJoinTab") {
a.text = i18n("Emmet: split or join a tag");
} else if (cmd == "emmetEvaluateMathExpression") {
a.text = i18n("Emmet: evaluate a simple math expression");
} else if (cmd == "emmetDecrementNumberBy1") {
a.text = i18n("Emmet: decrement number by 1");
} else if (cmd == "emmetDecrementNumberBy10") {
a.text = i18n("Emmet: decrement number by 10");
} else if (cmd == "emmetDecrementNumberBy01") {
a.text = i18n("Emmet: decrement number by 0.1");
} else if (cmd == "emmetIncrementNumberBy1") {
a.text = i18n("Emmet: increment number by 1");
} else if (cmd == "emmetIncrementNumberBy10") {
a.text = i18n("Emmet: increment number by 10");
} else if (cmd == "emmetIncrementNumberBy01") {
a.text = i18n("Emmet: increment number by 0.1");
}
return a;
}
//Returns the kate editor interface
function getInterface() {
var kateInterface = new zen_editor(document, view);
return kateInterface;
}
function emmetExpand() {
emmet.require('actions').run('expand_abbreviation', getInterface());
}
function emmetWrap(par) {
emmet.require('actions').run('wrap_with_abbreviation', getInterface(), par || 'div');
}
function emmetMatchingPair() {
emmet.require('actions').run('matching_pair', getInterface());
}
function emmetSelectTagPairInwards() {
emmet.require('actions').run('match_pair', getInterface());
}
function emmetSelectTagPairOutwards() {
emmet.require('actions').run('match_pair', getInterface());
}
function emmetNext() {
emmet.require('actions').run('next_edit_point', getInterface());
}
function emmetPrev() {
emmet.require('actions').run('prev_edit_point', getInterface());
}
function emmetSelectNext() {
emmet.require('actions').run('select_next_item', getInterface());
}
function emmetSelectPrev() {
emmet.require('actions').run('select_previous_item', getInterface());
}
function emmetToggleComment() {
emmet.require('actions').run('toggle_comment', getInterface());
}
function emmetDelete() {
emmet.require('actions').run('remove_tag', getInterface());
}
function emmetSplitJoinTab() {
emmet.require('actions').run('split_join_tag', getInterface());
}
function emmetEvaluateMathExpression() {
emmet.require('actions').run('evaluate_math_expression', getInterface());
}
function emmetIncrementNumberBy1() {
emmet.require('actions').run('increment_number_by_1', getInterface());
}
function emmetIncrementNumberBy10() {
emmet.require('actions').run('increment_number_by_10', getInterface());
}
function emmetIncrementNumberBy01() {
emmet.require('actions').run('increment_number_by_01', getInterface());
}
function emmetDecrementNumberBy1() {
emmet.require('actions').run('decrement_number_by_1', getInterface());
}
function emmetDecrementNumberBy10() {
emmet.require('actions').run('decrement_number_by_10', getInterface());
}
function emmetDecrementNumberBy01() {
emmet.require('actions').run('decrement_number_by_01', getInterface());
}
\ No newline at end of file
// Utility functions
// distance - Like cursor, only absolute
// interval - Like range, only absolute
distanceFromCursor = function(cursor) {
var pos = cursor.column;
var a;
for (a = 0; a < cursor.line; a++) {
pos += document.lineLength(a) + 1; //Kate normalizes newlines to \n
}
return pos;
}
intervalFromRange = function(range) {
return {
start: distanceFromCursor(range.start),
end: distanceFromCursor(range.end)
}
}
cursorFromDistance = function(distance) {
var line = 0;
var positionSoFar = 0;
while (positionSoFar + (document.lineLength(line) + 1) <= distance) {
positionSoFar += (document.lineLength(line) + 1)
line++;
}
return new Cursor(line, distance - positionSoFar);
}
rangeFromIntervals = function(distance1, distance2) {
return new Range( cursorFromDistance(distance1), cursorFromDistance(distance2) );
}
// Pseudo editor interface
var zen_editor = function(document, view) {
this.context = document;
this.setContext = function(document) {
this.context = document;
}
/**
* Returns character indexes of selected text: object with <code>start</code>
* and <code>end</code> properties. If there's no selection, should return
* object with <code>start</code> and <code>end</code> properties referring
* to current caret position
* @return {Object}
* @example
* var selection = editor.getSelectionRange();
* alert(selection.start + ', ' + selection.end);
*/
this.getSelectionRange = function() {
var returnValue
if (view.hasSelection()) {
returnValue = intervalFromRange(view.selection());
}
else {
returnValue = intervalFromRange(new Range(view.cursorPosition(), view.cursorPosition()));
}
return returnValue;
}
/**
* Creates selection from <code>start</code> to <code>end</code> character
* indexes. If <code>end</code> is ommited, this method should place caret
* and <code>start</code> index
* @param {Number} start
* @param {Number} [end]
* @example
* editor.createSelection(10, 40);
*
* //move caret to 15th character
* editor.createSelection(15);
*/
this.createSelection = function(start, end) {
var selection = rangeFromIntervals(start, end);
view.setSelection(selection);
}
/**
* Returns current line's start and end indexes as object with <code>start</code>
* and <code>end</code> properties
* @return {Object}
* @example
* var range = editor.getCurrentLineRange();
* alert(range.start + ', ' + range.end);
*/
this.getCurrentLineRange = function() {
var position = view.cursorPosition()
return {
start: distanceFromCursor(new Cursor(position.line, 0)),
end: distanceFromCursor(new Cursor(position.line,document.lineLength(position.line)))
};
}
/**
* Returns current caret position
* @return {Number|null}
*/
this.getCaretPos = function(){
return distanceFromCursor(view.cursorPosition());
}
/**
* Set new caret position
* @param {Number} pos Caret position
*/
this.setCaretPos = function(pos){
view.setCursorPosition(cursorFromDistance(pos));
}
/**
* Returns content of current line
* @return {String}
*/
this.getCurrentLine = function() {
return document.line(view.cursorPosition().line);
}
/**
* Replace editor's content or it's part (from <code>start</code> to
* <code>end</code> index). If <code>value</code> contains
* <code>caret_placeholder</code>, the editor will put caret into
* this position. If you skip <code>start</code> and <code>end</code>
* arguments, the whole target's content will be replaced with
* <code>value</code>.
*
* If you pass <code>start</code> argument only,
* the <code>value</code> will be placed at <code>start</code> string
* index of current content.
*
* If you pass <code>start</code> and <code>end</code> arguments,
* the corresponding substring of current target's content will be
* replaced with <code>value</code>.
* @param {String} value Content you want to paste
* @param {Number} [start] Start index of editor's content
* @param {Number} [end] End index of editor's content
* @param {Boolean} [no_indent] Do not auto indent <code>value</code>
*/
this.replaceContent = function(value, start, end, no_indent) {
//handle the no indent parameter
if (!no_indent) {
var utils = emmet.require('utils');
value = utils.padString(value, utils.getLinePadding(this.getCurrentLine()));
}
//emmet's tabstops - TODO: what would be the best way to support this in Kate?
var tabstopData = emmet.require('tabStops').extract(value, {
escape: function(ch) {
return ch;
}
});
value = tabstopData.text;
var firstTabStop = tabstopData.tabstops[0];
if (typeof(end) == 'undefined') {
if (typeof(start) == 'undefined') {
document.setText(value);
} else {
document.insertText(cursorFromDistance(start), value);
}
} else {
document.editBegin(); //group changes together in undo history
var docRange = rangeFromIntervals(start, end);
document.removeText(docRange);
document.insertText(docRange.start, value);
document.editEnd();
}
if (firstTabStop) this.setCaretPos(start + firstTabStop.start);
}
/**
* Returns editor's content
* @return {String}
*/
this.getContent = function(){
return document.text();
}
/**
* Returns current editor's syntax mode
* @return {String}
*/
this.getSyntax = function(){
var syntax = document.highlightingModeAt(view.cursorPosition()).toLowerCase();
if (syntax == 'haml with ruby') syntax = 'haml'; //fixes a common syntax highlighting verbosity
return syntax;
}
/**
* Returns current output profile name (@see emmet#setupProfile)
* @return {String}
*/
this.getProfileName = function() {
var profile = this.getSyntax();
if (profile == 'html') {
var htmlProfile = emmet.require('resources').getVariable('profile');
if (!htmlProfile) {
htmlProfile = this.getContent().search(/<!DOCTYPE[^>]+XHTML/i) != -1 ? 'xhtml': 'html';
}
profile = htmlProfile;
}
return profile;
}
/**
* Ask user to enter something
* @param {String} title Dialog title
* @return {String} Entered data
* @since 0.65
*/
this.prompt = function(title) {
return '';
}
/**
* Returns current selection
* @return {String}
* @since 0.65
*/
this.getSelection = function() {
return view.selectedText();
}
/**
* Returns current editor's file path
* @return {String}
* @since 0.65
*/
this.getFilePath = function() {
return document.url(); //or would document.fileName() be better?
}
}
\ No newline at end of file
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment