branch: elpa/helm
commit a0ad597efd6f310fd0bf74c8d05db2fd745707b5
Author: Thierry Volpiatto <[email protected]>
Commit: Thierry Volpiatto <[email protected]>

    Add helm-add-to-list
---
 helm-lib.el | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/helm-lib.el b/helm-lib.el
index 78090c7b70..2f378e7306 100644
--- a/helm-lib.el
+++ b/helm-lib.el
@@ -1068,6 +1068,31 @@ Examples:
            (beg-part (butlast seq len)))
       (append beg-part elm end-part))))
 
+;;;###autoload
+(defun helm-add-to-list (var elm index &optional replace)
+  "Add or move ELM to the value of VAR at INDEX unless already here.
+
+If ELM is member of var value and at index INDEX, return var value
+unchanged, if INDEX value is different move ELM at this `nth' INDEX value.
+If ELM is not present in list add it at `nth' INDEX.
+
+If REPLACE is non nil replace element at INDEX by ELM.
+
+Do not use this function in helm code, use `helm-append-at-nth'
+instead.  It is meant to be used in config files only."
+  (cl-assert (boundp var) nil "Unbound variable `%s'" var)
+  (let ((val (symbol-value var))
+        flag)
+    (cond ((and (member elm val) (equal elm (nth index val))))
+          ((member elm val)
+           (setq val (delete elm val) flag t))
+          (replace
+           (setq val (delete (nth index val) val) flag t))
+          (t (setq flag t)))
+    (if flag
+        (set var (helm-append-at-nth val elm index))
+      val)))
+
 (cl-defgeneric helm-take (seq n)
   "Return the first N elements of SEQ if SEQ is longer than N.
 It is used for narrowing list of candidates to the

Reply via email to