Peter.O
Peter.O

Reputation: 6856

Different results when elisp function is run different ways; why?

EDIT: Perhaps (in original post) I used the term "transient" incorrectly (I'm not familiar enough with the jargon yet). What I really mean is that the highlighted region will disappear immediately when the user presses a navigation keys eg. arrow-keys... (2nd EDIT: I've removed the word "transient")


The particular issue of selecting a region so the user gets "cursor-key movement will make highlighting disappear" has been the bane of my existance recently. I get differnt results depending on how I run the following script.

Why does it give different results, and more specifically, is there a way to make it produce "cursor-keys make highlighting disappear" regardless of which mode is running, or whether it is being evaluated while testing? .. CUA mode has this behaviour, but I really need that non CUA mode does it too (and eval, if possible)...

Here are the results, followed by the code. (GNU Emacs 23.1.1)


 ;test (trans-hi) EOL
 (defun trans-hi ()
   "transient highlight"
     (beginning-of-line)
     (push-mark (point))
     (end-of-line)
     (activate-mark))

 ;test (call-trans-hi) EOL
 (defun call-trans-hi ()
   "call transient highlight"
     (interactive)
     (trans-hi))
 (global-set-key [C-f1] 'call-trans-hi)

Upvotes: 4

Views: 216

Answers (2)

Rörd
Rörd

Reputation: 6681

When you look at the source of activate-mark, you can see that it's just setting some variables. I suppose that's why you don't see the mark in both 1., because the actual highlighting happens in some stuff that's done when executing functions interactively instead of just calling them.

In the other cases of no CUA-mode, that just how transient highlighting works outside of CUA-mode. If you want the CUA-mode behaviour, use CUA-mode resp. that part of it.

EDIT:

Does this change (the addition of the setq line) to trans-hi make the highlighting work the way you want?

(defun trans-hi ()
  "transient highlight"
  (beginning-of-line)
  (push-mark (point))
  (end-of-line)
  (setq transient-mark-mode (cons 'only transient-mark-mode))
  (activate-mark))

Upvotes: 2

Daimrod
Daimrod

Reputation: 5020

If you want to see the region highlighted when you mark it, you need to activate the minor mode transient-mark-mode.

When a region is highlighted and a character insterted the default is to disable the highlighting and insert the character at the cursor.

If you wish you can delete the selected region by activating the minor mode delete-selection-mode.

Upvotes: 0

Related Questions