How to use different fonts within the same (org-mode) buffer? - emacs

Since I use Emacs' org-mode for academic writing, in the beginning of my .org files, I always have a long list of latex export settings of the form:
#+LATEX_HEADER: lorem ipsum dolor
#+LATEX_HEADER: lorem ipsum dolor
...
Or code snippets scattered around the document of the form:
#+BEGIN_SRC emacs-lips
lorem ipsum dolor
#+END_SRC
All these lines (both the lorem ipsum part and the #+BLAHBLAH part), I would like to have displayed in a mono spaced font, say Liberation Mono, that respects the custom colors and custom sizes I have defined elsewhere.
I searched, and I came to the conclusion that, as suggested here, using font-lock would be the way to go. Unfortunately, by looking at examples, I couldn't figure out on my own how to set it up properly. I tried the following, but either the regexp is wrong or the syntax is wrong:
(add-hook 'org-mode-hook
(lambda ()
(font-lock-add-keywords nil
'(("^\#\+.*$" 1
font-lock-comment-face t)))))
So, the question is: has anybody been able to work this out? Or, would anyone suggest a different way?

I was trying to get something similar going for me.
I came up with the following code, after poking around other answers here and in the Emacs Stack Exchange.
(add-hook 'org-mode-hook
'(lambda ()
(variable-pitch-mode 1) ;; All fonts with variable pitch.
(mapc
(lambda (face) ;; Other fonts with fixed-pitch.
(set-face-attribute face nil :inherit 'fixed-pitch))
(list 'org-code
'org-link
'org-block
'org-table
'org-block-begin-line
'org-block-end-line
'org-meta-line
'org-document-info-keyword))))
I tried it with Emacs 25.1 and Org-mode 9.0.3.

You can always create a heading * Configuration :ARCHIVE: with the ARCHIVE tag at the top of your file. That keeps the heading from being expanded, hiding it from normal view. The ARCHIVE tag
[...] does not open when you attempt to do so with a visibility cycling command (see Visibility cycling). You can force cycling archived
subtrees with C-TAB, or by setting the option
org-cycle-open-archived-trees. Also normal outline commands like
show-all will open archived subtrees.
During sparse tree construction (see Sparse trees), matches in archived subtrees are not exposed, unless you configure the option
org-sparse-tree-open-archived-trees.
During agenda view construction (see Agenda views), the content of archived trees is ignored unless you configure the option
org-agenda-skip-archived-trees, in which case these trees will always
be included. In the agenda you can press v a to get archives
temporarily included.
Archived trees are not exported (see Exporting), only the headline is. Configure the details using the variable
org-export-with-archived-trees.
Archived trees are excluded from column view unless the variable org-columns-skip-archived-trees is configured to nil.
LaTeX-exporting works if you set '(org-export-with-archived-trees nil).

Related

Does org-mode solve these 5 `hi-lock` problems?

For very many reference files, I am rather addicted to the combination of hi-lock, text-mode, and the very plain utility of TABs.
Here is a simple example. The file
hi-lock: (("===^^^===" (0 (quote compilation-line-number) t)))
hi-lock: (("http.?://.*" (0 (quote browse-url-button) t)))
===^^^===
===^^^===
Flutter Navigation
Routes and Navigation
Navigator https://api.flutter.dev/flutter/widgets/Navigator-class.html
Named Routes https://docs.flutter.dev/cookbook/navigation/named-routes
===^^^===
etc..
===^^^===
renders as follows.
This works nicely.
I can insert lines to highlight the divisions between sections.
I see HTTP links clearly highlighted (even if I still need to cut-and-paste to access them).
The TABs, as mentioned, provide adequate "section" and "subsection" semantics.
It's just text. This is not a trivial advantage, because my OS (macOS) indexes .txt files. There might be a way to nudge it to index other extensions. Still, the commands of any other file formats (as with, notably, LaTeX) will get in the way.
But there are also several problems.
I need to manually maintain (an otherwise unnecessary) hi-lock header for each file.
I need to maintain regular expressions (though this can be fun for tricky cases).
The semantics of hi-lock are dubious. (Why is compilation-line-number chosen for a separating line?)
Maintaining a reasonable color scheme becomes difficult after a few colors.
Maintaining indentation with TABs is laborious (as you can see from the misalignment of the URLs).
Does org-mode solve these problems while maintaining the advantages of text-mode and hi-lock?
If you're comfortable with ORG, I would particularly like to see how the snippet above can be rendered in an equally—or better—looking way using ORG.
Related:
Compare markdown or org mode
The spirit of the question is:
I couldn't be bothered to write configurations so frequently.
I'm seeking the largest signal-to-noise ratio.
The following is a remedy (working) solution, on the path to migrating to org-mode from a combination of text-mode and hi-lock.
Using the following .emacs file.
(add-to-list 'default-frame-alist '(background-color . "black"))
(add-to-list 'default-frame-alist '(foreground-color . "white"))
(set-frame-font "-*-*-medium-r-normal--24-*-*-*-*-*-*-*")
(setq org-startup-indented t)
(require 'org-superstar)
(add-hook 'org-mode-hook (lambda () (org-superstar-mode 1)))
The following lines
* Flutter Navigation
** Routes and Navigation
- Navigator https://api.flutter.dev/flutter/widgets/Navigator-class.html
- Named Routes https://docs.flutter.dev/cookbook/navigation/named-routes
* etc..
render as follows.
Though this addresses neither the separating lines nor the tabs.
Separating lines are easy, if they're meant for the output (PDF, etc) rather for the Emacs display itself.
Also...
Open URLs with the system-wide browser (
1,
2).

Overriding emacs org-mode faces

I am trying to override the default syntax highlighting in org-mode and org-agenda-mode buffers.
To clarify my intention: my current reason for doing this is to highlight headings (or parts of headings) based on their tags. The built-in variable org-tag-faces only allows customisation of the tag itself, not the heading containing the tag.
With reference to the following related questions:
Emacs font lock mode: provide a custom color instead of a face
https://emacs.stackexchange.com/questions/8211/color-code-a-new-generic-character-combination
https://emacs.stackexchange.com/questions/8222/how-to-make-the-custom-font-lock-keywords-not-override-the-default-major-mode-fo#comment12615_8222
In thread 2 the accepted answer is to use font-lock for this purpose.
In thread 3 I am trying to achieve the exact opposite of the poster. The last comment by Jordon Biondo says:
take out the t from your keywords, what that t specifies is that font-lock should override already colored things.
Since I want to override already coloured things I am adding in the t but as far as I can tell the org-mode highlighting is still overriding my custom face.
In org-mode buffers this manifests as the main body of the heading text being changed but any other items such as todo-states, dates, tags etc. retaining there existing faces.
In org-agenda-mode buffers it completely fails to modify any aspect of the matched lines.
By way of a simple example here is some code I'm trying to use to set any lines containing :TT: to red in org-mode buffers:
(add-hook 'org-mode-hook
(lambda ()
(font-lock-add-keywords
'org-mode
'(
("^.*:TT:.*$" 0 '(:foreground "#FF0000") t)
))))
This mostly works for me:
(add-hook 'org-mode-hook
(lambda ()
(font-lock-add-keywords
'org-mode
'(("^.*:TT:.*$" . font-lock-warning-face)))))
The headline is red, although the tag itself is not.

Paragraph filling for org-mode inside latex environment

I started using org-mode to write mathematical papers so I make a heavy use of latex environments such as proof, theorem, lemma, etc. For example I often write
\begin{proof}
a very long proof follows
\end{proof}
The problem is that in org-mode the fill-paragraph (or M-q) doesn't work inside latex environments. This complicates my life because some proofs can be very long, reaching several pages when compiled to pdf, and I am unable to efficiently format them in org-mode. I couldn't find any information in the manual on options controlling paragraph filling. Is it possible to enable fill-paragraph in this case?
The problem disappears if you use an org block instead of a latex environment:
#+BEGIN_proof
...
#+END_proof
This gets exported as \begin{proof}...\end{proof}. It also lets you use org syntax inside the block, and fill paragraph works.
if you don't want to do that, maybe try visual-line-mode as a workaround.
Edit: change fill-paragraph behaviour
If you want fill paragraph to work in latex environments, you have to dig a little deeper. Filling is done by org-fill-paragraph in org.el and this function ignores latex environments by default. To change this, go to the end of the function and replace
;; Ignore every other element.
(otherwise t)
with
(latex-environment nil) ;; use default fill-paragraph
;; Ignore every other element.
(otherwise t)
If you'd rather not change the org sources, you could use advice instead, e.g.
(defun org-fill-paragraph--latex-environment (&rest args)
"Use default fill-paragraph in latex environments."
(not (eql (org-element-type (org-element-context)) 'latex-environment)))
(advice-add 'org-fill-paragraph :before-while #'org-fill-paragraph--latex-environment)
I have a command that you could bind to M-q in Org:
(defun leuven-good-old-fill-paragraph ()
(interactive)
(let ((fill-paragraph-function nil)
(adaptive-fill-function nil))
(fill-paragraph)))
Though, I don't understand either why it's not enabled by default. Maybe a question for the Org ML?

Wrong indentation of comments in Emacs

In many languages, the line comment starts with a single symbol, for example # in Python and R.
I find that in Emacs, when writing such line comments, I have to repeat the comment symbol twice to make the correct indentation.
See the following example:
(setq x-select-enable-clipboard t)
;using a single comment symbol indents wrongly
;; repeating the comment symbol indents fine
(setq-default c-basic-offset 4)
With a single ; at the beginning of the line cannot get the correct indentation. How to get the correct setting? Thanks!
EDIT:
I found the solution myself. In ESS's document:
Comments are also handled specially by ESS, using an idea borrowed
from the Emacs-Lisp indentation style. By default, comments beginning
with ‘###’ are aligned to the beginning of the line. Comments
beginning with ‘##’ are aligned to the current level of indentation
for the block containing the comment. Finally, comments beginning with
‘#’ are aligned to a column on the right (the 40th column by default,
but this value is controlled by the variable comment-column,) or just
after the expression on the line containing the comment if it extends
beyond the indentation column. You turn off the default behavior by
adding the line (setq ess-fancy-comments nil) to your .emacs file.
So I put this in my .emacs:
(setq ess-fancy-comments nil) ; this is for ESS
I think for Python mode, it has a similar variable.
Your example use Emacs Lisp, in this language the standard convention is that a single ; is indented to the right, whereas two ;; is indented like code would be indented at that point. I strongly recommend that you stick to this convention, otherwise your code would stand out as being different. And three ;;; is indented to the left. Four ;;;; is left indented, and used for major sections. (See https://www.gnu.org/software/emacs/manual/html_node/elisp/Comment-Tips.html)
For Ruby, comments always indent as code, as far as I know.
The major mode should take care of this properly. If not, consider filing an enhancement request or bug report to the maintainers. Of course, "properly" might be in the eye of the beholder. You can try to make your preferences known, however. And check whether the major-mode code might already have user options for this.
Beyond that, the function that is the value of variable comment-indent-function governs this. Normally, this is set by the major mode. You can set it to any function you want (e.g. on the mode hook, so that your definition overrides the one provided by the major-mode code).
It accepts no arguments, and it returns the column you want the comment to be indented to.
Here is code that indents a comment to column 0, for example:
(defun foo () (setq comment-indent-function (lambda () 0)))
(add-hook 'SOME-MODE-HOOK 'foo 'APPEND)
For Emacs-Lisp mode, for example, you would use (add-hook 'emacs-lisp-mode-hook 'foo 'APPEND).

Colorize snippets of text in emacs

Suppose I have a few words I would like to highlight, so I want to change the color of those few words only to, say, green.
Is there an easy way to do this in emacs?
Thank you.
This is what I've done, using font-lock-add-keywords. I wanted to highlight the words TODO:, HACK:, and FIXME: in my code.
(defface todo-face
'((t ()))
"Face for highlighting comments like TODO: and HACK:")
(set-face-background 'todo-face cyan-name)
;; Add keywords we want highlighted
(defun add-todo-to-current-mode ()
(font-lock-add-keywords nil
'(("\\(TODO\\|HACK\\|FIXME\\):" 1 'todo-face prepend))
t))
Use library HighLight. You can use overlays or text properties. You can save the highlighting permanently or let it be temporary. You can highlight in many ways (regexp, mouse-drag,...). Lots of possibilities.
The highlight package has hlt-highlight-regexp-region and hlt-highlight-regexp-to-end, which do exactly what you want.
http://www.emacswiki.org/cgi-bin/wiki/highlight.el
Use the function font-lock-add-keywords to define a new matcher for the string in question, binding that matcher to some face you've defined that will display as green. For example:
(font-lock-add-keywords nil
'("\\<foo\\>" 0 my-green-face))
Note that you can specify a particular mode where I wrote nil above, and the matching forms can take on any of six different styles. See the documentation for the variable font-lock-keywords for the rules and a few examples.
If you want them highlighted only temporarily, I find M-x highlight-regexp command very helpful, it is especially nice for looking through log files of sorts. For example you made yourself a logging class that outputs some tracing info like MyClass::function() > when function is run and MyClass::function() < when it exits (can be especially useful sometimes when debugging multithreading issues) then you just ask emacs to highlight some of them green and other red and then you can see how did the execution go.
I use what Dimitri suggested. In particular, I have the following two lines in my .emacs
(global-hi-lock-mode t)
(global-set-key (kbd "C-M-h") 'highlight-regexp)
Every-time I need to highlight a certain word (or regex) in a buffer, I hit "C-M-h", which then prompts me for the word (or regex) I want to be displayed differently and then for a face to display it in.