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-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) "FIXEDCASE" "LITERAL")))
         (if (string-equal $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.

Put $5 at https://www.patreon.com/xahlee , or goto paypal.com and pay to Xah@XahLee.org . Message me on XahLee discord, say so, ask me questions.
Or Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys

ELisp

ELisp Examples

ELisp Write Major Mode


ELisp Examples

Text Transform Under Cursor

Commands Do thing-at-point

Command to Insert Things

Script Examples

Misc