2

I'm wondering if anyone knows of a way to have a function (that I can bind to a key) that pastes the current text in the clipboard as a formatted org table?

Also as a bonus (since this is used to create as ingredients table for a cookbook) it would be great to be able to define the columns in advance so that the amount|type|ingredients are nicely separated. For example take this list

3/4 cup milk
2 tablespoons white vinegar 
1 cup all-purpose flour
2 tablespoons white sugar
1 teaspoon baking powder
1/2 teaspoon baking soda
1/2 teaspoon salt
1 egg
2 tablespoons butter, melted
cooking spray 

If I mark this as a region and issue org-table-create-or-convert-from-region this is the result

|     3/4 | cup         | milk        |         |      |        |     |       |         |
|       2 | tablespoons | white       | vinegar | (%or | better | yet | lemon | juice%) |
|       1 | cup         | all-purpose | flour   |      |        |     |       |         |
|       2 | tablespoons | white       | sugar   |      |        |     |       |         |
|       1 | teaspoon    | baking      | powder  |      |        |     |       |         |
|     1/2 | teaspoon    | baking      | soda    |      |        |     |       |         |
|     1/2 | teaspoon    | salt        |         |      |        |     |       |         |
|       1 | egg         |             |         |      |        |     |       |         |
|       2 | tablespoons | butter,     | melted  |      |        |     |       |         |
| cooking | spray       |             |         |      |        |     |       |         |

As you can see it got the amount (1/2) and the unit (cup) right yet the actual ingredients is cut into several columns.

I don't really know how to deal with this. Is there a more intelligent way of auto directing the conversion to split it the correct way?

1

You need something that basically parses your recipe. Here is one that seems to do what you want. You highlight the recipe ingredients and run it.

(defun recipe-to-org (start end)
  "Split an ingredient list to a table.
Column 1 is a the quantity, column two is the ingredient.
We split on spaces, then handle these cases:

1. first word starts with a number, and the number of words is two
  1 egg

2. The first word does not start with a number
  cooking spray

3. Everything else.
  2 tablespoons butter, melted
"
  (interactive "r")
  (let* ((lines (split-string (buffer-substring-no-properties start end) "\n"))
     tokens
     (table
      (mapconcat
       'identity (loop for line in lines
               do
               (setq tokens (split-string line))
               collect
               (cond
                ;; 1 egg
                ((and (string-match "^[0-9]" (car tokens))
                  (= 2 (length tokens)))
                 (format "| %s | %s |" (nth 0 tokens) (nth 1 tokens)))
                ;; line not starting with a number
                ((not (string-match "^[0-9]" (car tokens)))
                 (format "| | %s |" line))
                ;; everything else.
                (t
                 (format
                  "| %s | %s"
                  (concat (nth 0 tokens) " " (nth 1 tokens))
                  (mapconcat 'identity (cddr tokens) " ")))))
       "\n")))

    (setf (buffer-substring start end) table)
    (org-table-align)))

This converts your recipe ingredients to:

| 3/4 cup       | milk              |
| 2 tablespoons | white vinegar     |
| 1 cup         | all-purpose flour |
| 2 tablespoons | white sugar       |
| 1 teaspoon    | baking powder     |
| 1/2 teaspoon  | baking soda       |
| 1/2 teaspoon  | salt              |
| 1             | egg               |
| 2 tablespoons | butter, melted    |
|               | cooking spray     |
  • thx @John. i seem to be getting an error and: Wrong type argument: stringp, nil when i try to run the function on a highlighted section – zeltak Mar 17 '17 at 12:51
  • It sounds like you highlighted an empty line, either before or after the region. It should work if you just highlight the region from a start of a line to the end of a line. – John Kitchin Mar 17 '17 at 16:39
  • perfect, sorry for the late response – zeltak Apr 16 '17 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.