ad_editor/
lib.rs

1//! ad :: the adaptable editor
2#![warn(
3    clippy::complexity,
4    clippy::correctness,
5    clippy::style,
6    future_incompatible,
7    missing_debug_implementations,
8    // missing_docs,
9    rust_2018_idioms,
10    rustdoc::all,
11    clippy::undocumented_unsafe_blocks
12)]
13// Required for testing rendering behaviour.
14// As of https://github.com/rust-lang/rust/issues/140281 this needs to be at the crate level
15#![allow(text_direction_codepoint_in_literal)]
16
17use libc::termios as Termios;
18use std::{io::Write, process, sync::OnceLock};
19
20pub use ad_event::Source;
21
22pub mod buffer;
23pub mod cli;
24pub mod config;
25pub mod dot;
26pub mod editor;
27pub mod exec;
28pub mod fsys;
29pub mod input;
30pub mod key;
31pub mod log;
32pub mod lsp;
33pub mod mode;
34mod parse;
35pub mod plumb;
36pub mod regex;
37pub mod syntax;
38pub mod system;
39pub mod term;
40pub mod trie;
41pub mod ui;
42pub mod util;
43pub mod ziplist;
44
45pub use cli::{CliAction, Cmd9p, ConfigSource, ParsedArgs, USAGE};
46pub use config::Config;
47pub use editor::{Editor, EditorMode};
48pub use exec::{Edit, Program};
49pub use log::LogBuffer;
50pub use plumb::PlumbingRules;
51
52use term::{disable_alternate_screen, disable_bracketed_paste, disable_mouse_support, set_termios};
53
54/// The environment variable to set to control logging within ad
55pub const LOG_LEVEL_ENV_VAR: &str = "AD_LOG";
56/// The current version of the editor
57pub const VERSION: &str = env!("CARGO_PKG_VERSION");
58
59pub(crate) const UNNAMED_BUFFER: &str = "[No Name]";
60pub(crate) const MAX_NAME_LEN: usize = 50;
61
62pub(crate) static ORIGINAL_TERMIOS: OnceLock<Termios> = OnceLock::new();
63
64pub(crate) static PID: OnceLock<u32> = OnceLock::new();
65
66pub(crate) fn pid() -> u32 {
67    *PID.get_or_init(process::id)
68}
69
70/// Helper for accessing config stored on self as an `Arc<Mutex<Config>>`
71#[macro_export]
72macro_rules! config_handle {
73    ($self:expr) => {{
74        match $self.config.read() {
75            Ok(config) => config,
76            Err(err) => {
77                $self.config.clear_poison();
78                err.into_inner()
79            }
80        }
81    }};
82}
83
84/// Helper for panicking the program but first ensuring that we have restored the
85/// terminal state in the same way that we do when the Editor is dropped cleanly
86#[macro_export]
87macro_rules! die {
88    ($template:expr $(, $arg:expr)*) => {{
89        $crate::restore_terminal_state(&mut ::std::io::stdout());
90        panic!($template $(, $arg)*)
91    }};
92
93}
94
95/// Restore the terminal state to what we had originally before starting our UI.
96pub(crate) fn restore_terminal_state(so: &mut impl Write) {
97    disable_alternate_screen(so);
98    disable_mouse_support(so);
99    disable_bracketed_paste(so);
100    let t = match ORIGINAL_TERMIOS.get() {
101        Some(t) => t,
102        None => return,
103    };
104    set_termios(*t);
105}