all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* random color of variables in programming languages
@ 2013-12-11 12:42 Luca Ferrari
  0 siblings, 0 replies; 7+ messages in thread
From: Luca Ferrari @ 2013-12-11 12:42 UTC (permalink / raw)
  To: help-gnu-emacs

Hi all,
anyone knows about an Emacs extension so that in different programming
languages (e.g., C/C++) variables are assigned a different random
color like KDevelop does?

Thanks,
Luca



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: random color of variables in programming languages
       [not found] <mailman.8911.1386765750.10748.help-gnu-emacs@gnu.org>
@ 2013-12-11 16:06 ` Emanuel Berg
  2013-12-12 15:21   ` Luca Ferrari
       [not found]   ` <mailman.9022.1386861695.10748.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 7+ messages in thread
From: Emanuel Berg @ 2013-12-11 16:06 UTC (permalink / raw)
  To: help-gnu-emacs

Luca Ferrari <fluca1978@infinito.it> writes:

> Hi all, anyone knows about an Emacs extension so that
> in different programming languages (e.g., C/C++)
> variables are assigned a different random color like
> KDevelop does?

I don't know. That seems like a cool idea though. Then
you would see (i.e., not read) all the occurrences of
that variable at once. However I don't know how it
would work in practice. Perhaps you would just get
dizzy. With lots of parameters, perhaps it is better to
have them a uniform color, to make a really clear
separation what is data and what is metadata,
branching, etc.

If you were to put it up, I wouldn't recommend random
colors, instead you could use the variable *name* (the
letters) as function input to be mapped to colors: the
first letter - the ASCII position, "normalized" - could
be red, the second - same for green - etc.

First step if you were to do this would be to examine
how colors are setup to begin with. Instead of the
static color, you would hook a defun to calculate the
color based on the variable name.

-- 
Emanuel Berg, programmer-for-rent. CV, projects, etc at uXu
underground experts united:  http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: random color of variables in programming languages
  2013-12-11 16:06 ` random color of variables in programming languages Emanuel Berg
@ 2013-12-12 15:21   ` Luca Ferrari
       [not found]   ` <mailman.9022.1386861695.10748.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 7+ messages in thread
From: Luca Ferrari @ 2013-12-12 15:21 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs

On Wed, Dec 11, 2013 at 4:06 PM, Emanuel Berg <embe8573@student.uu.se> wrote:

> I don't know. That seems like a cool idea though. Then
> you would see (i.e., not read) all the occurrences of
> that variable at once. However I don't know how it
> would work in practice.

I've used it within kdevelop and I have to say it takes a few to get
used to, but then it is really cool.


> If you were to put it up, I wouldn't recommend random
> colors, instead you could use the variable *name* (the
> letters) as function input to be mapped to colors: the
> first letter - the ASCII position, "normalized" - could
> be red, the second - same for green - etc.
>

Using the variable name is a good idea, even if names could easily
produce a clash.
However, any suggestion on where to start (in elisp code) for variable
font face?

