Como trocar as palavras `true` e` false` no texto do buffer?

Nov 02 2020

Eu me peguei mudando o texto truepara falsee vice-versa, enquanto codificava com muita frequência. É muito tedioso marcar tudo e substituí-lo pelo oposto. Seria ótimo ter uma função que substituísse a palavra sob o cursor por truese fosse falsee vice-versa.

Existe alguma solução existente para resolver isso? Se não, como isso pode ser implementado.

Respostas

5 PanJunjie潘俊杰 Nov 02 2020 at 13:21

Oh, acabei de escrever isso há alguns dias . No entanto, ele não faz nada mais do que o pacote "papagaio" recomendado em outra resposta. Aqui está o código:

(require 'dash)
(defvar my-flip-symbol-alist
  '(("true" . "false")
    ("false" . "true"))
  "symbols to be quick flipped when editing")

(defun my/flip-symbol ()
  "\"I don't want to type here, just do it for me.\""
  (interactive)
  (-let* (((beg . end) (bounds-of-thing-at-point 'symbol))
          (sym (buffer-substring-no-properties beg end)))
    (when (member sym (cl-loop for cell in my-flip-symbol-alist
                               collect (car cell)))
      (delete-region beg end)
      (insert (alist-get sym my-flip-symbol-alist "" nil 'equal)))))

;; TODO here, bind keys to the function with your favorite key binding function
4 gigiair Nov 02 2020 at 04:13

Dê uma olhada lá: (info "(emacs) Substituição Regex") troque 'x' e 'y' desta forma: .... então,

  M-x replace-regexp <RET> \(true\)\|false <RET>
   \,(if \1 "false" "true") <RET>

deve fazer o truque

editar:

Provavelmente este comando é muito melhor para sua demanda

(defun swap-true-false ()
  (interactive)
  (save-excursion
    (when (thing-at-point-looking-at "\\b\\(true\\)\\|false\\b")
      (replace-match (if (match-string 1) "false" "true")))
    ))

Você pode vincular uma chave a este comando de acordo com seu gosto, por exemplo

 (bind-key (kbd "C-c s") #'swap-true-false)
2 stackunderflow Nov 02 2020 at 07:24

Que tal parrot.el ? Você pode definir seu próprio parrot-rotate-dict.

CSM Nov 03 2020 at 01:16

Pode ser melhor mudar a abordagem e usar variáveis ​​constantes globais (se o seu idioma as suportar) para ligar / desligar o recurso.