Skip to content

Commit

Permalink
feat: add support for full name rendering in completion
Browse files Browse the repository at this point in the history
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: emacs-citar#805
  • Loading branch information
torfjelde committed Oct 23, 2023
1 parent ac91ec3 commit f7b213c
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions citar.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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."
Expand All @@ -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
Expand All @@ -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)))
Expand Down

0 comments on commit f7b213c

Please sign in to comment.