0

This function starts a new named shell buffer. How can I handle the case where bufrnm set by interactive in empty?

(defun galaxy-shell (&optional bufrnm)
  "TODO"

  (interactive "s Buffer_name: ")

  (if bufrnm
      (let ( (bufrnam (generate-new-buffer-name bufrnm)) )
         (shell bufrnam))
    (let ( (bufrnam (generate-new-buffer-name "foo")) )
       (shell bufrnam)))
0

3 Answers 3

1

EDIT in response to your 'extended/edited' question

I think you should think about how to simplify this yourself. But let me show you two options, then you can come up with, arguably, the most straightforward simplification, using if i.c.w. or

(shell
 (generate-new-buffer-name
  (pcase buffer-name
    ((or "" (pred null)) "galaxy")
    (name name))))

or

(when (string-blank-p buffer-name)
  (setq buffer-name nil))
(shell
 (generate-new-buffer-name
  (or buffer-name "galaxy")))

You can clean it up a little more by making the buffer-name argument mandatory, so that you do not have to check for the argument being nil.

END EDIT

I would suggest that you experiment a little using the scratch buffer

(defun galaxy-shell (&optional buffer-name)
  (interactive "sEnter buffer name: ")
  (print buffer-name))

Try calling it interactively and non-interactively (as making the argument optional suggests that you would like to support non-interactive calls also).

Now that you have checked the 'empty' value(s) of buffer-name, have a look at string-empty-p (and null).

1
  • Many thanks. Have also found string-blank-p. When using interactive with Code Character s, the user can pass an empty string or string composed of only whitespace.
    – Dilna
    Jul 24, 2022 at 12:35
0

Have come up with this solution. Can it be simplified?

(defun galaxy-shell (&optional bufrnm)
  "Make a console terminal subshell that works through an emacs buffer.
BUFRNM  Name used to set the title of the buffer."

  (interactive "s Buffer_name: ")

  (if bufrnm

      (if (string-blank-p bufrnm)
          (shell (generate-new-buffer-name "galaxy"))
        (shell (generate-new-buffer-name bufrnm)))

    (shell (generate-new-buffer-name "galaxy"))))
1
  • I am not sure how you could best extend your question, but I guess you could better just edit your original question. Anyway, I will answer by extending (editing) my original answer. But let me mention already that you are calling the same functions in all branches of your conditional expression(s) (only using different arguments). Jul 24, 2022 at 13:56
0

The real answer is that you can do algebra on lisp expressions. In algebra we learn that multiplication is distributive, meaning that ax+ay is equivalent to a(x+y). The distributive property lets us factor out the common expression, rewriting the expression so that the a is no longer repeated. We can do the same in Lisp. Starting with this code:

(if (string-blank-p bufrnm)
    (shell (generate-new-buffer-name "galaxy"))
  (shell (generate-new-buffer-name bufrnm)))

We can factor out the common call to shell, like this:

(shell
  (if (string-blank-p bufrnm)
      (generate-new-buffer-name "galaxy")
    (generate-new-buffer-name bufrnm)))

We can further factor out the call to generate-new-buffer-name:

(shell
  (generate-new-buffer-name
    (if (string-blank-p bufrnm)
        "galaxy"
      bufrnm)))

Not every programming language gives us this power. Those that do not are inferior and aren’t worth discussing here. Those that do are generally called functional programming languages.

2
  • Looks like you describe an example of the ? : ternary operator we find in non-fucntional languages ;-)
    – rsp
    Jul 25, 2022 at 16:40
  • Yes, that is an example of how many non–functional languages fail to be algebraic. By making too strong a distinction between expressions (such as a ternary) and statements (like an if statement), they require you to convert from one to the other in an ad–hoc way as you do the algebra. Languages like Lisp do not suffer from this defect.
    – db48x
    Jul 25, 2022 at 17:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.