0

I have a function that reads a link from the clipboard and generates a metadata-ful markdown link from it, and inserts it:

(defun night/unt ()
  (interactive)
  (let* ((link (current-kill 0))
         (cmd (concat "brishzr.dash " (shell-quote-argument (concat "ec " (shell-quote-argument link) " | inargsf unt"))))
         )
    (message "%s" cmd)
   
    (insert (shell-command-to-string cmd))
    (save-buffer)
    ))

But this doesn't respect the indentation at which the text is to be inserted.

Invoking it on here:

* https://github.com/org-roam/org-roam
     CARET_IS_HERE

will produce

* https://github.com/org-roam/org-roam
     * [org-roam/org-roam-server](https://github.com/org-roam/org-roam-server)
    * A Web Application to Visualize the Org-Roam Database - org-roam/org-roam-server
![](https://avatars0.githubusercontent.com/u/65036520?s=400&v=4)

I want it to produce

* https://github.com/org-roam/org-roam
     * [org-roam/org-roam-server](https://github.com/org-roam/org-roam-server)
         * A Web Application to Visualize the Org-Roam Database - org-roam/org-roam-server
     ![](https://avatars0.githubusercontent.com/u/65036520?s=400&v=4)
  • Is the file an ordinary text file? What major mode is it in when you edit it? – NickD Aug 4 at 19:34
  • @NickD It's a markdown file, in Markdown major mode. I am going to add a similar function for my org-mode files as well. – HappyFace Aug 4 at 21:04
  • Does (indent-region (point-min) (point-max)) indent everything properly? You should try it on a copy of the file to be safe. – NickD Aug 4 at 21:16
  • @NickD I don't think that would work at all. The indentation in markdown is not "wrong", it just encodes the hierarchy between the items. After the insertion has happened, there is no way to know what the correct indentation should be programmatically. I need some way to add the current lines indent to all inserted lines. – HappyFace Aug 4 at 21:56
  • In that case, you'll have to capture the output of the shell command in a string and munge it appropriately before inserting it in the buffer - but it's not clear from your example what that "munging appropriately" would look like. – NickD Aug 5 at 0:07
0

I found an answer:

(defun night/unt ()
  (interactive)
  (let* (
         (my-column (current-column))
         (link (current-kill 0))
         (cmd (concat "brishzr.dash " (shell-quote-argument (concat "ec " (shell-quote-argument link) " | inargsf unt"))))
         (text (progn
                 (message "%s" cmd)
                 (shell-command-to-string cmd)))
         )
    (let ((lines (split-string text "\n")))
      (dolist (line lines)
        (progn (if (not (string= "" line)) ; so as to not insert the last empty line
                   (insert line "\n" (make-string my-column ?\s))
                 ))))
    (save-buffer)
    ))
| improve this answer | |
  • 1
    You can construct a string with the requisiite number of spaces (setq spaces (make-string my-column) ?\s) and then insert it with (insert spaces), instead of inserting one space at a time in a loop. – NickD Aug 6 at 3:40

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.