From f7b213caea7ccecc124c72620f10e5e3cbff4552 Mon Sep 17 00:00:00 2001 From: Tor Erlend Fjelde Date: Mon, 23 Oct 2023 22:08:27 +0100 Subject: [PATCH] feat: add support for full name rendering in completion Add new display transform function for full names. Generalize existing `citar--shorten-names` to `citar--render-names` to allow code-sharing between completion rendering of family names and full names. Refs: #805 --- citar.el | 52 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/citar.el b/citar.el index 7dbe1fa5..cb8e12a4 100644 --- a/citar.el +++ b/citar.el @@ -191,6 +191,7 @@ references as a string." (defcustom citar-display-transform-functions ;; TODO change this name, as it might be confusing? `((sn . (citar--shorten-names)) + (fn . (citar--full-names)) (etal . (citar--shorten-names 3 "&"))) "Configure transformation of field display values from raw values. @@ -1416,20 +1417,14 @@ See the documentation for `citar-add-file-sources' for more details." ;;; Format and display field values -(defun citar--shorten-name-position (namelist name) +(defun citar--render-name-position (namelist name) "Return NAME position in a NAMELIST." (+ (seq-position namelist name) 1)) -(defun citar--shorten-name (name) - "Return family NAME in `family, given' string. +(defun citar--render-names (namestr name-func &optional truncate andstr) + "Return a list of names from a list of full NAMESTR. -Otherwise, return as is." - (car (split-string name ", "))) - -(defun citar--shorten-names (namestr &optional truncate andstr) - "Return a list of family names from a list of full NAMESTR. -To better accommodate corporate names, this will only shorten -personal names of the form \"family, given\". +Each name will be processed by NAME-FUNC. With an integer TRUNCATE, will shorten the list, and ANDSTR will replace last comma." @@ -1439,8 +1434,8 @@ replace last comma." (tnamelength (length tnamelist))) (mapconcat (lambda (n) - (let* ((shortname (citar--shorten-name n)) - (pos (citar--shorten-name-position tnamelist n)) + (let* ((name (funcall name-func n)) + (pos (citar--render-name-position tnamelist n)) (suffix (cond ;; if last name in the list and we're truncating add et al.; otherwise, no suffix @@ -1451,9 +1446,40 @@ replace last comma." (concat " " andstr " ")) ;; otherwise, use a comma (t ", ")))) - (concat shortname suffix))) + (concat name suffix))) tnamelist ""))) +(defun citar--shorten-name (name) + "Return family NAME in `family, given' string. + +Otherwise, return as is." + (car (split-string name ", "))) + +(defun citar--shorten-names (namestr &optional truncate andstr) + "Return a list of family names from a list of full NAMESTR. +To better accommodate corporate names, this will only shorten +personal names of the form \"family, given\". + +With an integer TRUNCATE, will shorten the list, and ANDSTR will +replace last comma." + (citar--render-names namestr #'citar--shorten-name truncate andstr)) + +(defun citar--full-name (name) + "Return `given family' in `family, given' string NAME. + +Otherwise, return as is." + (let ((split-names (split-string name ", "))) + (if (> (length split-names) 1) + (concat (cadr split-names) " " (car split-names)) + name))) + +(defun citar--full-names (namestr &optional truncate andstr) + "Return a list of full names from a list of full NAMESTR. + +With an integer TRUNCATE, will shorten the list, and ANDSTR will +replace last comma." + (citar--render-names namestr #'citar--full-name truncate andstr)) + (defun citar--fields-for-format (template) "Return list of fields for TEMPLATE." (mapcan (lambda (fieldspec) (when (consp fieldspec) (cdr fieldspec)))