0

I am trying to write a function where I insert some text, and then activate the mark at the end of the function. I have several use-cases where I would like to use this.

I cannot figure out how do make it work. I have the following expression which I am using to test the behavior:

(progn
  (set-mark-command nil)
  (save-excursion
    (insert "hi"))
  (forward-line 2)
  (activate-mark))

Evaluating this expression does not activate the mark; however, if I comment out the save-excursion part which inserts some text, the mark is activated. Why isn't the mark being activated, and what can I do to make the expression work as expected?

0

My question is a duplicate of this question and this one.

Quick recap: From the answers in those threads, it seems certain commands will deactivate the mark. Insert appears to be such a command.

We can fix this issue in two ways:

  1. Change the first line
- (progn 
+ (let (deactivate-mark)
    (set-mark-command nil)
    (save-excursion
      (insert "hi"))
    (forward-line 2)
    (activate-mark))

This sets the variable deactivate-mark to nil. This was the first time I had seen a let with a variable without assigning it a value. You can read more about this here.

  1. Unset the variable deactivate-mark at the end of our function
 (progn 
   (set-mark-command nil)
   (save-excursion
     (insert "hi"))
   (forward-line 2)
   (activate-mark)
+  (setq deactivate-mark nil))
| improve this answer | |

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