danielhilst

gkos.el

Oct 28th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.54 KB | None | 0 0
  1. ;; gkos lib
  2. ;;
  3. ;;
  4.  
  5. (defmacro for-each-line (&rest body)
  6.   `(save-excursion
  7.      (let (line)
  8.        (goto-char (point-min))
  9.        (while (< (point) (point-max))
  10.      (setq line (buffer-substring-no-properties (progn
  11.                               (beginning-of-line)
  12.                               (point))
  13.                             (progn
  14.                               (end-of-line)
  15.                               (point))))
  16.      (forward-line 1)
  17.      ,@body))))
  18.  
  19. (defmacro awk (&rest body)
  20.   `(for-each-line
  21.    (setq line (split-string line))
  22.    ,@body))
  23.  
  24. (defun sprunge-buffer ()
  25.   (interactive)
  26.   (let ((url-request-method "POST")
  27.     (url-request-data (format "sprunge=%s"
  28.                   (url-hexify-string
  29.                    (buffer-substring-no-properties (point-min) (point-max))))))
  30.     (switch-to-buffer-other-window (url-retrieve-synchronously "http://sprunge.us"))))
  31.  
  32. (defun ascii-to-hex (b e)
  33.   "Translate an ascii string to a hex string and copy it to clipboard"
  34.   (interactive "r")
  35.   (save-excursion
  36.     (let ((i b)
  37.           (x-select-enable-clipboard t)
  38.           s)
  39.       (while (< i e)
  40.         (setq s (concat s (format "%x" (get-byte i))))
  41.         (setq i (+ i 1)))
  42.       (kill-new s t)
  43.       (message s))))
  44.  
  45. (defun hex-to-ascii (b e)
  46.   "Translate the region from hex to ascii and copy it to clipboard.
  47. I use that to translate urls in hex and paste it on url bar on my
  48. browser."
  49.   (interactive "r")
  50.   (let ((i b)
  51.         (x-select-enable-clipboard t)
  52.         s)
  53.     (while (< i e)
  54.       (setq s (concat s (format "%c" (read (concat "#x" (buffer-substring-no-properties i (+ i 2)))))))
  55.       (setq i (+ i 2)))
  56.     (kill-new s t)
  57.     (message (format "%s copied to clipboard" s))))
  58.  
  59.  
  60. (defun reverse-string-region (b e)
  61.   "Reverse a string on region"
  62.   (interactive "r")
  63.   (let ((str (buffer-substring-no-properties b e)))
  64.     (delete-region b e)
  65.     (insert (reverse-string str))))
  66.  
  67. (defun reverse-string (str)
  68.   "Reverse a string using nreverse"
  69.   (apply 'string (nreverse (string-to-list str))))
  70.  
  71.  
  72. (defun seq (start end &optional step)
  73.   "Return a list (@start @start+step @start+step*2 ... @end)"
  74.   (let ((l)
  75.         (i end)
  76.         (step (or step 1)))
  77.     (while (> i start)
  78.       (setq i (- i step))
  79.       (setq l (cons i l))
  80.       )
  81.     l)
  82.   )
  83.  
  84. (defmacro inc (n &optional step)
  85.   `(setq n (+ n ,(or step 1))))
  86.  
  87. (defun bytes-in-region (b e)
  88.   "Count number of bytes on region.
  89. I use this to calculate lenght of strings while programming. The
  90. lenght (in characters) is copied to clipboard"
  91.   (interactive "r")
  92.   (let ((i 0)
  93.         (x-select-enable-clipboard t))
  94.     (while (< (+ b i) e)
  95.       (setq i (1+ i)))
  96.     (kill-new (format "%d" i))
  97.     (message (format "%d bytes" i))))
  98.  
  99. (provide 'gkos)
Advertisement
Add Comment
Please, Sign In to add comment