Elisp: Twitterfy

By Xah Lee. Date: . Last updated: .

Here's a emacs lisp command that shortens text to fit twitter's 140 chars.

(defun xah-twitterfy ()
  "Shorten words for Twitter 280 char limit on current line or selection.

If `universal-argument' is called first, ask for conversion direction (shorten/lenthen).

Note: calling this function twice in opposite direction does not necessarily return the origial, because the map is not one-to-one.

URL `http://ergoemacs.org/emacs/elisp_twitterfy.html'
Version 2019-01-16"
  (interactive)
  (let (
        $p1 $p2
        $direction

        ($shorten-map
         [
          ["\\bare\\b" "r"]
          ["\\byou\\b" "u"]
          ["e.g. " "eg "]
          ["\bto\b" "2"]
          [" your" " ur "]
          ["\\band\\b" "&"]
          ["because" "∵"]
          ["therefore " "∴"]
          [" at " " @ "]
          [" love " " ♥ "]
          [" one " " 1 "]
          [" two " " 2 "]
          [" three " " 3 "]
          [" four " " 4 "]
          [" zero " " 0 "]
          ["hexadecimal " "hex "]
          ["Emacs: " "#emacs "]
          ["JavaScript: " "#JavaScript "]
          ["Python: " "#python "]
          ["Ruby: " "#ruby "]
          ["Perl: " "#perl "]
          ["Emacs Lisp: " "#emacs #lisp "]
          ["Elisp: " "#emacs #lisp "]
          [", " ","]
          ["\\.\\.\\." "…"]
          ["\\. " "。"]
          ["\\? " "?"]
          [": " ":"]
          ["! " "!"]]
         )
        ($lengeth-map
         [
          ["\\bu\\b" "you"]
          ["\\br\\b" "are"]
          ["eg " "e.g. "]
          [" 2 " " to "]
          ["\\bur\\b" "your"]
          ["\\b&\\b" "and"]
          ["\\bcuz\\b" "because"]
          ["\\b∴\\b" "therefore "]
          [" @ " " at "]
          [" ♥ " " love "]
          [" 1 " " one "]
          [" 2 " " two "]
          [" 3 " " three "]
          [" 4 " " four "]
          [" 0 " " zero "]
          ["hex " "hexadecimal "]
          ["," ", "]
          ["…" "..."]
          ["。" ". "]
          ["?" "? "]
          [":" ": "]
          ["!" "! "]
          ]
         ))
    (if (region-active-p)
        (setq $p1 (region-beginning) $p2 (region-end))
      (save-excursion
        (if (re-search-backward "\n[ \t]*\n" nil "move")
            (progn
              (setq $p1 (point))
              (re-search-forward "\n[ \t]*\n"))
          (setq $p1 (point)))
        (progn
          (re-search-forward "\n[ \t]*\n" nil "move")
          (setq $p2 (point)))))
    (setq $direction
          (if current-prefix-arg
              (ido-completing-read
               "Direction: "
               '( "shorten"  "lengthen")
               "PREDICATE"
               "REQUIRE-MATCH")
            "auto"
            ))
    (save-restriction
      (narrow-to-region $p1 $p2)
      (when (string= $direction "auto")
        (goto-char (point-min))
        (setq $direction
              (if (re-search-forward "。\\|,\\|?\\|!" nil t)
                  "lengthen" "shorten"
                  )))
      (let ( (case-fold-search nil))
        (mapc
         (lambda ($x)
           (goto-char (point-min))
           (while (re-search-forward (elt $x 0) nil t)
             (replace-match (elt $x 1) "FIXEDCASE" "LITERAL")))
         (if (string= $direction "shorten")
             $shorten-map
           $lengeth-map))
        (goto-char (point-min))
        (while (re-search-forward "  +" nil t)
          (replace-match " " "FIXEDCASE" "LITERAL")))
      (goto-char (+ (point-min) 280)))))

2019-01-16 am still refining this command. So you may see some bad code here.

Emacs Text Transform Under Cursor

  1. elisp wrapper + Python Ruby …
  2. Toggle Letter Case
  3. Title Case
  4. Upcase Sentences
  5. Cycle Space Hyphen Underscore
  6. Escape Quotes
  7. Quote Lines
  8. Spaces to New Lines
  9. Change Brackets/Quotes
  10. Remove Accent Marks
  11. Convert Straight/Curly Quotes
  12. Convert English/Chinese Punctuations
  13. Color Conversion (RGB, HSL, HSV)
  14. Decimal to Hexadecimal
  15. Replace Greek Letter Names to Unicode
  16. Twitterfy Text
  17. Toggle line wrap
  18. Clean White Space

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs

Emacs

Emacs Lisp

Misc