Skip to main content

Attempts to open a file at a specified line in the default editor.

Project description

spawneditor

A Python module that attempts to provide a common interface for opening an editor at a specified line in a file.

The launched editor will be chosen from, in order:

  1. The explicitly specified editor.
  2. The VISUAL environment variable, if DISPLAY is available.
  3. The EDITOR environment variable.
  4. Hard-coded paths to common editors.

Installation

pip install --user spawneditor

Alternatively clone the Git repository as a Git submodule named spawneditor.

Examples

import spawneditor

# Open an existing file in the default editor.
spawneditor.edit_file("path/to/file.txt")

# A line number may be specified.
spawneditor.edit_file("path/to/file.txt", line_number=123)

# Edits a blank temporary file.
edited_lines = spawneditor.edit_temporary()  

# Edits a temporary file with specified content.  Unlike `edit_file`,
# `edit_temporary` returns the contents of the edited file.
instructions = [
    "Instructions:",
    "Enter some text below the line.",
    "----",
]
new_contents = spawneditor.edit_temporary(instructions,
                                          line_number=(len(instructions) + 1))

# ... or, if you prefer a single string with newlines:
new_contents = spawneditor.edit_temporary(
    sss["Instructions:\nEnter some text below the line.\n----"],
    line_number=4,
)

for line in new_contents:
    print(line, end="")

FAQ

Q: How does spawneditor know how to invoke the editor at a specified line number?

spawneditor is hard-coded to recognize and invoke some common editors based on the name of the executed binary.

Q: What is considered a "common editor"?

See the editor_syntax_table in spawneditor.py.

Q: What if my editor isn't supported?

Other editors should still work. At worst, the specified file should be opened in the default editor, just not at the specified line.

If your editor isn't supported, I also encourage filing an issue or pull request to add support.

Q: Why not provide some mechanism for end-users to specify the syntax for their editor?

It's tempting to support using, say, a configuration file to allow users to specify how to invoke arbitrary editors. I currently prefer not to because:

  • It adds code complexity and incurs a penalty for what is likely to be a rare situation.
  • It reduces incentives to make spawneditor support other editors directly.
  • The fallback behavior still should open the file in the default editor, just not at the specified line number. I think that's pretty acceptable.

One pathological situation where an override would be necessary is if an editor uses the same executable name as one of the recognized editors and uses a different command-line syntax for specifying the line number. However, that also should be a very rare situation.

One compelling reason might be to allow people to set their default editor to a wrapper script that invokes their actual editor. I'm sympathetic to that, but in the meantime, the wrapper script could be given the same name as the actual editor.

Q: Why does VISUAL depend on DISPLAY?

Technically VISUAL should refer to a full-screen editor, not necessarily a graphical editor. In practice, however, I think that the graphical vs. text-mode distinction is far more relevant and important than the distinction between full-screen and line-based editors.

Q: Why does spawneditor.edit_file immediately return when spawning a multi-document editor (e.g. Visual Studio Code, Sublime Text)?

Multi-document editors are typically also single-instance: when the editor is invoked, a new process will be spawned, but that process then will forward the request to an existing, primary process. The new, secondary process then will immediately exit and cause spawneditor.edit_file to return. Such editors typically provide a command-line option (e.g. --wait for Visual Studio Code and Sublime Text) to keep the secondary process alive until the file is closed by the primary process. Consult the documentation to your editor.

Q: Aren't there already Python packages that do this?

Yes. The editor and python-editor packages are alternatives that also invoke the default editor. Currently neither one supports opening a file to a specified line number. Were I aware of them when I wrote spawneditor, and possibly I would have tried contributing to them instead. However, spawneditor is implemented rather differently than either one, and I'm not sure that either project would have appreciated drastic changes. There likely is room for cross-pollination.


Copyright © 2020-2022 James D. Lin.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spawneditor-0.1.2.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spawneditor-0.1.2-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file spawneditor-0.1.2.tar.gz.

File metadata

  • Download URL: spawneditor-0.1.2.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for spawneditor-0.1.2.tar.gz
Algorithm Hash digest
SHA256 496a765c1b6f33cae8ff363a686e3d8c1ce293e9ea1130a6a275cee2038e03aa
MD5 373f74598d3f5ec9eae2419c7ebf7082
BLAKE2b-256 f7a58812ea7ed892391d543bdc85c72326651ea642c9dcc260e5b00b7122544c

See more details on using hashes here.

File details

Details for the file spawneditor-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: spawneditor-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for spawneditor-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 effd5573e962b48b52cdcc8c1a53f20cffd31ba07eb979090d3dab191dd17646
MD5 44ba01bcddabf328b95208cedd4e0e15
BLAKE2b-256 1d73901ab9b3d8b80526781a7195dd9375f72e6773fcc11003490287c8b3ad87

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page