emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

recommended techniques for "temporary highlighting"?


From: Miles Bader
Subject: recommended techniques for "temporary highlighting"?
Date: Sat, 14 Oct 2017 11:12:37 +0900

Hi all,

I wrote some code for dictionary lookup that looks up the maximal
Chinese word/phrase following point, displays the definition, and
moves point past the term found.  Chinese has no word boundaries, so I
found it very useful to highlight the actual word looked up to make it
clear to the user exactly what text was used.

What I'm wondering is:  What's the best way to do this sort of
temporary highlighting?


I've tried several different methods far:

(1) Setting a temporary active region:

    ;; [This code copied from `handle-shift-selection' in Emacs
    ;; simple.el; maybe a function to this should be added?]
    (unless (and mark-active
                (eq (car-safe transient-mark-mode) 'only))
      (setq-local transient-mark-mode
                 (cons 'only
                       (unless (eq transient-mark-mode 'lambda)
                         transient-mark-mode))))
    ;; The documentation warns against using set-mark directly, but
    ;; push-mark seems to have other weird side-effects: in particular,
    ;; it suppresses the above call to forward-char...(???)
    (set-mark start-pos)

    ;; Display the returned dictionary entry.
    (message "%s" definition)))


(2) Using an explicit overlay (`cedict-lookup-term-highlight-overlay'
below) and deleting it after 'sit-for':

    (move-overlay cedict-lookup-term-highlight-overlay
          start-pos longest-pos (current-buffer))

    ;; Display the returned dictionary entry.
    (message "%s" definition)

    (sit-for 5)
    (delete-overlay cedict-lookup-term-highlight-overlay))


Technique (1) made me uncomfortable because it affects the region when
the user may well not want that, and seems to be using internal
mechanism it probably shouldn't be using.

Technique (2) is maybe better, but the practice of nominally handing
control back to the user while actually sitting inside sit-for feels a
little dodgy to me.  I'd kinda rather the command be done when it
looks like it's done, with the highlight state just sort of
disappearing automagically as happens with (1).

Any thoughts as to what the best way to do this sort of thing is?

Thanks,

-miles

-- 
Cat is power.  Cat is peace.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]