Discussion:
Error in new mode for font-lock
(too old to reply)
AndreLTR
2016-02-07 18:30:43 UTC
Permalink
Hello,

I'm receiving the error below for a font-lock mode I'm developing:

Symbol's value as variable is void: font-lock

It occurs when I press M-x odba-mode. The related .emacs are below:

(global-font-lock-mode t)

(cond ((fboundp 'global-font-lock-mode)
;; turn on font-lock in all modes that support it
(global-font-lock-mode t)
;; maximum colors
(setq font-lock-maximum-decoration t)))

:;
;; odba mode
;;
(setq load-path (append '("/home/andreltr/.emacs.d/odba-mode/") load-path))
(add-to-list 'load-path "/home/andreltr/.emacs.d/odba-mode/myodba-mode.el")
(autoload 'odba-mode "myodba-mode" "Major mode for ODBA files" t)
;; Turn on font lock when in odba mode
(add-hook 'odba-mode-hook
'turn-on-font-lock)
(setq auto-mode-alist
(append
(list
'("\\.odba" . odba-mode))
auto-mode-alist))
(push (cons "\\.odba" 'odba-mode) auto-mode-alist)

The myodba-mode is below:

;; This is my suggestion for an odba mode
;; Based on the lsl-mode.el
;; André Luiz Tietbohl Ramos <***@gmail.com>

;; Example of font coloring:

;; RED -> comment
;; GREEN -> type
;; PURPLE -> "keyword"
;; PINK -> event
;; BLUE -> function

(require font-lock)

;; Use colors to highlight commands, etc.
(global-font-lock-mode t)

;;; Permite usar font-lock sempre
(cond ((fboundp 'global-font-lock-mode)
;; turn on font-lock in all modes that support it
(global-font-lock-mode t)
;; maximum colors
(setq font-lock-maximum-decoration t)))

;; odba-mode, my first major mode

(setq odba-highlights
'(("[PrefixDeclaration]\\|[SourceDeclaration]\\|[MappingDeclaration]"
. font-lock-function-name-face)
("sourceUri\\|connectionUrl\\|username\\|password\\|driverClass"
. font-lock-builtin-face)
("SELECT\\|FROM\\|WHERE" . font-lock-keyword-face)
("[[\\|]]" . font-lock-constant-face)
("@collection" . font-lock-type-face)))

(define-derived-mode odba-mode fundamental-mode
(setq font-lock-defaults '(odba-highlights))
(setq mode-name "odba lang"))

;; add the mode to the `features' list
(provide 'odba-mode)


;;; mylsl-mode.el --- sample major mode for editing LSL.

;; Copyright (c) 2015, by you

;; Author: your name ( your email )
;; Version: 2.0.13
;; Created: 26 Jun 2015
;; Keywords: languages
;; Homepage: http://ergoemacs.org/emacs/elisp_syntax_coloring.html

;; This file is not part of GNU Emacs.

;;; License:

;; You can redistribute this program and/or modify it under the terms of the
;; GNU General Public License version 2.

;;; Commentary:

;; short description here

;; full doc on how to use here

;;
;;; Code:

;; define several category of keywords
(setq odba-functions '("[PrefixDeclaration]" "[SourceDeclaration]" "[MappingDeclaration]"))
(setq odba-keywords '("sourceUri" "connectionUrl" "username" "password" "driverClass"))
(setq odba-types '("SELECT" "FROM" "WHERE"))
(setq odba-constants '("[[" "]]"))
(setq odba-events '("@collection"))

;; generate regex string for each category of keywords
(setq odba-keywords-regexp (regexp-opt odba-keywords 'words))
(setq odba-type-regexp (regexp-opt odba-types 'words))
(setq odba-constant-regexp (regexp-opt odba-constants 'words))
(setq odba-event-regexp (regexp-opt odba-events 'words))
(setq odba-functions-regexp (regexp-opt odba-functions 'words))

;; create the list for font-lock.
;; each category of keyword is given a particular face
(setq odba-font-lock-keywords
`(
(,odba-type-regexp . font-lock-type-face) ;;
(,odba-constant-regexp . font-lock-constant-face) ;;
(,odba-event-regexp . font-lock-builtin-face) ;;
(,odba-functions-regexp . font-lock-function-name-face) ;;
(,odba-keywords-regexp . font-lock-keyword-face)
;; note: order above matters, because once colored, that part won't change.
;; in general, longer words first
))

;;;###autoload
(define-derived-mode odba-mode fundamental-mode
"odba mode"
"Major mode for editing odba (Open Database Access Language)"

;; code for syntax highlighting
(setq font-lock-defaults '((odba-font-lock-keywords))))

;; clear memory. no longer needed
(setq odba-keywords nil)
(setq odba-types nil)
(setq odba-constants nil)
(setq odba-events nil)
(setq odba-functions nil)

;; clear memory. no longer needed
(setq odba-keywords-regexp nil)
(setq odba-types-regexp nil)
(setq odba-constants-regexp nil)
(setq odba-events-regexp nil)
(setq odba-functions-regexp nil)

;; add the mode to the `features' list
(provide 'odba-mode)

;; Local Variables:
;; coding: utf-8
;; End:

;;; odba-mode.el ends here

Any suggestions as to why it is happening, please? Regarding emacs-lisp, I'm quite a newbie.

TIA,

Andre Luiz
Michael Heerdegen
2016-02-07 18:48:08 UTC
Permalink
Post by AndreLTR
(require font-lock)
^

There's a quote missing:

(require 'font-lock)

"font-lock" here is the name of a feature (a symbol), not a variable.
So you want to specify the symbol, and due to Lisp evaluation rules,
..., etc.

But you don't need that require form anyway - font-lock is preloaded.


Regards,

Michael.
AndreLTR
2016-02-10 00:32:05 UTC
Permalink
Dear Michael,

Thanks for outlining the error, which was correct. Now, I tried the odba-mode below with no success for there are a blue color throughout the odba file chosen. Could someone point out a good tutorial about how to define my customized font-lock mode please?

;; This is my suggestion for an odba mode
;; Based on the lsl-mode.el
;; André Luiz Tietbohl Ramos <***@gmail.com>

;; Example of font coloring:

;; RED -> comment
;; GREEN -> type
;; PURPLE -> "keyword"
;; PINK -> event
;; BLUE -> function

(require 'font-lock)

;; Use colors to highlight commands, etc.
(global-font-lock-mode t)

;;; Permite usar font-lock sempre
(cond ((fboundp 'global-font-lock-mode)
;; turn on font-lock in all modes that support it
(global-font-lock-mode t)
;; maximum colors
(setq font-lock-maximum-decoration t)))

;; odba-mode, my first major mode

(setq odba-highlights
'(("[PrefixDeclaration]\\|[SourceDeclaration]\\|[MappingDeclaration]"
. font-lock-function-name-face)
("sourceUri\\|connectionUrl\\|username\\|password\\|driverClass"
. font-lock-builtin-face)
;; ("SELECT\\|FROM\\|WHERE" . font-lock-keyword-face)
("mappingId\\|target\\|source" . font-lock-keyword-face)
("[[\\|]]" . font-lock-constant-face)
("@collection" . font-lock-type-face)))

(define-derived-mode odba-mode fundamental-mode
(setq font-lock-defaults '(odba-highlights))
(setq mode-name "odba lang"))

;; add the mode to the `features' list
(provide 'odba-mode)


TIA,

Andre Luiz
Post by AndreLTR
Hello,
Symbol's value as variable is void: font-lock
(global-font-lock-mode t)
(cond ((fboundp 'global-font-lock-mode)
;; turn on font-lock in all modes that support it
(global-font-lock-mode t)
;; maximum colors
(setq font-lock-maximum-decoration t)))
:;
;; odba mode
;;
(setq load-path (append '("/home/andreltr/.emacs.d/odba-mode/") load-path))
(add-to-list 'load-path "/home/andreltr/.emacs.d/odba-mode/myodba-mode.el")
(autoload 'odba-mode "myodba-mode" "Major mode for ODBA files" t)
;; Turn on font lock when in odba mode
(add-hook 'odba-mode-hook
'turn-on-font-lock)
(setq auto-mode-alist
(append
(list
'("\\.odba" . odba-mode))
auto-mode-alist))
(push (cons "\\.odba" 'odba-mode) auto-mode-alist)
;; This is my suggestion for an odba mode
;; Based on the lsl-mode.el
;; RED -> comment
;; GREEN -> type
;; PURPLE -> "keyword"
;; PINK -> event
;; BLUE -> function
(require font-lock)
;; Use colors to highlight commands, etc.
(global-font-lock-mode t)
;;; Permite usar font-lock sempre
(cond ((fboundp 'global-font-lock-mode)
;; turn on font-lock in all modes that support it
(global-font-lock-mode t)
;; maximum colors
(setq font-lock-maximum-decoration t)))
;; odba-mode, my first major mode
(setq odba-highlights
'(("[PrefixDeclaration]\\|[SourceDeclaration]\\|[MappingDeclaration]"
. font-lock-function-name-face)
("sourceUri\\|connectionUrl\\|username\\|password\\|driverClass"
. font-lock-builtin-face)
("SELECT\\|FROM\\|WHERE" . font-lock-keyword-face)
("[[\\|]]" . font-lock-constant-face)
(define-derived-mode odba-mode fundamental-mode
(setq font-lock-defaults '(odba-highlights))
(setq mode-name "odba lang"))
;; add the mode to the `features' list
(provide 'odba-mode)
;;; mylsl-mode.el --- sample major mode for editing LSL.
;; Copyright (c) 2015, by you
;; Author: your name ( your email )
;; Version: 2.0.13
;; Created: 26 Jun 2015
;; Keywords: languages
;; Homepage: http://ergoemacs.org/emacs/elisp_syntax_coloring.html
;; This file is not part of GNU Emacs.
;; You can redistribute this program and/or modify it under the terms of the
;; GNU General Public License version 2.
;; short description here
;; full doc on how to use here
;;
;; define several category of keywords
(setq odba-functions '("[PrefixDeclaration]" "[SourceDeclaration]" "[MappingDeclaration]"))
(setq odba-keywords '("sourceUri" "connectionUrl" "username" "password" "driverClass"))
(setq odba-types '("SELECT" "FROM" "WHERE"))
(setq odba-constants '("[[" "]]"))
;; generate regex string for each category of keywords
(setq odba-keywords-regexp (regexp-opt odba-keywords 'words))
(setq odba-type-regexp (regexp-opt odba-types 'words))
(setq odba-constant-regexp (regexp-opt odba-constants 'words))
(setq odba-event-regexp (regexp-opt odba-events 'words))
(setq odba-functions-regexp (regexp-opt odba-functions 'words))
;; create the list for font-lock.
;; each category of keyword is given a particular face
(setq odba-font-lock-keywords
`(
(,odba-type-regexp . font-lock-type-face) ;;
(,odba-constant-regexp . font-lock-constant-face) ;;
(,odba-event-regexp . font-lock-builtin-face) ;;
(,odba-functions-regexp . font-lock-function-name-face) ;;
(,odba-keywords-regexp . font-lock-keyword-face)
;; note: order above matters, because once colored, that part won't change.
;; in general, longer words first
))
;;;###autoload
(define-derived-mode odba-mode fundamental-mode
"odba mode"
"Major mode for editing odba (Open Database Access Language)"
;; code for syntax highlighting
(setq font-lock-defaults '((odba-font-lock-keywords))))
;; clear memory. no longer needed
(setq odba-keywords nil)
(setq odba-types nil)
(setq odba-constants nil)
(setq odba-events nil)
(setq odba-functions nil)
;; clear memory. no longer needed
(setq odba-keywords-regexp nil)
(setq odba-types-regexp nil)
(setq odba-constants-regexp nil)
(setq odba-events-regexp nil)
(setq odba-functions-regexp nil)
;; add the mode to the `features' list
(provide 'odba-mode)
;; coding: utf-8
;;; odba-mode.el ends here
Any suggestions as to why it is happening, please? Regarding emacs-lisp, I'm quite a newbie.
TIA,
Andre Luiz
Michael Heerdegen
2016-02-10 11:14:19 UTC
Permalink
Post by AndreLTR
'(("[PrefixDeclaration]\\|[SourceDeclaration]\\|[MappingDeclaration]"
This is not what you want: "[" and "]" are characters interpreted
specially in regexp syntax: They define a character alternative. So the
above regexp matches nearly every letter of the alphabet. You need to
quote these character if you want them literally.

I don't know of any tutorial. This is a complicated matter spanning
several aspects of Emacs that are not the most basic ones.

I only can give you the advice to (1) learn how Emacs regexps work, (2)
learn how font lock works and (3) meanwhile, have a look how other
simple major modes implement their font-lock highlighting. This is
probably better than any tutorial. You are already on a good way - this
is not an easy task.


Regards,

Michael.

Continue reading on narkive:
Loading...