Emacs overwrites ligatures - emacs

I am trying to get ligatures working in emacs with "Fantasque Sans Mono Nerd Font Mono" (that's its name...)
My script is the following:
(provide 'add-fantasque-ligatures)
(defconst fantasque-fontlock-keywords-alist
(mapcar (lambda (regex-char-pair)
`(,(car regex-char-pair)
(0 (prog1 ()
(compose-region (match-beginning 1)
(match-end 1)
,(concat (list ?\C-i)
(list (decode-char 'ucs (cadr regex-char-pair)))))))))
'(("\\(!=\\)" #xe10a)
("\\(==\\)" #xe103)
("\\(->\\)" #xe112)
("\\(<-\\)" #xe121)
("[^<]\\(<=\\)" #xe11b)
("\\(&&\\)" #xe038)
("[^>]\\(>=\\)" #xe10c))))
(defun add-fantasque-ligatures ()
(font-lock-add-keywords nil fantasque-fontlock-keywords-alist))
(add-hook 'prog-mode-hook
#'add-fantasque-ligatures)
It works fine, except for the &&. Emacs displays a play button in a filled circle. I penta-checked it, and U+e038 is its unicode value.
I have an idea, though, what emacs does here.
There are some other ligatures I checked too, like ||, and emacs displayed a play button in an unfilled circle. I changed the font, and the && and || still did not work.
I think there are some unicode characters, that emacs overwrites to make them font-independent. Is there a way though to overcome this problem, and display the correct ligatures?

Related

emacsclient font check not working

I am trying to set my font for emacsclient like so:
(let ((default-font (cond
((member "Inconsolata" (font-family-list))
"Inconsolata 14")
(t
"monospace 20"))))
(set-default-font default-font)
(add-to-list 'default-frame-alist `(font . ,default-font)))
I C-x C-e at the cond sexp and it returns "Inconsolata 14". I C-x C-e at the let sexp and the font is updated.
When I launch emacs via
$ emacs
it works (the font is set to Inconsolata 14).
However when I launch the application via
$ emacsclient --alternate-editor="" --create-frame "$#"
the font is monospace 20 instead.
Please advise.
EDIT:
I have discovered that by including
(message "%s" (font-family-list))
in my .emacs file that (font-family-list) returns nil when emacsclient is starting up.
Unfortunately, also during initialization:
;; Both also print `nil` to the `*Messages*` buffer.
(message "%s" (find-font (font-spec :name "inconsolata")))
(message "%s" (find-font (font-spec :name "Inconsolata")))
;; Throws "error: No fonts being used"
(message "%s" (describe-font "Inconsolata"))
I do not know how to detect if a font is installed during initialization. My question has become: How do I reliably check whether a font is available when emacsclient starts up?
EDIT 2:
Echoing in after-init-hook, emacs-startup-hook, window-setup-hook, before-make-frame-hook, and after-make-frame-functions also results in nil.
Sigh... was annoyed with this problem as well, but I found the Emacs Lisp solution. Here is a straight copy/paste of the respective snippet from my Emacs configuration:
(defun frame-font-setup
(&rest ...)
;; (remove-hook 'focus-in-hook #'frame-font-setup)
(unless (assoc 'font default-frame-alist)
(let* ((font-family (catch 'break
(dolist (font-family
'("Powerline Consolas"
"Consolas for Powerline"
"Consolas"
;;
"Powerline Inconsolata-g"
"Inconsolata-g for Powerline"
"Inconsolata-g"
;;
"Powerline Source Code Pro"
"Source Code Pro for Powerline"
"Source Code Pro"
;;
"Powerline DejaVu Sans Mono"
"DejaVu Sans Mono for Powerline"
"DejaVu Sans Mono"
;;
"Monospace"))
(when (member font-family (font-family-list))
(throw 'break font-family)))))
(font (when font-family (format "%s-12" font-family))))
(when font
(add-to-list 'default-frame-alist (cons 'font font))
(set-frame-font font t t)))))
(add-hook 'focus-in-hook #'frame-font-setup)
Mmmhhh... [Rejoice]
I managed to find an answer in this post in emacs subreddit.
Here is the code snippet that I put in in init.el:
(defun rag-set-face (frame)
"Configure faces on frame creation"
(select-frame frame)
(if (display-graphic-p)
(progn
(when (member "PragmataPro" (font-family-list))
(set-frame-font "PragmataPro-13")))))
When you start Emacs as a daemon (which is done implicitly by emacsclient on-demand), the .emacs is loaded before Emacs has made a connection to any "display device" (aka "terminal"), i.e. it is not connected to any GUI nor any tty. Instead its terminal is a dummy device which reads from stdin and sends the output to stdout (well, at the beginning and soon after, even that communication link is cut), so there are no fonts there.
One way to get what you want is to do something like:
(add-to-list face-font-family-alternatives '("myfont" "Inconsolata" "Monospace"))
and then to customize the default face to use the font family myfont. You may still have problems with the size of the font, in which case you may want to play with face-font-rescale-alist.
Seems to be pretty much impossible with elisp alone so I've settled for fc-list. This works.
(let ((default-font (cond
((and
(eq system-type 'gnu/linux)
(null (string= "" (shell-command-to-string "which fc-list")))
(null (string= "" (shell-command-to-string "fc-list inconsolata"))))
"Inconsolata 14")
(t
"monospace 12"))))
(set-default-font default-font)
(add-to-list 'default-frame-alist `(font . ,default-font)))

Emacs: highlight 2 or more empty lines

I have entered the following code in my .emacs file to highlight unwanted white spaces.
(require 'whitespace)
(setq whitespace-style '(face empty tabs lines-tail trailing))
(global-whitespace-mode t)
This shows (1) empty lines at the beginning & end of buffer
(2) tabs
(3) lines which go over the 80 character limit
(4) trailing white spaces
I would like emacs to automatically highlight '2 or more empty lines'. Any ideas on how to implement this? I did find a blog post explaining a way to do this with the help of regexp, but I am not sure how to implement this in .emacs file.
Edit 1: Found a way to delete extra blank lines but this still doesn't help me with highlighting multiple blank lines automatically. delete extra blank lines in emacs
Edit 2: Adding the following to .emacs seems to work, but only after I save and reopen file in a buffer.
(add-hook 'change-major-mode-hook '(lambda () (highlight-regexp "\\(^\\s-*$\\)\n" 'hi-yellow)))
Edit 3: After adding (global-hi-lock-mode 1) to .emacs file just before the line in Edit 2, it seems to highlight 1 or more empty lines within the buffer. I am not sure how to modify the regexp so that it will only accept 2 or more empty lines.
Just use library Highlight (highlight.el). That's what it's for.
Use command hlt-highlight-regexp-region (C-x X h x) or hlt-highlight-regexp-to-end (C-x X h e). (To unhighlight a regexp, use C-x X u x or C-x X u e.)
Interactively, you input the regexp to use as usual in Emacs (with C-q C-j to match a newline character, and no need for double backslashes), so you type \(^\s-*$\) C-q C-j.
Your highlight-regexp-solution can be made into a minor-mode with the following elisp (e.g., in your .emacs file).
You can activate the minor mode by right-clicking onto one of the mode-names in the mode-line and then selecting nl2-mode. You can deactivate the minor mode by clicking on nl2 in the mode line and selecting Turn off minor mode.
To understand the code see the help for define-minor-mode and define-key (e.g., C-h f define-minor-mode RET). Note, that in emacs also mouse clicks in menus count as key strokes.
(define-minor-mode nl2-mode
"Highlight two successive newlines."
:global t
:lighter " nl2"
(if nl2-mode
(highlight-regexp "\\(^\\s-*$\\)\n" 'hi-yellow)
(unhighlight-regexp "\\(^\\s-*$\\)\n")))
(define-key mode-line-mode-menu [nl2-mode]
`(menu-item ,(purecopy "nl2-mode") nl2-mode
:help "Highlight two succesive newlines."
:button (:toggle . (bound-and-true-p nl2-mode))))
There are several facts that make highlighting two consecutive empty lines more complicated (font-lock tends to only highlight non-empty regions, linebreaks are limits for the region to re-fontify, re-fontification after buffer changes are required).
The following code shows one way. Maybe, there are easier ways.
(require 'font-lock)
(global-font-lock-mode)
(defface jit-lock-nl2-face '((default :background "yellow"))
"Face to indicate two or more successive newlines."
:group 'jit-lock)
(defun jit-nl2-extend (start end &optional old)
"Extend region to be re-fontified"
(save-excursion
(save-match-data
;; trailing:
(goto-char end)
(skip-chars-forward "[[:blank:]]\n")
(setq jit-lock-end (point))
;; leading:
(goto-char start)
(beginning-of-line)
(skip-chars-backward "[[:blank:]]\n")
(unless (bolp) (forward-line))
(setq jit-lock-start (point)))))
(defun jit-nl2 (jit-lock-start jit-lock-end)
"Highlight two or more successive newlines."
(save-excursion
(save-match-data
(jit-nl2-extend jit-lock-start jit-lock-end)
;; cleanup
(remove-text-properties jit-lock-start jit-lock-end '(font-lock-face jit-lock-nl2-face))
;; highlight
(while (< (point) jit-lock-end)
(if (looking-at "[[:blank:]]*\n\\([[:blank:]]*\n\\)+")
(progn (put-text-property (match-beginning 0) (match-end 0) 'font-lock-face 'jit-lock-nl2-face)
(goto-char (match-end 0)))
(forward-line))))))
(add-hook 'after-change-major-mode-hook (lambda ()
(add-hook 'jit-lock-after-change-extend-region-functions 'jit-nl2-extend)
(jit-lock-register 'jit-nl2)
(jit-lock-mode 1)
))

emacs custom php tags highlight syntax

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.

How to configure Emacs to use variable fonts to display the previsualisation of LaTeX formulas?

I would like to use Emacs 24.2.1 in orgmode 7.9.3 on a data projector in class.
I can pre-visualize latex formulas using org-preview-latex-fragment (C-c C-x C-l), and
I can increase the font used to display the text using text-scale-increase (C-+).
The problem is that the math fonts are not increasing along the text fonts.
Is there a way to configure Emacs so that the math fonts are variable too?
Was this solved in a later version of orgmode (I was planning to wait for the end of the term to upgrade to version 8+, being afraid of having to invest a lot of time in updating my configuration).
A poor man's solution that works with advices and hooks is shown below.
Paste the code into your .emacs configuration file and re-start emacs. Afterwards, the equation images should be scaled at along with the text if you press C-+/-/0. They are just previews and their The bad thing is that the resolution is not enlarged. To get equation images with a good resolution hit C-c C-x C-l to regenerate the images. Note, that this sets the :scale property of the customization variable org-format-latex-options. The customization of this variable will be lost for the current emacs session.
You need a gnu-emacs with built-in imagemagick. Should be standard by now.
(defvar text-scale-mode-hook nil
"Hook run at end of command `text-scale-mode'.")
(defadvice text-scale-mode (after text-scale-mode-hooks nil activate)
"Run `text-scale-mode-hook' at end of command `text-scale-mode'."
(if (functionp text-scale-mode-hook)
(funcall text-scale-mode-hook)
(loop for hook in text-scale-mode-hook do
(if (eq hook 't)
(run-hooks (default-value text-scale-mode-hook))
(run-hooks hook)))))
(defun org-text-scale-eye ()
"Scale equation images according to text-scale-mode-amount."
(when (boundp 'text-scale-mode-amount)
(let ((relwidth (* (expt text-scale-mode-step text-scale-mode-amount))))
(loop for ol in (overlays-in (point-min) (point-max)) do
(when (eq (overlay-get ol 'org-overlay-type) 'org-latex-overlay)
(unless (overlay-get ol 'org-image-original-width)
(overlay-put ol 'org-image-original-width (car (image-size (overlay-get ol 'display) t))))
(let ((ol-disp-plist (cdr (overlay-get ol 'display))))
(setq ol-disp-plist (plist-put ol-disp-plist :type 'imagemagick))
(setq ol-disp-plist (plist-put ol-disp-plist :width (round (* relwidth (overlay-get ol 'org-image-original-width)))))
(overlay-put ol 'display (append '(image) ol-disp-plist))
))))
(force-window-update)
))
(add-hook 'org-mode-hook '(lambda () (add-hook 'text-scale-mode-hook 'org-text-scale-eye)))
(defadvice org-format-latex (before set-scale activate)
"Set :scale in `org-format-latex-options' to the scaling factor resulting from `text-scale-mode' and clear cache."
(let ((relwidth (expt text-scale-mode-step text-scale-mode-amount)))
(unless (= (plist-get org-format-latex-options :scale) relwidth)
(plist-put org-format-latex-options :scale relwidth))))
Actually, I had a look at the code in org.el:
The font height is regarded in the function org-create-formula-image-with-dvipng and the resolution option -D is set. Maybe, there goes something wrong. Furthermore, pityingly, in org-format-latex the images are cached and not reproduced after font size changes.
If you want to increase the size of the latex-formulas for one session in general with good quality you can set the :scale value of the customization option "Org Format Latex Options" in the group "org-latex" to a higher value.
Edit 2013-10-10 (marked as italic throughout the answer except the code, naturally): The :scale property of the option org-format-latex-options is now set automagically. Therefore, the above hack should be quite usable (such as the latex version mentioned below).
The same problem as you describe in the original-post exists for latex-preview. But, for that case there is a better solution:
(require 'face-remap)
(defadvice preview-inherited-face-attribute (after preview-inherit-local-face nil activate)
"Scale preview images with respect to buffer-local face"
(when (and text-scale-mode (eq attribute :height))
(setq ad-return-value (* (expt text-scale-mode-step text-scale-mode-amount) ad-return-value))))
After C-+/-/0 type C-c C-p C-d to re-generate all formulas of the document. Afterwards, the formulas have just the right size and the right resolution.
If switching to latex documents with preview is an option for you I would recommend it. I use org only for notes and time-tables with easy links.
For more descriptive documents I use latex documents with auctex/preview. But, I actually compile them only very seldom to a final pdf document. The preview is good enough. See the following Figure.

Emacs: TODO indicator on left fringe has a strange side-effect - deleting characters

I just read Emacs :TODO indicator at left side, and tried it out. It seems intriguing. The little indicator triangles appear, but I'm getting a weird side effect: the text itself is being altered. Characters are being deleted.
Before:
After:
The mode-line does indicate that the buffer has been altered after running annotate-todo.
What explains this?
(I'm using emacs 22.2.1 on Windows)
Ahhh... I see the error of my ways earlier. Here's a new version.
(defun annotate-todo ()
"put fringe marker on TODO: lines in the curent buffer"
(interactive)
(save-excursion
(goto-char (point-min))
(while (re-search-forward "TODO:" nil t)
(let ((overlay (make-overlay (- (point) 5) (point))))
(overlay-put overlay 'before-string (propertize (format "A")
'display '(left-fringe right-triangle)))))))
The first solution used a the 'display text property, which changes how the specified text is displayed, in this case it was replaced by the triangle in the left fringe. What I needed to do was to use a 'before-string overlay instead. Which doesn't change the string being displayed.
Another advantage, the cut/paste of the code annotated by this does not carry the markup.
I've updated the code in the original question to reflect this change as well.