emacs custom php tags highlight syntax - emacs

I want to set custom color background in tags, but other do not touch
(define-derived-mode php-mode fundamental-mode
"php-mode"
:syntax-table php-syntax-table
(setq font-lock-defaults '(php-keywords)))
(defvar php-syntax-table (make-syntax-table) "Syntax table for php-mode")
(make-face 'php-region-face)
(set-face-background 'php-region-face "red")
(setq php-keywords '(("<\\?php[[:ascii:]]*?\\?>" 0 'php-region-face t)))
But highlight background tags is not correctly, see below:

You'll want to setup multiline font-lock and define the boundaries of the font-lock search (so it doesn't take too much time). My information (and this code) comes from this SO question.
Here, I define multiline font-locking to take place within tags (< ... >). If this is added to your define-derived mode, it works as you describe.
(set (make-local-variable 'font-lock-multiline) t)
(add-hook 'font-lock-extend-region-functions
'test-font-lock-extend-region)
(defun test-font-lock-extend-region ()
"Extend the search region to include an entire SGML tag."
;; Avoid compiler warnings about these global variables from font-lock.el.
;; See the documentation for variable `font-lock-extend-region-functions'.
(eval-when-compile (defvar font-lock-beg) (defvar font-lock-end))
(save-excursion
(goto-char font-lock-beg)
(let ((found (or (re-search-backward "<" nil t) (point-min))))
(goto-char font-lock-end)
(when (re-search-forward ">" nil t)
(beginning-of-line)
(setq font-lock-end (point)))
(setq font-lock-beg found))))
EDIT: for some reason, SO doesn't like my code formatting.

Related

How to highlight tab characters for files from specific project in Emacs

I have this code to highlight tab characters and want to disable them using project-specifics macro:
(require 'highlight-chars)
(make-variable-buffer-local 'prevent-highlight-tabs)
(setq highlight-chars-disable '(term-mode erc-mode fundamental-mode))
(setq-default prevent-highlight-tabs nil)
(add-hook 'font-lock-mode-hook
(lambda()
(message "lock")
(when (and (null (memql major-mode highlight-chars-disable))
(not prevent-highlight-tabs))
(message "%s" prevent-highlight-tabs)
(hc-highlight-tabs))))
(custom-set-faces '(hc-tab ((t (:background "red")))))
and project-specifics is a macro that define add find-file-hook and dired-after-readin-hook from this question
(project-specifics "projects/test"
(message "specific")
(setq prevent-highlight-tabs t)
(setq indent-tabs-mode t))
What I wanted to do is disable red tabs (I want them because in most projects I want only spaces, and want to see tabs) for files in project/test, but I have a problem because the code from font-lock-mode-hook is executing before project-specifics (find-file-hook), and prevent-highlight-tabs is always nil in font-lock-mode-hook. Why is that, and how to fix it?
I suggest that you place your settings in .dir-locals.el as described in Per-Directory Local Variables.
What you are doing seems convoluted.
And it is partly undefined (here) - e.g., what is the major mode highlight-chars-disable? That is not defined in library highlight-chars.el, and it doesn't sound like something that would be a good candidate for a major mode.
See the Commentary of library highlight-chars.el for suggestions.
Something like this, perhaps (and you would put property prevent-highlight-tabs on any major-mode symbols you like:
(add-hook 'font-lock-mode-hook
(lambda () (unless (get major-mode 'prevent-highlight-tabs)
(hc-highlight-tabs))))
Or something like this (from the Commentary):
(add-hook 'change-major-mode-hook
(lambda ()
(add-hook 'font-lock-mode-hook 'hc-highlight-tabs)))
(add-hook 'after-change-major-mode-hook
(lambda ()
(when (eq major-mode 'THE-MODE)
(remove-hook 'font-lock-mode-hook 'hc-highlight-tabs)
(hc-dont-highlight-tabs)))
'APPEND)

How to highlight all the function's name in Emacs' lisp-mode?

How to highlight all the function's name in Emacs' lisp-mode? I want them bolded.
In other words, all the words from ( to the first space. Don't care exceptions like (a . b)
Just like GitHub:
Use this:
(defface font-lock-func-face
'((nil (:foreground "#7F0055" :weight bold))
(t (:bold t :italic t)))
"Font Lock mode face used for function calls."
:group 'font-lock-highlighting-faces)
(font-lock-add-keywords
'emacs-lisp-mode
'(("(\\s-*\\(\\_<\\(?:\\sw\\|\\s_\\)+\\)\\_>"
1 'font-lock-func-face)))
A funny thing: this messes up with let bindings, just like Github.
But that's what you asked for, right:)?
The code below highlights the names of known Emacs-Lisp functions.
Be aware that it does so even if some occurrence of a given function name does not represent the function. For example, the name might be used as a variable name. Not a big problem in practice, but good to know.
;; `setq' is a variable here, but it is highlighted anyway.
(let ((setq (foobar)))...)
To turn on the highlighting automatically in Emacs-Lisp mode, do this:
(font-lock-add-keywords 'emacs-lisp-mode
'((my-fl . 'font-lock-constant-face)) ; Or whatever face you want. 'APPEND)
(defun my-fl (_limit)
(let ((opoint (point))
(found nil))
(with-syntax-table emacs-lisp-mode-syntax-table
(while (not found)
(cond ((condition-case ()
(save-excursion
(skip-chars-forward "'")
(setq opoint (point))
(let ((obj (read (current-buffer))))
(and (symbolp obj) (fboundp obj)
(progn (set-match-data (list opoint (point))) t))))
(error nil))
(forward-sexp 1)
(setq opoint (point)
found t))
(t
(if (looking-at "\\(\\sw\\|\\s_\\)")
(forward-sexp 1)
(forward-char 1)))))
found)))
Note: If you want to see the effect of only this highlighting, then first do this in an Emacs-Lisp mode buffer, to get rid of other Emacs-Lisp font-lock highlighting:
M-: (setq font-lock-keywords ()) RET
UPDATE ---
I created a minor-mode command and library for this:
Code: hl-defined.el
Description
It lets you highlight defined Emacs-Lisp symbols: functions and variables, only functions, or only variables. Alternatively you can highlight only symbols not known to be defined.

Changing cursor color based on a minor mode

I have written a minor-mode, it defines some key bindings and does some initialization. The mode properly sets up Navi-mode and Navi-mode-map.
How do I enhance this minor-mode to change the color of the cursor when Navi-mode is enabled, and change it back whenever the mode is disabled? Can I use the hook Navi-mode-hook?
Try this:
(define-minor-mode foo-mode "doodad" :lighter ""
(if foo-mode
(setq cursor-type 'bar)
(setq cursor-type (default-value 'cursor-type))))
Or, if you anticipate cursor-type to already have a non-default value, you can save it when the mode is enabled and restore the saved value when it's disabled.
Either you have total control of the minor mode (because you wrote it), and you can embed this behaviour directly in your minor-mode function as explained in Dmitry's answer:
(define-minor-mode navi-mode
"Navi mode does wonderful things"
:lighter " Navi"
:global t
:init-value 0
(if navi-mode
(progn
(setq old-cursor-color (cdr (assoc 'cursor-color (frame-parameters))))
(set-cursor-color "red"))
(set-cursor-color old-cursor-color)))
Or you don't control the minor mode definition and you'll have to use a hook:
(defun navi-change-cursor-color ()
(if navi-mode
(progn
(setq old-cursor-color (cdr (assoc 'cursor-color (frame-parameters))))
(set-cursor-color "red"))
(set-cursor-color old-cursor-color)))
(add-hook 'navi-mode-hook 'navi-change-cursor-color)

Disable auto-fill-mode locally (or un fill-paragraph) with emacs

I use M-q for fill-paragraph, can I do the un-fill-paragraph in auto-fill-mode?
With org mode, I sometimes enter [[Very long HTML][Name with spaces]], and for the 'Name with spaces' the auto-fill mode break the whole line based on the inserted space, which makes it very ugly.
Is there a command something like un-fill-paragraph? Or, is there a way disable auto-fill-mode temporarily/locally?
Emacs does not record what was your line before calling fill-paragraph. So the only thing you can do is C-_ which runs the command undo. It can undo your fill-paragraph command but only if it is the preceding command call.
If you want to put a multi-line paragraph on one line you could do like this :
Select the region
C-M-% C-q C-j RET SPACE RET !
Xah Lee has updated his code since monotux's answer, and I refactored it somewhat for readability:
(defun my-toggle-fill-paragraph ()
;; Based on http://xahlee.org/emacs/modernization_fill-paragraph.html
"Fill or unfill the current paragraph, depending upon the current line length.
When there is a text selection, act on the region.
See `fill-paragraph' and `fill-region'."
(interactive)
;; We set a property 'currently-filled-p on this command's symbol
;; (i.e. on 'my-toggle-fill-paragraph), thus avoiding the need to
;; create a variable for remembering the current fill state.
(save-excursion
(let* ((deactivate-mark nil)
(line-length (- (line-end-position) (line-beginning-position)))
(currently-filled (if (eq last-command this-command)
(get this-command 'currently-filled-p)
(< line-length fill-column)))
(fill-column (if currently-filled
most-positive-fixnum
fill-column)))
(if (region-active-p)
(fill-region (region-beginning) (region-end))
(fill-paragraph))
(put this-command 'currently-filled-p (not currently-filled)))))
To remake a long line out of a paragraph in Org mode, I gave myself a new command. Here is the associated Emacs Lisp code:
(defun fp-unfill-paragraph (&optional justify region)
(interactive (progn
(barf-if-buffer-read-only)
(list (if current-prefix-arg 'full) t)))
(interactive)
(let ((fill-column 100000))
(fill-paragraph justify region)))
(global-set-key "\C-ceu" 'fp-unfill-paragraph)
Of course, you adjust the command keybinding as you see fit!
I use the following snippet to fill and un-fill paragraphs (using only M-q), it is really, really handy. I borrowed it from Xah Lee, but removed some comments and whitespace in order to make it fit in here. The link in the first comment goes to his original code.
;; http://xahlee.org/emacs/modernization_fill-paragraph.html
(defun compact-uncompact-block ()
"Remove or add line endings on the current block of text.
This is similar to a toggle for fill-paragraph and unfill-paragraph
When there is a text selection, act on the region.
When in text mode, a paragraph is considered a block. When in programing
language mode, the block defined by between empty lines.
Todo: The programing language behavior is currently not done.
Right now, the code uses fill* functions, so does not work or work well
in programing lang modes. A proper implementation to compact is replacing
newline chars by space when the newline char is not inside string.
"
(interactive)
(let (bds currentLineCharCount currentStateIsCompact
(bigFillColumnVal 4333999) (deactivate-mark nil))
(save-excursion
(setq currentLineCharCount
(progn
(setq bds (bounds-of-thing-at-point 'line))
(length (buffer-substring-no-properties (car bds) (cdr bds)))))
(setq currentStateIsCompact
(if (eq last-command this-command)
(get this-command 'stateIsCompact-p)
(if (> currentLineCharCount fill-column) t nil)))
(if (and transient-mark-mode mark-active)
(if currentStateIsCompact
(fill-region (region-beginning) (region-end))
(let ((fill-column bigFillColumnVal))
(fill-region (region-beginning) (region-end)))
)
(if currentStateIsCompact
(fill-paragraph nil)
(let ((fill-column bigFillColumnVal))
(fill-paragraph nil))))
(put this-command 'stateIsCompact-p
(if currentStateIsCompact
nil t)))))
(global-set-key (kbd "M-q") 'compact-uncompact-block)

Suppress emacs auto-fill in a selected region

I use emacs to edit everything. On some of my LateX documents I would like to automatically disable auto-fill mode when I am editing tables and code. Basically, I'd like to have two tags, like:
%%% BEGIN NO FILL
%%% END NO FILL
and nothing between them will be autofilled.
Can anybody think of a way to do this? I would need to figure out whether or not the cursor is inside the region and then have to toggle the mode, and would need to do that every time the cursor moved. Or is there a better way to do it?
If you are using AUCTeX (you should be) then you may want to check out LaTeX-indent-environment-list. Adding an environment to this variable will make it so that (among other things) M-q doesn't refill the paragraph. Unfortunately it doesn't seem work for auto-fill-mode. The following largely untested code added to LaTeX-mode-hook might do what you want.
(setq auto-fill-function
(lambda ()
(unless (> (save-excursion (or (search-backward "%%% BEGIN NO FILL" (point-min) t) 0))
(save-excursion (or (search-backward "%%% END NO FILL" (point-min) t) 0)))
(do-auto-fill))))
It's very stupid and inefficient, but seems to be fast enough on my machine. It doesn't allow nesting, and requires that you manually mark up all sections that you don't want filled. What I am thinking of adding to my .emacs (until I read your question I didn't realize how much this bugged me) is below which keys off of the current environment so there is no need for special markup (though it only looks at the innermost environment (I'm not sure how much of a problem that will cause in practice)). Combining the two is left as an exercise to the interested reader.
;; You can use the following to unset the variables and play around with them
;; (makunbound 'auto-fill-ignore-environments)
;; (makunbound 'auto-fill-ignore-environments-regexp)
(defcustom auto-fill-ignore-environments
(mapcar 'car LaTeX-indent-environment-list)
"List of environments for which `auto-fill-mode' should be
disabled. Used to generate `auto-fill-ignore-environments-regexp'."
:type '(sexp)
)
(defcustom auto-fill-ignore-environments-regexp
(regexp-opt auto-fill-ignore-environments)
"Regexp matching LaTeX environments for which `auto-fill-mode'
should be disabled. If not set, automatically generated from
`auto-fill-ignore-environments'"
:type '(string)
:set-after '(auto-fill-ignore-environments)
)
(add-hook 'LaTeX-mode-hook
(lambda ()
(setq auto-fill-function
(lambda ()
(unless (string-match auto-fill-ignore-environments-regexp
(LaTeX-current-environment))
(do-auto-fill))))))
I have never used defcustom before so I'm sure that part could be improved quite a bit.
Got it. Check this out:
(defun in-no-auto-fill-region ()
(> (save-excursion (or (search-backward "%%% BEGIN NO FILL" (point-min) t) 0))
(save-excursion (or (search-backward "%%% END NO FILL" (point-min) t) 0))
))
(defun previous-line-checking-auto-fill (arg)
(interactive "P")
(previous-line arg)
(if (in-no-auto-fill-region)
(turn-off-auto-fill)
(turn-on-auto-fill)))
(defun next-line-checking-auto-fill (arg)
(interactive "P")
(next-line arg)
(if (in-no-auto-fill-region)
(turn-off-auto-fill)
(turn-on-auto-fill)))
(add-hook 'LaTeX-mode-hook
'(lambda nil
(local-set-key "C-p" 'previous-line-checking-auto-fill)
(local-set-key "C-n" 'next-line-checking-auto-fill)
(auto-fill-mode 1)
))
Alternately, you can turn off auto-fill-mode and use M-q to format paragraphs. I don't love auto-fill's jumpiness so I use this in every mode.
If you want to go the route of advising/redefining all the movement functions, this should help:
(defmacro movement-advice (func)
`(defadvice ,func (after ; run this after the original function is done (and point has moved)
;; Give it a unique name
,(intern (concat (symbol-name func) "-auto-fill-auto-off"))
;; Hopefully this satisfies the arguments of any function we can throw at it
(&rest args)
;; turn it on
activate
)
"Turn auto-fill-mode on or off automatically."
(auto-fill-mode (not (in-no-auto-fill-region)))))
(dolist (func '(next-line
previous-line
forward-paragraph
backward-paragraph
mouse-drag-region
;; Whatever you use
))
(eval `(movement-advice ,func)))