.emacs code to identify OS? - macos

I'm using emacs on both Mac OS X and Ubuntu. My .emacs is mostly the same for both platforms, with a couple of lines concerning local fonts and other OS-related stuff. As I usually do additions to my .emacs files, I would like to sync them in a quasi-automatic manner.
My question is---is there a way in Lisp to add a conditional procedure to detect the running OS? Something like (pseudo-code):
If OS X:
run this and that command;
If Linux:
run that other command;
Fi
Thanks in advance.

Following bmeric's advice, this solution worked for me:
(cond
((string-equal system-type "gnu/linux")
;; window size
(add-to-list 'default-frame-alist '(left . 0))
(add-to-list 'default-frame-alist '(top . 0))
(add-to-list 'default-frame-alist '(height . 32))
(add-to-list 'default-frame-alist '(width . 70))
)
((string-equal system-type "darwin")
;; window size
(add-to-list 'default-frame-alist '(left . 0))
(add-to-list 'default-frame-alist '(top . 0))
(add-to-list 'default-frame-alist '(height . 63))
(add-to-list 'default-frame-alist '(width . 100))
)
)

You can use system-type variable.

Related

Display-buffer-at-bottom to popup a message

I wrote such a function to pop-up a buffer at bottom
(defun popup-new-message (msg)
(interactive
(let ((buffer (generate-new-buffer "pop-message")))
(with-current-buffer buffer
(insert msg))
(display-buffer buffer '(display-buffer-at-bottom . nil)))
))
(popup-new-message "It's time to have a rest.")
When run it, nothing happened.
What's the problem?
I checked that functions of generate-new-buffer and display-message working
#+begin_src emacs-lisp :tangle yes
(let ((buffer (generate-new-buffer "pop-message")))
(print buffer))
#+end_src
#+RESULTS:
: #<buffer pop-message<2>>
Your (interactive S-EXP is not closed correctly.

How get Area of Objects inside a Block/autocad into a variable using lisp

Here is the Code;
(setq Tobjs (vla-Explode Tvlaobj))
(setq ObjectsAsList (vlax-safearray->list (vlax-variant-value Tobjs ) ) )
(foreach % ObjectsAsList
(setq TSublayerNew (vla-get-layer %))
(cond ((eq TSublayerNew TLaynemeShort)
(command "_.AREA" "_O" %)
(setq Teee (getvar 'area))
(setq Ttvm (+ Ttvm Teee))
(princ (strcat (rtos Teee 2 0) " | "))
(setq Tcntr (+ Tcntr 1))
)
)
)
(setq TNumF Ttvm)
(princ (strcat (rtos TNumF 2 2)))
I want to get the Total area for all Region and Solids in Block (Tobjs) into variable TNumF, if it matches the condition,
it give an error: bad argument value: AutoCAD command: #
please help
Thanks in advance
It's because % is entity as vla-object, but command needs entity, so try this code:
(command "_.AREA" "_O" (vlax-vla-object->ename % ))

autocad : script that runs autolisp functions

I've got a working batch-file that runs a script on a bunch of drawings.
The script is supposed to run a lisp-function but that function appears to only run after the main function has ran.
since I don't know much about lisps, I'll try to give the information I have.
the lsp :
(princ "\nLoading AREAS...")
(defun c:areas(); Start the program.
(setvar "cmdecho" 0)
(if (= (getvar "tilemode") 1)
(progn
(command "_.ucs" "_world")
(setq osnp (getvar "osmode"))
(setq laag (getvar "clayer"))
(setvar "osmode" 0)
(setq dimz (getvar "dimzin"))
(setvar "dimzin" 0)
(ge_dellay ladeptmp)
(if (>= (substr (getvar "acadver") 1 2) "15")
(ge_convert)
)
(setq allsel (list (cons 0 "POLYLINE")'(-4 . "<OR")(cons 8 ladeppoly)(cons 8 ladeptraf)'(-4 . "OR>")'(-3 ("COSBI"))))
(setq depsel (list (cons 0 "POLYLINE")(cons 8 ladeppoly)'(-3 ("COSBI"))))
(setq areasel (list (cons 0 "POLYLINE")(cons 8 ladeptraf)'(-3 ("COSBI"))))
(setq textsel (list (cons 0 "TEXT")(cons 8 ladeptext)'(-3 ("COSBI"))))
(setq dcl_area (load_dialog "areas"))
(setq dcl_gen (load_dialog "general"))
(setq intp nil dparea nil seltot nil)
(ar_setdep)
(ar_dia)
(if (= what_next 1)(ar_setlay))
(if (= what_next 2)(ar_check))
(if (= what_next 3)(ar_startcheck))
(unload_dialog dcl_area)
(unload_dialog dcl_gen)
(setvar "osmode" osnp)
(setvar "clayer" laag)
(setvar "dimzin" dimz)
(ge_dellay ladeptmp)
)
(alert "Only allowed in original drawing...")
)
(princ)
)
is followed by a few other (not sure) less important functions, like the ar_dia - which opens a dialog box with buttons to call other functions.
One of the other functions is AR_LIST, which is the one I need to run on each file the batch-file opens, in a script.
the ar_list is a few blocks down and looks like this
(defun ar_list(); Make department/areas list.
(setq sel (ssget "x" allsel))
(if sel
(progn
(setq temp (findfile "template.sqm"))
(if temp
(progn
(command "_.zoom" "_all")
(setq rowlist nil deplist nil)
(setq bestand (open temp "r"))
(setq row (read-line bestand))
(while row
(setq row (read-line bestand))
(if row
(progn
(setq rowlist (cons (strcase (strcat (spatie (substr row 23 14)) "_-")) rowlist))
(setq deplist (cons (strcase (spatie (substr row 23 14))) deplist))
)
)
)
(setq country (ge_dir 3 "Country"))
(ge_dwg)
(if (= (strlen dwgnaam) 9); 3to4storenr
(progn
(setq store (substr dwgnaam (- (strlen dwgnaam) 3))); 3to4storenr
(setq floor (substr dwgnaam (- (strlen dwgnaam) 5) 2)); 3to4storenr
(setq num 0)
(repeat (sslength sel)
(setq depname (cdr (cadadr (assoc -3 (entget (ssname sel num)'("COSBI"))))))
(if (not (wcmatch depname "*`island*"))
(progn
(setq ename (ssname sel num))
(command "_.area" "_a" "_o" ename "")
(ge_puntlist ename)
(setq numpol 0)
(setq selpol (ssget "_wp" puntlist allsel))
(if selpol
(repeat (sslength selpol)
(setq islname (cdr (cadadr (assoc -3 (entget (ssname selpol numpol)'("COSBI"))))))
(if (= islname (strcat depname "-island"))
(command "_s" "_o" (ssname selpol numpol) "")
)
(setq numpol (1+ numpol))
)
)
(command "")
(setq deparea (/ (getvar "area") 1000000))
(if (not (member (strcase depname) deplist))
(progn
(setq deplist (cons (strcase depname) deplist))
(setq rowlist (cons (strcase (strcat depname "_-")) rowlist))
)
)
(setq nummem (- (length deplist)(length (member (strcase depname) deplist))))
(setq deptot (nth nummem rowlist))
(vindpos "_" deptot)
(setq depareaold (substr deptot (+ pos 1)))
(if (/= depareaold "-")
(setq deparea (+ (atof depareaold) deparea))
)
(setq rowlist (subst (strcase (strcat depname "_" (rtos deparea 2 1))) (nth nummem rowlist) rowlist))
)
)
(setq num (1+ num))
)
(command "_.zoom" "_previous")
(setq rowlist (acad_strlsort rowlist)); 13-10-2014
(setq deplist (acad_strlsort deplist)); 13-10-2014
;(setq rowlist (reverse rowlist))
;(setq deplist (reverse deplist))
(ar_write)
)
(alert (strcat "Drawing name " dwgnaam " not correct, must be 9 characters.")); 3to4storenr
)
)
(alert "File TEMPLATE.SQM not found...")
)
)
(alert "No department or traffic found...")
)
)
the script only needs to run this command, close the drawing, and don't save.
so I tried (test.scr)
(ar_list)
quit
n
but that gives me the error:
Command: (ar_list)
bad argument type: stringp nil
I think the ar_list needs something from the defun c:areas , but I don't know what. The ar_list works after entering areas in the command bar.
So I also tried
areas
(ar_list)
quit
n
, but that opened the areas dialog box, does not close it, blocking the loop.
Also, when I cancel the dialog box, the ar_list works, but it again opens the areas dialog box. I think the code repeats itself in the script.
Any help would be very welcome. I received related help on here
stringp nil give us sugestion that some variable which should be text string in fact is nil. Probably because it reads value from dialog control (which is unavaliable while dialog is not active).
there is few places which may cause such problem:
we don't know what happes in ge_dir ge_dwg ge_puntlist vindpos ar_write
ar_write Maybe want to write something to dialog?
variable dwgnaam is used as string but never initialized in this function, (maybe somewhere else it is?)
(setq depname (cdr (cadadr (assoc -3 (entget (ssname sel num)'("COSBI")))))) (if (not (wcmatch depname "*island*")) if selected entity not contains XData "cosbi", it can be problem, but if I'm not wrong, there is other error message.

(wrong-type-argument arrayp nil) on starting emacs

Emacs 24 and Mac OSX Mavericks, and my emacs file is as follows:
(setq debug-on-error t)
;; -- common-lisp compatibility if not added earlier in your .emacs
(require 'cl)
;; -- Tuareg mode -----------------------------------------
;; Add Tuareg to your search path
(add-to-list
'load-path
;; Change the path below to be wherever you've put your tuareg installation.
(expand-file-name "~/.elisp/tuareg"))
(require 'tuareg)
(setq auto-mode-alist
(append '(("\\.ml[ily]?$" . tuareg-mode))
auto-mode-alist))
;; -- Tweaks for OS X -------------------------------------
;; Tweak for problem on OS X where Emacs.app doesn't run the right
;; init scripts when invoking a sub-shell
(cond
((eq window-system 'ns) ; macosx
;; Invoke login shells, so that .profile or .bash_profile is read
(setq shell-command-switch "-lc")))
;; -- opam and utop setup --------------------------------
;; Setup environment variables using opam
(defun opam-vars ()
(let* ((x (shell-command-to-string "opam config env"))
(x (split-string x "\n"))
(x (remove-if (lambda (x) (equal x "")) x))
(x (mapcar (lambda (x) (split-string x ";")) x))
(x (mapcar (lambda (x) (car x)) x))
(x (mapcar (lambda (x) (split-string x "=")) x))
)
x))
(dolist (var (opam-vars))
(setenv (car var) (substring (cadr var) 1 -1)))
;; The following simpler alternative works as of opam 1.1
;; (dolist
;; (var (car (read-from-string
;; (shell-command-to-string "opam config env --sexp"))))
;; (setenv (car var) (cadr var)))
;; Update the emacs path
(setq exec-path (split-string (getenv "PATH") path-separator))
;; Update the emacs load path
(push (concat (getenv "OCAML_TOPLEVEL_PATH")
"/../../share/emacs/site-lisp") load-path)
;; Automatically load utop.el
(autoload 'utop "utop" "Toplevel for OCaml" t)
(autoload 'utop-setup-ocaml-buffer "utop" "Toplevel for OCaml" t)
(add-hook 'tuareg-mode-hook 'utop-setup-ocaml-buffer)
Any help fixing this error would be great. Thanks!
Nots: I do have tuareg mode where I said it is (in~/.elisp/tuareg). I started getting this error after doing the following :
opam install \
async yojson core_extended core_bench \
cohttp async_graphics cryptokit menhir
Not sure whats wrong, but I had basically the same emacs file before installing those and had no issues. Now in emacs utop also doesn't run properly when I try to evaluate an ocaml file.
As noted above:
If I had to guess, I would think that opam config env is now outputting something that is resistant to the manipulations of the function opam-vars
Run Emacs --debug-init and you will get the backtrace.
And bisect that init file, recursively, to narrow things down more.

Emacs Lisp: evaluate variable in alist

This is probably silly but I don't have enough Elisp knowledge to understand what is going on with respect to quoting and evaluation.
Suppose I have this Elisp code:
(add-to-list 'default-frame-alist '(width . 100))
(add-to-list 'default-frame-alist '(height . 50))
It will result in the expected default-frame-alist value:
((height 50)
(width 100))
But now if I have this:
(setq my-frame-width 100)
(setq my-frame-height 50)
(add-to-list 'default-frame-alist '(width . my-frame-width))
(add-to-list 'default-frame-alist '(height . my-frame-height))
It will result in -
((height my-frame-height)
(width my-frame-width))
and, judging from the frame geometry, never evaluates those variables. How do I make the actual values of my-frame-width and height appear in this alist? Do I have too many quotes? But I cannot remove any from the add-to-list evaluations...
Try this:
(setq my-frame-width 100)
(setq my-frame-height 50)
(add-to-list 'default-frame-alist `(width . ,my-frame-width))
(add-to-list 'default-frame-alist `(height . ,my-frame-height))
Using backquote instead of quote allows you to use , to force the evaluation of a parameter.
See the Elisp reference manual. Type C-x info, search for the elisp reference manual, then search for backquote within that.
As an alternative to the backquote operator in mch's answer, you can use the cons function. This function will build a cons cell with the first argument as its car and the second argument as its cdr. The dotted pair notation in your code is shorthand for this. So we could rewrite your code this way:
(setq my-frame-width 100)
(setq my-frame-height 50)
(add-to-list 'default-frame-alist (cons 'width my-frame-width))
(add-to-list 'default-frame-alist (cons 'height my-frame-height))
This way, you can quote the symbols you want to appear literally (like width and height) and evaluate the symbols whose values you need (like my-frame-width and my-frame-height). I prefer this method because it is more straight-forward. However, that is certainly a matter of opinion. Here is some more information on cons and list for future reference.

Resources