joestar

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 15742e666c3c06844d737db469a10a15d86bc410
parent 5605fa5b2645d08b3feb3f2c8c2315b5d61784ea
Author: Ryan Jeffrey <ryan@ryanmj.xyz>
Date:   Sat,  2 Jan 2021 20:17:43 -0800

Merge branch 'dev' of https://github.com/Ma11ock/joestar-old into dev

Diffstat:
Mjoestar.el | 152+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
1 file changed, 102 insertions(+), 50 deletions(-)

diff --git a/joestar.el b/joestar.el @@ -57,24 +57,24 @@ (make-variable-buffer-local 'joe-last-emacs-point) ;;; set mark variables -(defvar joe-mark-0 nil "Mark 0.") -(defvar joe-mark-1 nil "Mark 1.") -(defvar joe-mark-2 nil "Mark 2.") -(defvar joe-mark-3 nil "Mark 3.") -(defvar joe-mark-4 nil "Mark 4.") -(defvar joe-mark-5 nil "Mark 5.") -(defvar joe-mark-6 nil "Mark 6.") -(defvar joe-mark-7 nil "Mark 7.") -(defvar joe-mark-8 nil "Mark 8.") -(defvar joe-mark-9 nil "Mark 9.") +(defvar-local joe-mark-0 nil "Mark 0.") +(defvar-local joe-mark-1 nil "Mark 1.") +(defvar-local joe-mark-2 nil "Mark 2.") +(defvar-local joe-mark-3 nil "Mark 3.") +(defvar-local joe-mark-4 nil "Mark 4.") +(defvar-local joe-mark-5 nil "Mark 5.") +(defvar-local joe-mark-6 nil "Mark 6.") +(defvar-local joe-mark-7 nil "Mark 7.") +(defvar-local joe-mark-8 nil "Mark 8.") +(defvar-local joe-mark-9 nil "Mark 9.") -(defvar joe-lastmark nil "The last mark id.") -(defvar joe-nextmark nil "The next mark id.") +(defvar-local joe-lastmark nil "The last mark id.") +(defvar-local joe-nextmark nil "The next mark id.") -(defvar joe-marklist nil "List of the currently used marks.") +(defvar-local joe-marklist nil "List of the currently used marks.") -(defvar joe-prev-search nil "The last searched-for item.") -(defvar joe-prev-search-action nil "The last search action.") +(defvar-local joe-prev-search nil "The last searched-for item.") +(defvar-local joe-prev-search-action nil "The last search action.") (defvar joe-macro-0 nil "Macro 0.") (defvar joe-macro-1 nil "Macro 1.") @@ -87,56 +87,108 @@ (defvar joe-macro-8 nil "Macro 8.") (defvar joe-macro-9 nil "Macro 9.") - -(make-variable-buffer-local 'joe-mark-0) -(make-variable-buffer-local 'joe-mark-1) -(make-variable-buffer-local 'joe-mark-2) -(make-variable-buffer-local 'joe-mark-3) -(make-variable-buffer-local 'joe-mark-4) -(make-variable-buffer-local 'joe-mark-5) -(make-variable-buffer-local 'joe-mark-6) -(make-variable-buffer-local 'joe-mark-7) -(make-variable-buffer-local 'joe-mark-8) -(make-variable-buffer-local 'joe-mark-9) - -(make-variable-buffer-local 'joe-lastmark) -(make-variable-buffer-local 'joe-nextmark) - -(make-variable-buffer-local 'joe-prev-search) -(make-variable-buffer-local 'joe-prev-search-action) - ;; non-interactive helper functions ;; these functions exist to break-up functionality and do not necessarily ;; correspond to any function in Joe. -; TODO fix bug where it does not keep the value of joe-prev-search beyond one repitition -(defun joe-get-findstr (prompt) - "If PROMPT is t ask user for search words. If PROMPT is nil return joe-prev-search." - (setq joe-prev-search (if (null joe-prev-search) - (read-string "Find: ") - (if prompt - (read-string (format "Find [%s]: " joe-prev-search)) - joe-prev-search)))) +(defun joe-get-findstr () + "Ask user for search words." + (setq joe-prev-search (if (null joe-prev-search) + (read-string "Find: ") + (let ((new-search (read-string (format "Find [%s]: " joe-prev-search)))) + (if (or (null new-search) (string= new-search "")) + joe-prev-search + new-search))))) + +; TODO fix bug where unto-tree undoes too many things. +(defun joe-replace (str2 str1 pos &optional noask back prev-edits) + "Prompt user, replace STR1 with STR2 at POS. +If NOASK is set just replace string without asking the user. +If BACK is t then move backwards, if nil then forwards. +PREV-EDITS is a list of where previous edits occurred." + (when str1 + (hlt-highlight-region (- (point) (length str1)) (point) 'highlight) + (let ((reg-min (- (point) (length str1))) + (result (if noask + ?Y + (upcase (read-key "Replace (Y)es (N)o (R)est (B)ackup?")))) + (point-at-begin (+ (point) (length str1))) + (next-call (if back + (lambda () + (search-backward str1) + (search-forward str1)) + (lambda () + (search-forward str1))))) + + (cond ((= result ?Y) (progn + (hlt-unhighlight-region reg-min (point) 'highlight) + (kill-region (point) reg-min) + (insert str2) + (joe-replace str2 str1 (funcall next-call) noask back (cons point-at-begin prev-edits)))) + + ((= result ?N) (progn + (hlt-unhighlight-region reg-min (point) 'highlight) + (joe-replace str2 str1 (funcall next-call) noask back (cons nil prev-edits)))) + + ((= result ?B) ;; Undo the previous edits. + (let ((last-edit (if (car prev-edits) + (car prev-edits) + (if back + (point-max) + (point-min))))) + (hlt-unhighlight-region reg-min (point) 'highlight) + (if back + (if (< last-edit (point)) + nil + nil) + (goto-char (- (point) (length str1))) + (search-backward str1) + (if (> last-edit (point)) + (progn + (goto-char last-edit) + (kill-region (point) (- (point) (length str2))) + (insert str1)) + (goto-char (+ (point) (length str1))))) + (joe-replace str2 str1 (point) noask back (cdr prev-edits)))) + + ((= result ?R) (progn + (hlt-unhighlight-region reg-min (point) 'highlight) + (joe-replace str2 str1 (funcall next-call) t back prev-edits))) + (t (message (format "%c" result)) + (hlt-unhighlight-region reg-min (point) 'highlight)))))) + (defun joe-get-find-action (prompt) "If PROMPT as the user for an action. Otherwise, return previous action." (setq joe-prev-search-action (if (or prompt (null joe-prev-search)) - (read-string "(I)gnore (R)eplace (B)ackwards Bloc(K): ") + (upcase + (read-string + "(I)gnore (R)eplace (B)ackwards Bloc(K): ")) joe-prev-search))) (defun joe-find-do (action str) "Perform find ACTION on STR." - (cond ((string= action "R") ; replace - (joe-replace str (read-string "Replace with: "))) - ((string= action "B") ; search backward + (cond ((string-match-p "R" action) ; replace + (if (string-match-p "B" action) + (joe-replace (read-string "Replace with: ") + str + (progn + (search-backward str) + (search-forward str)) + nil + t) + (joe-replace (read-string "Replace with: ") + str + (search-forward str)))) + ((string= action "B") ; search backward (search-backward str)) - ((string= action "K") ; search block + ((string= action "K") ; search block TODO (progn (call-interactively 'narrow-to-region) (goto-char (point-min)) (search-forward str) (widen))) - ((search-forward str)))) ; default, search forward + (t (search-forward str)))) ; default, search forward (defun joe-shift-region (distance) "Shift the region DISTANCE number of whitespace." @@ -777,18 +829,18 @@ Move mark to joestar's end of block and move point to joestar's end of block." (defun joe-qrepl (str) "Search and replace STR." - (interactive (list (joe-get-findstr t))) + (interactive (list (joe-get-findstr))) (joe-find-do joe-prev-search-action str)) (defun joe-fnext (str action) "Repeat previous search on STR and perform previous ACTION." - (interactive (list (joe-get-findstr nil) (joe-get-find-action nil))) + (interactive (list joe-prev-search (joe-get-find-action nil))) (joe-find-do action str)) ; TODO does not replace yet (defun joe-ffirst (str action) "Find next STR, perform ACTION." - (interactive (list (joe-get-findstr t) (joe-get-find-action t))) + (interactive (list (joe-get-findstr) (joe-get-find-action t))) (joe-find-do action str)) (defun joe-stat ()