My attempt to optimize my emacs load time <1 second
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1 KiB

  1. ;;; convert-to-table --- Convert a selected region to an `org-mode' table.
  2. ;;; Commentary:
  3. ;;; Code:
  4. (defun leo/remove-excess-line-breaks (input)
  5. "Remove excess line breaks from INPUT."
  6. (let ((mylist (split-string input "\n")))
  7. ;; (setq mylist (remove-duplicates mylist :test 'string=))
  8. ;; (delete "" mylist)
  9. (string-join mylist "\n")
  10. ))
  11. (defun set-nth (list n val)
  12. "Update LIST at position N with value VAL."
  13. (if (> n 0)
  14. (cons (car list)
  15. (set-nth (cdr list) (1- n) val))
  16. (cons val (cdr list))))
  17. (defun leo/build-divider (cols)
  18. "Build an `org-mode' table divider with COLS columns."
  19. (let ((index 0)
  20. (output '()))
  21. (while (> cols index)
  22. (push "---" output)
  23. (setq index (+ index 1)))
  24. (concat "|" (string-join output "|") "|")))
  25. (defun leo/convert-to-table (beg end)
  26. "Convert <RET> delimited region (BEG and END) into `org-mode' table."
  27. (interactive (if (use-region-p)
  28. (list (region-beginning) (region-end))
  29. (list nil nil)))
  30. (let (
  31. (input (leo/remove-excess-line-breaks (buffer-substring-no-properties beg end)))
  32. (cols (read-number "How many columns: "))
  33. )
  34. (if (and input cols)
  35. (let (
  36. (lines (split-string input "\n"))
  37. (length (length (split-string input "\n")))
  38. (times 1)
  39. (output "")
  40. )
  41. (setq output (mapcar (lambda (in) (concat " | " in)) lines))
  42. ;; (message "%s" output)
  43. (while (>= length (* times cols))
  44. (setq output (set-nth output (- (* times cols) 1) (concat (nth (- (* times cols) 1) output) " |\n")))
  45. (setq times (+ times 1))
  46. )
  47. (kill-region beg end)
  48. (let (
  49. (rows (split-string (string-join output "") "\n"))
  50. (first "")
  51. )
  52. (setq first (pop rows))
  53. (push (leo/build-divider cols) rows)
  54. (push first rows)
  55. (insert (string-join rows "\n"))
  56. ))
  57. (message "Nothing selected!"))))
  58. (provide 'convert-to-table)
  59. ;;; convert-to-table.el ends here