Advertisement
froleyks

temp.el

May 13th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 10.31 KB | None | 0 0
  1.    #+BEGIN_SRC emacs-lisp
  2.      ;;; rotate-text.el --- cycle through words, symbols and patterns
  3.      ;;
  4.      ;; Copyright (C) 2009 Nikolaj Schumacher
  5.      ;;
  6.      ;; Author: Nikolaj Schumacher <bugs * nschum de>
  7.      ;; Version: 0.1
  8.      ;; Keywords: abbrev, convenience, matching
  9.      ;; URL: http://nschum.de/src/emacs/rotate-text/
  10.      ;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x
  11.      ;;
  12.      ;; This file is NOT part of GNU Emacs.
  13.      ;;
  14.      ;; This program is free software; you can redistribute it and/or
  15.      ;; modify it under the terms of the GNU General Public License
  16.      ;; as published by the Free Software Foundation; either version 2
  17.      ;; of the License, or (at your option) any later version.
  18.      ;;
  19.      ;; This program is distributed in the hope that it will be useful,
  20.      ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.      ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.      ;; GNU General Public License for more details.
  23.      ;;
  24.      ;; You should have received a copy of the GNU General Public License
  25.      ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
  26.      ;;
  27.      ;;; Commentary:
  28.      ;;
  29.      ;; rotate-text allows you cycle through commonly interchanged text with a single
  30.      ;; keystroke.  For example, you can toggle between "frame-width" and
  31.      ;; "frame-height", between "public", "protected" and "private" and between
  32.      ;; "variable1", "variable2" through "variableN".
  33.      ;;
  34.      ;; Add the following to your .emacs:
  35.      ;;
  36.      ;; (add-to-list 'load-path "/path/to/rotate-text")
  37.      ;; (autoload 'rotate-text "rotate-text" nil t)
  38.      ;; (autoload 'rotate-text-backward "rotate-text" nil t)
  39.      ;;
  40.      ;; Customize the variables `rotate-text-patterns', `rotate-text-symbols' and
  41.      ;; `rotate-text-words'.  You can make buffer-local additions in
  42.      ;; `rotate-text-local-patterns', `rotate-text-local-symbols' and
  43.      ;; `rotate-text-local-words'.
  44.      ;;
  45.      ;; Use the commands `rotate-text' and `rotate-text-backward' to rotate the
  46.      ;; text.
  47.      ;;
  48.      ;;; Change Log:
  49.      ;;
  50.      ;; 2009-04-13 (0.1)
  51.      ;;    Initial release.
  52.      ;;
  53.      ;;; Code:
  54.  
  55.      (eval-when-compile (require 'cl))
  56.  
  57.      (add-to-list 'debug-ignored-errors "^Nothing to rotate$")
  58.  
  59.      (defgroup rotate-text nil
  60.        "Cycle through words, symbols and patterns."
  61.        :group 'abbrev
  62.        :group 'convenience
  63.        :group 'matching)
  64.  
  65.      (defcustom rotate-text-patterns
  66.        '(("\\_<[^-]\\(\\sw\\|\\s_\\)*[0-9]+" rotate-text-increment-number-in-symbol)
  67.          ("-?0x?[0-9a-fA-F]+" rotate-text-increment-hex-number)
  68.          ("-?[0-9]+" rotate-text-increment-number))
  69.        "*Patterns and functions to rotate them.
  70.     Each entry is a list.  Its first element should be the regular expression to
  71.     replace, the second element is a function.  When rotating, it is called with the
  72.     matching text and an integer determining the rotation amount and direction."
  73.        :group 'rotate-text
  74.        :type '(repeat (list (string :tag "Regular expression")
  75.                             (function :tag "Rotation function"))))
  76.  
  77.      (defcustom rotate-text-symbols '(("private" "protected" "public"))
  78.        "*List of symbol names to rotate.
  79.     Each element is a list of symbols that should be cycled through."
  80.        :group 'rotate-text
  81.        :type '(repeat (repeat :tag "Rotation group" (string :tag "Symbol"))))
  82.  
  83.      (defcustom rotate-text-words '(("width" "height")
  84.                                     ("left" "right" "top" "bottom"))
  85.        "*List of words to rotate.
  86.     Each element is a list of words that should be cycled through.  Individual
  87.     segments in symbol names are recognized as words, i.e. windowWidth can be
  88.     replaced with windowHeight.
  89.     All entries must be in lower case. The case is determined by the rotated
  90.     text."
  91.        :group 'rotate-text
  92.        :type '(repeat (repeat :tag "Rotation group" (string :tag "Word"))))
  93.  
  94.      (defvar rotate-text-local-patterns nil
  95.        "*Buffer local additions to `rotate-text-patterns'.")
  96.      (make-variable-buffer-local 'rotate-text-local-patterns)
  97.  
  98.      (defvar rotate-text-local-symbols nil
  99.        "*Buffer local additions to `rotate-text-symbols'.")
  100.      (make-variable-buffer-local 'rotate-text-local-symbols)
  101.  
  102.      (defvar rotate-text-local-words nil
  103.        "*Buffer local additions to `rotate-text-words'.")
  104.      (make-variable-buffer-local 'rotate-text-local-words)
  105.  
  106.      ;;; numbers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  107.  
  108.      (defun rotate-text-increment-number (original arg &optional minimum)
  109.        (number-to-string (max (+ (string-to-number original) arg)
  110.                               (or minimum most-negative-fixnum))))
  111.  
  112.      (defun rotate-text-increment-hex-number (original arg)
  113.        (when (string-match "\\`-?\\(0x\\)" original)
  114.          (setq original (replace-match "" t t original 1)))
  115.        (let ((result (+ (string-to-number original 16) arg)))
  116.          (format "%s0x%x" (if (< result 0) "-" "") (abs result))))
  117.  
  118.      (defun rotate-text-increment-number-in-symbol (original arg)
  119.        (when (string-match "[0-9]+" original)
  120.          (replace-match (rotate-text-increment-number (match-string 0 original)
  121.                                                       arg 0)
  122.                         t t original)))
  123.  
  124.      ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  125.  
  126.      (defun rotate-text-replacement (replacements original dir)
  127.        "Find the replacement for ORIGINAL in REPLACEMENTS."
  128.        (save-match-data
  129.          (if (functionp (car replacements))
  130.              ;; function
  131.              (if (and (< dir 0) (functionp (cadr replacements)))
  132.                  (funcall (cadr replacements) original (- dir))
  133.                (funcall (car replacements) original dir))
  134.            ;; list
  135.            (let ((rest-pattern (member original replacements)))
  136.              (when rest-pattern
  137.                (car (nthcdr (mod (- dir (length rest-pattern)) (length replacements))
  138.                             replacements)))))))
  139.  
  140.      (defun rotate-text-match-at-point (regexp)
  141.        (save-excursion
  142.          (let ((pos (point)))
  143.            (goto-char (point-at-bol))
  144.            (catch 'match
  145.              (while (re-search-forward regexp (1+ (point-at-eol)) t)
  146.                (and (>= pos (match-beginning 0))
  147.                     (<= pos (match-end 0))
  148.                     (throw 'match (match-string-no-properties 0))))))))
  149.  
  150.      (defun rotate-text-symbol-at-point ()
  151.        "Rotate the symbol at point."
  152.        (rotate-text-match-at-point "\\_<\\(\\s_\\|\\sw\\)+\\_>"))
  153.  
  154.      (defun rotate-text-word-at-point ()
  155.        "Rotate the word at point."
  156.        (let ((case-fold-search nil))
  157.          (or (rotate-text-match-at-point "\\(\\<\\|[[:upper:]]\\)[[:lower:]]+")
  158.              (rotate-text-match-at-point "\\<[[:upper:]]+"))))
  159.  
  160.      (defun rotate-text-match-case (original new)
  161.        "Match the case of ORIGINAL in NEW."
  162.        (let ((case-fold-search nil))
  163.          (save-match-data
  164.            (cond
  165.             ((string-match "\\`[[:upper:]][[:lower:]]" original) (capitalize new))
  166.             ((string-match "\\`[[:upper:]][[:upper:]]" original) (upcase new))
  167.             (t new)))))
  168.  
  169.      (defvar rotate-text-last-offset nil)
  170.  
  171.      ;;;###autoload
  172.      (defun rotate-text (arg)
  173.        "Rotate the text at point."
  174.        (interactive (list (if (consp current-prefix-arg)
  175.                               -1
  176.                             (prefix-numeric-value current-prefix-arg))))
  177.        (let ((pos (point))
  178.              (offset 0)
  179.              match replacement)
  180.          (or ;; symbols
  181.              (when (setq match (rotate-text-symbol-at-point))
  182.                (dolist (symbols (append rotate-text-local-symbols
  183.                                         rotate-text-symbols))
  184.                  (when (setq replacement
  185.                              (rotate-text-replacement symbols match arg))
  186.                    (return t))))
  187.              ;; words
  188.              (when (setq match (rotate-text-word-at-point))
  189.                (dolist (words (append rotate-text-local-words
  190.                                       rotate-text-words))
  191.                  (when (setq replacement
  192.                              (rotate-text-replacement words (downcase match) arg))
  193.                    (setq replacement (rotate-text-match-case match replacement))
  194.                    (return t))))
  195.              ;; regexp
  196.              (dolist (pattern (append rotate-text-local-patterns
  197.                                       rotate-text-patterns))
  198.                (when (setq match (rotate-text-match-at-point (car pattern)))
  199.                  (setq replacement (rotate-text-replacement (cdr pattern) match arg))
  200.                  (return t)))
  201.              (error "Nothing to rotate"))
  202.  
  203.          (unless (eq last-command this-command)
  204.            (setq rotate-text-last-offset
  205.                  (if (eq pos (match-end 0))
  206.                      'end
  207.                    (- pos (match-beginning 0)))))
  208.  
  209.          (replace-match replacement)
  210.  
  211.          (goto-char (if (eq rotate-text-last-offset 'end)
  212.                         (match-end 0)
  213.                       (min (+ (match-beginning 0) rotate-text-last-offset)
  214.                            (match-end 0))))))
  215.  
  216.      ;;;###autoload
  217.      (defun rotate-text-backward (arg)
  218.        "Rotate the text at point backwards."
  219.        (interactive (list (if (consp current-prefix-arg)
  220.                               -1
  221.                             (prefix-numeric-value current-prefix-arg))))
  222.        (rotate-text (- arg)))
  223.  
  224.      (provide 'rotate-text)
  225.      ;;; rotate-text.el ends here
  226.  
  227.      (setq rotate-text-words (append rotate-text-words '(("True" "False") ("true" "false"))))
  228.  
  229.      (defun froleyks-search-rotate()
  230.        (interactive)
  231.        ;; ( search-backward-regexp "\s")
  232.        ;; (evil-forward-char)
  233.        (search-forward-regexp  "\\(\\+\\|-\\)?[0-9]+\\(\\.[0-9]+\\)?\\|true\\|false" nil t)
  234.        (call-interactively 'rotate-text)
  235.        (evil-backward-WORD-begin)
  236.        )
  237.  
  238.      (defun froleyks-search-rotate-backward()
  239.        (interactive)
  240.        ;; ( search-backward-regexp "\s")
  241.        ;; (evil-forward-char)
  242.        (search-forward-regexp  "\\(\\+\\|-\\)?[0-9]+\\(\\.[0-9]+\\)?\\|true\\|false" nil t)
  243.        (call-interactively 'rotate-text-backward)
  244.        (evil-backward-WORD-begin)
  245.        )
  246.  
  247.      (require 'general)
  248.      (general-define-key
  249.       :states 'normal
  250.       "ö" 'froleyks-search-rotate-backward
  251.       "ä" 'froleyks-search-rotate
  252.       )
  253.   #+END_SRC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement