Merge pull request #294 from pdubq/surfraw-refactor

surfraw: refactor and add autocomplete
This commit is contained in:
David 2024-11-29 11:05:18 -05:00 committed by GitHub
commit e12303a28d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 35 additions and 28 deletions

View file

@ -5,12 +5,14 @@
(dolist (elvi (surfraw-elvis-list))
(let ((key (first elvi))
(description (second elvi)))
(push `(defcommand ,(intern (concat "sr-" key)) (search)
(push `(defcommand ,(format-symbol t "~@:(sr-~a~)" key) (search)
((:string ,(concat description ": ")))
,description
(surfraw ,key search))
commands)
(push `(defcommand ,(intern (concat "sr-sel-" key)) () ()
(push `(defcommand ,(format-symbol t "~@:(sr-sel-~a~)" key) () ()
(surfraw ,key (get-x-selection)))
commands)))
(cons 'progn (reverse commands))))
(auto-define-surfraw-commands-from-elvis-list)

View file

@ -1,5 +1,11 @@
;;;; package.lisp
(defpackage #:surfraw
(:use #:cl :stumpwm))
(:use #:cl #:stumpwm)
(:import-from #:uiop
#:run-program
#:file-exists-p
#:read-file-string)
(:import-from #:alexandria
#:if-let
#:format-symbol))

View file

@ -5,7 +5,7 @@
:description "Integrates surfraw with stumpwm."
:author "Ivy Foster"
:license "GPLv3"
:depends-on (#:stumpwm)
:depends-on (#:stumpwm #:alexandria #:uiop)
:components ((:file "package")
(:file "macros" :depends-on ("package"))
(:file "surfraw" :depends-on ("macros"))))
(:file "surfraw")
(:file "auto-commands")))

View file

@ -14,24 +14,27 @@
;;; Code:
(defun split-by-- (str)
(let ((pos (position #\- str :start (1+ (position #\- str)))))
(list (subseq str 0 (1- pos))
(subseq str (1+ pos)))))
(defun surfraw-elvis-list ()
(mapcar (lambda (x)
(mapcar (lambda (x) (string-trim '(#\Space #\Tab #\Newline) x))
(split-by-- x)))
(remove-if-not #'(lambda (string) (search "--" string))
(split-string (run-shell-command "surfraw -elvi" :collect-output-p)
'(#\Newline)))))
(macrolet ((trim (str) `(string-trim '(#\Space #\Tab) ,str)))
(loop :for line :in (run-program '("surfraw" "-elvi") :output :lines)
:for pos = (search "--" line)
:when pos
:collect (list (trim (subseq line 0 pos))
(trim (subseq line (+ pos 2)))))))
(auto-define-surfraw-commands-from-elvis-list)
;;; Regular surfraw commands
(define-stumpwm-type :surfraw-elvi (input prompt)
(let ((elvis (mapcar 'car (surfraw-elvis-list))))
(or (find (or (argument-pop input)
(completing-read (current-screen) prompt elvis :require-match t)
(throw 'error "Abort"))
elvis :test 'string=)
(throw 'error "Such elvi doesn't exist"))))
(defcommand surfraw (engine search)
((:string "What engine? ") (:string "Search for what? "))
((:surfraw-elvi "What engine? ")
(:string "Search for what? "))
"Use SURFRAW to surf the net; reclaim heathen lands."
(check-type engine string)
(check-type search string)
@ -39,19 +42,15 @@
;;; Bookmarks
(defun display-file (file)
"Display a file in the message area."
(if (probe-file file)
(run-shell-command (concat "cat " file) t)
(message "The file ~a does not exist." file)))
(defvar *surfraw-bookmark-file* nil
"The surfraw bookmark file")
(defcommand sr-bookmark (bmk) ((:string "Bookmark: "))
(surfraw "" bmk))
(run-shell-command (concat "exec surfraw -g " bmk)))
(defcommand sr-bookmark-file-display () ()
(display-file *surfraw-bookmark-file*))
(if-let ((path (file-exists-p *surfraw-bookmark-file*)))
(read-file-string path)
(message "The file ~a does not exist." file)))
;;; surfraw.lisp ends here