Search code examples
elisp

Emacs generate values based on system-type in an early return schema


I try to generate values from the system-type like in an common Pascal function with a early return schema:

{ ASSUMING system_type is a global VAR }
FUNCTION font_name: String
BEGIN
  result :='Courier';
  IF system_type = 'darwin'    THEN Exit('Menlo');
  IF system_type = 'gnu/linux' THEN Exit('Monospace');
END;

But cannot find the right expression:

The snippet


(defmacro font-name ()
  (declare (indent defun))
  `(when (eq system-type 'darwin)    "Menlo")
  `(when (eq system-type 'gnu/linux) "Monospace")
   "Courier")

gives me always "Courier" despite having system-type 'darwin.

This one works but with nested if else...

(defun i4w-editor-font ()
   (if (eq system-type 'darwin)
       "Menlo"
       (if (eq system-type 'gnu/linux)
          "Monospace"
          "Courier")))

Is there a possibility to write an 'early-return' schema.


Solution

  • The first thing is: don't define a macro if there's no need to, and there's no need for that to be a macro. Macros are a whole separate topic, but: As with the return value of a function, a macro's expansion is the evaluation of the final form of its definition, and hence any call to your macro expands to "Courier".

    This one works but with nested if else...

    There are other conditional constructs:

    (defun i4w-editor-font ()
      "Return font according to `system-type'."
      (cl-case system-type
        ('darwin "Menlo")
        ('gnu/linux "Monospace")
        (t "Courier")))