0

To create a list from sentences in org mode, I use following command

  string-insert-rectangle 
    replace-regexp ^ with - 

How can I combine these two to form a single keyboard short-cut?

2
  • 1
    Note that C- - (control minus) on a region will turn lines into list items, by calling org-toggle-item or org-cycle-list-bullet accordingly.
    – Juancho
    Jun 16 at 12:59
  • What @Juancho said: see the Plain Lists section in the Org mode manual.
    – NickD
    Jun 16 at 13:56

1 Answer 1

1

As comments have mentioned, org-toggle-item may suit your needs. Though this seems like a great opportunity to learn Emacs Lisp. The following code though does as explain:

(defun my-toggle-item (start end)
  ;; interactive r says that the parameters START and END
  ;; should be populated from the active region.
  (interactive "r")
  ;; save which line the region starts and ends on
  (let ((start-line (save-excursion
                      (goto-char start)
                      (line-number-at-pos)))
        (end-line (save-excursion
                    (goto-char end)
                    (line-number-at-pos))))
    ;; open rectangle on region
    (open-rectangle start end)
    ;; calculate new start and end points and run replace
    (let ((region-start (save-excursion (goto-line start-line) (beginning-of-line) (point)))
          (region-end (save-excursion (goto-line end-line) (end-of-line) (point))))
      (replace-regexp-in-region "^" "- " region-start region-end))))

One edge case with this implementation is that it adds a dash even on blank lines.

The following is another way we could solve this:

(defun my-toggle-item (start end)
  ;; interactive r says that the parameters START and END
  ;; should be populated from the active region.
  (interactive "r")
  ;; save which line the region starts and ends on
  (let ((start-line (save-excursion
                      (goto-char start)
                      (line-number-at-pos)))
        (end-line (save-excursion
                    (goto-char end)
                    (line-number-at-pos))))
    ;; open rectangle on region
    (open-rectangle start end)

    ;; starting from the start line, 
    (goto-line start-line)
    ;; loop until we move past where the region ended
    (while (and (<= (line-number-at-pos) end-line)
                ;; make sure we're also not at the end of buffer
                (not (eobp)))
      (when (not (looking-at "\n")) ; ignore lines without text
        (skip-chars-forward " \t")
        (insert "- "))
      (forward-line))
    ;; move point to where the region began
    (goto-line start-line)
    (skip-chars-forward " \t")))

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.