Hi Jarmo,

Jarmo Hurri <jarmo.hu...@syk.fi> writes:

> I have nothing against the idea. However, I get only errors when I try
> to apply your functions in my examples. I can track down the source of
> the problems later (not today, though).

There was a typo, I attach the correct patch.

> But how about combining your idea about getting rid of CL's position
> with the following idea of building the docstring on the fly in the
> macro? That is, you do not need to define multiple functions by hand,
> but the docstrings can still be unique.

As you prefer.  But C-h f will not point to org-table.el if we use this
macro.  This is acceptable, but we need to mention `org-lookup' as the
"matrix" function in the docstring, so that user C-h f'ing org-lookup
will find it in org-table.el.

Another wish style-wise: the first sentence of the docstring should be
one line long.  See the short parameters names and the docstrings in my
patch to get an idea -- but please feel free to also follow your taste
here, of course.

Thanks!

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
-- 
 Bastien

Reply via email to