Discussion:
regexp doubts
(too old to reply)
AndreLTR
2016-02-10 22:26:09 UTC
Permalink
Hello,

I need to colour the following character sequences but could not after many tries,

1 - [[ and ]] with space character before of after them.
2 - [ with a variable character string length afterword.
3 - ] with a variable character string length beforehand.
2 and 3 could form a single regexp since the keyword in question is variable.
4 - a single period at the end of a sentence with a space before it.
5 - the : character is preceded by a variable length string (including zero characters).

[ and ] shall have the same colour. Likewise for : and period. Could anyone help me, please?

TIA,
Michael Heerdegen
2016-02-11 13:45:32 UTC
Permalink
Post by AndreLTR
Hello,
I need to colour the following character sequences but could not after many tries,
1 - [[ and ]] with space character before of after them.
2 - [ with a variable character string length afterword.
3 - ] with a variable character string length beforehand.
2 and 3 could form a single regexp since the keyword in question is variable.
4 - a single period at the end of a sentence with a space before it.
5 - the : character is preceded by a variable length string (including zero characters).
[ and ] shall have the same colour. Likewise for : and period. Could
anyone help me, please?
Do you think you could figure it out yourself using rx.el? It's a quite
simple tool to construct regexps; you can include calls to the macro
`rx' in your code, they will be transformed when compiling your file.

You will probably want to use some syntax related forms in your regexps
to limit the text to match.


Regards,

Michael.
AndreLTR
2016-02-13 16:19:59 UTC
Permalink
Hello Michael,

After your suggestion, all done! THX! The answer is below.

(require 'font-lock)

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

;;; allows font-lock usage always
(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)))

(setq odba-highlights
'(("\\(?:\\[PrefixDeclaration]\\)\\|\\(?:\\[SourceDeclaration]\\)\\|\\(?:\\[MappingDeclaration]\\)" . font-lock-function-name-face)
("sourceUri\\|connectionUrl\\|username\\|password\\|driverClass\\|mappingId\\|target\\|source\\|\\(?: a \\)\\|\\(?: \\.\\)"
. font-lock-variable-name-face)
("@collection\\|^[[:blank:]]*]]\\|\\(?:\\[\\[\\)" . font-lock-keyword-face)
("[A-Za-z]+:\\|:\\|#" . font-lock-type-face)
("SELECT\\|FROM\\|WHERE\\|LIMIT\\|;\\|OR\\|AND" . 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)

I'm facing another issue now that it works: how could a .odba colorized automatically, please? I'm receiving the following warning when opening emacs alone:

Warning (initialization): An error occurred while loading `/home/andreltr/.emacs':
error: Required feature `odba-mode.el' was not provided

How come?

When opening emacs along with files the warning does not show. Plus, .odba files are colorized correctly when M-x odba-mode is done! The relevant section of my .emacs file is below. How to solve this, please?

:;
;; 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/odba-mode.el")
(autoload 'odba-mode "odba-mode" "Major mode for ODBA files" t)
(add-to-list 'auto-mode-alist '("\\.odba\\'" . odba-mode))
;; 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))
(require 'odba-mode.el)

TIA,

Andre Luiz
Post by AndreLTR
Hello,
I need to colour the following character sequences but could not after many tries,
1 - [[ and ]] with space character before of after them.
2 - [ with a variable character string length afterword.
3 - ] with a variable character string length beforehand.
2 and 3 could form a single regexp since the keyword in question is variable.
4 - a single period at the end of a sentence with a space before it.
5 - the : character is preceded by a variable length string (including zero characters).
[ and ] shall have the same colour. Likewise for : and period. Could anyone help me, please?
TIA,
Michael Heerdegen
2016-02-14 11:44:03 UTC
Permalink
Post by AndreLTR
(require 'font-lock)
;; Use colors to highlight commands, etc.
(global-font-lock-mode t)
;;; allows font-lock usage always
(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)))
In the Emacsen I know, this is all redundant, because it complies with
the defaults.
Post by AndreLTR
(define-derived-mode odba-mode fundamental-mode
(setq font-lock-defaults '(odba-highlights))
(setq mode-name "odba lang"))
It would probably be better to derive from `prog-mode'.
Post by AndreLTR
(setq auto-mode-alist
(append
(list
'("\\.odba\\" . odba-mode))
^^

This is probably not what you want, I think.
Post by AndreLTR
auto-mode-alist))
(require 'odba-mode.el)
You require a feature (analogously to `provide'), not a file name, so

(require 'odba-mode)


HTH,

Michael.

Loading...