5

I found myself switching the text true to false, and vice versa, while coding very often. It's quite tedious to mark the entire thing and replace it with opposite. It would be great to have a function that replaces word under the cursor to true if it was false and vice versa.

Is there any existing solution to solve that? If not how this can be implemented.

1
  • @Drew thanks for fixing wording. It looks much better now.
    – Navidot
    Nov 1, 2020 at 18:48

4 Answers 4

5

Oh, I just wrote this a few days ago. It does nothing more than the package "parrot" recommended in another answer, though. Here's the code:

(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
2
  • Thanks for sharing. I found your answer the most appealing so far. And it doesn't put any crazy animated icons on my screen, which is much appreciated. Because the main goal is to swap values back and forth, I think we can assume every record in my-flip-symbol-alist will be checked both ways, so we don't have to duplicate the items.
    – Navidot
    Nov 2, 2020 at 10:30
  • Yes, I didn't write the function to check both ways just because I don't know how to reverse-get in an alist in elisp, neither do I know what's the "hashmap in both ways" data structure in elisp built-in libs, so I just wrote the alist to have both entries :/
    – JJPandari
    Nov 3, 2020 at 9:41
4

Have a look there :(info "(emacs)Regex replacement") exchange ‘x’ and ‘y’ this way:.... so,

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

should do the trick

edit:

Probably this command is much better for your demand

(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")))
    ))

You can bind a key to this command according to your taste, for instance

 (bind-key (kbd "C-c s") #'swap-true-false)
1
  • Thanks. To make it work I have to put the cursor before the symbol, and it's not a canned solution that it's easily reusable across other symbols. Those issues were addressed in the accepted answer.
    – Navidot
    Nov 2, 2020 at 10:38
2

How about parrot.el? You can define you own parrot-rotate-dict.

3
  • 2
    Please expand on your answer to show OP how to use it.
    – Dan
    Nov 2, 2020 at 2:03
  • The parrot readme file is great :)
    – phils
    Nov 2, 2020 at 9:21
  • 1
    Thanks, it looks like it does the thing, but how to turn off this parrot icon? I only need to feature to replace the symbol. I assume it takes much more code to implement this redundant animation than the core feature...
    – Navidot
    Nov 2, 2020 at 10:32
0

It might be better to change approach, and use global constant variables (if your language supports them) to turn the feature on/off.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.