From 3744f92ad21860ca2fa7e008e3e4311851117ace Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Sun, 2 Mar 2014 11:18:54 -0500 Subject: [PATCH 1/4] avoid double choices reversing Both yas-choose-value the yas-prompt-functions were reversing values (cancelling each other out), instead just keep the choices in order the whole time. --- yasnippet.el | 98 +++++++++++++++++++++------------------------------- 1 file changed, 40 insertions(+), 58 deletions(-) diff --git a/yasnippet.el b/yasnippet.el index 608cccf..f585b88 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -1542,25 +1542,24 @@ Optional PROMPT sets the prompt to use." ;; up as `yas--all-templates' I think. ;; (when (and window-system choices) - (let ((chosen - (let (menu d) ;; d for display - (dolist (c choices) - (setq d (or (and display-fn (funcall display-fn c)) - c)) - (cond ((stringp d) - (push (cons (concat " " d) c) menu)) - ((listp d) - (push (car d) menu)))) - (setq menu (list prompt (push "title" menu))) - (x-popup-menu (if (fboundp 'posn-at-point) - (let ((x-y (posn-x-y (posn-at-point (point))))) - (list (list (+ (car x-y) 10) - (+ (cdr x-y) 20)) - (selected-window))) - t) - menu)))) - (or chosen - (keyboard-quit))))) + (or + (let* ((display-fn (or display-fn #'identity)) + (menu + (list prompt + (cons "title" + (mapcar (lambda (c) + (let ((d (funcall display-fn c))) + (cond ((stringp d) (cons (concat " " d) c)) + ((listp d) (car d))))) + choices))))) + (x-popup-menu (if (fboundp 'posn-at-point) + (let ((x-y (posn-x-y (posn-at-point (point))))) + (list (list (+ (car x-y) 10) + (+ (cdr x-y) 20)) + (selected-window))) + t) + menu)) + (keyboard-quit)))) (defun yas--x-pretty-prompt-templates (prompt templates) "Display TEMPLATES, grouping neatly by table name." @@ -1601,46 +1600,28 @@ Optional PROMPT sets the prompt to use." (defun yas-dropdown-prompt (_prompt choices &optional display-fn) (when (fboundp 'dropdown-list) - (let (formatted-choices - filtered-choices - d - n) - (dolist (choice choices) - (setq d (or (and display-fn (funcall display-fn choice)) - choice)) - (when (stringp d) - (push d formatted-choices) - (push choice filtered-choices))) - - (setq n (and formatted-choices (dropdown-list formatted-choices))) + (let* ((formatted-choices (if display-fn (delete-if-not display-fn choices) + choices)) + (filtered-choices (if display-fn (mapcar display-fn filtered-choices) + choices)) + (n (and formatted-choices + (dropdown-list formatted-choices)))) (if n (nth n filtered-choices) (keyboard-quit))))) (defun yas-completing-prompt (prompt choices &optional display-fn completion-fn) - (let (formatted-choices - filtered-choices - chosen - d - (completion-fn (or completion-fn - #'completing-read))) - (dolist (choice choices) - (setq d (or (and display-fn (funcall display-fn choice)) - choice)) - (when (stringp d) - (push d formatted-choices) - (push choice filtered-choices))) - (setq chosen (and formatted-choices - (funcall completion-fn prompt - formatted-choices - nil - 'require-match - nil - nil))) - (let ((position (or (and chosen - (position chosen formatted-choices :test #'string=)) - 0))) - (nth position filtered-choices)))) + (let* ((formatted-choices (if display-fn (delete-if-not display-fn choices) + choices)) + (filtered-choices (if display-fn (mapcar display-fn filtered-choices) + choices)) + (chosen (and formatted-choices + (funcall (or completion-fn #'completing-read) + prompt formatted-choices + nil 'require-match nil nil))) + (position (and chosen + (position chosen formatted-choices :test #'string=)))) + (nth (or position 0) filtered-choices))) (defun yas-no-prompt (_prompt choices &optional _display-fn) (first choices)) @@ -2808,10 +2789,11 @@ If found, the content of subexp group SUBEXP (default 0) is The last element of POSSIBILITIES may be a list of strings." (unless (or yas-moving-away-p yas-modified-p) - (setq possibilities (nreverse possibilities)) - (setq possibilities (if (listp (car possibilities)) - (append (reverse (car possibilities)) (rest possibilities)) - possibilities)) + (let* ((last-link (last possibilities)) + (last-elem (car last-link))) + (when (listp last-elem) + (setcar last-link (car last-elem)) + (setcdr last-link (cdr last-elem)))) (some #'(lambda (fn) (funcall fn "Choose: " possibilities)) yas-prompt-functions))) From 44701103845fc4ae660e1587748ec661dca3b98d Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Mon, 3 Mar 2014 18:20:47 -0500 Subject: [PATCH 2/4] display-fn isn't actually used for filtering --- yasnippet.el | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/yasnippet.el b/yasnippet.el index f585b88..d21f3cb 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -1600,28 +1600,22 @@ Optional PROMPT sets the prompt to use." (defun yas-dropdown-prompt (_prompt choices &optional display-fn) (when (fboundp 'dropdown-list) - (let* ((formatted-choices (if display-fn (delete-if-not display-fn choices) - choices)) - (filtered-choices (if display-fn (mapcar display-fn filtered-choices) - choices)) - (n (and formatted-choices - (dropdown-list formatted-choices)))) - (if n - (nth n filtered-choices) + (let* ((formatted-choices + (if display-fn (mapcar display-fn choices) choices)) + (n (dropdown-list formatted-choices))) + (if n (nth n choices) (keyboard-quit))))) (defun yas-completing-prompt (prompt choices &optional display-fn completion-fn) - (let* ((formatted-choices (if display-fn (delete-if-not display-fn choices) - choices)) - (filtered-choices (if display-fn (mapcar display-fn filtered-choices) - choices)) - (chosen (and formatted-choices - (funcall (or completion-fn #'completing-read) - prompt formatted-choices - nil 'require-match nil nil))) - (position (and chosen - (position chosen formatted-choices :test #'string=)))) - (nth (or position 0) filtered-choices))) + (let* ((formatted-choices + (if display-fn (mapcar display-fn choices) choices)) + (chosen (funcall (or completion-fn #'completing-read) + prompt formatted-choices + nil 'require-match nil nil))) + (if (eq choices formatted-choices) + chosen + (nth (or (position chosen formatted-choices :test #'string=) 0) + choices)))) (defun yas-no-prompt (_prompt choices &optional _display-fn) (first choices)) From c07db054ecac82fbb8353240648e8454e734d166 Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Tue, 4 Mar 2014 21:46:05 -0500 Subject: [PATCH 3/4] yas-x-prompt: remove dead code --- yasnippet.el | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/yasnippet.el b/yasnippet.el index d21f3cb..62a1d8f 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -1543,22 +1543,17 @@ Optional PROMPT sets the prompt to use." ;; (when (and window-system choices) (or - (let* ((display-fn (or display-fn #'identity)) - (menu - (list prompt - (cons "title" - (mapcar (lambda (c) - (let ((d (funcall display-fn c))) - (cond ((stringp d) (cons (concat " " d) c)) - ((listp d) (car d))))) - choices))))) - (x-popup-menu (if (fboundp 'posn-at-point) - (let ((x-y (posn-x-y (posn-at-point (point))))) - (list (list (+ (car x-y) 10) - (+ (cdr x-y) 20)) - (selected-window))) - t) - menu)) + (x-popup-menu + (if (fboundp 'posn-at-point) + (let ((x-y (posn-x-y (posn-at-point (point))))) + (list (list (+ (car x-y) 10) + (+ (cdr x-y) 20)) + (selected-window))) + t) + `(,prompt ("title" + ,@(mapcar* (lambda (c d) `(,(concat " " d) . ,c)) + choices + (if display-fn (mapcar display-fn choices) choices))))) (keyboard-quit)))) (defun yas--x-pretty-prompt-templates (prompt templates) From 43a501aa567d5507e9797c30ab7dc98a13565d4e Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Wed, 5 Mar 2014 22:38:46 -0500 Subject: [PATCH 4/4] remove yas--x-pretty-prompt-templates --- yasnippet.el | 51 +++++++-------------------------------------------- 1 file changed, 7 insertions(+), 44 deletions(-) diff --git a/yasnippet.el b/yasnippet.el index 62a1d8f..1a87e39 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -1489,10 +1489,6 @@ Here's a list of currently recognized directives: ;;; Popping up for keys and templates -(defvar yas--x-pretty-prompt-templates nil - "If non-nil, attempt to prompt for templates like TextMate.") - - (defun yas--prompt-for-template (templates &optional prompt) "Interactively choose a template from the list TEMPLATES. @@ -1504,13 +1500,11 @@ Optional PROMPT sets the prompt to use." (sort templates #'(lambda (t1 t2) (< (length (yas--template-name t1)) (length (yas--template-name t2)))))) - (if yas--x-pretty-prompt-templates - (yas--x-pretty-prompt-templates "Choose a snippet" templates) - (some #'(lambda (fn) - (funcall fn (or prompt "Choose a snippet: ") - templates - #'yas--template-name)) - yas-prompt-functions)))) + (some #'(lambda (fn) + (funcall fn (or prompt "Choose a snippet: ") + templates + #'yas--template-name)) + yas-prompt-functions))) (defun yas--prompt-for-keys (keys &optional prompt) "Interactively choose a template key from the list KEYS. @@ -1556,37 +1550,6 @@ Optional PROMPT sets the prompt to use." (if display-fn (mapcar display-fn choices) choices))))) (keyboard-quit)))) -(defun yas--x-pretty-prompt-templates (prompt templates) - "Display TEMPLATES, grouping neatly by table name." - (let ((organized (make-hash-table :test #'equal)) - menu - more-than-one-table - prefix) - (dolist (tl templates) - (puthash (yas--template-table tl) - (cons tl - (gethash (yas--template-table tl) organized)) - organized)) - (setq more-than-one-table (> (hash-table-count organized) 1)) - (setq prefix (if more-than-one-table - " " "")) - (if more-than-one-table - (maphash #'(lambda (table templates) - (push (yas--table-name table) menu) - (dolist (tl templates) - (push (cons (concat prefix (yas--template-name tl)) tl) menu))) organized) - (setq menu (mapcar #'(lambda (tl) (cons (concat prefix (yas--template-name tl)) tl)) templates))) - - (setq menu (nreverse menu)) - (or (x-popup-menu (if (fboundp 'posn-at-point) - (let ((x-y (posn-x-y (posn-at-point (point))))) - (list (list (+ (car x-y) 10) - (+ (cdr x-y) 20)) - (selected-window))) - t) - (list prompt (push "title" menu))) - (keyboard-quit)))) - (defun yas-ido-prompt (prompt choices &optional display-fn) (when (and (fboundp 'ido-completing-read) (or (>= emacs-major-version 24) @@ -2224,8 +2187,8 @@ Prompt the user if TEMPLATES has more than one element, else expand immediately. Common gateway for `yas-expand-from-trigger-key' and `yas-expand-from-keymap'." (let ((yas--current-template (or (and (rest templates) ;; more than one - (yas--prompt-for-template (mapcar #'cdr templates))) - (cdar templates)))) + (yas--prompt-for-template (mapcar #'cdr templates))) + (cdar templates)))) (when yas--current-template (yas-expand-snippet (yas--template-content yas--current-template) start