Thanks,
Luca



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: random color of variables in programming languages
       [not found]   ` <mailman.9022.1386861695.10748.help-gnu-emacs@gnu.org>
@ 2013-12-12 21:05     ` Emanuel Berg
  2013-12-20  1:36     ` Emanuel Berg
  1 sibling, 0 replies; 7+ messages in thread
From: Emanuel Berg @ 2013-12-12 21:05 UTC (permalink / raw)
  To: help-gnu-emacs

Luca Ferrari <fluca1978@infinito.it> writes:

> Using the variable name is a good idea, even if names
> could easily produce a clash.  However, any
> suggestion on where to start (in elisp code) for
> variable font face?

To setup the faces of a major mode, the way I did it
was to first define the faces, then setup regular
expressions (in groups), and then hook those groups to
faces. If there isn't any groups (or just one,
perhaps), you don't have to use the digits to hook the
groups, but can use the dotted pair notation (check out
fpscalc-comment below).

However, I am unsure if this can be twisted to do
"dynamic faces".

If not, we'll have to move closer to where actual
coloring is done. Does the Emacs idle-timer invoke
that?  Could you insert something in between?

Or what about a hook for each completed word? Or would
that be too slow?

;; ... the other faces

(defface fpscalc-semaphore-name
  '((t :inherit font-lock-variable-name-face :background "black" :foreground "yellow" :bold t))
  "The word after the word semaphore, after the left parenthesis."
  )

(defvar fpscalc-keywords
  '(("\\(!.*$\\)" . 'fpscalc-comment)
    ("\\(semaphore\\)\\((\\)\\([[:word:]]+\\)\\(\\, \\)\\([[:word:]]+\\)"
     (1 'fpscalc-semaphore-keyword)
     (3 'fpscalc-semaphore-name)
     (5 'fpscalc-task-name-and-index) )
    ("declarations\\|initialise\\|semaphores\\|formulas" . 'fpscalc-program-parts)
    ("sigma\\|floor\\|ceiling\\|min\\|max" . 'fpscalc-math-functions)
    ("lp\\|ep\\|hp\\|all" . 'fpscalc-task-priority-sets)
    ("\\([[:word:]]\\)\\(\\[\\)\\(i\\)\\(\\]\\)"
     (1 'fpscalc-variable-name)
     (3 'fpscalc-i-task) )
    ("\\([[:word:]]\\)\\(\\[\\)\\(j\\)\\(\\]\\)"
     (1 'fpscalc-variable-name)
     (3 'fpscalc-j-task))
    ("\\([[:word:]]\\)\\(\\[\\)\\(.*\\)\\(\\]\\)"
     (1 'fpscalc-variable-name)
     (3 'fpscalc-task-name-and-index))
    ("\\(system \\)\\(.* \\)"
     (1 'fpscalc-system-keyword)
     (2 'fpscalc-system-name))
    ("\\(scalar\\|indexed\\|priority\\|blocking\\)\\( \\)+\\(\\(\\([[:word:]]\\( *\\, *\\)?\\)\\)*\\)"
     (1 'fpscalc-variable-type)
     (3 'fpscalc-variable-name))
    ("\\(tasks +\\)\\(\\(\\([[:word:]]\\( *\\, *\\)?\\)\\)*\\)"
     (1 'fpscalc-tasks-keyword)
     (2 'fpscalc-task-name-and-index))
    ))

-- 
Emanuel Berg, programmer-for-rent. CV, projects, etc at uXu
underground experts united:  http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: random color of variables in programming languages
       [not found]   ` <mailman.9022.1386861695.10748.help-gnu-emacs@gnu.org>
  2013-12-12 21:05     ` Emanuel Berg
@ 2013-12-20  1:36     ` Emanuel Berg
  2013-12-23 14:51       ` Luca Ferrari
       [not found]       ` <mailman.10142.1387810292.10748.help-gnu-emacs@gnu.org>
  1 sibling, 2 replies; 7+ messages in thread
From: Emanuel Berg @ 2013-12-20  1:36 UTC (permalink / raw)
  To: help-gnu-emacs

Luca Ferrari <fluca1978@infinito.it> writes:

> any suggestion on where to start (in elisp code) for
> variable font face?

Cool dump: http://user.it.uu.se/~embe8573/colvar.png

