diff --git a/julia-mode-tests.el b/julia-mode-tests.el index 45db3c7..b42237d 100644 --- a/julia-mode-tests.el +++ b/julia-mode-tests.el @@ -1009,6 +1009,49 @@ hello world (string-to-syntax "\\") (syntax-after 13))))) +;;; testing julia-latexsub-or-indent + +(cl-defun julia-test-latexsub-or-indent (from &key (position (1+ (length from))) (greedy t)) + "Utility function to test `julia-latexsub-or-indent'. + +This is how it works: + +1. FROM is inserted in a buffer. + +2. The point is moved to POSITION. + +3. `julia-latexsub-or-indent' is called on the buffer. + +If `julia-latexsub-selector' is called, it selects the first replacement, which is also placed in SELECTION (otherwise it is NIL). + +Return a cons of the + +1. buffer contents + +2. the replacement of SELECTION when not nil. + +The latter can be used to construct test comparisons." + (let* ((selection) + (julia-latexsub-selector + (lambda (replacements) + (setf selection (car replacements)) + selection)) + (julia-latexsub-greedy greedy)) + (cons (with-temp-buffer + (insert from) + (goto-char position) + (julia-latexsub-or-indent t) + (buffer-string)) + (gethash selection julia-mode-latexsubs)))) + +(ert-deftest julia--test-latexsub-or-indent () + (should (equal (julia-test-latexsub-or-indent "\\circ") '("∘"))) + (let ((result (julia-test-latexsub-or-indent "\\circXX" :position 5))) + (should (equal (car result) (concat (cdr result) "cXX")))) + (let ((result (julia-test-latexsub-or-indent "\\circ" :greedy nil))) + (should (equal (car result) (cdr result)))) + (should (equal (julia-test-latexsub-or-indent "\\alpha") '("α")))) + ;;; ;;; run all tests ;;; diff --git a/julia-mode.el b/julia-mode.el index 877b667..e877737 100644 --- a/julia-mode.el +++ b/julia-mode.el @@ -951,16 +951,16 @@ When multiple options match, ask the user to clarify via `julia-latexsub-selecto (let ((partial (buffer-substring-no-properties beg (point)))) (when-let (replacements (gethash partial julia-mode--latexsubs-partials)) (let* ((complete-match (member partial replacements)) - (replacement (cond - ;; complete match w/ greedy - ((and complete-match julia-latexsub-greedy) partial) - ;; multiple replacements, ask user - ((cdr replacements) (funcall julia-latexsub-selector replacements)) - ;; single replacement, pick that - (t (car replacements))))) - (cons beg replacement)))))) - -(defun julia-latexsub-or-indent (arg) + (latex (cond + ;; complete match w/ greedy + ((and complete-match julia-latexsub-greedy) partial) + ;; multiple replacements, ask user + ((cdr replacements) (funcall julia-latexsub-selector replacements)) + ;; single replacement, pick that + (t (car replacements))))) + (cons beg latex)))))) + +(defun julia-latexsub-or-indent (arg) "Either indent according to Julia mode conventions or perform a LaTeX-like symbol substution. When multiple options match, ask the user to clarify via `julia-latexsub-selector', unless there is a complete match and `julia-latexsub-greedy' is `t'. @@ -969,7 +969,7 @@ Presently, this is not the default. Enable with eg (define-key julia-mode-map (kbd \"TAB\") 'julia-latexsub-or-indent) - in your `julia-mode-hook'." +eg in your `julia-mode-hook'." (interactive "*i") (if-let (replacement (julia-mode--latexsub-before-point)) (progn