Emacs Lisp: 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://xahlee.info/emacs/emacs/elisp_twitterfy.html'
Version: 2019-03-02 2021-08-17 2022-04-07"
  (interactive)
  (let ( $p1 $p2 $direction
        ($shorten-map
         [
          ["\\bare\\b" "r"]
          ["\\byou\\b" "u"]
          ["e.g. " "eg "]
          ["\bto\b" "2"]
          [" your" " ur "]
          ["\\band\\b" "&"]
          ["\\bbecause\\b" "∵"]
          ["\\bcuz\\b" "∵"]
          ["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 "]
          ["," ", "]
          ["…" "..."]
          ["。" ". "]
          ["?" "? "]
          [":" ": "]
          ["!" "! "]
          ]
         ))
    (let (($bds (xah-get-bounds-of-block-or-region))) (setq $p1 (car $bds) $p2 (cdr $bds)))
    (setq $direction
          (if current-prefix-arg
              (completing-read
               "Direction: "
               '( "shorten"  "lengthen")
               "PREDICATE"
               "REQUIRE-MATCH")
            "auto"
            ))
    (save-restriction
      (narrow-to-region $p1 $p2)
      (when (string-equal $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) t t)))
         (if (string-equal $direction "shorten")
             $shorten-map
           $lengeth-map))
        (goto-char (point-min))
        (while (re-search-forward "  +" nil t)
          (replace-match " " t t)))
      (goto-char (+ (point-min) 280)))))

you need Emacs: xah-get-thing.el

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Elisp Examples

Text Transform Under Cursor

Commands Do thing-at-point

Command to Insert Things

Script Examples

Misc