Textadept Usage Tips

Brynne Taylor
5 min readDec 18, 2021

--

In the previous blog, I recommended Textadept to everyone, and in this article, I will talk about my Textadept experience. To facilitate your daily use of Textadept.

We know that Textadept is a cross-platform, graphical interface based on GTK. Anyone who’s ever used Lua or GTK will think they run better on Linux. So Textadept needs to be configured a little bit more in Windows than in Linux to be configured properly. These are since paths or files under Windows like to use an anti-human GBK series encoding (for Chinese users). Windows paths use backslashes. How easy it would be to program if paths use slashes instead of backslashes like Linux.

Add encodings

I’ll start with this since I just mentioned encoding. Textadept doesn’t list all possible encodings in its table like some other editors do, for reasons of minimalism and efficiency. Only some common encodings for Latin languages are listed. It may take a long time for Linux users who commonly use UTF-8 to notice it, but other encodings are often encountered on Windows. To support GBK encoding, we need to add gbk to io.encodings in core/file_io.lua. Once added, you will notice that opening GBK encoded files does not crash.

Changing fonts

We probably won’t see it on Linux, but Chinese characters probably won’t show up on Windows. Since the encoding has been corrected, then why still doesn’t show up? The reason is that the font used by Textadept for the code is directly taken from the default monospace font of GTK, which may be Courier New under Windows, but this is a problem, this font does not have Chinese characters. Then why do other editors using Courier New display Chinese characters well? The reason is that they check the system’s fallback fonts, but Textadept because of the minimalism, of course, doesn’t check, so we have to manually configure it. The easiest way is to set the font (as well as the theme) for Textadept in the first line of ~/.textadept/init.lua, and the font has to support both English and Chinese, such as WenQuanYi Micro Hei:

view:set_theme('light', {font = 'WenQuanYi Micro Hei Mono'})

But there’s one other place you might want to set up, and that’s the font for the interface menu. Modify share/themes/MS-Windows/gtk-2.0/gtkrc file and add these at the bottom

style "wqyfont"
{
font_name = "WenQuanYi Micro Hei 10"
}
widget "*" style "wqyfont"

User modules

If you want to use someone else’s extensions or write your own extensions, you will have to import modules. Never write all your code in a single init.lua, the way Lua imports modules is simple.

_M.file_browser = require('file_browser')

Just simply require it. Some people say that Lua is like JavaScript, and I think so too. Some feelings of JavaScript programming can be used for reference. To avoid reinventing wheels, you’d better read this Wiki. Mitchell is a very enthusiastic person, and he answered all the questions I asked.

I believe the language modules are the ones you use the most. It provides auto-completion of languages, shortcuts, and snippets. In addition to these, I also use folding (code folding), File Browser, etc.

Pattern matching

If you haven’t used Lua before, I think the thing that will probably be most unfamiliar to you is Lua’s pattern matching. Textadept uses three different pattern matching schemes in different places. Of course, if you hold a learning attitude, it may be more helpful to you. Let me talk about these three pattern matching schemes.

  • Lua’s native pattern matching. In most places the author uses this scheme. Common matching is fine, that is, there is a relatively large difference with the commonly used regular expressions, but in general the syntax is still similar. If you’re not familiar with it, check out the official documentation or Textadept’s manual.
  • LPeg. LPeg can be said to be quite powerful, but also will give you a completely different experience. I feel that LPeg is more flexible and has a sense of programming. In Textadept, LPeg is used in lexers. However, there are quite a lot of lexers, and, inevitably, some lexers are not well implemented or do not produce good-looking syntax highlighting. I hope everyone can improve them easily.
  • EcmaScript style standard regular expressions. Used in the interface of the search, replace, etc. The source code is achieved by calling the Scintilla search function. This also brings a little disadvantage, Scintilla does not allow matching line breaks (\r and \n); in fact, Textadept also uses Lua pattern matching to search, but the new version has replaced it. If you want to use Lua pattern matching, you can consider trying this.

Code Formatting

Indentation size-related conversions can be solved without any external tools. As long as the indent size has been set up for your language, such as a 2-space indent, you can just copy the 4-space file over and it will automatically become 2-space. (This functionality has been moved to an extension later.)

More specialized formatting will require the use of external tools. Here are two simple examples: JSON and HTML. Since python has a ready-made JSON module, I changed it a little to meet my needs:

import argparse
import json
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def main():
prog = 'python jsontool.py'
description = ('A simple command line interface for json module '
'to validate and pretty-print JSON objects.')
parser = argparse.ArgumentParser(prog=prog, description=description)
parser.add_argument('infile', nargs='?', type=argparse.FileType(),
help='a JSON file to be validated or pretty-printed')
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
help='write the output of infile to outfile')
parser.add_argument('--sort-keys', action='store_true', default=False,
help='sort the output of dictionaries alphabetically by key')
options = parser.parse_args()
infile = options.infile or sys.stdin
outfile = options.outfile or sys.stdout
sort_keys = options.sort_keys
with infile:
try:
obj = json.load(infile)
except ValueError as e:
raise SystemExit(e)
with outfile:
json.dump(obj, outfile, sort_keys=sort_keys, indent=2, ensure_ascii=False)
outfile.write('\n')
if __name__ == '__main__':
main()

After the change, you can complete the formatting in the terminal. But if you want to be a little lazier and complete the operation in Textadept, you need the “filter through” function. When I translated the interface text, I did not know how to translate this function into Chinese, so I kept the English. Filter through has the keyboard shortcut Ctrl+|, enter in the command box below

python jsontool.py

Then you will see the perfectly formatted JSON. in fact, our input goes in through stdin, and Textadept will take its stdout and update our file. The same goes for HTML, for example, by typing in the commands

tidy -qi -w 106 --drop-empty-elements 0 --drop-empty-paras 0 --tidy-mark 0

and we’ll get the formatted file. Other external tools that support pipe input and output should be fine.

Problems

You cannot open a file with a Chinese path under Windows with double-clicking. If you open it, it will cause Textadept to crash. The reason is not yet clear.

Conclusion

In short, if you use Textadept, you can’t expect everything to be configured for you. But through Textadept’s detailed API, it is not so difficult to implement many functions.

Originally published at brynne8.github.io.

--

--