0

I'm creating a function to easily insert ditaa boxes while drafting reports in org-mode. I have the meat of the function accomplished, but not sure how to implement it so that boxes can be inserted without losing formatting across new-lines.

Here is the current function:

(defun create-ditaa-box (x)
  (interactive "sText: ")
  (let* ((input x)
         (input-list (s-split "\n" input))
         (input-lengths (mapcar 'length input-list))
         (max-len (+ 2 (seq-max input-lengths)))
         (border (concat "+" (make-string max-len ?-) "+"))
         (input-format (mapcar* '(lambda (x y)
                                   (concat "| "
                                           x
                                           (make-string (- (- max-len y) 1) ? )
                                           "|")) input-list input-lengths)))
    (with-current-buffer (buffer-name)
      (insert border)
      (mapc '(lambda (x)
               (newline-and-indent)
               (insert x))
            input-format)
      (newline-and-indent)
      (insert border))))

It currently works as expected when my cursor is at the beginning of a line:

+--------+
| Hello  |
| World! |
+--------+

When creating diagrams I might have a box that needs to be in the center of the buffer. Currently, my function can't handle this:

Output:

                  <cursor-here>+--------+
| Hello  |
| World! |
+--------+

Expected Output:

                  <cursor-here>+--------+
                               | Hello  |
                               | World! |
                               +--------+
  • Without looking closely at the question, does library boxquote.el help? Or picture-mode? – Drew Jan 18 at 2:46
0

You need to find the current column and then insert at column COL (see lm-insert-at-column).

(defun create-ditaa-box (x)
  (interactive "sText: ")
  (let* ((input x)
         (col (current-column))
         (input-list (split-string input "\n"))
         (input-lengths (mapcar 'length input-list))
         (max-len (+ 2 (seq-max input-lengths)))
         (border (concat "+" (make-string max-len ?-) "+"))
         (input-format (mapcar* (lambda (x y)
                                  (concat "| "
                                          x
                                          (make-string (- (- max-len y) 1) ? )
                                          "|"))
                                input-list input-lengths)))
    (with-current-buffer (buffer-name)
      (save-excursion
        (insert border)
        (mapc (lambda (x)
                (unless (eobp)
                  (forward-line 1))
                (lm-insert-at-column col x))
              input-format)
        (unless (eobp)
          (forward-line 1))
        (lm-insert-at-column col border)))))

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.