diff --git a/lisp/org-table.el b/lisp/org-table.el index 37889af..95b8231 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -4826,6 +4826,29 @@ list of the fields in the rectangle ." (org-table-get-range (match-string 0 form) tbeg 1)) form))))))))) +(defun org-lookup (val s-list r-list lastp &optional predicate) + "Look for VAL in S-LIST and return the corresponding element in R-LIST. +If LASTP, ignore all matching VAL in S-LIST except the last one. +If PREDICATE is not nil, use this instead of `equal' to match VAL." + (let ((p (or predicate 'equal)) (c 0) r) + (nth (dolist (i s-list r) (setq c (1+ c)) + (if (and (funcall p val i) (or lastp (not r))) + (setq r (1- c)))) + return-list))) + +(defun org-lookup-first (val s-list r-list &optional predicate) + "Look for VAL in S-LIST and return the corresponding element in R-LIST. +If PREDICATE is not nil, use this instead of `equal' to match VAL." + (org-lookup val s-list r-list nil predicate)) + +(defun org-lookup-last (val s-list r-list &optional predicate) + "Look for VAL in S-LIST and return the corresponding element in R-LIST. +If PREDICATE is not nil, use this instead of `equal' to match VAL." + (org-lookup val s-list r-list t predicate)) + +;; (org-lookup-first 2 '(1 2 3 2) '(A B C D E)) => B +;; (org-lookup-last 2 '(1 2 3 2) '(A B C D E)) => D + (provide 'org-table) ;;; org-table.el ends here