Skip to content

Instantly share code, notes, and snippets.

@sheijk
Last active January 26, 2021 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheijk/b409b5f0a50264f6a8d91736b0a35beb to your computer and use it in GitHub Desktop.
Save sheijk/b409b5f0a50264f6a8d91736b0a35beb to your computer and use it in GitHub Desktop.
A simple menu to change text (upper/lower case, incr numbers, move parenthesis, replace inner/outer)
(defun my-increment-number-decimal (&optional arg)
"Increment the number forward from point by 'arg'."
(interactive "p*")
(save-excursion
(save-match-data
(let (inc-by field-width answer)
(setq inc-by (if arg arg 1))
(skip-chars-backward "0123456789")
(when (re-search-forward "[0-9]+" nil t)
(setq field-width (- (match-end 0) (match-beginning 0)))
(setq answer (+ (string-to-number (match-string 0) 10) inc-by))
(when (< answer 0)
(setq answer (+ (expt 10 field-width) answer)))
(replace-match (format (concat "%0" (int-to-string field-width) "d")
answer)))))))
(defun my-decrement-number-decimal (&optional arg)
"Decrement the number forward from point by 'arg'."
(interactive "p*")
(my-increment-number-decimal (- arg)))
;;; change-menu.el --- A simple menu to change text
;; Copyright 2021 Jan Rehders
;;
;; Author: Jan Rehders <nospam@sheijk.net>
;; Version: 0.1
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;; A simple menu to change text case, etc.
(require 'transient)
(require 'paredit)
(transient-define-prefix shk-transpose-menu ()
"A menu to transpose items"
[["Transpose"
("w" "words" transpose-words)
("l" "lines" transpose-lines)
("c" "chars" transpose-chars)
("p" "paragraphs" transpose-paragraphs)
("s" "sexps" transpose-sexps)
("." "sentences" transpose-sentences)]
["Lines"
("r" "reverse" reverse-region :if-non-nil mark-active)
("S" "sort" sort-lines :if-non-nil mark-active)
("R" "sort regexp" sort-regexp-fields :if-non-nil mark-active)
("a" "align regexp" align-regexp :if-non-nil mark-active)]])
(transient-define-prefix shk-change-menu ()
"A menu to change text case, etc."
[["Back"
("c" "capitalize" smart-capitalize-word :transient t)
("l" "downcase" smart-downcase-word :transient t)
("u" "upcase" smart-upcase-word :transient t)]
["Forward"
("C" "capitalize" capitalize-word :transient t)
("L" "downcase" downcase-word :transient t)
("U" "upcase" upcase-word :transient t)]
["Numbers"
("+" "increment" my-increment-number-decimal :transient t)
("=" "increment" my-increment-number-decimal :transient t)
("-" "decrement" my-decrement-number-decimal :transient t)]
["Replace"
("i" "inner" change-inner)
("o" "outer" change-outer)]
["Paredit"
(">" "forward" paredit-forward-slurp-sexp :transient t)
("<" "backward" paredit-backward-slurp-sexp :transient t)
("/" "raise" paredit-raise-sexp :transient t)]
["Wrap"
("w(" "(wrap)" paredit-wrap-round :transient t)
("w<" "<wrap>" paredit-wrap-angled :transient t)
("w{" "{wrap}" paredit-wrap-curly :transient t)
("w[" "[wrap]" paredit-wrap-square :transient t)]
["More"
("t" "transpose" shk-transpose-menu)]])
(global-set-key (kbd "M-c") 'shk-change-menu)
(global-set-key (kbd "M-t") 'shk-transpose-menu)
;;; shk-smart-case.el --- Smart case commands
;; Copyright 2020 Jan Rehders
;;
;; Author: Jan Rehders <nospam@sheijk.net>
;; Version: 0.1
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;; Some helpers for smart conversion of up/down/camel-case
;;
(defvar seq-store-times 0)
(defvar seq-start-point 0
"Stores location of pointer when sequence of calls of the same
function was started. This variable is updated by `seq-times'")
(defun seq-times (name &optional max)
"Returns number of times command `name' was executed. If `max'
is specified the counter will wrap around at the value of `max'
never reaching it. It also updates `seq-start-point'."
(if (eq last-command name)
(if (= (setq seq-store-times (1+ seq-store-times)) max)
(setq seq-store-times 0) seq-store-times)
(setq seq-start-point (point) seq-store-times 0)))
(defmacro define-smart-case-func (name case-command)
"Will define a function which will call case-command. When a
prefix is given it is passed as argument. Otherwise it will
pass 1 for the first invocation in a row, 2 for the second time
etc."
`(defun ,name (prefix)
(interactive "P")
(when (equal 'last-command (quote ,case-command))
(setq last-command (quote ,name)))
(let ((arg (or prefix
(- (1+ (seq-times (quote ,name) 10000))))))
(ignore-errors
(when (fboundp 'nav-flash-show)
(save-excursion
(let ((original-point (point)))
(forward-word arg)
(let* ((bounds (bounds-of-thing-at-point 'word))
(start (min original-point (car bounds)))
(end (max original-point (cdr bounds))))
(nav-flash-show start end nil 0.2))))))
(,case-command arg))))
(define-smart-case-func smart-upcase-word upcase-word)
(define-smart-case-func smart-downcase-word downcase-word)
(define-smart-case-func smart-capitalize-word capitalize-word)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment