aboutsummaryrefslogtreecommitdiffhomepage
path: root/core/init.lua
blob: fd5a089de66e22d24c62c4faf84b5a971d4e3af5 (plain)
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
-- Copyright 2007-2022 Mitchell. See LICENSE.

_RELEASE = 'Textadept 11.3'
_COPYRIGHT = 'Copyright © 2007-2022 Mitchell. See LICENSE.\n' ..
  'https://orbitalquark.github.io/textadept'

package.path = string.format('%s/core/?.lua;%s', _HOME, package.path)

-- for _, arg in ipairs(arg) do if arg == '-t' or arg == '--test' then pcall(require, 'luacov') end end

require('assert')
_SCINTILLA = require('iface')
events = require('events')
args = require('args')
_L = require('locale')
require('file_io')
require('lfs_ext')
require('ui')
keys = require('keys')
_M = {} -- language modules table

-- pdcurses compatibility.
if CURSES and WIN32 then
  function os.spawn(cmd, ...)
    local cwd = lfs.currentdir()
    local args, i = {...}, 1
    if type(args[i]) == 'string' then
      lfs.chdir(args[i]) -- cwd
      i = i + 1
    end
    if type(args[i]) == 'table' then i = i + 1 end -- env (ignore)
    local p = io.popen(assert_type(cmd, 'string', 1) .. ' 2>&1')
    if type(args[i]) == 'function' then args[i](p:read('a')) end -- stdout_cb
    local status = select(3, p:close())
    if type(args[i + 2]) == 'function' then args[i + 2](status) end -- exit_cb
    lfs.chdir(cwd) -- restore
    return p
  end
end

-- Replacement for original `buffer:text_range()`, which has a C struct for an argument.
-- Documentation is in core/.buffer.luadoc.
local function text_range(buffer, start_pos, end_pos)
  local target_start, target_end = buffer.target_start, buffer.target_end
  buffer:set_target_range(math.max(1, assert_type(start_pos, 'number', 2)),
    math.min(assert_type(end_pos, 'number', 3), buffer.length + 1))
  local text = buffer.target_text
  buffer:set_target_range(target_start, target_end) -- restore
  return text
end

local GETNAMEDSTYLE = _SCINTILLA.properties.named_styles[1]
-- Documentation is in core/.buffer.luadoc.
local function style_of_name(buffer, style_name)
  return buffer:private_lexer_call(GETNAMEDSTYLE, assert_type(style_name, 'string', 2))
end

events.connect(events.BUFFER_NEW,
  function() buffer.text_range, buffer.style_of_name = text_range, style_of_name end)

--[[ This comment is for LuaDoc.
---
-- Extends Lua's _G table to provide extra functions and fields for Textadept.
-- @field _HOME (string)
--   The path to Textadept's home, or installation, directory.
-- @field _RELEASE (string)
--   The Textadept release version string.
-- @field _USERHOME (string)
--   The path to the user's *~/.textadept/* directory, where all preferences and user-data
--   is stored.
--   On Windows machines *~/* is the value of the "USERHOME" environment variable (typically
--   *C:\Users\username\\* or *C:\Documents and Settings\username\\*). On Linux, BSD, and macOS
--   machines *~/* is the value of "$HOME" (typically */home/username/* and */Users/username/*
--   respectively).
-- @field _CHARSET (string)
--   The filesystem's character encoding.
--   This is used when [working with files](#io).
-- @field WIN32 (bool)
--   Whether or not Textadept is running on Windows.
-- @field OSX (bool)
--   Whether or not Textadept is running on macOS as a GUI application.
-- @field LINUX (bool)
--   Whether or not Textadept is running on Linux.
-- @field BSD (bool)
--   Whether or not Textadept is running on BSD.
-- @field CURSES (bool)
--   Whether or not Textadept is running in a terminal.
--   Curses feature incompatibilities are listed in the [Appendix][].
--
--   [Appendix]: manual.html#terminal-version-compatibility
module('_G')]]

--[[ The tables below were defined in C.

---
-- Table of command line parameters passed to Textadept.
-- @class table
-- @see args
-- @name arg
local arg

---
-- Table of all open buffers in Textadept.
-- Numeric keys have buffer values and buffer keys have their associated numeric keys.
-- @class table
-- @usage _BUFFERS[n]      --> buffer at index n
-- @usage _BUFFERS[buffer] --> index of buffer in _BUFFERS
-- @see _G.buffer
-- @name _BUFFERS
local _BUFFERS

---
-- Table of all views in Textadept.
-- Numeric keys have view values and view keys have their associated numeric keys.
-- @class table
-- @usage _VIEWS[n]    --> view at index n
-- @usage _VIEWS[view] --> index of view in _VIEWS
-- @see _G.view
-- @name _VIEWS
local _VIEWS

---
-- The current [buffer](#buffer) in the [current view](#_G.view).
-- @class table
-- @name buffer
local buffer

---
-- The current [view](#view).
-- @class table
-- @name view
local view

-- The functions below are Lua C functions.

---
-- Emits a `QUIT` event, and unless any handler returns `false`, quits Textadept.
-- @see events.QUIT
-- @class function
-- @name quit
local quit

---
-- Resets the Lua State by reloading all initialization scripts.
-- Language modules for opened files are NOT reloaded. Re-opening the files that use them will
-- reload those modules instead.
-- This function is useful for modifying user scripts (such as *~/.textadept/init.lua* and
-- *~/.textadept/modules/textadept/keys.lua*) on the fly without having to restart Textadept. `arg`
-- is set to `nil` when reinitializing the Lua State. Any scripts that need to differentiate
-- between startup and reset can test `arg`.
-- @class function
-- @name reset
local reset

---
-- Calls function *f* with the given arguments after *interval* seconds.
-- If *f* returns `true`, calls *f* repeatedly every *interval* seconds as long as *f* returns
-- `true`. A `nil` or `false` return value stops repetition.
-- @param interval The interval in seconds to call *f* after.
-- @param f The function to call.
-- @param ... Additional arguments to pass to *f*.
-- @class function
-- @name timeout
local timeout
]]