(internal): move faces into own file (#987)

This commit is contained in:
Jethro Kuan 2020-07-30 09:12:56 +08:00 committed by GitHub
parent 6345d0c22e
commit 0443351800
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 111 additions and 68 deletions

67
org-roam-faces.el Normal file
View file

@ -0,0 +1,67 @@
;;; org-roam-faces.el --- Face definitions -*- coding: utf-8; lexical-binding: t; -*-
;; Copyright © 2020 Jethro Kuan <jethrokuan95@gmail.com>
;; Author: Jethro Kuan <jethrokuan95@gmail.com>
;; URL: https://github.com/org-roam/org-roam
;; Keywords: org-mode, roam, convenience
;; Version: 1.2.1
;; Package-Requires: ((emacs "26.1"))
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; This file contains the face definitions for Org-roam.
;;; Code:
(defgroup org-roam-faces nil
"Faces used by Org-roam."
:group 'org-roam
:group 'faces)
;;; Definitions
(defface org-roam-link
'((t :inherit org-link))
"Face for Org-roam links."
:group 'org-roam-faces)
(defface org-roam-link-current
'((t :inherit org-link))
"Face for Org-roam links pointing to the current buffer."
:group 'org-roam-faces)
(defface org-roam-link-invalid
'((t :inherit (error org-link)))
"Face for Org-roam links that are not valid.
This face is used for links without a destination."
:group 'org-roam-faces)
(defface org-roam-link-shielded
'((t :inherit (warning org-link)))
"Face for Org-roam links that are shielded.
This face is used on the region target by `org-roam-insertion'
during an `org-roam-capture'."
:group 'org-roam-faces)
;;; _
(provide 'org-roam-faces)
;;; org-roam-faces.el ends here

View file

@ -54,6 +54,8 @@
;; @TODO: implement something akin to `org-modules' that allows
;; selectively loading different sets of features.
;; ~NV [2020-05-22 Fri]
(require 'org-roam-faces)
(require 'org-roam-buffer)
(require 'org-roam-completion)
(require 'org-roam-capture)
@ -78,11 +80,6 @@
:link '(url-link :tag "Github" "https://github.com/org-roam/org-roam")
:link '(url-link :tag "Online Manual" "https://www.orgroam.com/manual/"))
(defgroup org-roam-faces nil
"Faces used by Org-roam."
:group 'org-roam
:group 'faces)
(defcustom org-roam-directory (expand-file-name "~/org-roam/")
"Default path to Org-roam files.
All Org files, at any level of nesting, are considered part of the Org-roam."
@ -942,32 +939,7 @@ Return nil if the file does not exist."
file)
org-roam-directory))))
;;; The org-roam buffer
;;;; org-roam-link-face
(defface org-roam-link
'((t :inherit org-link))
"Face for Org-roam links."
:group 'org-roam-faces)
(defface org-roam-link-current
'((t :inherit org-link))
"Face for Org-roam links pointing to the current buffer."
:group 'org-roam-faces)
(defface org-roam-link-invalid
'((t :inherit (error org-link)))
"Face for Org-roam links that are not valid.
This face is used for links without a destination."
:group 'org-roam-faces)
(defface org-roam-link-shielded
'((t :inherit (warning org-link)))
"Face for Org-roam links that are shielded.
This face is used on the region target by `org-roam-insertion'
during an `org-roam-capture'."
:group 'org-roam-faces)
;;;; org-roam-backlinks-mode
;;; org-roam-backlinks-mode
(define-minor-mode org-roam-backlinks-mode
"Minor mode for the `org-roam-buffer'.
\\{org-roam-backlinks-mode-map}"
@ -1006,39 +978,6 @@ buffer or a marker."
(backlink-dest (org-roam--retrieve-link-destination)))
(string= current backlink-dest)))
(defun org-roam--roam-file-link-face (path)
"Conditional face for org file links.
Applies `org-roam-link-current' if PATH corresponds to the
currently opened Org-roam file in the backlink buffer, or
`org-roam-link-face' if PATH corresponds to any other Org-roam
file."
(cond ((and (not (file-remote-p path)) ;; Prevent lockups opening Tramp links
(not (file-exists-p path)))
'org-roam-link-invalid)
((and (org-roam--in-buffer-p)
(org-roam--backlink-to-current-p))
'org-roam-link-current)
((org-roam--org-roam-file-p path)
'org-roam-link)
(t
'org-link)))
(defun org-roam--roam-id-link-face (id)
"Conditional face for org ID links.
Applies `org-roam-link-current' if ID corresponds to the
currently opened Org-roam file in the backlink buffer, or
`org-roam-link-face' if ID corresponds to any other Org-roam
file."
(cond ((not (org-roam-id-find id))
'org-roam-link-invalid)
((and (org-roam--in-buffer-p)
(org-roam--backlink-to-current-p))
'org-roam-link-current)
((org-roam-id-find id t)
'org-roam-link)
(t
'org-link)))
(defun org-roam-open-at-point ()
"Open an Org-roam link or visit the text previewed at point.
When point is on an Org-roam link, open the link in the Org-roam window.
@ -1156,15 +1095,50 @@ This function hooks into `org-open-at-point' via
(t
nil)))))
;;; The global minor org-roam-mode
;;; Org-roam-mode
;;;; Function Faces
;; These faces are used by `org-link-set-parameters', which take one argument,
;; which is the path.
(defun org-roam--file-link-face (path)
"Conditional face for file: links.
Applies `org-roam-link-current' if PATH corresponds to the
currently opened Org-roam file in the backlink buffer, or
`org-roam-link-face' if PATH corresponds to any other Org-roam
file."
(cond ((and (not (file-remote-p path)) ;; Prevent lockups opening Tramp links
(not (file-exists-p path)))
'org-roam-link-invalid)
((and (org-roam--in-buffer-p)
(org-roam--backlink-to-current-p))
'org-roam-link-current)
((org-roam--org-roam-file-p path)
'org-roam-link)
(t
'org-link)))
(defun org-roam--id-link-face (id)
"Conditional face for id links.
Applies `org-roam-link-current' if ID corresponds to the
currently opened Org-roam file in the backlink buffer, or
`org-roam-link-face' if ID corresponds to any other Org-roam
file."
(cond ((not (org-roam-id-find id))
'org-roam-link-invalid)
((and (org-roam--in-buffer-p)
(org-roam--backlink-to-current-p))
'org-roam-link-current)
((org-roam-id-find id t)
'org-roam-link)
(t
'org-link)))
;;;; Hooks and Advices
(defun org-roam--find-file-hook-function ()
"Called by `find-file-hook' when mode symbol `org-roam-mode' is on."
(when (org-roam--org-roam-file-p)
(setq org-roam-last-window (get-buffer-window))
(add-hook 'post-command-hook #'org-roam-buffer--update-maybe nil t)
(add-hook 'after-save-hook #'org-roam-db--update-file nil t)
(org-link-set-parameters "file" :face 'org-roam--roam-file-link-face :store #'org-roam-store-link-file)
(org-link-set-parameters "id" :face 'org-roam--roam-id-link-face)
(org-roam-buffer--update-maybe :redisplay t)))
(defun org-roam--delete-file-advice (file &optional _trash)
@ -1318,6 +1292,8 @@ M-x info for more information at Org-roam > Installation > Post-Installation Tas
(add-hook 'org-open-at-point-functions #'org-roam-open-id-at-point)
(advice-add 'rename-file :after #'org-roam--rename-file-advice)
(advice-add 'delete-file :before #'org-roam--delete-file-advice)
(org-link-set-parameters "file" :face 'org-roam--file-link-face :store #'org-roam-store-link-file)
(org-link-set-parameters "id" :face 'org-roam---id-link-face)
(org-roam-db-build-cache))
(t
(setq org-execute-file-search-functions (delete 'org-roam--execute-file-row-col org-execute-file-search-functions))
@ -1326,11 +1302,11 @@ M-x info for more information at Org-roam > Installation > Post-Installation Tas
(remove-hook 'org-open-at-point-functions #'org-roam-open-id-at-point)
(advice-remove 'rename-file #'org-roam--rename-file-advice)
(advice-remove 'delete-file #'org-roam--delete-file-advice)
(org-link-set-parameters "file" :face 'org-link)
(org-roam-db--close-all)
;; Disable local hooks for all org-roam buffers
(dolist (buf (org-roam--get-roam-buffers))
(with-current-buffer buf
(org-link-set-parameters "file" :face 'org-link)
(remove-hook 'post-command-hook #'org-roam-buffer--update-maybe t)
(remove-hook 'after-save-hook #'org-roam-db--update-file t))))))