Last active
January 26, 2021 22:05
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 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