(save-excursion
  (require 'thingatpt) ; word-at-point
  (if (search-forward-regexp "defvar " (point-max) t)
      (let*((var-name (word-at-point))
            (color-code (car (string-to-list var-name)))
            (the-face (if (= color-code 114) ; if it starts with 'r'
                     'font-lock-warning-face ; use this face
                     'font-lock-variable-face)) )
        (font-lock-add-keywords
         'emacs-lisp-mode
         `((,var-name . ,the-face)))
        (emacs-lisp-mode) ))) ; <-- hit this

(defvar red 1)
(defvar another-variable 4)

(if (= red 1) t nil)

-- 
Emanuel Berg, programmer-for-rent. CV, projects, etc at uXu
underground experts united:  http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: random color of variables in programming languages
  2013-12-20  1:36     ` Emanuel Berg
@ 2013-12-23 14:51       ` Luca Ferrari
       [not found]       ` <mailman.10142.1387810292.10748.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 7+ messages in thread
From: Luca Ferrari @ 2013-12-23 14:51 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs

On Fri, Dec 20, 2013 at 2:36 AM, Emanuel Berg <embe8573@student.uu.se> wrote:
> Luca Ferrari <fluca1978@infinito.it> writes:
>
>> any suggestion on where to start (in elisp code) for
>> variable font face?
>
> Cool dump: http://user.it.uu.se/~embe8573/colvar.png
>
> (save-excursion
>   (require 'thingatpt) ; word-at-point
>   (if (search-forward-regexp "defvar " (point-max) t)
>       (let*((var-name (word-at-point))
>             (color-code (car (string-to-list var-name)))
>             (the-face (if (= color-code 114) ; if it starts with 'r'
>                      'font-lock-warning-face ; use this face
>                      'font-lock-variable-face)) )
>         (font-lock-add-keywords
>          'emacs-lisp-mode
>          `((,var-name . ,the-face)))
>         (emacs-lisp-mode) ))) ; <-- hit this
>
> (defvar red 1)
> (defvar another-variable 4)
>
> (if (= red 1) t nil)

Very good example, thanks.

Luca



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: random color of variables in programming languages
       [not found]       ` <mailman.10142.1387810292.10748.help-gnu-emacs@gnu.org>
@ 2013-12-24 19:31         ` Emanuel Berg
  0 siblings, 0 replies; 7+ messages in thread
From: Emanuel Berg @ 2013-12-24 19:31 UTC (permalink / raw)
  To: help-gnu-emacs

Luca Ferrari <fluca1978@infinito.it> writes:

> Very good example, thanks.

Yes, the example is good (or at least interesting) but
I think to actually make it work would require some
work.

First problem is, when to update the table?

Could it be done periodically without slowing
everything down? Remember the idle-timer, that perhaps
is suitable for a thing like that.

Or, what about the method with abbrev, when instead of
inserting a word, you evaluate code, as in:

("word" "" (lambda ()
              (interactive)
              ;; do stuff ))

Say that you define a variable with `int' (as in C),
then you could add "int" as such an "abbrev", and that
word would set a flag that indicates that work must be
done. If the idle-timer executes, but that flag isn't
set, no work is done (and the flag is unset at the end
of a "work burst").

Second problem is, how to remove stuff from the table?
When an int var_digit; line is removed, should
highlighting be dropped from the "var_digit"? Really, I
don't know, perhaps it is only good that it still
sticks out because each occurrence is a likely bug at
that state.

Perhaps it is easiest to have a separate keyword table,
and for each update, erase the whole thing and build it
anew.

Anyway, if you do anything more on this, be sure to
tell us.

-- 
Emanuel Berg, programmer-for-rent. CV, projects, etc at uXu
underground experts united:  http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2013-12-24 19:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.8911.1386765750.10748.help-gnu-emacs@gnu.org>
2013-12-11 16:06 ` random color of variables in programming languages Emanuel Berg
2013-12-12 15:21   ` Luca Ferrari
     [not found]   ` <mailman.9022.1386861695.10748.help-gnu-emacs@gnu.org>
2013-12-12 21:05     ` Emanuel Berg
2013-12-20  1:36     ` Emanuel Berg
2013-12-23 14:51       ` Luca Ferrari
     [not found]       ` <mailman.10142.1387810292.10748.help-gnu-emacs@gnu.org>
2013-12-24 19:31         ` Emanuel Berg
2013-12-11 12:42 Luca Ferrari

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.