rougier / nano-modeline

GNU Emacs / N Λ N O Modeline
GNU General Public License v3.0
170 stars 29 forks source link

How to make buttons? #61

Closed chookity-pokk closed 1 year ago

chookity-pokk commented 1 year ago

I see that it's possible to make buttons from both your reddit post and your commit history. How does one actually achieve this though?

reynard93 commented 1 year ago

curious on this too, would be useful to have a sample config!

rougier commented 1 year ago

Here is one example from the source:

(defun nano-modeline-org-capture-mode ()
  "Nano line for org capture mode"

  (let ((buttons '(("CAPTURE" . (org-capture-finalize))
                   ("CANCEL" . (org-capture-kill)))))  
    (funcall nano-modeline-position
             `((nano-modeline-buffer-status "ORG") " "
               (nano-modeline-buffer-name "Capture") " "
               (nano-modeline-org-capture-description))
             `((nano-modeline-buttons ,buttons t) " "
               (nano-modeline-window-dedicated)))))
rougier commented 1 year ago

And another one:

(defun nano-modeline-mu4e-message-mode ()
  "Nano line for mu4e message mode with several buttons for most
common action"

  (let ((buttons '(("archive:bootstrap" . (mu4e-view-mark-for-refile . "Archive message"))
                   ("trash:bootstrap" . (mu4e-view-mark-for-trash . "Delete message"))
                   ("file-richtext:bootstrap". (nano-modeline-mu4e-view-in-xwidget . "View message as HTML"))
                   ("folder:bootstrap". (mu4e-headers-mark-for-move . "Move message"))
                   ("tag:bootstrap". (mu4e-headers-mark-for-tag . "Tag message"))
                   ("reply:bootstrap". (mu4e-compose-reply . "Reply to message"))
                   ("forward:bootstrap". (mu4e-compose-forward . "Forward message")))))
    (funcall nano-modeline-position
             `((nano-modeline-buffer-status "FROM") " "
               (nano-modeline-buffer-name ,(nano-modeline-mu4e-message-from)))
             `((nano-modeline-buttons ,buttons t) " "
               (nano-modeline-window-dedicated)))))
chookity-pokk commented 1 year ago

Thank you for the response! Any chance you can share the config used in your reddit post announcing the 1.0 version?

rougier commented 1 year ago

Sure. Mostly experimental. Just evaluate the buffer and you should get the buttons.

(require 'nano-theme)
(require 'nano-command)
(require 'nano-modeline)

(defun nano-org-insert-code-block (&optional default)
  "Prompt for language using nano-command and insert a code block with
givn language. Initial prompt is set to DEFAULT."

  (interactive)
  (let ((language (nano-command "CODE-BLOCK" "Enter language"
                                (lambda () (insert (or default "emacs-lisp"))))))
    (when language
      (insert (format "\n#+begin_src %s \n\n#+end_src\n" language))
  (forward-line -2)
  (goto-char (line-end-position)))))

(defun nano-org-insert-table (&optional default)
  "Prompt for size using nano-command and insert a table with
given size. Initial prompt is set to DEFAULT."

  (interactive)
  (let ((size (nano-command "TABLE" "Table size as cols x rows"
                            (lambda () (insert (or default "3x4"))))))
    (when size
      (org-table-create size))))

(defun nano-org-insert-header (level)
  "Insert a header of given LEVEL. Title is prompted using nano-command"

  (interactive)
  (let* ((levels '(("H1" . ("*" . "New section"))
                   ("H2" . ("**" . "New subsection"))
                   ("H3" . ("***" . "New subsubsection"))))
         (title (nano-command level "Enter title"
                              (lambda () (insert (cddr (assoc level levels)))))))
    (when title
      (beginning-of-line)
      (insert (format "%s %s\n" (cadr (assoc level levels)) title)))))

(defun nano-org-insert-h1 ()
  "Insert a section. Title is prompted using nano-command"
  (interactive)
  (nano-org-insert-header "H1"))

(defun nano-org-insert-h2 ()
  "Insert a subsection. Title is prompted using nano-command"

  (interactive)
  (nano-org-insert-header "H2"))

(defun nano-org-insert-h3 ()
  "Insert a subsubsection. Title is prompted using nano-command"

  (interactive)
  (nano-org-insert-header "H3"))

(defun nano-org-insert-link ()
  (interactive)
  (let ((url (nano-command "LINK" "Enter URL")))
    (when url)))

(defun nano-org-toggle-markup (&optional marker)
  (interactive)
  (save-excursion
    (save-match-data
      (let* ((emphasized (org-in-regexp org-emph-re 2))
             (beg (cond (emphasized (match-beginning 3))
                        ((region-active-p) (region-beginning))
                        (t (car (bounds-of-thing-at-point 'word)))))
             (end (cond (emphasized (match-end 4))
                        ((region-active-p) (region-end))
                        (t (cdr (bounds-of-thing-at-point 'word))))))
        (when (and beg end)
          (let ((beg-marker (buffer-substring beg (1+ beg)))
                (end-marker (buffer-substring end (1+ end))))
            (cond ((string= beg-marker marker)
                   (goto-char end) (delete-char 1)
                   (goto-char beg) (delete-char 1))
                  ((string-search beg-marker "*/_+~=")
                   (goto-char end) (delete-char 1) (insert marker)
                   (goto-char beg) (delete-char 1) (insert marker))
                  (t
                   (goto-char end) (insert marker)
                   (goto-char beg) (insert marker)))))))))

(defun nano-org-toggle-bold ()
  (interactive)
  (nano-org-toggle-markup "*"))

(defun nano-org-toggle-italic ()
  (interactive)
  (nano-org-toggle-markup "/"))

(let ((header-buttons
       '(("type-h1:bootstrap" . (nano-org-insert-h1 . "Create section"))
         ("type-h2:bootstrap" . (nano-org-insert-h2 . "Insert subsection"))
         ("type-h3:bootstrap" . (nano-org-insert-h3 . "Insert paragraph"))))
      (format-buttons
         '(("type-bold:bootstrap" . (nano-org-toggle-bold . "Toggle bold"))
           ("type-italic:bootstrap" . (nano-org-toggle-italic . "Toggle italic"))
           ("link:bootstrap" . (nano-org-insert-link . "Insert link"))
           ))
      (block-buttons
       '(("image:bootstrap". (nil . "Insert image"))
         ("table:bootstrap". (nano-org-insert-table . "Insert table"))
         ("quote:bootstrap". (nil . "Insert citation"))
         ("code:bootstrap". (nano-org-insert-code-block . "Insert code"))))
      (export-buttons
       '(("upload:bootstrap". (nil . "Save as PDF or HTML")))))
    (nano-modeline-header
     `((nano-modeline-buffer-status "ORG") " "
       (nano-modeline-buffer-name))
     `((nano-modeline-buttons ,header-buttons t 1)
       "  " (nano-modeline-buttons ,format-buttons t 2)
       "  " (nano-modeline-buttons ,block-buttons t 3)
       "  " (nano-modeline-buttons ,export-buttons t 4)
       " "
       (nano-modeline-window-dedicated))))
chookity-pokk commented 1 year ago

Thank you! I'll mark this as closed now!