/* * Get HOME directory. */ astr get_home_dir (void) { char *s = getenv ("HOME"); if (s != NULL) return astr_new_cstr (s); return NULL; }
/* * Get HOME directory. */ astr get_home_dir(void) { char *s = getenv("HOME"); astr as; if (s != NULL && strlen(s) < PATH_MAX) as = astr_new_cstr(s); else as = agetcwd(); return as; }
/* Safe getcwd */ astr agetcwd(void) { size_t len = PATH_MAX; char *buf = (char *)zmalloc(len); char *res; astr as; while ((res = getcwd(buf, len)) == NULL && errno == ERANGE) { len *= 2; buf = zrealloc(buf, len); } /* If there was an error, return the empty string */ if (res == NULL) *buf = '\0'; as = astr_new_cstr(buf); free(buf); return as; }
/* * Return the current directory. */ static astr get_current_dir(void) { astr buf; int p; if (cur_bp->filename != NULL) /* If the current buffer has a filename, get the current directory name from it. */ buf = astr_new_cstr(cur_bp->filename); else { /* Get the current directory name from the system. */ buf = agetcwd(); if (astr_len(buf) != 0 && *astr_char(buf, -1) != '/') astr_cat_char(buf, '/'); } p = astr_rfind_cstr(buf, "/"); astr_truncate(buf, p + 1); return buf; }
/* * Return a `~/foo' like path if the user is under his home directory, * else the unmodified path. */ astr compact_path (astr path) { struct passwd *pw = getpwuid (getuid ()); if (pw != NULL) { /* Replace `/userhome/' (if found) with `~/'. */ size_t homelen = strlen (pw->pw_dir); if (homelen > 0 && pw->pw_dir[homelen - 1] == '/') homelen--; if (astr_len (path) > homelen && !strncmp (pw->pw_dir, astr_cstr (path), homelen) && astr_get (path, homelen) == '/') astr_cpy (path, astr_cat_cstr (astr_new_cstr ("~/"), astr_cstr (path) + homelen + 1)); } return path; }
bool find_file (const char *filename) { Buffer bp; for (bp = head_bp; bp != NULL; bp = get_buffer_next (bp)) if (get_buffer_filename (bp) != NULL && STREQ (get_buffer_filename (bp), filename)) break; if (bp == NULL) { if (exist_file (filename) && !is_regular_file (filename)) { minibuf_error ("File exists but could not be read"); return false; } else { bp = buffer_new (); set_buffer_names (bp, filename); set_buffer_dir (bp, astr_new_cstr (dir_name (filename))); estr es = estr_readf (filename); if (es) set_buffer_readonly (bp, !check_writable (filename)); else es = estr_new_astr (astr_new ()); set_buffer_text (bp, es); /* Reset undo history. */ set_buffer_next_undop (bp, NULL); set_buffer_last_undop (bp, NULL); set_buffer_modified (bp, false); } } switch_to_buffer (bp); thisflag |= FLAG_NEED_RESYNC; return true; }
/* * Return the current directory, if available. */ astr agetcwd (void) { char *s = xgetcwd (); return astr_new_cstr (s ? s : ""); }
static astr do_minibuf_read (const char *prompt, const char *value, size_t pos, Completion * cp, History * hp) { static int overwrite_mode = 0; int c, thistab, lasttab = -1; size_t prompt_len; char *s; astr as = astr_new_cstr (value), saved = NULL; prompt_len = strlen (prompt); if (pos == SIZE_MAX) pos = astr_len (as); for (;;) { switch (lasttab) { case COMPLETION_MATCHEDNONUNIQUE: s = " [Complete, but not unique]"; break; case COMPLETION_NOTMATCHED: s = " [No match]"; break; case COMPLETION_MATCHED: s = " [Sole completion]"; break; default: s = ""; } draw_minibuf_read (prompt, astr_cstr (as), prompt_len, s, pos); thistab = -1; switch (c = getkey ()) { case KBD_NOKEY: break; case KBD_CTRL | 'z': FUNCALL (suspend_emacs); break; case KBD_RET: term_move (term_height () - 1, 0); term_clrtoeol (); if (saved) astr_delete (saved); return as; case KBD_CANCEL: term_move (term_height () - 1, 0); term_clrtoeol (); if (saved) astr_delete (saved); astr_delete (as); return NULL; case KBD_CTRL | 'a': case KBD_HOME: pos = 0; break; case KBD_CTRL | 'e': case KBD_END: pos = astr_len (as); break; case KBD_CTRL | 'b': case KBD_LEFT: if (pos > 0) --pos; else ding (); break; case KBD_CTRL | 'f': case KBD_RIGHT: if (pos < astr_len (as)) ++pos; else ding (); break; case KBD_CTRL | 'k': /* FIXME: do kill-register save. */ if (pos < astr_len (as)) astr_truncate (as, pos); else ding (); break; case KBD_BS: if (pos > 0) astr_remove (as, --pos, 1); else ding (); break; case KBD_CTRL | 'd': case KBD_DEL: if (pos < astr_len (as)) astr_remove (as, pos, 1); else ding (); break; case KBD_INS: overwrite_mode = overwrite_mode ? 0 : 1; break; case KBD_META | 'v': case KBD_PGUP: if (cp == NULL) { ding (); break; } if (get_completion_flags (cp) & CFLAG_POPPEDUP) { completion_scroll_down (); thistab = lasttab; } break; case KBD_CTRL | 'v': case KBD_PGDN: if (cp == NULL) { ding (); break; } if (get_completion_flags (cp) & CFLAG_POPPEDUP) { completion_scroll_up (); thistab = lasttab; } break; case KBD_UP: case KBD_META | 'p': if (hp) { const char *elem = previous_history_element (hp); if (elem) { if (!saved) saved = astr_cpy (astr_new (), as); astr_cpy_cstr (as, elem); } } break; case KBD_DOWN: case KBD_META | 'n': if (hp) { const char *elem = next_history_element (hp); if (elem) astr_cpy_cstr (as, elem); else if (saved) { astr_cpy (as, saved); astr_delete (saved); saved = NULL; } } break; case KBD_TAB: got_tab: if (cp == NULL) { ding (); break; } if (lasttab != -1 && lasttab != COMPLETION_NOTMATCHED && get_completion_flags (cp) & CFLAG_POPPEDUP) { completion_scroll_up (); thistab = lasttab; } else { astr bs = astr_new (); astr_cpy (bs, as); thistab = completion_try (cp, bs, true); astr_delete (bs); switch (thistab) { case COMPLETION_MATCHED: case COMPLETION_MATCHEDNONUNIQUE: case COMPLETION_NONUNIQUE: { bs = astr_new (); if (get_completion_flags (cp) & CFLAG_FILENAME) astr_cat (bs, get_completion_path (cp)); astr_ncat_cstr (bs, get_completion_match (cp), get_completion_matchsize (cp)); if (strncmp (astr_cstr (as), astr_cstr (bs), astr_len (bs)) != 0) thistab = -1; astr_delete (as); as = bs; pos = astr_len (as); break; } case COMPLETION_NOTMATCHED: ding (); } } break; case ' ': if (cp != NULL) goto got_tab; /* FALLTHROUGH */ default: if (c > 255 || !isprint (c)) { ding (); break; } astr_insert_char (as, pos++, c); if (overwrite_mode && pos != astr_len (as)) astr_remove (as, pos, 1); } lasttab = thistab; } }