How to use usocket to create a connection? (common lisp) - sockets

I'm trying to use socket in lisp to create a connection. Using sbcl, i found "usocket" available. But I failed to sending strings between the server and client. Here's the code:
Server:
(ql:quickload "usocket")
(use-package 'usocket)
(defun default-tcp-handler (stream) ; null
(declare (type stream stream))
(terpri stream))
(defparameter *us* (socket-server "127.0.0.1" 4547 #'default-tcp-handler))
(defparameter *s* (socket-accept *us*))
(defparameter *ss* (usocket:socket-stream *s*))
Client:
(ql:quickload "usocket")
(use-package 'usocket)
(defparameter *us* (socket-connect "127.0.0.1" 4547))
(defparameter *st* (usocket:socket-stream *us*))
I ran the server code first, it freezes. Then I ran the client code. But the server seemed to be no response.
I tried to format a string into the st stream:
(format *st* "hello, server")
But it returns nil.
How can i solve the problem??
Thanks a lot.

I tried your code and there were no errors, so everything seemed to be all right with the connection. However, if you only write to streams without reading (not to mention flushing the output), the connection has no way to manifest itself. Here is a modified version where the server reads a single line from the socket:
;;; server
(ql:quickload "usocket")
(use-package :usocket)
(defun default-tcp-handler (stream)
(declare (type stream stream))
(format t "~A~%" (read-line stream))
(finish-output))
(defparameter *us* (socket-server "127.0.0.1" 4547 #'default-tcp-handler))
;;; client
(ql:quickload "usocket")
(use-package :usocket)
(defparameter *us* (socket-connect "127.0.0.1" 4547))
(defparameter *st* (socket-stream *us*))
(write-line "hello server" *st*)
(finish-output *st*)
In your case format returned nil, because it always returns nil except format nil. The server hangs because it starts listening. If you don't want that, you'll have to work with multiple threads.

Related

ox-hugo github actions : Debugger entered--Lisp error: (void-function org-hugo-export-wim-to-md) Error: Process completed with exit code 255

Im trying to deploy my hugo site on github actions. Im using the following github action which does the following :
1.On Ubuntu
2.Setup up emacs
3.git clone ox-hugo package
4.ox-hugo package should convert my .org files to .md
setup and build using Hugo and deploy
https://gist.github.com/shwetarkadam/d890b7054b65fe21b63609ca03650bdc
I'm facing an issue on step 4 where I encounter the following error on GitHub action :
Run emacs ./config.org --batch -L ./ox-hugo -L ox-hugo.el --eval="(org-hugo-export-wim-to-md t)" --kill
Debugger entered--Lisp error: (void-function org-hugo-export-wim-to-md)
(org-hugo-export-wim-to-md t)
eval((org-hugo-export-wim-to-md t) t)
command-line-1(("./config.org" "-L" "./ox-hugo" "-L" "ox-hugo.el" "--eval=(org-hugo-export-wim-to-md t)" "--kill"))
command-line()
normal-top-level()
Approaches tried till now :
Changing (org-hugo-export-wim-to-md t) to (org-hugo-export-wim-to-md :all-subtrees)
Adding the expression (org-hugo-export-wim-to-md :all-subtrees) in single quotes and double quotes.
I happened to be working on a similar problem today. I had searched for guidance on it and I stumbled across Batch export of org-mode files from the command line which led me to fniessen/orgmk and in orgmk.el in particular.
After experimenting in my *scratch* buffer and asking Emacs' various help facilities a few questions, I bludgeoned my way to a standalone file containing Emacs Lisp code that appears to work independently of my initialization files.
$ touch export.el
$ emacs -q --batch -l export.el
Designating package sites
Designating package site melpa-stable => https://stable.melpa.org/packages/
Designating package site melpa => https://melpa.org/packages/
Designating package site gnu => https://elpa.gnu.org/packages/
Installing package ox-hugo
Setting ‘package-selected-packages’ temporarily since "emacs -q" would overwrite customizations
‘ox-hugo’ is already installed
Exporting org subtrees to hugo from content.org
1 files scanned, 0 files contains IDs, and 0 IDs found.
[ox-hugo] 1/ Exporting ‘Redacted site title’ ..
[ox-hugo] 2/ Exporting ‘Posts’ ..
[ox-hugo] 3/ Exporting ‘Redacted post title’ ..
[ox-hugo] Exported 3 subtrees from content.org in 0.510s (0.170s avg)
Exporting all org subtrees in all files in /redacted/directory
$ find content -newer export.el
content
content/_index.md
content/posts
content/posts/redacted-post-title.md
content/posts/_index.md
(Some names have been changed to protect the guilty er um I mean innocent.)
The file export.el contains:
(defvar my/package-archives
(list
(cons "melpa-stable" "https://stable.melpa.org/packages/")
(cons "melpa" "https://melpa.org/packages/")
(cons "gnu" "https://elpa.gnu.org/packages/")))
(defvar my/packages-to-install '(ox-hugo))
(defun my/designate-package-site (site)
(message "Designating package site %s => %s" (car site) (cdr site))
(add-to-list 'package-archives site t))
(defun my/designate-package-sites ()
(message "Designating package sites")
(mapcar #'my/designate-package-site my/package-archives))
(defun my/install-package (pkg)
(message "Installing package %s" pkg)
(ignore-errors (package-install pkg)))
(when (locate-library "package")
(require 'package)
(my/designate-package-sites)
(package-initialize)
(unless package-archive-contents (package-refresh-contents))
(mapcar #'my/install-package my/packages-to-install))
(defun my/batch-ox-hugo-file (file)
(message "Exporting org subtrees to hugo from %s" file)
(let ((all-subtrees t)
(any-visibility nil))
(with-current-buffer (find-file-noselect file)
(org-hugo-export-wim-to-md all-subtrees any-visibility))))
(defun my/batch-ox-hugo-directory (directory)
(message "Exporting all org subtrees in all files in %s" directory
(let ((default-directory (expand-file-name directory)))
(mapcar #'my/batch-ox-hugo-file
(file-expand-wildcards "*.org")))))
(my/batch-ox-hugo-directory default-directory)
The execution environment where I developed this is Emacs 28.2 on FreeBSD 13.1.
I haven't tried it out with Github Actions yet, but that's my next step.

in Emacs, how to open file in external program without errors?

I use the following code to direct Emacs to open PDF files using an external application:
(require 'openwith)
'(openwith-associations (quote (("\\.skim\\'" "open" (file)) ("\\.pdf\\'" "open" (file)))))
(openwith-mode t)
When I visit a PDF file, it successfully opens the file in my external program, but it also gives me errors and a backtrace:
Debugger entered--Lisp error: (error "Opened Foundation - Isaac Asimov.pdf in external program")
signal(error ("Opened Foundation - Isaac Asimov.pdf in external program"))
error("Opened %s in external program" "Foundation - Isaac Asimov.pdf")
openwith-file-handler(insert-file-contents "/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf" t nil nil nil)
insert-file-contents("~/iBooks/Books/Foundation - Isaac Asimov.pdf" t)
byte-code("\302\303 \302\"\210)\302\207" [inhibit-read-only filename t insert-file-contents] 3)
find-file-noselect-1(#<killed buffer> "~/iBooks/Books/Foundation - Isaac Asimov.pdf" nil nil "~/Library/Containers/com.apple.BKAgentService/Data/Documents/iBooks/Books/Foundation - Isaac Asimov.pdf" (21490564 16777218))
find-file-noselect("/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf" nil nil nil)
find-file("/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf")
mapc(find-file ("/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf"))
helm-find-many-files("/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf")
apply(helm-find-many-files "/Users/jay/iBooks/Books/Foundation - Isaac Asimov.pdf")
How do I open files in external applications without throwing errors?
My recommendation would be to use start-process in conjunction with dired-mode to open files in external applications.
Xah Lee has written short, yet effective, function to handle opening files in the external default application set up on the OS: http://ergoemacs.org/emacs/emacs_dired_open_file_in_ext_apps.html
(defun xah-open-in-external-app (&optional file)
"Open the current file or dired marked files in external app.
The app is chosen from your OS's preference."
(interactive)
(let ( doIt
(myFileList
(cond
((string-equal major-mode "dired-mode") (dired-get-marked-files))
((not file) (list (buffer-file-name)))
(file (list file)))))
(setq doIt (if (<= (length myFileList) 5)
t
(y-or-n-p "Open more than 5 files? ") ) )
(when doIt
(cond
((string-equal system-type "windows-nt")
(mapc (lambda (fPath) (w32-shell-execute "open" (replace-regexp-in-string "/" "\\" fPath t t)) ) myFileList))
((string-equal system-type "darwin")
(mapc (lambda (fPath) (shell-command (format "open \"%s\"" fPath)) ) myFileList) )
((string-equal system-type "gnu/linux")
(mapc (lambda (fPath) (let ((process-connection-type nil)) (start-process "" nil "xdg-open" fPath)) ) myFileList) ) ) ) ) )
I use something similar (which can be viewed at the following Github link), but it is not as straight forward as the function written by Xah Lee: https://github.com/lawlist/dired-read-file-name/blob/master/dired-read-file-name.el
If the path to the file is in the buffer, I save it with M-w then invoke shell-command and call xdg-open C-y. This will yank the path to the file previously saved and opens it with the associated program. xdg-open takes care of finding the right program to open that file (screenshots).
In a dired buffer, browse-url-of-dired-file ("ask a WWW browser to display the file named on this line" - bound to W by default for me) opened a .csv file in Excel & a .md file in MacDown (both the OS default for those filetypes).
This is good enough for me, I might try to work out how to do it for the current buffer file some time.
(macOS Monterey 12.6.2, Emacs 28.2)

How to make swank working with stumpw?

I've added this code snippet to my stumpwmrc file:
(defun load-swank ()
"Load a swank server"
(ql:quickload 'swank)
(require 'swank)
(setq swank:*use-dedicated-output-stream* nil)
(setq slime-net-coding-system 'utf-8-unix)
(swank:create-server :port 4006))
(load-swank)
I am expecting to open a socket server, accepting the "swank" protocol. Thus I could connect to it with emacs (thanks to Slime).
But when I login and and stumpwm is reading its configuration file, here is the error message I get:
15:00:34 Outputting a message:
^B^1*Error loading ^b/home/ybaumes/.stumpwmrc^B: ^nThe name "SWANK" does not designate any package.
How can I fix that? I invoke 'require, or even 'quickload functions. What's the issue here?
A typical error is this:
You load the file and the reader sees the code:
SWANK is not loaded
(defun load-swank ()
"Load a swank server"
SWANK is not loaded
(ql:quickload 'swank)
SWANK is not loaded - remember, we are still reading the form.
(require 'swank)
SWANK is not loaded - remember, we are still reading the form.
Now use us a symbol in package which is not existing... the reader complains:
(setq swank:*use-dedicated-output-stream* nil) ; the package SWANK does not exist yet.
(setq slime-net-coding-system 'utf-8-unix)
(swank:create-server :port 4006))
Now you want to load SWANK:
(load-swank)
You can't use a symbol from a package which does not exist.
For example what works is this inside the function:
(setf (symbol-value (read-from-string "swank:*use-dedicated-output-stream*")) nil)
and so on.
You need to find the symbol at runtime of that function. Use (find-symbol "FOO" "SWANK") (remember Common Lisp is upcase internally) or (read-from-string "SWANK::FOO").

Connect emacs to a remote ensime server

How can I connect to an ensime server on a remote host? My netbook is a bit slow for that kind of stuff. I copied the data over and use tramp to edit the files remotely. I ran bin/server to create the server and an ssh forwarding to be able to connect to it. I use ensime-connect to connect to the port on localhost. The ensime server on the remote server answers with Got connection, creating handler..., but that's about it. Ensime is in [ENSIME: wtf] mode in the emacs status line. How do I fix this?
The problem is the ensime can not find "config" of the connection (made through ensime-connect).
and then following line will throws error:
(if (and loose (ensime-file-in-directory-p file project-root))
because project-root is nil.
By setting the connection to ensime-buffer-connection, the problem can be fixed.
try adding following function to your ensime.el
and using the ensime-stackoverflow-connect to connect.
(defun ensime-stackoverflow-connect (host port)
(interactive (list
(read-from-minibuffer "Host: " ensime-default-server-host)
(read-from-minibuffer "Port: " (format "%d" ensime-default-port)
nil t)))
(let ((c (ensime-connect host port))
(config (ensime-config-load "/Users/whunmr/lab/scala/.ensime")))
(ensime-set-config c config)
(setq ensime-buffer-connection c))
)
remember to change the config path in the code: "/Users/whunmr/lab/scala/.ensime"
EDIT1:
the ".ensime" file was created by M-x ensime command, in your scala project folder.
actually, by just hardcode the config, you can ignore the file.
(defun ensime-my-connection (host port)
(interactive (list
(read-from-minibuffer "Host: " ensime-default-server-host)
(read-from-minibuffer "Port: " (format "%d" ensime-default-port)
nil t)))
(let ((c (ensime-connect host port))
(config '(:project-name "test" :project-package "com.whunmr" :sources ("./src") :compile-jars ("./" "../../apps/scala/lib/") :target "./bin" :root-dir "/Users/twer/lab/scala/")))
(ensime-set-config c config)
(setq ensime-buffer-connection c))
)

ELPA/Marmalade reports "cannot open load file" for ~/.emacs.d/elpa/archives/-pkg

I have installed Marmalade and downloaded some interesting packages. But now, when I start Emacs I get this error:
Cannot open load file: c:/Documents and Settings/Carlos/Datos de programa/.emacs.d/elpa/archives/-pkg
If I use --debug-init, I get:
Debugger entered--Lisp error: (file-error "Cannot open load file" "c:/Documents and Settings/Carlos/Datos de programa/.emacs.d/elpa/archives/-pkg")
load("c:/Documents and Settings/Carlos/Datos de programa/.emacs.d/elpa/archives/-pkg" nil t)
(if (file-directory-p pkg-dir) (load (concat pkg-dir ... "-pkg") nil t))
(let ((pkg-dir ...)) (if (file-directory-p pkg-dir) (load ... nil t)))
package-load-descriptor("c:/Documents and Settings/Carlos/Datos de programa/.emacs.d/elpa/" "archives")
(lambda (name) (package-load-descriptor dir name))("archives")
mapc((lambda (name) (package-load-descriptor dir name)) ("anything-1.287" "anything- complete-1.86" "anything-config-0.4.1" "archive-contents" "archive-contents~" "archives" "auto-indent-mode-0.35" "builtin-packages" "builtin-packages~" "clojure-mode-1.7.1" "clojurescript-mode-0.5" "coffee-mode-0.3.0" "color-file-completion-1.0.1" "color-theme-6.6.1" "color-theme-eclipse-0.0.2" "color-theme-github-0.0.3" "color-theme-railscasts-0.0.2" "color-theme-twilight-0.1" "css-mode-1.0" "drag-stuff-0.0.3" "evernote-mode-0.41" "find-file-in-project-2.0" "flymake-coffee-0.4" "flymake-haml-0.5" "flymake-ruby-0.4" "flymake-shell-0.5" "haml-mode-3.0.14" "highlight-parentheses-1.0.1" "html-script-src-0.0.2" "inf-ruby-2.1" "js-comint-0.0.1" "js2-mode-20090814" "lua-mode-20100617" "package.el" "package.el~" "project-local-variables-0.2" "ruby-compilation-0.7" "ruby-electric-1.1" "ruby-mode-1.1" "slime-20100404" "tabbar-2.0.1" "tabbar-ruler-0.2" "yasnippet-0.6.1" "yasnippet-bundle-0.6.1" "zenburn-1.8"))
(if (file-directory-p dir) (mapc (lambda ... ...) (directory-files dir nil "^[^.]")))
(lambda (dir) (if (file-directory-p dir) (mapc ... ...)))("c:/Documents and Settings/Carlos/Datos de programa/.emacs.d/elpa/")
mapc((lambda (dir) (if (file-directory-p dir) (mapc ... ...))) ("c:/Documents and Settings/Carlos/Datos de programa/.emacs.d/elpa/" "/usr/share/emacs/site-lisp/elpa/"))
package-load-all-descriptors()
package-initialize()
(progn (package-initialize))
(if (load (expand-file-name "~/.emacs.d/elpa/package.el")) (progn (package-initialize)))
(when (load (expand-file-name "~/.emacs.d/elpa/package.el")) (package-initialize))
eval-buffer(#<buffer *load*> nil "c:/Documents and Settings/Carlos/Datos de programa/.emacs" nil t) ; Reading at buffer position 9019
load-with-code-conversion("c:/Documents and Settings/Carlos/Datos de programa/.emacs" "c:/Documents and Settings/Carlos/Datos de programa/.emacs" t t)
load("~/.emacs" t t)
#[nil "\205\264
This file doesn't exist. I've tried searching for this problem with Google but I didn't find anything.
Easy. For whatever reason, the package.el you're now using is the "classic" version from http://tromey.com/elpa/package.el. It's wildly incompatible with recent versions of package.el. How you managed to install Marmalade packages using that version, I don't know; you must have had a different version of package.el when you installed those packages, perhaps because you were using a newer Emacs at the time.
The fix is to use the version bundled with your Emacs (if it's new enough to have one), or to replace your ~/.emacs.d/elpa/package.el with this version instead, as described in the Marmalade instructions.
I've stumbled upon this question while trying to solve a similar problem. In my case, forcing package-initialize have solved the problem:
;; init.el
(require 'package)
(package-initialize)
Even though I did not read the source code for package.el, I believe some kind of laziness in package.el is cousing the problem. Athough this is and old question, I wanted to answer, in case someone else need some help.
For more information on Emacs start-up, see http://www.gnu.org/software/emacs/manual/html_node/elisp/Startup-Summary.html.
Adding a file, not directory, using touch ~/.emacs.d/elpa/archives/-pkg solved it for me.
I think it may just go away if you manually create a directory at:
c:/Documents and Settings/Carlos/Datos de programa/.emacs.d/elpa/archives/-pkg
In my case I had to add this directive to tell emacs where it can find the files required:
First this line
(add-to-list 'load-path "~/.emacs.d/")
And the required package ...
(require 'package)
(add-to-list 'package-archives
'("marmalade" . "http://marmalade-repo.org/packages/") t)
(package-initialize)
(when (not package-archive-contents)
(package-refresh-contents))