EFLS / zetteldeft

A Zettelkasten system! Or rather, some functions on top of the emacs deft package.
https://efls.github.io/zetteldeft
GNU General Public License v3.0
394 stars 42 forks source link

Highlighting #tags and improving the tag regex #55

Closed stampf closed 4 years ago

stampf commented 4 years ago

Hello, Thanks for the incredible package. I'm re-learning emacs and the day to day evolution of the Ztteldeft package is nice to follow-up.

In my setup, I tried to add a sexp to highlight #tags, but for some reason, it doesn't work. Here's my use-package section (last lines are releveant). I'm still a noob in emacs-lisp...

;; Zetteldeft https://www.eliasstorms.net/zetteldeft/
(use-package zetteldeft
  :ensure t
  :after (deft avy)
  :config
  (zetteldeft-set-classic-keybindings)
  ;;  (advice-add 'zetteldeft-find-file-id-insert :override #'my-private-function/zetteldeft-find-file-id-insert)
  ;;  (advice-add 'zetteldeft-find-file-full-title-insert :override #'my-private-function/zetteldeft-find-file-full-title-insert)
  (setq zetteldeft-link-indicator "[["
    zetteldeft-link-suffix "]]")
  ;; To highlight links you need to set up font-lock keywords for markdown-mode.
  (font-lock-add-keywords 'markdown-mode
              `((,zetteldeft-id-regex
                 . font-lock-warning-face))
              `((,zetteldeft-tag-regex
                 . font-lock-warning-face))
              )
)
EFLS commented 4 years ago

Great to hear!

The error is with your font-lock setup. You can check how you need to use the function by hitting C-h f font-lock-add-keywords and reading the documentation.

The regular expressions and faces you pass should be a single list, with pairs as its elements (okay, maybe it requires a bit more knowledge than C-h f provides directly).

In any case, this means:

(font-lock-add-keywords 'markdown-mode
     `((,zetteldeft-id-regex . font-lock-warning-face)
       (,zetteldeft-tag-regex . font-lock-warning-face)))
stampf commented 4 years ago

Thanks for the elisp correction. It works better now although usually not on the first document I open (I bound F8 to (deft) in order to launch it, and then open a file from there).

I also suggest you change the default regex because it doesn't work with non-ascii ([a-z]) character. I'm french and a tag like #qualité gets truncated as #qualit (when I do a "C-c d T" to list all tags, I can see some of them being truncated). My suggestion (I have in my .emacs):

(setq zetteldeft-tag-regex "[#@][[:alnum:]]+")

I still have some truncated tags though. When, on the list, I do a "C-c d S" to search, I can't find the corresponding tags in the files shown. This is strange and I don't understand why.

I can see the following in your code and I'm not sure of the use of prefixing the regex with newline and space (if I understand it well). I don't know if this is a valid initial source of bug hunting:

(defconst zetteldeft--tag-format (concat "\\(^\\|\s\\)" zetteldeft-tag-regex))
EFLS commented 4 years ago

Good suggestion on the default tag regex. I'll change the default to "[#@][[:alnum:]_-]+".

Also, I now notice that using defconst to adjust the regular expression for extracting tags means that later changes to the tag regex aren't picked up. I'll change this into a function.

To generate a list of tags, the zetteldeft--tag-format is used to make sure that #'s in URLs aren't caught in its net. So it makes sure that only tags are found with either a space in front of them or that are at the start of a new line. I don't think it has an influence on the issue you are experiencing, as it is only used to generate the tag buffer itself.

Can you provide a more concrete example of what you are having trouble with? And what do you mean with "when on the list"? EDIT: Ah, the list of tags in the tag buffer of course. When you use C-c d s on such a tag, does it correctly copy the tag to the Deft search buffer?

There have been some issues with Deft and non-ASCII characters, as far as I understand this issue: https://github.com/jrblevin/deft/issues/59

stampf commented 4 years ago

I just tested again, and everything's fine. Although I'm a long time basic user of emacs (25 years up and down... !), I've just only recently jumped into packages and emacs-lisp. So I think it might have been a temporary issue (like update without restarting emacs, etc.)

Anyway, all's fine, thanks!