zsh/ported/zle/
textobjects.rs1use crate::ported::zle::zle_h::{MOD_MULT, MOD_TMULT, MOD_VIBUF, MOD_VIAPP, MOD_NEG, MOD_NULL, MOD_CHAR, MOD_LINE, MOD_PRI, MOD_CLIP, MOD_OSSEL};
7
8#[allow(unused_imports)]
13#[allow(unused_imports)]
14use crate::ported::zle::zle_main::*;
15#[allow(unused_imports)]
16use crate::ported::zle::zle_misc::*;
17#[allow(unused_imports)]
18use crate::ported::zle::zle_hist::*;
19#[allow(unused_imports)]
20use crate::ported::zle::zle_move::*;
21#[allow(unused_imports)]
22use crate::ported::zle::zle_word::*;
23#[allow(unused_imports)]
24use crate::ported::zle::zle_params::*;
25#[allow(unused_imports)]
26use crate::ported::zle::zle_vi::*;
27#[allow(unused_imports)]
28use crate::ported::zle::zle_utils::*;
29#[allow(unused_imports)]
30use crate::ported::zle::zle_refresh::*;
31#[allow(unused_imports)]
32use crate::ported::zle::zle_tricky::*;
33#[allow(unused_imports)]
34use crate::ported::zle::deltochar::*;
35
36pub fn blankwordclass(x: char) -> i32 { if x == ' ' || x == '\t' { 0 } else { 1 } }
40
41pub fn selectword() -> i32 { let mut n: i32 = if crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags & MOD_MULT != 0 { crate::ported::zle::zle_main::ZMOD.lock().unwrap().mult
60 } else {
61 1
62 };
63 let widget = crate::ported::zle::zle_main::BINDK.lock().unwrap().as_ref().map(|t| t.nam.clone()).unwrap_or_default();
64 let widget = widget.as_str();
65 let is_aword = widget == "select-a-word";
66 let is_inword = widget == "select-in-word";
67 let is_ablankword = widget == "select-a-blank-word";
68 let mut all: i32 = (is_aword || is_ablankword) as i32; let viclass: fn(char) -> i32 = if is_aword || is_inword {
70 crate::ported::zle::zle_word::wordclass } else {
72 blankwordclass
73 };
74 if crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) == 0 {
75 return 1;
76 }
77 let cur = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)).copied().unwrap_or('\n');
78 let mut sclass: i32 = viclass(cur); let mut doblanks: i32 = all & ((sclass != 0) as i32); let region_active = crate::ported::zle::zle_main::REGION_ACTIVE.load(std::sync::atomic::Ordering::SeqCst) != 0; if !region_active || crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) == crate::ported::zle::zle_main::MARK.load(std::sync::atomic::Ordering::SeqCst) { crate::ported::zle::zle_main::MARK.store(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst), std::sync::atomic::Ordering::SeqCst); while crate::ported::zle::zle_main::MARK.load(std::sync::atomic::Ordering::SeqCst) != 0 { let pos = crate::ported::zle::zle_main::MARK.load(std::sync::atomic::Ordering::SeqCst) - 1; let cp = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(pos).copied().unwrap_or('\n');
94 if cp == '\n' || viclass(cp) != sclass { break; }
97 crate::ported::zle::zle_main::MARK.store(pos, std::sync::atomic::Ordering::SeqCst); }
99 while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) { crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); let mut pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst); if all != 0 && sclass == 0 && pos < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) && crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(pos).copied() == Some('\n')
106 {
107 pos += 1; }
109 let pc = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(pos).copied().unwrap_or('\n');
110 if pc == '\n' || viclass(pc) != sclass { break; }
113 }
114
115 if all != 0 { let cc = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)).copied().unwrap_or('\n');
117 let nclass = viclass(cc); if nclass == 0 || sclass == 0 { while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) { crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); let cc = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)).copied().unwrap_or('\n');
124 if cc == '\n' || viclass(cc) != nclass { break; }
127 }
128 if n < 2 { doblanks = 0; }
131 }
132 }
133 } else { if crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > crate::ported::zle::zle_main::MARK.load(std::sync::atomic::Ordering::SeqCst) { if crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) { crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); }
140 } else if crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != 0 { crate::ported::zle::zle_main::ZLECS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst); }
143 if crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) < crate::ported::zle::zle_main::MARK.load(std::sync::atomic::Ordering::SeqCst) { while {
147 let cont = n > 0;
148 n -= 1;
149 cont
150 } { let mut pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst); let zc_pos = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(pos).copied().unwrap_or('\n');
153 if all != 0 && (viclass(zc_pos) == 0 || zc_pos == '\n') { all = 0; while pos != 0 { pos -= 1; let pc = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(pos).copied().unwrap_or('\n');
159 if pc == '\n' { break; }
162 crate::ported::zle::zle_main::ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst); if viclass(pc) != 0 { break; }
166 }
167 } else if crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != 0
168 && crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)).copied() == Some('\n')
169 { pos -= 1; let pc = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(pos).copied().unwrap_or('\n');
173 if pc != '\n' { crate::ported::zle::zle_main::ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst); }
176 }
177 pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst); let cur = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)).copied().unwrap_or('\n');
179 sclass = viclass(cur); loop { let pc = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(pos).copied().unwrap_or('\n');
183 if pc == '\n' || viclass(pc) != sclass {
184 break;
185 }
186 crate::ported::zle::zle_main::ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst); if pos == 0 { crate::ported::zle::zle_main::ZLECS.store(0, std::sync::atomic::Ordering::SeqCst); break; }
191 pos -= 1; }
193 if all != 0 && crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != 0 { pos = crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst);
196 pos -= 1; let pc = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(pos).copied().unwrap_or('\n');
198 if viclass(pc) == 0 { while pos != 0 { pos -= 1; let pc = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(pos).copied().unwrap_or('\n');
202 if pc == '\n' || viclass(pc) != 0 { break; }
205 crate::ported::zle::zle_main::ZLECS.store(pos, std::sync::atomic::Ordering::SeqCst); }
207 }
208 }
209 }
210 return 0; }
212 n += 1; doblanks = 0; }
215 crate::ported::zle::zle_main::REGION_ACTIVE.store(if region_active { 1 } else { 0 }, std::sync::atomic::Ordering::SeqCst);
217
218 while {
220 n -= 1;
221 n > 0
222 } { if crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst)
224 && crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)).copied() == Some('\n')
225 { crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); }
228 let cur = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)).copied().unwrap_or('\n');
229 sclass = viclass(cur); while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) { crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); let cc = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)).copied().unwrap_or('\n');
233 if cc == '\n' || viclass(cc) != sclass { break; }
236 }
237 if all != 0 { if crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst)
240 && crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)).copied() == Some('\n')
241 { crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); }
244 let cc = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)).copied().unwrap_or('\n');
245 let cls_here = viclass(cc);
246 if sclass == 0 || cls_here == 0 { sclass = cls_here; if n == 1 && sclass == 0 { doblanks = 0; }
251 while crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) < crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) { crate::ported::zle::zle_main::ZLECS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); let cc = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst)).copied().unwrap_or('\n');
254 if cc == '\n' || viclass(cc) != sclass { break; }
257 }
258 }
259 }
260 }
261
262 if doblanks != 0 { let mut pos = crate::ported::zle::zle_main::MARK.load(std::sync::atomic::Ordering::SeqCst); while pos != 0 { pos -= 1; let pc = crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(pos).copied().unwrap_or('\n');
269 if pc == '\n' { break; }
272 if !(pc == ' ' || pc == '\t') { pos += 1; crate::ported::zle::zle_main::MARK.store(pos, std::sync::atomic::Ordering::SeqCst); break; }
277 }
278 }
279 let virangeflag = crate::ported::zle::zle_vi::VIRANGEFLAG
288 .load(std::sync::atomic::Ordering::Relaxed) != 0;
289 if !virangeflag { if !in_vi_cmd_mode() { crate::ported::zle::zle_main::REGION_ACTIVE.store(1, std::sync::atomic::Ordering::SeqCst); } else if crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) != 0 && crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > crate::ported::zle::zle_main::MARK.load(std::sync::atomic::Ordering::SeqCst) { crate::ported::zle::zle_main::ZLECS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst); }
295 }
296
297 0 }
299
300pub fn selectargument() -> i32 { let n: i32 = if crate::ported::zle::zle_main::ZMOD.lock().unwrap().flags & MOD_MULT != 0 {
312 crate::ported::zle::zle_main::ZMOD.lock().unwrap().mult } else {
314 1
315 };
316 if n < 1 || (2 * n as usize) > crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst) + 1 { return 1;
318 }
319 if !in_vi_cmd_mode() { crate::ported::zle::zle_main::REGION_ACTIVE.store(1, std::sync::atomic::Ordering::SeqCst); crate::ported::zle::zle_main::MARK.store(crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst), std::sync::atomic::Ordering::SeqCst); }
323 let mut starts: Vec<usize> = Vec::with_capacity(n as usize);
326 let mut in_word = false;
327 let mut word_start = 0usize;
328 starts.push(0);
329 for (i, &c) in crate::ported::zle::zle_main::ZLELINE.lock().unwrap().iter().enumerate() {
330 if c.is_whitespace() {
331 if in_word {
332 in_word = false;
333 if starts.len() < n as usize { starts.push(i + 1); }
334 }
335 } else if !in_word {
336 in_word = true;
337 word_start = i;
338 if i >= crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) { break; }
339 }
340 }
341 let arg_idx = (n - 1) as usize;
342 let s = starts.get(arg_idx).copied().unwrap_or(word_start);
343 let e = (s..crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst))
344 .find(|&i| crate::ported::zle::zle_main::ZLELINE.lock().unwrap().get(i).copied().map_or(true, |c| c.is_whitespace()))
345 .unwrap_or(crate::ported::zle::zle_main::ZLELL.load(std::sync::atomic::Ordering::SeqCst));
346 crate::ported::zle::zle_main::MARK.store(s, std::sync::atomic::Ordering::SeqCst);
347 crate::ported::zle::zle_main::ZLECS.store(e, std::sync::atomic::Ordering::SeqCst);
348 if in_vi_cmd_mode() && crate::ported::zle::zle_main::ZLECS.load(std::sync::atomic::Ordering::SeqCst) > 0 {
349 crate::ported::zle::zle_main::ZLECS.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
350 }
351 0
352}