;文字前后括号处理[mini_khcl].lsp
;此功能可极大拓展
(defun c:mini_khcl ( / old_cmd del_space e cen ss sset_redo unctr add lst elst
						khcl_Undo khcl_redo del_one del_all match_lst kuohao_lst sset_undo add_txt
					)

(defun khcl_Undo ()
	(if (cadr sset_undo)
		(progn
			(mai_undo_end)
			; (mai_undo_times 1)
			(setq
				sset_redo (cons ss sset_redo)
				ss (cadr sset_undo)
				sset_undo (cddr sset_undo)
				unctr (1- unctr)
			)
			(mapcar '(lambda(x)(entmod (cadr x))) ss)
		)
	)
)

(defun khcl_redo ()
	(if sset_redo
		(progn
			(mai_undo_end)
			; (mai_undo_times 1)
			(setq
				ss (car sset_redo)
				sset_redo (cdr sset_redo)
				unctr (1+ unctr)
			)
			(mapcar '(lambda(x)(entmod (cadr x))) ss)
		)
	)
)



(defun del_space ( tt )
	; (setq tt (vl-string-trim " \t" tt))
	(setq tt (reverse (mai_str2lst tt "")))
	(while (member (car tt) (list " " "　" "\t"))
		(setq tt (cdr tt))
	)
	(setq tt (reverse tt))
	(while (member (car tt) (list " " "　" "\t"))
		(setq tt (cdr tt))
	)
	(apply 'strcat tt)
)



(defun del_one ( tx lst / tt chk )
	(setq tt (reverse (mai_str2lst tx "")))
	(while (member (car tt) (list " " "　" "\t"))
		(setq tt (cdr tt))
	)
	(setq tt (reverse tt))
	(while (member (car tt) (list " " "　" "\t"))
		(setq tt (cdr tt))
	)
	(if (vl-every 'eq (list (car tt) (last tt)) lst)
		(setq tt (mai_rcdr (cdr tt)) chk T)
	)
	(if chk (apply 'strcat tt) tx)
)

(defun del_all ( tt )
	(setq tt (reverse (mai_str2lst tt "")))
	(while (member (car tt) (list " " "　" "\t"))
		(setq tt (cdr tt))
	)
	(setq tt (reverse tt))
	(while (member (car tt) (list " " "　" "\t"))
		(setq tt (cdr tt))
	)
	(while (member (list (car tt) (last tt)) match_lst)
		(setq tt (mai_rcdr (cdr tt)))
	)
	(apply 'strcat tt)
)

	(mai_start nil)
	(mai_last_com "mini_khcl")
	(setq
		unctr 0
		ss (mai_select_first
				'((0 . "*text"))
				"选择要前后括号处理文本:<退出>"
				"没有找到单行文本."
			)
		ss (mai_ssget_2_list ss)
		ss (mapcar '(lambda( x / y )(setq y (entget x)) (list (mai_dxf 1 y) y x)) ss)
		match_lst
				(list
					(list "(" ")")
					(list "[" "]")
					(list "{" "}")
					(list "<" ">")
					(list "（" "）")
					(list "【" "】")
					(list "｛" "｝")
					(list "《" "》")
				)
		add T
	)
	(mai_undo_end)
	(while add
		(mai_undo_start)
		(setq
			unctr (1+ unctr)
			sset_undo (cons ss sset_undo)
			add nil
			kuohao_lst
				(list "文字前后括号处理"
					(list
						"英文括号"
						(list "删除 ( ~ )" "(setq add 1 lst (car match_lst))")
						(list "删除 [ ~ ]" "(setq add 1 lst (cadr match_lst))")
						(list "删除 { ~ }" "(setq add 1 lst (caddr match_lst))")
						(list "删除 < ~ >" "(setq add 1 lst (cadddr match_lst))")
						nil
						(list "中文括号")
						(list "删除 （ ~ ）" "(setq add 1 lst (nth 4 match_lst))")
						(list "删除 【 ~ 】" "(setq add 1 lst (nth 5 match_lst))")
						(list "删除 ｛ ~ ｝" "(setq add 1 lst (nth 6 match_lst))")
						(list "删除 《 ~ 》" "(setq add 1 lst (nth 7 match_lst))")
						nil
						(list "删全部前后括号" "(setq add 3)")
						(list "删除前后空格" "(setq add 4)")
						(list "删除所有空格" "(setq add 16)")
					)
					(list
						"英文括号"
						(list "增加 ( ~ )" "(setq add 2 lst (car match_lst))")
						(list "增加 [ ~ ]" "(setq add 2 lst (cadr match_lst))")
						(list "增加 { ~ }" "(setq add 2 lst (caddr match_lst))")
						(list "增加 < ~ >" "(setq add 2 lst (cadddr match_lst))")
						nil
						(list "中文括号")
						(list "增加 （ ~ ）" "(setq add 2 lst (nth 4 match_lst))")
						(list "增加 【 ~ 】" "(setq add 2 lst (nth 5 match_lst))")
						(list "增加 ｛ ~ ｝" "(setq add 2 lst (nth 6 match_lst))")
						(list "增加 《 ~ 》" "(setq add 2 lst (nth 7 match_lst))")
						nil
						(list "去除文本中格式符" "(setq add 15)")
						nil
						(list "删除输入的字符" "(setq add 17)")
					)
					(list
						"其他功能"
						(list "删前面一个字符" "(setq add 5)")
						(list "删后面一个字符" "(setq add 6)")
						nil
						(list "增加前缀" "(setq add 7)")
						(list "增加后缀" "(setq add 8)")
						nil
						(list "删除特殊符号前" "(setq add 11)")
						(list "删除特殊符号后" "(setq add 12)")
						nil
						(list "全改大写" "(setq add 13)")
						(list "全改小写" "(setq add 14)")
						nil
						(if (> unctr 1) (list "撤销一次" "(setq add 9)") (list "撤销一次"))
						(if sset_redo (list "重做一次" "(setq add 10)") (list "重做一次"))
					)
				)
		)
		(mai_make_panel_up kuohao_lst)
		(or mai_khcl_qianzui (setq mai_khcl_qianzui "1-"))
		(if (member add (list 7 8))
			(setq
				add_txt (getstring (strcat "\n输入需要批量增加的【" (if (= add 7) "前" "后") "】缀文本:<" mai_khcl_qianzui ">"))
				mai_khcl_qianzui (cond ((= add_txt "") mai_khcl_qianzui) (add_txt))
			)
		)
		(or mai_khcl_delsb (setq mai_khcl_delsb "$"))
		(if (member add (list 11 12))
			(setq
				add_txt (getstring (strcat "\n输入需要批量删除符号【" (if (= add 11) "前" "后") "】面部份的特殊符号:<" mai_khcl_delsb ">"))
				mai_khcl_delsb (cond ((= add_txt "") mai_khcl_delsb) (add_txt))
			)
		)
		
		 ;有新动作后停止再做一次功能
		(and add
			(or (< 0 add 9) (< 10 add 18))
			(setq sset_redo nil)
		)
		
		(cond
			(	(= add 9) (khcl_Undo))
			(	(= add 10) (khcl_redo))
			(	(and (member add (list 7 8)) (= "" add_txt)))
			(	add
				(setq
					ss (mapcar
							'(lambda( x / txt tmp )
								(setq txt (car x))
								(cond 
									(	(= add 2) (setq txt (strcat (car lst) txt (cadr lst))))
									(	(= add 1) (setq txt (del_one txt lst)))
									(	(= add 3) (setq txt (del_all txt)))
									(	(= add 4) (setq txt (del_space txt)))
									(	(= add 5) (setq txt (apply 'strcat (cdr (mai_str2lst txt "")))))
									(	(= add 6) (setq txt (apply 'strcat (mai_rcdr (mai_str2lst txt "")))))
									(	(= add 7) (setq txt (strcat mai_khcl_qianzui txt)))
									(	(= add 8) (setq txt (strcat txt mai_khcl_qianzui)))
									(	(= add 11)
										(setq txt
												(if (wcmatch txt (strcat "*" mai_khcl_delsb "*"))
													(mai_lst2str (cdr (mai_str2lst2 txt mai_khcl_qianzui)) mai_khcl_qianzui)
													txt
												)
										)
									)
									(	(= add 12)
										(setq txt
												(if (wcmatch txt (strcat "*" mai_khcl_delsb "*"))
													(mai_lst2str (mai_rcdr (mai_str2lst2 txt mai_khcl_qianzui)) mai_khcl_qianzui)
													txt
												)
										)
									)
									(	(= add 13) (setq txt (strcase txt)))
									(	(= add 14) (setq txt (strcase txt T)))
									(	(= add 15)
										(setq txt (mai_clear_mtformat (mai_string_del txt (list "%%U" "%%u" "%%O" "%%o"))))
									)
									(	(= add 16)
										(setq txt (mai_string_del txt (list " " "　" "\t")))
									)
									(	(= add 17)
										(or mai_panel_del_string (setq mai_panel_del_string (list "")))
										(if (setq tmp (mai_txtedit (mai_lst2str mai_panel_del_string " ") "输入要删除的字符，空格分隔:"))
											(setq
												tmp (mai_str2lst tmp " ")
												tmp (if (member "" tmp)
														(vl-list* " " "　" "\t" (vl-remove "" tmp))
														tmp
													)
												mai_panel_del_string tmp
												txt (mai_string_del txt mai_panel_del_string)
											)
										)
									)
								)
								(setq
									elst (cadr x)
									elst (subst (cons 1 txt) (assoc 1 elst) elst)
								)
								(entmod elst)
								(list txt elst (caddr x))
							)
							ss
						)
				)
			)
		)
		(mai_undo_end)
	)
	(mai_undo_start)
	(mai_end)
)
