我的 Textadept 配置

原创
2021/11/30 21:06
阅读数 407

一直在用 Scite 作为默认文本编辑器,小巧快速,特性也不少,配置好之后,可比肩 notepad++,语法高亮、补全、编译运行,都挺便利。 只是对于主题、扩展的支持差强人意,官方文档感觉较烂,看着头疼,第三方资源也不多。 直到看到这篇介绍开源编辑器的文章:35 Best open-source text editors for programming as of 2021 - Slant,提到另一款基于 Scintilla 的编辑器:Textadept,便安装试了一下,默认特性比 Scite 丰富,性能略弱但差别不大。 看了下官网,文档细致,API 设计优雅,悦之,遂换

  1. 从官方手册里扒了下初始配置,lua 语言写的,配置可读性好,非常容易上手
  2. 把一些 Scite 上的常用配置移植了过来
  3. 找了些主题和扩展,研究了一下,把以前一些 scite 的使用痛点用 API 解决了
-- Adjust the default theme's font and size.
-- https://github.com/rgieseke/base16-textadept/
if not CURSES then
    view:set_theme('light', {font = 'Monaco', size = 13})
end
-- 根据日落日出时间自动设置主题
events.connect(events.FOCUS, function()
    local time = tonumber(os.date("%H%M", os.time()))
    local theme = (0700 < time and time < 2000) and 'base16-google-light' or 'base16-dracula'
    view:set_theme(theme, {font = 'Monaco', size = 13})
end)

-- 文件编码
table.insert(io.encodings, 3, 'CP936') -- before CP1252

-- Use tabs for indentation.
buffer.use_tabs = false
buffer.tab_width = 4

-- 中文标点识别
buffer.punctuation_chars = buffer.punctuation_chars .. ',。;?!)(“:”、'

-- 自动换行
view.wrap_mode = view.WRAP_WORD
view.wrap_visual_flags = view.WRAPVISUALFLAG_MARGIN
-- 右键菜单
textadept.menu.context_menu[#textadept.menu.context_menu + 1] = {''} -- separator
textadept.menu.context_menu[#textadept.menu.context_menu + 1] = {
    '自动换行', function()
        view.wrap_mode = (view.wrap_mode == view.WRAP_NONE) and view.WRAP_WORD or view.WRAP_NONE
    end
}

-- 隐藏水平滚动条,加大可视空间,可通过右键菜单调出
view.h_scroll_bar = false
textadept.menu.context_menu[#textadept.menu.context_menu + 1] = {
    '水平滚动条', function()
        view.h_scroll_bar = not view.h_scroll_bar
    end
}

-- Highlight all occurrences of the selected word.
textadept.editing.highlight_words = textadept.editing.HIGHLIGHT_SELECTED
-- Auto-enclose selected text between any typed punctuation character (textadept.editing.auto_pairs) 
textadept.editing.auto_enclose = true

-- Always strip trailing spaces on save, automatically highlight the current
-- word, and use C99-style line comments in C code.
textadept.editing.strip_trailing_space = true
textadept.editing.comment_string.ansi_c = '//'

-- 快捷键,个人习惯
-- modules/textadept/keys.lua
-- https://orbitalquark.github.io/textadept/api.html
keys['ctrl+q'] = textadept.editing.toggle_comment
keys['ctrl+g'] = textadept.editing.goto_line
keys['ctrl+l'] = buffer.line_cut
keys['ctrl+alt+l'] = buffer.line_delete
keys['ctrl+shift+down'] = buffer.line_down_extend
keys['ctrl+shift+up'] = buffer.line_up_extend
keys['ctrl+alt+down'] = buffer.move_selected_lines_down
keys['ctrl+alt+up'] = buffer.move_selected_lines_up
keys['f5'] = textadept.run.compile
keys['ctrl+f5'] = textadept.run.run
keys['ctrl+u'] = function() io.quick_open(_USERHOME) end

-- 根据光标或选中自动查找文本
local function find_word_at_cursor(forward)
    -- Select the word at the current position
    local sel = buffer.get_sel_text()
    if #sel == 0 then
        local position = buffer.current_pos
        buffer.word_left()
        buffer.word_right_end_extend()
        sel = buffer.get_sel_text()
        buffer.set_sel(position, position)
        -- Escape special characters
        sel = string.gsub(sel, '([().*+?^$%%[%]-])', '%%%1')
    end
    if #sel > 0 then
        ui.find.find_entry_text = sel
        forward()
    end
end
keys['ctrl+f3'] = function() find_word_at_cursor(ui.find.find_next) end
keys['ctrl+shift+f3'] = function() find_word_at_cursor(ui.find.find_prev) end
-- 修改'查找&替换'默认行为,有选中时自动填入
keys['ctrl+f'] = function()
    local sel = buffer.get_sel_text()
    if #sel > 0 then ui.find.find_entry_text = sel end
    ui.find.focus()
end

-- Recognize files types.
textadept.file_types.extensions.luadoc = 'lua'
textadept.file_types.patterns['^#!.+/zsh'] = 'bash'
textadept.file_types.extensions.mark = 'json'
textadept.file_types.extensions.PKGBUILD = 'bash'

-- Change the file commands
textadept.run.run_commands.lua = 'lua5.4 "%f"'
textadept.run.run_commands.python = 'python3 "%f"'
textadept.run.compile_commands.bash = 'shellcheck "%f"'
textadept.run.compile_commands.json = 'cat "%f" | jq . | tee "%f"'

-- Indentation settings for individual languages.
-- Toggle between using tabs and spaces via Ctrl+Alt+Shift+T
events.connect(events.LEXER_LOADED, function(name)
	if name == 'python' then
		buffer.tab_width = 4
        buffer.tab_indents = true
	elseif name == 'ruby' or name == 'yaml' then
		buffer.tab_width = 2
	elseif name == 'markdown' then
		buffer.use_tabs = true
		buffer.tab_width = 4
		textadept.editing.strip_trailing_space = false
	end
end)

官方手册:https://orbitalquark.github.io/textadept/manual.html

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部